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