OSSL_trace_set_channel.pod 10 KB

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