blob: 261a5cabffeeb450c78adb9c3cc065702e9547e6 [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;
62 if (!DumpOpts.Brief)
63 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,
86 DWARFUnit *U, unsigned Indent) {
87 DWARFContext &Ctx = U->getContext();
88 const DWARFObject &Obj = Ctx.getDWARFObj();
89 const MCRegisterInfo *MRI = Ctx.getRegisterInfo();
90 if (FormValue.isFormClass(DWARFFormValue::FC_Block) ||
91 FormValue.isFormClass(DWARFFormValue::FC_Exprloc)) {
92 ArrayRef<uint8_t> Expr = *FormValue.getAsBlock();
93 DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()),
94 Ctx.isLittleEndian(), 0);
95 DWARFExpression(Data, U->getVersion(), U->getAddressByteSize())
96 .print(OS, MRI);
97 return;
98 }
99
100 FormValue.dump(OS);
101 if (FormValue.isFormClass(DWARFFormValue::FC_SectionOffset)) {
102 const DWARFSection &LocSection = Obj.getLocSection();
103 const DWARFSection &LocDWOSection = Obj.getLocDWOSection();
104 uint32_t Offset = *FormValue.getAsSectionOffset();
105
106 if (!LocSection.Data.empty()) {
107 DWARFDebugLoc DebugLoc;
108 DWARFDataExtractor Data(Obj, LocSection, Ctx.isLittleEndian(),
109 Obj.getAddressSize());
110 auto LL = DebugLoc.parseOneLocationList(Data, &Offset);
111 if (LL)
112 LL->dump(OS, Ctx.isLittleEndian(), Obj.getAddressSize(), MRI, Indent);
113 else
114 OS << "error extracting location list.";
115 } else if (!LocDWOSection.Data.empty()) {
116 DataExtractor Data(LocDWOSection.Data, Ctx.isLittleEndian(), 0);
117 auto LL = DWARFDebugLocDWO::parseOneLocationList(Data, &Offset);
118 if (LL)
119 LL->dump(OS, Ctx.isLittleEndian(), Obj.getAddressSize(), MRI, Indent);
120 else
121 OS << "error extracting location list.";
122 }
123 }
124}
125
Greg Claytonc8c10322016-12-13 18:25:19 +0000126static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
127 uint32_t *OffsetPtr, dwarf::Attribute Attr,
Adrian Prantl318d1192017-06-06 23:28:45 +0000128 dwarf::Form Form, unsigned Indent,
129 DIDumpOptions DumpOpts) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000130 if (!Die.isValid())
131 return;
132 const char BaseIndent[] = " ";
133 OS << BaseIndent;
134 OS.indent(Indent+2);
135 auto attrString = AttributeString(Attr);
136 if (!attrString.empty())
137 WithColor(OS, syntax::Attribute) << attrString;
138 else
139 WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", Attr);
Adrian Prantl318d1192017-06-06 23:28:45 +0000140
141 if (!DumpOpts.Brief) {
142 auto formString = FormEncodingString(Form);
143 if (!formString.empty())
144 OS << " [" << formString << ']';
145 else
146 OS << format(" [DW_FORM_Unknown_%x]", Form);
147 }
148
Greg Claytonc8c10322016-12-13 18:25:19 +0000149 DWARFUnit *U = Die.getDwarfUnit();
150 DWARFFormValue formValue(Form);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000151
Greg Claytonc8c10322016-12-13 18:25:19 +0000152 if (!formValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr, U))
153 return;
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000154
Greg Claytonc8c10322016-12-13 18:25:19 +0000155 OS << "\t(";
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000156
Greg Claytonc8c10322016-12-13 18:25:19 +0000157 StringRef Name;
158 std::string File;
159 auto Color = syntax::Enumerator;
160 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
161 Color = syntax::String;
162 if (const auto *LT = U->getContext().getLineTableForUnit(U))
163 if (LT->getFileNameByIndex(formValue.getAsUnsignedConstant().getValue(), U->getCompilationDir(), DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
164 File = '"' + File + '"';
165 Name = File;
166 }
167 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
168 Name = AttributeValueString(Attr, *Val);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000169
Greg Claytonc8c10322016-12-13 18:25:19 +0000170 if (!Name.empty())
171 WithColor(OS, Color) << Name;
172 else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
173 OS << *formValue.getAsUnsignedConstant();
Reid Klecknera0587362017-08-29 21:41:21 +0000174 else if (Attr == DW_AT_location || Attr == DW_AT_frame_base ||
175 Attr == DW_AT_data_member_location)
176 dumpLocation(OS, formValue, U, sizeof(BaseIndent) + Indent + 4);
Greg Claytonc8c10322016-12-13 18:25:19 +0000177 else
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000178 formValue.dump(OS, DumpOpts);
179
Greg Claytonc8c10322016-12-13 18:25:19 +0000180 // We have dumped the attribute raw value. For some attributes
181 // having both the raw value and the pretty-printed value is
182 // interesting. These attributes are handled below.
Jonas Devlieghere4942a0b2017-08-22 21:59:46 +0000183 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000184 if (const char *Name = Die.getAttributeValueAsReferencedDie(Attr).getName(DINameKind::LinkageName))
185 OS << " \"" << Name << '\"';
186 } else if (Attr == DW_AT_APPLE_property_attribute) {
187 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
188 dumpApplePropertyAttribute(OS, *OptVal);
189 } else if (Attr == DW_AT_ranges) {
George Rimar6957ab52017-08-15 12:32:54 +0000190 const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
191 dumpRanges(Obj, OS, Die.getAddressRanges(), U->getAddressByteSize(),
192 sizeof(BaseIndent) + Indent + 4, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000193 }
George Rimar6957ab52017-08-15 12:32:54 +0000194
Greg Claytonc8c10322016-12-13 18:25:19 +0000195 OS << ")\n";
196}
197
Greg Claytonc8c10322016-12-13 18:25:19 +0000198bool DWARFDie::isSubprogramDIE() const {
199 return getTag() == DW_TAG_subprogram;
200}
201
202bool DWARFDie::isSubroutineDIE() const {
203 auto Tag = getTag();
204 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
205}
206
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000207Optional<DWARFFormValue>
Greg Clayton97d22182017-01-13 21:08:18 +0000208DWARFDie::find(dwarf::Attribute Attr) const {
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000209 if (!isValid())
210 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000211 auto AbbrevDecl = getAbbreviationDeclarationPtr();
212 if (AbbrevDecl)
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000213 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
214 return None;
Greg Claytonc8c10322016-12-13 18:25:19 +0000215}
216
Greg Clayton97d22182017-01-13 21:08:18 +0000217Optional<DWARFFormValue>
Greg Claytonc109bbe2017-01-13 22:32:12 +0000218DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const {
219 if (!isValid())
220 return None;
221 auto AbbrevDecl = getAbbreviationDeclarationPtr();
222 if (AbbrevDecl) {
223 for (auto Attr : Attrs) {
224 if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
225 return Value;
226 }
227 }
228 return None;
229}
230
231Optional<DWARFFormValue>
232DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const {
233 if (!isValid())
234 return None;
David Blaikie1914c822017-03-13 21:46:37 +0000235 auto Die = *this;
236 if (auto Value = Die.find(Attrs))
Greg Claytonc109bbe2017-01-13 22:32:12 +0000237 return Value;
David Blaikie1914c822017-03-13 21:46:37 +0000238 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
239 Die = D;
240 if (auto Value = Die.find(Attrs))
241 return Value;
242 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification))
243 Die = D;
244 if (auto Value = Die.find(Attrs))
245 return Value;
Greg Claytonc109bbe2017-01-13 22:32:12 +0000246 return None;
247}
248
Greg Claytonc8c10322016-12-13 18:25:19 +0000249DWARFDie
250DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000251 auto SpecRef = toReference(find(Attr));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000252 if (SpecRef) {
253 auto SpecUnit = U->getUnitSection().getUnitForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000254 if (SpecUnit)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000255 return SpecUnit->getDIEForOffset(*SpecRef);
Greg Claytonc8c10322016-12-13 18:25:19 +0000256 }
257 return DWARFDie();
258}
259
Greg Clayton52fe1f62016-12-14 22:38:08 +0000260Optional<uint64_t>
261DWARFDie::getRangesBaseAttribute() const {
Greg Claytonc109bbe2017-01-13 22:32:12 +0000262 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
Greg Claytonc8c10322016-12-13 18:25:19 +0000263}
264
Greg Clayton2520c9e2016-12-19 20:36:41 +0000265Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000266 if (auto FormValue = find(DW_AT_high_pc)) {
Greg Clayton2520c9e2016-12-19 20:36:41 +0000267 if (auto Address = FormValue->getAsAddress()) {
268 // High PC is an address.
269 return Address;
270 }
271 if (auto Offset = FormValue->getAsUnsignedConstant()) {
272 // High PC is an offset from LowPC.
273 return LowPC + *Offset;
274 }
275 }
276 return None;
277}
Greg Clayton52fe1f62016-12-14 22:38:08 +0000278
George Rimara25d3292017-05-27 18:10:23 +0000279bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
280 uint64_t &SectionIndex) const {
281 auto F = find(DW_AT_low_pc);
282 auto LowPcAddr = toAddress(F);
Greg Clayton2520c9e2016-12-19 20:36:41 +0000283 if (!LowPcAddr)
Greg Clayton52fe1f62016-12-14 22:38:08 +0000284 return false;
Greg Clayton2520c9e2016-12-19 20:36:41 +0000285 if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
286 LowPC = *LowPcAddr;
287 HighPC = *HighPcAddr;
George Rimara25d3292017-05-27 18:10:23 +0000288 SectionIndex = F->getSectionIndex();
Greg Clayton2520c9e2016-12-19 20:36:41 +0000289 return true;
290 }
291 return false;
Greg Claytonc8c10322016-12-13 18:25:19 +0000292}
293
294DWARFAddressRangesVector
295DWARFDie::getAddressRanges() const {
296 if (isNULL())
297 return DWARFAddressRangesVector();
298 // Single range specified by low/high PC.
George Rimara25d3292017-05-27 18:10:23 +0000299 uint64_t LowPC, HighPC, Index;
300 if (getLowAndHighPC(LowPC, HighPC, Index))
301 return {{LowPC, HighPC, Index}};
George Rimar4671f2e2017-05-16 12:30:59 +0000302
Greg Claytonc8c10322016-12-13 18:25:19 +0000303 // Multiple ranges from .debug_ranges section.
Greg Clayton97d22182017-01-13 21:08:18 +0000304 auto RangesOffset = toSectionOffset(find(DW_AT_ranges));
Greg Clayton52fe1f62016-12-14 22:38:08 +0000305 if (RangesOffset) {
Greg Claytonc8c10322016-12-13 18:25:19 +0000306 DWARFDebugRangeList RangeList;
Greg Clayton52fe1f62016-12-14 22:38:08 +0000307 if (U->extractRangeList(*RangesOffset, RangeList))
Greg Claytonc8c10322016-12-13 18:25:19 +0000308 return RangeList.getAbsoluteRanges(U->getBaseAddress());
309 }
310 return DWARFAddressRangesVector();
311}
312
313void
314DWARFDie::collectChildrenAddressRanges(DWARFAddressRangesVector& Ranges) const {
315 if (isNULL())
316 return;
317 if (isSubprogramDIE()) {
318 const auto &DIERanges = getAddressRanges();
319 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
320 }
321
Greg Clayton93e4fe82017-01-05 23:47:37 +0000322 for (auto Child: children())
Greg Claytonc8c10322016-12-13 18:25:19 +0000323 Child.collectChildrenAddressRanges(Ranges);
Greg Claytonc8c10322016-12-13 18:25:19 +0000324}
325
326bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
327 for (const auto& R : getAddressRanges()) {
George Rimar4671f2e2017-05-16 12:30:59 +0000328 if (R.LowPC <= Address && Address < R.HighPC)
Greg Claytonc8c10322016-12-13 18:25:19 +0000329 return true;
330 }
331 return false;
332}
333
334const char *
335DWARFDie::getSubroutineName(DINameKind Kind) const {
336 if (!isSubroutineDIE())
337 return nullptr;
338 return getName(Kind);
339}
340
341const char *
342DWARFDie::getName(DINameKind Kind) const {
343 if (!isValid() || Kind == DINameKind::None)
344 return nullptr;
Greg Claytonc8c10322016-12-13 18:25:19 +0000345 // Try to get mangled name only if it was asked for.
346 if (Kind == DINameKind::LinkageName) {
Greg Claytonc109bbe2017-01-13 22:32:12 +0000347 if (auto Name = dwarf::toString(findRecursively({DW_AT_MIPS_linkage_name,
348 DW_AT_linkage_name}), nullptr))
Greg Clayton97d22182017-01-13 21:08:18 +0000349 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000350 }
Greg Clayton97d22182017-01-13 21:08:18 +0000351 if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr))
352 return Name;
Greg Claytonc8c10322016-12-13 18:25:19 +0000353 return nullptr;
354}
355
David Blaikieefc4eba2017-02-06 20:19:02 +0000356uint64_t DWARFDie::getDeclLine() const {
357 return toUnsigned(findRecursively(DW_AT_decl_line), 0);
358}
359
Greg Claytonc8c10322016-12-13 18:25:19 +0000360void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
Dehao Chenef700d52017-04-17 20:10:39 +0000361 uint32_t &CallColumn,
362 uint32_t &CallDiscriminator) const {
Greg Clayton97d22182017-01-13 21:08:18 +0000363 CallFile = toUnsigned(find(DW_AT_call_file), 0);
364 CallLine = toUnsigned(find(DW_AT_call_line), 0);
365 CallColumn = toUnsigned(find(DW_AT_call_column), 0);
Dehao Chenef700d52017-04-17 20:10:39 +0000366 CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
Greg Claytonc8c10322016-12-13 18:25:19 +0000367}
368
Adrian Prantl318d1192017-06-06 23:28:45 +0000369void DWARFDie::dump(raw_ostream &OS, unsigned RecurseDepth, unsigned Indent,
370 DIDumpOptions DumpOpts) const {
Greg Claytonc8c10322016-12-13 18:25:19 +0000371 if (!isValid())
372 return;
Paul Robinson17536b92017-06-29 16:52:08 +0000373 DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
Greg Claytonc8c10322016-12-13 18:25:19 +0000374 const uint32_t Offset = getOffset();
375 uint32_t offset = Offset;
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000376
Greg Claytonc8c10322016-12-13 18:25:19 +0000377 if (debug_info_data.isValidOffset(offset)) {
378 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
379 WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset);
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000380
Greg Claytonc8c10322016-12-13 18:25:19 +0000381 if (abbrCode) {
382 auto AbbrevDecl = getAbbreviationDeclarationPtr();
383 if (AbbrevDecl) {
384 auto tagString = TagString(getTag());
385 if (!tagString.empty())
386 WithColor(OS, syntax::Tag).get().indent(Indent) << tagString;
387 else
388 WithColor(OS, syntax::Tag).get().indent(Indent)
389 << format("DW_TAG_Unknown_%x", getTag());
Adrian Prantl318d1192017-06-06 23:28:45 +0000390
391 if (!DumpOpts.Brief)
392 OS << format(" [%u] %c", abbrCode,
393 AbbrevDecl->hasChildren() ? '*' : ' ');
394 OS << '\n';
395
Greg Claytonc8c10322016-12-13 18:25:19 +0000396 // Dump all data in the DIE for the attributes.
397 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
Victor Leschuk96d99812017-02-25 13:15:57 +0000398 if (AttrSpec.Form == DW_FORM_implicit_const) {
399 // We are dumping .debug_info section ,
400 // implicit_const attribute values are not really stored here,
401 // but in .debug_abbrev section. So we just skip such attrs.
402 continue;
403 }
Greg Claytonc8c10322016-12-13 18:25:19 +0000404 dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
Adrian Prantl318d1192017-06-06 23:28:45 +0000405 Indent, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000406 }
Jonas Devliegherea2faf7b2017-08-18 21:35:44 +0000407
Greg Claytonc8c10322016-12-13 18:25:19 +0000408 DWARFDie child = getFirstChild();
409 if (RecurseDepth > 0 && child) {
410 while (child) {
Adrian Prantl318d1192017-06-06 23:28:45 +0000411 child.dump(OS, RecurseDepth-1, Indent+2, DumpOpts);
Greg Claytonc8c10322016-12-13 18:25:19 +0000412 child = child.getSibling();
413 }
414 }
415 } else {
416 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
417 << abbrCode << '\n';
418 }
419 } else {
420 OS.indent(Indent) << "NULL\n";
421 }
422 }
423}
424
Adrian Prantl3d523a62017-08-16 17:43:01 +0000425LLVM_DUMP_METHOD void DWARFDie::dump() const { dump(llvm::errs(), 0); }
426
Greg Clayton78a07bf2016-12-21 21:37:06 +0000427DWARFDie DWARFDie::getParent() const {
428 if (isValid())
429 return U->getParent(Die);
430 return DWARFDie();
431}
432
433DWARFDie DWARFDie::getSibling() const {
434 if (isValid())
435 return U->getSibling(Die);
436 return DWARFDie();
437}
Greg Clayton0e62ee72017-01-13 00:13:42 +0000438
439iterator_range<DWARFDie::attribute_iterator>
440DWARFDie::attributes() const {
441 return make_range(attribute_iterator(*this, false),
442 attribute_iterator(*this, true));
443}
444
445DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End) :
446 Die(D), AttrValue(0), Index(0) {
447 auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
448 assert(AbbrDecl && "Must have abbreviation declaration");
449 if (End) {
450 // This is the end iterator so we set the index to the attribute count.
451 Index = AbbrDecl->getNumAttributes();
452 } else {
453 // This is the begin iterator so we extract the value for this->Index.
454 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
455 updateForIndex(*AbbrDecl, 0);
456 }
457}
458
459void DWARFDie::attribute_iterator::updateForIndex(
460 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
461 Index = I;
Hiroshi Inoueddb34d82017-07-03 06:32:59 +0000462 // AbbrDecl must be valid before calling this function.
Greg Clayton0e62ee72017-01-13 00:13:42 +0000463 auto NumAttrs = AbbrDecl.getNumAttributes();
464 if (Index < NumAttrs) {
465 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
466 // Add the previous byte size of any previous attribute value.
467 AttrValue.Offset += AttrValue.ByteSize;
468 AttrValue.Value.setForm(AbbrDecl.getFormByIndex(Index));
469 uint32_t ParseOffset = AttrValue.Offset;
470 auto U = Die.getDwarfUnit();
471 assert(U && "Die must have valid DWARF unit");
472 bool b = AttrValue.Value.extractValue(U->getDebugInfoExtractor(),
473 &ParseOffset, U);
474 (void)b;
475 assert(b && "extractValue cannot fail on fully parsed DWARF");
476 AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
477 } else {
478 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
479 AttrValue.clear();
480 }
481}
482
483DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
484 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
485 updateForIndex(*AbbrDecl, Index + 1);
486 return *this;
487}