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