LLVM  13.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;
43 
44  // DWARF-specific.
46 
49  }
50 
51  bool operator==(const DILineInfo &RHS) const {
52  return Line == RHS.Line && Column == RHS.Column &&
53  FileName == RHS.FileName && FunctionName == RHS.FunctionName &&
56  }
57 
58  bool operator!=(const DILineInfo &RHS) const {
59  return !(*this == RHS);
60  }
61 
62  bool operator<(const DILineInfo &RHS) const {
63  return std::tie(FileName, FunctionName, StartFileName, Line, Column,
65  std::tie(RHS.FileName, RHS.FunctionName, RHS.StartFileName, RHS.Line,
66  RHS.Column, RHS.StartLine, RHS.Discriminator);
67  }
68 
69  explicit operator bool() const { return *this != DILineInfo(); }
70 
71  void dump(raw_ostream &OS) {
72  OS << "Line info: ";
73  if (FileName != BadString)
74  OS << "file '" << FileName << "', ";
75  if (FunctionName != BadString)
76  OS << "function '" << FunctionName << "', ";
77  OS << "line " << Line << ", ";
78  OS << "column " << Column << ", ";
79  if (StartFileName != BadString)
80  OS << "start file '" << StartFileName << "', ";
81  OS << "start line " << StartLine << '\n';
82  }
83 };
84 
86 
87 /// A format-neutral container for inlined code description.
90 
91 public:
92  DIInliningInfo() = default;
93 
94  const DILineInfo & getFrame(unsigned Index) const {
95  assert(Index < Frames.size());
96  return Frames[Index];
97  }
98 
100  assert(Index < Frames.size());
101  return &Frames[Index];
102  }
103 
105  return Frames.size();
106  }
107 
108  void addFrame(const DILineInfo &Frame) {
109  Frames.push_back(Frame);
110  }
111 
112  void resize(unsigned i) {
113  Frames.resize(i);
114  }
115 };
116 
117 /// Container for description of a global variable.
118 struct DIGlobal {
119  std::string Name;
120  uint64_t Start = 0;
121  uint64_t Size = 0;
122 
123  DIGlobal() : Name(DILineInfo::BadString) {}
124 };
125 
126 struct DILocal {
127  std::string FunctionName;
128  std::string Name;
129  std::string DeclFile;
130  uint64_t DeclLine = 0;
134 };
135 
136 /// A DINameKind is passed to name search methods to specify a
137 /// preference regarding the type of name resolution the caller wants.
139 
140 /// Controls which fields of DILineInfo container should be filled
141 /// with data.
143  enum class FileLineInfoKind {
144  None,
145  // RawValue is whatever the compiler stored in the filename table. Could be
146  // a full path, could be something else.
147  RawValue,
148  BaseNameOnly,
149  // Relative to the compilation directory.
152  };
154 
157 
160  : FLIKind(FLIKind), FNKind(FNKind) {}
161 };
162 
163 /// This is just a helper to programmatically construct DIDumpType.
165 #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME, OPTION) \
166  DIDT_ID_##ENUM_NAME,
167 #include "llvm/BinaryFormat/Dwarf.def"
168 #undef HANDLE_DWARF_SECTION
171 };
172 static_assert(DIDT_ID_Count <= 32, "section types overflow storage");
173 
174 /// Selects which debug sections get dumped.
175 enum DIDumpType : unsigned {
177  DIDT_All = ~0U,
178 #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME, OPTION) \
179  DIDT_##ENUM_NAME = 1U << DIDT_ID_##ENUM_NAME,
180 #include "llvm/BinaryFormat/Dwarf.def"
181 #undef HANDLE_DWARF_SECTION
183 };
184 
185 /// Container for dump options that control which debug information will be
186 /// dumped.
188  unsigned DumpType = DIDT_All;
189  unsigned ChildRecurseDepth = -1U;
190  unsigned ParentRecurseDepth = -1U;
191  uint16_t Version = 0; // DWARF version to assume when extracting.
192  uint8_t AddrSize = 4; // Address byte size to assume when extracting.
193  bool ShowAddresses = true;
194  bool ShowChildren = false;
195  bool ShowParents = false;
196  bool ShowForm = false;
197  bool SummarizeTypes = false;
198  bool Verbose = false;
199  bool DisplayRawContents = false;
200 
201  /// Return default option set for printing a single DIE without children.
203  DIDumpOptions Opts;
204  Opts.ChildRecurseDepth = 0;
205  Opts.ParentRecurseDepth = 0;
206  return Opts;
207  }
208 
209  /// Return the options with RecurseDepth set to 0 unless explicitly required.
211  DIDumpOptions Opts = *this;
212  if (ChildRecurseDepth == -1U && !ShowChildren)
213  Opts.ChildRecurseDepth = 0;
214  if (ParentRecurseDepth == -1U && !ShowParents)
215  Opts.ParentRecurseDepth = 0;
216  return Opts;
217  }
218 
222 };
223 
224 class DIContext {
225 public:
229  };
230 
231  DIContext(DIContextKind K) : Kind(K) {}
232  virtual ~DIContext() = default;
233 
234  DIContextKind getKind() const { return Kind; }
235 
236  virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts) = 0;
237 
238  virtual bool verify(raw_ostream &OS, DIDumpOptions DumpOpts = {}) {
239  // No verifier? Just say things went well.
240  return true;
241  }
242 
243  virtual DILineInfo getLineInfoForAddress(
244  object::SectionedAddress Address,
245  DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 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.
278  virtual uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const {
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>
306 struct LoadedObjectInfoHelper : Base {
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:164
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
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:131
llvm::DIDumpOptions::ShowForm
bool ShowForm
Definition: DIContext.h:196
llvm
Definition: AllocatorList.h:23
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:51
llvm::DILineInfoTable
SmallVector< std::pair< uint64_t, DILineInfo >, 16 > DILineInfoTable
Definition: DIContext.h:85
llvm::DIInliningInfo
A format-neutral container for inlined code description.
Definition: DIContext.h:88
llvm::DILineInfoSpecifier
Controls which fields of DILineInfo container should be filled with data.
Definition: DIContext.h:142
llvm::DIDumpOptions::DumpType
unsigned DumpType
Definition: DIContext.h:188
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:1168
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:112
llvm::DIDumpOptions::WarningHandler
std::function< void(Error)> WarningHandler
Definition: DIContext.h:221
llvm::DILocal::Size
Optional< uint64_t > Size
Definition: DIContext.h:132
llvm::WithColor::defaultErrorHandler
static void defaultErrorHandler(Error Err)
Implement default handling for Error.
Definition: WithColor.cpp:137
llvm::Optional
Definition: APInt.h:33
llvm::DIContext::CK_DWARF
@ CK_DWARF
Definition: DIContext.h:227
llvm::DIDumpOptions::SummarizeTypes
bool SummarizeTypes
Definition: DIContext.h:197
llvm::DINameKind::LinkageName
@ LinkageName
llvm::DILocal::FunctionName
std::string FunctionName
Definition: DIContext.h:127
llvm::LoadedObjectInfo::LoadedObjectInfo
LoadedObjectInfo()=default
llvm::DIInliningInfo::DIInliningInfo
DIInliningInfo()=default
llvm::Data
@ Data
Definition: SIMachineScheduler.h:56
llvm::WithColor::defaultWarningHandler
static void defaultWarningHandler(Error Warning)
Implement default handling for Warning.
Definition: WithColor.cpp:143
llvm::DILineInfo::operator!=
bool operator!=(const DILineInfo &RHS) const
Definition: DIContext.h:58
llvm::DILineInfo::FunctionName
std::string FunctionName
Definition: DIContext.h:37
llvm::DILocal::TagOffset
Optional< uint64_t > TagOffset
Definition: DIContext.h:133
llvm::DILineInfo::Addr2LineBadString
static constexpr const char *const Addr2LineBadString
Definition: DIContext.h:35
llvm::DILineInfoSpecifier::FileLineInfoKind::BaseNameOnly
@ BaseNameOnly
llvm::DILocal::DeclLine
uint64_t DeclLine
Definition: DIContext.h:130
llvm::DIDumpOptions::ShowAddresses
bool ShowAddresses
Definition: DIContext.h:193
llvm::DIDumpOptions::AddrSize
uint8_t AddrSize
Definition: DIContext.h:192
llvm::DILineInfo::Column
uint32_t Column
Definition: DIContext.h:41
llvm::DIGlobal::Start
uint64_t Start
Definition: DIContext.h:120
llvm::DIDumpOptions::getForSingleDIE
static DIDumpOptions getForSingleDIE()
Return default option set for printing a single DIE without children.
Definition: DIContext.h:202
llvm::DIDumpOptions::Version
uint16_t Version
Definition: DIContext.h:191
llvm::DIDumpOptions::Verbose
bool Verbose
Definition: DIContext.h:198
llvm::DINameKind
DINameKind
A DINameKind is passed to name search methods to specify a preference regarding the type of name reso...
Definition: DIContext.h:138
llvm::DIContext::getLineInfoForAddress
virtual DILineInfo getLineInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier())=0
llvm::DIInliningInfo::addFrame
void addFrame(const DILineInfo &Frame)
Definition: DIContext.h:108
llvm::DIContext
Definition: DIContext.h:224
llvm::DIDumpOptions::ShowChildren
bool ShowChildren
Definition: DIContext.h:194
llvm::DIInliningInfo::getFrame
const DILineInfo & getFrame(unsigned Index) const
Definition: DIContext.h:94
llvm::DILineInfo::dump
void dump(raw_ostream &OS)
Definition: DIContext.h:71
llvm::DIContext::DIContext
DIContext(DIContextKind K)
Definition: DIContext.h:231
llvm::DIDumpOptions::ChildRecurseDepth
unsigned ChildRecurseDepth
Definition: DIContext.h:189
llvm::DILineInfo::DILineInfo
DILineInfo()
Definition: DIContext.h:47
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
llvm::DILineInfo::StartLine
uint32_t StartLine
Definition: DIContext.h:42
llvm::SmallVectorImpl::resize
void resize(size_type N)
Definition: SmallVector.h:606
llvm::DIGlobal::DIGlobal
DIGlobal()
Definition: DIContext.h:123
llvm::DIInliningInfo::getMutableFrame
DILineInfo * getMutableFrame(unsigned Index)
Definition: DIContext.h:99
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:175
llvm::DIDumpOptions::ShowParents
bool ShowParents
Definition: DIContext.h:195
llvm::DIContext::getInliningInfoForAddress
virtual DIInliningInfo getInliningInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier())=0
llvm::DILineInfo::operator<
bool operator<(const DILineInfo &RHS) const
Definition: DIContext.h:62
llvm::DIDT_ID_Count
@ DIDT_ID_Count
Definition: DIContext.h:170
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:158
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
llvm::LoadedObjectInfoHelper::LoadedObjectInfoHelper
LoadedObjectInfoHelper(Ts &&... Args)
Definition: DIContext.h:313
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:143
llvm::DILineInfoSpecifier::FileLineInfoKind::RawValue
@ RawValue
llvm::DIContext::verify
virtual bool verify(raw_ostream &OS, DIDumpOptions DumpOpts={})
Definition: DIContext.h:238
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ObjectFile.h
llvm::DIGlobal::Name
std::string Name
Definition: DIContext.h:119
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:83
llvm::DILineInfo::Discriminator
uint32_t Discriminator
Definition: DIContext.h:45
llvm::DIContext::getLineInfoForAddressRange
virtual DILineInfoTable getLineInfoForAddressRange(object::SectionedAddress Address, uint64_t Size, DILineInfoSpecifier Specifier=DILineInfoSpecifier())=0
llvm::DIDT_All
@ DIDT_All
Definition: DIContext.h:177
llvm::LoadedObjectInfoHelper::LoadedObjectInfoHelper
LoadedObjectInfoHelper()=default
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
uint32_t
llvm::DIContext::CK_PDB
@ CK_PDB
Definition: DIContext.h:228
llvm::DIDT_UUID
@ DIDT_UUID
Definition: DIContext.h:182
llvm::DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath
@ AbsoluteFilePath
llvm::DILineInfoSpecifier::FNKind
FunctionNameKind FNKind
Definition: DIContext.h:156
WithColor.h
std
Definition: BitVector.h:838
llvm::DIGlobal
Container for description of a global variable.
Definition: DIContext.h:118
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:157
llvm::DIGlobal::Size
uint64_t Size
Definition: DIContext.h:121
llvm::DILocal
Definition: DIContext.h:126
llvm::DIDumpOptions::DisplayRawContents
bool DisplayRawContents
Definition: DIContext.h:199
llvm::DIDT_Null
@ DIDT_Null
Definition: DIContext.h:176
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:226
llvm::DILineInfo::FileName
std::string FileName
Definition: DIContext.h:36
llvm::DIDT_ID_UUID
@ DIDT_ID_UUID
Definition: DIContext.h:169
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:210
llvm::DILocal::DeclFile
std::string DeclFile
Definition: DIContext.h:129
llvm::DILineInfoSpecifier::FLIKind
FileLineInfoKind FLIKind
Definition: DIContext.h:155
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:389
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:190
llvm::DIContext::getKind
DIContextKind getKind() const
Definition: DIContext.h:234
llvm::DILocal::Name
std::string Name
Definition: DIContext.h:128
llvm::DIDumpOptions
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:187
llvm::DIDumpOptions::RecoverableErrorHandler
std::function< void(Error)> RecoverableErrorHandler
Definition: DIContext.h:219
llvm::DILineInfo::StartFileName
std::string StartFileName
Definition: DIContext.h:38
llvm::DIInliningInfo::getNumberOfFrames
uint32_t getNumberOfFrames() const
Definition: DIContext.h:104