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