1 //===-- Debug.cpp - An easy way to add debug output to your code ----------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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.
12 //
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
18 // foo class.
19 //
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
22 // code.
23 //
24 //===----------------------------------------------------------------------===//
25
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/ManagedStatic.h"
29 #include "llvm/Support/Signals.h"
30 #include "llvm/Support/circular_raw_ostream.h"
31 #include "llvm/Support/raw_ostream.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
45 #if 0 // XXX BINARYEN
46 static ManagedStatic<std::vector<std::string>> CurrentDebugType;
47 #endif
48
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.
isCurrentDebugType(const char * DebugType)52 bool isCurrentDebugType(const char *DebugType) {
53 llvm_unreachable("debug type");
54 }
55
56 /// Set the current debug type, as if the -debug-only=X
57 /// option were specified. Note that DebugFlag also needs to be set to true for
58 /// debug output to be produced.
59 ///
60 void setCurrentDebugTypes(const char **Types, unsigned Count);
61
setCurrentDebugType(const char * Type)62 void setCurrentDebugType(const char *Type) {
63 setCurrentDebugTypes(&Type, 1);
64 }
65
setCurrentDebugTypes(const char ** Types,unsigned Count)66 void setCurrentDebugTypes(const char **Types, unsigned Count) {
67 llvm_unreachable("set debug type");
68 }
69 } // namespace llvm
70
71 // All Debug.h functionality is a no-op in NDEBUG mode.
72 #if 0 // XXX BINARYEN ndef NDEBUG
73
74 // -debug - Command line option to enable the DEBUG statements in the passes.
75 // This flag may only be enabled in debug builds.
76 static cl::opt<bool, true>
77 Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
78 cl::location(DebugFlag));
79
80 // -debug-buffer-size - Buffer the last N characters of debug output
81 //until program termination.
82 static cl::opt<unsigned>
83 DebugBufferSize("debug-buffer-size",
84 cl::desc("Buffer the last N characters of debug output "
85 "until program termination. "
86 "[default 0 -- immediate print-out]"),
87 cl::Hidden,
88 cl::init(0));
89
90 namespace {
91
92 struct DebugOnlyOpt {
93 void operator=(const std::string &Val) const {
94 if (Val.empty())
95 return;
96 DebugFlag = true;
97 SmallVector<StringRef,8> dbgTypes;
98 StringRef(Val).split(dbgTypes, ',', -1, false);
99 for (auto dbgType : dbgTypes)
100 CurrentDebugType->push_back(dbgType);
101 }
102 };
103
104 }
105
106 static DebugOnlyOpt DebugOnlyOptLoc;
107
108 static cl::opt<DebugOnlyOpt, true, cl::parser<std::string> >
109 DebugOnly("debug-only", cl::desc("Enable a specific type of debug output (comma separated list of types)"),
110 cl::Hidden, cl::ZeroOrMore, cl::value_desc("debug string"),
111 cl::location(DebugOnlyOptLoc), cl::ValueRequired);
112 // Signal handlers - dump debug output on termination.
113 static void debug_user_sig_handler(void *Cookie) {
114 // This is a bit sneaky. Since this is under #ifndef NDEBUG, we
115 // know that debug mode is enabled and dbgs() really is a
116 // circular_raw_ostream. If NDEBUG is defined, then dbgs() ==
117 // errs() but this will never be invoked.
118 llvm::circular_raw_ostream &dbgout =
119 static_cast<circular_raw_ostream &>(llvm::dbgs());
120 dbgout.flushBufferWithBanner();
121 }
122
123 /// dbgs - Return a circular-buffered debug stream.
124 raw_ostream &llvm::dbgs() {
125 // Do one-time initialization in a thread-safe way.
126 static struct dbgstream {
127 circular_raw_ostream strm;
128
129 dbgstream() :
130 strm(errs(), "*** Debug Log Output ***\n",
131 (!EnableDebugBuffering || !DebugFlag) ? 0 : DebugBufferSize) {
132 if (EnableDebugBuffering && DebugFlag && DebugBufferSize != 0)
133 // TODO: Add a handler for SIGUSER1-type signals so the user can
134 // force a debug dump.
135 sys::AddSignalHandler(&debug_user_sig_handler, nullptr);
136 // Otherwise we've already set the debug stream buffer size to
137 // zero, disabling buffering so it will output directly to errs().
138 }
139 } thestrm;
140
141 return thestrm.strm;
142 }
143
144 #else
145 // Avoid "has no symbols" warning.
146 namespace llvm {
147 /// dbgs - Return errs().
dbgs()148 raw_ostream &dbgs() {
149 return errs();
150 }
151 }
152
153 #endif
154
155 /// EnableDebugBuffering - Turn on signal handler installation.
156 ///
157 bool llvm::EnableDebugBuffering = false;
158