LLVM  8.0.0svn
Debug.cpp
Go to the documentation of this file.
1 //===-- Debug.cpp - An easy way to add debug output to your code ----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements a handy way of adding debugging information to your
11 // code, without it being enabled all of the time, and without having to add
12 // command line options to enable it.
13 //
14 // In particular, just wrap your code with the LLVM_DEBUG() macro, and it will
15 // be enabled automatically if you specify '-debug' on the command-line.
16 // Alternatively, you can also use the SET_DEBUG_TYPE("foo") macro to specify
17 // that your debug code belongs to class "foo". Then, on the command line, you
18 // can specify '-debug-only=foo' to enable JUST the debug information for the
19 // foo class.
20 //
21 // When compiling without assertions, the -debug-* options and all code in
22 // LLVM_DEBUG() statements disappears, so it does not affect the runtime of the
23 // code.
24 //
25 //===----------------------------------------------------------------------===//
26 
27 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/Signals.h"
33 
34 #undef isCurrentDebugType
35 #undef setCurrentDebugType
36 #undef setCurrentDebugTypes
37 
38 using namespace llvm;
39 
40 // Even though LLVM might be built with NDEBUG, define symbols that the code
41 // built without NDEBUG can depend on via the llvm/Support/Debug.h header.
42 namespace llvm {
43 /// Exported boolean set by the -debug option.
44 bool DebugFlag = false;
45 
47 
48 /// Return true if the specified string is the debug type
49 /// specified on the command line, or if none was specified on the command line
50 /// with the -debug-only=X option.
51 bool isCurrentDebugType(const char *DebugType) {
52  if (CurrentDebugType->empty())
53  return true;
54  // See if DebugType is in list. Note: do not use find() as that forces us to
55  // unnecessarily create an std::string instance.
56  for (auto &d : *CurrentDebugType) {
57  if (d == DebugType)
58  return true;
59  }
60  return false;
61 }
62 
63 /// Set the current debug type, as if the -debug-only=X
64 /// option were specified. Note that DebugFlag also needs to be set to true for
65 /// debug output to be produced.
66 ///
67 void setCurrentDebugTypes(const char **Types, unsigned Count);
68 
69 void setCurrentDebugType(const char *Type) {
70  setCurrentDebugTypes(&Type, 1);
71 }
72 
73 void setCurrentDebugTypes(const char **Types, unsigned Count) {
74  CurrentDebugType->clear();
75  for (size_t T = 0; T < Count; ++T)
76  CurrentDebugType->push_back(Types[T]);
77 }
78 } // namespace llvm
79 
80 // All Debug.h functionality is a no-op in NDEBUG mode.
81 #ifndef NDEBUG
82 
83 // -debug - Command line option to enable the DEBUG statements in the passes.
84 // This flag may only be enabled in debug builds.
86 Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
88 
89 // -debug-buffer-size - Buffer the last N characters of debug output
90 //until program termination.
91 static cl::opt<unsigned>
92 DebugBufferSize("debug-buffer-size",
93  cl::desc("Buffer the last N characters of debug output "
94  "until program termination. "
95  "[default 0 -- immediate print-out]"),
96  cl::Hidden,
97  cl::init(0));
98 
99 namespace {
100 
101 struct DebugOnlyOpt {
102  void operator=(const std::string &Val) const {
103  if (Val.empty())
104  return;
105  DebugFlag = true;
106  SmallVector<StringRef,8> dbgTypes;
107  StringRef(Val).split(dbgTypes, ',', -1, false);
108  for (auto dbgType : dbgTypes)
109  CurrentDebugType->push_back(dbgType);
110  }
111 };
112 
113 }
114 
115 static DebugOnlyOpt DebugOnlyOptLoc;
116 
118 DebugOnly("debug-only", cl::desc("Enable a specific type of debug output (comma separated list of types)"),
119  cl::Hidden, cl::ZeroOrMore, cl::value_desc("debug string"),
120  cl::location(DebugOnlyOptLoc), cl::ValueRequired);
121 // Signal handlers - dump debug output on termination.
122 static void debug_user_sig_handler(void *Cookie) {
123  // This is a bit sneaky. Since this is under #ifndef NDEBUG, we
124  // know that debug mode is enabled and dbgs() really is a
125  // circular_raw_ostream. If NDEBUG is defined, then dbgs() ==
126  // errs() but this will never be invoked.
128  static_cast<circular_raw_ostream &>(llvm::dbgs());
129  dbgout.flushBufferWithBanner();
130 }
131 
132 /// dbgs - Return a circular-buffered debug stream.
134  // Do one-time initialization in a thread-safe way.
135  static struct dbgstream {
137 
138  dbgstream() :
139  strm(errs(), "*** Debug Log Output ***\n",
140  (!EnableDebugBuffering || !DebugFlag) ? 0 : DebugBufferSize) {
141  if (EnableDebugBuffering && DebugFlag && DebugBufferSize != 0)
142  // TODO: Add a handler for SIGUSER1-type signals so the user can
143  // force a debug dump.
145  // Otherwise we've already set the debug stream buffer size to
146  // zero, disabling buffering so it will output directly to errs().
147  }
148  } thestrm;
149 
150  return thestrm.strm;
151 }
152 
153 #else
154 // Avoid "has no symbols" warning.
155 namespace llvm {
156  /// dbgs - Return errs().
157  raw_ostream &dbgs() {
158  return errs();
159  }
160 }
161 
162 #endif
163 
164 /// EnableDebugBuffering - Turn on signal handler installation.
165 ///
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
circular_raw_ostream - A raw_ostream which can save its data to a circular buffer, or can pass it through directly to an underlying stream if specified with a buffer of zero.
#define T
static DebugOnlyOpt DebugOnlyOptLoc
Definition: Debug.cpp:115
void setCurrentDebugType(const char *Type)
setCurrentDebugType - Set the current debug type, as if the -debug-only=X option were specified...
Definition: Debug.cpp:69
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:410
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
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))
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:727
void flushBufferWithBanner()
flushBufferWithBanner - Force output of the buffer along with a small header.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
static ManagedStatic< std::vector< std::string > > CurrentDebugType
Definition: Debug.cpp:46
static cl::opt< DebugOnlyOpt, true, cl::parser< std::string > > DebugOnly("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)
void AddSignalHandler(SignalHandlerCallback FnPtr, void *Cookie)
Add a function to be called when an abort/kill signal is delivered to the process.
bool EnableDebugBuffering
EnableDebugBuffering - This defaults to false.
Definition: Debug.cpp:166
DebugType
Definition: COFF.h:643
static cl::opt< bool, true > Debug("debug", cl::desc("Enable debug output"), cl::Hidden, cl::location(DebugFlag))
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
static void debug_user_sig_handler(void *Cookie)
Definition: Debug.cpp:122
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition: ManagedStatic.h:61
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:426
bool DebugFlag
This boolean is set to true if the &#39;-debug&#39; command line option is specified.
Definition: Debug.cpp:44
bool isCurrentDebugType(const char *Type)
isCurrentDebugType - Return true if the specified string is the debug type specified on the command l...
Definition: Debug.cpp:51
void setCurrentDebugTypes(const char **Types, unsigned Count)
setCurrentDebugTypes - Set the current debug type, as if the -debug-only=X,Y,Z option were specified...
Definition: Debug.cpp:73