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