blob: 323d53279b45319ae44ae18572e86001f1f8e62c [file] [log] [blame]
Chris Lattner8fb754a2003-08-01 22:15:41 +00001//===-- Debug.cpp - An easy way to add debug output to your code ----------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner8fb754a2003-08-01 22:15:41 +00009//
Chad Rosier7c427c42012-07-26 20:38:52 +000010// This file implements a handy way of adding debugging information to your
Chris Lattner8fb754a2003-08-01 22:15:41 +000011// 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//
Chad Rosier7c427c42012-07-26 20:38:52 +000021// When compiling without assertions, the -debug-* options and all code in
Chad Rosierbd9f2ba2012-07-27 21:41:59 +000022// DEBUG() statements disappears, so it does not affect the runtime of the code.
Chris Lattner8fb754a2003-08-01 22:15:41 +000023//
24//===----------------------------------------------------------------------===//
25
Bill Wendling9594f3a2006-11-17 09:54:47 +000026#include "llvm/Support/Debug.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Support/CommandLine.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000028#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000029#include "llvm/Support/Signals.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Support/circular_raw_ostream.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000031#include "llvm/Support/raw_ostream.h"
David Greene64506db2009-12-23 16:39:06 +000032
Dmitri Gribenko3e1551c2015-02-19 05:30:16 +000033#undef isCurrentDebugType
34#undef setCurrentDebugType
35
Chris Lattnerc9499b62003-12-14 21:35:53 +000036using namespace llvm;
Chris Lattner8fb754a2003-08-01 22:15:41 +000037
Dmitri Gribenko3e1551c2015-02-19 05:30:16 +000038// Even though LLVM might be built with NDEBUG, define symbols that the code
39// built without NDEBUG can depend on via the llvm/Support/Debug.h header.
40namespace llvm {
41/// Exported boolean set by the -debug option.
42bool DebugFlag = false;
43
44static ManagedStatic<std::vector<std::string>> CurrentDebugType;
45
46/// Return true if the specified string is the debug type
47/// specified on the command line, or if none was specified on the command line
48/// with the -debug-only=X option.
49bool isCurrentDebugType(const char *DebugType) {
50 if (CurrentDebugType->empty())
51 return true;
Yaron Keren90811cb2015-05-27 18:11:07 +000052 // See if DebugType is in list. Note: do not use find() as that forces us to
Dmitri Gribenko3e1551c2015-02-19 05:30:16 +000053 // unnecessarily create an std::string instance.
Yaron Keren90811cb2015-05-27 18:11:07 +000054 for (auto &d : *CurrentDebugType) {
Dmitri Gribenko3e1551c2015-02-19 05:30:16 +000055 if (d == DebugType)
56 return true;
57 }
58 return false;
59}
60
61/// Set the current debug type, as if the -debug-only=X
62/// option were specified. Note that DebugFlag also needs to be set to true for
63/// debug output to be produced.
64///
65void setCurrentDebugType(const char *Type) {
66 CurrentDebugType->clear();
67 CurrentDebugType->push_back(Type);
68}
69
70} // namespace llvm
71
Daniel Dunbar34ee2032009-08-23 08:50:52 +000072// All Debug.h functionality is a no-op in NDEBUG mode.
73#ifndef NDEBUG
Chris Lattner8fb754a2003-08-01 22:15:41 +000074
Chris Lattner7e3cfe32009-08-23 07:05:39 +000075// -debug - Command line option to enable the DEBUG statements in the passes.
76// This flag may only be enabled in debug builds.
77static cl::opt<bool, true>
78Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
79 cl::location(DebugFlag));
Chris Lattner8fb754a2003-08-01 22:15:41 +000080
David Greene64506db2009-12-23 16:39:06 +000081// -debug-buffer-size - Buffer the last N characters of debug output
82//until program termination.
83static cl::opt<unsigned>
84DebugBufferSize("debug-buffer-size",
Erik Verbruggenef40cdd2013-02-20 22:33:46 +000085 cl::desc("Buffer the last N characters of debug output "
David Greene64506db2009-12-23 16:39:06 +000086 "until program termination. "
87 "[default 0 -- immediate print-out]"),
88 cl::Hidden,
89 cl::init(0));
90
Dan Gohmanb29cda92010-04-15 17:08:50 +000091namespace {
92
93struct DebugOnlyOpt {
Chris Lattner7e3cfe32009-08-23 07:05:39 +000094 void operator=(const std::string &Val) const {
Matthias Braun87a3ba62014-11-21 18:06:09 +000095 if (Val.empty())
96 return;
97 DebugFlag = true;
Christof Doumaf617e672016-01-12 10:23:13 +000098 SmallVector<StringRef,8> dbgTypes;
99 StringRef(Val).split(dbgTypes, ',', -1, false);
100 for (auto dbgType : dbgTypes)
101 CurrentDebugType->push_back(dbgType);
Chris Lattner7e3cfe32009-08-23 07:05:39 +0000102 }
Dan Gohmanb29cda92010-04-15 17:08:50 +0000103};
104
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000105}
Dan Gohmanb29cda92010-04-15 17:08:50 +0000106
107static DebugOnlyOpt DebugOnlyOptLoc;
Chris Lattner8fb754a2003-08-01 22:15:41 +0000108
Chris Lattner7e3cfe32009-08-23 07:05:39 +0000109static cl::opt<DebugOnlyOpt, true, cl::parser<std::string> >
Christof Doumaf617e672016-01-12 10:23:13 +0000110DebugOnly("debug-only", cl::desc("Enable a specific type of debug output (comma separated list of types)"),
Matthias Braun87a3ba62014-11-21 18:06:09 +0000111 cl::Hidden, cl::ZeroOrMore, cl::value_desc("debug string"),
Chris Lattner7e3cfe32009-08-23 07:05:39 +0000112 cl::location(DebugOnlyOptLoc), cl::ValueRequired);
David Greene64506db2009-12-23 16:39:06 +0000113// Signal handlers - dump debug output on termination.
Dan Gohmanb452d4e2010-03-24 19:38:02 +0000114static void debug_user_sig_handler(void *Cookie) {
David Greene64506db2009-12-23 16:39:06 +0000115 // This is a bit sneaky. Since this is under #ifndef NDEBUG, we
116 // know that debug mode is enabled and dbgs() really is a
117 // circular_raw_ostream. If NDEBUG is defined, then dbgs() ==
118 // errs() but this will never be invoked.
Rafael Espindola62e6ec02015-04-09 16:59:07 +0000119 llvm::circular_raw_ostream &dbgout =
120 static_cast<circular_raw_ostream &>(llvm::dbgs());
121 dbgout.flushBufferWithBanner();
David Greene64506db2009-12-23 16:39:06 +0000122}
123
David Greene64506db2009-12-23 16:39:06 +0000124/// dbgs - Return a circular-buffered debug stream.
125raw_ostream &llvm::dbgs() {
126 // Do one-time initialization in a thread-safe way.
127 static struct dbgstream {
128 circular_raw_ostream strm;
129
130 dbgstream() :
131 strm(errs(), "*** Debug Log Output ***\n",
132 (!EnableDebugBuffering || !DebugFlag) ? 0 : DebugBufferSize) {
133 if (EnableDebugBuffering && DebugFlag && DebugBufferSize != 0)
134 // TODO: Add a handler for SIGUSER1-type signals so the user can
135 // force a debug dump.
Craig Topper2617dcc2014-04-15 06:32:26 +0000136 sys::AddSignalHandler(&debug_user_sig_handler, nullptr);
David Greene64506db2009-12-23 16:39:06 +0000137 // Otherwise we've already set the debug stream buffer size to
David Greeneb760d0c2009-12-23 23:23:15 +0000138 // zero, disabling buffering so it will output directly to errs().
David Greene64506db2009-12-23 16:39:06 +0000139 }
140 } thestrm;
141
142 return thestrm.strm;
143}
144
Daniel Dunbar34ee2032009-08-23 08:50:52 +0000145#else
146// Avoid "has no symbols" warning.
Chris Lattnerfbaac772009-10-28 15:32:19 +0000147namespace llvm {
David Greene35dca862010-01-20 15:27:19 +0000148 /// dbgs - Return errs().
David Greene64506db2009-12-23 16:39:06 +0000149 raw_ostream &dbgs() {
David Greene35dca862010-01-20 15:27:19 +0000150 return errs();
David Greene64506db2009-12-23 16:39:06 +0000151 }
Chris Lattnerfbaac772009-10-28 15:32:19 +0000152}
David Greene64506db2009-12-23 16:39:06 +0000153
Daniel Dunbar34ee2032009-08-23 08:50:52 +0000154#endif
David Greene64506db2009-12-23 16:39:06 +0000155
156/// EnableDebugBuffering - Turn on signal handler installation.
157///
158bool llvm::EnableDebugBuffering = false;