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// 10245431Sdim// This file implements a handy 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// 21245431Sdim// When compiling without assertions, the -debug-* options and all code in 22245431Sdim// DEBUG() statements disappears, so it does not affect the runtime of the code. 23193323Sed// 24193323Sed//===----------------------------------------------------------------------===// 25193323Sed 26252723Sdim#include "llvm/Support/Debug.h" 27193323Sed#include "llvm/Support/CommandLine.h" 28252723Sdim#include "llvm/Support/Signals.h" 29201360Srdivacky#include "llvm/Support/circular_raw_ostream.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", 47252723Sdim 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; 54207618Srdivacky 55207618Srdivackynamespace { 56207618Srdivacky 57207618Srdivackystruct DebugOnlyOpt { 58198090Srdivacky void operator=(const std::string &Val) const { 59198090Srdivacky DebugFlag |= !Val.empty(); 60198090Srdivacky CurrentDebugType = Val; 61198090Srdivacky } 62207618Srdivacky}; 63193323Sed 64207618Srdivacky} 65207618Srdivacky 66207618Srdivackystatic DebugOnlyOpt DebugOnlyOptLoc; 67207618Srdivacky 68198090Srdivackystatic cl::opt<DebugOnlyOpt, true, cl::parser<std::string> > 69198090SrdivackyDebugOnly("debug-only", cl::desc("Enable a specific type of debug output"), 70198090Srdivacky cl::Hidden, cl::value_desc("debug string"), 71198090Srdivacky cl::location(DebugOnlyOptLoc), cl::ValueRequired); 72193323Sed 73201360Srdivacky// Signal handlers - dump debug output on termination. 74206083Srdivackystatic void debug_user_sig_handler(void *Cookie) { 75201360Srdivacky // This is a bit sneaky. Since this is under #ifndef NDEBUG, we 76201360Srdivacky // know that debug mode is enabled and dbgs() really is a 77201360Srdivacky // circular_raw_ostream. If NDEBUG is defined, then dbgs() == 78201360Srdivacky // errs() but this will never be invoked. 79201360Srdivacky llvm::circular_raw_ostream *dbgout = 80201360Srdivacky static_cast<llvm::circular_raw_ostream *>(&llvm::dbgs()); 81201360Srdivacky dbgout->flushBufferWithBanner(); 82201360Srdivacky} 83201360Srdivacky 84193323Sed// isCurrentDebugType - Return true if the specified string is the debug type 85193323Sed// specified on the command line, or if none was specified on the command line 86193323Sed// with the -debug-only=X option. 87193323Sed// 88193323Sedbool llvm::isCurrentDebugType(const char *DebugType) { 89193323Sed return CurrentDebugType.empty() || DebugType == CurrentDebugType; 90198090Srdivacky} 91198892Srdivacky 92245431Sdim/// setCurrentDebugType - Set the current debug type, as if the -debug-only=X 93198892Srdivacky/// option were specified. Note that DebugFlag also needs to be set to true for 94198892Srdivacky/// debug output to be produced. 95198892Srdivacky/// 96245431Sdimvoid llvm::setCurrentDebugType(const char *Type) { 97198892Srdivacky CurrentDebugType = Type; 98198892Srdivacky} 99198892Srdivacky 100201360Srdivacky/// dbgs - Return a circular-buffered debug stream. 101201360Srdivackyraw_ostream &llvm::dbgs() { 102201360Srdivacky // Do one-time initialization in a thread-safe way. 103201360Srdivacky static struct dbgstream { 104201360Srdivacky circular_raw_ostream strm; 105201360Srdivacky 106201360Srdivacky dbgstream() : 107201360Srdivacky strm(errs(), "*** Debug Log Output ***\n", 108201360Srdivacky (!EnableDebugBuffering || !DebugFlag) ? 0 : DebugBufferSize) { 109201360Srdivacky if (EnableDebugBuffering && DebugFlag && DebugBufferSize != 0) 110201360Srdivacky // TODO: Add a handler for SIGUSER1-type signals so the user can 111201360Srdivacky // force a debug dump. 112201360Srdivacky sys::AddSignalHandler(&debug_user_sig_handler, 0); 113201360Srdivacky // Otherwise we've already set the debug stream buffer size to 114201360Srdivacky // zero, disabling buffering so it will output directly to errs(). 115201360Srdivacky } 116201360Srdivacky } thestrm; 117201360Srdivacky 118201360Srdivacky return thestrm.strm; 119201360Srdivacky} 120201360Srdivacky 121193323Sed#else 122198090Srdivacky// Avoid "has no symbols" warning. 123198892Srdivackynamespace llvm { 124202878Srdivacky /// dbgs - Return errs(). 125201360Srdivacky raw_ostream &dbgs() { 126202878Srdivacky return errs(); 127201360Srdivacky } 128198892Srdivacky} 129201360Srdivacky 130193323Sed#endif 131201360Srdivacky 132201360Srdivacky/// EnableDebugBuffering - Turn on signal handler installation. 133201360Srdivacky/// 134201360Srdivackybool llvm::EnableDebugBuffering = false; 135