LCOV - code coverage report
Current view: top level - lib/Support - Debug.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 2 14 14.3 %
Date: 2018-07-13 00:08:38 Functions: 1 4 25.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===-- Debug.cpp - An easy way to add debug output to your code ----------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file implements a handy way of adding debugging information to your
      11             : // code, without it being enabled all of the time, and without having to add
      12             : // command line options to enable it.
      13             : //
      14             : // In particular, just wrap your code with the LLVM_DEBUG() macro, and it will
      15             : // be enabled automatically if you specify '-debug' on the command-line.
      16             : // Alternatively, you can also use the SET_DEBUG_TYPE("foo") macro to specify
      17             : // that your debug code belongs to class "foo".  Then, on the command line, you
      18             : // can specify '-debug-only=foo' to enable JUST the debug information for the
      19             : // foo class.
      20             : //
      21             : // When compiling without assertions, the -debug-* options and all code in
      22             : // LLVM_DEBUG() statements disappears, so it does not affect the runtime of the
      23             : // code.
      24             : //
      25             : //===----------------------------------------------------------------------===//
      26             : 
      27             : #include "llvm/Support/Debug.h"
      28             : #include "llvm/Support/CommandLine.h"
      29             : #include "llvm/Support/ManagedStatic.h"
      30             : #include "llvm/Support/Signals.h"
      31             : #include "llvm/Support/circular_raw_ostream.h"
      32             : #include "llvm/Support/raw_ostream.h"
      33             : 
      34             : #undef isCurrentDebugType
      35             : #undef setCurrentDebugType
      36             : #undef setCurrentDebugTypes
      37             : 
      38             : using namespace llvm;
      39             : 
      40             : // Even though LLVM might be built with NDEBUG, define symbols that the code
      41             : // built without NDEBUG can depend on via the llvm/Support/Debug.h header.
      42             : namespace llvm {
      43             : /// Exported boolean set by the -debug option.
      44             : bool DebugFlag = false;
      45             : 
      46             : static ManagedStatic<std::vector<std::string>> CurrentDebugType;
      47             : 
      48             : /// Return true if the specified string is the debug type
      49             : /// specified on the command line, or if none was specified on the command line
      50             : /// with the -debug-only=X option.
      51           0 : bool isCurrentDebugType(const char *DebugType) {
      52           0 :   if (CurrentDebugType->empty())
      53             :     return true;
      54             :   // See if DebugType is in list. Note: do not use find() as that forces us to
      55             :   // unnecessarily create an std::string instance.
      56           0 :   for (auto &d : *CurrentDebugType) {
      57           0 :     if (d == DebugType)
      58             :       return true;
      59             :   }
      60             :   return false;
      61             : }
      62             : 
      63             : /// Set the current debug type, as if the -debug-only=X
      64             : /// option were specified.  Note that DebugFlag also needs to be set to true for
      65             : /// debug output to be produced.
      66             : ///
      67             : void setCurrentDebugTypes(const char **Types, unsigned Count);
      68             : 
      69           0 : void setCurrentDebugType(const char *Type) {
      70           0 :   setCurrentDebugTypes(&Type, 1);
      71           0 : }
      72             : 
      73           0 : void setCurrentDebugTypes(const char **Types, unsigned Count) {
      74           0 :   CurrentDebugType->clear();
      75           0 :   for (size_t T = 0; T < Count; ++T)
      76           0 :     CurrentDebugType->push_back(Types[T]);
      77           0 : }
      78             : } // namespace llvm
      79             : 
      80             : // All Debug.h functionality is a no-op in NDEBUG mode.
      81             : #ifndef NDEBUG
      82             : 
      83             : // -debug - Command line option to enable the DEBUG statements in the passes.
      84             : // This flag may only be enabled in debug builds.
      85             : static cl::opt<bool, true>
      86             : Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
      87             :       cl::location(DebugFlag));
      88             : 
      89             : // -debug-buffer-size - Buffer the last N characters of debug output
      90             : //until program termination.
      91             : static cl::opt<unsigned>
      92             : DebugBufferSize("debug-buffer-size",
      93             :                 cl::desc("Buffer the last N characters of debug output "
      94             :                          "until program termination. "
      95             :                          "[default 0 -- immediate print-out]"),
      96             :                 cl::Hidden,
      97             :                 cl::init(0));
      98             : 
      99             : namespace {
     100             : 
     101             : struct DebugOnlyOpt {
     102             :   void operator=(const std::string &Val) const {
     103             :     if (Val.empty())
     104             :       return;
     105             :     DebugFlag = true;
     106             :     SmallVector<StringRef,8> dbgTypes;
     107             :     StringRef(Val).split(dbgTypes, ',', -1, false);
     108             :     for (auto dbgType : dbgTypes)
     109             :       CurrentDebugType->push_back(dbgType);
     110             :   }
     111             : };
     112             : 
     113             : }
     114             : 
     115             : static DebugOnlyOpt DebugOnlyOptLoc;
     116             : 
     117             : static cl::opt<DebugOnlyOpt, true, cl::parser<std::string> >
     118             : DebugOnly("debug-only", cl::desc("Enable a specific type of debug output (comma separated list of types)"),
     119             :           cl::Hidden, cl::ZeroOrMore, cl::value_desc("debug string"),
     120             :           cl::location(DebugOnlyOptLoc), cl::ValueRequired);
     121             : // Signal handlers - dump debug output on termination.
     122             : static void debug_user_sig_handler(void *Cookie) {
     123             :   // This is a bit sneaky.  Since this is under #ifndef NDEBUG, we
     124             :   // know that debug mode is enabled and dbgs() really is a
     125             :   // circular_raw_ostream.  If NDEBUG is defined, then dbgs() ==
     126             :   // errs() but this will never be invoked.
     127             :   llvm::circular_raw_ostream &dbgout =
     128             :       static_cast<circular_raw_ostream &>(llvm::dbgs());
     129             :   dbgout.flushBufferWithBanner();
     130             : }
     131             : 
     132             : /// dbgs - Return a circular-buffered debug stream.
     133             : raw_ostream &llvm::dbgs() {
     134             :   // Do one-time initialization in a thread-safe way.
     135             :   static struct dbgstream {
     136             :     circular_raw_ostream strm;
     137             : 
     138             :     dbgstream() :
     139             :         strm(errs(), "*** Debug Log Output ***\n",
     140             :              (!EnableDebugBuffering || !DebugFlag) ? 0 : DebugBufferSize) {
     141             :       if (EnableDebugBuffering && DebugFlag && DebugBufferSize != 0)
     142             :         // TODO: Add a handler for SIGUSER1-type signals so the user can
     143             :         // force a debug dump.
     144             :         sys::AddSignalHandler(&debug_user_sig_handler, nullptr);
     145             :       // Otherwise we've already set the debug stream buffer size to
     146             :       // zero, disabling buffering so it will output directly to errs().
     147             :     }
     148             :   } thestrm;
     149             : 
     150             :   return thestrm.strm;
     151             : }
     152             : 
     153             : #else
     154             : // Avoid "has no symbols" warning.
     155             : namespace llvm {
     156             :   /// dbgs - Return errs().
     157       88952 :   raw_ostream &dbgs() {
     158       88952 :     return errs();
     159             :   }
     160             : }
     161             : 
     162             : #endif
     163             : 
     164             : /// EnableDebugBuffering - Turn on signal handler installation.
     165             : ///
     166             : bool llvm::EnableDebugBuffering = false;

Generated by: LCOV version 1.13