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