3 =for openssl foreign manual atexit(3)
7 OSSL_trace_set_channel, OSSL_trace_set_prefix, OSSL_trace_set_suffix,
8 OSSL_trace_set_callback, OSSL_trace_cb - Enabling trace output
12 #include <openssl/trace.h>
14 typedef size_t (*OSSL_trace_cb)(const char *buf, size_t cnt,
15 int category, int cmd, void *data);
17 void OSSL_trace_set_channel(int category, BIO *bio);
18 void OSSL_trace_set_prefix(int category, const char *prefix);
19 void OSSL_trace_set_suffix(int category, const char *suffix);
20 void OSSL_trace_set_callback(int category, OSSL_trace_cb cb, void *data);
24 If available (see L</NOTES> below), the application can request
25 internal trace output.
26 This output comes in form of free text for humans to read.
28 The trace output is divided into categories which can be
30 Every category can be enabled individually by attaching a so called
31 I<trace channel> to it, which in the simplest case is just a BIO object
32 to which the application can write the tracing output for this category.
33 Alternatively, the application can provide a tracer callback in order to
34 get more finegrained trace information. This callback will be wrapped
35 internally by a dedicated BIO object.
37 For the tracing code, both trace channel types are indistinguishable.
38 These are called a I<simple trace channel> and a I<callback trace channel>,
43 OSSL_trace_set_channel() is used to enable the given trace C<category>
44 by attaching the B<BIO> I<bio> object as (simple) trace channel.
46 OSSL_trace_set_prefix() and OSSL_trace_set_suffix() can be used to add
47 an extra line for each channel, to be output before and after group of
49 What constitutes an output group is decided by the code that produces
51 The lines given here are considered immutable; for more dynamic
52 tracing prefixes, consider setting a callback with
53 OSSL_trace_set_callback() instead.
55 OSSL_trace_set_callback() is used to enable the given trace
56 I<category> by giving it the tracer callback I<cb> with the associated
57 data I<data>, which will simply be passed through to I<cb> whenever
58 it's called. The callback function is internally wrapped by a
59 dedicated BIO object, the so called I<callback trace channel>.
60 This should be used when it's desirable to do form the trace output to
61 something suitable for application needs where a prefix and suffix
64 OSSL_trace_set_channel() and OSSL_trace_set_callback() are mutually
65 exclusive, calling one of them will clear whatever was set by the
68 Calling OSSL_trace_set_channel() with NULL for I<channel> or
69 OSSL_trace_set_callback() with NULL for I<cb> disables tracing for
70 the given I<category>.
74 The tracer callback must return a B<size_t>, which must be zero on
75 error and otherwise return the number of bytes that were output.
76 It receives a text buffer I<buf> with I<cnt> bytes of text, as well as
77 the I<category>, a control number I<cmd>, and the I<data> that was
78 passed to OSSL_trace_set_callback().
80 The possible control numbers are:
84 =item B<OSSL_TRACE_CTRL_BEGIN>
86 The callback is called from OSSL_trace_begin(), which gives the
87 callback the possibility to output a dynamic starting line, or set a
88 prefix that should be output at the beginning of each line, or
91 =item B<OSSL_TRACE_CTRL_WRITE>
93 This callback is called whenever data is written to the BIO by some
94 regular BIO output routine.
95 An arbitrary number of B<OSSL_TRACE_CTRL_WRITE> callbacks can occur
96 inside a group marked by a pair of B<OSSL_TRACE_CTRL_BEGIN> and
97 B<OSSL_TRACE_CTRL_END> calls, but never outside such a group.
99 =item B<OSSL_TRACE_CTRL_END>
101 The callback is called from OSSL_trace_end(), which gives the callback
102 the possibility to output a dynamic ending line, or reset the line
103 prefix that was set with B<OSSL_TRACE_CTRL_BEGIN>, or something other.
107 =head2 Trace categories
109 The trace categories are simple numbers available through macros.
113 =item B<OSSL_TRACE_CATEGORY_TRACE>
115 Traces the OpenSSL trace API itself.
117 More precisely, this will generate trace output any time a new
120 =item B<OSSL_TRACE_CATEGORY_INIT>
122 Traces OpenSSL library initialization and cleanup.
124 This needs special care, as OpenSSL will do automatic cleanup after
125 exit from C<main()>, and any tracing output done during this cleanup
126 will be lost if the tracing channel or callback were cleaned away
128 A suggestion is to make such cleanup part of a function that's
129 registered very early with L<atexit(3)>.
131 =item B<OSSL_TRACE_CATEGORY_TLS>
133 Traces the TLS/SSL protocol.
135 =item B<OSSL_TRACE_CATEGORY_TLS_CIPHER>
137 Traces the ciphers used by the TLS/SSL protocol.
139 =item B<OSSL_TRACE_CATEGORY_ENGINE_TABLE>
141 Traces the ENGINE algorithm table selection.
143 More precisely, functions like ENGINE_get_pkey_asn1_meth_engine(),
144 ENGINE_get_pkey_meth_engine(), ENGINE_get_cipher_engine(),
145 ENGINE_get_digest_engine(), will generate trace summaries of the
146 handling of internal tables.
148 =item B<OSSL_TRACE_CATEGORY_ENGINE_REF_COUNT>
150 Traces the ENGINE reference counting.
152 More precisely, both reference counts in the ENGINE structure will be
153 monitored with a line of trace output generated for each change.
155 =item B<OSSL_TRACE_CATEGORY_PKCS5V2>
157 Traces PKCS#5 v2 key generation.
159 =item B<OSSL_TRACE_CATEGORY_PKCS12_KEYGEN>
161 Traces PKCS#12 key generation.
163 =item B<OSSL_TRACE_CATEGORY_PKCS12_DECRYPT>
165 Traces PKCS#12 decryption.
167 =item B<OSSL_TRACE_CATEGORY_X509V3_POLICY>
169 Traces X509v3 policy processing.
171 More precisely, this generates the complete policy tree at various
172 point during evaluation.
174 =item B<OSSL_TRACE_CATEGORY_BN_CTX>
176 Traces BIGNUM context operations.
178 =item B<OSSL_TRACE_CATEGORY_CONF>
180 Traces details about the provider and engine configuration.
184 There is also B<OSSL_TRACE_CATEGORY_ALL>, which works as a fallback
185 and can be used to get I<all> trace output.
187 Note, however, that in this case all trace output will effectively be
188 associated with the 'ALL' category, which is undesirable if the
189 application intends to include the category name in the trace output.
190 In this case it is better to register separate channels for each
191 trace category instead.
195 OSSL_trace_set_channel(), OSSL_trace_set_prefix(),
196 OSSL_trace_set_suffix(), and OSSL_trace_set_callback() return 1 on
197 success, or 0 on failure.
201 In all examples below, the trace producing code is assumed to be
205 const char bar[] = { 0, 1, 2, 3, 4, 5, 6, 7,
206 8, 9, 10, 11, 12, 13, 14, 15 };
208 OSSL_TRACE_BEGIN(TLS) {
209 BIO_puts(trc_out, "foo: ");
210 BIO_printf(trc_out, "%d\n", foo);
211 BIO_dump(trc_out, bar, sizeof(bar));
212 } OSSL_TRACE_END(TLS);
214 =head2 Simple example
216 An example with just a channel and constant prefix / suffix.
218 int main(int argc, char *argv[])
220 BIO *err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
221 OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_SSL, err);
222 OSSL_trace_set_prefix(OSSL_TRACE_CATEGORY_SSL, "BEGIN TRACE[TLS]");
223 OSSL_trace_set_suffix(OSSL_TRACE_CATEGORY_SSL, "END TRACE[TLS]");
228 When the trace producing code above is performed, this will be output
233 0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................
236 =head2 Advanced example
238 This example uses the callback, and depends on pthreads functionality.
240 static size_t cb(const char *buf, size_t cnt,
241 int category, int cmd, void *vdata)
244 const char *label = NULL;
247 case OSSL_TRACE_CTRL_BEGIN:
250 case OSSL_TRACE_CTRL_END:
261 tid.tid = pthread_self();
262 BIO_printf(bio, "%s TRACE[%s]:%lx\n",
263 label, OSSL_trace_get_category_name(category), tid.ltid);
265 return (size_t)BIO_puts(bio, buf);
268 int main(int argc, char *argv[])
270 BIO *err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
271 OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_SSL, cb, err);
276 The output is almost the same as for the simple example above.
278 BEGIN TRACE[TLS]:7f9eb0193b80
280 0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................
281 END TRACE[TLS]:7f9eb0193b80
285 =head2 Configure Tracing
287 By default, the OpenSSL library is built with tracing disabled. To
288 use the tracing functionality documented here, it is therefore
289 necessary to configure and build OpenSSL with the 'enable-trace' option.
291 When the library is built with tracing disabled, the macro
292 B<OPENSSL_NO_TRACE> is defined in F<< <openssl/opensslconf.h> >> and all
293 functions described here are inoperational, i.e. will do nothing.
297 OSSL_trace_set_channel(), OSSL_trace_set_prefix(),
298 OSSL_trace_set_suffix(), and OSSL_trace_set_callback() were all added
303 Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
305 Licensed under the Apache License 2.0 (the "License"). You may not use
306 this file except in compliance with the License. You can obtain a copy
307 in the file LICENSE in the source distribution or at
308 L<https://www.openssl.org/source/license.html>.