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;
212 /// When verifying, print each error in detail.
213 bool ShowErrorDetails = true;
214 /// When verifying, print a summary of errors by categories.
216 bool PrintRegisterOnly = false;
218 /// List of DWARF tags to filter children by.
220 std::function<llvm::StringRef(uint64_t DwarfRegNum, bool IsEH)>
222 /// The target of the object being dumped.
224
228
229 /// Return default option set for printing a single DIE without children.
231 DIDumpOptions Opts;
232 Opts.ChildRecurseDepth = 0;
233 Opts.ParentRecurseDepth = 0;
234 return Opts;
235 }
236
237 /// Return the options with RecurseDepth set to 0 unless explicitly required.
239 DIDumpOptions Opts = *this;
240 if (ChildRecurseDepth == -1U && !ShowChildren)
241 Opts.ChildRecurseDepth = 0;
242 if (ParentRecurseDepth == -1U && !ShowParents)
243 Opts.ParentRecurseDepth = 0;
244 return Opts;
245 }
246
247 std::function<void(Error)> RecoverableErrorHandler =
250};
251
253public:
255
257 virtual ~DIContext() = default;
258
259 DIContextKind getKind() const { return Kind; }
260
261 virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts) = 0;
262
263 virtual bool verify(raw_ostream &OS, DIDumpOptions DumpOpts = {}) {
264 // No verifier? Just say things went well.
265 return true;
266 }
267
268 // For getLineInfoForAddress and getLineInfoForDataAddress, std::nullopt is
269 // returned when debug info is missing for the given address.
270 virtual std::optional<DILineInfo> getLineInfoForAddress(
272 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
273 virtual std::optional<DILineInfo>
277 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
280 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
281
282 virtual std::vector<DILocal>
284
285private:
286 const DIContextKind Kind;
287};
288
289/// An inferface for inquiring the load address of a loaded object file
290/// to be used by the DIContext implementations when applying relocations
291/// on the fly.
293protected:
294 LoadedObjectInfo() = default;
296
297public:
298 virtual ~LoadedObjectInfo() = default;
299
300 /// Obtain the Load Address of a section by SectionRef.
301 ///
302 /// Calculate the address of the given section.
303 /// The section need not be present in the local address space. The addresses
304 /// need to be consistent with the addresses used to query the DIContext and
305 /// the output of this function should be deterministic, i.e. repeated calls
306 /// with the same Sec should give the same address.
308 return 0;
309 }
310
311 /// If conveniently available, return the content of the given Section.
312 ///
313 /// When the section is available in the local address space, in relocated
314 /// (loaded) form, e.g. because it was relocated by a JIT for execution, this
315 /// function should provide the contents of said section in `Data`. If the
316 /// loaded section is not available, or the cost of retrieving it would be
317 /// prohibitive, this function should return false. In that case, relocations
318 /// will be read from the local (unrelocated) object file and applied on the
319 /// fly. Note that this method is used purely for optimzation purposes in the
320 /// common case of JITting in the local address space, so returning false
321 /// should always be correct.
323 StringRef &Data) const {
324 return false;
325 }
326
327 // FIXME: This is untested and unused anywhere in the LLVM project, it's
328 // used/needed by Julia (an external project). It should have some coverage
329 // (at least tests, but ideally example functionality).
330 /// Obtain a copy of this LoadedObjectInfo.
331 virtual std::unique_ptr<LoadedObjectInfo> clone() const = 0;
332};
333
334template <typename Derived, typename Base = LoadedObjectInfo>
336protected:
339
340public:
341 template <typename... Ts>
342 LoadedObjectInfoHelper(Ts &&...Args) : Base(std::forward<Ts>(Args)...) {}
343
344 std::unique_ptr<llvm::LoadedObjectInfo> clone() const override {
345 return std::make_unique<Derived>(static_cast<const Derived &>(*this));
346 }
347};
348
349} // end namespace llvm
350
351#endif // LLVM_DEBUGINFO_DICONTEXT_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t 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:263
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:259
virtual std::optional< DILineInfo > getLineInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier())=0
DIContext(DIContextKind K)
Definition DIContext.h:256
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:322
virtual ~LoadedObjectInfo()=default
virtual uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const
Obtain the Load Address of a section by SectionRef.
Definition DIContext.h:307
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:879
Container for dump options that control which debug information will be dumped.
Definition DIContext.h:197
std::function< void(Error)> WarningHandler
Definition DIContext.h:249
std::function< void(Error)> RecoverableErrorHandler
Definition DIContext.h:247
static DIDumpOptions getForSingleDIE()
Return default option set for printing a single DIE without children.
Definition DIContext.h:230
llvm::SmallVector< unsigned, 0 > FilterChildTag
List of DWARF tags to filter children by.
Definition DIContext.h:219
StringRef getNameForDWARFAddressSpace(uint64_t AS) const
Definition DIContext.h:225
std::string JsonErrSummaryFile
Definition DIContext.h:217
bool ShowAggregateErrors
When verifying, print a summary of errors by categories.
Definition DIContext.h:215
std::function< llvm::StringRef(uint64_t DwarfRegNum, bool IsEH)> GetNameForDWARFReg
Definition DIContext.h:221
Triple TT
The target of the object being dumped.
Definition DIContext.h:223
DIDumpOptions noImplicitRecursion() const
Return the options with RecurseDepth set to 0 unless explicitly required.
Definition DIContext.h:238
unsigned ChildRecurseDepth
Definition DIContext.h:199
unsigned ParentRecurseDepth
Definition DIContext.h:200
bool ShowErrorDetails
When verifying, print each error in detail.
Definition DIContext.h:213
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:342
std::unique_ptr< llvm::LoadedObjectInfo > clone() const override
Obtain a copy of this LoadedObjectInfo.
Definition DIContext.h:344