LLVM 19.0.0git
DIContext.h
Go to the documentation of this file.
1//===- DIContext.h ----------------------------------------------*- 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 defines DIContext, an abstract data structure that holds
10// debug information data.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_DEBUGINFO_DICONTEXT_H
15#define LLVM_DEBUGINFO_DICONTEXT_H
16
21#include <cassert>
22#include <cstdint>
23#include <memory>
24#include <optional>
25#include <string>
26#include <tuple>
27#include <utility>
28
29namespace llvm {
30
31/// A format-neutral container for source line information.
32struct DILineInfo {
33 // DILineInfo contains "<invalid>" for function/filename it cannot fetch.
34 static constexpr const char *const BadString = "<invalid>";
35 // Use "??" instead of "<invalid>" to make our output closer to addr2line.
36 static constexpr const char *const Addr2LineBadString = "??";
37 std::string FileName;
38 std::string FunctionName;
39 std::string StartFileName;
40 // Full source corresponding to `FileName`
41 std::optional<StringRef> Source;
42 // Source code for this particular line
43 // (in case if `Source` is not available)
44 std::optional<StringRef> LineSource;
48 std::optional<uint64_t> StartAddress;
49
50 // DWARF-specific.
52
55 }
56
57 bool operator==(const DILineInfo &RHS) const {
58 return Line == RHS.Line && Column == RHS.Column &&
59 FileName == RHS.FileName && FunctionName == RHS.FunctionName &&
60 StartFileName == RHS.StartFileName && StartLine == RHS.StartLine &&
61 Discriminator == RHS.Discriminator;
62 }
63
64 bool operator!=(const DILineInfo &RHS) const { return !(*this == RHS); }
65
66 bool operator<(const DILineInfo &RHS) const {
67 return std::tie(FileName, FunctionName, StartFileName, Line, Column,
69 std::tie(RHS.FileName, RHS.FunctionName, RHS.StartFileName, RHS.Line,
70 RHS.Column, RHS.StartLine, RHS.Discriminator);
71 }
72
73 explicit operator bool() const { return *this != DILineInfo(); }
74
76 OS << "Line info: ";
77 if (FileName != BadString)
78 OS << "file '" << FileName << "', ";
80 OS << "function '" << FunctionName << "', ";
81 OS << "line " << Line << ", ";
82 OS << "column " << Column << ", ";
84 OS << "start file '" << StartFileName << "', ";
85 OS << "start line " << StartLine << '\n';
86 }
87};
88
90
91/// A format-neutral container for inlined code description.
94
95public:
96 DIInliningInfo() = default;
97
98 /// Returns the frame at `Index`. Frames are stored in bottom-up
99 /// (leaf-to-root) order with increasing index.
100 const DILineInfo &getFrame(unsigned Index) const {
101 assert(Index < Frames.size());
102 return Frames[Index];
103 }
104
106 assert(Index < Frames.size());
107 return &Frames[Index];
108 }
109
110 uint32_t getNumberOfFrames() const { return Frames.size(); }
111
112 void addFrame(const DILineInfo &Frame) { Frames.push_back(Frame); }
113
114 void resize(unsigned i) { Frames.resize(i); }
115};
116
117/// Container for description of a global variable.
118struct DIGlobal {
119 std::string Name;
122 std::string DeclFile;
124
125 DIGlobal() : Name(DILineInfo::BadString) {}
126};
127
128struct DILocal {
129 std::string FunctionName;
130 std::string Name;
131 std::string DeclFile;
133 std::optional<int64_t> FrameOffset;
134 std::optional<uint64_t> Size;
135 std::optional<uint64_t> TagOffset;
136};
137
138/// A DINameKind is passed to name search methods to specify a
139/// preference regarding the type of name resolution the caller wants.
141
142/// Controls which fields of DILineInfo container should be filled
143/// with data.
145 enum class FileLineInfoKind {
146 None,
147 // RawValue is whatever the compiler stored in the filename table. Could be
148 // a full path, could be something else.
149 RawValue,
151 // Relative to the compilation directory.
154 };
156
159
160 DILineInfoSpecifier(FileLineInfoKind FLIKind = FileLineInfoKind::RawValue,
161 FunctionNameKind FNKind = FunctionNameKind::None)
163
164 inline bool operator==(const DILineInfoSpecifier &RHS) const {
165 return FLIKind == RHS.FLIKind && FNKind == RHS.FNKind;
166 }
167};
168
169/// This is just a helper to programmatically construct DIDumpType.
171#define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME, OPTION) \
172 DIDT_ID_##ENUM_NAME,
173#include "llvm/BinaryFormat/Dwarf.def"
174#undef HANDLE_DWARF_SECTION
178static_assert(DIDT_ID_Count <= 32, "section types overflow storage");
179
180/// Selects which debug sections get dumped.
181enum DIDumpType : unsigned {
183 DIDT_All = ~0U,
184#define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME, OPTION) \
185 DIDT_##ENUM_NAME = 1U << DIDT_ID_##ENUM_NAME,
186#include "llvm/BinaryFormat/Dwarf.def"
187#undef HANDLE_DWARF_SECTION
189};
190
191/// Container for dump options that control which debug information will be
192/// dumped.
194 unsigned DumpType = DIDT_All;
195 unsigned ChildRecurseDepth = -1U;
196 unsigned ParentRecurseDepth = -1U;
197 uint16_t Version = 0; // DWARF version to assume when extracting.
198 uint8_t AddrSize = 4; // Address byte size to assume when extracting.
199 bool ShowAddresses = true;
200 bool ShowChildren = false;
201 bool ShowParents = false;
202 bool ShowForm = false;
203 bool SummarizeTypes = false;
204 bool Verbose = false;
205 bool DisplayRawContents = false;
206 bool IsEH = false;
207 bool DumpNonSkeleton = false;
209 std::function<llvm::StringRef(uint64_t DwarfRegNum, bool IsEH)>
211
212 /// Return default option set for printing a single DIE without children.
214 DIDumpOptions Opts;
215 Opts.ChildRecurseDepth = 0;
216 Opts.ParentRecurseDepth = 0;
217 return Opts;
218 }
219
220 /// Return the options with RecurseDepth set to 0 unless explicitly required.
222 DIDumpOptions Opts = *this;
223 if (ChildRecurseDepth == -1U && !ShowChildren)
224 Opts.ChildRecurseDepth = 0;
225 if (ParentRecurseDepth == -1U && !ShowParents)
226 Opts.ParentRecurseDepth = 0;
227 return Opts;
228 }
229
230 std::function<void(Error)> RecoverableErrorHandler =
233};
234
236public:
238
239 DIContext(DIContextKind K) : Kind(K) {}
240 virtual ~DIContext() = default;
241
242 DIContextKind getKind() const { return Kind; }
243
244 virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts) = 0;
245
246 virtual bool verify(raw_ostream &OS, DIDumpOptions DumpOpts = {}) {
247 // No verifier? Just say things went well.
248 return true;
249 }
250
253 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
254 virtual DILineInfo
258 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
261 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
262
263 virtual std::vector<DILocal>
265
266private:
267 const DIContextKind Kind;
268};
269
270/// An inferface for inquiring the load address of a loaded object file
271/// to be used by the DIContext implementations when applying relocations
272/// on the fly.
274protected:
275 LoadedObjectInfo() = default;
277
278public:
279 virtual ~LoadedObjectInfo() = default;
280
281 /// Obtain the Load Address of a section by SectionRef.
282 ///
283 /// Calculate the address of the given section.
284 /// The section need not be present in the local address space. The addresses
285 /// need to be consistent with the addresses used to query the DIContext and
286 /// the output of this function should be deterministic, i.e. repeated calls
287 /// with the same Sec should give the same address.
289 return 0;
290 }
291
292 /// If conveniently available, return the content of the given Section.
293 ///
294 /// When the section is available in the local address space, in relocated
295 /// (loaded) form, e.g. because it was relocated by a JIT for execution, this
296 /// function should provide the contents of said section in `Data`. If the
297 /// loaded section is not available, or the cost of retrieving it would be
298 /// prohibitive, this function should return false. In that case, relocations
299 /// will be read from the local (unrelocated) object file and applied on the
300 /// fly. Note that this method is used purely for optimzation purposes in the
301 /// common case of JITting in the local address space, so returning false
302 /// should always be correct.
304 StringRef &Data) const {
305 return false;
306 }
307
308 // FIXME: This is untested and unused anywhere in the LLVM project, it's
309 // used/needed by Julia (an external project). It should have some coverage
310 // (at least tests, but ideally example functionality).
311 /// Obtain a copy of this LoadedObjectInfo.
312 virtual std::unique_ptr<LoadedObjectInfo> clone() const = 0;
313};
314
315template <typename Derived, typename Base = LoadedObjectInfo>
317protected:
320
321public:
322 template <typename... Ts>
323 LoadedObjectInfoHelper(Ts &&...Args) : Base(std::forward<Ts>(Args)...) {}
324
325 std::unique_ptr<llvm::LoadedObjectInfo> clone() const override {
326 return std::make_unique<Derived>(static_cast<const Derived &>(*this));
327 }
328};
329
330} // end namespace llvm
331
332#endif // LLVM_DEBUGINFO_DICONTEXT_H
uint64_t Size
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the SmallVector class.
Value * RHS
virtual std::vector< DILocal > getLocalsForAddress(object::SectionedAddress Address)=0
virtual DIInliningInfo getInliningInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier())=0
virtual DILineInfo getLineInfoForDataAddress(object::SectionedAddress Address)=0
virtual DILineInfo getLineInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier())=0
virtual DILineInfoTable getLineInfoForAddressRange(object::SectionedAddress Address, uint64_t Size, DILineInfoSpecifier Specifier=DILineInfoSpecifier())=0
virtual bool verify(raw_ostream &OS, DIDumpOptions DumpOpts={})
Definition: DIContext.h:246
virtual ~DIContext()=default
virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts)=0
DIContextKind getKind() const
Definition: DIContext.h:242
DIContext(DIContextKind K)
Definition: DIContext.h:239
A format-neutral container for inlined code description.
Definition: DIContext.h:92
DIInliningInfo()=default
const DILineInfo & getFrame(unsigned Index) const
Returns the frame at Index.
Definition: DIContext.h:100
uint32_t getNumberOfFrames() const
Definition: DIContext.h:110
void addFrame(const DILineInfo &Frame)
Definition: DIContext.h:112
DILineInfo * getMutableFrame(unsigned Index)
Definition: DIContext.h:105
void resize(unsigned i)
Definition: DIContext.h:114
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
An inferface for inquiring the load address of a loaded object file to be used by the DIContext imple...
Definition: DIContext.h:273
virtual std::unique_ptr< LoadedObjectInfo > clone() const =0
Obtain a copy of this LoadedObjectInfo.
LoadedObjectInfo(const LoadedObjectInfo &)=default
virtual bool getLoadedSectionContents(const object::SectionRef &Sec, StringRef &Data) const
If conveniently available, return the content of the given Section.
Definition: DIContext.h:303
virtual ~LoadedObjectInfo()=default
virtual uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const
Obtain the Load Address of a section by SectionRef.
Definition: DIContext.h:288
size_t size() const
Definition: SmallVector.h:91
void resize(size_type N)
Definition: SmallVector.h:651
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
static void defaultWarningHandler(Error Warning)
Implement default handling for Warning.
Definition: WithColor.cpp:164
static void defaultErrorHandler(Error Err)
Implement default handling for Error.
Definition: WithColor.cpp:158
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ None
Not a recurrence.
DINameKind
A DINameKind is passed to name search methods to specify a preference regarding the type of name reso...
Definition: DIContext.h:140
DIDumpTypeCounter
This is just a helper to programmatically construct DIDumpType.
Definition: DIContext.h:170
@ DIDT_ID_Count
Definition: DIContext.h:176
@ DIDT_ID_UUID
Definition: DIContext.h:175
DIDumpType
Selects which debug sections get dumped.
Definition: DIContext.h:181
@ DIDT_All
Definition: DIContext.h:183
@ DIDT_UUID
Definition: DIContext.h:188
@ DIDT_Null
Definition: DIContext.h:182
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:193
std::function< void(Error)> WarningHandler
Definition: DIContext.h:232
std::function< void(Error)> RecoverableErrorHandler
Definition: DIContext.h:230
static DIDumpOptions getForSingleDIE()
Return default option set for printing a single DIE without children.
Definition: DIContext.h:213
std::function< llvm::StringRef(uint64_t DwarfRegNum, bool IsEH)> GetNameForDWARFReg
Definition: DIContext.h:210
DIDumpOptions noImplicitRecursion() const
Return the options with RecurseDepth set to 0 unless explicitly required.
Definition: DIContext.h:221
unsigned ChildRecurseDepth
Definition: DIContext.h:195
unsigned ParentRecurseDepth
Definition: DIContext.h:196
Container for description of a global variable.
Definition: DIContext.h:118
uint64_t Size
Definition: DIContext.h:121
uint64_t Start
Definition: DIContext.h:120
std::string Name
Definition: DIContext.h:119
std::string DeclFile
Definition: DIContext.h:122
uint64_t DeclLine
Definition: DIContext.h:123
Controls which fields of DILineInfo container should be filled with data.
Definition: DIContext.h:144
FileLineInfoKind FLIKind
Definition: DIContext.h:157
DILineInfoSpecifier(FileLineInfoKind FLIKind=FileLineInfoKind::RawValue, FunctionNameKind FNKind=FunctionNameKind::None)
Definition: DIContext.h:160
bool operator==(const DILineInfoSpecifier &RHS) const
Definition: DIContext.h:164
FunctionNameKind FNKind
Definition: DIContext.h:158
A format-neutral container for source line information.
Definition: DIContext.h:32
static constexpr const char *const BadString
Definition: DIContext.h:34
void dump(raw_ostream &OS)
Definition: DIContext.h:75
std::optional< uint64_t > StartAddress
Definition: DIContext.h:48
uint32_t Discriminator
Definition: DIContext.h:51
bool operator!=(const DILineInfo &RHS) const
Definition: DIContext.h:64
static constexpr const char *const Addr2LineBadString
Definition: DIContext.h:36
uint32_t Line
Definition: DIContext.h:45
bool operator==(const DILineInfo &RHS) const
Definition: DIContext.h:57
std::optional< StringRef > Source
Definition: DIContext.h:41
std::string FileName
Definition: DIContext.h:37
std::string FunctionName
Definition: DIContext.h:38
uint32_t Column
Definition: DIContext.h:46
uint32_t StartLine
Definition: DIContext.h:47
bool operator<(const DILineInfo &RHS) const
Definition: DIContext.h:66
std::string StartFileName
Definition: DIContext.h:39
std::optional< StringRef > LineSource
Definition: DIContext.h:44
std::optional< uint64_t > Size
Definition: DIContext.h:134
std::optional< uint64_t > TagOffset
Definition: DIContext.h:135
std::string DeclFile
Definition: DIContext.h:131
std::optional< int64_t > FrameOffset
Definition: DIContext.h:133
uint64_t DeclLine
Definition: DIContext.h:132
std::string Name
Definition: DIContext.h:130
std::string FunctionName
Definition: DIContext.h:129
LoadedObjectInfoHelper(const LoadedObjectInfoHelper &)=default
LoadedObjectInfoHelper(Ts &&...Args)
Definition: DIContext.h:323
std::unique_ptr< llvm::LoadedObjectInfo > clone() const override
Obtain a copy of this LoadedObjectInfo.
Definition: DIContext.h:325