OSSL_trace_enabled.3ossl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. .\" -*- mode: troff; coding: utf-8 -*-
  2. .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
  3. .\"
  4. .\" Standard preamble:
  5. .\" ========================================================================
  6. .de Sp \" Vertical space (when we can't use .PP)
  7. .if t .sp .5v
  8. .if n .sp
  9. ..
  10. .de Vb \" Begin verbatim text
  11. .ft CW
  12. .nf
  13. .ne \\$1
  14. ..
  15. .de Ve \" End verbatim text
  16. .ft R
  17. .fi
  18. ..
  19. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
  20. .ie n \{\
  21. . ds C` ""
  22. . ds C' ""
  23. 'br\}
  24. .el\{\
  25. . ds C`
  26. . ds C'
  27. 'br\}
  28. .\"
  29. .\" Escape single quotes in literal strings from groff's Unicode transform.
  30. .ie \n(.g .ds Aq \(aq
  31. .el .ds Aq '
  32. .\"
  33. .\" If the F register is >0, we'll generate index entries on stderr for
  34. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  35. .\" entries marked with X<> in POD. Of course, you'll have to process the
  36. .\" output yourself in some meaningful fashion.
  37. .\"
  38. .\" Avoid warning from groff about undefined register 'F'.
  39. .de IX
  40. ..
  41. .nr rF 0
  42. .if \n(.g .if rF .nr rF 1
  43. .if (\n(rF:(\n(.g==0)) \{\
  44. . if \nF \{\
  45. . de IX
  46. . tm Index:\\$1\t\\n%\t"\\$2"
  47. ..
  48. . if !\nF==2 \{\
  49. . nr % 0
  50. . nr F 2
  51. . \}
  52. . \}
  53. .\}
  54. .rr rF
  55. .\" ========================================================================
  56. .\"
  57. .IX Title "OSSL_TRACE_ENABLED 3ossl"
  58. .TH OSSL_TRACE_ENABLED 3ossl 2025-01-17 3.4.0 OpenSSL
  59. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  60. .\" way too many mistakes in technical documents.
  61. .if n .ad l
  62. .nh
  63. .SH NAME
  64. OSSL_trace_enabled, OSSL_trace_begin, OSSL_trace_end,
  65. OSSL_TRACE_BEGIN, OSSL_TRACE_END, OSSL_TRACE_CANCEL,
  66. OSSL_TRACE, OSSL_TRACE1, OSSL_TRACE2, OSSL_TRACE3, OSSL_TRACE4,
  67. OSSL_TRACE5, OSSL_TRACE6, OSSL_TRACE7, OSSL_TRACE8, OSSL_TRACE9,
  68. OSSL_TRACEV,
  69. OSSL_TRACE_STRING, OSSL_TRACE_STRING_MAX, OSSL_trace_string,
  70. OSSL_TRACE_ENABLED
  71. \&\- OpenSSL Tracing API
  72. .SH SYNOPSIS
  73. .IX Header "SYNOPSIS"
  74. .Vb 1
  75. \& #include <openssl/trace.h>
  76. \&
  77. \& int OSSL_trace_enabled(int category);
  78. \&
  79. \& BIO *OSSL_trace_begin(int category);
  80. \& void OSSL_trace_end(int category, BIO *channel);
  81. \&
  82. \& /* trace group macros */
  83. \& OSSL_TRACE_BEGIN(category) {
  84. \& ...
  85. \& if (some_error) {
  86. \& /* Leave trace group prematurely in case of an error */
  87. \& OSSL_TRACE_CANCEL(category);
  88. \& goto err;
  89. \& }
  90. \& ...
  91. \& } OSSL_TRACE_END(category);
  92. \&
  93. \& /* one\-shot trace macros */
  94. \& OSSL_TRACE(category, text)
  95. \& OSSL_TRACE1(category, format, arg1)
  96. \& OSSL_TRACE2(category, format, arg1, arg2)
  97. \& ...
  98. \& OSSL_TRACE9(category, format, arg1, ..., arg9)
  99. \& OSSL_TRACE_STRING(category, text, full, data, len)
  100. \&
  101. \& #define OSSL_TRACE_STRING_MAX 80
  102. \& int OSSL_trace_string(BIO *out, int text, int full,
  103. \& const unsigned char *data, size_t size);
  104. \&
  105. \& /* check whether a trace category is enabled */
  106. \& if (OSSL_TRACE_ENABLED(category)) {
  107. \& ...
  108. \& }
  109. .Ve
  110. .SH DESCRIPTION
  111. .IX Header "DESCRIPTION"
  112. The functions described here are mainly interesting for those who provide
  113. OpenSSL functionality, either in OpenSSL itself or in engine modules
  114. or similar.
  115. .PP
  116. If the tracing facility is enabled (see "Configure Tracing" below),
  117. these functions are used to generate free text tracing output.
  118. .PP
  119. The tracing output is divided into types which are enabled
  120. individually by the application.
  121. The tracing types are described in detail in
  122. "Trace types" in \fBOSSL_trace_set_callback\fR\|(3).
  123. The fallback type \fBOSSL_TRACE_CATEGORY_ALL\fR should \fInot\fR be used
  124. with the functions described here.
  125. .PP
  126. Tracing for a specific category is enabled at run-time if a so-called
  127. \&\fItrace channel\fR is attached to it. A trace channel is simply a
  128. BIO object to which the application can write its trace output.
  129. .PP
  130. The application has two different ways of registering a trace channel,
  131. either by directly providing a BIO object using \fBOSSL_trace_set_channel\fR\|(3),
  132. or by providing a callback routine using \fBOSSL_trace_set_callback\fR\|(3).
  133. The latter is wrapped internally by a dedicated BIO object, so for the
  134. tracing code both channel types are effectively indistinguishable.
  135. We call them a \fIsimple trace channel\fR and a \fIcallback trace channel\fR,
  136. respectively.
  137. .PP
  138. To produce trace output, it is necessary to obtain a pointer to the
  139. trace channel (i.e., the BIO object) using \fBOSSL_trace_begin()\fR, write
  140. to it using arbitrary BIO output routines, and finally releases the
  141. channel using \fBOSSL_trace_end()\fR. The \fBOSSL_trace_begin()\fR/\fBOSSL_trace_end()\fR
  142. calls surrounding the trace output create a group, which acts as a
  143. critical section (guarded by a mutex) to ensure that the trace output
  144. of different threads does not get mixed up.
  145. .PP
  146. The tracing code normally does not call OSSL_trace_{begin,end}() directly,
  147. but rather uses a set of convenience macros, see the "Macros" section below.
  148. .SS Functions
  149. .IX Subsection "Functions"
  150. \&\fBOSSL_trace_enabled()\fR can be used to check if tracing for the given
  151. \&\fIcategory\fR is enabled, i.e., if the tracing facility has been statically
  152. enabled (see "Configure Tracing" below) and a trace channel has been
  153. registered using \fBOSSL_trace_set_channel\fR\|(3) or \fBOSSL_trace_set_callback\fR\|(3).
  154. .PP
  155. \&\fBOSSL_trace_begin()\fR is used to starts a tracing section, and get the
  156. channel for the given \fIcategory\fR in form of a BIO.
  157. This BIO can only be used for output.
  158. .PP
  159. \&\fBOSSL_trace_end()\fR is used to end a tracing section.
  160. .PP
  161. Using \fBOSSL_trace_begin()\fR and \fBOSSL_trace_end()\fR to wrap tracing sections
  162. is \fImandatory\fR.
  163. The result of trying to produce tracing output outside of such
  164. sections is undefined.
  165. .PP
  166. \&\fBOSSL_trace_string()\fR outputs \fIdata\fR of length \fIsize\fR as a string on BIO \fIout\fR.
  167. If \fItext\fR is 0, the function masks any included control characters apart from
  168. newlines and makes sure for nonempty input that the output ends with a newline.
  169. Unless \fIfull\fR is nonzero, the length is limited (with a suitable warning)
  170. to \fBOSSL_TRACE_STRING_MAX\fR characters, which currently is 80.
  171. .SS Macros
  172. .IX Subsection "Macros"
  173. There are a number of convenience macros defined, to make tracing
  174. easy and consistent.
  175. .PP
  176. \&\fBOSSL_TRACE_BEGIN()\fR and \fBOSSL_TRACE_END()\fR reserve the \fBBIO\fR \f(CW\*(C`trc_out\*(C'\fR and are
  177. used as follows to wrap a trace section:
  178. .PP
  179. .Vb 1
  180. \& OSSL_TRACE_BEGIN(TLS) {
  181. \&
  182. \& BIO_printf(trc_out, ... );
  183. \&
  184. \& } OSSL_TRACE_END(TLS);
  185. .Ve
  186. .PP
  187. This will normally expand to:
  188. .PP
  189. .Vb 8
  190. \& do {
  191. \& BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_TLS);
  192. \& if (trc_out != NULL) {
  193. \& ...
  194. \& BIO_printf(trc_out, ...);
  195. \& }
  196. \& OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
  197. \& } while (0);
  198. .Ve
  199. .PP
  200. \&\fBOSSL_TRACE_CANCEL()\fR must be used before returning from or jumping out of a
  201. trace section:
  202. .PP
  203. .Vb 1
  204. \& OSSL_TRACE_BEGIN(TLS) {
  205. \&
  206. \& if (some_error) {
  207. \& OSSL_TRACE_CANCEL(TLS);
  208. \& goto err;
  209. \& }
  210. \& BIO_printf(trc_out, ... );
  211. \&
  212. \& } OSSL_TRACE_END(TLS);
  213. .Ve
  214. .PP
  215. This will normally expand to:
  216. .PP
  217. .Vb 11
  218. \& do {
  219. \& BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_TLS);
  220. \& if (trc_out != NULL) {
  221. \& if (some_error) {
  222. \& OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
  223. \& goto err;
  224. \& }
  225. \& BIO_printf(trc_out, ... );
  226. \& }
  227. \& OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
  228. \& } while (0);
  229. .Ve
  230. .PP
  231. \&\fBOSSL_TRACE()\fR and \fBOSSL_TRACE1()\fR, \fBOSSL_TRACE2()\fR, ... \fBOSSL_TRACE9()\fR are
  232. so-called one-shot macros:
  233. .PP
  234. The macro call \f(CW\*(C`OSSL_TRACE(category, text)\*(C'\fR, produces literal text trace output.
  235. .PP
  236. The macro call \f(CW\*(C`OSSL_TRACEn(category, format, arg1, ..., argn)\*(C'\fR produces
  237. printf-style trace output with n format field arguments (n=1,...,9).
  238. It expands to:
  239. .PP
  240. .Vb 3
  241. \& OSSL_TRACE_BEGIN(category) {
  242. \& BIO_printf(trc_out, format, arg1, ..., argN);
  243. \& } OSSL_TRACE_END(category)
  244. .Ve
  245. .PP
  246. Internally, all one-shot macros are implemented using a generic \fBOSSL_TRACEV()\fR
  247. macro, since C90 does not support variadic macros. This helper macro has a rather
  248. weird synopsis and should not be used directly.
  249. .PP
  250. The macro call \f(CW\*(C`OSSL_TRACE_STRING(category, text, full, data, len)\*(C'\fR
  251. outputs \fIdata\fR of length \fIsize\fR as a string
  252. if tracing for the given \fIcategory\fR is enabled.
  253. It expands to:
  254. .PP
  255. .Vb 3
  256. \& OSSL_TRACE_BEGIN(category) {
  257. \& OSSL_trace_string(trc_out, text, full, data, len);
  258. \& } OSSL_TRACE_END(category)
  259. .Ve
  260. .PP
  261. The \fBOSSL_TRACE_ENABLED()\fR macro can be used to conditionally execute some code
  262. only if a specific trace category is enabled.
  263. In some situations this is simpler than entering a trace section using
  264. \&\fBOSSL_TRACE_BEGIN()\fR and \fBOSSL_TRACE_END()\fR.
  265. For example, the code
  266. .PP
  267. .Vb 3
  268. \& if (OSSL_TRACE_ENABLED(TLS)) {
  269. \& ...
  270. \& }
  271. .Ve
  272. .PP
  273. expands to
  274. .PP
  275. .Vb 3
  276. \& if (OSSL_trace_enabled(OSSL_TRACE_CATEGORY_TLS) {
  277. \& ...
  278. \& }
  279. .Ve
  280. .SH NOTES
  281. .IX Header "NOTES"
  282. If producing the trace output requires carrying out auxiliary calculations,
  283. this auxiliary code should be placed inside a conditional block which is
  284. executed only if the trace category is enabled.
  285. .PP
  286. The most natural way to do this is to place the code inside the trace section
  287. itself because it already introduces such a conditional block.
  288. .PP
  289. .Vb 2
  290. \& OSSL_TRACE_BEGIN(TLS) {
  291. \& int var = do_some_auxiliary_calculation();
  292. \&
  293. \& BIO_printf(trc_out, "var = %d\en", var);
  294. \&
  295. \& } OSSL_TRACE_END(TLS);
  296. .Ve
  297. .PP
  298. In some cases it is more advantageous to use a simple conditional group instead
  299. of a trace section. This is the case if calculations and tracing happen in
  300. different locations of the code, or if the calculations are so time consuming
  301. that placing them inside a (critical) trace section would create too much
  302. contention.
  303. .PP
  304. .Vb 2
  305. \& if (OSSL_TRACE_ENABLED(TLS)) {
  306. \& int var = do_some_auxiliary_calculation();
  307. \&
  308. \& OSSL_TRACE1("var = %d\en", var);
  309. \& }
  310. .Ve
  311. .PP
  312. Note however that premature optimization of tracing code is in general futile
  313. and it's better to keep the tracing code as simple as possible.
  314. Because most often the limiting factor for the application's speed is the time
  315. it takes to print the trace output, not to calculate it.
  316. .SS "Configure Tracing"
  317. .IX Subsection "Configure Tracing"
  318. By default, the OpenSSL library is built with tracing disabled. To
  319. use the tracing functionality documented here, it is therefore
  320. necessary to configure and build OpenSSL with the 'enable\-trace' option.
  321. .PP
  322. When the library is built with tracing disabled:
  323. .IP \(bu 4
  324. The macro \fBOPENSSL_NO_TRACE\fR is defined in \fI<openssl/opensslconf.h>\fR.
  325. .IP \(bu 4
  326. all functions are still present, but \fBOSSL_trace_enabled()\fR will always
  327. report the categories as disabled, and all other functions will do
  328. nothing.
  329. .IP \(bu 4
  330. the convenience macros are defined to produce dead code.
  331. For example, take this example from "Macros" section above:
  332. .Sp
  333. .Vb 1
  334. \& OSSL_TRACE_BEGIN(TLS) {
  335. \&
  336. \& if (condition) {
  337. \& OSSL_TRACE_CANCEL(TLS);
  338. \& goto err;
  339. \& }
  340. \& BIO_printf(trc_out, ... );
  341. \&
  342. \& } OSSL_TRACE_END(TLS);
  343. .Ve
  344. .Sp
  345. When the tracing API isn't operational, that will expand to:
  346. .Sp
  347. .Vb 10
  348. \& do {
  349. \& BIO *trc_out = NULL;
  350. \& if (0) {
  351. \& if (condition) {
  352. \& ((void)0);
  353. \& goto err;
  354. \& }
  355. \& BIO_printf(trc_out, ... );
  356. \& }
  357. \& } while (0);
  358. .Ve
  359. .SH "RETURN VALUES"
  360. .IX Header "RETURN VALUES"
  361. \&\fBOSSL_trace_enabled()\fR returns 1 if tracing for the given \fItype\fR is
  362. operational and enabled, otherwise 0.
  363. .PP
  364. \&\fBOSSL_trace_begin()\fR returns a \fBBIO\fR pointer if the given \fItype\fR is enabled,
  365. otherwise NULL.
  366. .PP
  367. \&\fBOSSL_trace_string()\fR returns the number of characters emitted, or \-1 on error.
  368. .SH "SEE ALSO"
  369. .IX Header "SEE ALSO"
  370. \&\fBOSSL_trace_set_channel\fR\|(3), \fBOSSL_trace_set_callback\fR\|(3)
  371. .SH HISTORY
  372. .IX Header "HISTORY"
  373. The OpenSSL Tracing API was added in OpenSSL 3.0.
  374. .PP
  375. \&\fBOSSL_TRACE_STRING()\fR, OSSL_TRACE_STRING_MAX, and OSSL_trace_string
  376. were added in OpenSSL 3.2.
  377. .SH COPYRIGHT
  378. .IX Header "COPYRIGHT"
  379. Copyright 2019\-2023 The OpenSSL Project Authors. All Rights Reserved.
  380. .PP
  381. Licensed under the Apache License 2.0 (the "License"). You may not use
  382. this file except in compliance with the License. You can obtain a copy
  383. in the file LICENSE in the source distribution or at
  384. <https://www.openssl.org/source/license.html>.