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