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