llvm.org GIT mirror llvm / b5d568c
Provide dbgs(), a circular-buffering debug output stream. By default it simply passes output to errs(). If -debug-buffer-size=N is set N > 0, dbgs() buffers its output until program termination and dumps the last N characters sent to it. This is handy when debugging very large inputs. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92002 91177308-0d34-0410-b5e6-96231b3b80d8 David Greene 10 years ago
2 changed file(s) with 73 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
2626 #define LLVM_SUPPORT_DEBUG_H
2727
2828 namespace llvm {
29
30 class raw_ostream;
2931
3032 /// DEBUG_TYPE macro - Files can specify a DEBUG_TYPE as a string, which causes
3133 /// all of their DEBUG statements to be activatable with -debug-only=thatstring.
5759 /// this is a debug build, then the code specified as the option to the macro
5860 /// will be executed. Otherwise it will not be. Example:
5961 ///
60 /// DEBUG_WITH_TYPE("bitset", errs() << "Bitset contains: " << Bitset << "\n");
62 /// DEBUG_WITH_TYPE("bitset", dbgs() << "Bitset contains: " << Bitset << "\n");
6163 ///
6264 /// This will emit the debug information if -debug is present, and -debug-only
6365 /// is not specified, or is specified as "bitset".
7173 #define DEBUG_WITH_TYPE(TYPE, X) do { } while (0)
7274 #endif
7375
76 /// EnableDebugBuffering - This defaults to false. If true, the debug
77 /// stream will install signal handlers to dump any buffered debug
78 /// output. It allows clients to selectively allow the debug stream
79 /// to install signal handlers if they are certain there will be no
80 /// conflict.
81 ///
82 extern bool EnableDebugBuffering;
83
84 /// dbgs() - This returns a reference to a raw_ostream for debugging
85 /// messages. If debugging is disabled it returns dbgs(). Use it
86 /// like: dbgs() << "foo" << "bar";
87 raw_ostream &dbgs();
88
7489 // DEBUG macro - This macro should be used by passes to emit debug information.
7590 // In the '-debug' option is specified on the commandline, and if this is a
7691 // debug build, then the code specified as the option to the macro will be
7792 // executed. Otherwise it will not be. Example:
7893 //
79 // DEBUG(errs() << "Bitset contains: " << Bitset << "\n");
94 // DEBUG(dbgs() << "Bitset contains: " << Bitset << "\n");
8095 //
8196 #define DEBUG(X) DEBUG_WITH_TYPE(DEBUG_TYPE, X)
82
97
8398 } // End llvm namespace
8499
85100 #endif
2424
2525 #include "llvm/Support/CommandLine.h"
2626 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/circular_raw_ostream.h"
28 #include "llvm/System/Signals.h"
29
2730 using namespace llvm;
2831
2932 // All Debug.h functionality is a no-op in NDEBUG mode.
3538 static cl::opt
3639 Debug("debug", cl::desc("Enable debug output"), cl::Hidden,
3740 cl::location(DebugFlag));
41
42 // -debug-buffer-size - Buffer the last N characters of debug output
43 //until program termination.
44 static cl::opt
45 DebugBufferSize("debug-buffer-size",
46 cl::desc("Buffer the last N characters of debug output"
47 "until program termination. "
48 "[default 0 -- immediate print-out]"),
49 cl::Hidden,
50 cl::init(0));
3851
3952 static std::string CurrentDebugType;
4053 static struct DebugOnlyOpt {
4861 DebugOnly("debug-only", cl::desc("Enable a specific type of debug output"),
4962 cl::Hidden, cl::value_desc("debug string"),
5063 cl::location(DebugOnlyOptLoc), cl::ValueRequired);
64
65 // Signal handlers - dump debug output on termination.
66 static void debug_user_sig_handler(void *Cookie)
67 {
68 // This is a bit sneaky. Since this is under #ifndef NDEBUG, we
69 // know that debug mode is enabled and dbgs() really is a
70 // circular_raw_ostream. If NDEBUG is defined, then dbgs() ==
71 // errs() but this will never be invoked.
72 llvm::circular_raw_ostream *dbgout =
73 static_cast(&llvm::dbgs());
74 dbgout->flushBufferWithBanner();
75 }
5176
5277 // isCurrentDebugType - Return true if the specified string is the debug type
5378 // specified on the command line, or if none was specified on the command line
6590 CurrentDebugType = Type;
6691 }
6792
93 /// dbgs - Return a circular-buffered debug stream.
94 raw_ostream &llvm::dbgs() {
95 // Do one-time initialization in a thread-safe way.
96 static struct dbgstream {
97 circular_raw_ostream strm;
98
99 dbgstream() :
100 strm(errs(), "*** Debug Log Output ***\n",
101 (!EnableDebugBuffering || !DebugFlag) ? 0 : DebugBufferSize) {
102 if (EnableDebugBuffering && DebugFlag && DebugBufferSize != 0)
103 // TODO: Add a handler for SIGUSER1-type signals so the user can
104 // force a debug dump.
105 sys::AddSignalHandler(&debug_user_sig_handler, 0);
106 // Otherwise we've already set the debug stream buffer size to
107 // zero, disabling buffering.
108 }
109 } thestrm;
110
111 return thestrm.strm;
112 }
113
68114 #else
69115 // Avoid "has no symbols" warning.
70116 namespace llvm {
71 int Debug_dummy = 0;
117 /// dbgs - Return dbgs().
118 raw_ostream &dbgs() {
119 return dbgs();
120 }
72121 }
122
73123 #endif
124
125 /// EnableDebugBuffering - Turn on signal handler installation.
126 ///
127 bool llvm::EnableDebugBuffering = false;