OSSL_trace_enabled.pod 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. =pod
  2. =head1 NAME
  3. OSSL_trace_enabled, OSSL_trace_begin, OSSL_trace_end,
  4. OSSL_TRACE_BEGIN, OSSL_TRACE_END, OSSL_TRACE1, OSSL_TRACE2, OSSL_TRACE9
  5. - OpenSSL Tracing API
  6. =head1 SYNOPSIS
  7. #include <openssl/trace.h>
  8. int OSSL_trace_enabled(int category);
  9. BIO *OSSL_trace_begin(int category);
  10. void OSSL_trace_end(int category, BIO *channel);
  11. /* trace group macros */
  12. OSSL_TRACE_BEGIN(category) {
  13. ...
  14. } OSSL_TRACE_END(category);
  15. /* one-shot trace macros */
  16. OSSL_TRACE1(category, format, arg1)
  17. OSSL_TRACE2(category, format, arg1, arg2)
  18. ...
  19. OSSL_TRACE9(category, format, arg1, ..., arg9)
  20. =head1 DESCRIPTION
  21. The functions described here are mainly interesting for those who provide
  22. OpenSSL functionality, either in OpenSSL itself or in engine modules
  23. or similar.
  24. If tracing is enabled (see L</NOTES> below), these functions are used to
  25. generate free text tracing output.
  26. The tracing output is divided into types which are enabled
  27. individually by the application.
  28. The tracing types are described in detail in
  29. L<OSSL_trace_set_callback(3)/Trace types>.
  30. The fallback type C<OSSL_TRACE_CATEGORY_ALL> should I<not> be used
  31. with the functions described here.
  32. Tracing for a specific category is enabled if a so called
  33. I<trace channel> is attached to it. A trace channel is simply a
  34. BIO object to which the application can write its trace output.
  35. The application has two different ways of registering a trace channel,
  36. either by directly providing a BIO object using OSSL_trace_set_channel(),
  37. or by providing a callback routine using OSSL_trace_set_callback().
  38. The latter is wrapped internally by a dedicated BIO object, so for the
  39. tracing code both channel types are effectively indistinguishable.
  40. We call them a I<simple trace channel> and a I<callback trace channel>,
  41. respectively.
  42. To produce trace output, it is necessary to obtain a pointer to the
  43. trace channel (i.e., the BIO object) using OSSL_trace_begin(), write
  44. to it using arbitrary BIO output routines, and finally releases the
  45. channel using OSSL_trace_end(). The OSSL_trace_begin()/OSSL_trace_end()
  46. calls surrounding the trace output create a group, which acts as a
  47. critical section (guarded by a mutex) to ensure that the trace output
  48. of different threads does not get mixed up.
  49. The tracing code normally does not call OSSL_trace_{begin,end}() directly,
  50. but rather uses a set of convenience macros, see the L</Macros> section below.
  51. =head2 Functions
  52. OSSL_trace_enabled() can be used to check if tracing for the given
  53. C<category> is enabled.
  54. OSSL_trace_begin() is used to starts a tracing section, and get the
  55. channel for the given C<category> in form of a BIO.
  56. This BIO can only be used for output.
  57. OSSL_trace_end() is used to end a tracing section.
  58. Using OSSL_trace_begin() and OSSL_trace_end() to wrap tracing sections
  59. is I<mandatory>.
  60. The result of trying to produce tracing output outside of such
  61. sections is undefined.
  62. =head2 Macros
  63. There are a number of convenience macros defined, to make tracing
  64. easy and consistent.
  65. C<OSSL_TRACE_BEGIN(category)> and C<OSSL_TRACE_END(category)> reserve
  66. the B<BIO> C<trc_out> and are used as follows to wrap a trace section:
  67. OSSL_TRACE_BEGIN(TLS) {
  68. BIO_fprintf(trc_out, ... );
  69. } OSSL_TRACE_END(TLS);
  70. This will normally expand to:
  71. do {
  72. BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_TLS);
  73. if (trc_out != NULL) {
  74. ...
  75. BIO_fprintf(trc_out, ...);
  76. }
  77. OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
  78. } while (0);
  79. C<OSSL_TRACE_CANCEL(category)> must be used before returning from or
  80. jumping out of a trace section:
  81. OSSL_TRACE_BEGIN(TLS) {
  82. if (condition) {
  83. OSSL_TRACE_CANCEL(TLS);
  84. goto err;
  85. }
  86. BIO_fprintf(trc_out, ... );
  87. } OSSL_TRACE_END(TLS);
  88. This will normally expand to:
  89. do {
  90. BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_TLS);
  91. if (trc_out != NULL) {
  92. if (condition) {
  93. OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
  94. goto err;
  95. }
  96. BIO_fprintf(trc_out, ... );
  97. }
  98. OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
  99. } while (0);
  100. C<OSSL_TRACE1()>, ... C<OSSL_TRACE9()> are one-shot macros which essentially wrap
  101. a single BIO_printf() into a tracing group.
  102. The call OSSL_TRACEn(category, format, arg1, ..., argN) expands to:
  103. OSSL_TRACE_BEGIN(category) {
  104. BIO_printf(trc_out, format, arg1, ..., argN)
  105. } OSSL_TRACE_END(category)
  106. =head1 NOTES
  107. It is advisable to always check that a trace type is enabled with
  108. OSSL_trace_enabled() before generating any output, for example:
  109. if (OSSL_trace_enabled(OSSL_TRACE_CATEGORY_TLS)) {
  110. BIO *trace = OSSL_trace_begin(OSSL_TRACE_CATEGORY_TLS);
  111. BIO_printf(trace, "FOO %d\n", somevalue);
  112. BIO_dump(trace, somememory, somememory_l);
  113. OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trace);
  114. }
  115. =head2 Configure Tracing
  116. By default, the OpenSSL library is built with tracing disabled. To
  117. use the tracing functionality documented here, it is therefore
  118. necessary to configure and build OpenSSL with the 'enable-trace' option.
  119. When the library is built with tracing disabled:
  120. =over 4
  121. =item *
  122. The macro C<OPENSSL_NO_TRACE> is defined in C<openssl/opensslconf.h>.
  123. =item *
  124. all functions are still present, bu OSSL_trace_enabled() will always
  125. report the categories as disabled, and all other functions will do
  126. nothing.
  127. =item *
  128. the convenience macros are defined to produce dead code.
  129. For example, take this example from L</Macros> section above:
  130. OSSL_TRACE_BEGIN(TLS) {
  131. if (condition) {
  132. OSSL_TRACE_CANCEL(TLS);
  133. goto err;
  134. }
  135. BIO_fprintf(trc_out, ... );
  136. } OSSL_TRACE_END(TLS);
  137. When the tracing API isn't operational, that will expand to:
  138. do {
  139. BIO *trc_out = NULL;
  140. if (0) {
  141. if (condition) {
  142. ((void)0);
  143. goto err;
  144. }
  145. BIO_fprintf(trc_out, ... );
  146. }
  147. } while (0);
  148. =back
  149. =head1 RETURN VALUES
  150. OSSL_trace_enabled() returns 1 if tracing for the given B<type> is
  151. operational and enabled, otherwise 0.
  152. OSSL_trace_begin() returns a C<BIO *> if the given B<type> is enabled,
  153. otherwise C<NULL>.
  154. =head1 HISTORY
  155. The OpenSSL Tracing API was added ino OpenSSL 3.0.0.
  156. =head1 COPYRIGHT
  157. Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
  158. Licensed under the Apache License 2.0 (the "License"). You may not use
  159. this file except in compliance with the License. You can obtain a copy
  160. in the file LICENSE in the source distribution or at
  161. L<https://www.openssl.org/source/license.html>.
  162. =cut