Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
Debug.cpp
Go to the documentation of this file.
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
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/ManagedStatic.h"
29#include "llvm/Support/Signals.h"
30#include "llvm/Support/circular_raw_ostream.h"
31#include "llvm/Support/raw_ostream.h"
32
33#include "DebugOptions.h"
34
35#undef isCurrentDebugType
36#undef setCurrentDebugType
37#undef setCurrentDebugTypes
38
39using namespacellvm;
40
41// Even though LLVM might be built with NDEBUG, define symbols that the code
42// built without NDEBUG can depend on via the llvm/Support/Debug.h header.
43namespacellvm {
44/// Exported boolean set by the -debug option.
45boolDebugFlag =false;
46
47staticManagedStatic<std::vector<std::string>>CurrentDebugType;
48
49/// Return true if the specified string is the debug type
50/// specified on the command line, or if none was specified on the command line
51/// with the -debug-only=X option.
52boolisCurrentDebugType(constchar *DebugType) {
53if (CurrentDebugType->empty())
54returntrue;
55// See if DebugType is in list. Note: do not use find() as that forces us to
56// unnecessarily create an std::string instance.
57for (auto &d : *CurrentDebugType) {
58if (d == DebugType)
59returntrue;
60 }
61returnfalse;
62}
63
64/// Set the current debug type, as if the -debug-only=X
65/// option were specified. Note that DebugFlag also needs to be set to true for
66/// debug output to be produced.
67///
68voidsetCurrentDebugTypes(constchar **Types,unsigned Count);
69
70voidsetCurrentDebugType(constchar *Type) {
71setCurrentDebugTypes(&Type, 1);
72}
73
74voidsetCurrentDebugTypes(constchar **Types,unsigned Count) {
75CurrentDebugType->clear();
76for (size_tT = 0;T < Count; ++T)
77CurrentDebugType->push_back(Types[T]);
78}
79}// namespace llvm
80
81// All Debug.h functionality is a no-op in NDEBUG mode.
82#ifndef NDEBUG
83
84namespace{
85structCreateDebug {
86staticvoid *call() {
87returnnewcl::opt<bool, true>("debug",cl::desc("Enable debug output"),
88cl::Hidden,cl::location(DebugFlag));
89 }
90};
91
92// -debug-buffer-size - Buffer the last N characters of debug output
93//until program termination.
94structCreateDebugBufferSize {
95staticvoid *call() {
96returnnewcl::opt<unsigned>(
97"debug-buffer-size",
98cl::desc("Buffer the last N characters of debug output "
99"until program termination. "
100"[default 0 -- immediate print-out]"),
101cl::Hidden,cl::init(0));
102 }
103};
104}// namespace
105
106// -debug - Command line option to enable the DEBUG statements in the passes.
107// This flag may only be enabled in debug builds.
108staticManagedStatic<cl::opt<bool, true>, CreateDebug>Debug;
109staticManagedStatic<cl::opt<unsigned>, CreateDebugBufferSize>DebugBufferSize;
110
111namespace{
112
113structDebugOnlyOpt {
114void operator=(const std::string &Val) const{
115if (Val.empty())
116return;
117DebugFlag =true;
118SmallVector<StringRef,8> dbgTypes;
119StringRef(Val).split(dbgTypes,',', -1,false);
120for (auto dbgType : dbgTypes)
121CurrentDebugType->push_back(std::string(dbgType));
122 }
123};
124}// namespace
125
126static DebugOnlyOptDebugOnlyOptLoc;
127
128namespace{
129structCreateDebugOnly {
130staticvoid *call() {
131returnnewcl::opt<DebugOnlyOpt, true, cl::parser<std::string>>(
132"debug-only",
133cl::desc("Enable a specific type of debug output (comma separated list "
134"of types)"),
135cl::Hidden,cl::value_desc("debug string"),
136cl::location(DebugOnlyOptLoc),cl::ValueRequired);
137 }
138};
139}// namespace
140
141staticManagedStatic<cl::opt<DebugOnlyOpt, true, cl::parser<std::string>>,
142 CreateDebugOnly>
143DebugOnly;
144
145voidllvm::initDebugOptions() {
146 *Debug;
147 *DebugBufferSize;
148 *DebugOnly;
149}
150
151// Signal handlers - dump debug output on termination.
152staticvoiddebug_user_sig_handler(void *Cookie) {
153// This is a bit sneaky. Since this is under #ifndef NDEBUG, we
154// know that debug mode is enabled and dbgs() really is a
155// circular_raw_ostream. If NDEBUG is defined, then dbgs() ==
156// errs() but this will never be invoked.
157llvm::circular_raw_ostream &dbgout =
158static_cast<circular_raw_ostream &>(llvm::dbgs());
159 dbgout.flushBufferWithBanner();
160}
161
162/// dbgs - Return a circular-buffered debug stream.
163raw_ostream &llvm::dbgs() {
164// Do one-time initialization in a thread-safe way.
165staticstructdbgstream {
166circular_raw_ostream strm;
167
168 dbgstream()
169 : strm(errs(),"*** Debug Log Output ***\n",
170 (!EnableDebugBuffering || !DebugFlag) ? 0 : *DebugBufferSize) {
171if (EnableDebugBuffering &&DebugFlag && *DebugBufferSize != 0)
172// TODO: Add a handler for SIGUSER1-type signals so the user can
173// force a debug dump.
174sys::AddSignalHandler(&debug_user_sig_handler,nullptr);
175// Otherwise we've already set the debug stream buffer size to
176// zero, disabling buffering so it will output directly to errs().
177 }
178 } thestrm;
179
180return thestrm.strm;
181}
182
183#else
184// Avoid "has no symbols" warning.
185namespacellvm {
186 /// dbgs - Return errs().
187raw_ostream &dbgs() {
188returnerrs();
189 }
190}
191voidllvm::initDebugOptions() {}
192#endif
193
194/// EnableDebugBuffering - Turn on signal handler installation.
195///
196boolllvm::EnableDebugBuffering =false;
CommandLine.h
DebugOptions.h
Debug
static ManagedStatic< cl::opt< bool, true >, CreateDebug > Debug
Definition:Debug.cpp:108
debug_user_sig_handler
static void debug_user_sig_handler(void *Cookie)
Definition:Debug.cpp:152
DebugBufferSize
static ManagedStatic< cl::opt< unsigned >, CreateDebugBufferSize > DebugBufferSize
Definition:Debug.cpp:109
DebugOnly
static ManagedStatic< cl::opt< DebugOnlyOpt, true, cl::parser< std::string > >, CreateDebugOnly > DebugOnly
Definition:Debug.cpp:143
DebugOnlyOptLoc
static DebugOnlyOpt DebugOnlyOptLoc
Definition:Debug.cpp:126
Debug.h
ManagedStatic.h
Signals.h
circular_raw_ostream.h
T
llvm::ManagedStatic
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
Definition:ManagedStatic.h:83
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition:SmallVector.h:1196
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::StringRef::split
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition:StringRef.h:700
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition:Type.h:45
llvm::circular_raw_ostream
circular_raw_ostream - A raw_ostream which can save its data to a circular buffer,...
Definition:circular_raw_ostream.h:24
llvm::circular_raw_ostream::flushBufferWithBanner
void flushBufferWithBanner()
flushBufferWithBanner - Force output of the buffer along with a small header.
Definition:circular_raw_ostream.cpp:38
llvm::cl::opt
Definition:CommandLine.h:1423
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition:raw_ostream.h:52
llvm::cl::ValueRequired
@ ValueRequired
Definition:CommandLine.h:131
llvm::cl::Hidden
@ Hidden
Definition:CommandLine.h:137
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition:CommandLine.h:443
llvm::cl::location
LocationClass< Ty > location(Ty &L)
Definition:CommandLine.h:463
llvm::sys::AddSignalHandler
void AddSignalHandler(SignalHandlerCallback FnPtr, void *Cookie)
Add a function to be called when an abort/kill signal is delivered to the process.
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::EnableDebugBuffering
bool EnableDebugBuffering
EnableDebugBuffering - This defaults to false.
Definition:Debug.cpp:196
llvm::initDebugOptions
void initDebugOptions()
Definition:Debug.cpp:145
llvm::DebugFlag
bool DebugFlag
This boolean is set to true if the '-debug' command line option is specified.
Definition:Debug.cpp:45
llvm::setCurrentDebugTypes
void setCurrentDebugTypes(const char **Types, unsigned Count)
setCurrentDebugTypes - Set the current debug type, as if the -debug-only=X,Y,Z option were specified.
Definition:Debug.cpp:74
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition:Debug.cpp:163
llvm::setCurrentDebugType
void setCurrentDebugType(const char *Type)
setCurrentDebugType - Set the current debug type, as if the -debug-only=X option were specified.
Definition:Debug.cpp:70
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition:raw_ostream.cpp:907
llvm::isCurrentDebugType
bool isCurrentDebugType(const char *Type)
isCurrentDebugType - Return true if the specified string is the debug type specified on the command l...
Definition:Debug.cpp:52
llvm::CurrentDebugType
static ManagedStatic< std::vector< std::string > > CurrentDebugType
Definition:Debug.cpp:47
raw_ostream.h
llvm::cl::desc
Definition:CommandLine.h:409
llvm::cl::value_desc
Definition:CommandLine.h:418

Generated on Thu Jul 17 2025 12:38:06 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp