]> git.ipfire.org Git - thirdparty/openssl.git/blame - include/openssl/trace.h
Adapt BN_CTX_DEBUG to the new generic trace API
[thirdparty/openssl.git] / include / openssl / trace.h
CommitLineData
2390c573
RL
1/*
2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10#ifndef OSSL_TRACE_H
11# define OSSL_TRACE_H
12
13# include <stdarg.h>
14
15# include <openssl/bio.h>
16
17# ifdef __cplusplus
18extern "C" {
19# endif
20
21/*
22 * TRACE CATEGORIES
23 */
24
25/*
26 * The trace messages of the OpenSSL libraries are organized into different
27 * categories. For every trace category, the application can register a separate
28 * tracer callback. When a callback is registered, a so called trace channel is
29 * created for this category. This channel consists essentially of an internal
30 * BIO which sends all trace output it receives to the registered application
31 * callback.
32 *
33 * The ANY category is used as a fallback category.
34 */
35# define OSSL_TRACE_CATEGORY_ANY 0 /* The fallback */
5c641735
RL
36# define OSSL_TRACE_CATEGORY_INIT 1
37# define OSSL_TRACE_CATEGORY_TLS 2
38# define OSSL_TRACE_CATEGORY_TLS_CIPHER 3
f4db05df 39# define OSSL_TRACE_CATEGORY_ENGINE_CONF 4
f272be67 40# define OSSL_TRACE_CATEGORY_ENGINE_TABLE 5
f518e3e8 41# define OSSL_TRACE_CATEGORY_ENGINE_REF_COUNT 6
3a9b3d2d 42# define OSSL_TRACE_CATEGORY_PKCS5V2 7
a902e43d 43# define OSSL_TRACE_CATEGORY_PKCS12_KEYGEN 8
5f8a5f46
RL
44# define OSSL_TRACE_CATEGORY_PKCS12_DECRYPT 9
45# define OSSL_TRACE_CATEGORY_X509V3_POLICY 10
6e810f2d
RL
46# define OSSL_TRACE_CATEGORY_BN_CTX 11
47# define OSSL_TRACE_CATEGORY_NUM 12
2390c573
RL
48
49/* Returns the trace category number for the given |name| */
50int OSSL_trace_get_category_num(const char *name);
51
52/* Returns the trace category name for the given |num| */
53const char *OSSL_trace_get_category_name(int num);
54
55/*
56 * TRACE CONSUMERS
57 */
58
59/*
60 * Enables tracing for the given |category| by providing a BIO sink
61 * as |channel|. If a null pointer is passed as |channel|, an existing
62 * trace channel is removed and tracing for the category is disabled.
63 *
64 * Returns 1 on success and 0 on failure
65 */
66int OSSL_trace_set_channel(int category, BIO* channel);
67
68/*
69 * Attach a prefix and a suffix to the given |category|, to be printed at the
70 * beginning and at the end of each trace output group, i.e. when
71 * OSSL_trace_begin() and OSSL_trace_end() are called.
72 * If a null pointer is passed as argument, the existing prefix or suffix is
73 * removed.
74 *
75 * They return 1 on success and 0 on failure
76 */
77int OSSL_trace_set_prefix(int category, const char *prefix);
78int OSSL_trace_set_suffix(int category, const char *suffix);
79
80/*
81 * OSSL_trace_cb is the type tracing callback provided by the application.
82 * It MUST return the number of bytes written, or 0 on error (in other words,
83 * it can never write zero bytes).
84 *
85 * The |buffer| will always contain text, which may consist of several lines.
86 * The |data| argument points to whatever data was provided by the application
87 * when registering the tracer function.
88 *
89 * The |category| number is given, as well as a |cmd| number, described below.
90 */
91typedef size_t (*OSSL_trace_cb)(const char *buffer, size_t count,
92 int category, int cmd, void *data);
93/*
94 * Possible |cmd| numbers.
95 */
96# define OSSL_TRACE_CTRL_BEGIN 0
97# define OSSL_TRACE_CTRL_DURING 1
98# define OSSL_TRACE_CTRL_END 2
99
100/*
101 * Enables tracing for the given |category| by creating an internal
102 * trace channel which sends the output to the given |callback|.
103 * If a null pointer is passed as callback, an existing trace channel
104 * is removed and tracing for the category is disabled.
105 *
106 * NOTE: OSSL_trace_set_channel() and OSSL_trace_set_callback() are mutually
107 * exclusive.
108 *
109 * Returns 1 on success and 0 on failure
110 */
111int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data);
112
113/*
114 * TRACE PRODUCERS
115 */
116
117/*
118 * Returns 1 if tracing for the specified category is enabled, otherwise 0
119 */
120int OSSL_trace_enabled(int category);
121
122/*
123 * Wrap a group of tracing output calls. OSSL_trace_begin() locks tracing and
124 * returns the trace channel associated with the given category, or NULL if no
125 * channel is associated with the category. OSSL_trace_end() unlocks tracing.
126 *
127 * Usage:
128 *
129 * BIO *out;
130 * if ((out = OSSL_trace_begin(category)) != NULL) {
131 * ...
132 * BIO_fprintf(out, ...);
133 * ...
134 * OSSL_trace_end(category, out);
135 * }
136 *
137 * See also the convenience macros OSSL_TRACE_BEGIN and OSSL_TRACE_END below.
138 */
139BIO *OSSL_trace_begin(int category);
140void OSSL_trace_end(int category, BIO *channel);
141
142/*
143 * OSSL_TRACE* Convenience Macros
144 */
145
146/*
147 * When the tracing feature is disabled, these macros are defined to
148 * produce dead code, which a good compiler should eliminate.
149 */
150
151/*
152 * OSSL_TRACE_BEGIN, OSSL_TRACE_END - Define a Trace Group
153 *
154 * These two macros can be used to create a block which is executed only
155 * if the corresponding trace category is enabled. Inside this block, a
156 * local variable named |trc_out| is defined, which points to the channel
157 * associated with the given trace category.
158 *
159 * Usage: (using 'TLS' as an example category)
160 *
161 * OSSL_TRACE_BEGIN(TLS) {
162 *
163 * BIO_fprintf(trc_out, ... );
164 *
165 * } OSSL_TRACE_END(TLS);
166 *
167 *
168 * This expands to the following code
169 *
170 * do {
171 * BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_TLS);
172 * if (trc_out != NULL) {
173 * ...
174 * BIO_fprintf(trc_out, ...);
175 * }
176 * OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
177 * } while (0);
178 *
179 * The use of the inner '{...}' group and the trailing ';' is enforced
180 * by the definition of the macros in order to make the code look as much
181 * like C code as possible.
182 *
183 * Before returning from inside the trace block, it is necessary to
184 * call OSSL_TRACE_CANCEL(category).
185 */
186
16a9d374
RL
187# ifndef OPENSSL_NO_TRACE
188
189# define OSSL_TRACE_BEGIN(category) \
2390c573
RL
190 do { \
191 BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_##category); \
192 \
193 if (trc_out != NULL)
194
16a9d374 195# define OSSL_TRACE_END(category) \
2390c573
RL
196 OSSL_trace_end(OSSL_TRACE_CATEGORY_##category, trc_out); \
197 } while (0)
198
16a9d374 199# define OSSL_TRACE_CANCEL(category) \
2390c573
RL
200 OSSL_trace_end(OSSL_TRACE_CATEGORY_##category, trc_out) \
201
16a9d374
RL
202# else
203
204# define OSSL_TRACE_BEGIN(category) \
205 do { \
206 BIO *trc_out = NULL; \
207 if (0)
208
209# define OSSL_TRACE_END(category) \
210 } while(0)
211
212# define OSSL_TRACE_CANCEL(category) \
213 ((void)0)
214
215# endif
216
2390c573
RL
217/*
218 * OSSL_TRACE_ENABLED() - Check whether tracing is enabled for |category|
219 *
220 * Usage:
221 *
222 * if (OSSL_TRACE_ENABLED(TLS)) {
223 * ...
224 * }
225 */
16a9d374 226# ifndef OPENSSL_NO_TRACE
2390c573 227
16a9d374 228# define OSSL_TRACE_ENABLED(category) \
2390c573
RL
229 OSSL_trace_enabled(OSSL_TRACE_CATEGORY_##category)
230
16a9d374
RL
231# else
232
233# define OSSL_TRACE_ENABLED(category) (0)
234
235# endif
236
2390c573
RL
237/*
238 * OSSL_TRACE*() - OneShot Trace Macros
239 *
240 * These macros are intended to produce a simple printf-style trace output.
241 * Unfortunately, C90 macros don't support variable arguments, so the
242 * "vararg" OSSL_TRACEV() macro has a rather weird usage pattern:
243 *
244 * OSSL_TRACEV(category, (trc_out, "format string", ...args...));
245 *
246 * Where 'channel' is the literal symbol of this name, not a variable.
247 * For that reason, it is currently not intended to be used directly,
248 * but only as helper macro for the other oneshot trace macros
249 * OSSL_TRACE(), OSSL_TRACE1(), OSSL_TRACE2(), ...
250 *
251 * Usage:
252 *
253 * OSSL_TRACE(INIT, "Hello world!\n");
254 * OSSL_TRACE1(TLS, "The answer is %d\n", 42);
255 * OSSL_TRACE2(TLS, "The ultimate question to answer %d is '%s'\n",
256 * 42, "What do you get when you multiply six by nine?");
257 */
258
259# define OSSL_TRACEV(category, args) \
260 OSSL_TRACE_BEGIN(category) \
261 BIO_printf args; \
262 OSSL_TRACE_END(category)
263
264# define OSSL_TRACE(category, text) \
265 OSSL_TRACEV(category, (trc_out, "%s", text))
266
267# define OSSL_TRACE1(category, format, arg1) \
268 OSSL_TRACEV(category, (trc_out, format, arg1))
269# define OSSL_TRACE2(category, format, arg1, arg2) \
270 OSSL_TRACEV(category, (trc_out, format, arg1, arg2))
271# define OSSL_TRACE3(category, format, arg1, arg2, arg3) \
272 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3))
273# define OSSL_TRACE4(category, format, arg1, arg2, arg3, arg4) \
274 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4))
275# define OSSL_TRACE5(category, format, arg1, arg2, arg3, arg4, arg5) \
276 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5))
277# define OSSL_TRACE6(category, format, arg1, arg2, arg3, arg4, arg5, arg6) \
278 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6))
279# define OSSL_TRACE7(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
280 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7))
281# define OSSL_TRACE8(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
282 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
283# define OSSL_TRACE9(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
284 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))
285
286# ifdef __cplusplus
287}
288# endif
289
290#endif