blob: 1af9c8001bb99ccda83ed1b4d71acd807e0b06a6 [file] [log] [blame]
Eugene Zelenkoe94042c2017-02-27 23:43:14 +00001//===- DWARFDie.cpp -------------------------------------------------------===//
Greg Claytonc8c10322016-12-13 18:25:19 +00002//
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
Chandler Carruth6bda14b2017-06-06 11:49:48 +000010#include "llvm/DebugInfo/DWARF/DWARFDie.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000011#include "SyntaxHighlighting.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000012#include "llvm/ADT/None.h"
13#include "llvm/ADT/Optional.h"
14#include "llvm/ADT/StringRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000015#include "llvm/BinaryFormat/Dwarf.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000016#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000017#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000018#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
Reid Klecknera0587362017-08-29 21:41:21 +000019#include "llvm/DebugInfo/DWARF/DWARFExpression.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000020#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000021#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
George Rimar6957ab52017-08-15 12:32:54 +000022#include "llvm/Object/ObjectFile.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000023#include "llvm/Support/DataExtractor.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000024#include "llvm/Support/Format.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000025#include "llvm/Support/MathExtras.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000026#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000027#include <algorithm>
28#include <cassert>
29#include <cinttypes>
30#include <cstdint>
31#include <string>
32#include <utility>
Greg Claytonc8c10322016-12-13 18:25:19 +000033
34using namespace llvm;
35using namespace dwarf;
George Rimar6957ab52017-08-15 12:32:54 +000036using namespace object;
Greg Claytonc8c10322016-12-13 18:25:19 +000037using namespace syntax;
38
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000039static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
Greg Claytonc8c10322016-12-13 18:25:19 +000040 OS << " (";
41 do {
42 uint64_t Shift = countTrailingZeros(Val);
43 assert(Shift < 64 && "undefined behavior");
44 uint64_t Bit = 1ULL << Shift;
45 auto PropName = ApplePropertyString(Bit);
46 if (!PropName.empty())
47 OS << PropName;
48 else
49 OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
50 if (!(Val ^= Bit))
51 break;
52 OS << ", ";
53 } while (true);
54 OS << ")";
55}
56
George Rimar6957ab52017-08-15 12:32:54 +000057static void dumpRanges(const DWARFObject &Obj, raw_ostream &OS,
58 const DWARFAddressRangesVector &Ranges,
59 unsigned AddressSize, unsigned Indent,
60 const DIDumpOptions &DumpOpts) {
George Rimare1c30f72017-08-15 15:54:43 +000061 ArrayRef<SectionName> SectionNames;
Jonas Devlieghere27476ce2017-09-13 09:43:05 +000062 if (DumpOpts.Verbose)
George Rimare1c30f72017-08-15 15:54:43 +000063 SectionNames = Obj.getSectionNames();
George Rimar6957ab52017-08-15 12:32:54 +000064
65 for (size_t I = 0; I < Ranges.size(); ++I) {
66 const DWARFAddressRange &R = Ranges[I];
67
Greg Claytonc8c10322016-12-13 18:25:19 +000068 OS << '\n';
69 OS.indent(Indent);
George Rimar6957ab52017-08-15 12:32:54 +000070 OS << format("[0x%0*" PRIx64 " - 0x%0*" PRIx64 ")", AddressSize * 2,
71 R.LowPC, AddressSize * 2, R.HighPC);
72
73 if (SectionNames.empty() || R.SectionIndex == -1ULL)
74 continue;
75
George Rimare1c30f72017-08-15 15:54:43 +000076 StringRef Name = SectionNames[R.SectionIndex].Name;
77 OS << " \"" << Name << '\"';
George Rimar6957ab52017-08-15 12:32:54 +000078
George Rimare1c30f72017-08-15 15:54:43 +000079 // Print section index if name is not unique.
80 if (!SectionNames[R.SectionIndex].IsNameUnique)
George Rimare5269432017-08-15 16:42:21 +000081 OS << format(" [%" PRIu64 "]", R.SectionIndex);
Greg Claytonc8c10322016-12-13 18:25:19 +000082 }
83}
84
Reid Klecknera0587362017-08-29 21:41:21 +000085static void dumpLocation(raw_ostream &OS, DWARFFormValue &FormValue,
Jonas Devlieghere27476ce2017-09-13 09:43:05 +000086 DWARFUnit *U, unsigned Indent,
87 DIDumpOptions DumpOpts) {
Reid Klecknera0587362017-08-29 21:41:21 +000088 DWARFContext &Ctx = U->getContext();
89 const DWARFObject &Obj = Ctx.getDWARFObj();
90 const MCRegisterInfo *MRI = Ctx.getRegisterInfo();
91 if (FormValue.isFormClass(DWARFFormValue::FC_Block) ||
92 FormValue.isFormClass(DWARFFormValue::FC_Exprloc)) {
93 ArrayRef<uint8_t> Expr = *FormValue.getAsBlock();
94 DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()),
95 Ctx.isLittleEndian(), 0);
96 DWARFExpression(Data, U->getVersion(), U->getAddressByteSize())
97 .print(OS, MRI);
98 return;
99 }
100
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000101 FormValue.dump(OS, DumpOpts);
Reid Klecknera0587362017-08-29 21:41:21 +0000102 if (FormValue.isFormClass(DWARFFormValue::FC_SectionOffset)) {
103 const DWARFSection &LocSection = Obj.getLocSection();
104 const DWARFSection &LocDWOSection = Obj.getLocDWOSection();
105 uint32_t Offset = *FormValue.getAsSectionOffset();
106
107 if (!LocSection.Data.empty()) {
108 DWARFDebugLoc DebugLoc;
109 DWARFDataExtractor Data(Obj, LocSection, Ctx.isLittleEndian(),
110 Obj.getAddressSize());
111 auto LL = DebugLoc.parseOneLocationList(Data, &Offset);
112 if (LL)
113 LL->dump(OS, Ctx.isLittleEndian(), Obj.getAddressSize(), MRI, Indent);
114 else
115 OS << "error extracting location list.";
116 } else if (!LocDWOSection.Data.empty()) {
117 DataExtractor Data(LocDWOSection.Data, Ctx.isLittleEndian(), 0);
118 auto LL = DWARFDebugLocDWO::parseOneLocationList(Data, &Offset);
119 if (LL)
120 LL->dump(OS, Ctx.isLittleEndian(), Obj.getAddressSize(), MRI, Indent);
121 else
122 OS << "error extracting location list.";
123 }
124 }
125}
126
Greg Claytonc8c10322016-12-13 18:25:19 +0000127static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
128 uint32_t *OffsetPtr, dwarf::Attribute Attr,
Adrian Prantl318d1192017-06-06 23:28:45 +0000129 dwarf::Form Form, unsigned Indent,
130 DIDumpOptions DumpOpts) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000131 if (!Die.isValid())
132 return;
133 const char BaseIndent[] = " ";
134 OS << BaseIndent;
135 OS.indent(Indent+2);
136 auto attrString = AttributeString(Attr);
137 if (!attrString.empty())
138 WithColor(OS, syntax::Attribute) << attrString;
139 else
140 WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", Attr);
Adrian Prantl318d1192017-06-06 23:28:45 +0000141
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000142 if (DumpOpts.Verbose) {
Adrian Prantl318d1192017-06-06 23:28:45 +0000143 auto formString = FormEncodingString(Form);
144 if (!formString.empty())
145 OS << " [" << formString << ']';
146 else
147 OS << format(" [DW_FORM_Unknown_%x]", Form);
148 }
149
Greg Claytonc8c10322016-12-13 18:25:19 +0000150 DWARFUnit *U = Die.getDwarfUnit();
151 DWARFFormValue formValue(Form);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000152
Greg Claytonc8c10322016-12-13 18:25:19 +0000153 if (!formValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr, U))
154 return;
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000155
Greg Claytonc8c10322016-12-13 18:25:19 +0000156 OS << "\t(";
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000157
Greg Claytonc8c10322016-12-13 18:25:19 +0000158 StringRef Name;
159 std::string File;
160 auto Color = syntax::Enumerator;
161 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
162 Color = syntax::String;
163 if (const auto *LT = U->getContext().getLineTableForUnit(U))
164 if (LT->getFileNameByIndex(formValue.getAsUnsignedConstant().getValue(), U->getCompilationDir(), DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
165 File = '"' + File + '"';
166 Name = File;
167 }
168 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
169 Name = AttributeValueString(Attr, *Val);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000170
Greg Claytonc8c10322016-12-13 18:25:19 +0000171 if (!Name.empty())
172 WithColor(OS, Color) << Name;
173 else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
174 OS << *formValue.getAsUnsignedConstant();
Reid Klecknera0587362017-08-29 21:41:21 +0000175 else if (Attr == DW_AT_location || Attr == DW_AT_frame_base ||
176 Attr == DW_AT_data_member_location)
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000177 dumpLocation(OS, formValue, U, sizeof(BaseIndent) + Indent + 4, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000178 else
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000179 formValue.dump(OS, DumpOpts);
180
Greg Claytonc8c10322016-12-13 18:25:19 +0000181 // We have dumped the attribute raw value. For some attributes
182 // having both the raw value and the pretty-printed value is
183 // interesting. These attributes are handled below.
Jonas Devlieghere4942a0b2017-08-22 21:59:46 +0000184 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000185 if (const char *Name = Die.getAttributeValueAsReferencedDie(Attr).getName(DINameKind::LinkageName))
186 OS << " \"" << Name << '\"';
187 } else if (Attr == DW_AT_APPLE_property_attribute) {
188 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
189 dumpApplePropertyAttribute(OS, *OptVal);
190 } else if (Attr == DW_AT_ranges) {
George Rimar6957ab52017-08-15 12:32:54 +0000191 const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
192 dumpRanges(Obj, OS, Die.getAddressRanges(), U->getAddressByteSize(),
193 sizeof(BaseIndent) + Indent + 4, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000194 }
George Rimar6957ab52017-08-15 12:32:54 +0000195
Greg Claytonc8c10322016-12-13 18:25:19 +0000196 OS << ")\n";
197}
198
Greg Claytonc8c10322016-12-13 18:25:19 +0000199bool DWARFDie::isSubprogramDIE() const {
200 return getTag() == DW_TAG_subprogram;
201}
202
203bool DWARFDie::isSubroutineDIE() const {
204 auto Tag = getTag();
205 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
206}
207
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000208Optional<DWARFFormValue>
Greg Clayton97d22182017-01-13 21:08:18 +0000209DWARFDie::find(dwarf::Attribute Attr) const {
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000210 if (!isValid())
211 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000212 auto AbbrevDecl = getAbbreviationDeclarationPtr();
213 if (AbbrevDecl)
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000214 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
215 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000216}
217
Greg Clayton97d22182017-01-13 21:08:18 +0000218Optional<DWARFFormValue>
Greg Claytonc109bbe2017-01-13 22:32:12 +0000219DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const {
220 if (!isValid())
221 return None;
222 auto AbbrevDecl = getAbbreviationDeclarationPtr();
223 if (AbbrevDecl) {
224 for (auto Attr : Attrs) {
225 if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
226 return Value;
227 }
228 }
229 return None;
230}
231
232Optional<DWARFFormValue>
233DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const {
234 if (!isValid())
235 return None;
David Blaikie1914c822017-03-13 21:46:37 +0000236 auto Die = *this;
237 if (auto Value = Die.find(Attrs))
Greg Claytonc109bbe2017-01-13 22:32:12 +0000238 return Value;
David Blaikie1914c822017-03-13 21:46:37 +0000239 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
240 Die = D;
241 if (auto Value = Die.find(Attrs))
242 return Value;
243 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification))
244 Die = D;
245 if (auto Value = Die.find(Attrs))
246 return Value;
Greg Claytonc109bbe2017-01-13 22:32:12 +0000247 return None;
248}
249
Greg Claytonc8c10322016-12-13 18:25:19 +0000250DWARFDie
251DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000252 auto SpecRef = toReference(find(Attr));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000253 if (SpecRef) {
254 auto SpecUnit = U->getUnitSection().getUnitForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000255 if (SpecUnit)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000256 return SpecUnit->getDIEForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000257 }
258 return DWARFDie();
259}
260
Greg Clayton52fe1f62016-12-14 22:38:08 +0000261Optional<uint64_t>
262DWARFDie::getRangesBaseAttribute() const {
Greg Claytonc109bbe2017-01-13 22:32:12 +0000263 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
Greg Claytonc8c10322016-12-13 18:25:19 +0000264}
265
Greg Clayton2520c9e2016-12-19 20:36:41 +0000266Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000267 if (auto FormValue = find(DW_AT_high_pc)) {
Greg Clayton2520c9e2016-12-19 20:36:41 +0000268 if (auto Address = FormValue->getAsAddress()) {
269 // High PC is an address.
270 return Address;
271 }
272 if (auto Offset = FormValue->getAsUnsignedConstant()) {
273 // High PC is an offset from LowPC.
274 return LowPC + *Offset;
275 }
276 }
277 return None;
278}
Greg Clayton52fe1f62016-12-14 22:38:08 +0000279
George Rimara25d3292017-05-27 18:10:23 +0000280bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
281 uint64_t &SectionIndex) const {
282 auto F = find(DW_AT_low_pc);
283 auto LowPcAddr = toAddress(F);
Greg Clayton2520c9e2016-12-19 20:36:41 +0000284 if (!LowPcAddr)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000285 return false;
Greg Clayton2520c9e2016-12-19 20:36:41 +0000286 if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
287 LowPC = *LowPcAddr;
288 HighPC = *HighPcAddr;
George Rimara25d3292017-05-27 18:10:23 +0000289 SectionIndex = F->getSectionIndex();
Greg Clayton2520c9e2016-12-19 20:36:41 +0000290 return true;
291 }
292 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000293}
294
295DWARFAddressRangesVector
296DWARFDie::getAddressRanges() const {
297 if (isNULL())
298 return DWARFAddressRangesVector();
299 // Single range specified by low/high PC.
George Rimara25d3292017-05-27 18:10:23 +0000300 uint64_t LowPC, HighPC, Index;
301 if (getLowAndHighPC(LowPC, HighPC, Index))
302 return {{LowPC, HighPC, Index}};
George Rimar4671f2e2017-05-16 12:30:59 +0000303
Greg Claytonc8c10322016-12-13 18:25:19 +0000304 // Multiple ranges from .debug_ranges section.
Greg Clayton97d22182017-01-13 21:08:18 +0000305 auto RangesOffset = toSectionOffset(find(DW_AT_ranges));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000306 if (RangesOffset) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000307 DWARFDebugRangeList RangeList;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000308 if (U->extractRangeList(*RangesOffset, RangeList))
Greg Claytonc8c10322016-12-13 18:25:19 +0000309 return RangeList.getAbsoluteRanges(U->getBaseAddress());
310 }
311 return DWARFAddressRangesVector();
312}
313
314void
315DWARFDie::collectChildrenAddressRanges(DWARFAddressRangesVector& Ranges) const {
316 if (isNULL())
317 return;
318 if (isSubprogramDIE()) {
319 const auto &DIERanges = getAddressRanges();
320 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
321 }
322
Greg Clayton93e4fe82017-01-05 23:47:37 +0000323 for (auto Child: children())
Greg Claytonc8c10322016-12-13 18:25:19 +0000324 Child.collectChildrenAddressRanges(Ranges);
Greg Claytonc8c10322016-12-13 18:25:19 +0000325}
326
327bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
328 for (const auto& R : getAddressRanges()) {
George Rimar4671f2e2017-05-16 12:30:59 +0000329 if (R.LowPC <= Address && Address < R.HighPC)
Greg Claytonc8c10322016-12-13 18:25:19 +0000330 return true;
331 }
332 return false;
333}
334
335const char *
336DWARFDie::getSubroutineName(DINameKind Kind) const {
337 if (!isSubroutineDIE())
338 return nullptr;
339 return getName(Kind);
340}
341
342const char *
343DWARFDie::getName(DINameKind Kind) const {
344 if (!isValid() || Kind == DINameKind::None)
345 return nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000346 // Try to get mangled name only if it was asked for.
347 if (Kind == DINameKind::LinkageName) {
Greg Claytonc109bbe2017-01-13 22:32:12 +0000348 if (auto Name = dwarf::toString(findRecursively({DW_AT_MIPS_linkage_name,
349 DW_AT_linkage_name}), nullptr))
Greg Clayton97d22182017-01-13 21:08:18 +0000350 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000351 }
Greg Clayton97d22182017-01-13 21:08:18 +0000352 if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr))
353 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000354 return nullptr;
355}
356
David Blaikieefc4eba2017-02-06 20:19:02 +0000357uint64_t DWARFDie::getDeclLine() const {
358 return toUnsigned(findRecursively(DW_AT_decl_line), 0);
359}
360
Greg Claytonc8c10322016-12-13 18:25:19 +0000361void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
Dehao Chenef700d52017-04-17 20:10:39 +0000362 uint32_t &CallColumn,
363 uint32_t &CallDiscriminator) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000364 CallFile = toUnsigned(find(DW_AT_call_file), 0);
365 CallLine = toUnsigned(find(DW_AT_call_line), 0);
366 CallColumn = toUnsigned(find(DW_AT_call_column), 0);
Dehao Chenef700d52017-04-17 20:10:39 +0000367 CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
Greg Claytonc8c10322016-12-13 18:25:19 +0000368}
369
Adrian Prantl318d1192017-06-06 23:28:45 +0000370void DWARFDie::dump(raw_ostream &OS, unsigned RecurseDepth, unsigned Indent,
371 DIDumpOptions DumpOpts) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000372 if (!isValid())
373 return;
Paul Robinson17536b92017-06-29 16:52:08 +0000374 DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
Greg Claytonc8c10322016-12-13 18:25:19 +0000375 const uint32_t Offset = getOffset();
376 uint32_t offset = Offset;
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000377
Greg Claytonc8c10322016-12-13 18:25:19 +0000378 if (debug_info_data.isValidOffset(offset)) {
379 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
380 WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000381
Greg Claytonc8c10322016-12-13 18:25:19 +0000382 if (abbrCode) {
383 auto AbbrevDecl = getAbbreviationDeclarationPtr();
384 if (AbbrevDecl) {
385 auto tagString = TagString(getTag());
386 if (!tagString.empty())
387 WithColor(OS, syntax::Tag).get().indent(Indent) << tagString;
388 else
389 WithColor(OS, syntax::Tag).get().indent(Indent)
390 << format("DW_TAG_Unknown_%x", getTag());
Adrian Prantl318d1192017-06-06 23:28:45 +0000391
Jonas Devlieghere27476ce2017-09-13 09:43:05 +0000392 if (DumpOpts.Verbose)
Adrian Prantl318d1192017-06-06 23:28:45 +0000393 OS << format(" [%u] %c", abbrCode,
394 AbbrevDecl->hasChildren() ? '*' : ' ');
395 OS << '\n';
396
Greg Claytonc8c10322016-12-13 18:25:19 +0000397 // Dump all data in the DIE for the attributes.
398 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
Victor Leschuk96d99812017-02-25 13:15:57 +0000399 if (AttrSpec.Form == DW_FORM_implicit_const) {
400 // We are dumping .debug_info section ,
401 // implicit_const attribute values are not really stored here,
402 // but in .debug_abbrev section. So we just skip such attrs.
403 continue;
404 }
Greg Claytonc8c10322016-12-13 18:25:19 +0000405 dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
Adrian Prantl318d1192017-06-06 23:28:45 +0000406 Indent, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000407 }
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000408
Greg Claytonc8c10322016-12-13 18:25:19 +0000409 DWARFDie child = getFirstChild();
410 if (RecurseDepth > 0 && child) {
411 while (child) {
Adrian Prantl318d1192017-06-06 23:28:45 +0000412 child.dump(OS, RecurseDepth-1, Indent+2, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000413 child = child.getSibling();
414 }
415 }
416 } else {
417 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
418 << abbrCode << '\n';
419 }
420 } else {
421 OS.indent(Indent) << "NULL\n";
422 }
423 }
424}
425
Adrian Prantl3d523a62017-08-16 17:43:01 +0000426LLVM_DUMP_METHOD void DWARFDie::dump() const { dump(llvm::errs(), 0); }
427
Greg Clayton78a07bf2016-12-21 21:37:06 +0000428DWARFDie DWARFDie::getParent() const {
429 if (isValid())
430 return U->getParent(Die);
431 return DWARFDie();
432}
433
434DWARFDie DWARFDie::getSibling() const {
435 if (isValid())
436 return U->getSibling(Die);
437 return DWARFDie();
438}
Greg Clayton0e62ee72017-01-13 00:13:42 +0000439
440iterator_range<DWARFDie::attribute_iterator>
441DWARFDie::attributes() const {
442 return make_range(attribute_iterator(*this, false),
443 attribute_iterator(*this, true));
444}
445
446DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End) :
447 Die(D), AttrValue(0), Index(0) {
448 auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
449 assert(AbbrDecl && "Must have abbreviation declaration");
450 if (End) {
451 // This is the end iterator so we set the index to the attribute count.
452 Index = AbbrDecl->getNumAttributes();
453 } else {
454 // This is the begin iterator so we extract the value for this->Index.
455 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
456 updateForIndex(*AbbrDecl, 0);
457 }
458}
459
460void DWARFDie::attribute_iterator::updateForIndex(
461 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
462 Index = I;
Hiroshi Inoueddb34d82017-07-03 06:32:59 +0000463 // AbbrDecl must be valid before calling this function.
Greg Clayton0e62ee72017-01-13 00:13:42 +0000464 auto NumAttrs = AbbrDecl.getNumAttributes();
465 if (Index < NumAttrs) {
466 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
467 // Add the previous byte size of any previous attribute value.
468 AttrValue.Offset += AttrValue.ByteSize;
469 AttrValue.Value.setForm(AbbrDecl.getFormByIndex(Index));
470 uint32_t ParseOffset = AttrValue.Offset;
471 auto U = Die.getDwarfUnit();
472 assert(U && "Die must have valid DWARF unit");
473 bool b = AttrValue.Value.extractValue(U->getDebugInfoExtractor(),
474 &ParseOffset, U);
475 (void)b;
476 assert(b && "extractValue cannot fail on fully parsed DWARF");
477 AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
478 } else {
479 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
480 AttrValue.clear();
481 }
482}
483
484DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
485 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
486 updateForIndex(*AbbrDecl, Index + 1);
487 return *this;
488}