LLVM  6.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 DEBUG() macro, and it will be
15 // 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 // DEBUG() statements disappears, so it does not affect the runtime of the code.
23 //
24 //===----------------------------------------------------------------------===//
25 
26 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/Signals.h"
32 
33 #undef isCurrentDebugType
34 #undef setCurrentDebugType
35 #undef setCurrentDebugTypes
36 
37 using namespace llvm;
38 
39 // Even though LLVM might be built with NDEBUG, define symbols that the code
40 // built without NDEBUG can depend on via the llvm/Support/Debug.h header.
41 namespace llvm {
42 /// Exported boolean set by the -debug option.
43 bool DebugFlag = false;
44 
46 
47 /// Return true if the specified string is the debug type
48 /// specified on the command line, or if none was specified on the command line
49 /// with the -debug-only=X option.
50 bool isCurrentDebugType(const char *DebugType) {
51  if (CurrentDebugType->empty())
52  return true;
53  // See if DebugType is in list. Note: do not use find() as that forces us to
54  // unnecessarily create an std::string instance.
55  for (auto &d : *CurrentDebugType) {
56  if (d == DebugType)
57  return true;
58  }
59  return false;
60 }
61 
62 /// Set the current debug type, as if the -debug-only=X
63 /// option were specified. Note that DebugFlag also needs to be set to true for
64 /// debug output to be produced.
65 ///
66 void setCurrentDebugTypes(const char **Types, unsigned Count);
67 
68 void setCurrentDebugType(const char *Type) {
69  setCurrentDebugTypes(&Type, 1);
70 }
71 
72 void setCurrentDebugTypes(const char **Types, unsigned Count) {
73  CurrentDebugType->clear();
74  for (size_t T = 0; T < Count; ++T)
75  CurrentDebugType->push_back(Types[T]);
76 }
77 } // namespace llvm
78 
79 // All Debug.h functionality is a no-op in NDEBUG mode.
80 #ifndef NDEBUG
81 
82 // -debug - Command line option to enable the DEBUG statements in the passes.
83 // This flag may only be enabled in debug builds.
85 Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
87 
88 // -debug-buffer-size - Buffer the last N characters of debug output
89 //until program termination.
90 static cl::opt<unsigned>
91 DebugBufferSize("debug-buffer-size",
92  cl::desc("Buffer the last N characters of debug output "
93  "until program termination. "
94  "[default 0 -- immediate print-out]"),
95  cl::Hidden,
96  cl::init(0));
97 
98 namespace {
99 
100 struct DebugOnlyOpt {
101  void operator=(const std::string &Val) const {
102  if (Val.empty())
103  return;
104  DebugFlag = true;
105  SmallVector<StringRef,8> dbgTypes;
106  StringRef(Val).split(dbgTypes, ',', -1, false);
107  for (auto dbgType : dbgTypes)
108  CurrentDebugType->push_back(dbgType);
109  }
110 };
111 
112 }
113 
114 static DebugOnlyOpt DebugOnlyOptLoc;
115 
117 DebugOnly("debug-only", cl::desc("Enable a specific type of debug output (comma separated list of types)"),
118  cl::Hidden, cl::ZeroOrMore, cl::value_desc("debug string"),
119  cl::location(DebugOnlyOptLoc), cl::ValueRequired);
120 // Signal handlers - dump debug output on termination.
121 static void debug_user_sig_handler(void *Cookie) {
122  // This is a bit sneaky. Since this is under #ifndef NDEBUG, we
123  // know that debug mode is enabled and dbgs() really is a
124  // circular_raw_ostream. If NDEBUG is defined, then dbgs() ==
125  // errs() but this will never be invoked.
127  static_cast<circular_raw_ostream &>(llvm::dbgs());
128  dbgout.flushBufferWithBanner();
129 }
130 
131 /// dbgs - Return a circular-buffered debug stream.
133  // Do one-time initialization in a thread-safe way.
134  static struct dbgstream {
136 
137  dbgstream() :
138  strm(errs(), "*** Debug Log Output ***\n",
139  (!EnableDebugBuffering || !DebugFlag) ? 0 : DebugBufferSize) {
140  if (EnableDebugBuffering && DebugFlag && DebugBufferSize != 0)
141  // TODO: Add a handler for SIGUSER1-type signals so the user can
142  // force a debug dump.
144  // Otherwise we've already set the debug stream buffer size to
145  // zero, disabling buffering so it will output directly to errs().
146  }
147  } thestrm;
148 
149  return thestrm.strm;
150 }
151 
152 #else
153 // Avoid "has no symbols" warning.
154 namespace llvm {
155  /// dbgs - Return errs().
156  raw_ostream &dbgs() {
157  return errs();
158  }
159 }
160 
161 #endif
162 
163 /// EnableDebugBuffering - Turn on signal handler installation.
164 ///
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.
DebugType
Definition: COFF.h:640
#define T
static DebugOnlyOpt DebugOnlyOptLoc
Definition: Debug.cpp:114
void setCurrentDebugType(const char *Type)
setCurrentDebugType - Set the current debug type, as if the -debug-only=X option were specified...
Definition: Debug.cpp:68
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:404
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:864
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:132
static ManagedStatic< std::vector< std::string > > CurrentDebugType
Definition: Debug.cpp:45
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)
bool EnableDebugBuffering
EnableDebugBuffering - This defaults to false.
Definition: Debug.cpp:165
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:44
static void debug_user_sig_handler(void *Cookie)
Definition: Debug.cpp:121
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:420
bool DebugFlag
This boolean is set to true if the &#39;-debug&#39; command line option is specified.
Definition: Debug.cpp:43
void AddSignalHandler(void(*FnPtr)(void *), void *Cookie)
AddSignalHandler - Add a function to be called when an abort/kill signal is delivered to the process...
bool isCurrentDebugType(const char *Type)
isCurrentDebugType - Return true if the specified string is the debug type specified on the command l...
Definition: Debug.cpp:50
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:72