2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
15 # include <openssl/bio.h>
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
33 * The ANY category is used as a fallback category.
35 # define OSSL_TRACE_CATEGORY_ANY 0 /* The fallback */
36 # define OSSL_TRACE_CATEGORY_NUM 1
38 /* Returns the trace category number for the given |name| */
39 int OSSL_trace_get_category_num(const char *name);
41 /* Returns the trace category name for the given |num| */
42 const char *OSSL_trace_get_category_name(int num);
49 * Enables tracing for the given |category| by providing a BIO sink
50 * as |channel|. If a null pointer is passed as |channel|, an existing
51 * trace channel is removed and tracing for the category is disabled.
53 * Returns 1 on success and 0 on failure
55 int OSSL_trace_set_channel(int category, BIO* channel);
58 * Attach a prefix and a suffix to the given |category|, to be printed at the
59 * beginning and at the end of each trace output group, i.e. when
60 * OSSL_trace_begin() and OSSL_trace_end() are called.
61 * If a null pointer is passed as argument, the existing prefix or suffix is
64 * They return 1 on success and 0 on failure
66 int OSSL_trace_set_prefix(int category, const char *prefix);
67 int OSSL_trace_set_suffix(int category, const char *suffix);
70 * OSSL_trace_cb is the type tracing callback provided by the application.
71 * It MUST return the number of bytes written, or 0 on error (in other words,
72 * it can never write zero bytes).
74 * The |buffer| will always contain text, which may consist of several lines.
75 * The |data| argument points to whatever data was provided by the application
76 * when registering the tracer function.
78 * The |category| number is given, as well as a |cmd| number, described below.
80 typedef size_t (*OSSL_trace_cb)(const char *buffer, size_t count,
81 int category, int cmd, void *data);
83 * Possible |cmd| numbers.
85 # define OSSL_TRACE_CTRL_BEGIN 0
86 # define OSSL_TRACE_CTRL_DURING 1
87 # define OSSL_TRACE_CTRL_END 2
90 * Enables tracing for the given |category| by creating an internal
91 * trace channel which sends the output to the given |callback|.
92 * If a null pointer is passed as callback, an existing trace channel
93 * is removed and tracing for the category is disabled.
95 * NOTE: OSSL_trace_set_channel() and OSSL_trace_set_callback() are mutually
98 * Returns 1 on success and 0 on failure
100 int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data);
107 * Returns 1 if tracing for the specified category is enabled, otherwise 0
109 int OSSL_trace_enabled(int category);
112 * Wrap a group of tracing output calls. OSSL_trace_begin() locks tracing and
113 * returns the trace channel associated with the given category, or NULL if no
114 * channel is associated with the category. OSSL_trace_end() unlocks tracing.
119 * if ((out = OSSL_trace_begin(category)) != NULL) {
121 * BIO_fprintf(out, ...);
123 * OSSL_trace_end(category, out);
126 * See also the convenience macros OSSL_TRACE_BEGIN and OSSL_TRACE_END below.
128 BIO *OSSL_trace_begin(int category);
129 void OSSL_trace_end(int category, BIO *channel);
132 * OSSL_TRACE* Convenience Macros
136 * When the tracing feature is disabled, these macros are defined to
137 * produce dead code, which a good compiler should eliminate.
141 * OSSL_TRACE_BEGIN, OSSL_TRACE_END - Define a Trace Group
143 * These two macros can be used to create a block which is executed only
144 * if the corresponding trace category is enabled. Inside this block, a
145 * local variable named |trc_out| is defined, which points to the channel
146 * associated with the given trace category.
148 * Usage: (using 'TLS' as an example category)
150 * OSSL_TRACE_BEGIN(TLS) {
152 * BIO_fprintf(trc_out, ... );
154 * } OSSL_TRACE_END(TLS);
157 * This expands to the following code
160 * BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_TLS);
161 * if (trc_out != NULL) {
163 * BIO_fprintf(trc_out, ...);
165 * OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
168 * The use of the inner '{...}' group and the trailing ';' is enforced
169 * by the definition of the macros in order to make the code look as much
170 * like C code as possible.
172 * Before returning from inside the trace block, it is necessary to
173 * call OSSL_TRACE_CANCEL(category).
176 # define OSSL_TRACE_BEGIN(category) \
178 BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_##category); \
182 # define OSSL_TRACE_END(category) \
183 OSSL_trace_end(OSSL_TRACE_CATEGORY_##category, trc_out); \
186 # define OSSL_TRACE_CANCEL(category) \
187 OSSL_trace_end(OSSL_TRACE_CATEGORY_##category, trc_out) \
190 * OSSL_TRACE_ENABLED() - Check whether tracing is enabled for |category|
194 * if (OSSL_TRACE_ENABLED(TLS)) {
199 # define OSSL_TRACE_ENABLED(category) \
200 OSSL_trace_enabled(OSSL_TRACE_CATEGORY_##category)
203 * OSSL_TRACE*() - OneShot Trace Macros
205 * These macros are intended to produce a simple printf-style trace output.
206 * Unfortunately, C90 macros don't support variable arguments, so the
207 * "vararg" OSSL_TRACEV() macro has a rather weird usage pattern:
209 * OSSL_TRACEV(category, (trc_out, "format string", ...args...));
211 * Where 'channel' is the literal symbol of this name, not a variable.
212 * For that reason, it is currently not intended to be used directly,
213 * but only as helper macro for the other oneshot trace macros
214 * OSSL_TRACE(), OSSL_TRACE1(), OSSL_TRACE2(), ...
218 * OSSL_TRACE(INIT, "Hello world!\n");
219 * OSSL_TRACE1(TLS, "The answer is %d\n", 42);
220 * OSSL_TRACE2(TLS, "The ultimate question to answer %d is '%s'\n",
221 * 42, "What do you get when you multiply six by nine?");
224 # define OSSL_TRACEV(category, args) \
225 OSSL_TRACE_BEGIN(category) \
227 OSSL_TRACE_END(category)
229 # define OSSL_TRACE(category, text) \
230 OSSL_TRACEV(category, (trc_out, "%s", text))
232 # define OSSL_TRACE1(category, format, arg1) \
233 OSSL_TRACEV(category, (trc_out, format, arg1))
234 # define OSSL_TRACE2(category, format, arg1, arg2) \
235 OSSL_TRACEV(category, (trc_out, format, arg1, arg2))
236 # define OSSL_TRACE3(category, format, arg1, arg2, arg3) \
237 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3))
238 # define OSSL_TRACE4(category, format, arg1, arg2, arg3, arg4) \
239 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4))
240 # define OSSL_TRACE5(category, format, arg1, arg2, arg3, arg4, arg5) \
241 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5))
242 # define OSSL_TRACE6(category, format, arg1, arg2, arg3, arg4, arg5, arg6) \
243 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6))
244 # define OSSL_TRACE7(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
245 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7))
246 # define OSSL_TRACE8(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
247 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
248 # define OSSL_TRACE9(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
249 OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))