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