Fix intermittent sslapitest early data related failures
[openssl.git] / doc / man3 / OSSL_trace_set_channel.pod
1 =pod
2
3 =for openssl foreign manual atexit(3)
4
5 =head1 NAME
6
7 OSSL_trace_set_channel, OSSL_trace_set_prefix, OSSL_trace_set_suffix,
8 OSSL_trace_set_callback, OSSL_trace_cb - Enabling trace output
9
10 =head1 SYNOPSIS
11
12  #include <openssl/trace.h>
13
14  typedef size_t (*OSSL_trace_cb)(const char *buf, size_t cnt,
15                                  int category, int cmd, void *data);
16
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);
21
22 =head1 DESCRIPTION
23
24 If available (see L</Configure Tracing> below), the application can request
25 internal trace output.
26 This output comes in form of free text for humans to read.
27
28 The trace output is divided into categories which can be
29 enabled individually.
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.
36
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>,
39 respectively.
40
41 L<OSSL_TRACE_ENABLED(3)> can be used to check whether tracing is currently
42 enabled for the given category.
43 Functions like L<OSSL_TRACE1(3)> and macros like L<OSSL_TRACE_BEGIN(3)>
44 can be used for producing free-text trace output.
45
46 =head2 Functions
47
48 OSSL_trace_set_channel() is used to enable the given trace C<category>
49 by attaching the B<BIO> I<bio> object as (simple) trace channel.
50
51 OSSL_trace_set_prefix() and OSSL_trace_set_suffix() can be used to add
52 an extra line for each channel, to be output before and after group of
53 tracing output.
54 What constitutes an output group is decided by the code that produces
55 the output.
56 The lines given here are considered immutable; for more dynamic
57 tracing prefixes, consider setting a callback with
58 OSSL_trace_set_callback() instead.
59
60 OSSL_trace_set_callback() is used to enable the given trace
61 I<category> by giving it the tracer callback I<cb> with the associated
62 data I<data>, which will simply be passed through to I<cb> whenever
63 it's called. The callback function is internally wrapped by a
64 dedicated BIO object, the so-called I<callback trace channel>.
65 This should be used when it's desirable to do form the trace output to
66 something suitable for application needs where a prefix and suffix
67 line aren't enough.
68
69 OSSL_trace_set_channel() and OSSL_trace_set_callback() are mutually
70 exclusive, calling one of them will clear whatever was set by the
71 previous call.
72
73 Calling OSSL_trace_set_channel() with NULL for I<channel> or
74 OSSL_trace_set_callback() with NULL for I<cb> disables tracing for
75 the given I<category>.
76
77 =head2 Trace callback
78
79 The tracer callback must return a B<size_t>, which must be zero on
80 error and otherwise return the number of bytes that were output.
81 It receives a text buffer I<buf> with I<cnt> bytes of text, as well as
82 the I<category>, a control number I<cmd>, and the I<data> that was
83 passed to OSSL_trace_set_callback().
84
85 The possible control numbers are:
86
87 =over 4
88
89 =item B<OSSL_TRACE_CTRL_BEGIN>
90
91 The callback is called from OSSL_trace_begin(), which gives the
92 callback the possibility to output a dynamic starting line, or set a
93 prefix that should be output at the beginning of each line, or
94 something other.
95
96 =item B<OSSL_TRACE_CTRL_WRITE>
97
98 This callback is called whenever data is written to the BIO by some
99 regular BIO output routine.
100 An arbitrary number of B<OSSL_TRACE_CTRL_WRITE> callbacks can occur
101 inside a group marked by a pair of B<OSSL_TRACE_CTRL_BEGIN> and
102 B<OSSL_TRACE_CTRL_END> calls, but never outside such a group.
103
104 =item B<OSSL_TRACE_CTRL_END>
105
106 The callback is called from OSSL_trace_end(), which gives the callback
107 the possibility to output a dynamic ending line, or reset the line
108 prefix that was set with B<OSSL_TRACE_CTRL_BEGIN>, or something other.
109
110 =back
111
112 =head2 Trace categories
113
114 The trace categories are simple numbers available through macros.
115
116 =over 4
117
118 =item B<OSSL_TRACE_CATEGORY_TRACE>
119
120 Traces the OpenSSL trace API itself.
121
122 More precisely, this will generate trace output any time a new
123 trace hook is set.
124
125 =item B<OSSL_TRACE_CATEGORY_INIT>
126
127 Traces OpenSSL library initialization and cleanup.
128
129 This needs special care, as OpenSSL will do automatic cleanup after
130 exit from C<main()>, and any tracing output done during this cleanup
131 will be lost if the tracing channel or callback were cleaned away
132 prematurely.
133 A suggestion is to make such cleanup part of a function that's
134 registered very early with L<atexit(3)>.
135
136 =item B<OSSL_TRACE_CATEGORY_TLS>
137
138 Traces the TLS/SSL protocol.
139
140 =item B<OSSL_TRACE_CATEGORY_TLS_CIPHER>
141
142 Traces the ciphers used by the TLS/SSL protocol.
143
144 =item B<OSSL_TRACE_CATEGORY_ENGINE_TABLE>
145
146 Traces the ENGINE algorithm table selection.
147
148 More precisely, functions like ENGINE_get_pkey_asn1_meth_engine(),
149 ENGINE_get_pkey_meth_engine(), ENGINE_get_cipher_engine(),
150 ENGINE_get_digest_engine(), will generate trace summaries of the
151 handling of internal tables.
152
153 =item B<OSSL_TRACE_CATEGORY_ENGINE_REF_COUNT>
154
155 Traces the ENGINE reference counting.
156
157 More precisely, both reference counts in the ENGINE structure will be
158 monitored with a line of trace output generated for each change.
159
160 =item B<OSSL_TRACE_CATEGORY_PKCS5V2>
161
162 Traces PKCS#5 v2 key generation.
163
164 =item B<OSSL_TRACE_CATEGORY_PKCS12_KEYGEN>
165
166 Traces PKCS#12 key generation.
167
168 =item B<OSSL_TRACE_CATEGORY_PKCS12_DECRYPT>
169
170 Traces PKCS#12 decryption.
171
172 =item B<OSSL_TRACE_CATEGORY_X509V3_POLICY>
173
174 Traces X509v3 policy processing.
175
176 More precisely, this generates the complete policy tree at various
177 point during evaluation.
178
179 =item B<OSSL_TRACE_CATEGORY_BN_CTX>
180
181 Traces BIGNUM context operations.
182
183 =item B<OSSL_TRACE_CATEGORY_CONF>
184
185 Traces details about the provider and engine configuration.
186
187 =back
188
189 There is also B<OSSL_TRACE_CATEGORY_ALL>, which works as a fallback
190 and can be used to get I<all> trace output.
191
192 Note, however, that in this case all trace output will effectively be
193 associated with the 'ALL' category, which is undesirable if the
194 application intends to include the category name in the trace output.
195 In this case it is better to register separate channels for each
196 trace category instead.
197
198 =head1 RETURN VALUES
199
200 OSSL_trace_set_channel(), OSSL_trace_set_prefix(),
201 OSSL_trace_set_suffix(), and OSSL_trace_set_callback() return 1 on
202 success, or 0 on failure.
203
204 =head1 EXAMPLES
205
206 In all examples below, the trace producing code is assumed to be
207 the following:
208
209  int foo = 42;
210  const char bar[] = { 0,  1,  2,  3,  4,  5,  6,  7,
211                       8,  9, 10, 11, 12, 13, 14, 15 };
212
213  OSSL_TRACE_BEGIN(TLS) {
214      BIO_puts(trc_out, "foo: ");
215      BIO_printf(trc_out, "%d\n", foo);
216      BIO_dump(trc_out, bar, sizeof(bar));
217  } OSSL_TRACE_END(TLS);
218
219 =head2 Simple example
220
221 An example with just a channel and constant prefix / suffix.
222
223  int main(int argc, char *argv[])
224  {
225      BIO *err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
226      OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_SSL, err);
227      OSSL_trace_set_prefix(OSSL_TRACE_CATEGORY_SSL, "BEGIN TRACE[TLS]");
228      OSSL_trace_set_suffix(OSSL_TRACE_CATEGORY_SSL, "END TRACE[TLS]");
229
230      /* ... work ... */
231  }
232
233 When the trace producing code above is performed, this will be output
234 on standard error:
235
236  BEGIN TRACE[TLS]
237  foo: 42
238  0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f   ................
239  END TRACE[TLS]
240
241 =head2 Advanced example
242
243 This example uses the callback, and depends on pthreads functionality.
244
245  static size_t cb(const char *buf, size_t cnt,
246                  int category, int cmd, void *vdata)
247  {
248      BIO *bio = vdata;
249      const char *label = NULL;
250
251      switch (cmd) {
252      case OSSL_TRACE_CTRL_BEGIN:
253          label = "BEGIN";
254          break;
255      case OSSL_TRACE_CTRL_END:
256          label = "END";
257          break;
258      }
259
260      if (label != NULL) {
261          union {
262              pthread_t tid;
263              unsigned long ltid;
264          } tid;
265
266          tid.tid = pthread_self();
267          BIO_printf(bio, "%s TRACE[%s]:%lx\n",
268                     label, OSSL_trace_get_category_name(category), tid.ltid);
269      }
270      return (size_t)BIO_puts(bio, buf);
271  }
272
273  int main(int argc, char *argv[])
274  {
275      BIO *err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
276      OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_SSL, cb, err);
277
278      /* ... work ... */
279  }
280
281 The output is almost the same as for the simple example above.
282
283  BEGIN TRACE[TLS]:7f9eb0193b80
284  foo: 42
285  0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f   ................
286  END TRACE[TLS]:7f9eb0193b80
287
288 =head1 NOTES
289
290 =head2 Configure Tracing
291
292 By default, the OpenSSL library is built with tracing disabled. To
293 use the tracing functionality documented here, it is therefore
294 necessary to configure and build OpenSSL with the 'enable-trace' option.
295
296 When the library is built with tracing disabled, the macro
297 B<OPENSSL_NO_TRACE> is defined in F<< <openssl/opensslconf.h> >> and all
298 functions described here are inoperational, i.e. will do nothing.
299
300 =head1 SEE ALSO
301
302 L<OSSL_TRACE_ENABLED(3)>, L<OSSL_TRACE_BEGIN(3)>, L<OSSL_TRACE1(3)>,
303 L<atexit(3)>
304
305 =head1 HISTORY
306
307 OSSL_trace_set_channel(), OSSL_trace_set_prefix(),
308 OSSL_trace_set_suffix(), and OSSL_trace_set_callback() were all added
309 in OpenSSL 3.0.
310
311 =head1 COPYRIGHT
312
313 Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
314
315 Licensed under the Apache License 2.0 (the "License").  You may not use
316 this file except in compliance with the License.  You can obtain a copy
317 in the file LICENSE in the source distribution or at
318 L<https://www.openssl.org/source/license.html>.
319
320 =cut