LLVM  6.0.0svn
DWARFContext.h
Go to the documentation of this file.
1 //===- DWARFContext.h -------------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===/
9 
10 #ifndef LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H
11 #define LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H
12 
13 #include "llvm/ADT/MapVector.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/ADT/StringRef.h"
35 #include "llvm/Object/Binary.h"
36 #include "llvm/Object/ObjectFile.h"
37 #include "llvm/Support/Error.h"
38 #include "llvm/Support/Host.h"
39 #include <cstdint>
40 #include <deque>
41 #include <map>
42 #include <memory>
43 
44 namespace llvm {
45 
46 class DataExtractor;
47 class MCRegisterInfo;
48 class MemoryBuffer;
49 class raw_ostream;
50 
51 /// Used as a return value for a error callback passed to DWARF context.
52 /// Callback should return Halt if client application wants to stop
53 /// object parsing, or should return Continue otherwise.
54 enum class ErrorPolicy { Halt, Continue };
55 
56 /// DWARFContext
57 /// This data structure is the top level entity that deals with dwarf debug
58 /// information parsing. The actual data is supplied through DWARFObj.
59 class DWARFContext : public DIContext {
61  std::deque<DWARFUnitSection<DWARFTypeUnit>> TUs;
62  std::unique_ptr<DWARFUnitIndex> CUIndex;
63  std::unique_ptr<DWARFGdbIndex> GdbIndex;
64  std::unique_ptr<DWARFUnitIndex> TUIndex;
65  std::unique_ptr<DWARFDebugAbbrev> Abbrev;
66  std::unique_ptr<DWARFDebugLoc> Loc;
67  std::unique_ptr<DWARFDebugAranges> Aranges;
68  std::unique_ptr<DWARFDebugLine> Line;
69  std::unique_ptr<DWARFDebugFrame> DebugFrame;
70  std::unique_ptr<DWARFDebugFrame> EHFrame;
71  std::unique_ptr<DWARFDebugMacro> Macro;
72  std::unique_ptr<DWARFAcceleratorTable> AppleNames;
73  std::unique_ptr<DWARFAcceleratorTable> AppleTypes;
74  std::unique_ptr<DWARFAcceleratorTable> AppleNamespaces;
75  std::unique_ptr<DWARFAcceleratorTable> AppleObjC;
76 
78  std::deque<DWARFUnitSection<DWARFTypeUnit>> DWOTUs;
79  std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
80  std::unique_ptr<DWARFDebugLocDWO> LocDWO;
81 
82  /// The maximum DWARF version of all units.
83  unsigned MaxVersion = 0;
84 
85  struct DWOFile {
87  std::unique_ptr<DWARFContext> Context;
88  };
90  std::weak_ptr<DWOFile> DWP;
91  bool CheckedForDWP = false;
92  std::string DWPName;
93 
94  std::unique_ptr<MCRegisterInfo> RegInfo;
95 
96  /// Read compile units from the debug_info section (if necessary)
97  /// and store them in CUs.
98  void parseCompileUnits();
99 
100  /// Read type units from the debug_types sections (if necessary)
101  /// and store them in TUs.
102  void parseTypeUnits();
103 
104  /// Read compile units from the debug_info.dwo section (if necessary)
105  /// and store them in DWOCUs.
106  void parseDWOCompileUnits();
107 
108  /// Read type units from the debug_types.dwo section (if necessary)
109  /// and store them in DWOTUs.
110  void parseDWOTypeUnits();
111 
112 protected:
113  std::unique_ptr<const DWARFObject> DObj;
114 
115 public:
116  DWARFContext(std::unique_ptr<const DWARFObject> DObj,
117  std::string DWPName = "");
118  ~DWARFContext();
119 
120  DWARFContext(DWARFContext &) = delete;
121  DWARFContext &operator=(DWARFContext &) = delete;
122 
123  const DWARFObject &getDWARFObj() const { return *DObj; }
124 
125  static bool classof(const DIContext *DICtx) {
126  return DICtx->getKind() == CK_DWARF;
127  }
128 
129  /// Dump a textual representation to \p OS. If any \p DumpOffsets are present,
130  /// dump only the record at the specified offset.
131  void dump(raw_ostream &OS, DIDumpOptions DumpOpts,
132  std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets);
133 
134  void dump(raw_ostream &OS, DIDumpOptions DumpOpts) override {
135  std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets;
136  dump(OS, DumpOpts, DumpOffsets);
137  }
138 
139  bool verify(raw_ostream &OS, DIDumpOptions DumpOpts = {}) override;
140 
144 
145  /// Get compile units in this context.
147  parseCompileUnits();
148  return cu_iterator_range(CUs.begin(), CUs.end());
149  }
150 
151  /// Get type units in this context.
153  parseTypeUnits();
154  return tu_section_iterator_range(TUs.begin(), TUs.end());
155  }
156 
157  /// Get compile units in the DWO context.
159  parseDWOCompileUnits();
160  return cu_iterator_range(DWOCUs.begin(), DWOCUs.end());
161  }
162 
163  /// Get type units in the DWO context.
165  parseDWOTypeUnits();
166  return tu_section_iterator_range(DWOTUs.begin(), DWOTUs.end());
167  }
168 
169  /// Get the number of compile units in this context.
170  unsigned getNumCompileUnits() {
171  parseCompileUnits();
172  return CUs.size();
173  }
174 
175  /// Get the number of compile units in this context.
176  unsigned getNumTypeUnits() {
177  parseTypeUnits();
178  return TUs.size();
179  }
180 
181  /// Get the number of compile units in the DWO context.
183  parseDWOCompileUnits();
184  return DWOCUs.size();
185  }
186 
187  /// Get the number of compile units in the DWO context.
188  unsigned getNumDWOTypeUnits() {
189  parseDWOTypeUnits();
190  return DWOTUs.size();
191  }
192 
193  /// Get the compile unit at the specified index for this compile unit.
195  parseCompileUnits();
196  return CUs[index].get();
197  }
198 
199  /// Get the compile unit at the specified index for the DWO compile units.
201  parseDWOCompileUnits();
202  return DWOCUs[index].get();
203  }
204 
205  DWARFCompileUnit *getDWOCompileUnitForHash(uint64_t Hash);
206 
207  /// Get a DIE given an exact offset.
208  DWARFDie getDIEForOffset(uint32_t Offset);
209 
210  unsigned getMaxVersion() const { return MaxVersion; }
211 
213  if (Version > MaxVersion)
214  MaxVersion = Version;
215  }
216 
217  const DWARFUnitIndex &getCUIndex();
218  DWARFGdbIndex &getGdbIndex();
219  const DWARFUnitIndex &getTUIndex();
220 
221  /// Get a pointer to the parsed DebugAbbrev object.
222  const DWARFDebugAbbrev *getDebugAbbrev();
223 
224  /// Get a pointer to the parsed DebugLoc object.
225  const DWARFDebugLoc *getDebugLoc();
226 
227  /// Get a pointer to the parsed dwo abbreviations object.
228  const DWARFDebugAbbrev *getDebugAbbrevDWO();
229 
230  /// Get a pointer to the parsed DebugLoc object.
231  const DWARFDebugLocDWO *getDebugLocDWO();
232 
233  /// Get a pointer to the parsed DebugAranges object.
234  const DWARFDebugAranges *getDebugAranges();
235 
236  /// Get a pointer to the parsed frame information object.
237  const DWARFDebugFrame *getDebugFrame();
238 
239  /// Get a pointer to the parsed eh frame information object.
240  const DWARFDebugFrame *getEHFrame();
241 
242  /// Get a pointer to the parsed DebugMacro object.
243  const DWARFDebugMacro *getDebugMacro();
244 
245  /// Get a reference to the parsed accelerator table object.
246  const DWARFAcceleratorTable &getAppleNames();
247 
248  /// Get a reference to the parsed accelerator table object.
249  const DWARFAcceleratorTable &getAppleTypes();
250 
251  /// Get a reference to the parsed accelerator table object.
252  const DWARFAcceleratorTable &getAppleNamespaces();
253 
254  /// Get a reference to the parsed accelerator table object.
255  const DWARFAcceleratorTable &getAppleObjC();
256 
257  /// Get a pointer to a parsed line table corresponding to a compile unit.
258  const DWARFDebugLine::LineTable *getLineTableForUnit(DWARFUnit *cu);
259 
260  /// Wraps the returned DIEs for a given address.
261  struct DIEsForAddress {
262  DWARFCompileUnit *CompileUnit = nullptr;
265  explicit operator bool() const { return CompileUnit != nullptr; }
266  };
267 
268  /// Get the compilation unit, the function DIE and lexical block DIE for the
269  /// given address where applicable.
270  DIEsForAddress getDIEsForAddress(uint64_t Address);
271 
272  DILineInfo getLineInfoForAddress(uint64_t Address,
273  DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
274  DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
275  DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
276  DIInliningInfo getInliningInfoForAddress(uint64_t Address,
277  DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
278 
279  bool isLittleEndian() const { return DObj->isLittleEndian(); }
280  static bool isSupportedVersion(unsigned version) {
281  return version == 2 || version == 3 || version == 4 || version == 5;
282  }
283 
284  std::shared_ptr<DWARFContext> getDWOContext(StringRef AbsolutePath);
285 
286  const MCRegisterInfo *getRegisterInfo() const { return RegInfo.get(); }
287 
288  /// Function used to handle default error reporting policy. Prints a error
289  /// message and returns Continue, so DWARF context ignores the error.
290  static ErrorPolicy defaultErrorHandler(Error E);
291  static std::unique_ptr<DWARFContext>
292  create(const object::ObjectFile &Obj, const LoadedObjectInfo *L = nullptr,
293  function_ref<ErrorPolicy(Error)> HandleError = defaultErrorHandler,
294  std::string DWPName = "");
295 
296  static std::unique_ptr<DWARFContext>
297  create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
298  uint8_t AddrSize, bool isLittleEndian = sys::IsLittleEndianHost);
299 
300  /// Loads register info for the architecture of the provided object file.
301  /// Improves readability of dumped DWARF expressions. Requires the caller to
302  /// have initialized the relevant target descriptions.
303  Error loadRegisterInfo(const object::ObjectFile &Obj);
304 
305 private:
306  /// Return the compile unit that includes an offset (relative to .debug_info).
307  DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
308 
309  /// Return the compile unit which contains instruction with provided
310  /// address.
311  DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
312 };
313 
314 } // end namespace llvm
315 
316 #endif // LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H
This implements the Apple accelerator table format, a precursor of the DWARF 5 accelerator table form...
DWARFCompileUnit * getDWOCompileUnitAtIndex(unsigned index)
Get the compile unit at the specified index for the DWO compile units.
Definition: DWARFContext.h:200
LLVMContext & Context
A parsed .debug_frame or .eh_frame section.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:136
ErrorPolicy
Used as a return value for a error callback passed to DWARF context.
Definition: DWARFContext.h:54
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
unsigned getNumDWOCompileUnits()
Get the number of compile units in the DWO context.
Definition: DWARFContext.h:182
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:89
This class is the base class for all object file types.
Definition: ObjectFile.h:189
unsigned getNumTypeUnits()
Get the number of compile units in this context.
Definition: DWARFContext.h:176
unsigned getMaxVersion() const
Definition: DWARFContext.h:210
std::unique_ptr< const DWARFObject > DObj
Definition: DWARFContext.h:113
A format-neutral container for source line information.
Definition: DIContext.h:31
unsigned getNumCompileUnits()
Get the number of compile units in this context.
Definition: DWARFContext.h:170
const MCRegisterInfo * getRegisterInfo() const
Definition: DWARFContext.h:286
unsigned getNumDWOTypeUnits()
Get the number of compile units in the DWO context.
Definition: DWARFContext.h:188
static const bool IsLittleEndianHost
Definition: Host.h:50
DIContextKind getKind() const
Definition: DIContext.h:188
DWARFCompileUnit * getCompileUnitAtIndex(unsigned index)
Get the compile unit at the specified index for this compile unit.
Definition: DWARFContext.h:194
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:153
Utility class that carries the DWARF compile/type unit and the debug info entry in an object...
Definition: DWARFDie.h:43
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
Controls which fields of DILineInfo container should be filled with data.
Definition: DIContext.h:117
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:116
A format-neutral container for inlined code description.
Definition: DIContext.h:77
bool verify(const TargetRegisterInfo &TRI) const
Check that information hold by this instance make sense for the given TRI.
const MCRegisterDesc & get(unsigned RegNo) const
Provide a get method, equivalent to [], but more useful with a pointer to this object.
void dump(raw_ostream &OS, DIDumpOptions DumpOpts) override
Definition: DWARFContext.h:134
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:864
Concrete instance of DWARFUnitSection, specialized for one Unit type.
Definition: DWARFUnit.h:71
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:59
tu_section_iterator_range dwo_type_unit_sections()
Get type units in the DWO context.
Definition: DWARFContext.h:164
A range adaptor for a pair of iterators.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:224
An inferface for inquiring the load address of a loaded object file to be used by the DIContext imple...
Definition: DIContext.h:211
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:120
static bool isSupportedVersion(unsigned version)
Definition: DWARFContext.h:280
Wraps the returned DIEs for a given address.
Definition: DWARFContext.h:261
cu_iterator_range compile_units()
Get compile units in this context.
Definition: DWARFContext.h:146
bool isLittleEndian() const
Definition: DWARFContext.h:279
const DWARFObject & getDWARFObj() const
Definition: DWARFContext.h:123
static bool classof(const DIContext *DICtx)
Definition: DWARFContext.h:125
cu_iterator_range dwo_compile_units()
Get compile units in the DWO context.
Definition: DWARFContext.h:158
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
tu_section_iterator_range type_unit_sections()
Get type units in this context.
Definition: DWARFContext.h:152
const uint64_t Version
Definition: InstrProf.h:867
void setMaxVersionIfGreater(unsigned Version)
Definition: DWARFContext.h:212