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