LLVM 22.0.0git
Debug.h
Go to the documentation of this file.
1//===- llvm/Support/Debug.h - Easy way to add debug output ------*- C++ -*-===//
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// LLVM_DEBUG() requires the DEBUG_TYPE macro to be defined. Set it to "foo"
16// specify that your debug code belongs to class "foo". Be careful that you only
17// do this after including Debug.h and not around any #include of headers.
18// Headers should define and undef the macro around the code that needs to use
19// the LLVM_DEBUG() macro. Then, on the command line, you can specify
20// '-debug-only=foo' to enable JUST the debug information for the foo class.
21//
22// When compiling without assertions, the -debug-* options and all code in
23// LLVM_DEBUG() statements disappears, so it does not affect the runtime of the
24// code.
25//
26//===----------------------------------------------------------------------===//
27
28#ifndef LLVM_SUPPORT_DEBUG_H
29#define LLVM_SUPPORT_DEBUG_H
30
32
33namespace llvm {
34
35class raw_ostream;
36
37#ifndef NDEBUG
38
39/// isCurrentDebugType - Return true if the specified string is the debug type
40/// specified on the command line, or if none was specified on the command line
41/// with the -debug-only=X option.
42/// An optional level can be provided to control the verbosity of the output.
43/// If the provided level is not 0 and user specified a level below the provided
44/// level, return false.
45LLVM_ABI bool isCurrentDebugType(const char *Type, int Level = 0);
46
47/// setCurrentDebugType - Set the current debug type, as if the -debug-only=X
48/// option were specified. Note that DebugFlag also needs to be set to true for
49/// debug output to be produced.
50/// The debug type format is "type[:level]", where the level is an optional
51/// integer. If a level is provided, the debug output is enabled only if the
52/// user specified a level at least as high as the provided level.
53/// 0 is a special level that acts as an opt-out for this specific debug type
54/// without affecting the other debug output.
55LLVM_ABI void setCurrentDebugType(const char *Type);
56
57/// setCurrentDebugTypes - Set the current debug type, as if the
58/// -debug-only=X,Y,Z option were specified. Note that DebugFlag
59/// also needs to be set to true for debug output to be produced.
60///
61LLVM_ABI void setCurrentDebugTypes(const char **Types, unsigned Count);
62
63/// DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug
64/// information. If the '-debug' option is specified on the commandline, and if
65/// this is a debug build, then the code specified as the option to the macro
66/// will be executed. Otherwise it will not be. Example:
67///
68/// DEBUG_WITH_TYPE("bitset", dbgs() << "Bitset contains: " << Bitset << "\n");
69///
70/// This will emit the debug information if -debug is present, and -debug-only
71/// is not specified, or is specified as "bitset".
72#define DEBUG_WITH_TYPE(TYPE, ...) \
73 do { \
74 if (::llvm::DebugFlag && ::llvm::isCurrentDebugType(TYPE, 1)) { \
75 __VA_ARGS__; \
76 } \
77 } while (false)
78
79#else
80#define isCurrentDebugType(X) (false)
81#define setCurrentDebugType(X) do { (void)(X); } while (false)
82#define setCurrentDebugTypes(X, N) do { (void)(X); (void)(N); } while (false)
83#define DEBUG_WITH_TYPE(TYPE, ...) \
84 do { \
85 } while (false)
86#endif
87
88/// This boolean is set to true if the '-debug' command line option
89/// is specified. This should probably not be referenced directly, instead, use
90/// the DEBUG macro below.
91///
92LLVM_ABI extern bool DebugFlag;
93
94/// EnableDebugBuffering - This defaults to false. If true, the debug
95/// stream will install signal handlers to dump any buffered debug
96/// output. It allows clients to selectively allow the debug stream
97/// to install signal handlers if they are certain there will be no
98/// conflict.
99///
101
102/// dbgs() - This returns a reference to a raw_ostream for debugging
103/// messages. If debugging is disabled it returns errs(). Use it
104/// like: dbgs() << "foo" << "bar";
105LLVM_ABI raw_ostream &dbgs();
106
107// DEBUG macro - This macro should be used by passes to emit debug information.
108// If the '-debug' option is specified on the commandline, and if this is a
109// debug build, then the code specified as the option to the macro will be
110// executed. Otherwise it will not be. Example:
111//
112// LLVM_DEBUG(dbgs() << "Bitset contains: " << Bitset << "\n");
113//
114#define LLVM_DEBUG(...) DEBUG_WITH_TYPE(DEBUG_TYPE, __VA_ARGS__)
115
116} // end namespace llvm
117
118#endif // LLVM_SUPPORT_DEBUG_H
#define LLVM_ABI
Definition Compiler.h:213
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool EnableDebugBuffering
EnableDebugBuffering - This defaults to false.
Definition Debug.cpp:240
LLVM_ABI bool isCurrentDebugType(const char *Type, int Level=0)
isCurrentDebugType - Return true if the specified string is the debug type specified on the command l...
Definition Debug.cpp:81
LLVM_ABI bool DebugFlag
This boolean is set to true if the '-debug' command line option is specified.
Definition Debug.cpp:68
LLVM_ABI 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:111
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void setCurrentDebugType(const char *Type)
setCurrentDebugType - Set the current debug type, as if the -debug-only=X option were specified.
Definition Debug.cpp:107
FunctionAddr VTableAddr Count
Definition InstrProf.h:139