xref: /src/contrib/llvm-project/llvm/lib/Support/Debug.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
1009b1c42SEd Schouten //===-- Debug.cpp - An easy way to add debug output to your code ----------===//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten //
958b69754SDimitry Andric // This file implements a handy way of adding debugging information to your
10009b1c42SEd Schouten // code, without it being enabled all of the time, and without having to add
11009b1c42SEd Schouten // command line options to enable it.
12009b1c42SEd Schouten //
13eb11fae6SDimitry Andric // In particular, just wrap your code with the LLVM_DEBUG() macro, and it will
14eb11fae6SDimitry Andric // be enabled automatically if you specify '-debug' on the command-line.
15009b1c42SEd Schouten // Alternatively, you can also use the SET_DEBUG_TYPE("foo") macro to specify
16009b1c42SEd Schouten // that your debug code belongs to class "foo".  Then, on the command line, you
17009b1c42SEd Schouten // can specify '-debug-only=foo' to enable JUST the debug information for the
18009b1c42SEd Schouten // foo class.
19009b1c42SEd Schouten //
2058b69754SDimitry Andric // When compiling without assertions, the -debug-* options and all code in
21eb11fae6SDimitry Andric // LLVM_DEBUG() statements disappears, so it does not affect the runtime of the
22eb11fae6SDimitry Andric // code.
23009b1c42SEd Schouten //
24009b1c42SEd Schouten //===----------------------------------------------------------------------===//
25009b1c42SEd Schouten 
26009b1c42SEd Schouten #include "llvm/Support/Debug.h"
274a16efa3SDimitry Andric #include "llvm/Support/CommandLine.h"
2867c32a98SDimitry Andric #include "llvm/Support/ManagedStatic.h"
29cf099d11SDimitry Andric #include "llvm/Support/Signals.h"
304a16efa3SDimitry Andric #include "llvm/Support/circular_raw_ostream.h"
315a5ac124SDimitry Andric #include "llvm/Support/raw_ostream.h"
325a5ac124SDimitry Andric 
33344a3780SDimitry Andric #include "DebugOptions.h"
34344a3780SDimitry Andric 
355a5ac124SDimitry Andric #undef isCurrentDebugType
365a5ac124SDimitry Andric #undef setCurrentDebugType
37b915e9e0SDimitry Andric #undef setCurrentDebugTypes
381e7804dbSRoman Divacky 
39009b1c42SEd Schouten using namespace llvm;
40009b1c42SEd Schouten 
415a5ac124SDimitry Andric // Even though LLVM might be built with NDEBUG, define symbols that the code
425a5ac124SDimitry Andric // built without NDEBUG can depend on via the llvm/Support/Debug.h header.
435a5ac124SDimitry Andric namespace llvm {
445a5ac124SDimitry Andric /// Exported boolean set by the -debug option.
455a5ac124SDimitry Andric bool DebugFlag = false;
465a5ac124SDimitry Andric 
475a5ac124SDimitry Andric static ManagedStatic<std::vector<std::string>> CurrentDebugType;
485a5ac124SDimitry Andric 
495a5ac124SDimitry Andric /// Return true if the specified string is the debug type
505a5ac124SDimitry Andric /// specified on the command line, or if none was specified on the command line
515a5ac124SDimitry Andric /// with the -debug-only=X option.
isCurrentDebugType(const char * DebugType)525a5ac124SDimitry Andric bool isCurrentDebugType(const char *DebugType) {
535a5ac124SDimitry Andric   if (CurrentDebugType->empty())
545a5ac124SDimitry Andric     return true;
5585d8b2bbSDimitry Andric   // See if DebugType is in list. Note: do not use find() as that forces us to
565a5ac124SDimitry Andric   // unnecessarily create an std::string instance.
5785d8b2bbSDimitry Andric   for (auto &d : *CurrentDebugType) {
585a5ac124SDimitry Andric     if (d == DebugType)
595a5ac124SDimitry Andric       return true;
605a5ac124SDimitry Andric   }
615a5ac124SDimitry Andric   return false;
625a5ac124SDimitry Andric }
635a5ac124SDimitry Andric 
645a5ac124SDimitry Andric /// Set the current debug type, as if the -debug-only=X
655a5ac124SDimitry Andric /// option were specified.  Note that DebugFlag also needs to be set to true for
665a5ac124SDimitry Andric /// debug output to be produced.
675a5ac124SDimitry Andric ///
68b915e9e0SDimitry Andric void setCurrentDebugTypes(const char **Types, unsigned Count);
69b915e9e0SDimitry Andric 
setCurrentDebugType(const char * Type)705a5ac124SDimitry Andric void setCurrentDebugType(const char *Type) {
71b915e9e0SDimitry Andric   setCurrentDebugTypes(&Type, 1);
725a5ac124SDimitry Andric }
735a5ac124SDimitry Andric 
setCurrentDebugTypes(const char ** Types,unsigned Count)74b915e9e0SDimitry Andric void setCurrentDebugTypes(const char **Types, unsigned Count) {
75b915e9e0SDimitry Andric   CurrentDebugType->clear();
76b915e9e0SDimitry Andric   for (size_t T = 0; T < Count; ++T)
77b915e9e0SDimitry Andric     CurrentDebugType->push_back(Types[T]);
78b915e9e0SDimitry Andric }
795a5ac124SDimitry Andric } // namespace llvm
805a5ac124SDimitry Andric 
8159850d08SRoman Divacky // All Debug.h functionality is a no-op in NDEBUG mode.
8259850d08SRoman Divacky #ifndef NDEBUG
83009b1c42SEd Schouten 
84344a3780SDimitry Andric namespace {
85344a3780SDimitry Andric struct CreateDebug {
call__anon7e7bcd180111::CreateDebug86344a3780SDimitry Andric   static void *call() {
87344a3780SDimitry Andric     return new cl::opt<bool, true>("debug", cl::desc("Enable debug output"),
88344a3780SDimitry Andric                                    cl::Hidden, cl::location(DebugFlag));
89344a3780SDimitry Andric   }
90344a3780SDimitry Andric };
91009b1c42SEd Schouten 
921e7804dbSRoman Divacky // -debug-buffer-size - Buffer the last N characters of debug output
931e7804dbSRoman Divacky //until program termination.
94344a3780SDimitry Andric struct CreateDebugBufferSize {
call__anon7e7bcd180111::CreateDebugBufferSize95344a3780SDimitry Andric   static void *call() {
96344a3780SDimitry Andric     return new cl::opt<unsigned>(
97344a3780SDimitry Andric         "debug-buffer-size",
981e7804dbSRoman Divacky         cl::desc("Buffer the last N characters of debug output "
991e7804dbSRoman Divacky                  "until program termination. "
1001e7804dbSRoman Divacky                  "[default 0 -- immediate print-out]"),
101344a3780SDimitry Andric         cl::Hidden, cl::init(0));
102344a3780SDimitry Andric   }
103344a3780SDimitry Andric };
104344a3780SDimitry Andric } // namespace
105344a3780SDimitry Andric 
106344a3780SDimitry Andric // -debug - Command line option to enable the DEBUG statements in the passes.
107344a3780SDimitry Andric // This flag may only be enabled in debug builds.
108344a3780SDimitry Andric static ManagedStatic<cl::opt<bool, true>, CreateDebug> Debug;
109344a3780SDimitry Andric static ManagedStatic<cl::opt<unsigned>, CreateDebugBufferSize> DebugBufferSize;
1101e7804dbSRoman Divacky 
111d7f7719eSRoman Divacky namespace {
112d7f7719eSRoman Divacky 
113d7f7719eSRoman Divacky struct DebugOnlyOpt {
operator =__anon7e7bcd180211::DebugOnlyOpt114009b1c42SEd Schouten   void operator=(const std::string &Val) const {
11567c32a98SDimitry Andric     if (Val.empty())
11667c32a98SDimitry Andric       return;
11767c32a98SDimitry Andric     DebugFlag = true;
118050e163aSDimitry Andric     SmallVector<StringRef,8> dbgTypes;
119050e163aSDimitry Andric     StringRef(Val).split(dbgTypes, ',', -1, false);
120050e163aSDimitry Andric     for (auto dbgType : dbgTypes)
121cfca06d7SDimitry Andric       CurrentDebugType->push_back(std::string(dbgType));
122009b1c42SEd Schouten   }
123d7f7719eSRoman Divacky };
124344a3780SDimitry Andric } // namespace
125d7f7719eSRoman Divacky 
126d7f7719eSRoman Divacky static DebugOnlyOpt DebugOnlyOptLoc;
127009b1c42SEd Schouten 
128344a3780SDimitry Andric namespace {
129344a3780SDimitry Andric struct CreateDebugOnly {
call__anon7e7bcd180311::CreateDebugOnly130344a3780SDimitry Andric   static void *call() {
131344a3780SDimitry Andric     return new cl::opt<DebugOnlyOpt, true, cl::parser<std::string>>(
132344a3780SDimitry Andric         "debug-only",
133344a3780SDimitry Andric         cl::desc("Enable a specific type of debug output (comma separated list "
134344a3780SDimitry Andric                  "of types)"),
135145449b1SDimitry Andric         cl::Hidden, cl::value_desc("debug string"),
136009b1c42SEd Schouten         cl::location(DebugOnlyOptLoc), cl::ValueRequired);
137344a3780SDimitry Andric   }
138344a3780SDimitry Andric };
139344a3780SDimitry Andric } // namespace
140344a3780SDimitry Andric 
141344a3780SDimitry Andric static ManagedStatic<cl::opt<DebugOnlyOpt, true, cl::parser<std::string>>,
142344a3780SDimitry Andric                      CreateDebugOnly>
143344a3780SDimitry Andric     DebugOnly;
144344a3780SDimitry Andric 
initDebugOptions()145344a3780SDimitry Andric void llvm::initDebugOptions() {
146344a3780SDimitry Andric   *Debug;
147344a3780SDimitry Andric   *DebugBufferSize;
148344a3780SDimitry Andric   *DebugOnly;
149344a3780SDimitry Andric }
150344a3780SDimitry Andric 
1511e7804dbSRoman Divacky // Signal handlers - dump debug output on termination.
debug_user_sig_handler(void * Cookie)152104bd817SRoman Divacky static void debug_user_sig_handler(void *Cookie) {
1531e7804dbSRoman Divacky   // This is a bit sneaky.  Since this is under #ifndef NDEBUG, we
1541e7804dbSRoman Divacky   // know that debug mode is enabled and dbgs() really is a
1551e7804dbSRoman Divacky   // circular_raw_ostream.  If NDEBUG is defined, then dbgs() ==
1561e7804dbSRoman Divacky   // errs() but this will never be invoked.
1575a5ac124SDimitry Andric   llvm::circular_raw_ostream &dbgout =
1585a5ac124SDimitry Andric       static_cast<circular_raw_ostream &>(llvm::dbgs());
1595a5ac124SDimitry Andric   dbgout.flushBufferWithBanner();
16036bf506aSRoman Divacky }
16136bf506aSRoman Divacky 
1621e7804dbSRoman Divacky /// dbgs - Return a circular-buffered debug stream.
dbgs()1631e7804dbSRoman Divacky raw_ostream &llvm::dbgs() {
1641e7804dbSRoman Divacky   // Do one-time initialization in a thread-safe way.
1651e7804dbSRoman Divacky   static struct dbgstream {
1661e7804dbSRoman Divacky     circular_raw_ostream strm;
1671e7804dbSRoman Divacky 
168344a3780SDimitry Andric     dbgstream()
169344a3780SDimitry Andric         : strm(errs(), "*** Debug Log Output ***\n",
170344a3780SDimitry Andric                (!EnableDebugBuffering || !DebugFlag) ? 0 : *DebugBufferSize) {
171344a3780SDimitry Andric       if (EnableDebugBuffering && DebugFlag && *DebugBufferSize != 0)
1721e7804dbSRoman Divacky         // TODO: Add a handler for SIGUSER1-type signals so the user can
1731e7804dbSRoman Divacky         // force a debug dump.
1745ca98fd9SDimitry Andric         sys::AddSignalHandler(&debug_user_sig_handler, nullptr);
1751e7804dbSRoman Divacky       // Otherwise we've already set the debug stream buffer size to
1761e7804dbSRoman Divacky       // zero, disabling buffering so it will output directly to errs().
1771e7804dbSRoman Divacky     }
1781e7804dbSRoman Divacky   } thestrm;
1791e7804dbSRoman Divacky 
1801e7804dbSRoman Divacky   return thestrm.strm;
1811e7804dbSRoman Divacky }
1821e7804dbSRoman Divacky 
183009b1c42SEd Schouten #else
18459850d08SRoman Divacky // Avoid "has no symbols" warning.
18536bf506aSRoman Divacky namespace llvm {
186989df958SRoman Divacky   /// dbgs - Return errs().
dbgs()1871e7804dbSRoman Divacky   raw_ostream &dbgs() {
188989df958SRoman Divacky     return errs();
18936bf506aSRoman Divacky   }
1901e7804dbSRoman Divacky }
initDebugOptions()191344a3780SDimitry Andric void llvm::initDebugOptions() {}
192009b1c42SEd Schouten #endif
1931e7804dbSRoman Divacky 
1941e7804dbSRoman Divacky /// EnableDebugBuffering - Turn on signal handler installation.
1951e7804dbSRoman Divacky ///
1961e7804dbSRoman Divacky bool llvm::EnableDebugBuffering = false;
197