2003-08-02 00:15:41 +02:00
|
|
|
//===-- Debug.cpp - An easy way to add debug output to your code ----------===//
|
2005-04-22 00:55:34 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 00:55:34 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-08-02 00:15:41 +02:00
|
|
|
//
|
|
|
|
// This file implements a handle way of adding debugging information to your
|
|
|
|
// code, without it being enabled all of the time, and without having to add
|
|
|
|
// command line options to enable it.
|
|
|
|
//
|
|
|
|
// In particular, just wrap your code with the DEBUG() macro, and it will be
|
|
|
|
// enabled automatically if you specify '-debug' on the command-line.
|
|
|
|
// Alternatively, you can also use the SET_DEBUG_TYPE("foo") macro to specify
|
|
|
|
// that your debug code belongs to class "foo". Then, on the command line, you
|
|
|
|
// can specify '-debug-only=foo' to enable JUST the debug information for the
|
|
|
|
// foo class.
|
|
|
|
//
|
|
|
|
// When compiling in release mode, the -debug-* options and all code in DEBUG()
|
|
|
|
// statements disappears, so it does not effect the runtime of the code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-02 00:55:40 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2006-11-17 10:54:47 +01:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-12-23 17:39:06 +01:00
|
|
|
#include "llvm/Support/circular_raw_ostream.h"
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/Signals.h"
|
2009-12-23 17:39:06 +01:00
|
|
|
|
2003-12-14 22:35:53 +01:00
|
|
|
using namespace llvm;
|
2003-08-02 00:15:41 +02:00
|
|
|
|
2009-08-23 10:50:52 +02:00
|
|
|
// All Debug.h functionality is a no-op in NDEBUG mode.
|
|
|
|
#ifndef NDEBUG
|
2003-12-14 22:35:53 +01:00
|
|
|
bool llvm::DebugFlag; // DebugFlag - Exported boolean set by the -debug option
|
2003-08-02 00:15:41 +02:00
|
|
|
|
2009-08-23 09:05:39 +02:00
|
|
|
// -debug - Command line option to enable the DEBUG statements in the passes.
|
|
|
|
// This flag may only be enabled in debug builds.
|
|
|
|
static cl::opt<bool, true>
|
|
|
|
Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
|
|
|
|
cl::location(DebugFlag));
|
|
|
|
|
2009-12-23 17:39:06 +01:00
|
|
|
// -debug-buffer-size - Buffer the last N characters of debug output
|
|
|
|
//until program termination.
|
|
|
|
static cl::opt<unsigned>
|
|
|
|
DebugBufferSize("debug-buffer-size",
|
|
|
|
cl::desc("Buffer the last N characters of debug output"
|
|
|
|
"until program termination. "
|
|
|
|
"[default 0 -- immediate print-out]"),
|
|
|
|
cl::Hidden,
|
|
|
|
cl::init(0));
|
|
|
|
|
2009-08-23 09:05:39 +02:00
|
|
|
static std::string CurrentDebugType;
|
2010-04-15 19:08:50 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct DebugOnlyOpt {
|
2009-08-23 09:05:39 +02:00
|
|
|
void operator=(const std::string &Val) const {
|
|
|
|
DebugFlag |= !Val.empty();
|
|
|
|
CurrentDebugType = Val;
|
|
|
|
}
|
2010-04-15 19:08:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static DebugOnlyOpt DebugOnlyOptLoc;
|
2009-08-23 09:05:39 +02:00
|
|
|
|
|
|
|
static cl::opt<DebugOnlyOpt, true, cl::parser<std::string> >
|
|
|
|
DebugOnly("debug-only", cl::desc("Enable a specific type of debug output"),
|
|
|
|
cl::Hidden, cl::value_desc("debug string"),
|
|
|
|
cl::location(DebugOnlyOptLoc), cl::ValueRequired);
|
2003-08-02 00:15:41 +02:00
|
|
|
|
2009-12-23 17:39:06 +01:00
|
|
|
// Signal handlers - dump debug output on termination.
|
2010-03-24 20:38:02 +01:00
|
|
|
static void debug_user_sig_handler(void *Cookie) {
|
2009-12-23 17:39:06 +01:00
|
|
|
// This is a bit sneaky. Since this is under #ifndef NDEBUG, we
|
|
|
|
// know that debug mode is enabled and dbgs() really is a
|
|
|
|
// circular_raw_ostream. If NDEBUG is defined, then dbgs() ==
|
|
|
|
// errs() but this will never be invoked.
|
|
|
|
llvm::circular_raw_ostream *dbgout =
|
|
|
|
static_cast<llvm::circular_raw_ostream *>(&llvm::dbgs());
|
|
|
|
dbgout->flushBufferWithBanner();
|
|
|
|
}
|
|
|
|
|
2003-08-02 00:15:41 +02:00
|
|
|
// isCurrentDebugType - Return true if the specified string is the debug type
|
|
|
|
// specified on the command line, or if none was specified on the command line
|
|
|
|
// with the -debug-only=X option.
|
|
|
|
//
|
2003-12-14 22:35:53 +01:00
|
|
|
bool llvm::isCurrentDebugType(const char *DebugType) {
|
2003-08-02 00:15:41 +02:00
|
|
|
return CurrentDebugType.empty() || DebugType == CurrentDebugType;
|
2009-08-23 10:50:52 +02:00
|
|
|
}
|
2009-10-28 16:32:19 +01:00
|
|
|
|
|
|
|
/// SetCurrentDebugType - Set the current debug type, as if the -debug-only=X
|
|
|
|
/// option were specified. Note that DebugFlag also needs to be set to true for
|
|
|
|
/// debug output to be produced.
|
|
|
|
///
|
2009-11-09 15:50:34 +01:00
|
|
|
void llvm::SetCurrentDebugType(const char *Type) {
|
2009-10-28 16:32:19 +01:00
|
|
|
CurrentDebugType = Type;
|
|
|
|
}
|
|
|
|
|
2009-12-23 17:39:06 +01:00
|
|
|
/// dbgs - Return a circular-buffered debug stream.
|
|
|
|
raw_ostream &llvm::dbgs() {
|
|
|
|
// Do one-time initialization in a thread-safe way.
|
|
|
|
static struct dbgstream {
|
|
|
|
circular_raw_ostream strm;
|
|
|
|
|
|
|
|
dbgstream() :
|
|
|
|
strm(errs(), "*** Debug Log Output ***\n",
|
|
|
|
(!EnableDebugBuffering || !DebugFlag) ? 0 : DebugBufferSize) {
|
|
|
|
if (EnableDebugBuffering && DebugFlag && DebugBufferSize != 0)
|
|
|
|
// TODO: Add a handler for SIGUSER1-type signals so the user can
|
|
|
|
// force a debug dump.
|
|
|
|
sys::AddSignalHandler(&debug_user_sig_handler, 0);
|
|
|
|
// Otherwise we've already set the debug stream buffer size to
|
2009-12-24 00:23:15 +01:00
|
|
|
// zero, disabling buffering so it will output directly to errs().
|
2009-12-23 17:39:06 +01:00
|
|
|
}
|
|
|
|
} thestrm;
|
|
|
|
|
|
|
|
return thestrm.strm;
|
|
|
|
}
|
|
|
|
|
2003-08-12 22:46:50 +02:00
|
|
|
#else
|
2009-08-23 10:50:52 +02:00
|
|
|
// Avoid "has no symbols" warning.
|
2009-10-28 16:32:19 +01:00
|
|
|
namespace llvm {
|
2010-01-20 16:27:19 +01:00
|
|
|
/// dbgs - Return errs().
|
2009-12-23 17:39:06 +01:00
|
|
|
raw_ostream &dbgs() {
|
2010-01-20 16:27:19 +01:00
|
|
|
return errs();
|
2009-12-23 17:39:06 +01:00
|
|
|
}
|
2009-10-28 16:32:19 +01:00
|
|
|
}
|
2009-12-23 17:39:06 +01:00
|
|
|
|
2003-08-12 22:46:50 +02:00
|
|
|
#endif
|
2009-12-23 17:39:06 +01:00
|
|
|
|
|
|
|
/// EnableDebugBuffering - Turn on signal handler installation.
|
|
|
|
///
|
|
|
|
bool llvm::EnableDebugBuffering = false;
|