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