OSSL_trace_enabled.pod 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 starts a tracing section, and get the
  78. channel for the given I<category> in form of a BIO.
  79. This BIO can only be used for output.
  80. OSSL_trace_end() is used to end a tracing section.
  81. Using OSSL_trace_begin() and OSSL_trace_end() to wrap tracing sections
  82. is I<mandatory>.
  83. The result of trying to produce tracing output outside of such
  84. sections is undefined.
  85. OSSL_trace_string() outputs I<data> of length I<size> as a string on BIO I<out>.
  86. If I<text> is 0, the function masks any included control characters apart from
  87. newlines and makes sure for nonempty input that the output ends with a newline.
  88. Unless I<full> is nonzero, the length is limited (with a suitable warning)
  89. to B<OSSL_TRACE_STRING_MAX> characters, which currently is 80.
  90. =head2 Macros
  91. There are a number of convenience macros defined, to make tracing
  92. easy and consistent.
  93. OSSL_TRACE_BEGIN() and OSSL_TRACE_END() reserve the B<BIO> C<trc_out> and are
  94. used as follows to wrap a trace section:
  95. OSSL_TRACE_BEGIN(TLS) {
  96. BIO_printf(trc_out, ... );
  97. } OSSL_TRACE_END(TLS);
  98. This will normally expand to:
  99. do {
  100. BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_TLS);
  101. if (trc_out != NULL) {
  102. ...
  103. BIO_printf(trc_out, ...);
  104. }
  105. OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
  106. } while (0);
  107. OSSL_TRACE_CANCEL() must be used before returning from or jumping out of a
  108. trace section:
  109. OSSL_TRACE_BEGIN(TLS) {
  110. if (some_error) {
  111. OSSL_TRACE_CANCEL(TLS);
  112. goto err;
  113. }
  114. BIO_printf(trc_out, ... );
  115. } OSSL_TRACE_END(TLS);
  116. This will normally expand to:
  117. do {
  118. BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_TLS);
  119. if (trc_out != NULL) {
  120. if (some_error) {
  121. OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
  122. goto err;
  123. }
  124. BIO_printf(trc_out, ... );
  125. }
  126. OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
  127. } while (0);
  128. OSSL_TRACE() and OSSL_TRACE1(), OSSL_TRACE2(), ... OSSL_TRACE9() are
  129. so-called one-shot macros:
  130. The macro call C<OSSL_TRACE(category, text)>, produces literal text trace output.
  131. The macro call C<OSSL_TRACEn(category, format, arg1, ..., argn)> produces
  132. printf-style trace output with n format field arguments (n=1,...,9).
  133. It expands to:
  134. OSSL_TRACE_BEGIN(category) {
  135. BIO_printf(trc_out, format, arg1, ..., argN);
  136. } OSSL_TRACE_END(category)
  137. Internally, all one-shot macros are implemented using a generic OSSL_TRACEV()
  138. macro, since C90 does not support variadic macros. This helper macro has a rather
  139. weird synopsis and should not be used directly.
  140. The macro call C<OSSL_TRACE_STRING(category, text, full, data, len)>
  141. outputs I<data> of length I<size> as a string
  142. if tracing for the given I<category> is enabled.
  143. It expands to:
  144. OSSL_TRACE_BEGIN(category) {
  145. OSSL_trace_string(trc_out, text, full, data, len);
  146. } OSSL_TRACE_END(category)
  147. The OSSL_TRACE_ENABLED() macro can be used to conditionally execute some code
  148. only if a specific trace category is enabled.
  149. In some situations this is simpler than entering a trace section using
  150. OSSL_TRACE_BEGIN() and OSSL_TRACE_END().
  151. For example, the code
  152. if (OSSL_TRACE_ENABLED(TLS)) {
  153. ...
  154. }
  155. expands to
  156. if (OSSL_trace_enabled(OSSL_TRACE_CATEGORY_TLS) {
  157. ...
  158. }
  159. =head1 NOTES
  160. If producing the trace output requires carrying out auxiliary calculations,
  161. this auxiliary code should be placed inside a conditional block which is
  162. executed only if the trace category is enabled.
  163. The most natural way to do this is to place the code inside the trace section
  164. itself because it already introduces such a conditional block.
  165. OSSL_TRACE_BEGIN(TLS) {
  166. int var = do_some_auxiliary_calculation();
  167. BIO_printf(trc_out, "var = %d\n", var);
  168. } OSSL_TRACE_END(TLS);
  169. In some cases it is more advantageous to use a simple conditional group instead
  170. of a trace section. This is the case if calculations and tracing happen in
  171. different locations of the code, or if the calculations are so time consuming
  172. that placing them inside a (critical) trace section would create too much
  173. contention.
  174. if (OSSL_TRACE_ENABLED(TLS)) {
  175. int var = do_some_auxiliary_calculation();
  176. OSSL_TRACE1("var = %d\n", var);
  177. }
  178. Note however that premature optimization of tracing code is in general futile
  179. and it's better to keep the tracing code as simple as possible.
  180. Because most often the limiting factor for the application's speed is the time
  181. it takes to print the trace output, not to calculate it.
  182. =head2 Configure Tracing
  183. By default, the OpenSSL library is built with tracing disabled. To
  184. use the tracing functionality documented here, it is therefore
  185. necessary to configure and build OpenSSL with the 'enable-trace' option.
  186. When the library is built with tracing disabled:
  187. =over 4
  188. =item *
  189. The macro B<OPENSSL_NO_TRACE> is defined in F<< <openssl/opensslconf.h> >>.
  190. =item *
  191. all functions are still present, but OSSL_trace_enabled() will always
  192. report the categories as disabled, and all other functions will do
  193. nothing.
  194. =item *
  195. the convenience macros are defined to produce dead code.
  196. For example, take this example from L</Macros> section above:
  197. OSSL_TRACE_BEGIN(TLS) {
  198. if (condition) {
  199. OSSL_TRACE_CANCEL(TLS);
  200. goto err;
  201. }
  202. BIO_printf(trc_out, ... );
  203. } OSSL_TRACE_END(TLS);
  204. When the tracing API isn't operational, that will expand to:
  205. do {
  206. BIO *trc_out = NULL;
  207. if (0) {
  208. if (condition) {
  209. ((void)0);
  210. goto err;
  211. }
  212. BIO_printf(trc_out, ... );
  213. }
  214. } while (0);
  215. =back
  216. =head1 RETURN VALUES
  217. OSSL_trace_enabled() returns 1 if tracing for the given I<type> is
  218. operational and enabled, otherwise 0.
  219. OSSL_trace_begin() returns a B<BIO> pointer if the given I<type> is enabled,
  220. otherwise NULL.
  221. OSSL_trace_string() returns the number of characters emitted, or -1 on error.
  222. =head1 SEE ALSO
  223. L<OSSL_trace_set_channel(3)>, L<OSSL_trace_set_callback(3)>
  224. =head1 HISTORY
  225. The OpenSSL Tracing API was added in OpenSSL 3.0.
  226. OSSL_TRACE_STRING(), OSSL_TRACE_STRING_MAX, and OSSL_trace_string
  227. were added in OpenSSL 3.2.
  228. =head1 COPYRIGHT
  229. Copyright 2019-2023 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