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;
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 &&
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  const DILineInfo &getFrame(unsigned Index) const {
94  assert(Index < Frames.size());
95  return Frames[Index];
96  }
97 
99  assert(Index < Frames.size());
100  return &Frames[Index];
101  }
102 
103  uint32_t getNumberOfFrames() const { return Frames.size(); }
104 
105  void addFrame(const DILineInfo &Frame) { Frames.push_back(Frame); }
106 
107  void resize(unsigned i) { Frames.resize(i); }
108 };
109 
110 /// Container for description of a global variable.
111 struct DIGlobal {
112  std::string Name;
113  uint64_t Start = 0;
114  uint64_t Size = 0;
115 
116  DIGlobal() : Name(DILineInfo::BadString) {}
117 };
118 
119 struct DILocal {
120  std::string FunctionName;
121  std::string Name;
122  std::string DeclFile;
123  uint64_t DeclLine = 0;
127 };
128 
129 /// A DINameKind is passed to name search methods to specify a
130 /// preference regarding the type of name resolution the caller wants.
132 
133 /// Controls which fields of DILineInfo container should be filled
134 /// with data.
136  enum class FileLineInfoKind {
137  None,
138  // RawValue is whatever the compiler stored in the filename table. Could be
139  // a full path, could be something else.
140  RawValue,
141  BaseNameOnly,
142  // Relative to the compilation directory.
145  };
147 
150 
153  : FLIKind(FLIKind), FNKind(FNKind) {}
154 };
155 
156 /// This is just a helper to programmatically construct DIDumpType.
158 #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME, OPTION) \
159  DIDT_ID_##ENUM_NAME,
160 #include "llvm/BinaryFormat/Dwarf.def"
161 #undef HANDLE_DWARF_SECTION
164 };
165 static_assert(DIDT_ID_Count <= 32, "section types overflow storage");
166 
167 /// Selects which debug sections get dumped.
168 enum DIDumpType : unsigned {
170  DIDT_All = ~0U,
171 #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME, OPTION) \
172  DIDT_##ENUM_NAME = 1U << DIDT_ID_##ENUM_NAME,
173 #include "llvm/BinaryFormat/Dwarf.def"
174 #undef HANDLE_DWARF_SECTION
176 };
177 
178 /// Container for dump options that control which debug information will be
179 /// dumped.
181  unsigned DumpType = DIDT_All;
182  unsigned ChildRecurseDepth = -1U;
183  unsigned ParentRecurseDepth = -1U;
184  uint16_t Version = 0; // DWARF version to assume when extracting.
185  uint8_t AddrSize = 4; // Address byte size to assume when extracting.
186  bool ShowAddresses = true;
187  bool ShowChildren = false;
188  bool ShowParents = false;
189  bool ShowForm = false;
190  bool SummarizeTypes = false;
191  bool Verbose = false;
192  bool DisplayRawContents = false;
193 
194  /// Return default option set for printing a single DIE without children.
196  DIDumpOptions Opts;
197  Opts.ChildRecurseDepth = 0;
198  Opts.ParentRecurseDepth = 0;
199  return Opts;
200  }
201 
202  /// Return the options with RecurseDepth set to 0 unless explicitly required.
204  DIDumpOptions Opts = *this;
205  if (ChildRecurseDepth == -1U && !ShowChildren)
206  Opts.ChildRecurseDepth = 0;
207  if (ParentRecurseDepth == -1U && !ShowParents)
208  Opts.ParentRecurseDepth = 0;
209  return Opts;
210  }
211 
215 };
216 
217 class DIContext {
218 public:
220 
221  DIContext(DIContextKind K) : Kind(K) {}
222  virtual ~DIContext() = default;
223 
224  DIContextKind getKind() const { return Kind; }
225 
226  virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts) = 0;
227 
228  virtual bool verify(raw_ostream &OS, DIDumpOptions DumpOpts = {}) {
229  // No verifier? Just say things went well.
230  return true;
231  }
232 
233  virtual DILineInfo getLineInfoForAddress(
234  object::SectionedAddress Address,
235  DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
237  object::SectionedAddress Address, uint64_t Size,
238  DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
239  virtual DIInliningInfo getInliningInfoForAddress(
240  object::SectionedAddress Address,
241  DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
242 
243  virtual std::vector<DILocal>
244  getLocalsForAddress(object::SectionedAddress Address) = 0;
245 
246 private:
247  const DIContextKind Kind;
248 };
249 
250 /// An inferface for inquiring the load address of a loaded object file
251 /// to be used by the DIContext implementations when applying relocations
252 /// on the fly.
254 protected:
255  LoadedObjectInfo() = default;
256  LoadedObjectInfo(const LoadedObjectInfo &) = default;
257 
258 public:
259  virtual ~LoadedObjectInfo() = default;
260 
261  /// Obtain the Load Address of a section by SectionRef.
262  ///
263  /// Calculate the address of the given section.
264  /// The section need not be present in the local address space. The addresses
265  /// need to be consistent with the addresses used to query the DIContext and
266  /// the output of this function should be deterministic, i.e. repeated calls
267  /// with the same Sec should give the same address.
268  virtual uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const {
269  return 0;
270  }
271 
272  /// If conveniently available, return the content of the given Section.
273  ///
274  /// When the section is available in the local address space, in relocated
275  /// (loaded) form, e.g. because it was relocated by a JIT for execution, this
276  /// function should provide the contents of said section in `Data`. If the
277  /// loaded section is not available, or the cost of retrieving it would be
278  /// prohibitive, this function should return false. In that case, relocations
279  /// will be read from the local (unrelocated) object file and applied on the
280  /// fly. Note that this method is used purely for optimzation purposes in the
281  /// common case of JITting in the local address space, so returning false
282  /// should always be correct.
284  StringRef &Data) const {
285  return false;
286  }
287 
288  // FIXME: This is untested and unused anywhere in the LLVM project, it's
289  // used/needed by Julia (an external project). It should have some coverage
290  // (at least tests, but ideally example functionality).
291  /// Obtain a copy of this LoadedObjectInfo.
292  virtual std::unique_ptr<LoadedObjectInfo> clone() const = 0;
293 };
294 
295 template <typename Derived, typename Base = LoadedObjectInfo>
297 protected:
299  LoadedObjectInfoHelper() = default;
300 
301 public:
302  template <typename... Ts>
303  LoadedObjectInfoHelper(Ts &&...Args) : Base(std::forward<Ts>(Args)...) {}
304 
305  std::unique_ptr<llvm::LoadedObjectInfo> clone() const override {
306  return std::make_unique<Derived>(static_cast<const Derived &>(*this));
307  }
308 };
309 
310 } // end namespace llvm
311 
312 #endif // LLVM_DEBUGINFO_DICONTEXT_H
llvm::DIDumpTypeCounter
DIDumpTypeCounter
This is just a helper to programmatically construct DIDumpType.
Definition: DIContext.h:157
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:124
llvm::DIDumpOptions::ShowForm
bool ShowForm
Definition: DIContext.h:189
llvm
---------------------— PointerInfo ------------------------------------—
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:283
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:135
llvm::DIDumpOptions::DumpType
unsigned DumpType
Definition: DIContext.h:181
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:296
llvm::DILineInfo::BadString
static constexpr const char *const BadString
Definition: DIContext.h:33
llvm::DIInliningInfo::resize
void resize(unsigned i)
Definition: DIContext.h:107
llvm::DIDumpOptions::WarningHandler
std::function< void(Error)> WarningHandler
Definition: DIContext.h:214
llvm::DILocal::Size
Optional< uint64_t > Size
Definition: DIContext.h:125
llvm::WithColor::defaultErrorHandler
static void defaultErrorHandler(Error Err)
Implement default handling for Error.
Definition: WithColor.cpp:151
llvm::Optional
Definition: APInt.h:33
llvm::DIContext::CK_DWARF
@ CK_DWARF
Definition: DIContext.h:219
llvm::DIDumpOptions::SummarizeTypes
bool SummarizeTypes
Definition: DIContext.h:190
llvm::DINameKind::LinkageName
@ LinkageName
llvm::DILocal::FunctionName
std::string FunctionName
Definition: DIContext.h:120
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:157
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:126
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:123
llvm::DIDumpOptions::ShowAddresses
bool ShowAddresses
Definition: DIContext.h:186
llvm::DIDumpOptions::AddrSize
uint8_t AddrSize
Definition: DIContext.h:185
llvm::DILineInfo::Column
uint32_t Column
Definition: DIContext.h:41
llvm::DIGlobal::Start
uint64_t Start
Definition: DIContext.h:113
llvm::DIDumpOptions::getForSingleDIE
static DIDumpOptions getForSingleDIE()
Return default option set for printing a single DIE without children.
Definition: DIContext.h:195
llvm::DIDumpOptions::Version
uint16_t Version
Definition: DIContext.h:184
llvm::DIDumpOptions::Verbose
bool Verbose
Definition: DIContext.h:191
llvm::DINameKind
DINameKind
A DINameKind is passed to name search methods to specify a preference regarding the type of name reso...
Definition: DIContext.h:131
llvm::DIContext::getLineInfoForAddress
virtual DILineInfo getLineInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier())=0
llvm::DIInliningInfo::addFrame
void addFrame(const DILineInfo &Frame)
Definition: DIContext.h:105
llvm::DIContext
Definition: DIContext.h:217
llvm::DIDumpOptions::ShowChildren
bool ShowChildren
Definition: DIContext.h:187
llvm::DIInliningInfo::getFrame
const DILineInfo & getFrame(unsigned Index) const
Definition: DIContext.h:93
llvm::DILineInfo::dump
void dump(raw_ostream &OS)
Definition: DIContext.h:70
llvm::DIContext::DIContext
DIContext(DIContextKind K)
Definition: DIContext.h:221
llvm::DIDumpOptions::ChildRecurseDepth
unsigned ChildRecurseDepth
Definition: DIContext.h:182
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:606
llvm::DIGlobal::DIGlobal
DIGlobal()
Definition: DIContext.h:116
llvm::DIInliningInfo::getMutableFrame
DILineInfo * getMutableFrame(unsigned Index)
Definition: DIContext.h:98
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:168
llvm::DIDumpOptions::ShowParents
bool ShowParents
Definition: DIContext.h:188
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:61
llvm::DIDT_ID_Count
@ DIDT_ID_Count
Definition: DIContext.h:163
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:268
llvm::DILineInfoSpecifier::DILineInfoSpecifier
DILineInfoSpecifier(FileLineInfoKind FLIKind=FileLineInfoKind::RawValue, FunctionNameKind FNKind=FunctionNameKind::None)
Definition: DIContext.h:151
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
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:136
llvm::DILineInfoSpecifier::FileLineInfoKind::RawValue
@ RawValue
llvm::DIContext::verify
virtual bool verify(raw_ostream &OS, DIDumpOptions DumpOpts={})
Definition: DIContext.h:228
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ObjectFile.h
llvm::DIGlobal::Name
std::string Name
Definition: DIContext.h:112
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:83
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:170
llvm::LoadedObjectInfoHelper::LoadedObjectInfoHelper
LoadedObjectInfoHelper()=default
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::LoadedObjectInfoHelper::LoadedObjectInfoHelper
LoadedObjectInfoHelper(Ts &&...Args)
Definition: DIContext.h:303
uint32_t
llvm::DIContext::CK_PDB
@ CK_PDB
Definition: DIContext.h:219
llvm::DIDT_UUID
@ DIDT_UUID
Definition: DIContext.h:175
llvm::DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath
@ AbsoluteFilePath
llvm::DILineInfoSpecifier::FNKind
FunctionNameKind FNKind
Definition: DIContext.h:149
WithColor.h
std
Definition: BitVector.h:838
llvm::DIGlobal
Container for description of a global variable.
Definition: DIContext.h:111
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:114
llvm::DILocal
Definition: DIContext.h:119
llvm::DIDumpOptions::DisplayRawContents
bool DisplayRawContents
Definition: DIContext.h:192
llvm::DIDT_Null
@ DIDT_Null
Definition: DIContext.h:169
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:253
llvm::DIContext::DIContextKind
DIContextKind
Definition: DIContext.h:219
llvm::DILineInfo::FileName
std::string FileName
Definition: DIContext.h:36
llvm::DIDT_ID_UUID
@ DIDT_ID_UUID
Definition: DIContext.h:162
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:305
llvm::DIDumpOptions::noImplicitRecursion
DIDumpOptions noImplicitRecursion() const
Return the options with RecurseDepth set to 0 unless explicitly required.
Definition: DIContext.h:203
llvm::DILocal::DeclFile
std::string DeclFile
Definition: DIContext.h:122
llvm::DILineInfoSpecifier::FLIKind
FileLineInfoKind FLIKind
Definition: DIContext.h:148
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:183
llvm::DIContext::getKind
DIContextKind getKind() const
Definition: DIContext.h:224
llvm::sampleprof::Base
@ Base
Definition: Discriminator.h:58
llvm::DILocal::Name
std::string Name
Definition: DIContext.h:121
llvm::DIDumpOptions
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:180
llvm::DIDumpOptions::RecoverableErrorHandler
std::function< void(Error)> RecoverableErrorHandler
Definition: DIContext.h:212
llvm::DILineInfo::StartFileName
std::string StartFileName
Definition: DIContext.h:38
llvm::DIInliningInfo::getNumberOfFrames
uint32_t getNumberOfFrames() const
Definition: DIContext.h:103