Debug.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //===-- Debug.cpp - An easy way to add debug output to your code ----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements a handy way of adding debugging information to your
  10. // code, without it being enabled all of the time, and without having to add
  11. // command line options to enable it.
  12. //
  13. // In particular, just wrap your code with the LLVM_DEBUG() macro, and it will
  14. // be enabled automatically if you specify '-debug' on the command-line.
  15. // Alternatively, you can also use the SET_DEBUG_TYPE("foo") macro to specify
  16. // that your debug code belongs to class "foo". Then, on the command line, you
  17. // can specify '-debug-only=foo' to enable JUST the debug information for the
  18. // foo class.
  19. //
  20. // When compiling without assertions, the -debug-* options and all code in
  21. // LLVM_DEBUG() statements disappears, so it does not affect the runtime of the
  22. // code.
  23. //
  24. //===----------------------------------------------------------------------===//
  25. #include "llvm/Support/Debug.h"
  26. #include "llvm/Support/CommandLine.h"
  27. #include "llvm/Support/ManagedStatic.h"
  28. #include "llvm/Support/Signals.h"
  29. #include "llvm/Support/circular_raw_ostream.h"
  30. #include "llvm/Support/raw_ostream.h"
  31. #include "DebugOptions.h"
  32. #undef isCurrentDebugType
  33. #undef setCurrentDebugType
  34. #undef setCurrentDebugTypes
  35. using namespace llvm;
  36. // Even though LLVM might be built with NDEBUG, define symbols that the code
  37. // built without NDEBUG can depend on via the llvm/Support/Debug.h header.
  38. namespace llvm {
  39. /// Exported boolean set by the -debug option.
  40. bool DebugFlag = false;
  41. static ManagedStatic<std::vector<std::string>> CurrentDebugType;
  42. /// Return true if the specified string is the debug type
  43. /// specified on the command line, or if none was specified on the command line
  44. /// with the -debug-only=X option.
  45. bool isCurrentDebugType(const char *DebugType) {
  46. if (CurrentDebugType->empty())
  47. return true;
  48. // See if DebugType is in list. Note: do not use find() as that forces us to
  49. // unnecessarily create an std::string instance.
  50. for (auto &d : *CurrentDebugType) {
  51. if (d == DebugType)
  52. return true;
  53. }
  54. return false;
  55. }
  56. /// Set the current debug type, as if the -debug-only=X
  57. /// option were specified. Note that DebugFlag also needs to be set to true for
  58. /// debug output to be produced.
  59. ///
  60. void setCurrentDebugTypes(const char **Types, unsigned Count);
  61. void setCurrentDebugType(const char *Type) {
  62. setCurrentDebugTypes(&Type, 1);
  63. }
  64. void setCurrentDebugTypes(const char **Types, unsigned Count) {
  65. CurrentDebugType->clear();
  66. for (size_t T = 0; T < Count; ++T)
  67. CurrentDebugType->push_back(Types[T]);
  68. }
  69. } // namespace llvm
  70. // All Debug.h functionality is a no-op in NDEBUG mode.
  71. #ifndef NDEBUG
  72. namespace {
  73. struct CreateDebug {
  74. static void *call() {
  75. return new cl::opt<bool, true>("debug", cl::desc("Enable debug output"),
  76. cl::Hidden, cl::location(DebugFlag));
  77. }
  78. };
  79. // -debug-buffer-size - Buffer the last N characters of debug output
  80. //until program termination.
  81. struct CreateDebugBufferSize {
  82. static void *call() {
  83. return new cl::opt<unsigned>(
  84. "debug-buffer-size",
  85. cl::desc("Buffer the last N characters of debug output "
  86. "until program termination. "
  87. "[default 0 -- immediate print-out]"),
  88. cl::Hidden, cl::init(0));
  89. }
  90. };
  91. } // namespace
  92. // -debug - Command line option to enable the DEBUG statements in the passes.
  93. // This flag may only be enabled in debug builds.
  94. static ManagedStatic<cl::opt<bool, true>, CreateDebug> Debug;
  95. static ManagedStatic<cl::opt<unsigned>, CreateDebugBufferSize> DebugBufferSize;
  96. namespace {
  97. struct DebugOnlyOpt {
  98. void operator=(const std::string &Val) const {
  99. if (Val.empty())
  100. return;
  101. DebugFlag = true;
  102. SmallVector<StringRef,8> dbgTypes;
  103. StringRef(Val).split(dbgTypes, ',', -1, false);
  104. for (auto dbgType : dbgTypes)
  105. CurrentDebugType->push_back(std::string(dbgType));
  106. }
  107. };
  108. } // namespace
  109. static DebugOnlyOpt DebugOnlyOptLoc;
  110. namespace {
  111. struct CreateDebugOnly {
  112. static void *call() {
  113. return new cl::opt<DebugOnlyOpt, true, cl::parser<std::string>>(
  114. "debug-only",
  115. cl::desc("Enable a specific type of debug output (comma separated list "
  116. "of types)"),
  117. cl::Hidden, cl::ZeroOrMore, cl::value_desc("debug string"),
  118. cl::location(DebugOnlyOptLoc), cl::ValueRequired);
  119. }
  120. };
  121. } // namespace
  122. static ManagedStatic<cl::opt<DebugOnlyOpt, true, cl::parser<std::string>>,
  123. CreateDebugOnly>
  124. DebugOnly;
  125. void llvm::initDebugOptions() {
  126. *Debug;
  127. *DebugBufferSize;
  128. *DebugOnly;
  129. }
  130. // Signal handlers - dump debug output on termination.
  131. static void debug_user_sig_handler(void *Cookie) {
  132. // This is a bit sneaky. Since this is under #ifndef NDEBUG, we
  133. // know that debug mode is enabled and dbgs() really is a
  134. // circular_raw_ostream. If NDEBUG is defined, then dbgs() ==
  135. // errs() but this will never be invoked.
  136. llvm::circular_raw_ostream &dbgout =
  137. static_cast<circular_raw_ostream &>(llvm::dbgs());
  138. dbgout.flushBufferWithBanner();
  139. }
  140. /// dbgs - Return a circular-buffered debug stream.
  141. raw_ostream &llvm::dbgs() {
  142. // Do one-time initialization in a thread-safe way.
  143. static struct dbgstream {
  144. circular_raw_ostream strm;
  145. dbgstream()
  146. : strm(errs(), "*** Debug Log Output ***\n",
  147. (!EnableDebugBuffering || !DebugFlag) ? 0 : *DebugBufferSize) {
  148. if (EnableDebugBuffering && DebugFlag && *DebugBufferSize != 0)
  149. // TODO: Add a handler for SIGUSER1-type signals so the user can
  150. // force a debug dump.
  151. sys::AddSignalHandler(&debug_user_sig_handler, nullptr);
  152. // Otherwise we've already set the debug stream buffer size to
  153. // zero, disabling buffering so it will output directly to errs().
  154. }
  155. } thestrm;
  156. return thestrm.strm;
  157. }
  158. #else
  159. // Avoid "has no symbols" warning.
  160. namespace llvm {
  161. /// dbgs - Return errs().
  162. raw_ostream &dbgs() {
  163. return errs();
  164. }
  165. }
  166. void llvm::initDebugOptions() {}
  167. #endif
  168. /// EnableDebugBuffering - Turn on signal handler installation.
  169. ///
  170. bool llvm::EnableDebugBuffering = false;