1 //===- DWARFContext.h -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===/
10 #ifndef LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H
11 #define LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H
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"
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/DebugInfo/DIContext.h"
20 #include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
21 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
22 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
23 #include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h"
24 #include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
25 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
26 #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
27 #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
28 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
29 #include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h"
30 #include "llvm/DebugInfo/DWARF/DWARFObject.h"
31 #include "llvm/DebugInfo/DWARF/DWARFSection.h"
32 #include "llvm/DebugInfo/DWARF/DWARFTypeUnit.h"
33 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
34 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
35 #include "llvm/Object/Binary.h"
36 #include "llvm/Object/ObjectFile.h"
37 #include "llvm/Support/DataExtractor.h"
38 #include "llvm/Support/Error.h"
39 #include "llvm/Support/Host.h"
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 };
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 {
60 DWARFUnitSection<DWARFCompileUnit> CUs;
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<DWARFDebugNames> Names;
73 std::unique_ptr<AppleAcceleratorTable> AppleNames;
74 std::unique_ptr<AppleAcceleratorTable> AppleTypes;
75 std::unique_ptr<AppleAcceleratorTable> AppleNamespaces;
76 std::unique_ptr<AppleAcceleratorTable> AppleObjC;
78 DWARFUnitSection<DWARFCompileUnit> DWOCUs;
79 std::deque<DWARFUnitSection<DWARFTypeUnit>> DWOTUs;
80 std::unique_ptr<DWARFDebugAbbrev> AbbrevDWO;
81 std::unique_ptr<DWARFDebugLocDWO> LocDWO;
83 /// The maximum DWARF version of all units.
84 unsigned MaxVersion = 0;
87 object::OwningBinary<object::ObjectFile> File;
88 std::unique_ptr<DWARFContext> Context;
90 StringMap<std::weak_ptr<DWOFile>> DWOFiles;
91 std::weak_ptr<DWOFile> DWP;
92 bool CheckedForDWP = false;
95 std::unique_ptr<MCRegisterInfo> RegInfo;
97 /// Read compile units from the debug_info section (if necessary)
98 /// and store them in CUs.
99 void parseCompileUnits();
101 /// Read type units from the debug_types sections (if necessary)
102 /// and store them in TUs.
103 void parseTypeUnits();
105 /// Read compile units from the debug_info.dwo section (if necessary)
106 /// and store them in DWOCUs.
107 void parseDWOCompileUnits();
109 /// Read type units from the debug_types.dwo section (if necessary)
110 /// and store them in DWOTUs.
111 void parseDWOTypeUnits();
114 std::unique_ptr<const DWARFObject> DObj;
117 DWARFContext(std::unique_ptr<const DWARFObject> DObj,
118 std::string DWPName = "");
121 DWARFContext(DWARFContext &) = delete;
122 DWARFContext &operator=(DWARFContext &) = delete;
124 const DWARFObject &getDWARFObj() const { return *DObj; }
126 static bool classof(const DIContext *DICtx) {
127 return DICtx->getKind() == CK_DWARF;
130 /// Dump a textual representation to \p OS. If any \p DumpOffsets are present,
131 /// dump only the record at the specified offset.
132 void dump(raw_ostream &OS, DIDumpOptions DumpOpts,
133 std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets);
135 void dump(raw_ostream &OS, DIDumpOptions DumpOpts) override {
136 std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets;
137 dump(OS, DumpOpts, DumpOffsets);
140 bool verify(raw_ostream &OS, DIDumpOptions DumpOpts = {}) override;
142 using cu_iterator_range = DWARFUnitSection<DWARFCompileUnit>::iterator_range;
143 using tu_iterator_range = DWARFUnitSection<DWARFTypeUnit>::iterator_range;
144 using tu_section_iterator_range = iterator_range<decltype(TUs)::iterator>;
146 /// Get compile units in this context.
147 cu_iterator_range compile_units() {
149 return cu_iterator_range(CUs.begin(), CUs.end());
152 /// Get type units in this context.
153 tu_section_iterator_range type_unit_sections() {
155 return tu_section_iterator_range(TUs.begin(), TUs.end());
158 /// Get compile units in the DWO context.
159 cu_iterator_range dwo_compile_units() {
160 parseDWOCompileUnits();
161 return cu_iterator_range(DWOCUs.begin(), DWOCUs.end());
164 /// Get type units in the DWO context.
165 tu_section_iterator_range dwo_type_unit_sections() {
167 return tu_section_iterator_range(DWOTUs.begin(), DWOTUs.end());
170 /// Get the number of compile units in this context.
171 unsigned getNumCompileUnits() {
176 /// Get the number of compile units in this context.
177 unsigned getNumTypeUnits() {
182 /// Get the number of compile units in the DWO context.
183 unsigned getNumDWOCompileUnits() {
184 parseDWOCompileUnits();
185 return DWOCUs.size();
188 /// Get the number of compile units in the DWO context.
189 unsigned getNumDWOTypeUnits() {
191 return DWOTUs.size();
194 /// Get the compile unit at the specified index for this compile unit.
195 DWARFCompileUnit *getCompileUnitAtIndex(unsigned index) {
197 return CUs[index].get();
200 /// Get the compile unit at the specified index for the DWO compile units.
201 DWARFCompileUnit *getDWOCompileUnitAtIndex(unsigned index) {
202 parseDWOCompileUnits();
203 return DWOCUs[index].get();
206 DWARFCompileUnit *getDWOCompileUnitForHash(uint64_t Hash);
208 /// Return the compile unit that includes an offset (relative to .debug_info).
209 DWARFCompileUnit *getCompileUnitForOffset(uint32_t Offset);
211 /// Get a DIE given an exact offset.
212 DWARFDie getDIEForOffset(uint32_t Offset);
214 unsigned getMaxVersion() const { return MaxVersion; }
216 void setMaxVersionIfGreater(unsigned Version) {
217 if (Version > MaxVersion)
218 MaxVersion = Version;
221 const DWARFUnitIndex &getCUIndex();
222 DWARFGdbIndex &getGdbIndex();
223 const DWARFUnitIndex &getTUIndex();
225 /// Get a pointer to the parsed DebugAbbrev object.
226 const DWARFDebugAbbrev *getDebugAbbrev();
228 /// Get a pointer to the parsed DebugLoc object.
229 const DWARFDebugLoc *getDebugLoc();
231 /// Get a pointer to the parsed dwo abbreviations object.
232 const DWARFDebugAbbrev *getDebugAbbrevDWO();
234 /// Get a pointer to the parsed DebugLoc object.
235 const DWARFDebugLocDWO *getDebugLocDWO();
237 /// Get a pointer to the parsed DebugAranges object.
238 const DWARFDebugAranges *getDebugAranges();
240 /// Get a pointer to the parsed frame information object.
241 const DWARFDebugFrame *getDebugFrame();
243 /// Get a pointer to the parsed eh frame information object.
244 const DWARFDebugFrame *getEHFrame();
246 /// Get a pointer to the parsed DebugMacro object.
247 const DWARFDebugMacro *getDebugMacro();
249 /// Get a reference to the parsed accelerator table object.
250 const DWARFDebugNames &getDebugNames();
252 /// Get a reference to the parsed accelerator table object.
253 const AppleAcceleratorTable &getAppleNames();
255 /// Get a reference to the parsed accelerator table object.
256 const AppleAcceleratorTable &getAppleTypes();
258 /// Get a reference to the parsed accelerator table object.
259 const AppleAcceleratorTable &getAppleNamespaces();
261 /// Get a reference to the parsed accelerator table object.
262 const AppleAcceleratorTable &getAppleObjC();
264 /// Get a pointer to a parsed line table corresponding to a compile unit.
265 /// Report any parsing issues as warnings on stderr.
266 const DWARFDebugLine::LineTable *getLineTableForUnit(DWARFUnit *U);
268 /// Get a pointer to a parsed line table corresponding to a compile unit.
269 /// Report any recoverable parsing problems using the callback.
270 Expected<const DWARFDebugLine::LineTable *>
271 getLineTableForUnit(DWARFUnit *U,
272 std::function<void(Error)> RecoverableErrorCallback);
274 DataExtractor getStringExtractor() const {
275 return DataExtractor(DObj->getStringSection(), false, 0);
277 DataExtractor getLineStringExtractor() const {
278 return DataExtractor(DObj->getLineStringSection(), false, 0);
281 /// Wraps the returned DIEs for a given address.
282 struct DIEsForAddress {
283 DWARFCompileUnit *CompileUnit = nullptr;
284 DWARFDie FunctionDIE;
286 explicit operator bool() const { return CompileUnit != nullptr; }
289 /// Get the compilation unit, the function DIE and lexical block DIE for the
290 /// given address where applicable.
291 DIEsForAddress getDIEsForAddress(uint64_t Address);
293 DILineInfo getLineInfoForAddress(uint64_t Address,
294 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
295 DILineInfoTable getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
296 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
297 DIInliningInfo getInliningInfoForAddress(uint64_t Address,
298 DILineInfoSpecifier Specifier = DILineInfoSpecifier()) override;
300 bool isLittleEndian() const { return DObj->isLittleEndian(); }
301 static bool isSupportedVersion(unsigned version) {
302 return version == 2 || version == 3 || version == 4 || version == 5;
305 std::shared_ptr<DWARFContext> getDWOContext(StringRef AbsolutePath);
307 const MCRegisterInfo *getRegisterInfo() const { return RegInfo.get(); }
309 /// Function used to handle default error reporting policy. Prints a error
310 /// message and returns Continue, so DWARF context ignores the error.
311 static ErrorPolicy defaultErrorHandler(Error E);
312 static std::unique_ptr<DWARFContext>
313 create(const object::ObjectFile &Obj, const LoadedObjectInfo *L = nullptr,
314 function_ref<ErrorPolicy(Error)> HandleError = defaultErrorHandler,
315 std::string DWPName = "");
317 static std::unique_ptr<DWARFContext>
318 create(const StringMap<std::unique_ptr<MemoryBuffer>> &Sections,
319 uint8_t AddrSize, bool isLittleEndian = sys::IsLittleEndianHost);
321 /// Loads register info for the architecture of the provided object file.
322 /// Improves readability of dumped DWARF expressions. Requires the caller to
323 /// have initialized the relevant target descriptions.
324 Error loadRegisterInfo(const object::ObjectFile &Obj);
326 /// Get address size from CUs.
327 /// TODO: refactor compile_units() to make this const.
328 uint8_t getCUAddrSize();
331 /// Return the compile unit which contains instruction with provided
333 DWARFCompileUnit *getCompileUnitForAddress(uint64_t Address);
336 } // end namespace llvm
338 #endif // LLVM_DEBUGINFO_DWARF_DWARFCONTEXT_H