trace.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OPENSSL_TRACE_H
  10. # define OPENSSL_TRACE_H
  11. # include <stdarg.h>
  12. # include <openssl/bio.h>
  13. # ifdef __cplusplus
  14. extern "C" {
  15. # endif
  16. /*
  17. * TRACE CATEGORIES
  18. */
  19. /*
  20. * The trace messages of the OpenSSL libraries are organized into different
  21. * categories. For every trace category, the application can register a separate
  22. * tracer callback. When a callback is registered, a so called trace channel is
  23. * created for this category. This channel consists essentially of an internal
  24. * BIO which sends all trace output it receives to the registered application
  25. * callback.
  26. *
  27. * The ALL category can be used as a fallback category to register a single
  28. * channel which receives the output from all categories. However, if the
  29. * application intends to print the trace channel name in the line prefix,
  30. * it is better to register channels for all categories separately.
  31. * (This is how the openssl application does it.)
  32. */
  33. # define OSSL_TRACE_CATEGORY_ALL 0 /* The fallback */
  34. # define OSSL_TRACE_CATEGORY_TRACE 1
  35. # define OSSL_TRACE_CATEGORY_INIT 2
  36. # define OSSL_TRACE_CATEGORY_TLS 3
  37. # define OSSL_TRACE_CATEGORY_TLS_CIPHER 4
  38. # define OSSL_TRACE_CATEGORY_CONF 5
  39. # define OSSL_TRACE_CATEGORY_ENGINE_TABLE 6
  40. # define OSSL_TRACE_CATEGORY_ENGINE_REF_COUNT 7
  41. # define OSSL_TRACE_CATEGORY_PKCS5V2 8
  42. # define OSSL_TRACE_CATEGORY_PKCS12_KEYGEN 9
  43. # define OSSL_TRACE_CATEGORY_PKCS12_DECRYPT 10
  44. # define OSSL_TRACE_CATEGORY_X509V3_POLICY 11
  45. # define OSSL_TRACE_CATEGORY_BN_CTX 12
  46. # define OSSL_TRACE_CATEGORY_CMP 13
  47. # define OSSL_TRACE_CATEGORY_STORE 14
  48. # define OSSL_TRACE_CATEGORY_NUM 15
  49. /* Returns the trace category number for the given |name| */
  50. int OSSL_trace_get_category_num(const char *name);
  51. /* Returns the trace category name for the given |num| */
  52. const char *OSSL_trace_get_category_name(int num);
  53. /*
  54. * TRACE CONSUMERS
  55. */
  56. /*
  57. * Enables tracing for the given |category| by providing a BIO sink
  58. * as |channel|. If a null pointer is passed as |channel|, an existing
  59. * trace channel is removed and tracing for the category is disabled.
  60. *
  61. * Returns 1 on success and 0 on failure
  62. */
  63. int OSSL_trace_set_channel(int category, BIO* channel);
  64. /*
  65. * Attach a prefix and a suffix to the given |category|, to be printed at the
  66. * beginning and at the end of each trace output group, i.e. when
  67. * OSSL_trace_begin() and OSSL_trace_end() are called.
  68. * If a null pointer is passed as argument, the existing prefix or suffix is
  69. * removed.
  70. *
  71. * They return 1 on success and 0 on failure
  72. */
  73. int OSSL_trace_set_prefix(int category, const char *prefix);
  74. int OSSL_trace_set_suffix(int category, const char *suffix);
  75. /*
  76. * OSSL_trace_cb is the type tracing callback provided by the application.
  77. * It MUST return the number of bytes written, or 0 on error (in other words,
  78. * it can never write zero bytes).
  79. *
  80. * The |buffer| will always contain text, which may consist of several lines.
  81. * The |data| argument points to whatever data was provided by the application
  82. * when registering the tracer function.
  83. *
  84. * The |category| number is given, as well as a |cmd| number, described below.
  85. */
  86. typedef size_t (*OSSL_trace_cb)(const char *buffer, size_t count,
  87. int category, int cmd, void *data);
  88. /*
  89. * Possible |cmd| numbers.
  90. */
  91. # define OSSL_TRACE_CTRL_BEGIN 0
  92. # define OSSL_TRACE_CTRL_WRITE 1
  93. # define OSSL_TRACE_CTRL_END 2
  94. /*
  95. * Enables tracing for the given |category| by creating an internal
  96. * trace channel which sends the output to the given |callback|.
  97. * If a null pointer is passed as callback, an existing trace channel
  98. * is removed and tracing for the category is disabled.
  99. *
  100. * NOTE: OSSL_trace_set_channel() and OSSL_trace_set_callback() are mutually
  101. * exclusive.
  102. *
  103. * Returns 1 on success and 0 on failure
  104. */
  105. int OSSL_trace_set_callback(int category, OSSL_trace_cb callback, void *data);
  106. /*
  107. * TRACE PRODUCERS
  108. */
  109. /*
  110. * Returns 1 if tracing for the specified category is enabled, otherwise 0
  111. */
  112. int OSSL_trace_enabled(int category);
  113. /*
  114. * Wrap a group of tracing output calls. OSSL_trace_begin() locks tracing and
  115. * returns the trace channel associated with the given category, or NULL if no
  116. * channel is associated with the category. OSSL_trace_end() unlocks tracing.
  117. *
  118. * Usage:
  119. *
  120. * BIO *out;
  121. * if ((out = OSSL_trace_begin(category)) != NULL) {
  122. * ...
  123. * BIO_fprintf(out, ...);
  124. * ...
  125. * OSSL_trace_end(category, out);
  126. * }
  127. *
  128. * See also the convenience macros OSSL_TRACE_BEGIN and OSSL_TRACE_END below.
  129. */
  130. BIO *OSSL_trace_begin(int category);
  131. void OSSL_trace_end(int category, BIO *channel);
  132. /*
  133. * OSSL_TRACE* Convenience Macros
  134. */
  135. /*
  136. * When the tracing feature is disabled, these macros are defined to
  137. * produce dead code, which a good compiler should eliminate.
  138. */
  139. /*
  140. * OSSL_TRACE_BEGIN, OSSL_TRACE_END - Define a Trace Group
  141. *
  142. * These two macros can be used to create a block which is executed only
  143. * if the corresponding trace category is enabled. Inside this block, a
  144. * local variable named |trc_out| is defined, which points to the channel
  145. * associated with the given trace category.
  146. *
  147. * Usage: (using 'TLS' as an example category)
  148. *
  149. * OSSL_TRACE_BEGIN(TLS) {
  150. *
  151. * BIO_fprintf(trc_out, ... );
  152. *
  153. * } OSSL_TRACE_END(TLS);
  154. *
  155. *
  156. * This expands to the following code
  157. *
  158. * do {
  159. * BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_TLS);
  160. * if (trc_out != NULL) {
  161. * ...
  162. * BIO_fprintf(trc_out, ...);
  163. * }
  164. * OSSL_trace_end(OSSL_TRACE_CATEGORY_TLS, trc_out);
  165. * } while (0);
  166. *
  167. * The use of the inner '{...}' group and the trailing ';' is enforced
  168. * by the definition of the macros in order to make the code look as much
  169. * like C code as possible.
  170. *
  171. * Before returning from inside the trace block, it is necessary to
  172. * call OSSL_TRACE_CANCEL(category).
  173. */
  174. # ifndef OPENSSL_NO_TRACE
  175. # define OSSL_TRACE_BEGIN(category) \
  176. do { \
  177. BIO *trc_out = OSSL_trace_begin(OSSL_TRACE_CATEGORY_##category); \
  178. \
  179. if (trc_out != NULL)
  180. # define OSSL_TRACE_END(category) \
  181. OSSL_trace_end(OSSL_TRACE_CATEGORY_##category, trc_out); \
  182. } while (0)
  183. # define OSSL_TRACE_CANCEL(category) \
  184. OSSL_trace_end(OSSL_TRACE_CATEGORY_##category, trc_out) \
  185. # else
  186. # define OSSL_TRACE_BEGIN(category) \
  187. do { \
  188. BIO *trc_out = NULL; \
  189. if (0)
  190. # define OSSL_TRACE_END(category) \
  191. } while(0)
  192. # define OSSL_TRACE_CANCEL(category) \
  193. ((void)0)
  194. # endif
  195. /*
  196. * OSSL_TRACE_ENABLED() - Check whether tracing is enabled for |category|
  197. *
  198. * Usage:
  199. *
  200. * if (OSSL_TRACE_ENABLED(TLS)) {
  201. * ...
  202. * }
  203. */
  204. # ifndef OPENSSL_NO_TRACE
  205. # define OSSL_TRACE_ENABLED(category) \
  206. OSSL_trace_enabled(OSSL_TRACE_CATEGORY_##category)
  207. # else
  208. # define OSSL_TRACE_ENABLED(category) (0)
  209. # endif
  210. /*
  211. * OSSL_TRACE*() - OneShot Trace Macros
  212. *
  213. * These macros are intended to produce a simple printf-style trace output.
  214. * Unfortunately, C90 macros don't support variable arguments, so the
  215. * "vararg" OSSL_TRACEV() macro has a rather weird usage pattern:
  216. *
  217. * OSSL_TRACEV(category, (trc_out, "format string", ...args...));
  218. *
  219. * Where 'channel' is the literal symbol of this name, not a variable.
  220. * For that reason, it is currently not intended to be used directly,
  221. * but only as helper macro for the other oneshot trace macros
  222. * OSSL_TRACE(), OSSL_TRACE1(), OSSL_TRACE2(), ...
  223. *
  224. * Usage:
  225. *
  226. * OSSL_TRACE(INIT, "Hello world!\n");
  227. * OSSL_TRACE1(TLS, "The answer is %d\n", 42);
  228. * OSSL_TRACE2(TLS, "The ultimate question to answer %d is '%s'\n",
  229. * 42, "What do you get when you multiply six by nine?");
  230. */
  231. # define OSSL_TRACEV(category, args) \
  232. OSSL_TRACE_BEGIN(category) \
  233. BIO_printf args; \
  234. OSSL_TRACE_END(category)
  235. # define OSSL_TRACE(category, text) \
  236. OSSL_TRACEV(category, (trc_out, "%s", text))
  237. # define OSSL_TRACE1(category, format, arg1) \
  238. OSSL_TRACEV(category, (trc_out, format, arg1))
  239. # define OSSL_TRACE2(category, format, arg1, arg2) \
  240. OSSL_TRACEV(category, (trc_out, format, arg1, arg2))
  241. # define OSSL_TRACE3(category, format, arg1, arg2, arg3) \
  242. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3))
  243. # define OSSL_TRACE4(category, format, arg1, arg2, arg3, arg4) \
  244. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4))
  245. # define OSSL_TRACE5(category, format, arg1, arg2, arg3, arg4, arg5) \
  246. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5))
  247. # define OSSL_TRACE6(category, format, arg1, arg2, arg3, arg4, arg5, arg6) \
  248. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6))
  249. # define OSSL_TRACE7(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7) \
  250. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7))
  251. # define OSSL_TRACE8(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
  252. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
  253. # define OSSL_TRACE9(category, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8) \
  254. OSSL_TRACEV(category, (trc_out, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9))
  255. # ifdef __cplusplus
  256. }
  257. # endif
  258. #endif