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