1038e05a9171ca81c106bc3b1c52c77284cac80e2Chris Lattner//===-- Debug.cpp - An easy way to add debug output to your code ----------===//
2f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7f976c856fcc5055f3fc7d9f070d72c2d027c1d9dMisha Brukman//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
9038e05a9171ca81c106bc3b1c52c77284cac80e2Chris Lattner//
10ad2b592cf95e5db34a48c6e2c9b1fe55405355fbChad Rosier// This file implements a handy way of adding debugging information to your
11038e05a9171ca81c106bc3b1c52c77284cac80e2Chris Lattner// code, without it being enabled all of the time, and without having to add
12038e05a9171ca81c106bc3b1c52c77284cac80e2Chris Lattner// command line options to enable it.
13038e05a9171ca81c106bc3b1c52c77284cac80e2Chris Lattner//
14038e05a9171ca81c106bc3b1c52c77284cac80e2Chris Lattner// In particular, just wrap your code with the DEBUG() macro, and it will be
15038e05a9171ca81c106bc3b1c52c77284cac80e2Chris Lattner// enabled automatically if you specify '-debug' on the command-line.
16038e05a9171ca81c106bc3b1c52c77284cac80e2Chris Lattner// Alternatively, you can also use the SET_DEBUG_TYPE("foo") macro to specify
17038e05a9171ca81c106bc3b1c52c77284cac80e2Chris Lattner// that your debug code belongs to class "foo".  Then, on the command line, you
18038e05a9171ca81c106bc3b1c52c77284cac80e2Chris Lattner// can specify '-debug-only=foo' to enable JUST the debug information for the
19038e05a9171ca81c106bc3b1c52c77284cac80e2Chris Lattner// foo class.
20038e05a9171ca81c106bc3b1c52c77284cac80e2Chris Lattner//
21ad2b592cf95e5db34a48c6e2c9b1fe55405355fbChad Rosier// When compiling without assertions, the -debug-* options and all code in
2221edb397b27d4501bca932cf5fce036f4f3c9473Chad Rosier// DEBUG() statements disappears, so it does not affect the runtime of the code.
23038e05a9171ca81c106bc3b1c52c77284cac80e2Chris Lattner//
24038e05a9171ca81c106bc3b1c52c77284cac80e2Chris Lattner//===----------------------------------------------------------------------===//
25038e05a9171ca81c106bc3b1c52c77284cac80e2Chris Lattner
26ab41c3e6b7520a84560113d84ea83c2d86b1eff7Bill Wendling#include "llvm/Support/Debug.h"
27d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Support/CommandLine.h"
28ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "llvm/Support/ManagedStatic.h"
291f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/Signals.h"
30d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Support/circular_raw_ostream.h"
314c5e43da7792f75567b693105cc53e3f1992ad98Pirama Arumuga Nainar#include "llvm/Support/raw_ostream.h"
32ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
33ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#undef isCurrentDebugType
34ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#undef setCurrentDebugType
35b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene
362cdd21c2e4d855500dfb53f77aa74da53ccf9de6Chris Lattnerusing namespace llvm;
37038e05a9171ca81c106bc3b1c52c77284cac80e2Chris Lattner
38ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines// Even though LLVM might be built with NDEBUG, define symbols that the code
39ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines// built without NDEBUG can depend on via the llvm/Support/Debug.h header.
40ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesnamespace llvm {
41ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// Exported boolean set by the -debug option.
42ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesbool DebugFlag = false;
43ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
44ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesstatic ManagedStatic<std::vector<std::string>> CurrentDebugType;
45ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
46ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// Return true if the specified string is the debug type
47ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// specified on the command line, or if none was specified on the command line
48ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// with the -debug-only=X option.
49ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesbool isCurrentDebugType(const char *DebugType) {
50ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  if (CurrentDebugType->empty())
51ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    return true;
526948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  // See if DebugType is in list. Note: do not use find() as that forces us to
53ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  // unnecessarily create an std::string instance.
546948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar  for (auto &d : *CurrentDebugType) {
55ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    if (d == DebugType)
56ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      return true;
57ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  }
58ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  return false;
59ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines}
60ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
61ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// Set the current debug type, as if the -debug-only=X
62ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// option were specified.  Note that DebugFlag also needs to be set to true for
63ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines/// debug output to be produced.
64ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines///
65ebe69fe11e48d322045d5949c83283927a0d790bStephen Hinesvoid setCurrentDebugType(const char *Type) {
66ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  CurrentDebugType->clear();
67ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines  CurrentDebugType->push_back(Type);
68ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines}
69ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
70ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines} // namespace llvm
71ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
7243ed267db3512823a9698f810be4e64bee227270Daniel Dunbar// All Debug.h functionality is a no-op in NDEBUG mode.
7343ed267db3512823a9698f810be4e64bee227270Daniel Dunbar#ifndef NDEBUG
74038e05a9171ca81c106bc3b1c52c77284cac80e2Chris Lattner
75a36b81d64f90f7cc7c946080d317322c3f4e3a0fChris Lattner// -debug - Command line option to enable the DEBUG statements in the passes.
76a36b81d64f90f7cc7c946080d317322c3f4e3a0fChris Lattner// This flag may only be enabled in debug builds.
77a36b81d64f90f7cc7c946080d317322c3f4e3a0fChris Lattnerstatic cl::opt<bool, true>
78a36b81d64f90f7cc7c946080d317322c3f4e3a0fChris LattnerDebug("debug", cl::desc("Enable debug output"), cl::Hidden,
79a36b81d64f90f7cc7c946080d317322c3f4e3a0fChris Lattner      cl::location(DebugFlag));
80a36b81d64f90f7cc7c946080d317322c3f4e3a0fChris Lattner
81b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene// -debug-buffer-size - Buffer the last N characters of debug output
82b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene//until program termination.
83b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greenestatic cl::opt<unsigned>
84b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David GreeneDebugBufferSize("debug-buffer-size",
853179b179467a07e601be5172213850786a985f74Erik Verbruggen                cl::desc("Buffer the last N characters of debug output "
86b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene                         "until program termination. "
87b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene                         "[default 0 -- immediate print-out]"),
88b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene                cl::Hidden,
89b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene                cl::init(0));
90b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene
91b35798347ea87b8b6d36155b211016a7769f01abDan Gohmannamespace {
92b35798347ea87b8b6d36155b211016a7769f01abDan Gohman
93b35798347ea87b8b6d36155b211016a7769f01abDan Gohmanstruct DebugOnlyOpt {
94a36b81d64f90f7cc7c946080d317322c3f4e3a0fChris Lattner  void operator=(const std::string &Val) const {
95ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    if (Val.empty())
96ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      return;
97ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines    DebugFlag = true;
98de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    SmallVector<StringRef,8> dbgTypes;
99de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    StringRef(Val).split(dbgTypes, ',', -1, false);
100de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar    for (auto dbgType : dbgTypes)
101de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar      CurrentDebugType->push_back(dbgType);
102a36b81d64f90f7cc7c946080d317322c3f4e3a0fChris Lattner  }
103b35798347ea87b8b6d36155b211016a7769f01abDan Gohman};
104b35798347ea87b8b6d36155b211016a7769f01abDan Gohman
105b35798347ea87b8b6d36155b211016a7769f01abDan Gohman}
106b35798347ea87b8b6d36155b211016a7769f01abDan Gohman
107b35798347ea87b8b6d36155b211016a7769f01abDan Gohmanstatic DebugOnlyOpt DebugOnlyOptLoc;
108a36b81d64f90f7cc7c946080d317322c3f4e3a0fChris Lattner
109a36b81d64f90f7cc7c946080d317322c3f4e3a0fChris Lattnerstatic cl::opt<DebugOnlyOpt, true, cl::parser<std::string> >
110de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga NainarDebugOnly("debug-only", cl::desc("Enable a specific type of debug output (comma separated list of types)"),
111ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines          cl::Hidden, cl::ZeroOrMore, cl::value_desc("debug string"),
112a36b81d64f90f7cc7c946080d317322c3f4e3a0fChris Lattner          cl::location(DebugOnlyOptLoc), cl::ValueRequired);
113b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene// Signal handlers - dump debug output on termination.
11416e02097d283159c240996185a8b20f4ea46ccfcDan Gohmanstatic void debug_user_sig_handler(void *Cookie) {
115b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene  // This is a bit sneaky.  Since this is under #ifndef NDEBUG, we
116b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene  // know that debug mode is enabled and dbgs() really is a
117b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene  // circular_raw_ostream.  If NDEBUG is defined, then dbgs() ==
118b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene  // errs() but this will never be invoked.
1190c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  llvm::circular_raw_ostream &dbgout =
1200c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar      static_cast<circular_raw_ostream &>(llvm::dbgs());
1210c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar  dbgout.flushBufferWithBanner();
122b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene}
123b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene
124b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene/// dbgs - Return a circular-buffered debug stream.
125b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greeneraw_ostream &llvm::dbgs() {
126b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene  // Do one-time initialization in a thread-safe way.
127b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene  static struct dbgstream {
128b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene    circular_raw_ostream strm;
129b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene
130b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene    dbgstream() :
131b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene        strm(errs(), "*** Debug Log Output ***\n",
132b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene             (!EnableDebugBuffering || !DebugFlag) ? 0 : DebugBufferSize) {
133b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene      if (EnableDebugBuffering && DebugFlag && DebugBufferSize != 0)
134b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene        // TODO: Add a handler for SIGUSER1-type signals so the user can
135b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene        // force a debug dump.
136dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines        sys::AddSignalHandler(&debug_user_sig_handler, nullptr);
137b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene      // Otherwise we've already set the debug stream buffer size to
138037764227e5a0ebbcb3d634164bc08631f2965d4David Greene      // zero, disabling buffering so it will output directly to errs().
139b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene    }
140b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene  } thestrm;
141b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene
142b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene  return thestrm.strm;
143b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene}
144b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene
14533fbad7024f5c58f201bef4c166bad8dcbf01a3eChris Lattner#else
14643ed267db3512823a9698f810be4e64bee227270Daniel Dunbar// Avoid "has no symbols" warning.
14770197a3a0413476801622484b9c83cf6f2d6f82cChris Lattnernamespace llvm {
1482ef951e6ee6fdb3994fd6cc194abe2c94ff53fbeDavid Greene  /// dbgs - Return errs().
149b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene  raw_ostream &dbgs() {
1502ef951e6ee6fdb3994fd6cc194abe2c94ff53fbeDavid Greene    return errs();
151b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene  }
15270197a3a0413476801622484b9c83cf6f2d6f82cChris Lattner}
153b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene
15433fbad7024f5c58f201bef4c166bad8dcbf01a3eChris Lattner#endif
155b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene
156b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene/// EnableDebugBuffering - Turn on signal handler installation.
157b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greene///
158b5d568cc70506c4cb3aa7abc3370d3ac9411b4a0David Greenebool llvm::EnableDebugBuffering = false;
159