Home | History | Annotate | Download | only in Support
      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 DEBUG() macro, and it will be
     15 // 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 // DEBUG() statements disappears, so it does not affect the runtime of the code.
     23 //
     24 //===----------------------------------------------------------------------===//
     25 
     26 #include "llvm/Support/Debug.h"
     27 #include "llvm/Support/CommandLine.h"
     28 #include "llvm/Support/Signals.h"
     29 #include "llvm/Support/circular_raw_ostream.h"
     30 
     31 using namespace llvm;
     32 
     33 // All Debug.h functionality is a no-op in NDEBUG mode.
     34 #ifndef NDEBUG
     35 bool llvm::DebugFlag;  // DebugFlag - Exported boolean set by the -debug option
     36 
     37 // -debug - Command line option to enable the DEBUG statements in the passes.
     38 // This flag may only be enabled in debug builds.
     39 static cl::opt<bool, true>
     40 Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
     41       cl::location(DebugFlag));
     42 
     43 // -debug-buffer-size - Buffer the last N characters of debug output
     44 //until program termination.
     45 static cl::opt<unsigned>
     46 DebugBufferSize("debug-buffer-size",
     47                 cl::desc("Buffer the last N characters of debug output "
     48                          "until program termination. "
     49                          "[default 0 -- immediate print-out]"),
     50                 cl::Hidden,
     51                 cl::init(0));
     52 
     53 static std::string CurrentDebugType;
     54 
     55 namespace {
     56 
     57 struct DebugOnlyOpt {
     58   void operator=(const std::string &Val) const {
     59     DebugFlag |= !Val.empty();
     60     CurrentDebugType = Val;
     61   }
     62 };
     63 
     64 }
     65 
     66 static DebugOnlyOpt DebugOnlyOptLoc;
     67 
     68 static cl::opt<DebugOnlyOpt, true, cl::parser<std::string> >
     69 DebugOnly("debug-only", cl::desc("Enable a specific type of debug output"),
     70           cl::Hidden, cl::value_desc("debug string"),
     71           cl::location(DebugOnlyOptLoc), cl::ValueRequired);
     72 
     73 // Signal handlers - dump debug output on termination.
     74 static void debug_user_sig_handler(void *Cookie) {
     75   // This is a bit sneaky.  Since this is under #ifndef NDEBUG, we
     76   // know that debug mode is enabled and dbgs() really is a
     77   // circular_raw_ostream.  If NDEBUG is defined, then dbgs() ==
     78   // errs() but this will never be invoked.
     79   llvm::circular_raw_ostream *dbgout =
     80     static_cast<llvm::circular_raw_ostream *>(&llvm::dbgs());
     81   dbgout->flushBufferWithBanner();
     82 }
     83 
     84 // isCurrentDebugType - Return true if the specified string is the debug type
     85 // specified on the command line, or if none was specified on the command line
     86 // with the -debug-only=X option.
     87 //
     88 bool llvm::isCurrentDebugType(const char *DebugType) {
     89   return CurrentDebugType.empty() || DebugType == CurrentDebugType;
     90 }
     91 
     92 /// setCurrentDebugType - Set the current debug type, as if the -debug-only=X
     93 /// option were specified.  Note that DebugFlag also needs to be set to true for
     94 /// debug output to be produced.
     95 ///
     96 void llvm::setCurrentDebugType(const char *Type) {
     97   CurrentDebugType = Type;
     98 }
     99 
    100 /// dbgs - Return a circular-buffered debug stream.
    101 raw_ostream &llvm::dbgs() {
    102   // Do one-time initialization in a thread-safe way.
    103   static struct dbgstream {
    104     circular_raw_ostream strm;
    105 
    106     dbgstream() :
    107         strm(errs(), "*** Debug Log Output ***\n",
    108              (!EnableDebugBuffering || !DebugFlag) ? 0 : DebugBufferSize) {
    109       if (EnableDebugBuffering && DebugFlag && DebugBufferSize != 0)
    110         // TODO: Add a handler for SIGUSER1-type signals so the user can
    111         // force a debug dump.
    112         sys::AddSignalHandler(&debug_user_sig_handler, 0);
    113       // Otherwise we've already set the debug stream buffer size to
    114       // zero, disabling buffering so it will output directly to errs().
    115     }
    116   } thestrm;
    117 
    118   return thestrm.strm;
    119 }
    120 
    121 #else
    122 // Avoid "has no symbols" warning.
    123 namespace llvm {
    124   /// dbgs - Return errs().
    125   raw_ostream &dbgs() {
    126     return errs();
    127   }
    128 }
    129 
    130 #endif
    131 
    132 /// EnableDebugBuffering - Turn on signal handler installation.
    133 ///
    134 bool llvm::EnableDebugBuffering = false;
    135