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