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"
20 #include "llvm/Support/raw_ostream.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;
39   Optional<StringRef> Source;
40   uint32_t Line = 0;
41   uint32_t Column = 0;
42   uint32_t StartLine = 0;
43   Optional<uint64_t> StartAddress;
44 
45   // DWARF-specific.
46   uint32_t Discriminator = 0;
47 
48   DILineInfo()
49       : FileName(BadString), FunctionName(BadString), StartFileName(BadString) {
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,
63                     StartLine, Discriminator) <
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 
84 using DILineInfoTable = SmallVector<std::pair<uint64_t, DILineInfo>, 16>;
85 
86 /// A format-neutral container for inlined code description.
87 class DIInliningInfo {
88   SmallVector<DILineInfo, 4> Frames;
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 
100   DILineInfo *getMutableFrame(unsigned Index) {
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;
115   uint64_t Start = 0;
116   uint64_t Size = 0;
117   std::string DeclFile;
118   uint64_t DeclLine = 0;
119 
120   DIGlobal() : Name(DILineInfo::BadString) {}
121 };
122 
123 struct DILocal {
124   std::string FunctionName;
125   std::string Name;
126   std::string DeclFile;
127   uint64_t DeclLine = 0;
128   Optional<int64_t> FrameOffset;
129   Optional<uint64_t> Size;
130   Optional<uint64_t> TagOffset;
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.
135 enum class DINameKind { None, ShortName, LinkageName };
136 
137 /// Controls which fields of DILineInfo container should be filled
138 /// with data.
139 struct DILineInfoSpecifier {
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.
147     RelativeFilePath,
148     AbsoluteFilePath
149   };
150   using FunctionNameKind = DINameKind;
151 
152   FileLineInfoKind FLIKind;
153   FunctionNameKind FNKind;
154 
155   DILineInfoSpecifier(FileLineInfoKind FLIKind = FileLineInfoKind::RawValue,
156                       FunctionNameKind FNKind = FunctionNameKind::None)
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.
165 enum DIDumpTypeCounter {
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
170   DIDT_ID_UUID,
171   DIDT_ID_Count
172 };
173 static_assert(DIDT_ID_Count <= 32, "section types overflow storage");
174 
175 /// Selects which debug sections get dumped.
176 enum DIDumpType : unsigned {
177   DIDT_Null,
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
183   DIDT_UUID = 1 << DIDT_ID_UUID,
184 };
185 
186 /// Container for dump options that control which debug information will be
187 /// dumped.
188 struct DIDumpOptions {
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.
203   static DIDumpOptions getForSingleDIE() {
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.
211   DIDumpOptions noImplicitRecursion() const {
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 
220   std::function<void(Error)> RecoverableErrorHandler =
221       WithColor::defaultErrorHandler;
222   std::function<void(Error)> WarningHandler = WithColor::defaultWarningHandler;
223 };
224 
225 class DIContext {
226 public:
227   enum DIContextKind { CK_DWARF, CK_PDB };
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;
246   virtual DILineInfoTable getLineInfoForAddressRange(
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.
263 class LoadedObjectInfo {
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.
293   virtual bool getLoadedSectionContents(const object::SectionRef &Sec,
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:
308   LoadedObjectInfoHelper(const LoadedObjectInfoHelper &) = default;
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
323