1*0b57cec5SDimitry Andric //===-- Debug.cpp - An easy way to add debug output to your code ----------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file implements a handy way of adding debugging information to your
10*0b57cec5SDimitry Andric // code, without it being enabled all of the time, and without having to add
11*0b57cec5SDimitry Andric // command line options to enable it.
12*0b57cec5SDimitry Andric //
13*0b57cec5SDimitry Andric // In particular, just wrap your code with the LLVM_DEBUG() macro, and it will
14*0b57cec5SDimitry Andric // be enabled automatically if you specify '-debug' on the command-line.
15*0b57cec5SDimitry Andric // Alternatively, you can also use the SET_DEBUG_TYPE("foo") macro to specify
16*0b57cec5SDimitry Andric // that your debug code belongs to class "foo". Then, on the command line, you
17*0b57cec5SDimitry Andric // can specify '-debug-only=foo' to enable JUST the debug information for the
18*0b57cec5SDimitry Andric // foo class.
19*0b57cec5SDimitry Andric //
20*0b57cec5SDimitry Andric // When compiling without assertions, the -debug-* options and all code in
21*0b57cec5SDimitry Andric // LLVM_DEBUG() statements disappears, so it does not affect the runtime of the
22*0b57cec5SDimitry Andric // code.
23*0b57cec5SDimitry Andric //
24*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
25*0b57cec5SDimitry Andric
26*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
27*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
28*0b57cec5SDimitry Andric #include "llvm/Support/ManagedStatic.h"
29*0b57cec5SDimitry Andric #include "llvm/Support/Signals.h"
30*0b57cec5SDimitry Andric #include "llvm/Support/circular_raw_ostream.h"
31*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
32*0b57cec5SDimitry Andric
33*0b57cec5SDimitry Andric #include "DebugOptions.h"
34*0b57cec5SDimitry Andric
35*0b57cec5SDimitry Andric #undef isCurrentDebugType
36*0b57cec5SDimitry Andric #undef setCurrentDebugType
37*0b57cec5SDimitry Andric #undef setCurrentDebugTypes
38*0b57cec5SDimitry Andric
39*0b57cec5SDimitry Andric using namespace llvm;
40*0b57cec5SDimitry Andric
41*0b57cec5SDimitry Andric // Even though LLVM might be built with NDEBUG, define symbols that the code
42*0b57cec5SDimitry Andric // built without NDEBUG can depend on via the llvm/Support/Debug.h header.
43*0b57cec5SDimitry Andric namespace llvm {
44*0b57cec5SDimitry Andric /// Exported boolean set by the -debug option.
45*0b57cec5SDimitry Andric bool DebugFlag = false;
46*0b57cec5SDimitry Andric
47*0b57cec5SDimitry Andric static ManagedStatic<std::vector<std::string>> CurrentDebugType;
48*0b57cec5SDimitry Andric
49*0b57cec5SDimitry Andric /// Return true if the specified string is the debug type
50*0b57cec5SDimitry Andric /// specified on the command line, or if none was specified on the command line
51*0b57cec5SDimitry Andric /// with the -debug-only=X option.
isCurrentDebugType(const char * DebugType)52*0b57cec5SDimitry Andric bool isCurrentDebugType(const char *DebugType) {
53*0b57cec5SDimitry Andric if (CurrentDebugType->empty())
54*0b57cec5SDimitry Andric return true;
55*0b57cec5SDimitry Andric // See if DebugType is in list. Note: do not use find() as that forces us to
56*0b57cec5SDimitry Andric // unnecessarily create an std::string instance.
57*0b57cec5SDimitry Andric for (auto &d : *CurrentDebugType) {
58*0b57cec5SDimitry Andric if (d == DebugType)
59*0b57cec5SDimitry Andric return true;
60*0b57cec5SDimitry Andric }
61*0b57cec5SDimitry Andric return false;
62*0b57cec5SDimitry Andric }
63*0b57cec5SDimitry Andric
64*0b57cec5SDimitry Andric /// Set the current debug type, as if the -debug-only=X
65*0b57cec5SDimitry Andric /// option were specified. Note that DebugFlag also needs to be set to true for
66*0b57cec5SDimitry Andric /// debug output to be produced.
67*0b57cec5SDimitry Andric ///
68*0b57cec5SDimitry Andric void setCurrentDebugTypes(const char **Types, unsigned Count);
69*0b57cec5SDimitry Andric
setCurrentDebugType(const char * Type)70*0b57cec5SDimitry Andric void setCurrentDebugType(const char *Type) {
71*0b57cec5SDimitry Andric setCurrentDebugTypes(&Type, 1);
72*0b57cec5SDimitry Andric }
73*0b57cec5SDimitry Andric
setCurrentDebugTypes(const char ** Types,unsigned Count)74*0b57cec5SDimitry Andric void setCurrentDebugTypes(const char **Types, unsigned Count) {
75*0b57cec5SDimitry Andric CurrentDebugType->clear();
76*0b57cec5SDimitry Andric for (size_t T = 0; T < Count; ++T)
77*0b57cec5SDimitry Andric CurrentDebugType->push_back(Types[T]);
78*0b57cec5SDimitry Andric }
79*0b57cec5SDimitry Andric } // namespace llvm
80*0b57cec5SDimitry Andric
81*0b57cec5SDimitry Andric // All Debug.h functionality is a no-op in NDEBUG mode.
82*0b57cec5SDimitry Andric #ifndef NDEBUG
83*0b57cec5SDimitry Andric
84*0b57cec5SDimitry Andric namespace {
85*0b57cec5SDimitry Andric struct CreateDebug {
call__anon882be4d90111::CreateDebug86*0b57cec5SDimitry Andric static void *call() {
87*0b57cec5SDimitry Andric return new cl::opt<bool, true>("debug", cl::desc("Enable debug output"),
88*0b57cec5SDimitry Andric cl::Hidden, cl::location(DebugFlag));
89*0b57cec5SDimitry Andric }
90*0b57cec5SDimitry Andric };
91*0b57cec5SDimitry Andric
92*0b57cec5SDimitry Andric // -debug-buffer-size - Buffer the last N characters of debug output
93*0b57cec5SDimitry Andric //until program termination.
94*0b57cec5SDimitry Andric struct CreateDebugBufferSize {
call__anon882be4d90111::CreateDebugBufferSize95*0b57cec5SDimitry Andric static void *call() {
96*0b57cec5SDimitry Andric return new cl::opt<unsigned>(
97*0b57cec5SDimitry Andric "debug-buffer-size",
98*0b57cec5SDimitry Andric cl::desc("Buffer the last N characters of debug output "
99*0b57cec5SDimitry Andric "until program termination. "
100*0b57cec5SDimitry Andric "[default 0 -- immediate print-out]"),
101*0b57cec5SDimitry Andric cl::Hidden, cl::init(0));
102*0b57cec5SDimitry Andric }
103*0b57cec5SDimitry Andric };
104*0b57cec5SDimitry Andric } // namespace
105*0b57cec5SDimitry Andric
106*0b57cec5SDimitry Andric // -debug - Command line option to enable the DEBUG statements in the passes.
107*0b57cec5SDimitry Andric // This flag may only be enabled in debug builds.
108*0b57cec5SDimitry Andric static ManagedStatic<cl::opt<bool, true>, CreateDebug> Debug;
109*0b57cec5SDimitry Andric static ManagedStatic<cl::opt<unsigned>, CreateDebugBufferSize> DebugBufferSize;
110*0b57cec5SDimitry Andric
111*0b57cec5SDimitry Andric namespace {
112*0b57cec5SDimitry Andric
113*0b57cec5SDimitry Andric struct DebugOnlyOpt {
operator =__anon882be4d90211::DebugOnlyOpt114*0b57cec5SDimitry Andric void operator=(const std::string &Val) const {
115*0b57cec5SDimitry Andric if (Val.empty())
116*0b57cec5SDimitry Andric return;
117*0b57cec5SDimitry Andric DebugFlag = true;
118*0b57cec5SDimitry Andric SmallVector<StringRef,8> dbgTypes;
119*0b57cec5SDimitry Andric StringRef(Val).split(dbgTypes, ',', -1, false);
120*0b57cec5SDimitry Andric for (auto dbgType : dbgTypes)
121*0b57cec5SDimitry Andric CurrentDebugType->push_back(std::string(dbgType));
122*0b57cec5SDimitry Andric }
123*0b57cec5SDimitry Andric };
124*0b57cec5SDimitry Andric } // namespace
125*0b57cec5SDimitry Andric
126*0b57cec5SDimitry Andric static DebugOnlyOpt DebugOnlyOptLoc;
127*0b57cec5SDimitry Andric
128*0b57cec5SDimitry Andric namespace {
129*0b57cec5SDimitry Andric struct CreateDebugOnly {
call__anon882be4d90311::CreateDebugOnly130*0b57cec5SDimitry Andric static void *call() {
131*0b57cec5SDimitry Andric return new cl::opt<DebugOnlyOpt, true, cl::parser<std::string>>(
132*0b57cec5SDimitry Andric "debug-only",
133*0b57cec5SDimitry Andric cl::desc("Enable a specific type of debug output (comma separated list "
134*0b57cec5SDimitry Andric "of types)"),
135*0b57cec5SDimitry Andric cl::Hidden, cl::value_desc("debug string"),
136*0b57cec5SDimitry Andric cl::location(DebugOnlyOptLoc), cl::ValueRequired);
137*0b57cec5SDimitry Andric }
138*0b57cec5SDimitry Andric };
139*0b57cec5SDimitry Andric } // namespace
140*0b57cec5SDimitry Andric
141*0b57cec5SDimitry Andric static ManagedStatic<cl::opt<DebugOnlyOpt, true, cl::parser<std::string>>,
142*0b57cec5SDimitry Andric CreateDebugOnly>
143*0b57cec5SDimitry Andric DebugOnly;
144*0b57cec5SDimitry Andric
initDebugOptions()145*0b57cec5SDimitry Andric void llvm::initDebugOptions() {
146*0b57cec5SDimitry Andric *Debug;
147*0b57cec5SDimitry Andric *DebugBufferSize;
148*0b57cec5SDimitry Andric *DebugOnly;
149*0b57cec5SDimitry Andric }
150*0b57cec5SDimitry Andric
151*0b57cec5SDimitry Andric // Signal handlers - dump debug output on termination.
debug_user_sig_handler(void * Cookie)152*0b57cec5SDimitry Andric static void debug_user_sig_handler(void *Cookie) {
153*0b57cec5SDimitry Andric // This is a bit sneaky. Since this is under #ifndef NDEBUG, we
154*0b57cec5SDimitry Andric // know that debug mode is enabled and dbgs() really is a
155*0b57cec5SDimitry Andric // circular_raw_ostream. If NDEBUG is defined, then dbgs() ==
156*0b57cec5SDimitry Andric // errs() but this will never be invoked.
157*0b57cec5SDimitry Andric llvm::circular_raw_ostream &dbgout =
158*0b57cec5SDimitry Andric static_cast<circular_raw_ostream &>(llvm::dbgs());
159*0b57cec5SDimitry Andric dbgout.flushBufferWithBanner();
160*0b57cec5SDimitry Andric }
161*0b57cec5SDimitry Andric
162*0b57cec5SDimitry Andric /// dbgs - Return a circular-buffered debug stream.
dbgs()163*0b57cec5SDimitry Andric raw_ostream &llvm::dbgs() {
164*0b57cec5SDimitry Andric // Do one-time initialization in a thread-safe way.
165*0b57cec5SDimitry Andric static struct dbgstream {
166 circular_raw_ostream strm;
167
168 dbgstream()
169 : strm(errs(), "*** Debug Log Output ***\n",
170 (!EnableDebugBuffering || !DebugFlag) ? 0 : *DebugBufferSize) {
171 if (EnableDebugBuffering && DebugFlag && *DebugBufferSize != 0)
172 // TODO: Add a handler for SIGUSER1-type signals so the user can
173 // force a debug dump.
174 sys::AddSignalHandler(&debug_user_sig_handler, nullptr);
175 // Otherwise we've already set the debug stream buffer size to
176 // zero, disabling buffering so it will output directly to errs().
177 }
178 } thestrm;
179
180 return thestrm.strm;
181 }
182
183 #else
184 // Avoid "has no symbols" warning.
185 namespace llvm {
186 /// dbgs - Return errs().
dbgs()187 raw_ostream &dbgs() {
188 return errs();
189 }
190 }
initDebugOptions()191 void llvm::initDebugOptions() {}
192 #endif
193
194 /// EnableDebugBuffering - Turn on signal handler installation.
195 ///
196 bool llvm::EnableDebugBuffering = false;
197