OSSL_trace_enabled.pod 8.4 KB

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