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