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