blob: 824654257acc4fa93399feb15e8ced89ae47a931 [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"
Michael J. Spencer447762d2010-11-29 18:16:10 +000028#include "llvm/Support/Signals.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/Support/circular_raw_ostream.h"
Chris Bienemanf7a63722014-09-03 17:50:14 +000030#include "llvm/Support/ManagedStatic.h"
David Greene64506db2009-12-23 16:39:06 +000031
Chris Lattnerc9499b62003-12-14 21:35:53 +000032using namespace llvm;
Chris Lattner8fb754a2003-08-01 22:15:41 +000033
Daniel Dunbar34ee2032009-08-23 08:50:52 +000034// All Debug.h functionality is a no-op in NDEBUG mode.
35#ifndef NDEBUG
Chris Lattnerc9499b62003-12-14 21:35:53 +000036bool llvm::DebugFlag; // DebugFlag - Exported boolean set by the -debug option
Chris Lattner8fb754a2003-08-01 22:15:41 +000037
Chris Lattner7e3cfe32009-08-23 07:05:39 +000038// -debug - Command line option to enable the DEBUG statements in the passes.
39// This flag may only be enabled in debug builds.
40static cl::opt<bool, true>
41Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
42 cl::location(DebugFlag));
Chris Lattner8fb754a2003-08-01 22:15:41 +000043
David Greene64506db2009-12-23 16:39:06 +000044// -debug-buffer-size - Buffer the last N characters of debug output
45//until program termination.
46static cl::opt<unsigned>
47DebugBufferSize("debug-buffer-size",
Erik Verbruggenef40cdd2013-02-20 22:33:46 +000048 cl::desc("Buffer the last N characters of debug output "
David Greene64506db2009-12-23 16:39:06 +000049 "until program termination. "
50 "[default 0 -- immediate print-out]"),
51 cl::Hidden,
52 cl::init(0));
53
Chris Bienemanf7a63722014-09-03 17:50:14 +000054static ManagedStatic<std::string> CurrentDebugType;
Dan Gohmanb29cda92010-04-15 17:08:50 +000055
56namespace {
57
58struct DebugOnlyOpt {
Chris Lattner7e3cfe32009-08-23 07:05:39 +000059 void operator=(const std::string &Val) const {
60 DebugFlag |= !Val.empty();
Chris Bienemanf7a63722014-09-03 17:50:14 +000061 *CurrentDebugType = Val;
Chris Lattner7e3cfe32009-08-23 07:05:39 +000062 }
Dan Gohmanb29cda92010-04-15 17:08:50 +000063};
64
65}
66
67static DebugOnlyOpt DebugOnlyOptLoc;
Chris Lattner8fb754a2003-08-01 22:15:41 +000068
Chris Lattner7e3cfe32009-08-23 07:05:39 +000069static cl::opt<DebugOnlyOpt, true, cl::parser<std::string> >
70DebugOnly("debug-only", cl::desc("Enable a specific type of debug output"),
71 cl::Hidden, cl::value_desc("debug string"),
72 cl::location(DebugOnlyOptLoc), cl::ValueRequired);
Chris Lattner8fb754a2003-08-01 22:15:41 +000073
David Greene64506db2009-12-23 16:39:06 +000074// Signal handlers - dump debug output on termination.
Dan Gohmanb452d4e2010-03-24 19:38:02 +000075static void debug_user_sig_handler(void *Cookie) {
David Greene64506db2009-12-23 16:39:06 +000076 // This is a bit sneaky. Since this is under #ifndef NDEBUG, we
77 // know that debug mode is enabled and dbgs() really is a
78 // circular_raw_ostream. If NDEBUG is defined, then dbgs() ==
79 // errs() but this will never be invoked.
80 llvm::circular_raw_ostream *dbgout =
81 static_cast<llvm::circular_raw_ostream *>(&llvm::dbgs());
82 dbgout->flushBufferWithBanner();
83}
84
Chris Lattner8fb754a2003-08-01 22:15:41 +000085// isCurrentDebugType - Return true if the specified string is the debug type
86// specified on the command line, or if none was specified on the command line
87// with the -debug-only=X option.
88//
Chris Lattnerc9499b62003-12-14 21:35:53 +000089bool llvm::isCurrentDebugType(const char *DebugType) {
Chris Bienemanf7a63722014-09-03 17:50:14 +000090 return CurrentDebugType->empty() || DebugType == *CurrentDebugType;
Chris Lattner8fb754a2003-08-01 22:15:41 +000091}
Chris Lattnerfbaac772009-10-28 15:32:19 +000092
Chad Rosier7c427c42012-07-26 20:38:52 +000093/// setCurrentDebugType - Set the current debug type, as if the -debug-only=X
Chris Lattnerfbaac772009-10-28 15:32:19 +000094/// option were specified. Note that DebugFlag also needs to be set to true for
95/// debug output to be produced.
96///
Chad Rosier7c427c42012-07-26 20:38:52 +000097void llvm::setCurrentDebugType(const char *Type) {
Chris Bienemanf7a63722014-09-03 17:50:14 +000098 *CurrentDebugType = Type;
Chris Lattnerfbaac772009-10-28 15:32:19 +000099}
100
David Greene64506db2009-12-23 16:39:06 +0000101/// dbgs - Return a circular-buffered debug stream.
102raw_ostream &llvm::dbgs() {
103 // Do one-time initialization in a thread-safe way.
104 static struct dbgstream {
105 circular_raw_ostream strm;
106
107 dbgstream() :
108 strm(errs(), "*** Debug Log Output ***\n",
109 (!EnableDebugBuffering || !DebugFlag) ? 0 : DebugBufferSize) {
110 if (EnableDebugBuffering && DebugFlag && DebugBufferSize != 0)
111 // TODO: Add a handler for SIGUSER1-type signals so the user can
112 // force a debug dump.
Craig Topper2617dcc2014-04-15 06:32:26 +0000113 sys::AddSignalHandler(&debug_user_sig_handler, nullptr);
David Greene64506db2009-12-23 16:39:06 +0000114 // Otherwise we've already set the debug stream buffer size to
David Greeneb760d0c2009-12-23 23:23:15 +0000115 // zero, disabling buffering so it will output directly to errs().
David Greene64506db2009-12-23 16:39:06 +0000116 }
117 } thestrm;
118
119 return thestrm.strm;
120}
121
Daniel Dunbar34ee2032009-08-23 08:50:52 +0000122#else
123// Avoid "has no symbols" warning.
Chris Lattnerfbaac772009-10-28 15:32:19 +0000124namespace llvm {
David Greene35dca862010-01-20 15:27:19 +0000125 /// dbgs - Return errs().
David Greene64506db2009-12-23 16:39:06 +0000126 raw_ostream &dbgs() {
David Greene35dca862010-01-20 15:27:19 +0000127 return errs();
David Greene64506db2009-12-23 16:39:06 +0000128 }
Chris Lattnerfbaac772009-10-28 15:32:19 +0000129}
David Greene64506db2009-12-23 16:39:06 +0000130
Daniel Dunbar34ee2032009-08-23 08:50:52 +0000131#endif
David Greene64506db2009-12-23 16:39:06 +0000132
133/// EnableDebugBuffering - Turn on signal handler installation.
134///
135bool llvm::EnableDebugBuffering = false;