LLVM 22.0.0git
DWARFDie.cpp
Go to the documentation of this file.
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//===----------------------------------------------------------------------===//
8
11#include "llvm/ADT/SmallSet.h"
12#include "llvm/ADT/StringRef.h"
26#include "llvm/Support/Format.h"
30#include <cassert>
31#include <cinttypes>
32#include <cstdint>
33#include <string>
34#include <utility>
35
36using namespace llvm;
37using namespace dwarf;
38using namespace object;
39
41 OS << " (";
42 do {
43 uint64_t Shift = llvm::countr_zero(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 << ")";
56}
57
58static 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;
64
65 for (const DWARFAddressRange &R : Ranges) {
66 OS << '\n';
67 OS.indent(Indent);
68 R.dump(OS, AddressSize, DumpOpts, &Obj);
69 }
70}
71
72static void dumpLocationList(raw_ostream &OS, const DWARFFormValue &FormValue,
73 DWARFUnit *U, unsigned Indent,
74 DIDumpOptions DumpOpts) {
76 "bad FORM for location list");
77 DWARFContext &Ctx = U->getContext();
78 uint64_t Offset = *FormValue.getAsSectionOffset();
79
80 if (FormValue.getForm() == DW_FORM_loclistx) {
81 FormValue.dump(OS, DumpOpts);
82
83 if (auto LoclistOffset = U->getLoclistOffset(Offset))
84 Offset = *LoclistOffset;
85 else
86 return;
87 }
88 U->getLocationTable().dumpLocationList(
89 &Offset, OS, U->getBaseAddress(), Ctx.getDWARFObj(), U, DumpOpts, Indent);
90}
91
92static void dumpLocationExpr(raw_ostream &OS, const DWARFFormValue &FormValue,
93 DWARFUnit *U, unsigned Indent,
94 DIDumpOptions DumpOpts) {
97 "bad FORM for location expression");
98 DWARFContext &Ctx = U->getContext();
99 ArrayRef<uint8_t> Expr = *FormValue.getAsBlock();
100 DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()),
101 Ctx.isLittleEndian(), 0);
102 DWARFExpression DE(Data, U->getAddressByteSize(), U->getFormParams().Format);
103 printDwarfExpression(&DE, OS, DumpOpts, U);
104}
105
107 return D.getAttributeValueAsReferencedDie(F).resolveTypeUnitReference();
108}
109
110static llvm::StringRef
112 const DWARFDie &Die) {
113 if (AttrValue.Attr != DW_AT_language_version)
114 return {};
115
116 auto NameForm = Die.find(DW_AT_language_name);
117 if (!NameForm)
118 return {};
119
120 auto LName = NameForm->getAsUnsignedConstant();
121 if (!LName)
122 return {};
123
124 auto LVersion = AttrValue.Value.getAsUnsignedConstant();
125 if (!LVersion)
126 return {};
127
129 static_cast<SourceLanguageName>(*LName), *LVersion);
130}
131
134 if (!PropDIE)
135 return llvm::createStringError("invalid DIE");
136
137 if (PropDIE.getTag() != DW_TAG_APPLE_property)
138 return llvm::createStringError("not referencing a DW_TAG_APPLE_property");
139
140 auto PropNameForm = PropDIE.find(DW_AT_APPLE_property_name);
141 if (!PropNameForm)
142 return "";
143
144 auto NameOrErr = PropNameForm->getAsCString();
145 if (!NameOrErr)
146 return NameOrErr.takeError();
147
148 return *NameOrErr;
149}
150
151static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
152 const DWARFAttribute &AttrValue, unsigned Indent,
153 DIDumpOptions DumpOpts) {
154 if (!Die.isValid())
155 return;
156 const char BaseIndent[] = " ";
157 OS << BaseIndent;
158 OS.indent(Indent + 2);
159 dwarf::Attribute Attr = AttrValue.Attr;
160 WithColor(OS, HighlightColor::Attribute) << formatv("{0}", Attr);
161
162 dwarf::Form Form = AttrValue.Value.getForm();
163 if (DumpOpts.Verbose || DumpOpts.ShowForm)
164 OS << formatv(" [{0}]", Form);
165
166 DWARFUnit *U = Die.getDwarfUnit();
167 const DWARFFormValue &FormValue = AttrValue.Value;
168
169 OS << "\t(";
170
171 StringRef Name;
172 std::string File;
173 auto Color = HighlightColor::Enumerator;
174 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
176 if (const auto *LT = U->getContext().getLineTableForUnit(U)) {
177 if (std::optional<uint64_t> Val = FormValue.getAsUnsignedConstant()) {
178 if (LT->getFileNameByIndex(
179 *Val, U->getCompilationDir(),
181 File)) {
182 File = '"' + File + '"';
183 Name = File;
184 }
185 }
186 }
187 } else if (std::optional<uint64_t> Val = FormValue.getAsUnsignedConstant())
188 Name = AttributeValueString(Attr, *Val);
189
190 auto DumpUnsignedConstant = [&OS,
191 &DumpOpts](const DWARFFormValue &FormValue) {
192 if (std::optional<uint64_t> Val = FormValue.getAsUnsignedConstant())
193 OS << *Val;
194 else
195 FormValue.dump(OS, DumpOpts);
196 };
197
198 llvm::StringRef PrettyVersionName =
199 prettyLanguageVersionString(AttrValue, Die);
200 bool ShouldDumpRawLanguageVersion =
201 Attr == DW_AT_language_version &&
202 (DumpOpts.Verbose || PrettyVersionName.empty());
203
204 if (!Name.empty())
205 WithColor(OS, Color) << Name;
206 else if (Attr == DW_AT_decl_line || Attr == DW_AT_decl_column ||
207 Attr == DW_AT_call_line || Attr == DW_AT_call_column) {
208 DumpUnsignedConstant(FormValue);
209 } else if (Attr == DW_AT_language_version) {
210 if (ShouldDumpRawLanguageVersion)
211 DumpUnsignedConstant(FormValue);
212 } else if (Attr == DW_AT_low_pc &&
213 (FormValue.getAsAddress() ==
214 dwarf::computeTombstoneAddress(U->getAddressByteSize()))) {
215 if (DumpOpts.Verbose) {
216 FormValue.dump(OS, DumpOpts);
217 OS << " (";
218 }
219 OS << "dead code";
220 if (DumpOpts.Verbose)
221 OS << ')';
222 } else if (Attr == DW_AT_high_pc && !DumpOpts.ShowForm && !DumpOpts.Verbose &&
223 FormValue.getAsUnsignedConstant()) {
224 if (DumpOpts.ShowAddresses) {
225 // Print the actual address rather than the offset.
226 uint64_t LowPC, HighPC, Index;
227 if (Die.getLowAndHighPC(LowPC, HighPC, Index))
228 DWARFFormValue::dumpAddress(OS, U->getAddressByteSize(), HighPC);
229 else
230 FormValue.dump(OS, DumpOpts);
231 }
232 } else if (DWARFAttribute::mayHaveLocationList(Attr) &&
234 dumpLocationList(OS, FormValue, U, sizeof(BaseIndent) + Indent + 4,
235 DumpOpts);
236 else if (FormValue.isFormClass(DWARFFormValue::FC_Exprloc) ||
239 dumpLocationExpr(OS, FormValue, U, sizeof(BaseIndent) + Indent + 4,
240 DumpOpts);
241 else
242 FormValue.dump(OS, DumpOpts);
243
244 std::string Space = DumpOpts.ShowAddresses ? " " : "";
245
246 // We have dumped the attribute raw value. For some attributes
247 // having both the raw value and the pretty-printed value is
248 // interesting. These attributes are handled below.
249 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin ||
250 Attr == DW_AT_call_origin) {
251 if (const char *Name =
254 OS << Space << "\"" << Name << '\"';
255 } else if (Attr == DW_AT_APPLE_property) {
256 auto PropDIE = Die.getAttributeValueAsReferencedDie(FormValue);
257 if (auto PropNameOrErr = getApplePropertyName(PropDIE))
258 OS << Space << "\"" << *PropNameOrErr << '\"';
259 else
262 llvm::formatv("decoding DW_AT_APPLE_property_name: {}",
263 toString(PropNameOrErr.takeError()))));
264 } else if (Attr == DW_AT_type || Attr == DW_AT_containing_type) {
265 DWARFDie D = resolveReferencedType(Die, FormValue);
266 if (D && !D.isNULL()) {
267 OS << Space << "\"";
269 OS << '"';
270 }
271 } else if (Attr == DW_AT_APPLE_property_attribute) {
272 if (std::optional<uint64_t> OptVal = FormValue.getAsUnsignedConstant())
273 dumpApplePropertyAttribute(OS, *OptVal);
274 } else if (Attr == DW_AT_ranges) {
275 const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
276 // For DW_FORM_rnglistx we need to dump the offset separately, since
277 // we have only dumped the index so far.
278 if (FormValue.getForm() == DW_FORM_rnglistx)
279 if (auto RangeListOffset =
280 U->getRnglistOffset(*FormValue.getAsSectionOffset())) {
282 dwarf::DW_FORM_sec_offset, *RangeListOffset);
283 FV.dump(OS, DumpOpts);
284 }
285 if (auto RangesOrError = Die.getAddressRanges())
286 dumpRanges(Obj, OS, RangesOrError.get(), U->getAddressByteSize(),
287 sizeof(BaseIndent) + Indent + 4, DumpOpts);
288 else
290 errc::invalid_argument, "decoding address ranges: %s",
291 toString(RangesOrError.takeError()).c_str()));
292 } else if (Attr == DW_AT_language_version) {
293 if (!PrettyVersionName.empty())
294 WithColor(OS, Color) << (ShouldDumpRawLanguageVersion ? " " : "")
295 << PrettyVersionName;
296 }
297
298 OS << ")\n";
299}
300
302 std::string *OriginalFullName) const {
303 const char *NamePtr = getShortName();
304 if (!NamePtr)
305 return;
306 if (getTag() == DW_TAG_GNU_template_parameter_pack)
307 return;
308 dumpTypeUnqualifiedName(*this, OS, OriginalFullName);
309}
310
311bool DWARFDie::isSubprogramDIE() const { return getTag() == DW_TAG_subprogram; }
312
314 auto Tag = getTag();
315 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
316}
317
318std::optional<DWARFFormValue> DWARFDie::find(dwarf::Attribute Attr) const {
319 if (!isValid())
320 return std::nullopt;
321 auto AbbrevDecl = getAbbreviationDeclarationPtr();
322 if (AbbrevDecl)
323 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
324 return std::nullopt;
325}
326
327std::optional<DWARFFormValue>
329 if (!isValid())
330 return std::nullopt;
331 auto AbbrevDecl = getAbbreviationDeclarationPtr();
332 if (AbbrevDecl) {
333 for (auto Attr : Attrs) {
334 if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
335 return Value;
336 }
337 }
338 return std::nullopt;
339}
340
341std::optional<DWARFFormValue>
344 Worklist.push_back(*this);
345
346 // Keep track if DIEs already seen to prevent infinite recursion.
347 // Empirically we rarely see a depth of more than 3 when dealing with valid
348 // DWARF. This corresponds to following the DW_AT_abstract_origin and
349 // DW_AT_specification just once.
351 Seen.insert(*this);
352
353 while (!Worklist.empty()) {
354 DWARFDie Die = Worklist.pop_back_val();
355
356 if (!Die.isValid())
357 continue;
358
359 if (auto Value = Die.find(Attrs))
360 return Value;
361
362 for (dwarf::Attribute Attr :
363 {DW_AT_abstract_origin, DW_AT_specification, DW_AT_signature}) {
364 if (auto D = Die.getAttributeValueAsReferencedDie(Attr))
365 if (Seen.insert(D).second)
366 Worklist.push_back(D);
367 }
368 }
369
370 return std::nullopt;
371}
372
375 if (std::optional<DWARFFormValue> F = find(Attr))
377 return DWARFDie();
378}
379
382 DWARFDie Result;
383 if (std::optional<uint64_t> Offset = V.getAsRelativeReference()) {
384 Result = const_cast<DWARFUnit *>(V.getUnit())
385 ->getDIEForOffset(V.getUnit()->getOffset() + *Offset);
386 } else if (Offset = V.getAsDebugInfoReference(); Offset) {
387 if (DWARFUnit *SpecUnit = U->getUnitVector().getUnitForOffset(*Offset))
388 Result = SpecUnit->getDIEForOffset(*Offset);
389 } else if (std::optional<uint64_t> Sig = V.getAsSignatureReference()) {
390 if (DWARFTypeUnit *TU =
391 U->getContext().getTypeUnitForHash(*Sig, U->isDWOUnit()))
392 Result = TU->getDIEForOffset(TU->getTypeOffset() + TU->getOffset());
393 }
394 return Result;
395}
396
398 if (auto Attr = find(DW_AT_signature)) {
399 if (std::optional<uint64_t> Sig = Attr->getAsReferenceUVal()) {
400 if (DWARFTypeUnit *TU =
401 U->getContext().getTypeUnitForHash(*Sig, U->isDWOUnit()))
402 return TU->getDIEForOffset(TU->getTypeOffset() + TU->getOffset());
403 }
404 }
405 return *this;
406}
407
414
415std::optional<uint64_t> DWARFDie::getRangesBaseAttribute() const {
416 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
417}
418
419std::optional<uint64_t> DWARFDie::getLocBaseAttribute() const {
420 return toSectionOffset(find(DW_AT_loclists_base));
421}
422
423std::optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
424 uint64_t Tombstone = dwarf::computeTombstoneAddress(U->getAddressByteSize());
425 if (LowPC == Tombstone)
426 return std::nullopt;
427 if (auto FormValue = find(DW_AT_high_pc)) {
428 if (auto Address = FormValue->getAsAddress()) {
429 // High PC is an address.
430 return Address;
431 }
432 if (auto Offset = FormValue->getAsUnsignedConstant()) {
433 // High PC is an offset from LowPC.
434 return LowPC + *Offset;
435 }
436 }
437 return std::nullopt;
438}
439
441 uint64_t &SectionIndex) const {
442 auto F = find(DW_AT_low_pc);
443 auto LowPcAddr = toSectionedAddress(F);
444 if (!LowPcAddr)
445 return false;
446 if (auto HighPcAddr = getHighPC(LowPcAddr->Address)) {
447 LowPC = LowPcAddr->Address;
448 HighPC = *HighPcAddr;
449 SectionIndex = LowPcAddr->SectionIndex;
450 return true;
451 }
452 return false;
453}
454
456 if (isNULL())
458 // Single range specified by low/high PC.
459 uint64_t LowPC, HighPC, Index;
460 if (getLowAndHighPC(LowPC, HighPC, Index))
461 return DWARFAddressRangesVector{{LowPC, HighPC, Index}};
462
463 std::optional<DWARFFormValue> Value = find(DW_AT_ranges);
464 if (Value) {
465 if (Value->getForm() == DW_FORM_rnglistx)
466 return U->findRnglistFromIndex(*Value->getAsSectionOffset());
467 return U->findRnglistFromOffset(*Value->getAsSectionOffset());
468 }
470}
471
473 auto RangesOrError = getAddressRanges();
474 if (!RangesOrError) {
475 llvm::consumeError(RangesOrError.takeError());
476 return false;
477 }
478
479 for (const auto &R : RangesOrError.get())
480 if (R.LowPC <= Address && Address < R.HighPC)
481 return true;
482 return false;
483}
484
485std::optional<uint64_t> DWARFDie::getLanguage() const {
486 if (isValid()) {
487 if (std::optional<DWARFFormValue> LV =
488 U->getUnitDIE().find(dwarf::DW_AT_language))
489 return LV->getAsUnsignedConstant();
490 }
491 return std::nullopt;
492}
493
496 std::optional<DWARFFormValue> Location = find(Attr);
497 if (!Location)
500
501 if (std::optional<uint64_t> Off = Location->getAsSectionOffset()) {
502 uint64_t Offset = *Off;
503
504 if (Location->getForm() == DW_FORM_loclistx) {
505 if (auto LoclistOffset = U->getLoclistOffset(Offset))
506 Offset = *LoclistOffset;
507 else
509 "Loclist table not found");
510 }
511 return U->findLoclistFromOffset(Offset);
512 }
513
514 if (std::optional<ArrayRef<uint8_t>> Expr = Location->getAsBlock()) {
516 DWARFLocationExpression{std::nullopt, to_vector<4>(*Expr)}};
517 }
518
519 return createStringError(
520 inconvertibleErrorCode(), "Unsupported %s encoding: %s",
522 dwarf::FormEncodingString(Location->getForm()).data());
523}
524
526 if (!isSubroutineDIE())
527 return nullptr;
528 return getName(Kind);
529}
530
532 if (!isValid() || Kind == DINameKind::None)
533 return nullptr;
534 // Try to get mangled name only if it was asked for.
536 if (auto Name = getLinkageName())
537 return Name;
538 }
539 return getShortName();
540}
541
542const char *DWARFDie::getShortName() const {
543 if (!isValid())
544 return nullptr;
545
546 return dwarf::toString(findRecursively(dwarf::DW_AT_name), nullptr);
547}
548
549const char *DWARFDie::getLinkageName() const {
550 if (!isValid())
551 return nullptr;
552
553 return dwarf::toString(findRecursively({dwarf::DW_AT_MIPS_linkage_name,
554 dwarf::DW_AT_linkage_name}),
555 nullptr);
556}
557
559 return toUnsigned(findRecursively(DW_AT_decl_line), 0);
560}
561
562std::string
564 if (auto FormValue = findRecursively(DW_AT_decl_file))
565 if (auto OptString = FormValue->getAsFile(Kind))
566 return *OptString;
567 return {};
568}
569
571 uint32_t &CallColumn,
572 uint32_t &CallDiscriminator) const {
573 CallFile = toUnsigned(find(DW_AT_call_file), 0);
574 CallLine = toUnsigned(find(DW_AT_call_line), 0);
575 CallColumn = toUnsigned(find(DW_AT_call_column), 0);
576 CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
577}
578
579static std::optional<uint64_t>
582 // Cycle detected?
583 if (!Visited.insert(Die.getDebugInfoEntry()).second)
584 return {};
585 if (auto SizeAttr = Die.find(DW_AT_byte_size))
586 if (std::optional<uint64_t> Size = SizeAttr->getAsUnsignedConstant())
587 return Size;
588
589 switch (Die.getTag()) {
590 case DW_TAG_pointer_type:
591 case DW_TAG_reference_type:
592 case DW_TAG_rvalue_reference_type:
593 return PointerSize;
594 case DW_TAG_ptr_to_member_type: {
596 if (BaseType.getTag() == DW_TAG_subroutine_type)
597 return 2 * PointerSize;
598 return PointerSize;
599 }
600 case DW_TAG_const_type:
601 case DW_TAG_immutable_type:
602 case DW_TAG_volatile_type:
603 case DW_TAG_restrict_type:
604 case DW_TAG_template_alias:
605 case DW_TAG_typedef: {
607 return getTypeSizeImpl(BaseType, PointerSize, Visited);
608 break;
609 }
610 case DW_TAG_array_type: {
612 if (!BaseType)
613 return std::nullopt;
614 std::optional<uint64_t> BaseSize =
615 getTypeSizeImpl(BaseType, PointerSize, Visited);
616 if (!BaseSize)
617 return std::nullopt;
618 uint64_t Size = *BaseSize;
619 for (DWARFDie Child : Die) {
620 if (Child.getTag() != DW_TAG_subrange_type)
621 continue;
622
623 if (auto ElemCountAttr = Child.find(DW_AT_count))
624 if (std::optional<uint64_t> ElemCount =
625 ElemCountAttr->getAsUnsignedConstant())
626 Size *= *ElemCount;
627 if (auto UpperBoundAttr = Child.find(DW_AT_upper_bound))
628 if (std::optional<int64_t> UpperBound =
629 UpperBoundAttr->getAsSignedConstant()) {
630 int64_t LowerBound = 0;
631 if (auto LowerBoundAttr = Child.find(DW_AT_lower_bound))
632 LowerBound = LowerBoundAttr->getAsSignedConstant().value_or(0);
633 Size *= *UpperBound - LowerBound + 1;
634 }
635 }
636 return Size;
637 }
638 default:
640 return getTypeSizeImpl(BaseType, PointerSize, Visited);
641 break;
642 }
643 return std::nullopt;
644}
645
646std::optional<uint64_t> DWARFDie::getTypeSize(uint64_t PointerSize) {
648 return getTypeSizeImpl(*this, PointerSize, Visited);
649}
650
651/// Helper to dump a DIE with all of its parents, but no siblings.
652static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent,
653 DIDumpOptions DumpOpts, unsigned Depth = 0) {
654 if (!Die)
655 return Indent;
656 if (DumpOpts.ParentRecurseDepth > 0 && Depth >= DumpOpts.ParentRecurseDepth)
657 return Indent;
658 Indent = dumpParentChain(Die.getParent(), OS, Indent, DumpOpts, Depth + 1);
659 Die.dump(OS, Indent, DumpOpts);
660 return Indent + 2;
661}
662
663void DWARFDie::dump(raw_ostream &OS, unsigned Indent,
664 DIDumpOptions DumpOpts) const {
665 if (!isValid())
666 return;
667 DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
668 const uint64_t Offset = getOffset();
669 uint64_t offset = Offset;
670 if (DumpOpts.ShowParents) {
671 DIDumpOptions ParentDumpOpts = DumpOpts;
672 ParentDumpOpts.ShowParents = false;
673 ParentDumpOpts.ShowChildren = false;
674 Indent = dumpParentChain(getParent(), OS, Indent, ParentDumpOpts);
675 }
676
677 if (debug_info_data.isValidOffset(offset)) {
678 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
679 if (DumpOpts.ShowAddresses)
681 << format("\n0x%8.8" PRIx64 ": ", Offset);
682
683 if (abbrCode) {
684 auto AbbrevDecl = getAbbreviationDeclarationPtr();
685 if (AbbrevDecl) {
687 << formatv("{0}", getTag());
688 if (DumpOpts.Verbose) {
689 OS << format(" [%u] %c", abbrCode,
690 AbbrevDecl->hasChildren() ? '*' : ' ');
691 if (std::optional<uint32_t> ParentIdx = Die->getParentIdx())
692 OS << format(" (0x%8.8" PRIx64 ")",
693 U->getDIEAtIndex(*ParentIdx).getOffset());
694 }
695 OS << '\n';
696
697 // Dump all data in the DIE for the attributes.
698 for (const DWARFAttribute &AttrValue : attributes())
699 dumpAttribute(OS, *this, AttrValue, Indent, DumpOpts);
700
701 if (DumpOpts.ShowChildren && DumpOpts.ChildRecurseDepth > 0) {
702 DWARFDie Child = getFirstChild();
703 DumpOpts.ChildRecurseDepth--;
704 DIDumpOptions ChildDumpOpts = DumpOpts;
705 ChildDumpOpts.ShowParents = false;
706 while (Child) {
707 Child.dump(OS, Indent + 2, ChildDumpOpts);
708 Child = Child.getSibling();
709 }
710 }
711 } else {
712 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
713 << abbrCode << '\n';
714 }
715 } else {
716 OS.indent(Indent) << "NULL\n";
717 }
718 }
719}
720
722
724 if (isValid())
725 return U->getParent(Die);
726 return DWARFDie();
727}
728
730 if (isValid())
731 return U->getSibling(Die);
732 return DWARFDie();
733}
734
736 if (isValid())
737 return U->getPreviousSibling(Die);
738 return DWARFDie();
739}
740
742 if (isValid())
743 return U->getFirstChild(Die);
744 return DWARFDie();
745}
746
748 if (isValid())
749 return U->getLastChild(Die);
750 return DWARFDie();
751}
752
757
759 : Die(D), Index(0) {
760 auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
761 assert(AbbrDecl && "Must have abbreviation declaration");
762 if (End) {
763 // This is the end iterator so we set the index to the attribute count.
764 Index = AbbrDecl->getNumAttributes();
765 } else {
766 // This is the begin iterator so we extract the value for this->Index.
767 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
768 updateForIndex(*AbbrDecl, 0);
769 }
770}
771
772void DWARFDie::attribute_iterator::updateForIndex(
773 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
774 Index = I;
775 // AbbrDecl must be valid before calling this function.
776 auto NumAttrs = AbbrDecl.getNumAttributes();
777 if (Index < NumAttrs) {
778 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
779 // Add the previous byte size of any previous attribute value.
780 AttrValue.Offset += AttrValue.ByteSize;
781 uint64_t ParseOffset = AttrValue.Offset;
783 AttrValue.Value = DWARFFormValue::createFromSValue(
784 AbbrDecl.getFormByIndex(Index),
786 else {
787 auto U = Die.getDwarfUnit();
788 assert(U && "Die must have valid DWARF unit");
789 AttrValue.Value = DWARFFormValue::createFromUnit(
790 AbbrDecl.getFormByIndex(Index), U, &ParseOffset);
791 }
792 AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
793 } else {
794 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
795 AttrValue = {};
796 }
797}
798
800 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
801 updateForIndex(*AbbrDecl, Index + 1);
802 return *this;
803}
804
806 switch(Attr) {
807 case DW_AT_location:
808 case DW_AT_string_length:
809 case DW_AT_return_addr:
810 case DW_AT_data_member_location:
811 case DW_AT_frame_base:
812 case DW_AT_static_link:
813 case DW_AT_segment:
814 case DW_AT_use_location:
815 case DW_AT_vtable_elem_location:
816 return true;
817 default:
818 return false;
819 }
820}
821
823 switch (Attr) {
824 // From the DWARF v5 specification.
825 case DW_AT_location:
826 case DW_AT_byte_size:
827 case DW_AT_bit_offset:
828 case DW_AT_bit_size:
829 case DW_AT_string_length:
830 case DW_AT_lower_bound:
831 case DW_AT_return_addr:
832 case DW_AT_bit_stride:
833 case DW_AT_upper_bound:
834 case DW_AT_count:
835 case DW_AT_data_member_location:
836 case DW_AT_frame_base:
837 case DW_AT_segment:
838 case DW_AT_static_link:
839 case DW_AT_use_location:
840 case DW_AT_vtable_elem_location:
841 case DW_AT_allocated:
842 case DW_AT_associated:
843 case DW_AT_data_location:
844 case DW_AT_byte_stride:
845 case DW_AT_rank:
846 case DW_AT_call_value:
847 case DW_AT_call_origin:
848 case DW_AT_call_target:
849 case DW_AT_call_target_clobbered:
850 case DW_AT_call_data_location:
851 case DW_AT_call_data_value:
852 // Extensions.
853 case DW_AT_GNU_call_site_value:
854 case DW_AT_GNU_call_site_target:
855 return true;
856 default:
857 return false;
858 }
859}
860
861namespace llvm {
862
866
868 std::string *OriginalFullName) {
870}
871
872} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:638
static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die, const DWARFAttribute &AttrValue, unsigned Indent, DIDumpOptions DumpOpts)
Definition DWARFDie.cpp:151
static void dumpLocationExpr(raw_ostream &OS, const DWARFFormValue &FormValue, DWARFUnit *U, unsigned Indent, DIDumpOptions DumpOpts)
Definition DWARFDie.cpp:92
static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent, DIDumpOptions DumpOpts, unsigned Depth=0)
Helper to dump a DIE with all of its parents, but no siblings.
Definition DWARFDie.cpp:652
static DWARFDie resolveReferencedType(DWARFDie D, DWARFFormValue F)
Definition DWARFDie.cpp:106
static void dumpLocationList(raw_ostream &OS, const DWARFFormValue &FormValue, DWARFUnit *U, unsigned Indent, DIDumpOptions DumpOpts)
Definition DWARFDie.cpp:72
static llvm::StringRef prettyLanguageVersionString(const DWARFAttribute &AttrValue, const DWARFDie &Die)
Definition DWARFDie.cpp:111
static llvm::Expected< llvm::StringRef > getApplePropertyName(const DWARFDie &PropDIE)
Definition DWARFDie.cpp:133
static std::optional< uint64_t > getTypeSizeImpl(DWARFDie Die, uint64_t PointerSize, SmallPtrSetImpl< const DWARFDebugInfoEntry * > &Visited)
Definition DWARFDie.cpp:580
static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val)
Definition DWARFDie.cpp:40
static void dumpRanges(const DWARFObject &Obj, raw_ostream &OS, const DWARFAddressRangesVector &Ranges, unsigned AddressSize, unsigned Indent, const DIDumpOptions &DumpOpts)
Definition DWARFDie.cpp:58
This file contains constants used for implementing Dwarf debug support.
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
This file defines the SmallPtrSet class.
This file defines the SmallSet class.
static Split data
LocallyHashedType DenseMapInfo< LocallyHashedType >::Tombstone
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition ArrayRef.h:143
const T * data() const
Definition ArrayRef.h:140
A structured debug information entry.
Definition DIE.h:828
dwarf::Attribute getAttrByIndex(uint32_t idx) const
int64_t getAttrImplicitConstValueByIndex(uint32_t idx) const
dwarf::Form getFormByIndex(uint32_t idx) const
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
DWARFTypeUnit * getTypeUnitForHash(uint64_t Hash, bool IsDWO)
const DWARFObject & getDWARFObj() const
A DWARFDataExtractor (typically for an in-memory copy of an object-file section) plus a relocation ma...
LLVM_ABI attribute_iterator & operator++()
Definition DWARFDie.cpp:799
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition DWARFDie.h:43
LLVM_ABI void getFullName(raw_string_ostream &, std::string *OriginalFullName=nullptr) const
Definition DWARFDie.cpp:301
LLVM_ABI DWARFDie resolveTypeUnitReference() const
Definition DWARFDie.cpp:397
LLVM_ABI std::optional< uint64_t > getLocBaseAttribute() const
Definition DWARFDie.cpp:419
uint64_t getOffset() const
Get the absolute offset into the debug info or types section.
Definition DWARFDie.h:68
LLVM_ABI const char * getShortName() const
Return the DIE short name resolving DW_AT_specification or DW_AT_abstract_origin references if necess...
Definition DWARFDie.cpp:542
LLVM_ABI Expected< DWARFAddressRangesVector > getAddressRanges() const
Get the address ranges for this DIE.
Definition DWARFDie.cpp:455
LLVM_ABI DWARFDie getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE as the referenced DIE.
Definition DWARFDie.cpp:374
LLVM_ABI DWARFDie getParent() const
Get the parent of this DIE object.
Definition DWARFDie.cpp:723
LLVM_ABI std::optional< DWARFFormValue > find(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE.
Definition DWARFDie.cpp:318
DWARFUnit * getDwarfUnit() const
Definition DWARFDie.h:55
const DWARFDebugInfoEntry * getDebugInfoEntry() const
Definition DWARFDie.h:54
LLVM_ABI const char * getSubroutineName(DINameKind Kind) const
If a DIE represents a subprogram (or inlined subroutine), returns its mangled name (or short name,...
Definition DWARFDie.cpp:525
LLVM_ABI DWARFDie getSibling() const
Get the sibling of this DIE object.
Definition DWARFDie.cpp:729
LLVM_ABI bool isSubroutineDIE() const
Returns true if DIE represents a subprogram or an inlined subroutine.
Definition DWARFDie.cpp:313
LLVM_ABI bool getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC, uint64_t &SectionIndex) const
Retrieves DW_AT_low_pc and DW_AT_high_pc from CU.
Definition DWARFDie.cpp:440
LLVM_ABI LLVM_DUMP_METHOD void dump() const
Convenience zero-argument overload for debugging.
Definition DWARFDie.cpp:721
LLVM_ABI void getCallerFrame(uint32_t &CallFile, uint32_t &CallLine, uint32_t &CallColumn, uint32_t &CallDiscriminator) const
Retrieves values of DW_AT_call_file, DW_AT_call_line and DW_AT_call_column from DIE (or zeroes if the...
Definition DWARFDie.cpp:570
LLVM_ABI bool isSubprogramDIE() const
Returns true if DIE represents a subprogram (not inlined).
Definition DWARFDie.cpp:311
LLVM_ABI bool addressRangeContainsAddress(const uint64_t Address) const
Definition DWARFDie.cpp:472
LLVM_ABI std::optional< DWARFFormValue > findRecursively(ArrayRef< dwarf::Attribute > Attrs) const
Extract the first value of any attribute in Attrs from this DIE and recurse into any DW_AT_specificat...
Definition DWARFDie.cpp:342
llvm::DWARFFormValue DWARFFormValue
Definition DWARFDie.h:48
LLVM_ABI std::optional< uint64_t > getHighPC(uint64_t LowPC) const
Get the DW_AT_high_pc attribute value as an address.
Definition DWARFDie.cpp:423
LLVM_ABI std::optional< uint64_t > getTypeSize(uint64_t PointerSize)
Gets the type size (in bytes) for this DIE.
Definition DWARFDie.cpp:646
LLVM_ABI DWARFDie resolveReferencedType(dwarf::Attribute Attr) const
Definition DWARFDie.cpp:408
LLVM_ABI const char * getName(DINameKind Kind) const
Return the DIE name resolving DW_AT_specification or DW_AT_abstract_origin references if necessary.
Definition DWARFDie.cpp:531
LLVM_ABI DWARFDie getLastChild() const
Get the last child of this DIE object.
Definition DWARFDie.cpp:747
LLVM_ABI DWARFDie getPreviousSibling() const
Get the previous sibling of this DIE object.
Definition DWARFDie.cpp:735
const DWARFAbbreviationDeclaration * getAbbreviationDeclarationPtr() const
Get the abbreviation declaration for this DIE.
Definition DWARFDie.h:60
DWARFDie()=default
LLVM_ABI std::string getDeclFile(DILineInfoSpecifier::FileLineInfoKind Kind) const
Definition DWARFDie.cpp:563
LLVM_ABI DWARFDie getFirstChild() const
Get the first child of this DIE object.
Definition DWARFDie.cpp:741
LLVM_ABI uint64_t getDeclLine() const
Returns the declaration line (start line) for a DIE, assuming it specifies a subprogram.
Definition DWARFDie.cpp:558
dwarf::Tag getTag() const
Definition DWARFDie.h:73
LLVM_ABI const char * getLinkageName() const
Return the DIE linkage name resolving DW_AT_specification or DW_AT_abstract_origin references if nece...
Definition DWARFDie.cpp:549
LLVM_ABI Expected< DWARFLocationExpressionsVector > getLocations(dwarf::Attribute Attr) const
Definition DWARFDie.cpp:495
LLVM_ABI std::optional< uint64_t > getRangesBaseAttribute() const
Extract the range base attribute from this DIE as absolute section offset.
Definition DWARFDie.cpp:415
bool isNULL() const
Returns true for a valid DIE that terminates a sibling chain.
Definition DWARFDie.h:86
LLVM_ABI std::optional< uint64_t > getLanguage() const
Definition DWARFDie.cpp:485
bool isValid() const
Definition DWARFDie.h:52
LLVM_ABI iterator_range< attribute_iterator > attributes() const
Get an iterator range to all attributes in the current DIE only.
Definition DWARFDie.cpp:753
LLVM_ABI void dump(raw_ostream &OS, unsigned indent=0, DIDumpOptions DumpOpts=DIDumpOptions()) const
Dump the DIE and all of its attributes to the supplied stream.
Definition DWARFDie.cpp:663
static LLVM_ABI DWARFFormValue createFromUValue(dwarf::Form F, uint64_t V)
LLVM_ABI std::optional< ArrayRef< uint8_t > > getAsBlock() const
LLVM_ABI std::optional< uint64_t > getAsSectionOffset() const
LLVM_ABI bool isFormClass(FormClass FC) const
LLVM_ABI void dumpAddress(raw_ostream &OS, uint64_t Address) const
LLVM_ABI std::optional< uint64_t > getAsAddress() const
LLVM_ABI void dump(raw_ostream &OS, DIDumpOptions DumpOpts=DIDumpOptions()) const
static LLVM_ABI DWARFFormValue createFromSValue(dwarf::Form F, int64_t V)
LLVM_ABI std::optional< uint64_t > getAsUnsignedConstant() const
static LLVM_ABI DWARFFormValue createFromUnit(dwarf::Form F, const DWARFUnit *Unit, uint64_t *OffsetPtr)
dwarf::Form getForm() const
LLVM_ABI DWARFUnit * getUnitForOffset(uint64_t Offset) const
DWARFContext & getContext() const
Definition DWARFUnit.h:327
DWARFDie getDIEForOffset(uint64_t Offset)
Return the DIE object for a given offset Offset inside the unit's DIE vector.
Definition DWARFUnit.h:541
const DWARFUnitVector & getUnitVector() const
Return the DWARFUnitVector containing this unit.
Definition DWARFUnit.h:509
LLVM_ABI uint64_t getULEB128(uint64_t *offset_ptr, llvm::Error *Err=nullptr) const
Extract a unsigned LEB128 value from *offset_ptr.
bool isValidOffset(uint64_t offset) const
Test the validity of offset.
Tagged union holding either a T or a Error.
Definition Error.h:485
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:133
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:183
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
LLVM Value Representation.
Definition Value.h:75
An RAII object that temporarily switches an output stream to a specific color.
Definition WithColor.h:54
raw_ostream & get()
Definition WithColor.h:79
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
A raw_ostream that writes to an std::string.
LLVM_ABI StringRef AttributeString(unsigned Attribute)
Definition Dwarf.cpp:72
LLVM_ABI StringRef FormEncodingString(unsigned Encoding)
Definition Dwarf.cpp:105
LLVM_ABI StringRef ApplePropertyString(unsigned)
Definition Dwarf.cpp:792
Calculates the starting offsets for various sections within the .debug_names section.
Definition Dwarf.h:35
Attribute
Attributes.
Definition Dwarf.h:125
SourceLanguageName
Definition Dwarf.h:223
std::optional< const char * > toString(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
std::optional< object::SectionedAddress > toSectionedAddress(const std::optional< DWARFFormValue > &V)
std::optional< uint64_t > toSectionOffset(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an section offset.
LLVM_ABI StringRef AttributeValueString(uint16_t Attr, unsigned Val)
Returns the symbolic string representing Val when used as a value for attribute Attr.
Definition Dwarf.cpp:866
uint64_t computeTombstoneAddress(uint8_t AddressByteSize)
Definition Dwarf.h:1242
LLVM_ABI llvm::StringRef LanguageDescription(SourceLanguageName name)
Returns a version-independent language name.
Definition Dwarf.cpp:465
std::optional< uint64_t > toUnsigned(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an unsigned constant.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:98
LLVM_ABI void printDwarfExpression(const DWARFExpression *E, raw_ostream &OS, DIDumpOptions DumpOpts, DWARFUnit *U, bool IsEH=false)
Print a Dwarf expression/.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1305
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:202
@ invalid_argument
Definition Errc.h:56
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
SmallVector< ValueTypeFromRangeType< R >, Size > to_vector(R &&Range)
Given a range of type R, iterate the entire range and return a SmallVector with elements of the vecto...
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition Format.h:129
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ABI void dumpTypeQualifiedName(const DWARFDie &DIE, raw_ostream &OS)
Definition DWARFDie.cpp:863
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
DINameKind
A DINameKind is passed to name search methods to specify a preference regarding the type of name reso...
Definition DIContext.h:142
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
LLVM_ABI void dumpTypeUnqualifiedName(const DWARFDie &DIE, raw_ostream &OS, std::string *OriginalFullName=nullptr)
Definition DWARFDie.cpp:867
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1083
std::vector< DWARFAddressRange > DWARFAddressRangesVector
DWARFAddressRangesVector - represents a set of absolute address ranges.
std::vector< DWARFLocationExpression > DWARFLocationExpressionsVector
Represents a set of absolute location expressions.
Container for dump options that control which debug information will be dumped.
Definition DIContext.h:196
std::function< void(Error)> RecoverableErrorHandler
Definition DIContext.h:235
unsigned ChildRecurseDepth
Definition DIContext.h:198
unsigned ParentRecurseDepth
Definition DIContext.h:199
Encapsulates a DWARF attribute value and all of the data required to describe the attribute value.
static LLVM_ABI bool mayHaveLocationList(dwarf::Attribute Attr)
Identify DWARF attributes that may contain a pointer to a location list.
Definition DWARFDie.cpp:805
DWARFFormValue Value
The form and value for this attribute.
static LLVM_ABI bool mayHaveLocationExpr(dwarf::Attribute Attr)
Identifies DWARF attributes that may contain a reference to a DWARF expression.
Definition DWARFDie.cpp:822
dwarf::Attribute Attr
The attribute enumeration of this attribute.
Represents a single DWARF expression, whose value is location-dependent.
void appendQualifiedName(DieType D)
void appendUnqualifiedName(DieType D, std::string *OriginalFullName=nullptr)
Recursively append the DIE type name when applicable.