Debug.cpp revision 201360
1193323Sed//===-- Debug.cpp - An easy way to add debug output to your code ----------===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file implements a handle way of adding debugging information to your 11193323Sed// code, without it being enabled all of the time, and without having to add 12193323Sed// command line options to enable it. 13193323Sed// 14193323Sed// In particular, just wrap your code with the DEBUG() macro, and it will be 15193323Sed// enabled automatically if you specify '-debug' on the command-line. 16193323Sed// Alternatively, you can also use the SET_DEBUG_TYPE("foo") macro to specify 17193323Sed// that your debug code belongs to class "foo". Then, on the command line, you 18193323Sed// can specify '-debug-only=foo' to enable JUST the debug information for the 19193323Sed// foo class. 20193323Sed// 21193323Sed// When compiling in release mode, the -debug-* options and all code in DEBUG() 22193323Sed// statements disappears, so it does not effect the runtime of the code. 23193323Sed// 24193323Sed//===----------------------------------------------------------------------===// 25193323Sed 26193323Sed#include "llvm/Support/CommandLine.h" 27193323Sed#include "llvm/Support/Debug.h" 28201360Srdivacky#include "llvm/Support/circular_raw_ostream.h" 29201360Srdivacky#include "llvm/System/Signals.h" 30201360Srdivacky 31193323Sedusing namespace llvm; 32193323Sed 33198090Srdivacky// All Debug.h functionality is a no-op in NDEBUG mode. 34198090Srdivacky#ifndef NDEBUG 35193323Sedbool llvm::DebugFlag; // DebugFlag - Exported boolean set by the -debug option 36193323Sed 37198090Srdivacky// -debug - Command line option to enable the DEBUG statements in the passes. 38198090Srdivacky// This flag may only be enabled in debug builds. 39198090Srdivackystatic cl::opt<bool, true> 40198090SrdivackyDebug("debug", cl::desc("Enable debug output"), cl::Hidden, 41198090Srdivacky cl::location(DebugFlag)); 42193323Sed 43201360Srdivacky// -debug-buffer-size - Buffer the last N characters of debug output 44201360Srdivacky//until program termination. 45201360Srdivackystatic cl::opt<unsigned> 46201360SrdivackyDebugBufferSize("debug-buffer-size", 47201360Srdivacky cl::desc("Buffer the last N characters of debug output" 48201360Srdivacky "until program termination. " 49201360Srdivacky "[default 0 -- immediate print-out]"), 50201360Srdivacky cl::Hidden, 51201360Srdivacky cl::init(0)); 52201360Srdivacky 53198090Srdivackystatic std::string CurrentDebugType; 54198090Srdivackystatic struct DebugOnlyOpt { 55198090Srdivacky void operator=(const std::string &Val) const { 56198090Srdivacky DebugFlag |= !Val.empty(); 57198090Srdivacky CurrentDebugType = Val; 58198090Srdivacky } 59198090Srdivacky} DebugOnlyOptLoc; 60193323Sed 61198090Srdivackystatic cl::opt<DebugOnlyOpt, true, cl::parser<std::string> > 62198090SrdivackyDebugOnly("debug-only", cl::desc("Enable a specific type of debug output"), 63198090Srdivacky cl::Hidden, cl::value_desc("debug string"), 64198090Srdivacky cl::location(DebugOnlyOptLoc), cl::ValueRequired); 65193323Sed 66201360Srdivacky// Signal handlers - dump debug output on termination. 67201360Srdivackystatic void debug_user_sig_handler(void *Cookie) 68201360Srdivacky{ 69201360Srdivacky // This is a bit sneaky. Since this is under #ifndef NDEBUG, we 70201360Srdivacky // know that debug mode is enabled and dbgs() really is a 71201360Srdivacky // circular_raw_ostream. If NDEBUG is defined, then dbgs() == 72201360Srdivacky // errs() but this will never be invoked. 73201360Srdivacky llvm::circular_raw_ostream *dbgout = 74201360Srdivacky static_cast<llvm::circular_raw_ostream *>(&llvm::dbgs()); 75201360Srdivacky dbgout->flushBufferWithBanner(); 76201360Srdivacky} 77201360Srdivacky 78193323Sed// isCurrentDebugType - Return true if the specified string is the debug type 79193323Sed// specified on the command line, or if none was specified on the command line 80193323Sed// with the -debug-only=X option. 81193323Sed// 82193323Sedbool llvm::isCurrentDebugType(const char *DebugType) { 83193323Sed return CurrentDebugType.empty() || DebugType == CurrentDebugType; 84198090Srdivacky} 85198892Srdivacky 86198892Srdivacky/// SetCurrentDebugType - Set the current debug type, as if the -debug-only=X 87198892Srdivacky/// option were specified. Note that DebugFlag also needs to be set to true for 88198892Srdivacky/// debug output to be produced. 89198892Srdivacky/// 90199481Srdivackyvoid llvm::SetCurrentDebugType(const char *Type) { 91198892Srdivacky CurrentDebugType = Type; 92198892Srdivacky} 93198892Srdivacky 94201360Srdivacky/// dbgs - Return a circular-buffered debug stream. 95201360Srdivackyraw_ostream &llvm::dbgs() { 96201360Srdivacky // Do one-time initialization in a thread-safe way. 97201360Srdivacky static struct dbgstream { 98201360Srdivacky circular_raw_ostream strm; 99201360Srdivacky 100201360Srdivacky dbgstream() : 101201360Srdivacky strm(errs(), "*** Debug Log Output ***\n", 102201360Srdivacky (!EnableDebugBuffering || !DebugFlag) ? 0 : DebugBufferSize) { 103201360Srdivacky if (EnableDebugBuffering && DebugFlag && DebugBufferSize != 0) 104201360Srdivacky // TODO: Add a handler for SIGUSER1-type signals so the user can 105201360Srdivacky // force a debug dump. 106201360Srdivacky sys::AddSignalHandler(&debug_user_sig_handler, 0); 107201360Srdivacky // Otherwise we've already set the debug stream buffer size to 108201360Srdivacky // zero, disabling buffering so it will output directly to errs(). 109201360Srdivacky } 110201360Srdivacky } thestrm; 111201360Srdivacky 112201360Srdivacky return thestrm.strm; 113201360Srdivacky} 114201360Srdivacky 115193323Sed#else 116198090Srdivacky// Avoid "has no symbols" warning. 117198892Srdivackynamespace llvm { 118201360Srdivacky /// dbgs - Return dbgs(). 119201360Srdivacky raw_ostream &dbgs() { 120201360Srdivacky return dbgs(); 121201360Srdivacky } 122198892Srdivacky} 123201360Srdivacky 124193323Sed#endif 125201360Srdivacky 126201360Srdivacky/// EnableDebugBuffering - Turn on signal handler installation. 127201360Srdivacky/// 128201360Srdivackybool llvm::EnableDebugBuffering = false; 129