1f22ef01cSRoman Divacky //===-- Debug.cpp - An easy way to add debug output to your code ----------===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky //                     The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky //
107ae0e2c9SDimitry Andric // This file implements a handy way of adding debugging information to your
11f22ef01cSRoman Divacky // code, without it being enabled all of the time, and without having to add
12f22ef01cSRoman Divacky // command line options to enable it.
13f22ef01cSRoman Divacky //
14*4ba319b5SDimitry Andric // In particular, just wrap your code with the LLVM_DEBUG() macro, and it will
15*4ba319b5SDimitry Andric // be enabled automatically if you specify '-debug' on the command-line.
16f22ef01cSRoman Divacky // Alternatively, you can also use the SET_DEBUG_TYPE("foo") macro to specify
17f22ef01cSRoman Divacky // that your debug code belongs to class "foo".  Then, on the command line, you
18f22ef01cSRoman Divacky // can specify '-debug-only=foo' to enable JUST the debug information for the
19f22ef01cSRoman Divacky // foo class.
20f22ef01cSRoman Divacky //
217ae0e2c9SDimitry Andric // When compiling without assertions, the -debug-* options and all code in
22*4ba319b5SDimitry Andric // LLVM_DEBUG() statements disappears, so it does not affect the runtime of the
23*4ba319b5SDimitry Andric // code.
24f22ef01cSRoman Divacky //
25f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
26f22ef01cSRoman Divacky 
27f22ef01cSRoman Divacky #include "llvm/Support/Debug.h"
28139f7f9bSDimitry Andric #include "llvm/Support/CommandLine.h"
2939d628a0SDimitry Andric #include "llvm/Support/ManagedStatic.h"
302754fe60SDimitry Andric #include "llvm/Support/Signals.h"
31139f7f9bSDimitry Andric #include "llvm/Support/circular_raw_ostream.h"
32ff0cc061SDimitry Andric #include "llvm/Support/raw_ostream.h"
33ff0cc061SDimitry Andric 
34ff0cc061SDimitry Andric #undef isCurrentDebugType
35ff0cc061SDimitry Andric #undef setCurrentDebugType
36d88c1a5aSDimitry Andric #undef setCurrentDebugTypes
37f22ef01cSRoman Divacky 
38f22ef01cSRoman Divacky using namespace llvm;
39f22ef01cSRoman Divacky 
40ff0cc061SDimitry Andric // Even though LLVM might be built with NDEBUG, define symbols that the code
41ff0cc061SDimitry Andric // built without NDEBUG can depend on via the llvm/Support/Debug.h header.
42ff0cc061SDimitry Andric namespace llvm {
43ff0cc061SDimitry Andric /// Exported boolean set by the -debug option.
44ff0cc061SDimitry Andric bool DebugFlag = false;
45ff0cc061SDimitry Andric 
46ff0cc061SDimitry Andric static ManagedStatic<std::vector<std::string>> CurrentDebugType;
47ff0cc061SDimitry Andric 
48ff0cc061SDimitry Andric /// Return true if the specified string is the debug type
49ff0cc061SDimitry Andric /// specified on the command line, or if none was specified on the command line
50ff0cc061SDimitry Andric /// with the -debug-only=X option.
isCurrentDebugType(const char * DebugType)51ff0cc061SDimitry Andric bool isCurrentDebugType(const char *DebugType) {
52ff0cc061SDimitry Andric   if (CurrentDebugType->empty())
53ff0cc061SDimitry Andric     return true;
5497bc6c73SDimitry Andric   // See if DebugType is in list. Note: do not use find() as that forces us to
55ff0cc061SDimitry Andric   // unnecessarily create an std::string instance.
5697bc6c73SDimitry Andric   for (auto &d : *CurrentDebugType) {
57ff0cc061SDimitry Andric     if (d == DebugType)
58ff0cc061SDimitry Andric       return true;
59ff0cc061SDimitry Andric   }
60ff0cc061SDimitry Andric   return false;
61ff0cc061SDimitry Andric }
62ff0cc061SDimitry Andric 
63ff0cc061SDimitry Andric /// Set the current debug type, as if the -debug-only=X
64ff0cc061SDimitry Andric /// option were specified.  Note that DebugFlag also needs to be set to true for
65ff0cc061SDimitry Andric /// debug output to be produced.
66ff0cc061SDimitry Andric ///
67d88c1a5aSDimitry Andric void setCurrentDebugTypes(const char **Types, unsigned Count);
68d88c1a5aSDimitry Andric 
setCurrentDebugType(const char * Type)69ff0cc061SDimitry Andric void setCurrentDebugType(const char *Type) {
70d88c1a5aSDimitry Andric   setCurrentDebugTypes(&Type, 1);
71ff0cc061SDimitry Andric }
72ff0cc061SDimitry Andric 
setCurrentDebugTypes(const char ** Types,unsigned Count)73d88c1a5aSDimitry Andric void setCurrentDebugTypes(const char **Types, unsigned Count) {
74d88c1a5aSDimitry Andric   CurrentDebugType->clear();
75d88c1a5aSDimitry Andric   for (size_t T = 0; T < Count; ++T)
76d88c1a5aSDimitry Andric     CurrentDebugType->push_back(Types[T]);
77d88c1a5aSDimitry Andric }
78ff0cc061SDimitry Andric } // namespace llvm
79ff0cc061SDimitry Andric 
80f22ef01cSRoman Divacky // All Debug.h functionality is a no-op in NDEBUG mode.
81f22ef01cSRoman Divacky #ifndef NDEBUG
82f22ef01cSRoman Divacky 
83f22ef01cSRoman Divacky // -debug - Command line option to enable the DEBUG statements in the passes.
84f22ef01cSRoman Divacky // This flag may only be enabled in debug builds.
85f22ef01cSRoman Divacky static cl::opt<bool, true>
86f22ef01cSRoman Divacky Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
87f22ef01cSRoman Divacky       cl::location(DebugFlag));
88f22ef01cSRoman Divacky 
89f22ef01cSRoman Divacky // -debug-buffer-size - Buffer the last N characters of debug output
90f22ef01cSRoman Divacky //until program termination.
91f22ef01cSRoman Divacky static cl::opt<unsigned>
92f22ef01cSRoman Divacky DebugBufferSize("debug-buffer-size",
93f22ef01cSRoman Divacky                 cl::desc("Buffer the last N characters of debug output "
94f22ef01cSRoman Divacky                          "until program termination. "
95f22ef01cSRoman Divacky                          "[default 0 -- immediate print-out]"),
96f22ef01cSRoman Divacky                 cl::Hidden,
97f22ef01cSRoman Divacky                 cl::init(0));
98f22ef01cSRoman Divacky 
99f22ef01cSRoman Divacky namespace {
100f22ef01cSRoman Divacky 
101f22ef01cSRoman Divacky struct DebugOnlyOpt {
operator =__anon1220c2a80111::DebugOnlyOpt102f22ef01cSRoman Divacky   void operator=(const std::string &Val) const {
10339d628a0SDimitry Andric     if (Val.empty())
10439d628a0SDimitry Andric       return;
10539d628a0SDimitry Andric     DebugFlag = true;
106444ed5c5SDimitry Andric     SmallVector<StringRef,8> dbgTypes;
107444ed5c5SDimitry Andric     StringRef(Val).split(dbgTypes, ',', -1, false);
108444ed5c5SDimitry Andric     for (auto dbgType : dbgTypes)
109444ed5c5SDimitry Andric       CurrentDebugType->push_back(dbgType);
110f22ef01cSRoman Divacky   }
111f22ef01cSRoman Divacky };
112f22ef01cSRoman Divacky 
1133dac3a9bSDimitry Andric }
114f22ef01cSRoman Divacky 
115f22ef01cSRoman Divacky static DebugOnlyOpt DebugOnlyOptLoc;
116f22ef01cSRoman Divacky 
117f22ef01cSRoman Divacky static cl::opt<DebugOnlyOpt, true, cl::parser<std::string> >
118444ed5c5SDimitry Andric DebugOnly("debug-only", cl::desc("Enable a specific type of debug output (comma separated list of types)"),
11939d628a0SDimitry Andric           cl::Hidden, cl::ZeroOrMore, cl::value_desc("debug string"),
120f22ef01cSRoman Divacky           cl::location(DebugOnlyOptLoc), cl::ValueRequired);
121f22ef01cSRoman Divacky // Signal handlers - dump debug output on termination.
debug_user_sig_handler(void * Cookie)122f22ef01cSRoman Divacky static void debug_user_sig_handler(void *Cookie) {
123f22ef01cSRoman Divacky   // This is a bit sneaky.  Since this is under #ifndef NDEBUG, we
124f22ef01cSRoman Divacky   // know that debug mode is enabled and dbgs() really is a
125f22ef01cSRoman Divacky   // circular_raw_ostream.  If NDEBUG is defined, then dbgs() ==
126f22ef01cSRoman Divacky   // errs() but this will never be invoked.
127ff0cc061SDimitry Andric   llvm::circular_raw_ostream &dbgout =
128ff0cc061SDimitry Andric       static_cast<circular_raw_ostream &>(llvm::dbgs());
129ff0cc061SDimitry Andric   dbgout.flushBufferWithBanner();
130f22ef01cSRoman Divacky }
131f22ef01cSRoman Divacky 
132f22ef01cSRoman Divacky /// dbgs - Return a circular-buffered debug stream.
dbgs()133f22ef01cSRoman Divacky raw_ostream &llvm::dbgs() {
134f22ef01cSRoman Divacky   // Do one-time initialization in a thread-safe way.
135f22ef01cSRoman Divacky   static struct dbgstream {
136f22ef01cSRoman Divacky     circular_raw_ostream strm;
137f22ef01cSRoman Divacky 
138f22ef01cSRoman Divacky     dbgstream() :
139f22ef01cSRoman Divacky         strm(errs(), "*** Debug Log Output ***\n",
140f22ef01cSRoman Divacky              (!EnableDebugBuffering || !DebugFlag) ? 0 : DebugBufferSize) {
141f22ef01cSRoman Divacky       if (EnableDebugBuffering && DebugFlag && DebugBufferSize != 0)
142f22ef01cSRoman Divacky         // TODO: Add a handler for SIGUSER1-type signals so the user can
143f22ef01cSRoman Divacky         // force a debug dump.
14491bc56edSDimitry Andric         sys::AddSignalHandler(&debug_user_sig_handler, nullptr);
145f22ef01cSRoman Divacky       // Otherwise we've already set the debug stream buffer size to
146f22ef01cSRoman Divacky       // zero, disabling buffering so it will output directly to errs().
147f22ef01cSRoman Divacky     }
148f22ef01cSRoman Divacky   } thestrm;
149f22ef01cSRoman Divacky 
150f22ef01cSRoman Divacky   return thestrm.strm;
151f22ef01cSRoman Divacky }
152f22ef01cSRoman Divacky 
153f22ef01cSRoman Divacky #else
154f22ef01cSRoman Divacky // Avoid "has no symbols" warning.
155f22ef01cSRoman Divacky namespace llvm {
156f22ef01cSRoman Divacky   /// dbgs - Return errs().
dbgs()157f22ef01cSRoman Divacky   raw_ostream &dbgs() {
158f22ef01cSRoman Divacky     return errs();
159f22ef01cSRoman Divacky   }
160f22ef01cSRoman Divacky }
161f22ef01cSRoman Divacky 
162f22ef01cSRoman Divacky #endif
163f22ef01cSRoman Divacky 
164f22ef01cSRoman Divacky /// EnableDebugBuffering - Turn on signal handler installation.
165f22ef01cSRoman Divacky ///
166f22ef01cSRoman Divacky bool llvm::EnableDebugBuffering = false;
167