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