5 OSSL_trace_set_channel, OSSL_trace_set_prefix, OSSL_trace_set_suffix,
6 OSSL_trace_set_callback, OSSL_trace_cb - Enabling trace output
10 #include <openssl/trace.h>
12 typedef size_t (*OSSL_trace_cb)(const char *buf, size_t cnt,
13 int category, int cmd, void *data);
15 void OSSL_trace_set_channel(int category, BIO *bio);
16 void OSSL_trace_set_prefix(int category, const char *prefix);
17 void OSSL_trace_set_suffix(int category, const char *suffix);
18 void OSSL_trace_set_callback(int category, OSSL_trace_cb cb, void *data);
22 If available (see L</NOTES> below), the application can request
23 internal trace output.
24 This output comes in form of free text for humans to read.
26 The trace output is divided into categories which can be
28 Every category can be enabled individually by attaching a so called
29 I<trace channel> to it, which in the simplest case is just a BIO object
30 to which the application can write the tracing output for this category.
31 Alternatively, the application can provide a tracer callback in order to
32 get more finegrained trace information. This callback will be wrapped
33 internally by a dedicated BIO object.
35 For the tracing code, both trace channel types are indistinguishable.
36 These are called a I<simple trace channel> and a I<callback trace channel>,
41 OSSL_trace_set_channel() is used to enable the given trace C<category>
42 by attaching the B<BIO> C<bio> object as (simple) trace channel.
44 OSSL_trace_set_prefix() and OSSL_trace_set_suffix() can be used to add
45 an extra line for each channel, to be output before and after group of
47 What constitues an output group is decided by the code that produces
49 The lines given here are considered immutable; for more dynamic
50 tracing prefixes, consider setting a callback with
51 OSSL_trace_set_callback() instead.
53 OSSL_trace_set_callback() is used to enable the given trace
54 C<category> by giving it the tracer callback C<cb> with the associated
55 data C<data>, which will simply be passed through to C<cb> whenever
56 it's called. The callback function is internally wrapped by a
57 dedicated BIO object, the so called I<callback trace channel>.
58 This should be used when it's desirable to do form the trace output to
59 something suitable for application needs where a prefix and suffix
62 OSSL_trace_set_channel() and OSSL_trace_set_callback() are mutually
63 exclusive, calling one of them will clear whatever was set by the
66 Calling OSSL_trace_set_channel() with C<NULL> for C<channel> or
67 OSSL_trace_set_callback() with C<NULL> for C<cb> disables tracing for
72 The tracer callback must return a C<size_t>, which must be zero on
73 error and otherwise return the number of bytes that were output.
74 It receives a text buffer C<buf> with C<cnt> bytes of text, as well as
75 the C<category>, a control number C<cmd>, and the C<data> that was
76 passed to OSSL_trace_set_callback().
78 The possible control numbers are:
82 =item C<OSSL_TRACE_CTRL_BEGIN>
84 The callback is called from OSSL_trace_begin(), which gives the
85 callback the possibility to output a dynamic starting line, or set a
86 prefix that should be output at the beginning of each line, or
89 =item C<OSSL_TRACE_CTRL_WRITE>
91 This callback is called whenever data is written to the BIO by some
92 regular BIO output routine.
93 An arbitrary number of C<OSSL_TRACE_CTRL_WRITE> callbacks can occur
94 inside a group marked by a pair of C<OSSL_TRACE_CTRL_BEGIN> and
95 C<OSSL_TRACE_CTRL_END> calls, but never outside such a group.
97 =item C<OSSL_TRACE_CTRL_END>
99 The callback is called from OSSL_trace_end(), which gives the callback
100 the possibility to output a dynamic ending line, or reset the line
101 prefix that was set with OSSL_TRACE_CTRL_BEGIN, or something other.
105 =head2 Trace categories
107 The trace categories are simple numbers available through macros.
111 =item C<OSSL_TRACE_CATEGORY_TRACE>
113 Traces the OpenSSL trace API itself.
115 More precisely, this will generate trace output any time a new
118 =item C<OSSL_TRACE_CATEGORY_INIT>
120 Traces OpenSSL library initialization and cleanup.
122 This needs special care, as OpenSSL will do automatic cleanup after
123 exit from C<main()>, and any tracing output done during this cleanup
124 will be lost if the tracing channel or callback were cleaned away
126 A suggestion is to make such cleanup part of a function that's
127 registered very early with L<atexit(3)>.
129 =item C<OSSL_TRACE_CATEGORY_TLS>
131 Traces the TLS/SSL protocoll.
133 =item C<OSSL_TRACE_CATEGORY_TLS_CIPHER>
135 Traces the ciphers used by the TLS/SSL protocoll.
137 =item C<OSSL_TRACE_CATEGORY_ENGINE_CONF>
139 Traces the ENGINE configuration.
141 =item C<OSSL_TRACE_CATEGORY_ENGINE_TABLE>
143 Traces the ENGINE algorithm table selection.
145 More precisely, engine_table_select(), the function that is used by
146 RSA, DSA (etc) code to select registered ENGINEs, cache defaults and
147 functional references (etc), will generate trace summaries.
149 =item C<OSSL_TRACE_CATEGORY_ENGINE_REF_COUNT>
151 Tracds the ENGINE reference counting.
153 More precisely, both reference counts in the ENGINE structure will be
154 monitored with a line of trace output generated for each change.
156 =item C<OSSL_TRACE_CATEGORY_PKCS5V2>
158 Traces PKCS#5 v2 key generation.
160 =item C<OSSL_TRACE_CATEGORY_PKCS12_KEYGEN>
162 Traces PKCS#12 key generation.
164 =item C<OSSL_TRACE_CATEGORY_PKCS12_DECRYPT>
166 Traces PKCS#12 decryption.
168 =item C<OSSL_TRACE_CATEGORY_X509V3_POLICY>
170 Traces X509v3 policy processing.
172 More precisely, this generates the complete policy tree at various
173 point during evaluation.
175 =item C<OSSL_TRACE_CATEGORY_BN_CTX>
177 Traces BIGNUM context operations.
181 There is also C<OSSL_TRACE_CATEGORY_ANY>, which works as a fallback
182 and can be used to get I<all> trace output.
186 OSSL_trace_set_channel(), OSSL_trace_set_prefix(),
187 OSSL_trace_set_suffix(), and OSSL_trace_set_callback() return 1 on
188 success, or 0 on failure.
192 In all examples below, the trace producing code is assumed to be
196 const char bar[] = { 0, 1, 2, 3, 4, 5, 6, 7,
197 8, 9, 10, 11, 12, 13, 14, 15 };
199 OSSL_TRACE_BEGIN(TLS) {
200 BIO_puts(trc_out, "foo: ");
201 BIO_printf(trc_out, "%d\n", foo);
202 BIO_dump(trc_out, bar, sizeof(bar));
203 } OSSL_TRACE_END(TLS);
205 =head1 Simple example
207 An example with just a channel and constant prefix / suffix.
209 int main(int argc, char *argv[])
211 BIO *err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
212 OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_SSL, err);
213 OSSL_trace_set_prefix(OSSL_TRACE_CATEGORY_SSL, "BEGIN TRACE[TLS]");
214 OSSL_trace_set_suffix(OSSL_TRACE_CATEGORY_SSL, "END TRACE[TLS]");
219 When the trace producing code above is performed, this will be output
224 0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................
227 =head2 Advanced example
229 This example uses the callback, and depends on pthreads functionality.
231 static size_t cb(const char *buf, size_t cnt,
232 int category, int cmd, void *vdata)
235 const char *label = NULL;
238 case OSSL_TRACE_CTRL_BEGIN:
241 case OSSL_TRACE_CTRL_END:
252 tid.tid = pthread_self();
253 BIO_printf(bio, "%s TRACE[%s]:%lx\n",
254 label, OSSL_trace_get_category_name(category), tid.ltid);
256 return (size_t)BIO_puts(bio, buf);
259 int main(int argc, char *argv[])
261 BIO *err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
262 OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_SSL, cb, err);
267 The output is almost the same as for the simple example above.
269 BEGIN TRACE[TLS]:7f9eb0193b80
271 0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................
272 END TRACE[TLS]:7f9eb0193b80
276 =head2 Configure Tracing
278 By default, the OpenSSL library is built with tracing disabled. To
279 use the tracing functionality documented here, it is therefore
280 necessary to configure and build OpenSSL with the 'enable-trace' option.
282 When the library is built with tracing disabled, the macro
283 C<OPENSSL_NO_TRACE> is defined in C<openssl/opensslconf.h> and all
284 functions described here are inoperational, i.e. will do nothing.
288 OSSL_trace_set_channel(), OSSL_trace_set_prefix(),
289 OSSL_trace_set_suffix(), and OSSL_trace_set_callback() were all added
294 Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
296 Licensed under the Apache License 2.0 (the "License"). You may not use
297 this file except in compliance with the License. You can obtain a copy
298 in the file LICENSE in the source distribution or at
299 L<https://www.openssl.org/source/license.html>.