blob: 9aaa4dd53c96ac257212fe590dfb7fc577b2be4a [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"
Greg Claytonc8c10322016-12-13 18:25:19 +000019#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000020#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
George Rimar6957ab52017-08-15 12:32:54 +000021#include "llvm/Object/ObjectFile.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000022#include "llvm/Support/DataExtractor.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000023#include "llvm/Support/Format.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000024#include "llvm/Support/MathExtras.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000025#include "llvm/Support/raw_ostream.h"
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000026#include <algorithm>
27#include <cassert>
28#include <cinttypes>
29#include <cstdint>
30#include <string>
31#include <utility>
Greg Claytonc8c10322016-12-13 18:25:19 +000032
33using namespace llvm;
34using namespace dwarf;
George Rimar6957ab52017-08-15 12:32:54 +000035using namespace object;
Greg Claytonc8c10322016-12-13 18:25:19 +000036using namespace syntax;
37
Eugene Zelenkoe94042c2017-02-27 23:43:14 +000038static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
Greg Claytonc8c10322016-12-13 18:25:19 +000039 OS << " (";
40 do {
41 uint64_t Shift = countTrailingZeros(Val);
42 assert(Shift < 64 && "undefined behavior");
43 uint64_t Bit = 1ULL << Shift;
44 auto PropName = ApplePropertyString(Bit);
45 if (!PropName.empty())
46 OS << PropName;
47 else
48 OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
49 if (!(Val ^= Bit))
50 break;
51 OS << ", ";
52 } while (true);
53 OS << ")";
54}
55
George Rimar6957ab52017-08-15 12:32:54 +000056static void dumpRanges(const DWARFObject &Obj, raw_ostream &OS,
57 const DWARFAddressRangesVector &Ranges,
58 unsigned AddressSize, unsigned Indent,
59 const DIDumpOptions &DumpOpts) {
George Rimare1c30f72017-08-15 15:54:43 +000060 ArrayRef<SectionName> SectionNames;
61 if (!DumpOpts.Brief)
62 SectionNames = Obj.getSectionNames();
George Rimar6957ab52017-08-15 12:32:54 +000063
64 for (size_t I = 0; I < Ranges.size(); ++I) {
65 const DWARFAddressRange &R = Ranges[I];
66
Greg Claytonc8c10322016-12-13 18:25:19 +000067 OS << '\n';
68 OS.indent(Indent);
George Rimar6957ab52017-08-15 12:32:54 +000069 OS << format("[0x%0*" PRIx64 " - 0x%0*" PRIx64 ")", AddressSize * 2,
70 R.LowPC, AddressSize * 2, R.HighPC);
71
72 if (SectionNames.empty() || R.SectionIndex == -1ULL)
73 continue;
74
George Rimare1c30f72017-08-15 15:54:43 +000075 StringRef Name = SectionNames[R.SectionIndex].Name;
76 OS << " \"" << Name << '\"';
George Rimar6957ab52017-08-15 12:32:54 +000077
George Rimare1c30f72017-08-15 15:54:43 +000078 // Print section index if name is not unique.
79 if (!SectionNames[R.SectionIndex].IsNameUnique)
George Rimare5269432017-08-15 16:42:21 +000080 OS << format(" [%" PRIu64 "]", R.SectionIndex);
Greg Claytonc8c10322016-12-13 18:25:19 +000081 }
82}
83
84static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
85 uint32_t *OffsetPtr, dwarf::Attribute Attr,
Adrian Prantl318d1192017-06-06 23:28:45 +000086 dwarf::Form Form, unsigned Indent,
87 DIDumpOptions DumpOpts) {
Greg Claytonc8c10322016-12-13 18:25:19 +000088 if (!Die.isValid())
89 return;
90 const char BaseIndent[] = " ";
91 OS << BaseIndent;
92 OS.indent(Indent+2);
93 auto attrString = AttributeString(Attr);
94 if (!attrString.empty())
95 WithColor(OS, syntax::Attribute) << attrString;
96 else
97 WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", Attr);
Adrian Prantl318d1192017-06-06 23:28:45 +000098
99 if (!DumpOpts.Brief) {
100 auto formString = FormEncodingString(Form);
101 if (!formString.empty())
102 OS << " [" << formString << ']';
103 else
104 OS << format(" [DW_FORM_Unknown_%x]", Form);
105 }
106
Greg Claytonc8c10322016-12-13 18:25:19 +0000107 DWARFUnit *U = Die.getDwarfUnit();
108 DWARFFormValue formValue(Form);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000109
Greg Claytonc8c10322016-12-13 18:25:19 +0000110 if (!formValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr, U))
111 return;
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000112
Greg Claytonc8c10322016-12-13 18:25:19 +0000113 OS << "\t(";
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000114
Greg Claytonc8c10322016-12-13 18:25:19 +0000115 StringRef Name;
116 std::string File;
117 auto Color = syntax::Enumerator;
118 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
119 Color = syntax::String;
120 if (const auto *LT = U->getContext().getLineTableForUnit(U))
121 if (LT->getFileNameByIndex(formValue.getAsUnsignedConstant().getValue(), U->getCompilationDir(), DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
122 File = '"' + File + '"';
123 Name = File;
124 }
125 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
126 Name = AttributeValueString(Attr, *Val);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000127
Greg Claytonc8c10322016-12-13 18:25:19 +0000128 if (!Name.empty())
129 WithColor(OS, Color) << Name;
130 else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
131 OS << *formValue.getAsUnsignedConstant();
132 else
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000133 formValue.dump(OS, DumpOpts);
134
Greg Claytonc8c10322016-12-13 18:25:19 +0000135 // We have dumped the attribute raw value. For some attributes
136 // having both the raw value and the pretty-printed value is
137 // interesting. These attributes are handled below.
Jonas Devliegheref456d182017-08-22 21:41:49 +0000138 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin ||
139 Attr == DW_AT_type) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000140 if (const char *Name = Die.getAttributeValueAsReferencedDie(Attr).getName(DINameKind::LinkageName))
141 OS << " \"" << Name << '\"';
142 } else if (Attr == DW_AT_APPLE_property_attribute) {
143 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
144 dumpApplePropertyAttribute(OS, *OptVal);
145 } else if (Attr == DW_AT_ranges) {
George Rimar6957ab52017-08-15 12:32:54 +0000146 const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
147 dumpRanges(Obj, OS, Die.getAddressRanges(), U->getAddressByteSize(),
148 sizeof(BaseIndent) + Indent + 4, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000149 }
George Rimar6957ab52017-08-15 12:32:54 +0000150
Greg Claytonc8c10322016-12-13 18:25:19 +0000151 OS << ")\n";
152}
153
Greg Claytonc8c10322016-12-13 18:25:19 +0000154bool DWARFDie::isSubprogramDIE() const {
155 return getTag() == DW_TAG_subprogram;
156}
157
158bool DWARFDie::isSubroutineDIE() const {
159 auto Tag = getTag();
160 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
161}
162
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000163Optional<DWARFFormValue>
Greg Clayton97d22182017-01-13 21:08:18 +0000164DWARFDie::find(dwarf::Attribute Attr) const {
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000165 if (!isValid())
166 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000167 auto AbbrevDecl = getAbbreviationDeclarationPtr();
168 if (AbbrevDecl)
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000169 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
170 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000171}
172
Greg Clayton97d22182017-01-13 21:08:18 +0000173Optional<DWARFFormValue>
Greg Claytonc109bbe2017-01-13 22:32:12 +0000174DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const {
175 if (!isValid())
176 return None;
177 auto AbbrevDecl = getAbbreviationDeclarationPtr();
178 if (AbbrevDecl) {
179 for (auto Attr : Attrs) {
180 if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
181 return Value;
182 }
183 }
184 return None;
185}
186
187Optional<DWARFFormValue>
188DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const {
189 if (!isValid())
190 return None;
David Blaikie1914c822017-03-13 21:46:37 +0000191 auto Die = *this;
192 if (auto Value = Die.find(Attrs))
Greg Claytonc109bbe2017-01-13 22:32:12 +0000193 return Value;
David Blaikie1914c822017-03-13 21:46:37 +0000194 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
195 Die = D;
196 if (auto Value = Die.find(Attrs))
197 return Value;
198 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification))
199 Die = D;
200 if (auto Value = Die.find(Attrs))
201 return Value;
Greg Claytonc109bbe2017-01-13 22:32:12 +0000202 return None;
203}
204
Greg Claytonc8c10322016-12-13 18:25:19 +0000205DWARFDie
206DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000207 auto SpecRef = toReference(find(Attr));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000208 if (SpecRef) {
209 auto SpecUnit = U->getUnitSection().getUnitForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000210 if (SpecUnit)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000211 return SpecUnit->getDIEForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000212 }
213 return DWARFDie();
214}
215
Greg Clayton52fe1f62016-12-14 22:38:08 +0000216Optional<uint64_t>
217DWARFDie::getRangesBaseAttribute() const {
Greg Claytonc109bbe2017-01-13 22:32:12 +0000218 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
Greg Claytonc8c10322016-12-13 18:25:19 +0000219}
220
Greg Clayton2520c9e2016-12-19 20:36:41 +0000221Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000222 if (auto FormValue = find(DW_AT_high_pc)) {
Greg Clayton2520c9e2016-12-19 20:36:41 +0000223 if (auto Address = FormValue->getAsAddress()) {
224 // High PC is an address.
225 return Address;
226 }
227 if (auto Offset = FormValue->getAsUnsignedConstant()) {
228 // High PC is an offset from LowPC.
229 return LowPC + *Offset;
230 }
231 }
232 return None;
233}
Greg Clayton52fe1f62016-12-14 22:38:08 +0000234
George Rimara25d3292017-05-27 18:10:23 +0000235bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
236 uint64_t &SectionIndex) const {
237 auto F = find(DW_AT_low_pc);
238 auto LowPcAddr = toAddress(F);
Greg Clayton2520c9e2016-12-19 20:36:41 +0000239 if (!LowPcAddr)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000240 return false;
Greg Clayton2520c9e2016-12-19 20:36:41 +0000241 if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
242 LowPC = *LowPcAddr;
243 HighPC = *HighPcAddr;
George Rimara25d3292017-05-27 18:10:23 +0000244 SectionIndex = F->getSectionIndex();
Greg Clayton2520c9e2016-12-19 20:36:41 +0000245 return true;
246 }
247 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000248}
249
250DWARFAddressRangesVector
251DWARFDie::getAddressRanges() const {
252 if (isNULL())
253 return DWARFAddressRangesVector();
254 // Single range specified by low/high PC.
George Rimara25d3292017-05-27 18:10:23 +0000255 uint64_t LowPC, HighPC, Index;
256 if (getLowAndHighPC(LowPC, HighPC, Index))
257 return {{LowPC, HighPC, Index}};
George Rimar4671f2e2017-05-16 12:30:59 +0000258
Greg Claytonc8c10322016-12-13 18:25:19 +0000259 // Multiple ranges from .debug_ranges section.
Greg Clayton97d22182017-01-13 21:08:18 +0000260 auto RangesOffset = toSectionOffset(find(DW_AT_ranges));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000261 if (RangesOffset) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000262 DWARFDebugRangeList RangeList;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000263 if (U->extractRangeList(*RangesOffset, RangeList))
Greg Claytonc8c10322016-12-13 18:25:19 +0000264 return RangeList.getAbsoluteRanges(U->getBaseAddress());
265 }
266 return DWARFAddressRangesVector();
267}
268
269void
270DWARFDie::collectChildrenAddressRanges(DWARFAddressRangesVector& Ranges) const {
271 if (isNULL())
272 return;
273 if (isSubprogramDIE()) {
274 const auto &DIERanges = getAddressRanges();
275 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
276 }
277
Greg Clayton93e4fe82017-01-05 23:47:37 +0000278 for (auto Child: children())
Greg Claytonc8c10322016-12-13 18:25:19 +0000279 Child.collectChildrenAddressRanges(Ranges);
Greg Claytonc8c10322016-12-13 18:25:19 +0000280}
281
282bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
283 for (const auto& R : getAddressRanges()) {
George Rimar4671f2e2017-05-16 12:30:59 +0000284 if (R.LowPC <= Address && Address < R.HighPC)
Greg Claytonc8c10322016-12-13 18:25:19 +0000285 return true;
286 }
287 return false;
288}
289
290const char *
291DWARFDie::getSubroutineName(DINameKind Kind) const {
292 if (!isSubroutineDIE())
293 return nullptr;
294 return getName(Kind);
295}
296
297const char *
298DWARFDie::getName(DINameKind Kind) const {
299 if (!isValid() || Kind == DINameKind::None)
300 return nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000301 // Try to get mangled name only if it was asked for.
302 if (Kind == DINameKind::LinkageName) {
Greg Claytonc109bbe2017-01-13 22:32:12 +0000303 if (auto Name = dwarf::toString(findRecursively({DW_AT_MIPS_linkage_name,
304 DW_AT_linkage_name}), nullptr))
Greg Clayton97d22182017-01-13 21:08:18 +0000305 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000306 }
Greg Clayton97d22182017-01-13 21:08:18 +0000307 if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr))
308 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000309 return nullptr;
310}
311
David Blaikieefc4eba2017-02-06 20:19:02 +0000312uint64_t DWARFDie::getDeclLine() const {
313 return toUnsigned(findRecursively(DW_AT_decl_line), 0);
314}
315
Greg Claytonc8c10322016-12-13 18:25:19 +0000316void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
Dehao Chenef700d52017-04-17 20:10:39 +0000317 uint32_t &CallColumn,
318 uint32_t &CallDiscriminator) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000319 CallFile = toUnsigned(find(DW_AT_call_file), 0);
320 CallLine = toUnsigned(find(DW_AT_call_line), 0);
321 CallColumn = toUnsigned(find(DW_AT_call_column), 0);
Dehao Chenef700d52017-04-17 20:10:39 +0000322 CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
Greg Claytonc8c10322016-12-13 18:25:19 +0000323}
324
Adrian Prantl318d1192017-06-06 23:28:45 +0000325void DWARFDie::dump(raw_ostream &OS, unsigned RecurseDepth, unsigned Indent,
326 DIDumpOptions DumpOpts) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000327 if (!isValid())
328 return;
Paul Robinson17536b92017-06-29 16:52:08 +0000329 DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
Greg Claytonc8c10322016-12-13 18:25:19 +0000330 const uint32_t Offset = getOffset();
331 uint32_t offset = Offset;
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000332
Greg Claytonc8c10322016-12-13 18:25:19 +0000333 if (debug_info_data.isValidOffset(offset)) {
334 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
335 WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000336
Greg Claytonc8c10322016-12-13 18:25:19 +0000337 if (abbrCode) {
338 auto AbbrevDecl = getAbbreviationDeclarationPtr();
339 if (AbbrevDecl) {
340 auto tagString = TagString(getTag());
341 if (!tagString.empty())
342 WithColor(OS, syntax::Tag).get().indent(Indent) << tagString;
343 else
344 WithColor(OS, syntax::Tag).get().indent(Indent)
345 << format("DW_TAG_Unknown_%x", getTag());
Adrian Prantl318d1192017-06-06 23:28:45 +0000346
347 if (!DumpOpts.Brief)
348 OS << format(" [%u] %c", abbrCode,
349 AbbrevDecl->hasChildren() ? '*' : ' ');
350 OS << '\n';
351
Greg Claytonc8c10322016-12-13 18:25:19 +0000352 // Dump all data in the DIE for the attributes.
353 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
Victor Leschuk96d99812017-02-25 13:15:57 +0000354 if (AttrSpec.Form == DW_FORM_implicit_const) {
355 // We are dumping .debug_info section ,
356 // implicit_const attribute values are not really stored here,
357 // but in .debug_abbrev section. So we just skip such attrs.
358 continue;
359 }
Greg Claytonc8c10322016-12-13 18:25:19 +0000360 dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
Adrian Prantl318d1192017-06-06 23:28:45 +0000361 Indent, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000362 }
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000363
Greg Claytonc8c10322016-12-13 18:25:19 +0000364 DWARFDie child = getFirstChild();
365 if (RecurseDepth > 0 && child) {
366 while (child) {
Adrian Prantl318d1192017-06-06 23:28:45 +0000367 child.dump(OS, RecurseDepth-1, Indent+2, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000368 child = child.getSibling();
369 }
370 }
371 } else {
372 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
373 << abbrCode << '\n';
374 }
375 } else {
376 OS.indent(Indent) << "NULL\n";
377 }
378 }
379}
380
Adrian Prantl3d523a62017-08-16 17:43:01 +0000381LLVM_DUMP_METHOD void DWARFDie::dump() const { dump(llvm::errs(), 0); }
382
Greg Clayton78a07bf2016-12-21 21:37:06 +0000383DWARFDie DWARFDie::getParent() const {
384 if (isValid())
385 return U->getParent(Die);
386 return DWARFDie();
387}
388
389DWARFDie DWARFDie::getSibling() const {
390 if (isValid())
391 return U->getSibling(Die);
392 return DWARFDie();
393}
Greg Clayton0e62ee72017-01-13 00:13:42 +0000394
395iterator_range<DWARFDie::attribute_iterator>
396DWARFDie::attributes() const {
397 return make_range(attribute_iterator(*this, false),
398 attribute_iterator(*this, true));
399}
400
401DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End) :
402 Die(D), AttrValue(0), Index(0) {
403 auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
404 assert(AbbrDecl && "Must have abbreviation declaration");
405 if (End) {
406 // This is the end iterator so we set the index to the attribute count.
407 Index = AbbrDecl->getNumAttributes();
408 } else {
409 // This is the begin iterator so we extract the value for this->Index.
410 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
411 updateForIndex(*AbbrDecl, 0);
412 }
413}
414
415void DWARFDie::attribute_iterator::updateForIndex(
416 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
417 Index = I;
Hiroshi Inoueddb34d82017-07-03 06:32:59 +0000418 // AbbrDecl must be valid before calling this function.
Greg Clayton0e62ee72017-01-13 00:13:42 +0000419 auto NumAttrs = AbbrDecl.getNumAttributes();
420 if (Index < NumAttrs) {
421 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
422 // Add the previous byte size of any previous attribute value.
423 AttrValue.Offset += AttrValue.ByteSize;
424 AttrValue.Value.setForm(AbbrDecl.getFormByIndex(Index));
425 uint32_t ParseOffset = AttrValue.Offset;
426 auto U = Die.getDwarfUnit();
427 assert(U && "Die must have valid DWARF unit");
428 bool b = AttrValue.Value.extractValue(U->getDebugInfoExtractor(),
429 &ParseOffset, U);
430 (void)b;
431 assert(b && "extractValue cannot fail on fully parsed DWARF");
432 AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
433 } else {
434 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
435 AttrValue.clear();
436 }
437}
438
439DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
440 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
441 updateForIndex(*AbbrDecl, Index + 1);
442 return *this;
443}