LLVM API Documentation

Debug.cpp
Go to the documentation of this file.
00001 //===-- Debug.cpp - An easy way to add debug output to your code ----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements a handy way of adding debugging information to your
00011 // code, without it being enabled all of the time, and without having to add
00012 // command line options to enable it.
00013 //
00014 // In particular, just wrap your code with the DEBUG() macro, and it will be
00015 // enabled automatically if you specify '-debug' on the command-line.
00016 // Alternatively, you can also use the SET_DEBUG_TYPE("foo") macro to specify
00017 // that your debug code belongs to class "foo".  Then, on the command line, you
00018 // can specify '-debug-only=foo' to enable JUST the debug information for the
00019 // foo class.
00020 //
00021 // When compiling without assertions, the -debug-* options and all code in
00022 // DEBUG() statements disappears, so it does not affect the runtime of the code.
00023 //
00024 //===----------------------------------------------------------------------===//
00025 
00026 #include "llvm/Support/Debug.h"
00027 #include "llvm/Support/CommandLine.h"
00028 #include "llvm/Support/Signals.h"
00029 #include "llvm/Support/circular_raw_ostream.h"
00030 #include "llvm/Support/ManagedStatic.h"
00031 
00032 using namespace llvm;
00033 
00034 // All Debug.h functionality is a no-op in NDEBUG mode.
00035 #ifndef NDEBUG
00036 bool llvm::DebugFlag;  // DebugFlag - Exported boolean set by the -debug option
00037 
00038 // -debug - Command line option to enable the DEBUG statements in the passes.
00039 // This flag may only be enabled in debug builds.
00040 static cl::opt<bool, true>
00041 Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
00042       cl::location(DebugFlag));
00043 
00044 // -debug-buffer-size - Buffer the last N characters of debug output
00045 //until program termination.
00046 static cl::opt<unsigned>
00047 DebugBufferSize("debug-buffer-size",
00048                 cl::desc("Buffer the last N characters of debug output "
00049                          "until program termination. "
00050                          "[default 0 -- immediate print-out]"),
00051                 cl::Hidden,
00052                 cl::init(0));
00053 
00054 static ManagedStatic<std::vector<std::string> > CurrentDebugType;
00055 
00056 namespace {
00057 
00058 struct DebugOnlyOpt {
00059   void operator=(const std::string &Val) const {
00060     if (Val.empty())
00061       return;
00062     DebugFlag = true;
00063     CurrentDebugType->push_back(Val);
00064   }
00065 };
00066 
00067 }
00068 
00069 static DebugOnlyOpt DebugOnlyOptLoc;
00070 
00071 static cl::opt<DebugOnlyOpt, true, cl::parser<std::string> >
00072 DebugOnly("debug-only", cl::desc("Enable a specific type of debug output"),
00073           cl::Hidden, cl::ZeroOrMore, cl::value_desc("debug string"),
00074           cl::location(DebugOnlyOptLoc), cl::ValueRequired);
00075 
00076 // Signal handlers - dump debug output on termination.
00077 static void debug_user_sig_handler(void *Cookie) {
00078   // This is a bit sneaky.  Since this is under #ifndef NDEBUG, we
00079   // know that debug mode is enabled and dbgs() really is a
00080   // circular_raw_ostream.  If NDEBUG is defined, then dbgs() ==
00081   // errs() but this will never be invoked.
00082   llvm::circular_raw_ostream *dbgout =
00083     static_cast<llvm::circular_raw_ostream *>(&llvm::dbgs());
00084   dbgout->flushBufferWithBanner();
00085 }
00086 
00087 // isCurrentDebugType - Return true if the specified string is the debug type
00088 // specified on the command line, or if none was specified on the command line
00089 // with the -debug-only=X option.
00090 //
00091 bool llvm::isCurrentDebugType(const char *DebugType) {
00092   if (CurrentDebugType->empty())
00093     return true;
00094   // see if DebugType is in list. Note: do not use find() as that forces us to
00095   // unnecessarily create an std::string instance.
00096   for (auto d : *CurrentDebugType) {
00097     if (d == DebugType)
00098       return true;
00099   }
00100   return false;
00101 }
00102 
00103 /// setCurrentDebugType - Set the current debug type, as if the -debug-only=X
00104 /// option were specified.  Note that DebugFlag also needs to be set to true for
00105 /// debug output to be produced.
00106 ///
00107 void llvm::setCurrentDebugType(const char *Type) {
00108   CurrentDebugType->clear();
00109   CurrentDebugType->push_back(Type);
00110 }
00111 
00112 /// dbgs - Return a circular-buffered debug stream.
00113 raw_ostream &llvm::dbgs() {
00114   // Do one-time initialization in a thread-safe way.
00115   static struct dbgstream {
00116     circular_raw_ostream strm;
00117 
00118     dbgstream() :
00119         strm(errs(), "*** Debug Log Output ***\n",
00120              (!EnableDebugBuffering || !DebugFlag) ? 0 : DebugBufferSize) {
00121       if (EnableDebugBuffering && DebugFlag && DebugBufferSize != 0)
00122         // TODO: Add a handler for SIGUSER1-type signals so the user can
00123         // force a debug dump.
00124         sys::AddSignalHandler(&debug_user_sig_handler, nullptr);
00125       // Otherwise we've already set the debug stream buffer size to
00126       // zero, disabling buffering so it will output directly to errs().
00127     }
00128   } thestrm;
00129 
00130   return thestrm.strm;
00131 }
00132 
00133 #else
00134 // Avoid "has no symbols" warning.
00135 namespace llvm {
00136   /// dbgs - Return errs().
00137   raw_ostream &dbgs() {
00138     return errs();
00139   }
00140 }
00141 
00142 #endif
00143 
00144 /// EnableDebugBuffering - Turn on signal handler installation.
00145 ///
00146 bool llvm::EnableDebugBuffering = false;