#include "llvm/Support/Debug.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/circular_raw_ostream.h"
#include "llvm/Support/raw_ostream.h"
#include "DebugOptions.h"
#undef isCurrentDebugType
#undef setCurrentDebugType
#undef setCurrentDebugTypes
using namespace llvm;
namespace llvm {
bool DebugFlag = false;
static ManagedStatic<std::vector<std::string>> CurrentDebugType;
bool isCurrentDebugType(const char *DebugType) {
if (CurrentDebugType->empty())
return true;
for (auto &d : *CurrentDebugType) {
if (d == DebugType)
return true;
}
return false;
}
void setCurrentDebugTypes(const char **Types, unsigned Count);
void setCurrentDebugType(const char *Type) {
setCurrentDebugTypes(&Type, 1);
}
void setCurrentDebugTypes(const char **Types, unsigned Count) {
CurrentDebugType->clear();
for (size_t T = 0; T < Count; ++T)
CurrentDebugType->push_back(Types[T]);
}
}
#ifndef NDEBUG
namespace {
struct CreateDebug {
static void *call() {
return new cl::opt<bool, true>("debug", cl::desc("Enable debug output"),
cl::Hidden, cl::location(DebugFlag));
}
};
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));
}
};
}
static ManagedStatic<cl::opt<bool, true>, CreateDebug> Debug;
static ManagedStatic<cl::opt<unsigned>, CreateDebugBufferSize> DebugBufferSize;
namespace {
struct DebugOnlyOpt {
void operator=(const std::string &Val) const {
if (Val.empty())
return;
DebugFlag = true;
SmallVector<StringRef,8> dbgTypes;
StringRef(Val).split(dbgTypes, ',', -1, false);
for (auto dbgType : dbgTypes)
CurrentDebugType->push_back(std::string(dbgType));
}
};
}
static DebugOnlyOpt DebugOnlyOptLoc;
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::value_desc("debug string"),
cl::location(DebugOnlyOptLoc), cl::ValueRequired);
}
};
}
static ManagedStatic<cl::opt<DebugOnlyOpt, true, cl::parser<std::string>>,
CreateDebugOnly>
DebugOnly;
void llvm::initDebugOptions() {
*Debug;
*DebugBufferSize;
*DebugOnly;
}
static void debug_user_sig_handler(void *Cookie) {
llvm::circular_raw_ostream &dbgout =
static_cast<circular_raw_ostream &>(llvm::dbgs());
dbgout.flushBufferWithBanner();
}
raw_ostream &llvm::dbgs() {
static struct dbgstream {
circular_raw_ostream strm;
dbgstream()
: strm(errs(), "*** Debug Log Output ***\n",
(!EnableDebugBuffering || !DebugFlag) ? 0 : *DebugBufferSize) {
if (EnableDebugBuffering && DebugFlag && *DebugBufferSize != 0)
sys::AddSignalHandler(&debug_user_sig_handler, nullptr);
}
} thestrm;
return thestrm.strm;
}
#else
namespace llvm {
raw_ostream &dbgs() {
return errs();
}
}
void llvm::initDebugOptions() {}
#endif
bool llvm::EnableDebugBuffering = false;