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