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