Debug.cpp revision 249423
1138627Stakawata//===-- Debug.cpp - An easy way to add debug output to your code ----------===// 2138627Stakawata// 3147196Smarkus// The LLVM Compiler Infrastructure 4138627Stakawata// 5138627Stakawata// This file is distributed under the University of Illinois Open Source 6138627Stakawata// License. See LICENSE.TXT for details. 7138627Stakawata// 8138627Stakawata//===----------------------------------------------------------------------===// 9138627Stakawata// 10138627Stakawata// This file implements a handy way of adding debugging information to your 11138627Stakawata// code, without it being enabled all of the time, and without having to add 12138627Stakawata// command line options to enable it. 13138627Stakawata// 14138627Stakawata// In particular, just wrap your code with the DEBUG() macro, and it will be 15138627Stakawata// enabled automatically if you specify '-debug' on the command-line. 16138627Stakawata// Alternatively, you can also use the SET_DEBUG_TYPE("foo") macro to specify 17138627Stakawata// that your debug code belongs to class "foo". Then, on the command line, you 18138627Stakawata// can specify '-debug-only=foo' to enable JUST the debug information for the 19138627Stakawata// foo class. 20138627Stakawata// 21138627Stakawata// When compiling without assertions, the -debug-* options and all code in 22138627Stakawata// DEBUG() statements disappears, so it does not affect the runtime of the code. 23138627Stakawata// 24138627Stakawata//===----------------------------------------------------------------------===// 25138627Stakawata 26138627Stakawata#include "llvm/Support/Debug.h" 27138627Stakawata#include "llvm/Support/CommandLine.h" 28143002Sobrien#include "llvm/Support/Signals.h" 29143002Sobrien#include "llvm/Support/circular_raw_ostream.h" 30143002Sobrien 31147196Smarkususing namespace llvm; 32147196Smarkus 33147196Smarkus// All Debug.h functionality is a no-op in NDEBUG mode. 34147196Smarkus#ifndef NDEBUG 35147196Smarkusbool llvm::DebugFlag; // DebugFlag - Exported boolean set by the -debug option 36147196Smarkus 37147196Smarkus// -debug - Command line option to enable the DEBUG statements in the passes. 38147196Smarkus// This flag may only be enabled in debug builds. 39147196Smarkusstatic cl::opt<bool, true> 40138627StakawataDebug("debug", cl::desc("Enable debug output"), cl::Hidden, 41138627Stakawata cl::location(DebugFlag)); 42138627Stakawata 43138627Stakawata// -debug-buffer-size - Buffer the last N characters of debug output 44138627Stakawata//until program termination. 45150003Sobrienstatic cl::opt<unsigned> 46138627StakawataDebugBufferSize("debug-buffer-size", 47138627Stakawata cl::desc("Buffer the last N characters of debug output " 48138627Stakawata "until program termination. " 49147196Smarkus "[default 0 -- immediate print-out]"), 50138627Stakawata cl::Hidden, 51138627Stakawata cl::init(0)); 52138627Stakawata 53138825Snjlstatic std::string CurrentDebugType; 54138774Sscottl 55138774Sscottlnamespace { 56147196Smarkus 57147196Smarkusstruct DebugOnlyOpt { 58147196Smarkus void operator=(const std::string &Val) const { 59147196Smarkus DebugFlag |= !Val.empty(); 60147196Smarkus CurrentDebugType = Val; 61147196Smarkus } 62147196Smarkus}; 63147196Smarkus 64147196Smarkus} 65147196Smarkus 66147196Smarkusstatic DebugOnlyOpt DebugOnlyOptLoc; 67154326Smarkus 68154326Smarkusstatic cl::opt<DebugOnlyOpt, true, cl::parser<std::string> > 69154326SmarkusDebugOnly("debug-only", cl::desc("Enable a specific type of debug output"), 70138627Stakawata cl::Hidden, cl::value_desc("debug string"), 71147196Smarkus cl::location(DebugOnlyOptLoc), cl::ValueRequired); 72147196Smarkus 73147196Smarkus// Signal handlers - dump debug output on termination. 74147196Smarkusstatic void debug_user_sig_handler(void *Cookie) { 75147196Smarkus // This is a bit sneaky. Since this is under #ifndef NDEBUG, we 76147196Smarkus // know that debug mode is enabled and dbgs() really is a 77147196Smarkus // circular_raw_ostream. If NDEBUG is defined, then dbgs() == 78147196Smarkus // errs() but this will never be invoked. 79147196Smarkus llvm::circular_raw_ostream *dbgout = 80147196Smarkus static_cast<llvm::circular_raw_ostream *>(&llvm::dbgs()); 81147196Smarkus dbgout->flushBufferWithBanner(); 82147196Smarkus} 83147196Smarkus 84147196Smarkus// isCurrentDebugType - Return true if the specified string is the debug type 85147196Smarkus// specified on the command line, or if none was specified on the command line 86147196Smarkus// with the -debug-only=X option. 87147196Smarkus// 88147196Smarkusbool llvm::isCurrentDebugType(const char *DebugType) { 89147196Smarkus return CurrentDebugType.empty() || DebugType == CurrentDebugType; 90138627Stakawata} 91147196Smarkus 92147196Smarkus/// setCurrentDebugType - Set the current debug type, as if the -debug-only=X 93147196Smarkus/// option were specified. Note that DebugFlag also needs to be set to true for 94147196Smarkus/// debug output to be produced. 95147196Smarkus/// 96147196Smarkusvoid llvm::setCurrentDebugType(const char *Type) { 97147196Smarkus CurrentDebugType = Type; 98154326Smarkus} 99154326Smarkus 100147196Smarkus/// dbgs - Return a circular-buffered debug stream. 101147196Smarkusraw_ostream &llvm::dbgs() { 102147196Smarkus // Do one-time initialization in a thread-safe way. 103147196Smarkus static struct dbgstream { 104147196Smarkus circular_raw_ostream strm; 105147196Smarkus 106147196Smarkus dbgstream() : 107147196Smarkus strm(errs(), "*** Debug Log Output ***\n", 108147196Smarkus (!EnableDebugBuffering || !DebugFlag) ? 0 : DebugBufferSize) { 109147196Smarkus if (EnableDebugBuffering && DebugFlag && DebugBufferSize != 0) 110147196Smarkus // TODO: Add a handler for SIGUSER1-type signals so the user can 111147196Smarkus // force a debug dump. 112147196Smarkus sys::AddSignalHandler(&debug_user_sig_handler, 0); 113147196Smarkus // Otherwise we've already set the debug stream buffer size to 114147196Smarkus // zero, disabling buffering so it will output directly to errs(). 115147196Smarkus } 116147196Smarkus } thestrm; 117147196Smarkus 118147196Smarkus return thestrm.strm; 119147196Smarkus} 120147196Smarkus 121147196Smarkus#else 122147196Smarkus// Avoid "has no symbols" warning. 123147196Smarkusnamespace llvm { 124147196Smarkus /// dbgs - Return errs(). 125147196Smarkus raw_ostream &dbgs() { 126147196Smarkus return errs(); 127147196Smarkus } 128138627Stakawata} 129147196Smarkus 130147196Smarkus#endif 131147196Smarkus 132147196Smarkus/// EnableDebugBuffering - Turn on signal handler installation. 133147196Smarkus/// 134147196Smarkusbool llvm::EnableDebugBuffering = false; 135147196Smarkus