[ORC] Add std::tuple support to SimplePackedSerialization.
[llvm-project.git] / llvm / lib / DebugInfo / DWARF / DWARFDie.cpp
blob2d5cd864f718cc4f941c7c60ed778343bcecadca
1 //===- DWARFDie.cpp -------------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
10 #include "llvm/ADT/None.h"
11 #include "llvm/ADT/Optional.h"
12 #include "llvm/ADT/SmallSet.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/BinaryFormat/Dwarf.h"
15 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
16 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
17 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
18 #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
19 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
20 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/DataExtractor.h"
23 #include "llvm/Support/Format.h"
24 #include "llvm/Support/FormatAdapters.h"
25 #include "llvm/Support/FormatVariadic.h"
26 #include "llvm/Support/MathExtras.h"
27 #include "llvm/Support/WithColor.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include <algorithm>
30 #include <cassert>
31 #include <cinttypes>
32 #include <cstdint>
33 #include <string>
34 #include <utility>
36 using namespace llvm;
37 using namespace dwarf;
38 using namespace object;
40 static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
41 OS << " (";
42 do {
43 uint64_t Shift = countTrailingZeros(Val);
44 assert(Shift < 64 && "undefined behavior");
45 uint64_t Bit = 1ULL << Shift;
46 auto PropName = ApplePropertyString(Bit);
47 if (!PropName.empty())
48 OS << PropName;
49 else
50 OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
51 if (!(Val ^= Bit))
52 break;
53 OS << ", ";
54 } while (true);
55 OS << ")";
58 static void dumpRanges(const DWARFObject &Obj, raw_ostream &OS,
59 const DWARFAddressRangesVector &Ranges,
60 unsigned AddressSize, unsigned Indent,
61 const DIDumpOptions &DumpOpts) {
62 if (!DumpOpts.ShowAddresses)
63 return;
65 for (const DWARFAddressRange &R : Ranges) {
66 OS << '\n';
67 OS.indent(Indent);
68 R.dump(OS, AddressSize, DumpOpts, &Obj);
72 static void dumpLocationList(raw_ostream &OS, const DWARFFormValue &FormValue,
73 DWARFUnit *U, unsigned Indent,
74 DIDumpOptions DumpOpts) {
75 assert(FormValue.isFormClass(DWARFFormValue::FC_SectionOffset) &&
76 "bad FORM for location list");
77 DWARFContext &Ctx = U->getContext();
78 const MCRegisterInfo *MRI = Ctx.getRegisterInfo();
79 uint64_t Offset = *FormValue.getAsSectionOffset();
81 if (FormValue.getForm() == DW_FORM_loclistx) {
82 FormValue.dump(OS, DumpOpts);
84 if (auto LoclistOffset = U->getLoclistOffset(Offset))
85 Offset = *LoclistOffset;
86 else
87 return;
89 U->getLocationTable().dumpLocationList(&Offset, OS, U->getBaseAddress(), MRI,
90 Ctx.getDWARFObj(), U, DumpOpts,
91 Indent);
92 return;
95 static void dumpLocationExpr(raw_ostream &OS, const DWARFFormValue &FormValue,
96 DWARFUnit *U, unsigned Indent,
97 DIDumpOptions DumpOpts) {
98 assert((FormValue.isFormClass(DWARFFormValue::FC_Block) ||
99 FormValue.isFormClass(DWARFFormValue::FC_Exprloc)) &&
100 "bad FORM for location expression");
101 DWARFContext &Ctx = U->getContext();
102 const MCRegisterInfo *MRI = Ctx.getRegisterInfo();
103 ArrayRef<uint8_t> Expr = *FormValue.getAsBlock();
104 DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()),
105 Ctx.isLittleEndian(), 0);
106 DWARFExpression(Data, U->getAddressByteSize(), U->getFormParams().Format)
107 .print(OS, DumpOpts, MRI, U);
108 return;
111 /// Dump the name encoded in the type tag.
112 static void dumpTypeTagName(raw_ostream &OS, dwarf::Tag T) {
113 StringRef TagStr = TagString(T);
114 if (!TagStr.startswith("DW_TAG_") || !TagStr.endswith("_type"))
115 return;
116 OS << TagStr.substr(7, TagStr.size() - 12) << " ";
119 static void dumpArrayType(raw_ostream &OS, const DWARFDie &D) {
120 for (const DWARFDie &C : D.children())
121 if (C.getTag() == DW_TAG_subrange_type) {
122 Optional<uint64_t> LB;
123 Optional<uint64_t> Count;
124 Optional<uint64_t> UB;
125 Optional<unsigned> DefaultLB;
126 if (Optional<DWARFFormValue> L = C.find(DW_AT_lower_bound))
127 LB = L->getAsUnsignedConstant();
128 if (Optional<DWARFFormValue> CountV = C.find(DW_AT_count))
129 Count = CountV->getAsUnsignedConstant();
130 if (Optional<DWARFFormValue> UpperV = C.find(DW_AT_upper_bound))
131 UB = UpperV->getAsUnsignedConstant();
132 if (Optional<DWARFFormValue> LV =
133 D.getDwarfUnit()->getUnitDIE().find(DW_AT_language))
134 if (Optional<uint64_t> LC = LV->getAsUnsignedConstant())
135 if ((DefaultLB =
136 LanguageLowerBound(static_cast<dwarf::SourceLanguage>(*LC))))
137 if (LB && *LB == *DefaultLB)
138 LB = None;
139 if (!LB && !Count && !UB)
140 OS << "[]";
141 else if (!LB && (Count || UB) && DefaultLB)
142 OS << '[' << (Count ? *Count : *UB - *DefaultLB + 1) << ']';
143 else {
144 OS << "[[";
145 if (LB)
146 OS << *LB;
147 else
148 OS << '?';
149 OS << ", ";
150 if (Count)
151 if (LB)
152 OS << *LB + *Count;
153 else
154 OS << "? + " << *Count;
155 else if (UB)
156 OS << *UB + 1;
157 else
158 OS << '?';
159 OS << ")]";
164 /// Recursively dump the DIE type name when applicable.
165 static void dumpTypeName(raw_ostream &OS, const DWARFDie &D) {
166 if (!D.isValid())
167 return;
169 if (const char *Name = D.getName(DINameKind::LinkageName)) {
170 OS << Name;
171 return;
174 // FIXME: We should have pretty printers per language. Currently we print
175 // everything as if it was C++ and fall back to the TAG type name.
176 const dwarf::Tag T = D.getTag();
177 switch (T) {
178 case DW_TAG_array_type:
179 case DW_TAG_pointer_type:
180 case DW_TAG_ptr_to_member_type:
181 case DW_TAG_reference_type:
182 case DW_TAG_rvalue_reference_type:
183 case DW_TAG_subroutine_type:
184 break;
185 default:
186 dumpTypeTagName(OS, T);
189 // Follow the DW_AT_type if possible.
190 DWARFDie TypeDie = D.getAttributeValueAsReferencedDie(DW_AT_type);
191 dumpTypeName(OS, TypeDie);
193 switch (T) {
194 case DW_TAG_subroutine_type: {
195 if (!TypeDie)
196 OS << "void";
197 OS << '(';
198 bool First = true;
199 for (const DWARFDie &C : D.children()) {
200 if (C.getTag() == DW_TAG_formal_parameter) {
201 if (!First)
202 OS << ", ";
203 First = false;
204 dumpTypeName(OS, C.getAttributeValueAsReferencedDie(DW_AT_type));
207 OS << ')';
208 break;
210 case DW_TAG_array_type: {
211 dumpArrayType(OS, D);
212 break;
214 case DW_TAG_pointer_type:
215 OS << '*';
216 break;
217 case DW_TAG_ptr_to_member_type:
218 if (DWARFDie Cont =
219 D.getAttributeValueAsReferencedDie(DW_AT_containing_type)) {
220 dumpTypeName(OS << ' ', Cont);
221 OS << "::";
223 OS << '*';
224 break;
225 case DW_TAG_reference_type:
226 OS << '&';
227 break;
228 case DW_TAG_rvalue_reference_type:
229 OS << "&&";
230 break;
231 default:
232 break;
236 static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
237 const DWARFAttribute &AttrValue, unsigned Indent,
238 DIDumpOptions DumpOpts) {
239 if (!Die.isValid())
240 return;
241 const char BaseIndent[] = " ";
242 OS << BaseIndent;
243 OS.indent(Indent + 2);
244 dwarf::Attribute Attr = AttrValue.Attr;
245 WithColor(OS, HighlightColor::Attribute) << formatv("{0}", Attr);
247 dwarf::Form Form = AttrValue.Value.getForm();
248 if (DumpOpts.Verbose || DumpOpts.ShowForm)
249 OS << formatv(" [{0}]", Form);
251 DWARFUnit *U = Die.getDwarfUnit();
252 const DWARFFormValue &FormValue = AttrValue.Value;
254 OS << "\t(";
256 StringRef Name;
257 std::string File;
258 auto Color = HighlightColor::Enumerator;
259 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
260 Color = HighlightColor::String;
261 if (const auto *LT = U->getContext().getLineTableForUnit(U))
262 if (LT->getFileNameByIndex(
263 FormValue.getAsUnsignedConstant().getValue(),
264 U->getCompilationDir(),
265 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
266 File = '"' + File + '"';
267 Name = File;
269 } else if (Optional<uint64_t> Val = FormValue.getAsUnsignedConstant())
270 Name = AttributeValueString(Attr, *Val);
272 if (!Name.empty())
273 WithColor(OS, Color) << Name;
274 else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
275 OS << *FormValue.getAsUnsignedConstant();
276 else if (Attr == DW_AT_low_pc &&
277 (FormValue.getAsAddress() ==
278 dwarf::computeTombstoneAddress(U->getAddressByteSize()))) {
279 if (DumpOpts.Verbose) {
280 FormValue.dump(OS, DumpOpts);
281 OS << " (";
283 OS << "dead code";
284 if (DumpOpts.Verbose)
285 OS << ')';
286 } else if (Attr == DW_AT_high_pc && !DumpOpts.ShowForm && !DumpOpts.Verbose &&
287 FormValue.getAsUnsignedConstant()) {
288 if (DumpOpts.ShowAddresses) {
289 // Print the actual address rather than the offset.
290 uint64_t LowPC, HighPC, Index;
291 if (Die.getLowAndHighPC(LowPC, HighPC, Index))
292 DWARFFormValue::dumpAddress(OS, U->getAddressByteSize(), HighPC);
293 else
294 FormValue.dump(OS, DumpOpts);
296 } else if (DWARFAttribute::mayHaveLocationList(Attr) &&
297 FormValue.isFormClass(DWARFFormValue::FC_SectionOffset))
298 dumpLocationList(OS, FormValue, U, sizeof(BaseIndent) + Indent + 4,
299 DumpOpts);
300 else if (FormValue.isFormClass(DWARFFormValue::FC_Exprloc) ||
301 (DWARFAttribute::mayHaveLocationExpr(Attr) &&
302 FormValue.isFormClass(DWARFFormValue::FC_Block)))
303 dumpLocationExpr(OS, FormValue, U, sizeof(BaseIndent) + Indent + 4,
304 DumpOpts);
305 else
306 FormValue.dump(OS, DumpOpts);
308 std::string Space = DumpOpts.ShowAddresses ? " " : "";
310 // We have dumped the attribute raw value. For some attributes
311 // having both the raw value and the pretty-printed value is
312 // interesting. These attributes are handled below.
313 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
314 if (const char *Name =
315 Die.getAttributeValueAsReferencedDie(FormValue).getName(
316 DINameKind::LinkageName))
317 OS << Space << "\"" << Name << '\"';
318 } else if (Attr == DW_AT_type) {
319 OS << Space << "\"";
320 dumpTypeName(OS, Die.getAttributeValueAsReferencedDie(FormValue));
321 OS << '"';
322 } else if (Attr == DW_AT_APPLE_property_attribute) {
323 if (Optional<uint64_t> OptVal = FormValue.getAsUnsignedConstant())
324 dumpApplePropertyAttribute(OS, *OptVal);
325 } else if (Attr == DW_AT_ranges) {
326 const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
327 // For DW_FORM_rnglistx we need to dump the offset separately, since
328 // we have only dumped the index so far.
329 if (FormValue.getForm() == DW_FORM_rnglistx)
330 if (auto RangeListOffset =
331 U->getRnglistOffset(*FormValue.getAsSectionOffset())) {
332 DWARFFormValue FV = DWARFFormValue::createFromUValue(
333 dwarf::DW_FORM_sec_offset, *RangeListOffset);
334 FV.dump(OS, DumpOpts);
336 if (auto RangesOrError = Die.getAddressRanges())
337 dumpRanges(Obj, OS, RangesOrError.get(), U->getAddressByteSize(),
338 sizeof(BaseIndent) + Indent + 4, DumpOpts);
339 else
340 DumpOpts.RecoverableErrorHandler(createStringError(
341 errc::invalid_argument, "decoding address ranges: %s",
342 toString(RangesOrError.takeError()).c_str()));
345 OS << ")\n";
348 bool DWARFDie::isSubprogramDIE() const { return getTag() == DW_TAG_subprogram; }
350 bool DWARFDie::isSubroutineDIE() const {
351 auto Tag = getTag();
352 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
355 Optional<DWARFFormValue> DWARFDie::find(dwarf::Attribute Attr) const {
356 if (!isValid())
357 return None;
358 auto AbbrevDecl = getAbbreviationDeclarationPtr();
359 if (AbbrevDecl)
360 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
361 return None;
364 Optional<DWARFFormValue>
365 DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const {
366 if (!isValid())
367 return None;
368 auto AbbrevDecl = getAbbreviationDeclarationPtr();
369 if (AbbrevDecl) {
370 for (auto Attr : Attrs) {
371 if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
372 return Value;
375 return None;
378 Optional<DWARFFormValue>
379 DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const {
380 SmallVector<DWARFDie, 3> Worklist;
381 Worklist.push_back(*this);
383 // Keep track if DIEs already seen to prevent infinite recursion.
384 // Empirically we rarely see a depth of more than 3 when dealing with valid
385 // DWARF. This corresponds to following the DW_AT_abstract_origin and
386 // DW_AT_specification just once.
387 SmallSet<DWARFDie, 3> Seen;
388 Seen.insert(*this);
390 while (!Worklist.empty()) {
391 DWARFDie Die = Worklist.pop_back_val();
393 if (!Die.isValid())
394 continue;
396 if (auto Value = Die.find(Attrs))
397 return Value;
399 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
400 if (Seen.insert(D).second)
401 Worklist.push_back(D);
403 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification))
404 if (Seen.insert(D).second)
405 Worklist.push_back(D);
408 return None;
411 DWARFDie
412 DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
413 if (Optional<DWARFFormValue> F = find(Attr))
414 return getAttributeValueAsReferencedDie(*F);
415 return DWARFDie();
418 DWARFDie
419 DWARFDie::getAttributeValueAsReferencedDie(const DWARFFormValue &V) const {
420 if (auto SpecRef = V.getAsRelativeReference()) {
421 if (SpecRef->Unit)
422 return SpecRef->Unit->getDIEForOffset(SpecRef->Unit->getOffset() + SpecRef->Offset);
423 if (auto SpecUnit = U->getUnitVector().getUnitForOffset(SpecRef->Offset))
424 return SpecUnit->getDIEForOffset(SpecRef->Offset);
426 return DWARFDie();
429 Optional<uint64_t> DWARFDie::getRangesBaseAttribute() const {
430 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
433 Optional<uint64_t> DWARFDie::getLocBaseAttribute() const {
434 return toSectionOffset(find(DW_AT_loclists_base));
437 Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
438 uint64_t Tombstone = dwarf::computeTombstoneAddress(U->getAddressByteSize());
439 if (LowPC == Tombstone)
440 return None;
441 if (auto FormValue = find(DW_AT_high_pc)) {
442 if (auto Address = FormValue->getAsAddress()) {
443 // High PC is an address.
444 return Address;
446 if (auto Offset = FormValue->getAsUnsignedConstant()) {
447 // High PC is an offset from LowPC.
448 return LowPC + *Offset;
451 return None;
454 bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
455 uint64_t &SectionIndex) const {
456 auto F = find(DW_AT_low_pc);
457 auto LowPcAddr = toSectionedAddress(F);
458 if (!LowPcAddr)
459 return false;
460 if (auto HighPcAddr = getHighPC(LowPcAddr->Address)) {
461 LowPC = LowPcAddr->Address;
462 HighPC = *HighPcAddr;
463 SectionIndex = LowPcAddr->SectionIndex;
464 return true;
466 return false;
469 Expected<DWARFAddressRangesVector> DWARFDie::getAddressRanges() const {
470 if (isNULL())
471 return DWARFAddressRangesVector();
472 // Single range specified by low/high PC.
473 uint64_t LowPC, HighPC, Index;
474 if (getLowAndHighPC(LowPC, HighPC, Index))
475 return DWARFAddressRangesVector{{LowPC, HighPC, Index}};
477 Optional<DWARFFormValue> Value = find(DW_AT_ranges);
478 if (Value) {
479 if (Value->getForm() == DW_FORM_rnglistx)
480 return U->findRnglistFromIndex(*Value->getAsSectionOffset());
481 return U->findRnglistFromOffset(*Value->getAsSectionOffset());
483 return DWARFAddressRangesVector();
486 bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
487 auto RangesOrError = getAddressRanges();
488 if (!RangesOrError) {
489 llvm::consumeError(RangesOrError.takeError());
490 return false;
493 for (const auto &R : RangesOrError.get())
494 if (R.LowPC <= Address && Address < R.HighPC)
495 return true;
496 return false;
499 Expected<DWARFLocationExpressionsVector>
500 DWARFDie::getLocations(dwarf::Attribute Attr) const {
501 Optional<DWARFFormValue> Location = find(Attr);
502 if (!Location)
503 return createStringError(inconvertibleErrorCode(), "No %s",
504 dwarf::AttributeString(Attr).data());
506 if (Optional<uint64_t> Off = Location->getAsSectionOffset()) {
507 uint64_t Offset = *Off;
509 if (Location->getForm() == DW_FORM_loclistx) {
510 if (auto LoclistOffset = U->getLoclistOffset(Offset))
511 Offset = *LoclistOffset;
512 else
513 return createStringError(inconvertibleErrorCode(),
514 "Loclist table not found");
516 return U->findLoclistFromOffset(Offset);
519 if (Optional<ArrayRef<uint8_t>> Expr = Location->getAsBlock()) {
520 return DWARFLocationExpressionsVector{
521 DWARFLocationExpression{None, to_vector<4>(*Expr)}};
524 return createStringError(
525 inconvertibleErrorCode(), "Unsupported %s encoding: %s",
526 dwarf::AttributeString(Attr).data(),
527 dwarf::FormEncodingString(Location->getForm()).data());
530 const char *DWARFDie::getSubroutineName(DINameKind Kind) const {
531 if (!isSubroutineDIE())
532 return nullptr;
533 return getName(Kind);
536 const char *DWARFDie::getName(DINameKind Kind) const {
537 if (!isValid() || Kind == DINameKind::None)
538 return nullptr;
539 // Try to get mangled name only if it was asked for.
540 if (Kind == DINameKind::LinkageName) {
541 if (auto Name = getLinkageName())
542 return Name;
544 return getShortName();
547 const char *DWARFDie::getShortName() const {
548 if (!isValid())
549 return nullptr;
551 return dwarf::toString(findRecursively(dwarf::DW_AT_name), nullptr);
554 const char *DWARFDie::getLinkageName() const {
555 if (!isValid())
556 return nullptr;
558 return dwarf::toString(findRecursively({dwarf::DW_AT_MIPS_linkage_name,
559 dwarf::DW_AT_linkage_name}),
560 nullptr);
563 uint64_t DWARFDie::getDeclLine() const {
564 return toUnsigned(findRecursively(DW_AT_decl_line), 0);
567 std::string
568 DWARFDie::getDeclFile(DILineInfoSpecifier::FileLineInfoKind Kind) const {
569 auto D = getAttributeValueAsReferencedDie(DW_AT_abstract_origin);
570 if (!D)
571 D = *this;
572 std::string FileName;
573 if (auto DeclFile = toUnsigned(D.find(DW_AT_decl_file))) {
574 if (const auto *LineTable =
575 getDwarfUnit()->getContext().getLineTableForUnit(
576 D.getDwarfUnit()->getLinkedUnit()))
577 LineTable->getFileNameByIndex(
578 *DeclFile, D.getDwarfUnit()->getCompilationDir(), Kind, FileName);
580 return FileName;
583 void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
584 uint32_t &CallColumn,
585 uint32_t &CallDiscriminator) const {
586 CallFile = toUnsigned(find(DW_AT_call_file), 0);
587 CallLine = toUnsigned(find(DW_AT_call_line), 0);
588 CallColumn = toUnsigned(find(DW_AT_call_column), 0);
589 CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
592 /// Helper to dump a DIE with all of its parents, but no siblings.
593 static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent,
594 DIDumpOptions DumpOpts, unsigned Depth = 0) {
595 if (!Die)
596 return Indent;
597 if (DumpOpts.ParentRecurseDepth > 0 && Depth >= DumpOpts.ParentRecurseDepth)
598 return Indent;
599 Indent = dumpParentChain(Die.getParent(), OS, Indent, DumpOpts, Depth + 1);
600 Die.dump(OS, Indent, DumpOpts);
601 return Indent + 2;
604 void DWARFDie::dump(raw_ostream &OS, unsigned Indent,
605 DIDumpOptions DumpOpts) const {
606 if (!isValid())
607 return;
608 DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
609 const uint64_t Offset = getOffset();
610 uint64_t offset = Offset;
611 if (DumpOpts.ShowParents) {
612 DIDumpOptions ParentDumpOpts = DumpOpts;
613 ParentDumpOpts.ShowParents = false;
614 ParentDumpOpts.ShowChildren = false;
615 Indent = dumpParentChain(getParent(), OS, Indent, ParentDumpOpts);
618 if (debug_info_data.isValidOffset(offset)) {
619 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
620 if (DumpOpts.ShowAddresses)
621 WithColor(OS, HighlightColor::Address).get()
622 << format("\n0x%8.8" PRIx64 ": ", Offset);
624 if (abbrCode) {
625 auto AbbrevDecl = getAbbreviationDeclarationPtr();
626 if (AbbrevDecl) {
627 WithColor(OS, HighlightColor::Tag).get().indent(Indent)
628 << formatv("{0}", getTag());
629 if (DumpOpts.Verbose)
630 OS << format(" [%u] %c", abbrCode,
631 AbbrevDecl->hasChildren() ? '*' : ' ');
632 OS << '\n';
634 // Dump all data in the DIE for the attributes.
635 for (const DWARFAttribute &AttrValue : attributes())
636 dumpAttribute(OS, *this, AttrValue, Indent, DumpOpts);
638 if (DumpOpts.ShowChildren && DumpOpts.ChildRecurseDepth > 0) {
639 DWARFDie Child = getFirstChild();
640 DumpOpts.ChildRecurseDepth--;
641 DIDumpOptions ChildDumpOpts = DumpOpts;
642 ChildDumpOpts.ShowParents = false;
643 while (Child) {
644 Child.dump(OS, Indent + 2, ChildDumpOpts);
645 Child = Child.getSibling();
648 } else {
649 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
650 << abbrCode << '\n';
652 } else {
653 OS.indent(Indent) << "NULL\n";
658 LLVM_DUMP_METHOD void DWARFDie::dump() const { dump(llvm::errs(), 0); }
660 DWARFDie DWARFDie::getParent() const {
661 if (isValid())
662 return U->getParent(Die);
663 return DWARFDie();
666 DWARFDie DWARFDie::getSibling() const {
667 if (isValid())
668 return U->getSibling(Die);
669 return DWARFDie();
672 DWARFDie DWARFDie::getPreviousSibling() const {
673 if (isValid())
674 return U->getPreviousSibling(Die);
675 return DWARFDie();
678 DWARFDie DWARFDie::getFirstChild() const {
679 if (isValid())
680 return U->getFirstChild(Die);
681 return DWARFDie();
684 DWARFDie DWARFDie::getLastChild() const {
685 if (isValid())
686 return U->getLastChild(Die);
687 return DWARFDie();
690 iterator_range<DWARFDie::attribute_iterator> DWARFDie::attributes() const {
691 return make_range(attribute_iterator(*this, false),
692 attribute_iterator(*this, true));
695 DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End)
696 : Die(D), Index(0) {
697 auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
698 assert(AbbrDecl && "Must have abbreviation declaration");
699 if (End) {
700 // This is the end iterator so we set the index to the attribute count.
701 Index = AbbrDecl->getNumAttributes();
702 } else {
703 // This is the begin iterator so we extract the value for this->Index.
704 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
705 updateForIndex(*AbbrDecl, 0);
709 void DWARFDie::attribute_iterator::updateForIndex(
710 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
711 Index = I;
712 // AbbrDecl must be valid before calling this function.
713 auto NumAttrs = AbbrDecl.getNumAttributes();
714 if (Index < NumAttrs) {
715 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
716 // Add the previous byte size of any previous attribute value.
717 AttrValue.Offset += AttrValue.ByteSize;
718 uint64_t ParseOffset = AttrValue.Offset;
719 if (AbbrDecl.getAttrIsImplicitConstByIndex(Index))
720 AttrValue.Value = DWARFFormValue::createFromSValue(
721 AbbrDecl.getFormByIndex(Index),
722 AbbrDecl.getAttrImplicitConstValueByIndex(Index));
723 else {
724 auto U = Die.getDwarfUnit();
725 assert(U && "Die must have valid DWARF unit");
726 AttrValue.Value = DWARFFormValue::createFromUnit(
727 AbbrDecl.getFormByIndex(Index), U, &ParseOffset);
729 AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
730 } else {
731 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
732 AttrValue = {};
736 DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
737 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
738 updateForIndex(*AbbrDecl, Index + 1);
739 return *this;
742 bool DWARFAttribute::mayHaveLocationList(dwarf::Attribute Attr) {
743 switch(Attr) {
744 case DW_AT_location:
745 case DW_AT_string_length:
746 case DW_AT_return_addr:
747 case DW_AT_data_member_location:
748 case DW_AT_frame_base:
749 case DW_AT_static_link:
750 case DW_AT_segment:
751 case DW_AT_use_location:
752 case DW_AT_vtable_elem_location:
753 return true;
754 default:
755 return false;
759 bool DWARFAttribute::mayHaveLocationExpr(dwarf::Attribute Attr) {
760 switch (Attr) {
761 // From the DWARF v5 specification.
762 case DW_AT_location:
763 case DW_AT_byte_size:
764 case DW_AT_bit_offset:
765 case DW_AT_bit_size:
766 case DW_AT_string_length:
767 case DW_AT_lower_bound:
768 case DW_AT_return_addr:
769 case DW_AT_bit_stride:
770 case DW_AT_upper_bound:
771 case DW_AT_count:
772 case DW_AT_data_member_location:
773 case DW_AT_frame_base:
774 case DW_AT_segment:
775 case DW_AT_static_link:
776 case DW_AT_use_location:
777 case DW_AT_vtable_elem_location:
778 case DW_AT_allocated:
779 case DW_AT_associated:
780 case DW_AT_data_location:
781 case DW_AT_byte_stride:
782 case DW_AT_rank:
783 case DW_AT_call_value:
784 case DW_AT_call_origin:
785 case DW_AT_call_target:
786 case DW_AT_call_target_clobbered:
787 case DW_AT_call_data_location:
788 case DW_AT_call_data_value:
789 // Extensions.
790 case DW_AT_GNU_call_site_value:
791 case DW_AT_GNU_call_site_target:
792 return true;
793 default:
794 return false;