Debug.cpp revision 201360
1193323Sed//===-- Debug.cpp - An easy way to add debug output to your code ----------===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed// This file implements a handle way of adding debugging information to your
11193323Sed// code, without it being enabled all of the time, and without having to add
12193323Sed// command line options to enable it.
13193323Sed//
14193323Sed// In particular, just wrap your code with the DEBUG() macro, and it will be
15193323Sed// enabled automatically if you specify '-debug' on the command-line.
16193323Sed// Alternatively, you can also use the SET_DEBUG_TYPE("foo") macro to specify
17193323Sed// that your debug code belongs to class "foo".  Then, on the command line, you
18193323Sed// can specify '-debug-only=foo' to enable JUST the debug information for the
19193323Sed// foo class.
20193323Sed//
21193323Sed// When compiling in release mode, the -debug-* options and all code in DEBUG()
22193323Sed// statements disappears, so it does not effect the runtime of the code.
23193323Sed//
24193323Sed//===----------------------------------------------------------------------===//
25193323Sed
26193323Sed#include "llvm/Support/CommandLine.h"
27193323Sed#include "llvm/Support/Debug.h"
28193323Sed#include "llvm/Support/circular_raw_ostream.h"
29193323Sed#include "llvm/System/Signals.h"
30198090Srdivacky
31193323Sedusing namespace llvm;
32193323Sed
33193323Sed// All Debug.h functionality is a no-op in NDEBUG mode.
34193323Sed#ifndef NDEBUG
35193323Sedbool llvm::DebugFlag;  // DebugFlag - Exported boolean set by the -debug option
36193323Sed
37193323Sed// -debug - Command line option to enable the DEBUG statements in the passes.
38193323Sed// This flag may only be enabled in debug builds.
39193323Sedstatic cl::opt<bool, true>
40193323SedDebug("debug", cl::desc("Enable debug output"), cl::Hidden,
41193323Sed      cl::location(DebugFlag));
42193323Sed
43203954Srdivacky// -debug-buffer-size - Buffer the last N characters of debug output
44203954Srdivacky//until program termination.
45203954Srdivackystatic cl::opt<unsigned>
46203954SrdivackyDebugBufferSize("debug-buffer-size",
47203954Srdivacky                cl::desc("Buffer the last N characters of debug output"
48203954Srdivacky                         "until program termination. "
49203954Srdivacky                         "[default 0 -- immediate print-out]"),
50203954Srdivacky                cl::Hidden,
51203954Srdivacky                cl::init(0));
52203954Srdivacky
53203954Srdivackystatic std::string CurrentDebugType;
54203954Srdivackystatic struct DebugOnlyOpt {
55203954Srdivacky  void operator=(const std::string &Val) const {
56203954Srdivacky    DebugFlag |= !Val.empty();
57203954Srdivacky    CurrentDebugType = Val;
58203954Srdivacky  }
59203954Srdivacky} DebugOnlyOptLoc;
60203954Srdivacky
61203954Srdivackystatic cl::opt<DebugOnlyOpt, true, cl::parser<std::string> >
62203954SrdivackyDebugOnly("debug-only", cl::desc("Enable a specific type of debug output"),
63203954Srdivacky          cl::Hidden, cl::value_desc("debug string"),
64205218Srdivacky          cl::location(DebugOnlyOptLoc), cl::ValueRequired);
65205218Srdivacky
66205218Srdivacky// Signal handlers - dump debug output on termination.
67205218Srdivackystatic void debug_user_sig_handler(void *Cookie)
68205218Srdivacky{
69203954Srdivacky  // This is a bit sneaky.  Since this is under #ifndef NDEBUG, we
70203954Srdivacky  // know that debug mode is enabled and dbgs() really is a
71203954Srdivacky  // circular_raw_ostream.  If NDEBUG is defined, then dbgs() ==
72203954Srdivacky  // errs() but this will never be invoked.
73203954Srdivacky  llvm::circular_raw_ostream *dbgout =
74203954Srdivacky    static_cast<llvm::circular_raw_ostream *>(&llvm::dbgs());
75193323Sed  dbgout->flushBufferWithBanner();
76193323Sed}
77203954Srdivacky
78193323Sed// isCurrentDebugType - Return true if the specified string is the debug type
79203954Srdivacky// specified on the command line, or if none was specified on the command line
80203954Srdivacky// with the -debug-only=X option.
81203954Srdivacky//
82203954Srdivackybool llvm::isCurrentDebugType(const char *DebugType) {
83203954Srdivacky  return CurrentDebugType.empty() || DebugType == CurrentDebugType;
84203954Srdivacky}
85203954Srdivacky
86193323Sed/// SetCurrentDebugType - Set the current debug type, as if the -debug-only=X
87193323Sed/// option were specified.  Note that DebugFlag also needs to be set to true for
88193323Sed/// debug output to be produced.
89193323Sed///
90193323Sedvoid llvm::SetCurrentDebugType(const char *Type) {
91193323Sed  CurrentDebugType = Type;
92193323Sed}
93193323Sed
94193323Sed/// dbgs - Return a circular-buffered debug stream.
95193323Sedraw_ostream &llvm::dbgs() {
96193323Sed  // Do one-time initialization in a thread-safe way.
97203954Srdivacky  static struct dbgstream {
98203954Srdivacky    circular_raw_ostream strm;
99203954Srdivacky
100193323Sed    dbgstream() :
101193323Sed        strm(errs(), "*** Debug Log Output ***\n",
102193323Sed             (!EnableDebugBuffering || !DebugFlag) ? 0 : DebugBufferSize) {
103193323Sed      if (EnableDebugBuffering && DebugFlag && DebugBufferSize != 0)
104193323Sed        // TODO: Add a handler for SIGUSER1-type signals so the user can
105193323Sed        // force a debug dump.
106193323Sed        sys::AddSignalHandler(&debug_user_sig_handler, 0);
107193323Sed      // Otherwise we've already set the debug stream buffer size to
108199481Srdivacky      // zero, disabling buffering so it will output directly to errs().
109199481Srdivacky    }
110199481Srdivacky  } thestrm;
111199481Srdivacky
112199481Srdivacky  return thestrm.strm;
113193323Sed}
114193323Sed
115193323Sed#else
116198090Srdivacky// Avoid "has no symbols" warning.
117193323Sednamespace llvm {
118198090Srdivacky  /// dbgs - Return dbgs().
119193323Sed  raw_ostream &dbgs() {
120193323Sed    return dbgs();
121193323Sed  }
122193323Sed}
123193323Sed
124193323Sed#endif
125193323Sed
126/// EnableDebugBuffering - Turn on signal handler installation.
127///
128bool llvm::EnableDebugBuffering = false;
129