2003-08-02 06:15:41 +08:00
|
|
|
//===-- Debug.cpp - An easy way to add debug output to your code ----------===//
|
2005-04-22 06:55:34 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2005-04-22 06:55:34 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-08-02 06:15:41 +08:00
|
|
|
//
|
2012-07-27 04:38:52 +08:00
|
|
|
// This file implements a handy way of adding debugging information to your
|
2003-08-02 06:15:41 +08:00
|
|
|
// code, without it being enabled all of the time, and without having to add
|
|
|
|
// command line options to enable it.
|
|
|
|
//
|
2018-05-14 20:53:11 +08:00
|
|
|
// In particular, just wrap your code with the LLVM_DEBUG() macro, and it will
|
|
|
|
// be enabled automatically if you specify '-debug' on the command-line.
|
2003-08-02 06:15:41 +08:00
|
|
|
// 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.
|
|
|
|
//
|
2012-07-27 04:38:52 +08:00
|
|
|
// When compiling without assertions, the -debug-* options and all code in
|
2018-05-14 20:53:11 +08:00
|
|
|
// LLVM_DEBUG() statements disappears, so it does not affect the runtime of the
|
|
|
|
// code.
|
2003-08-02 06:15:41 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-11-17 17:54:47 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/circular_raw_ostream.h"
|
2015-03-24 03:32:43 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-12-24 00:39:06 +08:00
|
|
|
|
2021-07-16 07:52:44 +08:00
|
|
|
#include "DebugOptions.h"
|
|
|
|
|
2015-02-19 13:30:16 +08:00
|
|
|
#undef isCurrentDebugType
|
|
|
|
#undef setCurrentDebugType
|
2016-12-27 18:24:58 +08:00
|
|
|
#undef setCurrentDebugTypes
|
2015-02-19 13:30:16 +08:00
|
|
|
|
2003-12-15 05:35:53 +08:00
|
|
|
using namespace llvm;
|
2003-08-02 06:15:41 +08:00
|
|
|
|
2015-02-19 13:30:16 +08:00
|
|
|
// Even though LLVM might be built with NDEBUG, define symbols that the code
|
|
|
|
// built without NDEBUG can depend on via the llvm/Support/Debug.h header.
|
|
|
|
namespace llvm {
|
|
|
|
/// Exported boolean set by the -debug option.
|
|
|
|
bool DebugFlag = false;
|
|
|
|
|
|
|
|
static ManagedStatic<std::vector<std::string>> CurrentDebugType;
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
bool isCurrentDebugType(const char *DebugType) {
|
|
|
|
if (CurrentDebugType->empty())
|
|
|
|
return true;
|
2015-05-28 02:11:07 +08:00
|
|
|
// See if DebugType is in list. Note: do not use find() as that forces us to
|
2015-02-19 13:30:16 +08:00
|
|
|
// unnecessarily create an std::string instance.
|
2015-05-28 02:11:07 +08:00
|
|
|
for (auto &d : *CurrentDebugType) {
|
2015-02-19 13:30:16 +08:00
|
|
|
if (d == DebugType)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
///
|
2016-12-27 18:24:58 +08:00
|
|
|
void setCurrentDebugTypes(const char **Types, unsigned Count);
|
|
|
|
|
2015-02-19 13:30:16 +08:00
|
|
|
void setCurrentDebugType(const char *Type) {
|
2016-12-27 17:31:20 +08:00
|
|
|
setCurrentDebugTypes(&Type, 1);
|
2015-02-19 13:30:16 +08:00
|
|
|
}
|
|
|
|
|
2016-12-27 17:31:20 +08:00
|
|
|
void setCurrentDebugTypes(const char **Types, unsigned Count) {
|
|
|
|
CurrentDebugType->clear();
|
|
|
|
for (size_t T = 0; T < Count; ++T)
|
|
|
|
CurrentDebugType->push_back(Types[T]);
|
|
|
|
}
|
2015-02-19 13:30:16 +08:00
|
|
|
} // namespace llvm
|
|
|
|
|
2009-08-23 16:50:52 +08:00
|
|
|
// All Debug.h functionality is a no-op in NDEBUG mode.
|
|
|
|
#ifndef NDEBUG
|
2003-08-02 06:15:41 +08:00
|
|
|
|
2021-07-16 07:52:44 +08:00
|
|
|
namespace {
|
|
|
|
struct CreateDebug {
|
|
|
|
static void *call() {
|
|
|
|
return new cl::opt<bool, true>("debug", cl::desc("Enable debug output"),
|
|
|
|
cl::Hidden, cl::location(DebugFlag));
|
|
|
|
}
|
|
|
|
};
|
2009-08-23 15:05:39 +08:00
|
|
|
|
2009-12-24 00:39:06 +08:00
|
|
|
// -debug-buffer-size - Buffer the last N characters of debug output
|
|
|
|
//until program termination.
|
2021-07-16 07:52:44 +08:00
|
|
|
struct CreateDebugBufferSize {
|
|
|
|
static void *call() {
|
|
|
|
return new cl::opt<unsigned>(
|
|
|
|
"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));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// -debug - Command line option to enable the DEBUG statements in the passes.
|
|
|
|
// This flag may only be enabled in debug builds.
|
|
|
|
static ManagedStatic<cl::opt<bool, true>, CreateDebug> Debug;
|
|
|
|
static ManagedStatic<cl::opt<unsigned>, CreateDebugBufferSize> DebugBufferSize;
|
2009-12-24 00:39:06 +08:00
|
|
|
|
2010-04-16 01:08:50 +08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct DebugOnlyOpt {
|
2009-08-23 15:05:39 +08:00
|
|
|
void operator=(const std::string &Val) const {
|
2014-11-22 02:06:09 +08:00
|
|
|
if (Val.empty())
|
|
|
|
return;
|
|
|
|
DebugFlag = true;
|
2016-01-12 18:23:13 +08:00
|
|
|
SmallVector<StringRef,8> dbgTypes;
|
|
|
|
StringRef(Val).split(dbgTypes, ',', -1, false);
|
|
|
|
for (auto dbgType : dbgTypes)
|
2020-01-29 03:23:46 +08:00
|
|
|
CurrentDebugType->push_back(std::string(dbgType));
|
2009-08-23 15:05:39 +08:00
|
|
|
}
|
2010-04-16 01:08:50 +08:00
|
|
|
};
|
2021-07-16 07:52:44 +08:00
|
|
|
} // namespace
|
|
|
|
|
2021-07-16 15:34:41 +08:00
|
|
|
static DebugOnlyOpt DebugOnlyOptLoc;
|
2021-07-16 07:52:44 +08:00
|
|
|
|
2021-07-16 07:52:44 +08:00
|
|
|
namespace {
|
|
|
|
struct CreateDebugOnly {
|
|
|
|
static void *call() {
|
|
|
|
return new cl::opt<DebugOnlyOpt, true, cl::parser<std::string>>(
|
|
|
|
"debug-only",
|
|
|
|
cl::desc("Enable a specific type of debug output (comma separated list "
|
|
|
|
"of types)"),
|
|
|
|
cl::Hidden, cl::ZeroOrMore, cl::value_desc("debug string"),
|
|
|
|
cl::location(DebugOnlyOptLoc), cl::ValueRequired);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
static ManagedStatic<cl::opt<DebugOnlyOpt, true, cl::parser<std::string>>,
|
|
|
|
CreateDebugOnly>
|
|
|
|
DebugOnly;
|
|
|
|
|
|
|
|
void llvm::initDebugOptions() {
|
|
|
|
*Debug;
|
|
|
|
*DebugBufferSize;
|
|
|
|
*DebugOnly;
|
|
|
|
}
|
|
|
|
|
2009-12-24 00:39:06 +08:00
|
|
|
// Signal handlers - dump debug output on termination.
|
2010-03-25 03:38:02 +08:00
|
|
|
static void debug_user_sig_handler(void *Cookie) {
|
2009-12-24 00:39:06 +08: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.
|
2015-04-10 00:59:07 +08:00
|
|
|
llvm::circular_raw_ostream &dbgout =
|
|
|
|
static_cast<circular_raw_ostream &>(llvm::dbgs());
|
|
|
|
dbgout.flushBufferWithBanner();
|
2009-10-28 23:32:19 +08:00
|
|
|
}
|
|
|
|
|
2009-12-24 00:39:06 +08: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;
|
|
|
|
|
2021-07-16 07:52:44 +08:00
|
|
|
dbgstream()
|
|
|
|
: strm(errs(), "*** Debug Log Output ***\n",
|
|
|
|
(!EnableDebugBuffering || !DebugFlag) ? 0 : *DebugBufferSize) {
|
|
|
|
if (EnableDebugBuffering && DebugFlag && *DebugBufferSize != 0)
|
2009-12-24 00:39:06 +08:00
|
|
|
// TODO: Add a handler for SIGUSER1-type signals so the user can
|
|
|
|
// force a debug dump.
|
2014-04-15 14:32:26 +08:00
|
|
|
sys::AddSignalHandler(&debug_user_sig_handler, nullptr);
|
2009-12-24 00:39:06 +08:00
|
|
|
// Otherwise we've already set the debug stream buffer size to
|
2009-12-24 07:23:15 +08:00
|
|
|
// zero, disabling buffering so it will output directly to errs().
|
2009-12-24 00:39:06 +08:00
|
|
|
}
|
|
|
|
} thestrm;
|
|
|
|
|
|
|
|
return thestrm.strm;
|
|
|
|
}
|
|
|
|
|
2003-08-13 04:46:50 +08:00
|
|
|
#else
|
2009-08-23 16:50:52 +08:00
|
|
|
// Avoid "has no symbols" warning.
|
2009-10-28 23:32:19 +08:00
|
|
|
namespace llvm {
|
2010-01-20 23:27:19 +08:00
|
|
|
/// dbgs - Return errs().
|
2009-12-24 00:39:06 +08:00
|
|
|
raw_ostream &dbgs() {
|
2010-01-20 23:27:19 +08:00
|
|
|
return errs();
|
2009-12-24 00:39:06 +08:00
|
|
|
}
|
2009-10-28 23:32:19 +08:00
|
|
|
}
|
2021-07-16 07:52:44 +08:00
|
|
|
void llvm::initDebugOptions() {}
|
2003-08-13 04:46:50 +08:00
|
|
|
#endif
|
2009-12-24 00:39:06 +08:00
|
|
|
|
|
|
|
/// EnableDebugBuffering - Turn on signal handler installation.
|
|
|
|
///
|
|
|
|
bool llvm::EnableDebugBuffering = false;
|