trace.h raw

   1  // Copyright (c) 2020-present The Bitcoin Core developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #ifndef BITCOIN_UTIL_TRACE_H
   6  #define BITCOIN_UTIL_TRACE_H
   7  
   8  #include <bitcoin-build-config.h> // IWYU pragma: keep
   9  
  10  #ifdef ENABLE_TRACING
  11  
  12  // Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103395
  13  // systemtap 4.6 on 32-bit ARM triggers internal compiler error
  14  // (this workaround is included in systemtap 4.7+)
  15  #if defined(__arm__)
  16  #  define STAP_SDT_ARG_CONSTRAINT g
  17  #endif
  18  
  19  // Setting SDT_USE_VARIADIC lets systemtap (sys/sdt.h) know that we want to use
  20  // the optional variadic macros to define tracepoints.
  21  #define SDT_USE_VARIADIC 1
  22  
  23  // Setting _SDT_HAS_SEMAPHORES let's systemtap (sys/sdt.h) know that we want to
  24  // use the optional semaphore feature for our tracepoints. This feature allows
  25  // us to check if something is attached to a tracepoint. We only want to prepare
  26  // some potentially expensive tracepoint arguments, if the tracepoint is being
  27  // used. Here, an expensive argument preparation could, for example, be
  28  // calculating a hash or serialization of a data structure.
  29  #define _SDT_HAS_SEMAPHORES 1
  30  
  31  // Used to define a counting semaphore for a tracepoint. This semaphore is
  32  // automatically incremented by tracing frameworks (bpftrace, bcc, libbpf, ...)
  33  // upon attaching to the tracepoint and decremented when detaching. This needs
  34  // to be a global variable. It's placed in the '.probes' ELF section.
  35  #define TRACEPOINT_SEMAPHORE(context, event) \
  36      unsigned short context##_##event##_semaphore __attribute__((section(".probes")))
  37  
  38  #include <sys/sdt.h>
  39  
  40  // Returns true if something is attached to the tracepoint.
  41  #define TRACEPOINT_ACTIVE(context, event) (context##_##event##_semaphore > 0)
  42  
  43  // A USDT tracepoint with one to twelve arguments. It's checked that the
  44  // tracepoint is active before preparing its arguments.
  45  #define TRACEPOINT(context, event, ...)                                         \
  46      do {                                                                        \
  47          if (TRACEPOINT_ACTIVE(context, event)) {                                \
  48              STAP_PROBEV(context, event __VA_OPT__(, ) __VA_ARGS__);             \
  49          }                                                                       \
  50      } while(0)
  51  
  52  #else
  53  
  54  #define TRACEPOINT_SEMAPHORE(context, event)
  55  #define TRACEPOINT_ACTIVE(context, event) false
  56  #define TRACEPOINT(context, ...)
  57  
  58  #endif // ENABLE_TRACING
  59  
  60  
  61  #endif // BITCOIN_UTIL_TRACE_H
  62