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