LLVM  mainline
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/ManagedStatic.h"
00029 #include "llvm/Support/Signals.h"
00030 #include "llvm/Support/circular_raw_ostream.h"
00031 #include "llvm/Support/raw_ostream.h"
00032 
00033 #undef isCurrentDebugType
00034 #undef setCurrentDebugType
00035 
00036 using namespace llvm;
00037 
00038 // Even though LLVM might be built with NDEBUG, define symbols that the code
00039 // built without NDEBUG can depend on via the llvm/Support/Debug.h header.
00040 namespace llvm {
00041 /// Exported boolean set by the -debug option.
00042 bool DebugFlag = false;
00043 
00044 static ManagedStatic<std::vector<std::string>> CurrentDebugType;
00045 
00046 /// Return true if the specified string is the debug type
00047 /// specified on the command line, or if none was specified on the command line
00048 /// with the -debug-only=X option.
00049 bool isCurrentDebugType(const char *DebugType) {
00050   if (CurrentDebugType->empty())
00051     return true;
00052   // see if DebugType is in list. Note: do not use find() as that forces us to
00053   // unnecessarily create an std::string instance.
00054   for (auto d : *CurrentDebugType) {
00055     if (d == DebugType)
00056       return true;
00057   }
00058   return false;
00059 }
00060 
00061 /// Set the current debug type, as if the -debug-only=X
00062 /// option were specified.  Note that DebugFlag also needs to be set to true for
00063 /// debug output to be produced.
00064 ///
00065 void setCurrentDebugType(const char *Type) {
00066   CurrentDebugType->clear();
00067   CurrentDebugType->push_back(Type);
00068 }
00069 
00070 } // namespace llvm
00071 
00072 // All Debug.h functionality is a no-op in NDEBUG mode.
00073 #ifndef NDEBUG
00074 
00075 // -debug - Command line option to enable the DEBUG statements in the passes.
00076 // This flag may only be enabled in debug builds.
00077 static cl::opt<bool, true>
00078 Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
00079       cl::location(DebugFlag));
00080 
00081 // -debug-buffer-size - Buffer the last N characters of debug output
00082 //until program termination.
00083 static cl::opt<unsigned>
00084 DebugBufferSize("debug-buffer-size",
00085                 cl::desc("Buffer the last N characters of debug output "
00086                          "until program termination. "
00087                          "[default 0 -- immediate print-out]"),
00088                 cl::Hidden,
00089                 cl::init(0));
00090 
00091 namespace {
00092 
00093 struct DebugOnlyOpt {
00094   void operator=(const std::string &Val) const {
00095     if (Val.empty())
00096       return;
00097     DebugFlag = true;
00098     CurrentDebugType->push_back(Val);
00099   }
00100 };
00101 
00102 }
00103 
00104 static DebugOnlyOpt DebugOnlyOptLoc;
00105 
00106 static cl::opt<DebugOnlyOpt, true, cl::parser<std::string> >
00107 DebugOnly("debug-only", cl::desc("Enable a specific type of debug output"),
00108           cl::Hidden, cl::ZeroOrMore, cl::value_desc("debug string"),
00109           cl::location(DebugOnlyOptLoc), cl::ValueRequired);
00110 
00111 // Signal handlers - dump debug output on termination.
00112 static void debug_user_sig_handler(void *Cookie) {
00113   // This is a bit sneaky.  Since this is under #ifndef NDEBUG, we
00114   // know that debug mode is enabled and dbgs() really is a
00115   // circular_raw_ostream.  If NDEBUG is defined, then dbgs() ==
00116   // errs() but this will never be invoked.
00117   llvm::circular_raw_ostream &dbgout =
00118       static_cast<circular_raw_ostream &>(llvm::dbgs());
00119   dbgout.flushBufferWithBanner();
00120 }
00121 
00122 /// dbgs - Return a circular-buffered debug stream.
00123 raw_ostream &llvm::dbgs() {
00124   // Do one-time initialization in a thread-safe way.
00125   static struct dbgstream {
00126     circular_raw_ostream strm;
00127 
00128     dbgstream() :
00129         strm(errs(), "*** Debug Log Output ***\n",
00130              (!EnableDebugBuffering || !DebugFlag) ? 0 : DebugBufferSize) {
00131       if (EnableDebugBuffering && DebugFlag && DebugBufferSize != 0)
00132         // TODO: Add a handler for SIGUSER1-type signals so the user can
00133         // force a debug dump.
00134         sys::AddSignalHandler(&debug_user_sig_handler, nullptr);
00135       // Otherwise we've already set the debug stream buffer size to
00136       // zero, disabling buffering so it will output directly to errs().
00137     }
00138   } thestrm;
00139 
00140   return thestrm.strm;
00141 }
00142 
00143 #else
00144 // Avoid "has no symbols" warning.
00145 namespace llvm {
00146   /// dbgs - Return errs().
00147   raw_ostream &dbgs() {
00148     return errs();
00149   }
00150 }
00151 
00152 #endif
00153 
00154 /// EnableDebugBuffering - Turn on signal handler installation.
00155 ///
00156 bool llvm::EnableDebugBuffering = false;