trace: add PROVIDER_CONF trace category
[openssl.git] / doc / man3 / OSSL_trace_set_channel.pod
1 =pod
2
3 =head1 NAME
4
5 OSSL_trace_set_channel, OSSL_trace_set_prefix, OSSL_trace_set_suffix,
6 OSSL_trace_set_callback, OSSL_trace_cb - Enabling trace output
7
8 =head1 SYNOPSIS
9
10  #include <openssl/trace.h>
11
12  typedef size_t (*OSSL_trace_cb)(const char *buf, size_t cnt,
13                                  int category, int cmd, void *data);
14
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);
19
20 =head1 DESCRIPTION
21
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.
25
26 The trace output is divided into categories which can be
27 enabled individually.
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.
34
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>,
37 respectively.
38
39 =head2 Functions
40
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.
43
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
46 tracing output.
47 What constitues an output group is decided by the code that produces
48 the output.
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.
52
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
60 line aren't enough.
61
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
64 previous call.
65
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
68 the given C<category>
69
70 =head2 Trace callback
71
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().
77
78 The possible control numbers are:
79
80 =over 4
81
82 =item C<OSSL_TRACE_CTRL_BEGIN>
83
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
87 something other.
88
89 =item C<OSSL_TRACE_CTRL_WRITE>
90
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.
96
97 =item C<OSSL_TRACE_CTRL_END>
98
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.
102
103 =back
104
105 =head2 Trace categories
106
107 The trace categories are simple numbers available through macros.
108
109 =over 4
110
111 =item C<OSSL_TRACE_CATEGORY_TRACE>
112
113 Traces the OpenSSL trace API itself.
114
115 More precisely, this will generate trace output any time a new
116 trace hook is set.
117
118 =item C<OSSL_TRACE_CATEGORY_INIT>
119
120 Traces OpenSSL library initialization and cleanup.
121
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
125 prematurely.
126 A suggestion is to make such cleanup part of a function that's
127 registered very early with L<atexit(3)>.
128
129 =item C<OSSL_TRACE_CATEGORY_TLS>
130
131 Traces the TLS/SSL protocoll.
132
133 =item C<OSSL_TRACE_CATEGORY_TLS_CIPHER>
134
135 Traces the ciphers used by the TLS/SSL protocoll.
136
137 =item C<OSSL_TRACE_CATEGORY_ENGINE_CONF>
138
139 Traces the ENGINE configuration.
140
141 =item C<OSSL_TRACE_CATEGORY_ENGINE_TABLE>
142
143 Traces the ENGINE algorithm table selection.
144
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.
148
149 =item C<OSSL_TRACE_CATEGORY_ENGINE_REF_COUNT>
150
151 Tracds the ENGINE reference counting.
152
153 More precisely, both reference counts in the ENGINE structure will be
154 monitored with a line of trace output generated for each change.
155
156 =item C<OSSL_TRACE_CATEGORY_PKCS5V2>
157
158 Traces PKCS#5 v2 key generation.
159
160 =item C<OSSL_TRACE_CATEGORY_PKCS12_KEYGEN>
161
162 Traces PKCS#12 key generation.
163
164 =item C<OSSL_TRACE_CATEGORY_PKCS12_DECRYPT>
165
166 Traces PKCS#12 decryption.
167
168 =item C<OSSL_TRACE_CATEGORY_X509V3_POLICY>
169
170 Traces X509v3 policy processing.
171
172 More precisely, this generates the complete policy tree at various
173 point during evaluation.
174
175 =item C<OSSL_TRACE_CATEGORY_BN_CTX>
176
177 Traces BIGNUM context operations.
178
179 =item C<OSSL_TRACE_CATEGORY_PROVIDER_CONF>
180
181 Traces the OSSL_PROVIDER configuration.
182
183 =back
184
185 There is also C<OSSL_TRACE_CATEGORY_ALL>, which works as a fallback
186 and can be used to get I<all> trace output.
187
188 Note, however, that in this case all trace output will effectively be
189 associated with the 'ALL' category, which is undesirable if the
190 application intends to include the category name in the trace output.
191 In this case it is better to register separate channels for each
192 trace category instead.
193
194 =head1 RETURN VALUES
195
196 OSSL_trace_set_channel(), OSSL_trace_set_prefix(),
197 OSSL_trace_set_suffix(), and OSSL_trace_set_callback() return 1 on
198 success, or 0 on failure.
199
200 =head1 EXAMPLES
201
202 In all examples below, the trace producing code is assumed to be
203 the following:
204
205  int foo = 42;
206  const char bar[] = { 0,  1,  2,  3,  4,  5,  6,  7,
207                       8,  9, 10, 11, 12, 13, 14, 15 };
208
209  OSSL_TRACE_BEGIN(TLS) {
210      BIO_puts(trc_out, "foo: ");
211      BIO_printf(trc_out, "%d\n", foo);
212      BIO_dump(trc_out, bar, sizeof(bar));
213  } OSSL_TRACE_END(TLS);
214
215 =head1 Simple example
216
217 An example with just a channel and constant prefix / suffix.
218
219  int main(int argc, char *argv[])
220  {
221      BIO *err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
222      OSSL_trace_set_channel(OSSL_TRACE_CATEGORY_SSL, err);
223      OSSL_trace_set_prefix(OSSL_TRACE_CATEGORY_SSL, "BEGIN TRACE[TLS]");
224      OSSL_trace_set_suffix(OSSL_TRACE_CATEGORY_SSL, "END TRACE[TLS]");
225
226      /* ... work ... */
227  }
228
229 When the trace producing code above is performed, this will be output
230 on standard error:
231
232  BEGIN TRACE[TLS]
233  foo: 42
234  0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f   ................
235  END TRACE[TLS]
236
237 =head2 Advanced example
238
239 This example uses the callback, and depends on pthreads functionality.
240
241  static size_t cb(const char *buf, size_t cnt,
242                  int category, int cmd, void *vdata)
243  {
244      BIO *bio = vdata;
245      const char *label = NULL;
246
247      switch (cmd) {
248      case OSSL_TRACE_CTRL_BEGIN:
249          label = "BEGIN";
250          break;
251      case OSSL_TRACE_CTRL_END:
252          label = "END";
253          break;
254      }
255
256      if (label != NULL) {
257          union {
258              pthread_t tid;
259              unsigned long ltid;
260          } tid;
261
262          tid.tid = pthread_self();
263          BIO_printf(bio, "%s TRACE[%s]:%lx\n",
264                     label, OSSL_trace_get_category_name(category), tid.ltid);
265      }
266      return (size_t)BIO_puts(bio, buf);
267  }
268
269  int main(int argc, char *argv[])
270  {
271      BIO *err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
272      OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_SSL, cb, err);
273
274      /* ... work ... */
275  }
276
277 The output is almost the same as for the simple example above.
278
279  BEGIN TRACE[TLS]:7f9eb0193b80
280  foo: 42
281  0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f   ................
282  END TRACE[TLS]:7f9eb0193b80
283
284 =head1 NOTES
285
286 =head2 Configure Tracing
287
288 By default, the OpenSSL library is built with tracing disabled. To
289 use the tracing functionality documented here, it is therefore
290 necessary to configure and build OpenSSL with the 'enable-trace' option.
291
292 When the library is built with tracing disabled, the macro
293 C<OPENSSL_NO_TRACE> is defined in C<openssl/opensslconf.h> and all
294 functions described here are inoperational, i.e. will do nothing.
295
296 =head1 HISTORY
297
298 OSSL_trace_set_channel(), OSSL_trace_set_prefix(),
299 OSSL_trace_set_suffix(), and OSSL_trace_set_callback() were all added
300 in OpenSSL 3.0.0.
301
302 =head1 COPYRIGHT
303
304 Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
305
306 Licensed under the Apache License 2.0 (the "License").  You may not use
307 this file except in compliance with the License.  You can obtain a copy
308 in the file LICENSE in the source distribution or at
309 L<https://www.openssl.org/source/license.html>.
310
311 =cut