replace 'OpenSSL license' by 'Apache License 2.0'
[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 =back
180
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.
183
184 =head1 RETURN VALUES
185
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.
189
190 =head1 EXAMPLES
191
192 In all examples below, the trace producing code is assumed to be
193 the following:
194
195  int foo = 42;
196  const char bar[] = { 0,  1,  2,  3,  4,  5,  6,  7,
197                       8,  9, 10, 11, 12, 13, 14, 15 };
198
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);
204
205 =head1 Simple example
206
207 An example with just a channel and constant prefix / suffix.
208
209  int main(int argc, char *argv[])
210  {
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]");
215
216      /* ... work ... */
217  }
218
219 When the trace producing code above is performed, this will be output
220 on standard error:
221
222  BEGIN TRACE[TLS]
223  foo: 42
224  0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f   ................
225  END TRACE[TLS]
226
227 =head2 Advanced example
228
229 This example uses the callback, and depends on pthreads functionality.
230
231  static size_t cb(const char *buf, size_t cnt,
232                  int category, int cmd, void *vdata)
233  {
234      BIO *bio = vdata;
235      const char *label = NULL;
236
237      switch (cmd) {
238      case OSSL_TRACE_CTRL_BEGIN:
239          label = "BEGIN";
240          break;
241      case OSSL_TRACE_CTRL_END:
242          label = "END";
243          break;
244      }
245
246      if (label != NULL) {
247          union {
248              pthread_t tid;
249              unsigned long ltid;
250          } tid;
251
252          tid.tid = pthread_self();
253          BIO_printf(bio, "%s TRACE[%s]:%lx\n",
254                     label, OSSL_trace_get_category_name(category), tid.ltid);
255      }
256      return (size_t)BIO_puts(bio, buf);
257  }
258
259  int main(int argc, char *argv[])
260  {
261      BIO *err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
262      OSSL_trace_set_callback(OSSL_TRACE_CATEGORY_SSL, cb, err);
263
264      /* ... work ... */
265  }
266
267 The output is almost the same as for the simple example above.
268
269  BEGIN TRACE[TLS]:7f9eb0193b80
270  foo: 42
271  0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f   ................
272  END TRACE[TLS]:7f9eb0193b80
273
274 =head1 NOTES
275
276 =head2 Configure Tracing
277
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.
281
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.
285
286 =head1 HISTORY
287
288 OSSL_trace_set_channel(), OSSL_trace_set_prefix(),
289 OSSL_trace_set_suffix(), and OSSL_trace_set_callback() were all added
290 in OpenSSL 3.0.0.
291
292 =head1 COPYRIGHT
293
294 Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
295
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>.
300
301 =cut