LLVM 20.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"
25#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(Data, U->getAddressByteSize(), U->getFormParams().Format)
103 .print(OS, DumpOpts, U);
104}
105
107 return D.getAttributeValueAsReferencedDie(F).resolveTypeUnitReference();
108}
109
110static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
111 const DWARFAttribute &AttrValue, unsigned Indent,
112 DIDumpOptions DumpOpts) {
113 if (!Die.isValid())
114 return;
115 const char BaseIndent[] = " ";
116 OS << BaseIndent;
117 OS.indent(Indent + 2);
118 dwarf::Attribute Attr = AttrValue.Attr;
119 WithColor(OS, HighlightColor::Attribute) << formatv("{0}", Attr);
120
121 dwarf::Form Form = AttrValue.Value.getForm();
122 if (DumpOpts.Verbose || DumpOpts.ShowForm)
123 OS << formatv(" [{0}]", Form);
124
125 DWARFUnit *U = Die.getDwarfUnit();
126 const DWARFFormValue &FormValue = AttrValue.Value;
127
128 OS << "\t(";
129
131 std::string File;
132 auto Color = HighlightColor::Enumerator;
133 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
134 Color = HighlightColor::String;
135 if (const auto *LT = U->getContext().getLineTableForUnit(U)) {
136 if (std::optional<uint64_t> Val = FormValue.getAsUnsignedConstant()) {
137 if (LT->getFileNameByIndex(
138 *Val, U->getCompilationDir(),
139 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
140 File)) {
141 File = '"' + File + '"';
142 Name = File;
143 }
144 }
145 }
146 } else if (std::optional<uint64_t> Val = FormValue.getAsUnsignedConstant())
147 Name = AttributeValueString(Attr, *Val);
148
149 if (!Name.empty())
150 WithColor(OS, Color) << Name;
151 else if (Attr == DW_AT_decl_line || Attr == DW_AT_decl_column ||
152 Attr == DW_AT_call_line || Attr == DW_AT_call_column) {
153 if (std::optional<uint64_t> Val = FormValue.getAsUnsignedConstant())
154 OS << *Val;
155 else
156 FormValue.dump(OS, DumpOpts);
157 } else if (Attr == DW_AT_low_pc &&
158 (FormValue.getAsAddress() ==
159 dwarf::computeTombstoneAddress(U->getAddressByteSize()))) {
160 if (DumpOpts.Verbose) {
161 FormValue.dump(OS, DumpOpts);
162 OS << " (";
163 }
164 OS << "dead code";
165 if (DumpOpts.Verbose)
166 OS << ')';
167 } else if (Attr == DW_AT_high_pc && !DumpOpts.ShowForm && !DumpOpts.Verbose &&
168 FormValue.getAsUnsignedConstant()) {
169 if (DumpOpts.ShowAddresses) {
170 // Print the actual address rather than the offset.
171 uint64_t LowPC, HighPC, Index;
172 if (Die.getLowAndHighPC(LowPC, HighPC, Index))
173 DWARFFormValue::dumpAddress(OS, U->getAddressByteSize(), HighPC);
174 else
175 FormValue.dump(OS, DumpOpts);
176 }
177 } else if (DWARFAttribute::mayHaveLocationList(Attr) &&
179 dumpLocationList(OS, FormValue, U, sizeof(BaseIndent) + Indent + 4,
180 DumpOpts);
181 else if (FormValue.isFormClass(DWARFFormValue::FC_Exprloc) ||
184 dumpLocationExpr(OS, FormValue, U, sizeof(BaseIndent) + Indent + 4,
185 DumpOpts);
186 else
187 FormValue.dump(OS, DumpOpts);
188
189 std::string Space = DumpOpts.ShowAddresses ? " " : "";
190
191 // We have dumped the attribute raw value. For some attributes
192 // having both the raw value and the pretty-printed value is
193 // interesting. These attributes are handled below.
194 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin ||
195 Attr == DW_AT_call_origin) {
196 if (const char *Name =
198 DINameKind::LinkageName))
199 OS << Space << "\"" << Name << '\"';
200 } else if (Attr == DW_AT_type || Attr == DW_AT_containing_type) {
201 DWARFDie D = resolveReferencedType(Die, FormValue);
202 if (D && !D.isNULL()) {
203 OS << Space << "\"";
205 OS << '"';
206 }
207 } else if (Attr == DW_AT_APPLE_property_attribute) {
208 if (std::optional<uint64_t> OptVal = FormValue.getAsUnsignedConstant())
210 } else if (Attr == DW_AT_ranges) {
211 const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
212 // For DW_FORM_rnglistx we need to dump the offset separately, since
213 // we have only dumped the index so far.
214 if (FormValue.getForm() == DW_FORM_rnglistx)
215 if (auto RangeListOffset =
216 U->getRnglistOffset(*FormValue.getAsSectionOffset())) {
218 dwarf::DW_FORM_sec_offset, *RangeListOffset);
219 FV.dump(OS, DumpOpts);
220 }
221 if (auto RangesOrError = Die.getAddressRanges())
222 dumpRanges(Obj, OS, RangesOrError.get(), U->getAddressByteSize(),
223 sizeof(BaseIndent) + Indent + 4, DumpOpts);
224 else
226 errc::invalid_argument, "decoding address ranges: %s",
227 toString(RangesOrError.takeError()).c_str()));
228 }
229
230 OS << ")\n";
231}
232
234 std::string *OriginalFullName) const {
235 const char *NamePtr = getShortName();
236 if (!NamePtr)
237 return;
238 if (getTag() == DW_TAG_GNU_template_parameter_pack)
239 return;
240 dumpTypeUnqualifiedName(*this, OS, OriginalFullName);
241}
242
243bool DWARFDie::isSubprogramDIE() const { return getTag() == DW_TAG_subprogram; }
244
246 auto Tag = getTag();
247 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
248}
249
250std::optional<DWARFFormValue> DWARFDie::find(dwarf::Attribute Attr) const {
251 if (!isValid())
252 return std::nullopt;
253 auto AbbrevDecl = getAbbreviationDeclarationPtr();
254 if (AbbrevDecl)
255 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
256 return std::nullopt;
257}
258
259std::optional<DWARFFormValue>
261 if (!isValid())
262 return std::nullopt;
263 auto AbbrevDecl = getAbbreviationDeclarationPtr();
264 if (AbbrevDecl) {
265 for (auto Attr : Attrs) {
266 if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
267 return Value;
268 }
269 }
270 return std::nullopt;
271}
272
273std::optional<DWARFFormValue>
276 Worklist.push_back(*this);
277
278 // Keep track if DIEs already seen to prevent infinite recursion.
279 // Empirically we rarely see a depth of more than 3 when dealing with valid
280 // DWARF. This corresponds to following the DW_AT_abstract_origin and
281 // DW_AT_specification just once.
283 Seen.insert(*this);
284
285 while (!Worklist.empty()) {
286 DWARFDie Die = Worklist.pop_back_val();
287
288 if (!Die.isValid())
289 continue;
290
291 if (auto Value = Die.find(Attrs))
292 return Value;
293
294 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
295 if (Seen.insert(D).second)
296 Worklist.push_back(D);
297
298 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification))
299 if (Seen.insert(D).second)
300 Worklist.push_back(D);
301 }
302
303 return std::nullopt;
304}
305
308 if (std::optional<DWARFFormValue> F = find(Attr))
310 return DWARFDie();
311}
312
315 DWARFDie Result;
316 if (std::optional<uint64_t> Offset = V.getAsRelativeReference()) {
317 Result = const_cast<DWARFUnit *>(V.getUnit())
318 ->getDIEForOffset(V.getUnit()->getOffset() + *Offset);
319 } else if (Offset = V.getAsDebugInfoReference(); Offset) {
320 if (DWARFUnit *SpecUnit = U->getUnitVector().getUnitForOffset(*Offset))
321 Result = SpecUnit->getDIEForOffset(*Offset);
322 } else if (std::optional<uint64_t> Sig = V.getAsSignatureReference()) {
324 U->getVersion(), *Sig, U->isDWOUnit()))
325 Result = TU->getDIEForOffset(TU->getTypeOffset() + TU->getOffset());
326 }
327 return Result;
328}
329
331 if (auto Attr = find(DW_AT_signature)) {
332 if (std::optional<uint64_t> Sig = Attr->getAsReferenceUVal()) {
334 U->getVersion(), *Sig, U->isDWOUnit()))
335 return TU->getDIEForOffset(TU->getTypeOffset() + TU->getOffset());
336 }
337 }
338 return *this;
339}
340
341std::optional<uint64_t> DWARFDie::getRangesBaseAttribute() const {
342 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
343}
344
345std::optional<uint64_t> DWARFDie::getLocBaseAttribute() const {
346 return toSectionOffset(find(DW_AT_loclists_base));
347}
348
349std::optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
351 if (LowPC == Tombstone)
352 return std::nullopt;
353 if (auto FormValue = find(DW_AT_high_pc)) {
354 if (auto Address = FormValue->getAsAddress()) {
355 // High PC is an address.
356 return Address;
357 }
358 if (auto Offset = FormValue->getAsUnsignedConstant()) {
359 // High PC is an offset from LowPC.
360 return LowPC + *Offset;
361 }
362 }
363 return std::nullopt;
364}
365
367 uint64_t &SectionIndex) const {
368 auto F = find(DW_AT_low_pc);
369 auto LowPcAddr = toSectionedAddress(F);
370 if (!LowPcAddr)
371 return false;
372 if (auto HighPcAddr = getHighPC(LowPcAddr->Address)) {
373 LowPC = LowPcAddr->Address;
374 HighPC = *HighPcAddr;
375 SectionIndex = LowPcAddr->SectionIndex;
376 return true;
377 }
378 return false;
379}
380
382 if (isNULL())
384 // Single range specified by low/high PC.
385 uint64_t LowPC, HighPC, Index;
386 if (getLowAndHighPC(LowPC, HighPC, Index))
387 return DWARFAddressRangesVector{{LowPC, HighPC, Index}};
388
389 std::optional<DWARFFormValue> Value = find(DW_AT_ranges);
390 if (Value) {
391 if (Value->getForm() == DW_FORM_rnglistx)
392 return U->findRnglistFromIndex(*Value->getAsSectionOffset());
393 return U->findRnglistFromOffset(*Value->getAsSectionOffset());
394 }
396}
397
399 auto RangesOrError = getAddressRanges();
400 if (!RangesOrError) {
401 llvm::consumeError(RangesOrError.takeError());
402 return false;
403 }
404
405 for (const auto &R : RangesOrError.get())
406 if (R.LowPC <= Address && Address < R.HighPC)
407 return true;
408 return false;
409}
410
413 std::optional<DWARFFormValue> Location = find(Attr);
414 if (!Location)
416 dwarf::AttributeString(Attr).data());
417
418 if (std::optional<uint64_t> Off = Location->getAsSectionOffset()) {
419 uint64_t Offset = *Off;
420
421 if (Location->getForm() == DW_FORM_loclistx) {
422 if (auto LoclistOffset = U->getLoclistOffset(Offset))
423 Offset = *LoclistOffset;
424 else
426 "Loclist table not found");
427 }
428 return U->findLoclistFromOffset(Offset);
429 }
430
431 if (std::optional<ArrayRef<uint8_t>> Expr = Location->getAsBlock()) {
433 DWARFLocationExpression{std::nullopt, to_vector<4>(*Expr)}};
434 }
435
436 return createStringError(
437 inconvertibleErrorCode(), "Unsupported %s encoding: %s",
438 dwarf::AttributeString(Attr).data(),
439 dwarf::FormEncodingString(Location->getForm()).data());
440}
441
443 if (!isSubroutineDIE())
444 return nullptr;
445 return getName(Kind);
446}
447
449 if (!isValid() || Kind == DINameKind::None)
450 return nullptr;
451 // Try to get mangled name only if it was asked for.
453 if (auto Name = getLinkageName())
454 return Name;
455 }
456 return getShortName();
457}
458
459const char *DWARFDie::getShortName() const {
460 if (!isValid())
461 return nullptr;
462
463 return dwarf::toString(findRecursively(dwarf::DW_AT_name), nullptr);
464}
465
466const char *DWARFDie::getLinkageName() const {
467 if (!isValid())
468 return nullptr;
469
470 return dwarf::toString(findRecursively({dwarf::DW_AT_MIPS_linkage_name,
471 dwarf::DW_AT_linkage_name}),
472 nullptr);
473}
474
476 return toUnsigned(findRecursively(DW_AT_decl_line), 0);
477}
478
479std::string
481 if (auto FormValue = findRecursively(DW_AT_decl_file))
482 if (auto OptString = FormValue->getAsFile(Kind))
483 return *OptString;
484 return {};
485}
486
488 uint32_t &CallColumn,
489 uint32_t &CallDiscriminator) const {
490 CallFile = toUnsigned(find(DW_AT_call_file), 0);
491 CallLine = toUnsigned(find(DW_AT_call_line), 0);
492 CallColumn = toUnsigned(find(DW_AT_call_column), 0);
493 CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
494}
495
496static std::optional<uint64_t>
499 // Cycle detected?
500 if (!Visited.insert(Die.getDebugInfoEntry()).second)
501 return {};
502 if (auto SizeAttr = Die.find(DW_AT_byte_size))
503 if (std::optional<uint64_t> Size = SizeAttr->getAsUnsignedConstant())
504 return Size;
505
506 switch (Die.getTag()) {
507 case DW_TAG_pointer_type:
508 case DW_TAG_reference_type:
509 case DW_TAG_rvalue_reference_type:
510 return PointerSize;
511 case DW_TAG_ptr_to_member_type: {
513 if (BaseType.getTag() == DW_TAG_subroutine_type)
514 return 2 * PointerSize;
515 return PointerSize;
516 }
517 case DW_TAG_const_type:
518 case DW_TAG_immutable_type:
519 case DW_TAG_volatile_type:
520 case DW_TAG_restrict_type:
521 case DW_TAG_template_alias:
522 case DW_TAG_typedef: {
524 return getTypeSizeImpl(BaseType, PointerSize, Visited);
525 break;
526 }
527 case DW_TAG_array_type: {
529 if (!BaseType)
530 return std::nullopt;
531 std::optional<uint64_t> BaseSize =
532 getTypeSizeImpl(BaseType, PointerSize, Visited);
533 if (!BaseSize)
534 return std::nullopt;
535 uint64_t Size = *BaseSize;
536 for (DWARFDie Child : Die) {
537 if (Child.getTag() != DW_TAG_subrange_type)
538 continue;
539
540 if (auto ElemCountAttr = Child.find(DW_AT_count))
541 if (std::optional<uint64_t> ElemCount =
542 ElemCountAttr->getAsUnsignedConstant())
543 Size *= *ElemCount;
544 if (auto UpperBoundAttr = Child.find(DW_AT_upper_bound))
545 if (std::optional<int64_t> UpperBound =
546 UpperBoundAttr->getAsSignedConstant()) {
547 int64_t LowerBound = 0;
548 if (auto LowerBoundAttr = Child.find(DW_AT_lower_bound))
549 LowerBound = LowerBoundAttr->getAsSignedConstant().value_or(0);
550 Size *= *UpperBound - LowerBound + 1;
551 }
552 }
553 return Size;
554 }
555 default:
557 return getTypeSizeImpl(BaseType, PointerSize, Visited);
558 break;
559 }
560 return std::nullopt;
561}
562
563std::optional<uint64_t> DWARFDie::getTypeSize(uint64_t PointerSize) {
565 return getTypeSizeImpl(*this, PointerSize, Visited);
566}
567
568/// Helper to dump a DIE with all of its parents, but no siblings.
569static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent,
570 DIDumpOptions DumpOpts, unsigned Depth = 0) {
571 if (!Die)
572 return Indent;
573 if (DumpOpts.ParentRecurseDepth > 0 && Depth >= DumpOpts.ParentRecurseDepth)
574 return Indent;
575 Indent = dumpParentChain(Die.getParent(), OS, Indent, DumpOpts, Depth + 1);
576 Die.dump(OS, Indent, DumpOpts);
577 return Indent + 2;
578}
579
580void DWARFDie::dump(raw_ostream &OS, unsigned Indent,
581 DIDumpOptions DumpOpts) const {
582 if (!isValid())
583 return;
584 DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
585 const uint64_t Offset = getOffset();
586 uint64_t offset = Offset;
587 if (DumpOpts.ShowParents) {
588 DIDumpOptions ParentDumpOpts = DumpOpts;
589 ParentDumpOpts.ShowParents = false;
590 ParentDumpOpts.ShowChildren = false;
591 Indent = dumpParentChain(getParent(), OS, Indent, ParentDumpOpts);
592 }
593
594 if (debug_info_data.isValidOffset(offset)) {
595 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
596 if (DumpOpts.ShowAddresses)
598 << format("\n0x%8.8" PRIx64 ": ", Offset);
599
600 if (abbrCode) {
601 auto AbbrevDecl = getAbbreviationDeclarationPtr();
602 if (AbbrevDecl) {
604 << formatv("{0}", getTag());
605 if (DumpOpts.Verbose) {
606 OS << format(" [%u] %c", abbrCode,
607 AbbrevDecl->hasChildren() ? '*' : ' ');
608 if (std::optional<uint32_t> ParentIdx = Die->getParentIdx())
609 OS << format(" (0x%8.8" PRIx64 ")",
610 U->getDIEAtIndex(*ParentIdx).getOffset());
611 }
612 OS << '\n';
613
614 // Dump all data in the DIE for the attributes.
615 for (const DWARFAttribute &AttrValue : attributes())
616 dumpAttribute(OS, *this, AttrValue, Indent, DumpOpts);
617
618 if (DumpOpts.ShowChildren && DumpOpts.ChildRecurseDepth > 0) {
619 DWARFDie Child = getFirstChild();
620 DumpOpts.ChildRecurseDepth--;
621 DIDumpOptions ChildDumpOpts = DumpOpts;
622 ChildDumpOpts.ShowParents = false;
623 while (Child) {
624 Child.dump(OS, Indent + 2, ChildDumpOpts);
625 Child = Child.getSibling();
626 }
627 }
628 } else {
629 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
630 << abbrCode << '\n';
631 }
632 } else {
633 OS.indent(Indent) << "NULL\n";
634 }
635 }
636}
637
639
641 if (isValid())
642 return U->getParent(Die);
643 return DWARFDie();
644}
645
647 if (isValid())
648 return U->getSibling(Die);
649 return DWARFDie();
650}
651
653 if (isValid())
654 return U->getPreviousSibling(Die);
655 return DWARFDie();
656}
657
659 if (isValid())
660 return U->getFirstChild(Die);
661 return DWARFDie();
662}
663
665 if (isValid())
666 return U->getLastChild(Die);
667 return DWARFDie();
668}
669
671 return make_range(attribute_iterator(*this, false),
672 attribute_iterator(*this, true));
673}
674
676 : Die(D), Index(0) {
677 auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
678 assert(AbbrDecl && "Must have abbreviation declaration");
679 if (End) {
680 // This is the end iterator so we set the index to the attribute count.
681 Index = AbbrDecl->getNumAttributes();
682 } else {
683 // This is the begin iterator so we extract the value for this->Index.
684 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
685 updateForIndex(*AbbrDecl, 0);
686 }
687}
688
689void DWARFDie::attribute_iterator::updateForIndex(
690 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
691 Index = I;
692 // AbbrDecl must be valid before calling this function.
693 auto NumAttrs = AbbrDecl.getNumAttributes();
694 if (Index < NumAttrs) {
695 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
696 // Add the previous byte size of any previous attribute value.
697 AttrValue.Offset += AttrValue.ByteSize;
698 uint64_t ParseOffset = AttrValue.Offset;
700 AttrValue.Value = DWARFFormValue::createFromSValue(
701 AbbrDecl.getFormByIndex(Index),
703 else {
704 auto U = Die.getDwarfUnit();
705 assert(U && "Die must have valid DWARF unit");
706 AttrValue.Value = DWARFFormValue::createFromUnit(
707 AbbrDecl.getFormByIndex(Index), U, &ParseOffset);
708 }
709 AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
710 } else {
711 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
712 AttrValue = {};
713 }
714}
715
717 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
718 updateForIndex(*AbbrDecl, Index + 1);
719 return *this;
720}
721
723 switch(Attr) {
724 case DW_AT_location:
725 case DW_AT_string_length:
726 case DW_AT_return_addr:
727 case DW_AT_data_member_location:
728 case DW_AT_frame_base:
729 case DW_AT_static_link:
730 case DW_AT_segment:
731 case DW_AT_use_location:
732 case DW_AT_vtable_elem_location:
733 return true;
734 default:
735 return false;
736 }
737}
738
740 switch (Attr) {
741 // From the DWARF v5 specification.
742 case DW_AT_location:
743 case DW_AT_byte_size:
744 case DW_AT_bit_offset:
745 case DW_AT_bit_size:
746 case DW_AT_string_length:
747 case DW_AT_lower_bound:
748 case DW_AT_return_addr:
749 case DW_AT_bit_stride:
750 case DW_AT_upper_bound:
751 case DW_AT_count:
752 case DW_AT_data_member_location:
753 case DW_AT_frame_base:
754 case DW_AT_segment:
755 case DW_AT_static_link:
756 case DW_AT_use_location:
757 case DW_AT_vtable_elem_location:
758 case DW_AT_allocated:
759 case DW_AT_associated:
760 case DW_AT_data_location:
761 case DW_AT_byte_stride:
762 case DW_AT_rank:
763 case DW_AT_call_value:
764 case DW_AT_call_origin:
765 case DW_AT_call_target:
766 case DW_AT_call_target_clobbered:
767 case DW_AT_call_data_location:
768 case DW_AT_call_data_value:
769 // Extensions.
770 case DW_AT_GNU_call_site_value:
771 case DW_AT_GNU_call_site_target:
772 return true;
773 default:
774 return false;
775 }
776}
777
778namespace llvm {
779
782}
783
785 std::string *OriginalFullName) {
786 DWARFTypePrinter(OS).appendUnqualifiedName(DIE, OriginalFullName);
787}
788
789} // namespace llvm
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:533
static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die, const DWARFAttribute &AttrValue, unsigned Indent, DIDumpOptions DumpOpts)
Definition: DWARFDie.cpp:110
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:569
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 std::optional< uint64_t > getTypeSizeImpl(DWARFDie Die, uint64_t PointerSize, SmallPtrSetImpl< const DWARFDebugInfoEntry * > &Visited)
Definition: DWARFDie.cpp:497
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.
std::string Name
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file defines the SmallPtrSet class.
This file defines the SmallSet class.
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:165
const T * data() const
Definition: ArrayRef.h:162
A structured debug information entry.
Definition: DIE.h:819
bool getAttrIsImplicitConstByIndex(uint32_t idx) const
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...
Definition: DWARFContext.h:48
bool isLittleEndian() const
Definition: DWARFContext.h:401
DWARFTypeUnit * getTypeUnitForHash(uint16_t Version, uint64_t Hash, bool IsDWO)
const DWARFObject & getDWARFObj() const
Definition: DWARFContext.h:147
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
std::optional< uint32_t > getParentIdx() const
Returns index of the parent die.
const DWARFAbbreviationDeclaration * getAbbreviationDeclarationPtr() const
attribute_iterator & operator++()
Definition: DWARFDie.cpp:716
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition: DWARFDie.h:42
void getFullName(raw_string_ostream &, std::string *OriginalFullName=nullptr) const
Definition: DWARFDie.cpp:233
DWARFDie resolveTypeUnitReference() const
Definition: DWARFDie.cpp:330
std::optional< uint64_t > getLocBaseAttribute() const
Definition: DWARFDie.cpp:345
uint64_t getOffset() const
Get the absolute offset into the debug info or types section.
Definition: DWARFDie.h:66
const char * getShortName() const
Return the DIE short name resolving DW_AT_specification or DW_AT_abstract_origin references if necess...
Definition: DWARFDie.cpp:459
Expected< DWARFAddressRangesVector > getAddressRanges() const
Get the address ranges for this DIE.
Definition: DWARFDie.cpp:381
DWARFDie getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE as the referenced DIE.
Definition: DWARFDie.cpp:307
DWARFDie getParent() const
Get the parent of this DIE object.
Definition: DWARFDie.cpp:640
std::optional< DWARFFormValue > find(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE.
Definition: DWARFDie.cpp:250
DWARFUnit * getDwarfUnit() const
Definition: DWARFDie.h:53
const DWARFDebugInfoEntry * getDebugInfoEntry() const
Definition: DWARFDie.h:52
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:442
DWARFDie getSibling() const
Get the sibling of this DIE object.
Definition: DWARFDie.cpp:646
bool isSubroutineDIE() const
Returns true if DIE represents a subprogram or an inlined subroutine.
Definition: DWARFDie.cpp:245
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:366
LLVM_DUMP_METHOD void dump() const
Convenience zero-argument overload for debugging.
Definition: DWARFDie.cpp:638
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:487
bool isSubprogramDIE() const
Returns true if DIE represents a subprogram (not inlined).
Definition: DWARFDie.cpp:243
bool addressRangeContainsAddress(const uint64_t Address) const
Definition: DWARFDie.cpp:398
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:274
std::optional< uint64_t > getHighPC(uint64_t LowPC) const
Get the DW_AT_high_pc attribute value as an address.
Definition: DWARFDie.cpp:349
std::optional< uint64_t > getTypeSize(uint64_t PointerSize)
Gets the type size (in bytes) for this DIE.
Definition: DWARFDie.cpp:563
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:448
DWARFDie getLastChild() const
Get the last child of this DIE object.
Definition: DWARFDie.cpp:664
DWARFDie getPreviousSibling() const
Get the previous sibling of this DIE object.
Definition: DWARFDie.cpp:652
const DWARFAbbreviationDeclaration * getAbbreviationDeclarationPtr() const
Get the abbreviation declaration for this DIE.
Definition: DWARFDie.h:58
DWARFDie()=default
std::string getDeclFile(DILineInfoSpecifier::FileLineInfoKind Kind) const
Definition: DWARFDie.cpp:480
DWARFDie getFirstChild() const
Get the first child of this DIE object.
Definition: DWARFDie.cpp:658
uint64_t getDeclLine() const
Returns the declaration line (start line) for a DIE, assuming it specifies a subprogram.
Definition: DWARFDie.cpp:475
dwarf::Tag getTag() const
Definition: DWARFDie.h:71
const char * getLinkageName() const
Return the DIE linkage name resolving DW_AT_specification or DW_AT_abstract_origin references if nece...
Definition: DWARFDie.cpp:466
Expected< DWARFLocationExpressionsVector > getLocations(dwarf::Attribute Attr) const
Definition: DWARFDie.cpp:412
std::optional< uint64_t > getRangesBaseAttribute() const
Extract the range base attribute from this DIE as absolute section offset.
Definition: DWARFDie.cpp:341
bool isNULL() const
Returns true for a valid DIE that terminates a sibling chain.
Definition: DWARFDie.h:84
bool isValid() const
Definition: DWARFDie.h:50
iterator_range< attribute_iterator > attributes() const
Get an iterator range to all attributes in the current DIE only.
Definition: DWARFDie.cpp:670
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:580
void print(raw_ostream &OS, DIDumpOptions DumpOpts, DWARFUnit *U, bool IsEH=false) const
void dumpAddress(raw_ostream &OS, uint64_t Address) const
static DWARFFormValue createFromUValue(dwarf::Form F, uint64_t V)
std::optional< ArrayRef< uint8_t > > getAsBlock() const
std::optional< uint64_t > getAsSectionOffset() const
bool isFormClass(FormClass FC) const
std::optional< uint64_t > getAsAddress() const
void dump(raw_ostream &OS, DIDumpOptions DumpOpts=DIDumpOptions()) const
static DWARFFormValue createFromSValue(dwarf::Form F, int64_t V)
std::optional< uint64_t > getAsUnsignedConstant() const
static DWARFFormValue createFromUnit(dwarf::Form F, const DWARFUnit *Unit, uint64_t *OffsetPtr)
dwarf::Form getForm() const
DWARFUnit * getUnitForOffset(uint64_t Offset) const
Definition: DWARFUnit.cpp:152
DWARFDie getPreviousSibling(const DWARFDebugInfoEntry *Die)
Definition: DWARFUnit.cpp:945
DWARFDie getFirstChild(const DWARFDebugInfoEntry *Die)
Definition: DWARFUnit.cpp:984
DWARFDataExtractor getDebugInfoExtractor() const
Definition: DWARFUnit.cpp:209
DWARFDie getSibling(const DWARFDebugInfoEntry *Die)
Definition: DWARFUnit.cpp:923
DWARFContext & getContext() const
Definition: DWARFUnit.h:319
uint8_t getAddressByteSize() const
Definition: DWARFUnit.h:326
DWARFDie getParent(const DWARFDebugInfoEntry *Die)
Definition: DWARFUnit.cpp:901
std::optional< uint64_t > getLoclistOffset(uint32_t Index)
Definition: DWARFUnit.cpp:1219
uint16_t getVersion() const
Definition: DWARFUnit.h:325
Expected< DWARFLocationExpressionsVector > findLoclistFromOffset(uint64_t Offset)
Definition: DWARFUnit.cpp:709
Expected< DWARFAddressRangesVector > findRnglistFromOffset(uint64_t Offset)
Return a vector of address ranges resulting from a (possibly encoded) range list starting at a given ...
Definition: DWARFUnit.cpp:667
const DWARFUnitVector & getUnitVector() const
Return the DWARFUnitVector containing this unit.
Definition: DWARFUnit.h:501
Expected< DWARFAddressRangesVector > findRnglistFromIndex(uint32_t Index)
Return a vector of address ranges retrieved from an encoded range list whose offset is found via a ta...
Definition: DWARFUnit.cpp:684
DWARFDie getDIEAtIndex(unsigned Index)
Return the DIE object at the given index Index.
Definition: DWARFUnit.h:521
DWARFDie getLastChild(const DWARFDebugInfoEntry *Die)
Definition: DWARFUnit.cpp:1009
bool isDWOUnit() const
Definition: DWARFUnit.h:318
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:481
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:346
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:367
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:502
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:135
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:179
bool empty() const
Definition: SmallVector.h:94
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:131
LLVM Value Representation.
Definition: Value.h:74
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:52
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:661
StringRef AttributeString(unsigned Attribute)
Definition: Dwarf.cpp:72
StringRef FormEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:105
StringRef ApplePropertyString(unsigned)
Definition: Dwarf.cpp:642
Attribute
Attributes.
Definition: Dwarf.h:123
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.
StringRef AttributeValueString(uint16_t Attr, unsigned Val)
Returns the symbolic string representing Val when used as a value for attribute Attr.
Definition: Dwarf.cpp:716
uint64_t computeTombstoneAddress(uint8_t AddressByteSize)
Definition: Dwarf.h:1205
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.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
auto formatv(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(support::detail::build_format_adapter(std::forward< Ts >(Vals))...))>
std::vector< DWARFAddressRange > DWARFAddressRangesVector
DWARFAddressRangesVector - represents a set of absolute address ranges.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1286
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:215
std::vector< DWARFLocationExpression > DWARFLocationExpressionsVector
Represents a set of absolute location expressions.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:125
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void dumpTypeQualifiedName(const DWARFDie &DIE, raw_ostream &OS)
Definition: DWARFDie.cpp:780
DINameKind
A DINameKind is passed to name search methods to specify a preference regarding the type of name reso...
Definition: DIContext.h:142
void dumpTypeUnqualifiedName(const DWARFDie &DIE, raw_ostream &OS, std::string *OriginalFullName=nullptr)
Definition: DWARFDie.cpp:784
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1069
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:196
std::function< void(Error)> RecoverableErrorHandler
Definition: DIContext.h:234
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.
uint64_t Offset
The debug info/types offset for this attribute.
static bool mayHaveLocationList(dwarf::Attribute Attr)
Identify DWARF attributes that may contain a pointer to a location list.
Definition: DWARFDie.cpp:722
DWARFFormValue Value
The form and value for this attribute.
static bool mayHaveLocationExpr(dwarf::Attribute Attr)
Identifies DWARF attributes that may contain a reference to a DWARF expression.
Definition: DWARFDie.cpp:739
dwarf::Attribute Attr
The attribute enumeration of this attribute.
Represents a single DWARF expression, whose value is location-dependent.
void appendQualifiedName(DWARFDie D)
void appendUnqualifiedName(DWARFDie D, std::string *OriginalFullName=nullptr)
Recursively append the DIE type name when applicable.