OSSL_trace_enabled.pod 9.9 KB

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