LLVM  9.0.0svn
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"
20 #include <cassert>
21 #include <cstdint>
22 #include <memory>
23 #include <string>
24 #include <tuple>
25 #include <utility>
26 
27 namespace llvm {
28 
29 /// A format-neutral container for source line information.
30 struct DILineInfo {
31  std::string FileName;
32  std::string FunctionName;
37 
38  // DWARF-specific.
40 
41  DILineInfo() : FileName("<invalid>"), FunctionName("<invalid>") {}
42 
43  bool operator==(const DILineInfo &RHS) const {
44  return Line == RHS.Line && Column == RHS.Column &&
45  FileName == RHS.FileName && FunctionName == RHS.FunctionName &&
46  StartLine == RHS.StartLine && Discriminator == RHS.Discriminator;
47  }
48 
49  bool operator!=(const DILineInfo &RHS) const {
50  return !(*this == RHS);
51  }
52 
53  bool operator<(const DILineInfo &RHS) const {
54  return std::tie(FileName, FunctionName, Line, Column, StartLine,
55  Discriminator) <
56  std::tie(RHS.FileName, RHS.FunctionName, RHS.Line, RHS.Column,
57  RHS.StartLine, RHS.Discriminator);
58  }
59 
60  explicit operator bool() const { return *this != DILineInfo(); }
61 
62  void dump(raw_ostream &OS) {
63  OS << "Line info: ";
64  if (FileName != "<invalid>")
65  OS << "file '" << FileName << "', ";
66  if (FunctionName != "<invalid>")
67  OS << "function '" << FunctionName << "', ";
68  OS << "line " << Line << ", ";
69  OS << "column " << Column << ", ";
70  OS << "start line " << StartLine << '\n';
71  }
72 };
73 
75 
76 /// A format-neutral container for inlined code description.
79 
80 public:
81  DIInliningInfo() = default;
82 
83  const DILineInfo & getFrame(unsigned Index) const {
84  assert(Index < Frames.size());
85  return Frames[Index];
86  }
87 
89  assert(Index < Frames.size());
90  return &Frames[Index];
91  }
92 
94  return Frames.size();
95  }
96 
97  void addFrame(const DILineInfo &Frame) {
98  Frames.push_back(Frame);
99  }
100 
101  void resize(unsigned i) {
102  Frames.resize(i);
103  }
104 };
105 
106 /// Container for description of a global variable.
107 struct DIGlobal {
108  std::string Name;
109  uint64_t Start = 0;
110  uint64_t Size = 0;
111 
112  DIGlobal() : Name("<invalid>") {}
113 };
114 
115 /// A DINameKind is passed to name search methods to specify a
116 /// preference regarding the type of name resolution the caller wants.
118 
119 /// Controls which fields of DILineInfo container should be filled
120 /// with data.
122  enum class FileLineInfoKind { None, Default, AbsoluteFilePath };
124 
127 
130  : FLIKind(FLIKind), FNKind(FNKind) {}
131 };
132 
133 /// This is just a helper to programmatically construct DIDumpType.
135 #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \
136  DIDT_ID_##ENUM_NAME,
137 #include "llvm/BinaryFormat/Dwarf.def"
138 #undef HANDLE_DWARF_SECTION
141 };
142 static_assert(DIDT_ID_Count <= 32, "section types overflow storage");
143 
144 /// Selects which debug sections get dumped.
145 enum DIDumpType : unsigned {
147  DIDT_All = ~0U,
148 #define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME) \
149  DIDT_##ENUM_NAME = 1U << DIDT_ID_##ENUM_NAME,
150 #include "llvm/BinaryFormat/Dwarf.def"
151 #undef HANDLE_DWARF_SECTION
153 };
154 
155 /// Container for dump options that control which debug information will be
156 /// dumped.
158  unsigned DumpType = DIDT_All;
159  unsigned ChildRecurseDepth = -1U;
160  unsigned ParentRecurseDepth = -1U;
161  uint16_t Version = 0; // DWARF version to assume when extracting.
162  uint8_t AddrSize = 4; // Address byte size to assume when extracting.
163  bool ShowAddresses = true;
164  bool ShowChildren = false;
165  bool ShowParents = false;
166  bool ShowForm = false;
167  bool SummarizeTypes = false;
168  bool Verbose = false;
169  bool DisplayRawContents = false;
170 
171  /// Return default option set for printing a single DIE without children.
173  DIDumpOptions Opts;
174  Opts.ChildRecurseDepth = 0;
175  Opts.ParentRecurseDepth = 0;
176  return Opts;
177  }
178 
179  /// Return the options with RecurseDepth set to 0 unless explicitly required.
181  DIDumpOptions Opts = *this;
182  if (ChildRecurseDepth == -1U && !ShowChildren)
183  Opts.ChildRecurseDepth = 0;
184  if (ParentRecurseDepth == -1U && !ShowParents)
185  Opts.ParentRecurseDepth = 0;
186  return Opts;
187  }
188 };
189 
190 class DIContext {
191 public:
194  CK_PDB
195  };
196 
198  virtual ~DIContext() = default;
199 
200  DIContextKind getKind() const { return Kind; }
201 
202  virtual void dump(raw_ostream &OS, DIDumpOptions DumpOpts) = 0;
203 
204  virtual bool verify(raw_ostream &OS, DIDumpOptions DumpOpts = {}) {
205  // No verifier? Just say things went well.
206  return true;
207  }
208 
209  virtual DILineInfo getLineInfoForAddress(
211  DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
212  virtual DILineInfoTable getLineInfoForAddressRange(
213  object::SectionedAddress Address, uint64_t Size,
214  DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
215  virtual DIInliningInfo getInliningInfoForAddress(
216  object::SectionedAddress Address,
217  DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
218 
219 private:
220  const DIContextKind Kind;
221 };
222 
223 /// An inferface for inquiring the load address of a loaded object file
224 /// to be used by the DIContext implementations when applying relocations
225 /// on the fly.
227 protected:
228  LoadedObjectInfo() = default;
229  LoadedObjectInfo(const LoadedObjectInfo &) = default;
230 
231 public:
232  virtual ~LoadedObjectInfo() = default;
233 
234  /// Obtain the Load Address of a section by SectionRef.
235  ///
236  /// Calculate the address of the given section.
237  /// The section need not be present in the local address space. The addresses
238  /// need to be consistent with the addresses used to query the DIContext and
239  /// the output of this function should be deterministic, i.e. repeated calls
240  /// with the same Sec should give the same address.
241  virtual uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const {
242  return 0;
243  }
244 
245  /// If conveniently available, return the content of the given Section.
246  ///
247  /// When the section is available in the local address space, in relocated
248  /// (loaded) form, e.g. because it was relocated by a JIT for execution, this
249  /// function should provide the contents of said section in `Data`. If the
250  /// loaded section is not available, or the cost of retrieving it would be
251  /// prohibitive, this function should return false. In that case, relocations
252  /// will be read from the local (unrelocated) object file and applied on the
253  /// fly. Note that this method is used purely for optimzation purposes in the
254  /// common case of JITting in the local address space, so returning false
255  /// should always be correct.
257  StringRef &Data) const {
258  return false;
259  }
260 
261  // FIXME: This is untested and unused anywhere in the LLVM project, it's
262  // used/needed by Julia (an external project). It should have some coverage
263  // (at least tests, but ideally example functionality).
264  /// Obtain a copy of this LoadedObjectInfo.
265  virtual std::unique_ptr<LoadedObjectInfo> clone() const = 0;
266 };
267 
268 template <typename Derived, typename Base = LoadedObjectInfo>
270 protected:
272  LoadedObjectInfoHelper() = default;
273 
274 public:
275  template <typename... Ts>
276  LoadedObjectInfoHelper(Ts &&... Args) : Base(std::forward<Ts>(Args)...) {}
277 
278  std::unique_ptr<llvm::LoadedObjectInfo> clone() const override {
279  return llvm::make_unique<Derived>(static_cast<const Derived &>(*this));
280  }
281 };
282 
283 } // end namespace llvm
284 
285 #endif // LLVM_DEBUGINFO_DICONTEXT_H
uint32_t StartLine
Definition: DIContext.h:36
uint32_t Discriminator
Definition: DIContext.h:39
Optional< StringRef > Source
Definition: DIContext.h:33
bool operator!=(const DILineInfo &RHS) const
Definition: DIContext.h:49
DILineInfo * getMutableFrame(unsigned Index)
Definition: DIContext.h:88
std::string FileName
Definition: DIContext.h:31
This class represents lattice values for constants.
Definition: AllocatorList.h:23
virtual bool getLoadedSectionContents(const object::SectionRef &Sec, StringRef &Data) const
If conveniently available, return the content of the given Section.
Definition: DIContext.h:256
void addFrame(const DILineInfo &Frame)
Definition: DIContext.h:97
DINameKind
A DINameKind is passed to name search methods to specify a preference regarding the type of name reso...
Definition: DIContext.h:117
void resize(unsigned i)
Definition: DIContext.h:101
A format-neutral container for source line information.
Definition: DIContext.h:30
Definition: BitVector.h:937
FunctionNameKind FNKind
Definition: DIContext.h:126
FileLineInfoKind FLIKind
Definition: DIContext.h:125
bool operator<(const DILineInfo &RHS) const
Definition: DIContext.h:53
virtual uint64_t getSectionLoadAddress(const object::SectionRef &Sec) const
Obtain the Load Address of a section by SectionRef.
Definition: DIContext.h:241
DIContextKind getKind() const
Definition: DIContext.h:200
virtual bool verify(raw_ostream &OS, DIDumpOptions DumpOpts={})
Definition: DIContext.h:204
uint32_t getNumberOfFrames() const
Definition: DIContext.h:93
unsigned ParentRecurseDepth
Definition: DIContext.h:160
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:157
uint32_t Column
Definition: DIContext.h:35
Controls which fields of DILineInfo container should be filled with data.
Definition: DIContext.h:121
DIDumpOptions noImplicitRecursion() const
Return the options with RecurseDepth set to 0 unless explicitly required.
Definition: DIContext.h:180
bool operator==(const DILineInfo &RHS) const
Definition: DIContext.h:43
DIContext(DIContextKind K)
Definition: DIContext.h:197
A format-neutral container for inlined code description.
Definition: DIContext.h:77
LoadedObjectInfoHelper(Ts &&... Args)
Definition: DIContext.h:276
size_t size() const
Definition: SmallVector.h:52
void dump(raw_ostream &OS)
Definition: DIContext.h:62
DILineInfoSpecifier(FileLineInfoKind FLIKind=FileLineInfoKind::Default, FunctionNameKind FNKind=FunctionNameKind::None)
Definition: DIContext.h:128
uint32_t Line
Definition: DIContext.h:34
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
std::string Name
Definition: DIContext.h:108
std::string FunctionName
Definition: DIContext.h:32
DIDumpType
Selects which debug sections get dumped.
Definition: DIContext.h:145
An inferface for inquiring the load address of a loaded object file to be used by the DIContext imple...
Definition: DIContext.h:226
std::unique_ptr< llvm::LoadedObjectInfo > clone() const override
Definition: DIContext.h:278
DIDumpTypeCounter
This is just a helper to programmatically construct DIDumpType.
Definition: DIContext.h:134
uint32_t Size
Definition: Profile.cpp:46
unsigned ChildRecurseDepth
Definition: DIContext.h:159
static DIDumpOptions getForSingleDIE()
Return default option set for printing a single DIE without children.
Definition: DIContext.h:172
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const DILineInfo & getFrame(unsigned Index) const
Definition: DIContext.h:83
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
const uint64_t Version
Definition: InstrProf.h:984
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Container for description of a global variable.
Definition: DIContext.h:107
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
void resize(size_type N)
Definition: SmallVector.h:344