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