1//===-- Debug.cpp - An easy way to add debug output to your code ----------===// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7//===----------------------------------------------------------------------===// 9// This file implements a handy way of adding debugging information to your 10// code, without it being enabled all of the time, and without having to add 11// command line options to enable it. 13// In particular, just wrap your code with the LLVM_DEBUG() macro, and it will 14// be enabled automatically if you specify '-debug' on the command-line. 15// Alternatively, you can also use the SET_DEBUG_TYPE("foo") macro to specify 16// that your debug code belongs to class "foo". Then, on the command line, you 17// can specify '-debug-only=foo' to enable JUST the debug information for the 20// When compiling without assertions, the -debug-* options and all code in 21// LLVM_DEBUG() statements disappears, so it does not affect the runtime of the 24//===----------------------------------------------------------------------===// 35#undef isCurrentDebugType 36#undef setCurrentDebugType 37#undef setCurrentDebugTypes 41// Even though LLVM might be built with NDEBUG, define symbols that the code 42// built without NDEBUG can depend on via the llvm/Support/Debug.h header. 44/// Exported boolean set by the -debug option. 49/// Return true if the specified string is the debug type 50/// specified on the command line, or if none was specified on the command line 51/// with the -debug-only=X option. 55// See if DebugType is in list. Note: do not use find() as that forces us to 56// unnecessarily create an std::string instance. 64/// Set the current debug type, as if the -debug-only=X 65/// option were specified. Note that DebugFlag also needs to be set to true for 66/// debug output to be produced. 76for (
size_tT = 0;
T < Count; ++
T)
81// All Debug.h functionality is a no-op in NDEBUG mode. 92// -debug-buffer-size - Buffer the last N characters of debug output 93//until program termination. 94structCreateDebugBufferSize {
98cl::desc(
"Buffer the last N characters of debug output " 99"until program termination. " 100"[default 0 -- immediate print-out]"),
106// -debug - Command line option to enable the DEBUG statements in the passes. 107// This flag may only be enabled in debug builds. 114void operator=(
const std::string &Val)
const{
120for (
auto dbgType : dbgTypes)
129structCreateDebugOnly {
133cl::desc(
"Enable a specific type of debug output (comma separated list " 151// Signal handlers - dump debug output on termination. 153// This is a bit sneaky. Since this is under #ifndef NDEBUG, we 154// know that debug mode is enabled and dbgs() really is a 155// circular_raw_ostream. If NDEBUG is defined, then dbgs() == 156// errs() but this will never be invoked. 162/// dbgs - Return a circular-buffered debug stream. 164// Do one-time initialization in a thread-safe way. 165staticstructdbgstream {
169 : strm(
errs(),
"*** Debug Log Output ***\n",
172// TODO: Add a handler for SIGUSER1-type signals so the user can 173// force a debug dump. 175// Otherwise we've already set the debug stream buffer size to 176// zero, disabling buffering so it will output directly to errs(). 184// Avoid "has no symbols" warning. 186 /// dbgs - Return errs(). 194/// EnableDebugBuffering - Turn on signal handler installation. static ManagedStatic< cl::opt< bool, true >, CreateDebug > Debug
static void debug_user_sig_handler(void *Cookie)
static ManagedStatic< cl::opt< unsigned >, CreateDebugBufferSize > DebugBufferSize
static ManagedStatic< cl::opt< DebugOnlyOpt, true, cl::parser< std::string > >, CreateDebugOnly > DebugOnly
static DebugOnlyOpt DebugOnlyOptLoc
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
The instances of the Type class are immutable: once they are created, they are never changed.
circular_raw_ostream - A raw_ostream which can save its data to a circular buffer,...
void flushBufferWithBanner()
flushBufferWithBanner - Force output of the buffer along with a small header.
This class implements an extremely fast bulk output stream that can only output to a stream.
initializer< Ty > init(const Ty &Val)
LocationClass< Ty > location(Ty &L)
void AddSignalHandler(SignalHandlerCallback FnPtr, void *Cookie)
Add a function to be called when an abort/kill signal is delivered to the process.
This is an optimization pass for GlobalISel generic memory operations.
bool EnableDebugBuffering
EnableDebugBuffering - This defaults to false.
bool DebugFlag
This boolean is set to true if the '-debug' command line option is specified.
void setCurrentDebugTypes(const char **Types, unsigned Count)
setCurrentDebugTypes - Set the current debug type, as if the -debug-only=X,Y,Z option were specified.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void setCurrentDebugType(const char *Type)
setCurrentDebugType - Set the current debug type, as if the -debug-only=X option were specified.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
bool isCurrentDebugType(const char *Type)
isCurrentDebugType - Return true if the specified string is the debug type specified on the command l...
static ManagedStatic< std::vector< std::string > > CurrentDebugType