OSSL_trace_enabled.pod 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. =pod
  2. =head1 NAME
  3. OSSL_trace_enabled, OSSL_trace_begin, OSSL_trace_end,
  4. OSSL_TRACE_BEGIN, OSSL_TRACE_END, OSSL_TRACE_CANCEL,
  5. OSSL_TRACE, OSSL_TRACE1, OSSL_TRACE2, OSSL_TRACE3, OSSL_TRACE4,
  6. OSSL_TRACE5, OSSL_TRACE6, OSSL_TRACE7, OSSL_TRACE8, OSSL_TRACE9,
  7. OSSL_TRACEV,
  8. OSSL_TRACE_ENABLED
  9. - OpenSSL Tracing API
  10. =head1 SYNOPSIS
  11. =for openssl generic
  12. #include <openssl/trace.h>
  13. int OSSL_trace_enabled(int category);
  14. BIO *OSSL_trace_begin(int category);
  15. void OSSL_trace_end(int category, BIO *channel);
  16. /* trace group macros */
  17. OSSL_TRACE_BEGIN(category) {
  18. ...
  19. if (some_error) {
  20. /* Leave trace group prematurely in case of an error */
  21. OSSL_TRACE_CANCEL(category);
  22. goto err;
  23. }
  24. ...
  25. } OSSL_TRACE_END(category);
  26. /* one-shot trace macros */
  27. OSSL_TRACE(category, text)
  28. OSSL_TRACE1(category, format, arg1)
  29. OSSL_TRACE2(category, format, arg1, arg2)
  30. ...
  31. OSSL_TRACE9(category, format, arg1, ..., arg9)
  32. /* check whether a trace category is enabled */
  33. if (OSSL_TRACE_ENABLED(category)) {
  34. ...
  35. }
  36. =head1 DESCRIPTION
  37. The functions described here are mainly interesting for those who provide
  38. OpenSSL functionality, either in OpenSSL itself or in engine modules
  39. or similar.
  40. If the tracing facility is enabled (see L</Configure Tracing> below),
  41. these functions are used to generate free text tracing output.
  42. The tracing output is divided into types which are enabled
  43. individually by the application.
  44. The tracing types are described in detail in
  45. L<OSSL_trace_set_callback(3)/Trace types>.
  46. The fallback type B<OSSL_TRACE_CATEGORY_ALL> should I<not> be used
  47. with the functions described here.
  48. Tracing for a specific category is enabled at run-time if a so-called
  49. I<trace channel> is attached to it. A trace channel is simply a
  50. BIO object to which the application can write its trace output.
  51. The application has two different ways of registering a trace channel,
  52. either by directly providing a BIO object using L<OSSL_trace_set_channel(3)>,
  53. or by providing a callback routine using L<OSSL_trace_set_callback(3)>.
  54. The latter is wrapped internally by a dedicated BIO object, so for the
  55. tracing code both channel types are effectively indistinguishable.
  56. We call them a I<simple trace channel> and a I<callback trace channel>,
  57. respectively.
  58. To produce trace output, it is necessary to obtain a pointer to the
  59. trace channel (i.e., the BIO object) using OSSL_trace_begin(), write
  60. to it using arbitrary BIO output routines, and finally releases the
  61. channel using OSSL_trace_end(). The OSSL_trace_begin()/OSSL_trace_end()
  62. calls surrounding the trace output create a group, which acts as a
  63. critical section (guarded by a mutex) to ensure that the trace output
  64. of different threads does not get mixed up.
  65. The tracing code normally does not call OSSL_trace_{begin,end}() directly,
  66. but rather uses a set of convenience macros, see the L</Macros> section below.
  67. =head2 Functions
  68. OSSL_trace_enabled() can be used to check if tracing for the given
  69. I<category> is enabled, i.e., if the tracing facility has been statically
  70. enabled (see L</Configure Tracing> below) and a trace channel has been
  71. registered using L<OSSL_trace_set_channel(3)> or L<OSSL_trace_set_callback(3)>.
  72. OSSL_trace_begin() is used to starts a tracing section, and get the
  73. channel for the given I<category> in form of a BIO.
  74. This BIO can only be used for output.
  75. OSSL_trace_end() is used to end a tracing section.
  76. Using OSSL_trace_begin() and OSSL_trace_end() to wrap tracing sections
  77. is I<mandatory>.
  78. The result of trying to produce tracing output outside of such
  79. sections is undefined.
  80. =head2 Macros
  81. There are a number of convenience macros defined, to make tracing
  82. easy and consistent.
  83. OSSL_TRACE_BEGIN() and OSSL_TRACE_END() reserve the B<BIO> C<trc_out> and are
  84. used as follows to wrap a trace section:
  85. OSSL_TRACE_BEGIN(TLS) {
  86. BIO_printf(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. ...
  93. BIO_printf(trc_out, ...);
  94. }
  95. OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
  96. } while (0);
  97. OSSL_TRACE_CANCEL() must be used before returning from or jumping out of a
  98. trace section:
  99. OSSL_TRACE_BEGIN(TLS) {
  100. if (some_error) {
  101. OSSL_TRACE_CANCEL(TLS);
  102. goto err;
  103. }
  104. BIO_printf(trc_out, ... );
  105. } OSSL_TRACE_END(TLS);
  106. This will normally expand to:
  107. do {
  108. BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_TLS);
  109. if (trc_out != NULL) {
  110. if (some_error) {
  111. OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
  112. goto err;
  113. }
  114. BIO_printf(trc_out, ... );
  115. }
  116. OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
  117. } while (0);
  118. OSSL_TRACE() and OSSL_TRACE1(), OSSL_TRACE2(), ... OSSL_TRACE9() are
  119. so-called one-shot macros:
  120. The macro call C<OSSL_TRACE(category, text)>, produces literal text trace output.
  121. The macro call C<OSSL_TRACEn(category, format, arg1, ..., argn)> produces
  122. printf-style trace output with n format field arguments (n=1,...,9).
  123. It expands to:
  124. OSSL_TRACE_BEGIN(category) {
  125. BIO_printf(trc_out, format, arg1, ..., argN)
  126. } OSSL_TRACE_END(category)
  127. Internally, all one-shot macros are implemented using a generic OSSL_TRACEV()
  128. macro, since C90 does not support variadic macros. This helper macro has a rather
  129. weird synopsis and should not be used directly.
  130. The OSSL_TRACE_ENABLED() macro can be used to conditionally execute some code
  131. only if a specific trace category is enabled.
  132. In some situations this is simpler than entering a trace section using
  133. OSSL_TRACE_BEGIN() and OSSL_TRACE_END().
  134. For example, the code
  135. if (OSSL_TRACE_ENABLED(TLS)) {
  136. ...
  137. }
  138. expands to
  139. if (OSSL_trace_enabled(OSSL_TRACE_CATEGORY_TLS) {
  140. ...
  141. }
  142. =head1 NOTES
  143. If producing the trace output requires carrying out auxiliary calculations,
  144. this auxiliary code should be placed inside a conditional block which is
  145. executed only if the trace category is enabled.
  146. The most natural way to do this is to place the code inside the trace section
  147. itself because it already introduces such a conditional block.
  148. OSSL_TRACE_BEGIN(TLS) {
  149. int var = do_some_auxiliary_calculation();
  150. BIO_printf(trc_out, "var = %d\n", var);
  151. } OSSL_TRACE_END(TLS);
  152. In some cases it is more advantageous to use a simple conditional group instead
  153. of a trace section. This is the case if calculations and tracing happen in
  154. different locations of the code, or if the calculations are so time consuming
  155. that placing them inside a (critical) trace section would create too much
  156. contention.
  157. if (OSSL_TRACE_ENABLED(TLS)) {
  158. int var = do_some_auxiliary_calculation();
  159. OSSL_TRACE1("var = %d\n", var);
  160. }
  161. Note however that premature optimization of tracing code is in general futile
  162. and it's better to keep the tracing code as simple as possible.
  163. Because most often the limiting factor for the application's speed is the time
  164. it takes to print the trace output, not to calculate it.
  165. =head2 Configure Tracing
  166. By default, the OpenSSL library is built with tracing disabled. To
  167. use the tracing functionality documented here, it is therefore
  168. necessary to configure and build OpenSSL with the 'enable-trace' option.
  169. When the library is built with tracing disabled:
  170. =over 4
  171. =item *
  172. The macro B<OPENSSL_NO_TRACE> is defined in F<< <openssl/opensslconf.h> >>.
  173. =item *
  174. all functions are still present, but OSSL_trace_enabled() will always
  175. report the categories as disabled, and all other functions will do
  176. nothing.
  177. =item *
  178. the convenience macros are defined to produce dead code.
  179. For example, take this example from L</Macros> section above:
  180. OSSL_TRACE_BEGIN(TLS) {
  181. if (condition) {
  182. OSSL_TRACE_CANCEL(TLS);
  183. goto err;
  184. }
  185. BIO_printf(trc_out, ... );
  186. } OSSL_TRACE_END(TLS);
  187. When the tracing API isn't operational, that will expand to:
  188. do {
  189. BIO *trc_out = NULL;
  190. if (0) {
  191. if (condition) {
  192. ((void)0);
  193. goto err;
  194. }
  195. BIO_printf(trc_out, ... );
  196. }
  197. } while (0);
  198. =back
  199. =head1 RETURN VALUES
  200. OSSL_trace_enabled() returns 1 if tracing for the given I<type> is
  201. operational and enabled, otherwise 0.
  202. OSSL_trace_begin() returns a B<BIO> pointer if the given I<type> is enabled,
  203. otherwise NULL.
  204. =head1 SEE ALSO
  205. L<OSSL_trace_set_channel(3)>, L<OSSL_trace_set_callback(3)>
  206. =head1 HISTORY
  207. The OpenSSL Tracing API was added in OpenSSL 3.0.
  208. =head1 COPYRIGHT
  209. Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
  210. Licensed under the Apache License 2.0 (the "License"). You may not use
  211. this file except in compliance with the License. You can obtain a copy
  212. in the file LICENSE in the source distribution or at
  213. L<https://www.openssl.org/source/license.html>.
  214. =cut