LLVM  11.0.0git
DWARFContext.h
Go to the documentation of this file.
1 //===- DWARFContext.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 #ifndef LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H
10 #define LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H
11 
12 #include "llvm/ADT/MapVector.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/ADT/StringRef.h"
34 #include "llvm/Object/Binary.h"
35 #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 MCRegisterInfo;
47 class MemoryBuffer;
48 class raw_ostream;
49 
50 /// DWARFContext
51 /// This data structure is the top level entity that deals with dwarf debug
52 /// information parsing. The actual data is supplied through DWARFObj.
53 class DWARFContext : public DIContext {
54  DWARFUnitVector NormalUnits;
55  std::unique_ptr<DWARFUnitIndex> CUIndex;
56  std::unique_ptr<DWARFGdbIndex> GdbIndex;
57  std::unique_ptr<DWARFUnitIndex> TUIndex;
58  std::unique_ptr<DWARFDebugAbbrev> Abbrev;
59  std::unique_ptr<DWARFDebugLoc> Loc;
60  std::unique_ptr<DWARFDebugAranges> Aranges;
61  std::unique_ptr<DWARFDebugLine> Line;
62  std::unique_ptr<DWARFDebugFrame> DebugFrame;
63  std::unique_ptr<DWARFDebugFrame> EHFrame;
64  std::unique_ptr<DWARFDebugMacro> Macro;
65  std::unique_ptr<DWARFDebugMacro> Macinfo;
66  std::unique_ptr<DWARFDebugNames> Names;
67  std::unique_ptr<AppleAcceleratorTable> AppleNames;
68  std::unique_ptr<AppleAcceleratorTable> AppleTypes;
69  std::unique_ptr<AppleAcceleratorTable> AppleNamespaces;
70  std::unique_ptr<AppleAcceleratorTable> AppleObjC;
71 
72  DWARFUnitVector DWOUnits;
73  std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
74  std::unique_ptr<DWARFDebugMacro> MacinfoDWO;
75  std::unique_ptr<DWARFDebugMacro> MacroDWO;
76 
77  /// The maximum DWARF version of all units.
78  unsigned MaxVersion = 0;
79 
80  struct DWOFile {
82  std::unique_ptr<DWARFContext> Context;
83  };
85  std::weak_ptr<DWOFile> DWP;
86  bool CheckedForDWP = false;
87  std::string DWPName;
88 
89  std::unique_ptr<MCRegisterInfo> RegInfo;
90 
91  std::function<void(Error)> RecoverableErrorHandler =
93  std::function<void(Error)> WarningHandler = WithColor::defaultWarningHandler;
94 
95  /// Read compile units from the debug_info section (if necessary)
96  /// and type units from the debug_types sections (if necessary)
97  /// and store them in NormalUnits.
98  void parseNormalUnits();
99 
100  /// Read compile units from the debug_info.dwo section (if necessary)
101  /// and type units from the debug_types.dwo section (if necessary)
102  /// and store them in DWOUnits.
103  /// If \p Lazy is true, set up to parse but don't actually parse them.
104  enum { EagerParse = false, LazyParse = true };
105  void parseDWOUnits(bool Lazy = false);
106 
107  std::unique_ptr<const DWARFObject> DObj;
108 
109  /// Helper enum to distinguish between macro[.dwo] and macinfo[.dwo]
110  /// section.
111  enum MacroSecType {
112  MacinfoSection,
113  MacinfoDwoSection,
114  MacroSection,
115  MacroDwoSection
116  };
117 
118 public:
119  DWARFContext(std::unique_ptr<const DWARFObject> DObj,
120  std::string DWPName = "",
121  std::function<void(Error)> RecoverableErrorHandler =
123  std::function<void(Error)> WarningHandler =
125  ~DWARFContext();
126 
127  DWARFContext(DWARFContext &) = delete;
128  DWARFContext &operator=(DWARFContext &) = delete;
129 
130  const DWARFObject &getDWARFObj() const { return *DObj; }
131 
132  static bool classof(const DIContext *DICtx) {
133  return DICtx->getKind() == CK_DWARF;
134  }
135 
136  /// Dump a textual representation to \p OS. If any \p DumpOffsets are present,
137  /// dump only the record at the specified offset.
138  void dump(raw_ostream &OS, DIDumpOptions DumpOpts,
139  std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets);
140 
141  void dump(raw_ostream &OS, DIDumpOptions DumpOpts) override {
142  std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets;
143  dump(OS, DumpOpts, DumpOffsets);
144  }
145 
146  bool verify(raw_ostream &OS, DIDumpOptions DumpOpts = {}) override;
147 
149 
150  /// Get units from .debug_info in this context.
152  parseNormalUnits();
153  return unit_iterator_range(NormalUnits.begin(),
154  NormalUnits.begin() +
155  NormalUnits.getNumInfoUnits());
156  }
157 
158  /// Get units from .debug_types in this context.
160  parseNormalUnits();
161  return unit_iterator_range(
162  NormalUnits.begin() + NormalUnits.getNumInfoUnits(), NormalUnits.end());
163  }
164 
165  /// Get compile units in this context.
167 
168  /// Get type units in this context.
170 
171  /// Get all normal compile/type units in this context.
173  parseNormalUnits();
174  return unit_iterator_range(NormalUnits.begin(), NormalUnits.end());
175  }
176 
177  /// Get units from .debug_info..dwo in the DWO context.
179  parseDWOUnits();
180  return unit_iterator_range(DWOUnits.begin(),
181  DWOUnits.begin() + DWOUnits.getNumInfoUnits());
182  }
183 
184  /// Get units from .debug_types.dwo in the DWO context.
186  parseDWOUnits();
187  return unit_iterator_range(DWOUnits.begin() + DWOUnits.getNumInfoUnits(),
188  DWOUnits.end());
189  }
190 
191  /// Get compile units in the DWO context.
193 
194  /// Get type units in the DWO context.
196 
197  /// Get all units in the DWO context.
199  parseDWOUnits();
200  return unit_iterator_range(DWOUnits.begin(), DWOUnits.end());
201  }
202 
203  /// Get the number of compile units in this context.
204  unsigned getNumCompileUnits() {
205  parseNormalUnits();
206  return NormalUnits.getNumInfoUnits();
207  }
208 
209  /// Get the number of type units in this context.
210  unsigned getNumTypeUnits() {
211  parseNormalUnits();
212  return NormalUnits.getNumTypesUnits();
213  }
214 
215  /// Get the number of compile units in the DWO context.
217  parseDWOUnits();
218  return DWOUnits.getNumInfoUnits();
219  }
220 
221  /// Get the number of type units in the DWO context.
222  unsigned getNumDWOTypeUnits() {
223  parseDWOUnits();
224  return DWOUnits.getNumTypesUnits();
225  }
226 
227  /// Get the unit at the specified index.
228  DWARFUnit *getUnitAtIndex(unsigned index) {
229  parseNormalUnits();
230  return NormalUnits[index].get();
231  }
232 
233  /// Get the unit at the specified index for the DWO units.
234  DWARFUnit *getDWOUnitAtIndex(unsigned index) {
235  parseDWOUnits();
236  return DWOUnits[index].get();
237  }
238 
240 
241  /// Return the compile unit that includes an offset (relative to .debug_info).
243 
244  /// Get a DIE given an exact offset.
245  DWARFDie getDIEForOffset(uint64_t Offset);
246 
247  unsigned getMaxVersion() {
248  // Ensure info units have been parsed to discover MaxVersion
250  return MaxVersion;
251  }
252 
253  unsigned getMaxDWOVersion() {
254  // Ensure DWO info units have been parsed to discover MaxVersion
256  return MaxVersion;
257  }
258 
260  if (Version > MaxVersion)
261  MaxVersion = Version;
262  }
263 
264  const DWARFUnitIndex &getCUIndex();
266  const DWARFUnitIndex &getTUIndex();
267 
268  /// Get a pointer to the parsed DebugAbbrev object.
270 
271  /// Get a pointer to the parsed DebugLoc object.
272  const DWARFDebugLoc *getDebugLoc();
273 
274  /// Get a pointer to the parsed dwo abbreviations object.
276 
277  /// Get a pointer to the parsed DebugAranges object.
279 
280  /// Get a pointer to the parsed frame information object.
282 
283  /// Get a pointer to the parsed eh frame information object.
285 
286  /// Get a pointer to the parsed DebugMacinfo information object.
288 
289  /// Get a pointer to the parsed DebugMacinfoDWO information object.
291 
292  /// Get a pointer to the parsed DebugMacro information object.
294 
295  /// Get a pointer to the parsed DebugMacroDWO information object.
297 
298  /// Get a reference to the parsed accelerator table object.
300 
301  /// Get a reference to the parsed accelerator table object.
303 
304  /// Get a reference to the parsed accelerator table object.
306 
307  /// Get a reference to the parsed accelerator table object.
309 
310  /// Get a reference to the parsed accelerator table object.
312 
313  /// Get a pointer to a parsed line table corresponding to a compile unit.
314  /// Report any parsing issues as warnings on stderr.
316 
317  /// Get a pointer to a parsed line table corresponding to a compile unit.
318  /// Report any recoverable parsing problems using the handler.
321  function_ref<void(Error)> RecoverableErrorHandler);
322 
324  return DataExtractor(DObj->getStrSection(), false, 0);
325  }
327  return DataExtractor(DObj->getStrDWOSection(), false, 0);
328  }
330  return DataExtractor(DObj->getLineStrSection(), false, 0);
331  }
332 
333  /// Wraps the returned DIEs for a given address.
334  struct DIEsForAddress {
338  explicit operator bool() const { return CompileUnit != nullptr; }
339  };
340 
341  /// Get the compilation unit, the function DIE and lexical block DIE for the
342  /// given address where applicable.
343  /// TODO: change input parameter from "uint64_t Address"
344  /// into "SectionedAddress Address"
346 
348  object::SectionedAddress Address,
349  DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
351  object::SectionedAddress Address, uint64_t Size,
352  DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
354  object::SectionedAddress Address,
355  DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
356 
357  std::vector<DILocal>
359 
360  bool isLittleEndian() const { return DObj->isLittleEndian(); }
361  static bool isSupportedVersion(unsigned version) {
362  return version == 2 || version == 3 || version == 4 || version == 5;
363  }
364 
365  static bool isAddressSizeSupported(unsigned AddressSize) {
366  return AddressSize == 2 || AddressSize == 4 || AddressSize == 8;
367  }
368 
369  std::shared_ptr<DWARFContext> getDWOContext(StringRef AbsolutePath);
370 
371  const MCRegisterInfo *getRegisterInfo() const { return RegInfo.get(); }
372 
374  return RecoverableErrorHandler;
375  }
376 
377  function_ref<void(Error)> getWarningHandler() { return WarningHandler; }
378 
379  static std::unique_ptr<DWARFContext>
380  create(const object::ObjectFile &Obj, const LoadedObjectInfo *L = nullptr,
381  std::string DWPName = "",
382  std::function<void(Error)> RecoverableErrorHandler =
384  std::function<void(Error)> WarningHandler =
386 
387  static std::unique_ptr<DWARFContext>
388  create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
389  uint8_t AddrSize, bool isLittleEndian = sys::IsLittleEndianHost,
390  std::function<void(Error)> RecoverableErrorHandler =
392  std::function<void(Error)> WarningHandler =
394 
395  /// Loads register info for the architecture of the provided object file.
396  /// Improves readability of dumped DWARF expressions. Requires the caller to
397  /// have initialized the relevant target descriptions.
399 
400  /// Get address size from CUs.
401  /// TODO: refactor compile_units() to make this const.
402  uint8_t getCUAddrSize();
403 
405  return getDWARFObj().getFile()->getArch();
406  }
407 
408  /// Return the compile unit which contains instruction with provided
409  /// address.
410  /// TODO: change input parameter from "uint64_t Address"
411  /// into "SectionedAddress Address"
412  DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
413 
414 private:
415  /// Parse a macro[.dwo] or macinfo[.dwo] section.
416  std::unique_ptr<DWARFDebugMacro>
417  parseMacroOrMacinfo(MacroSecType SectionType);
418 
419  void addLocalsForDie(DWARFCompileUnit *CU, DWARFDie Subprogram, DWARFDie Die,
420  std::vector<DILocal> &Result);
421 };
422 
423 } // end namespace llvm
424 
425 #endif // LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H
const DWARFUnitIndex & getTUIndex()
DWARFUnit * getDWOUnitAtIndex(unsigned index)
Get the unit at the specified index for the DWO units.
Definition: DWARFContext.h:234
unsigned getMaxDWOVersion()
Definition: DWARFContext.h:253
LLVMContext & Context
unit_iterator_range dwo_type_units()
Get type units in the DWO context.
Definition: DWARFContext.h:195
This class represents lattice values for constants.
Definition: AllocatorList.h:23
const DWARFDebugLoc * getDebugLoc()
Get a pointer to the parsed DebugLoc object.
Stores all information relating to a compile unit, be it in its original instance in the object file ...
const AppleAcceleratorTable & getAppleNamespaces()
Get a reference to the parsed accelerator table object.
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
DWARFGdbIndex & getGdbIndex()
std::shared_ptr< DWARFContext > getDWOContext(StringRef AbsolutePath)
unsigned getNumDWOCompileUnits()
Get the number of compile units in the DWO context.
Definition: DWARFContext.h:216
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:176
uint8_t getCUAddrSize()
Get address size from CUs.
DIEsForAddress getDIEsForAddress(uint64_t Address)
Get the compilation unit, the function DIE and lexical block DIE for the given address where applicab...
This class is the base class for all object file types.
Definition: ObjectFile.h:225
const DWARFDebugLine::LineTable * getLineTableForUnit(DWARFUnit *U)
Get a pointer to a parsed line table corresponding to a compile unit.
unsigned getNumTypeUnits()
Get the number of type units in this context.
Definition: DWARFContext.h:210
unit_iterator_range dwo_info_section_units()
Get units from .debug_info..dwo in the DWO context.
Definition: DWARFContext.h:178
DataExtractor getStringDWOExtractor() const
Definition: DWARFContext.h:326
const DWARFDebugAbbrev * getDebugAbbrevDWO()
Get a pointer to the parsed dwo abbreviations object.
static void defaultErrorHandler(Error Err)
Implement default handling for Error.
Definition: WithColor.cpp:136
A format-neutral container for source line information.
Definition: DIContext.h:31
Triple::ArchType getArch() const
Definition: DWARFContext.h:404
unit_iterator_range normal_units()
Get all normal compile/type units in this context.
Definition: DWARFContext.h:172
unsigned getNumCompileUnits()
Get the number of compile units in this context.
Definition: DWARFContext.h:204
unit_iterator_range compile_units()
Get compile units in this context.
Definition: DWARFContext.h:166
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
const MCRegisterInfo * getRegisterInfo() const
Definition: DWARFContext.h:371
unsigned getNumDWOTypeUnits()
Get the number of type units in the DWO context.
Definition: DWARFContext.h:222
Expected< const DWARFDebugFrame * > getEHFrame()
Get a pointer to the parsed eh frame information object.
DataExtractor getStringExtractor() const
Definition: DWARFContext.h:323
DWARFDie getDIEForOffset(uint64_t Offset)
Get a DIE given an exact offset.
static const bool IsLittleEndianHost
bool verify(raw_ostream &OS, DIDumpOptions DumpOpts={}) override
llvm::iterator_range< typename UnitVector::iterator > iterator_range
Definition: DWARFUnit.h:128
DIContextKind getKind() const
Definition: DIContext.h:228
const DWARFDebugAranges * getDebugAranges()
Get a pointer to the parsed DebugAranges object.
const DWARFDebugAbbrev * getDebugAbbrev()
Get a pointer to the parsed DebugAbbrev object.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Error loadRegisterInfo(const object::ObjectFile &Obj)
Loads register info for the architecture of the provided object file.
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:181
unit_iterator_range info_section_units()
Get units from .debug_info in this context.
Definition: DWARFContext.h:151
Utility class that carries the DWARF compile/type unit and the debug info entry in an object...
Definition: DWARFDie.h:43
unsigned getMaxVersion()
Definition: DWARFContext.h:247
This implements the Apple accelerator table format, a precursor of the DWARF 5 accelerator table form...
unit_iterator_range dwo_types_section_units()
Get units from .debug_types.dwo in the DWO context.
Definition: DWARFContext.h:185
Controls which fields of DILineInfo container should be filled with data.
Definition: DIContext.h:136
Instrumentation for Order File
const DWARFDebugMacro * getDebugMacro()
Get a pointer to the parsed DebugMacro information object.
A format-neutral container for inlined code description.
Definition: DIContext.h:82
const DWARFDebugMacro * getDebugMacinfo()
Get a pointer to the parsed DebugMacinfo information object.
DataExtractor getLineStringExtractor() const
Definition: DWARFContext.h:329
DWARFContext(std::unique_ptr< const DWARFObject > DObj, std::string DWPName="", std::function< void(Error)> RecoverableErrorHandler=WithColor::defaultErrorHandler, std::function< void(Error)> WarningHandler=WithColor::defaultWarningHandler)
const DWARFDebugMacro * getDebugMacroDWO()
Get a pointer to the parsed DebugMacroDWO information object.
function_ref< void(Error)> getWarningHandler()
Definition: DWARFContext.h:377
const AppleAcceleratorTable & getAppleObjC()
Get a reference to the parsed accelerator table object.
static std::unique_ptr< DWARFContext > create(const object::ObjectFile &Obj, const LoadedObjectInfo *L=nullptr, std::string DWPName="", std::function< void(Error)> RecoverableErrorHandler=WithColor::defaultErrorHandler, std::function< void(Error)> WarningHandler=WithColor::defaultWarningHandler)
const AppleAcceleratorTable & getAppleTypes()
Get a reference to the parsed accelerator table object.
unit_iterator_range dwo_compile_units()
Get compile units in the DWO context.
Definition: DWARFContext.h:192
std::vector< DILocal > getLocalsForAddress(object::SectionedAddress Address) override
.debug_names section consists of one or more units.
DWARFCompileUnit * getDWOCompileUnitForHash(uint64_t Hash)
DIInliningInfo getInliningInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
unit_iterator_range type_units()
Get type units in this context.
Definition: DWARFContext.h:169
void dump(raw_ostream &OS, DIDumpOptions DumpOpts) override
Definition: DWARFContext.h:141
static bool isAddressSizeSupported(unsigned AddressSize)
Definition: DWARFContext.h:365
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:883
virtual Triple::ArchType getArch() const =0
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:53
Describe a collection of units.
Definition: DWARFUnit.h:118
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:106
An inferface for inquiring the load address of a loaded object file to be used by the DIContext imple...
Definition: DIContext.h:257
unit_iterator_range types_section_units()
Get units from .debug_types in this context.
Definition: DWARFContext.h:159
static bool isSupportedVersion(unsigned version)
Definition: DWARFContext.h:361
const DWARFDebugNames & getDebugNames()
Get a reference to the parsed accelerator table object.
const DWARFDebugMacro * getDebugMacinfoDWO()
Get a pointer to the parsed DebugMacinfoDWO information object.
const AppleAcceleratorTable & getAppleNames()
Get a reference to the parsed accelerator table object.
Wraps the returned DIEs for a given address.
Definition: DWARFContext.h:334
unit_iterator_range dwo_units()
Get all units in the DWO context.
Definition: DWARFContext.h:198
DWARFUnit * getUnitAtIndex(unsigned index)
Get the unit at the specified index.
Definition: DWARFContext.h:228
DILineInfo getLineInfoForAddress(object::SectionedAddress Address, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
uint32_t Size
Definition: Profile.cpp:46
void dump(raw_ostream &OS, DIDumpOptions DumpOpts, std::array< Optional< uint64_t >, DIDT_ID_Count > DumpOffsets)
Dump a textual representation to OS.
DILineInfoTable getLineInfoForAddressRange(object::SectionedAddress Address, uint64_t Size, DILineInfoSpecifier Specifier=DILineInfoSpecifier()) override
bool isLittleEndian() const
Definition: DWARFContext.h:360
const DWARFObject & getDWARFObj() const
Definition: DWARFContext.h:130
static bool classof(const DIContext *DICtx)
Definition: DWARFContext.h:132
const DWARFUnitIndex & getCUIndex()
DWARFCompileUnit * getCompileUnitForAddress(uint64_t Address)
Return the compile unit which contains instruction with provided address.
const MCRegisterDesc & get(MCRegister RegNo) const
Provide a get method, equivalent to [], but more useful with a pointer to this object.
static void defaultWarningHandler(Error Warning)
Implement default handling for Warning.
Definition: WithColor.cpp:142
function_ref< void(Error)> getRecoverableErrorHandler()
Definition: DWARFContext.h:373
DWARFUnitVector::iterator_range unit_iterator_range
Definition: DWARFContext.h:148
virtual const object::ObjectFile * getFile() const
Definition: DWARFObject.h:31
unsigned getNumTypesUnits() const
Returns number of units from all .debug_types[.dwo] sections.
Definition: DWARFUnit.h:158
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
Expected< const DWARFDebugFrame * > getDebugFrame()
Get a pointer to the parsed frame information object.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
print Print MemDeps of function
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
DWARFCompileUnit * getCompileUnitForOffset(uint64_t Offset)
Return the compile unit that includes an offset (relative to .debug_info).
SectionType
These are the section type and attributes fields.
Definition: MachO.h:119
DWARFContext & operator=(DWARFContext &)=delete
const uint64_t Version
Definition: InstrProf.h:988
void setMaxVersionIfGreater(unsigned Version)
Definition: DWARFContext.h:259
unsigned getNumInfoUnits() const
Returns number of units from all .debug_info[.dwo] sections.
Definition: DWARFUnit.h:154