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"
29#include <cassert>
30#include <cinttypes>
31#include <cstdint>
32#include <string>
33#include <utility>
34
35using namespace llvm;
36using namespace dwarf;
37using namespace object;
38
40 OS << " (";
41 do {
42 uint64_t Shift = llvm::countr_zero(Val);
43 assert(Shift < 64 && "undefined behavior");
44 uint64_t Bit = 1ULL << Shift;
45 auto PropName = ApplePropertyString(Bit);
46 if (!PropName.empty())
47 OS << PropName;
48 else
49 OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
50 if (!(Val ^= Bit))
51 break;
52 OS << ", ";
53 } while (true);
54 OS << ")";
55}
56
57static void dumpRanges(const DWARFObject &Obj, raw_ostream &OS,
58 const DWARFAddressRangesVector &Ranges,
59 unsigned AddressSize, unsigned Indent,
60 const DIDumpOptions &DumpOpts) {
61 if (!DumpOpts.ShowAddresses)
62 return;
63
64 for (const DWARFAddressRange &R : Ranges) {
65 OS << '\n';
66 OS.indent(Indent);
67 R.dump(OS, AddressSize, DumpOpts, &Obj);
68 }
69}
70
71static void dumpLocationList(raw_ostream &OS, const DWARFFormValue &FormValue,
72 DWARFUnit *U, unsigned Indent,
73 DIDumpOptions DumpOpts) {
75 "bad FORM for location list");
76 DWARFContext &Ctx = U->getContext();
77 uint64_t Offset = *FormValue.getAsSectionOffset();
78
79 if (FormValue.getForm() == DW_FORM_loclistx) {
80 FormValue.dump(OS, DumpOpts);
81
82 if (auto LoclistOffset = U->getLoclistOffset(Offset))
83 Offset = *LoclistOffset;
84 else
85 return;
86 }
87 U->getLocationTable().dumpLocationList(
88 &Offset, OS, U->getBaseAddress(), Ctx.getDWARFObj(), U, DumpOpts, Indent);
89}
90
91static void dumpLocationExpr(raw_ostream &OS, const DWARFFormValue &FormValue,
92 DWARFUnit *U, unsigned Indent,
93 DIDumpOptions DumpOpts) {
96 "bad FORM for location expression");
97 DWARFContext &Ctx = U->getContext();
98 ArrayRef<uint8_t> Expr = *FormValue.getAsBlock();
99 DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()),
100 Ctx.isLittleEndian(), 0);
101 DWARFExpression(Data, U->getAddressByteSize(), U->getFormParams().Format)
102 .print(OS, DumpOpts, U);
103}
104
106 return D.getAttributeValueAsReferencedDie(F).resolveTypeUnitReference();
107}
108
109static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
110 const DWARFAttribute &AttrValue, unsigned Indent,
111 DIDumpOptions DumpOpts) {
112 if (!Die.isValid())
113 return;
114 const char BaseIndent[] = " ";
115 OS << BaseIndent;
116 OS.indent(Indent + 2);
117 dwarf::Attribute Attr = AttrValue.Attr;
118 WithColor(OS, HighlightColor::Attribute) << formatv("{0}", Attr);
119
120 dwarf::Form Form = AttrValue.Value.getForm();
121 if (DumpOpts.Verbose || DumpOpts.ShowForm)
122 OS << formatv(" [{0}]", Form);
123
124 DWARFUnit *U = Die.getDwarfUnit();
125 const DWARFFormValue &FormValue = AttrValue.Value;
126
127 OS << "\t(";
128
130 std::string File;
131 auto Color = HighlightColor::Enumerator;
132 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
133 Color = HighlightColor::String;
134 if (const auto *LT = U->getContext().getLineTableForUnit(U)) {
135 if (std::optional<uint64_t> Val = FormValue.getAsUnsignedConstant()) {
136 if (LT->getFileNameByIndex(
137 *Val, U->getCompilationDir(),
138 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
139 File)) {
140 File = '"' + File + '"';
141 Name = File;
142 }
143 }
144 }
145 } else if (std::optional<uint64_t> Val = FormValue.getAsUnsignedConstant())
146 Name = AttributeValueString(Attr, *Val);
147
148 if (!Name.empty())
149 WithColor(OS, Color) << Name;
150 else if (Attr == DW_AT_decl_line || Attr == DW_AT_decl_column ||
151 Attr == DW_AT_call_line || Attr == DW_AT_call_column) {
152 if (std::optional<uint64_t> Val = FormValue.getAsUnsignedConstant())
153 OS << *Val;
154 else
155 FormValue.dump(OS, DumpOpts);
156 } else if (Attr == DW_AT_low_pc &&
157 (FormValue.getAsAddress() ==
158 dwarf::computeTombstoneAddress(U->getAddressByteSize()))) {
159 if (DumpOpts.Verbose) {
160 FormValue.dump(OS, DumpOpts);
161 OS << " (";
162 }
163 OS << "dead code";
164 if (DumpOpts.Verbose)
165 OS << ')';
166 } else if (Attr == DW_AT_high_pc && !DumpOpts.ShowForm && !DumpOpts.Verbose &&
167 FormValue.getAsUnsignedConstant()) {
168 if (DumpOpts.ShowAddresses) {
169 // Print the actual address rather than the offset.
170 uint64_t LowPC, HighPC, Index;
171 if (Die.getLowAndHighPC(LowPC, HighPC, Index))
172 DWARFFormValue::dumpAddress(OS, U->getAddressByteSize(), HighPC);
173 else
174 FormValue.dump(OS, DumpOpts);
175 }
176 } else if (DWARFAttribute::mayHaveLocationList(Attr) &&
178 dumpLocationList(OS, FormValue, U, sizeof(BaseIndent) + Indent + 4,
179 DumpOpts);
180 else if (FormValue.isFormClass(DWARFFormValue::FC_Exprloc) ||
183 dumpLocationExpr(OS, FormValue, U, sizeof(BaseIndent) + Indent + 4,
184 DumpOpts);
185 else
186 FormValue.dump(OS, DumpOpts);
187
188 std::string Space = DumpOpts.ShowAddresses ? " " : "";
189
190 // We have dumped the attribute raw value. For some attributes
191 // having both the raw value and the pretty-printed value is
192 // interesting. These attributes are handled below.
193 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin ||
194 Attr == DW_AT_call_origin) {
195 if (const char *Name =
197 DINameKind::LinkageName))
198 OS << Space << "\"" << Name << '\"';
199 } else if (Attr == DW_AT_type || Attr == DW_AT_containing_type) {
200 DWARFDie D = resolveReferencedType(Die, FormValue);
201 if (D && !D.isNULL()) {
202 OS << Space << "\"";
204 OS << '"';
205 }
206 } else if (Attr == DW_AT_APPLE_property_attribute) {
207 if (std::optional<uint64_t> OptVal = FormValue.getAsUnsignedConstant())
209 } else if (Attr == DW_AT_ranges) {
210 const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
211 // For DW_FORM_rnglistx we need to dump the offset separately, since
212 // we have only dumped the index so far.
213 if (FormValue.getForm() == DW_FORM_rnglistx)
214 if (auto RangeListOffset =
215 U->getRnglistOffset(*FormValue.getAsSectionOffset())) {
217 dwarf::DW_FORM_sec_offset, *RangeListOffset);
218 FV.dump(OS, DumpOpts);
219 }
220 if (auto RangesOrError = Die.getAddressRanges())
221 dumpRanges(Obj, OS, RangesOrError.get(), U->getAddressByteSize(),
222 sizeof(BaseIndent) + Indent + 4, DumpOpts);
223 else
225 errc::invalid_argument, "decoding address ranges: %s",
226 toString(RangesOrError.takeError()).c_str()));
227 }
228
229 OS << ")\n";
230}
231
233 std::string *OriginalFullName) const {
234 const char *NamePtr = getShortName();
235 if (!NamePtr)
236 return;
237 if (getTag() == DW_TAG_GNU_template_parameter_pack)
238 return;
239 dumpTypeUnqualifiedName(*this, OS, OriginalFullName);
240}
241
242bool DWARFDie::isSubprogramDIE() const { return getTag() == DW_TAG_subprogram; }
243
245 auto Tag = getTag();
246 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
247}
248
249std::optional<DWARFFormValue> DWARFDie::find(dwarf::Attribute Attr) const {
250 if (!isValid())
251 return std::nullopt;
252 auto AbbrevDecl = getAbbreviationDeclarationPtr();
253 if (AbbrevDecl)
254 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
255 return std::nullopt;
256}
257
258std::optional<DWARFFormValue>
260 if (!isValid())
261 return std::nullopt;
262 auto AbbrevDecl = getAbbreviationDeclarationPtr();
263 if (AbbrevDecl) {
264 for (auto Attr : Attrs) {
265 if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
266 return Value;
267 }
268 }
269 return std::nullopt;
270}
271
272std::optional<DWARFFormValue>
275 Worklist.push_back(*this);
276
277 // Keep track if DIEs already seen to prevent infinite recursion.
278 // Empirically we rarely see a depth of more than 3 when dealing with valid
279 // DWARF. This corresponds to following the DW_AT_abstract_origin and
280 // DW_AT_specification just once.
282 Seen.insert(*this);
283
284 while (!Worklist.empty()) {
285 DWARFDie Die = Worklist.pop_back_val();
286
287 if (!Die.isValid())
288 continue;
289
290 if (auto Value = Die.find(Attrs))
291 return Value;
292
293 for (dwarf::Attribute Attr :
294 {DW_AT_abstract_origin, DW_AT_specification, DW_AT_signature}) {
295 if (auto D = Die.getAttributeValueAsReferencedDie(Attr))
296 if (Seen.insert(D).second)
297 Worklist.push_back(D);
298 }
299 }
300
301 return std::nullopt;
302}
303
306 if (std::optional<DWARFFormValue> F = find(Attr))
308 return DWARFDie();
309}
310
313 DWARFDie Result;
314 if (std::optional<uint64_t> Offset = V.getAsRelativeReference()) {
315 Result = const_cast<DWARFUnit *>(V.getUnit())
316 ->getDIEForOffset(V.getUnit()->getOffset() + *Offset);
317 } else if (Offset = V.getAsDebugInfoReference(); Offset) {
318 if (DWARFUnit *SpecUnit = U->getUnitVector().getUnitForOffset(*Offset))
319 Result = SpecUnit->getDIEForOffset(*Offset);
320 } else if (std::optional<uint64_t> Sig = V.getAsSignatureReference()) {
321 if (DWARFTypeUnit *TU =
322 U->getContext().getTypeUnitForHash(*Sig, U->isDWOUnit()))
323 Result = TU->getDIEForOffset(TU->getTypeOffset() + TU->getOffset());
324 }
325 return Result;
326}
327
329 if (auto Attr = find(DW_AT_signature)) {
330 if (std::optional<uint64_t> Sig = Attr->getAsReferenceUVal()) {
331 if (DWARFTypeUnit *TU =
332 U->getContext().getTypeUnitForHash(*Sig, U->isDWOUnit()))
333 return TU->getDIEForOffset(TU->getTypeOffset() + TU->getOffset());
334 }
335 }
336 return *this;
337}
338
341}
344}
345
346std::optional<uint64_t> DWARFDie::getRangesBaseAttribute() const {
347 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
348}
349
350std::optional<uint64_t> DWARFDie::getLocBaseAttribute() const {
351 return toSectionOffset(find(DW_AT_loclists_base));
352}
353
354std::optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
356 if (LowPC == Tombstone)
357 return std::nullopt;
358 if (auto FormValue = find(DW_AT_high_pc)) {
359 if (auto Address = FormValue->getAsAddress()) {
360 // High PC is an address.
361 return Address;
362 }
363 if (auto Offset = FormValue->getAsUnsignedConstant()) {
364 // High PC is an offset from LowPC.
365 return LowPC + *Offset;
366 }
367 }
368 return std::nullopt;
369}
370
372 uint64_t &SectionIndex) const {
373 auto F = find(DW_AT_low_pc);
374 auto LowPcAddr = toSectionedAddress(F);
375 if (!LowPcAddr)
376 return false;
377 if (auto HighPcAddr = getHighPC(LowPcAddr->Address)) {
378 LowPC = LowPcAddr->Address;
379 HighPC = *HighPcAddr;
380 SectionIndex = LowPcAddr->SectionIndex;
381 return true;
382 }
383 return false;
384}
385
387 if (isNULL())
389 // Single range specified by low/high PC.
390 uint64_t LowPC, HighPC, Index;
391 if (getLowAndHighPC(LowPC, HighPC, Index))
392 return DWARFAddressRangesVector{{LowPC, HighPC, Index}};
393
394 std::optional<DWARFFormValue> Value = find(DW_AT_ranges);
395 if (Value) {
396 if (Value->getForm() == DW_FORM_rnglistx)
397 return U->findRnglistFromIndex(*Value->getAsSectionOffset());
398 return U->findRnglistFromOffset(*Value->getAsSectionOffset());
399 }
401}
402
404 auto RangesOrError = getAddressRanges();
405 if (!RangesOrError) {
406 llvm::consumeError(RangesOrError.takeError());
407 return false;
408 }
409
410 for (const auto &R : RangesOrError.get())
411 if (R.LowPC <= Address && Address < R.HighPC)
412 return true;
413 return false;
414}
415
416std::optional<uint64_t> DWARFDie::getLanguage() const {
417 if (isValid()) {
418 if (std::optional<DWARFFormValue> LV =
419 U->getUnitDIE().find(dwarf::DW_AT_language))
420 return LV->getAsUnsignedConstant();
421 }
422 return std::nullopt;
423}
424
427 std::optional<DWARFFormValue> Location = find(Attr);
428 if (!Location)
430 dwarf::AttributeString(Attr).data());
431
432 if (std::optional<uint64_t> Off = Location->getAsSectionOffset()) {
433 uint64_t Offset = *Off;
434
435 if (Location->getForm() == DW_FORM_loclistx) {
436 if (auto LoclistOffset = U->getLoclistOffset(Offset))
437 Offset = *LoclistOffset;
438 else
440 "Loclist table not found");
441 }
442 return U->findLoclistFromOffset(Offset);
443 }
444
445 if (std::optional<ArrayRef<uint8_t>> Expr = Location->getAsBlock()) {
447 DWARFLocationExpression{std::nullopt, to_vector<4>(*Expr)}};
448 }
449
450 return createStringError(
451 inconvertibleErrorCode(), "Unsupported %s encoding: %s",
452 dwarf::AttributeString(Attr).data(),
453 dwarf::FormEncodingString(Location->getForm()).data());
454}
455
457 if (!isSubroutineDIE())
458 return nullptr;
459 return getName(Kind);
460}
461
463 if (!isValid() || Kind == DINameKind::None)
464 return nullptr;
465 // Try to get mangled name only if it was asked for.
467 if (auto Name = getLinkageName())
468 return Name;
469 }
470 return getShortName();
471}
472
473const char *DWARFDie::getShortName() const {
474 if (!isValid())
475 return nullptr;
476
477 return dwarf::toString(findRecursively(dwarf::DW_AT_name), nullptr);
478}
479
480const char *DWARFDie::getLinkageName() const {
481 if (!isValid())
482 return nullptr;
483
484 return dwarf::toString(findRecursively({dwarf::DW_AT_MIPS_linkage_name,
485 dwarf::DW_AT_linkage_name}),
486 nullptr);
487}
488
490 return toUnsigned(findRecursively(DW_AT_decl_line), 0);
491}
492
493std::string
495 if (auto FormValue = findRecursively(DW_AT_decl_file))
496 if (auto OptString = FormValue->getAsFile(Kind))
497 return *OptString;
498 return {};
499}
500
502 uint32_t &CallColumn,
503 uint32_t &CallDiscriminator) const {
504 CallFile = toUnsigned(find(DW_AT_call_file), 0);
505 CallLine = toUnsigned(find(DW_AT_call_line), 0);
506 CallColumn = toUnsigned(find(DW_AT_call_column), 0);
507 CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
508}
509
510static std::optional<uint64_t>
513 // Cycle detected?
514 if (!Visited.insert(Die.getDebugInfoEntry()).second)
515 return {};
516 if (auto SizeAttr = Die.find(DW_AT_byte_size))
517 if (std::optional<uint64_t> Size = SizeAttr->getAsUnsignedConstant())
518 return Size;
519
520 switch (Die.getTag()) {
521 case DW_TAG_pointer_type:
522 case DW_TAG_reference_type:
523 case DW_TAG_rvalue_reference_type:
524 return PointerSize;
525 case DW_TAG_ptr_to_member_type: {
527 if (BaseType.getTag() == DW_TAG_subroutine_type)
528 return 2 * PointerSize;
529 return PointerSize;
530 }
531 case DW_TAG_const_type:
532 case DW_TAG_immutable_type:
533 case DW_TAG_volatile_type:
534 case DW_TAG_restrict_type:
535 case DW_TAG_template_alias:
536 case DW_TAG_typedef: {
538 return getTypeSizeImpl(BaseType, PointerSize, Visited);
539 break;
540 }
541 case DW_TAG_array_type: {
543 if (!BaseType)
544 return std::nullopt;
545 std::optional<uint64_t> BaseSize =
546 getTypeSizeImpl(BaseType, PointerSize, Visited);
547 if (!BaseSize)
548 return std::nullopt;
549 uint64_t Size = *BaseSize;
550 for (DWARFDie Child : Die) {
551 if (Child.getTag() != DW_TAG_subrange_type)
552 continue;
553
554 if (auto ElemCountAttr = Child.find(DW_AT_count))
555 if (std::optional<uint64_t> ElemCount =
556 ElemCountAttr->getAsUnsignedConstant())
557 Size *= *ElemCount;
558 if (auto UpperBoundAttr = Child.find(DW_AT_upper_bound))
559 if (std::optional<int64_t> UpperBound =
560 UpperBoundAttr->getAsSignedConstant()) {
561 int64_t LowerBound = 0;
562 if (auto LowerBoundAttr = Child.find(DW_AT_lower_bound))
563 LowerBound = LowerBoundAttr->getAsSignedConstant().value_or(0);
564 Size *= *UpperBound - LowerBound + 1;
565 }
566 }
567 return Size;
568 }
569 default:
571 return getTypeSizeImpl(BaseType, PointerSize, Visited);
572 break;
573 }
574 return std::nullopt;
575}
576
577std::optional<uint64_t> DWARFDie::getTypeSize(uint64_t PointerSize) {
579 return getTypeSizeImpl(*this, PointerSize, Visited);
580}
581
582/// Helper to dump a DIE with all of its parents, but no siblings.
583static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent,
584 DIDumpOptions DumpOpts, unsigned Depth = 0) {
585 if (!Die)
586 return Indent;
587 if (DumpOpts.ParentRecurseDepth > 0 && Depth >= DumpOpts.ParentRecurseDepth)
588 return Indent;
589 Indent = dumpParentChain(Die.getParent(), OS, Indent, DumpOpts, Depth + 1);
590 Die.dump(OS, Indent, DumpOpts);
591 return Indent + 2;
592}
593
594void DWARFDie::dump(raw_ostream &OS, unsigned Indent,
595 DIDumpOptions DumpOpts) const {
596 if (!isValid())
597 return;
598 DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
599 const uint64_t Offset = getOffset();
600 uint64_t offset = Offset;
601 if (DumpOpts.ShowParents) {
602 DIDumpOptions ParentDumpOpts = DumpOpts;
603 ParentDumpOpts.ShowParents = false;
604 ParentDumpOpts.ShowChildren = false;
605 Indent = dumpParentChain(getParent(), OS, Indent, ParentDumpOpts);
606 }
607
608 if (debug_info_data.isValidOffset(offset)) {
609 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
610 if (DumpOpts.ShowAddresses)
612 << format("\n0x%8.8" PRIx64 ": ", Offset);
613
614 if (abbrCode) {
615 auto AbbrevDecl = getAbbreviationDeclarationPtr();
616 if (AbbrevDecl) {
618 << formatv("{0}", getTag());
619 if (DumpOpts.Verbose) {
620 OS << format(" [%u] %c", abbrCode,
621 AbbrevDecl->hasChildren() ? '*' : ' ');
622 if (std::optional<uint32_t> ParentIdx = Die->getParentIdx())
623 OS << format(" (0x%8.8" PRIx64 ")",
624 U->getDIEAtIndex(*ParentIdx).getOffset());
625 }
626 OS << '\n';
627
628 // Dump all data in the DIE for the attributes.
629 for (const DWARFAttribute &AttrValue : attributes())
630 dumpAttribute(OS, *this, AttrValue, Indent, DumpOpts);
631
632 if (DumpOpts.ShowChildren && DumpOpts.ChildRecurseDepth > 0) {
633 DWARFDie Child = getFirstChild();
634 DumpOpts.ChildRecurseDepth--;
635 DIDumpOptions ChildDumpOpts = DumpOpts;
636 ChildDumpOpts.ShowParents = false;
637 while (Child) {
638 Child.dump(OS, Indent + 2, ChildDumpOpts);
639 Child = Child.getSibling();
640 }
641 }
642 } else {
643 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
644 << abbrCode << '\n';
645 }
646 } else {
647 OS.indent(Indent) << "NULL\n";
648 }
649 }
650}
651
653
655 if (isValid())
656 return U->getParent(Die);
657 return DWARFDie();
658}
659
661 if (isValid())
662 return U->getSibling(Die);
663 return DWARFDie();
664}
665
667 if (isValid())
668 return U->getPreviousSibling(Die);
669 return DWARFDie();
670}
671
673 if (isValid())
674 return U->getFirstChild(Die);
675 return DWARFDie();
676}
677
679 if (isValid())
680 return U->getLastChild(Die);
681 return DWARFDie();
682}
683
685 return make_range(attribute_iterator(*this, false),
686 attribute_iterator(*this, true));
687}
688
690 : Die(D), Index(0) {
691 auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
692 assert(AbbrDecl && "Must have abbreviation declaration");
693 if (End) {
694 // This is the end iterator so we set the index to the attribute count.
695 Index = AbbrDecl->getNumAttributes();
696 } else {
697 // This is the begin iterator so we extract the value for this->Index.
698 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
699 updateForIndex(*AbbrDecl, 0);
700 }
701}
702
703void DWARFDie::attribute_iterator::updateForIndex(
704 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
705 Index = I;
706 // AbbrDecl must be valid before calling this function.
707 auto NumAttrs = AbbrDecl.getNumAttributes();
708 if (Index < NumAttrs) {
709 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
710 // Add the previous byte size of any previous attribute value.
711 AttrValue.Offset += AttrValue.ByteSize;
712 uint64_t ParseOffset = AttrValue.Offset;
714 AttrValue.Value = DWARFFormValue::createFromSValue(
715 AbbrDecl.getFormByIndex(Index),
717 else {
718 auto U = Die.getDwarfUnit();
719 assert(U && "Die must have valid DWARF unit");
720 AttrValue.Value = DWARFFormValue::createFromUnit(
721 AbbrDecl.getFormByIndex(Index), U, &ParseOffset);
722 }
723 AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
724 } else {
725 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
726 AttrValue = {};
727 }
728}
729
731 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
732 updateForIndex(*AbbrDecl, Index + 1);
733 return *this;
734}
735
737 switch(Attr) {
738 case DW_AT_location:
739 case DW_AT_string_length:
740 case DW_AT_return_addr:
741 case DW_AT_data_member_location:
742 case DW_AT_frame_base:
743 case DW_AT_static_link:
744 case DW_AT_segment:
745 case DW_AT_use_location:
746 case DW_AT_vtable_elem_location:
747 return true;
748 default:
749 return false;
750 }
751}
752
754 switch (Attr) {
755 // From the DWARF v5 specification.
756 case DW_AT_location:
757 case DW_AT_byte_size:
758 case DW_AT_bit_offset:
759 case DW_AT_bit_size:
760 case DW_AT_string_length:
761 case DW_AT_lower_bound:
762 case DW_AT_return_addr:
763 case DW_AT_bit_stride:
764 case DW_AT_upper_bound:
765 case DW_AT_count:
766 case DW_AT_data_member_location:
767 case DW_AT_frame_base:
768 case DW_AT_segment:
769 case DW_AT_static_link:
770 case DW_AT_use_location:
771 case DW_AT_vtable_elem_location:
772 case DW_AT_allocated:
773 case DW_AT_associated:
774 case DW_AT_data_location:
775 case DW_AT_byte_stride:
776 case DW_AT_rank:
777 case DW_AT_call_value:
778 case DW_AT_call_origin:
779 case DW_AT_call_target:
780 case DW_AT_call_target_clobbered:
781 case DW_AT_call_data_location:
782 case DW_AT_call_data_value:
783 // Extensions.
784 case DW_AT_GNU_call_site_value:
785 case DW_AT_GNU_call_site_target:
786 return true;
787 default:
788 return false;
789 }
790}
791
792namespace llvm {
793
796}
797
799 std::string *OriginalFullName) {
801}
802
803} // 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:622
static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die, const DWARFAttribute &AttrValue, unsigned Indent, DIDumpOptions DumpOpts)
Definition: DWARFDie.cpp:109
static void dumpLocationExpr(raw_ostream &OS, const DWARFFormValue &FormValue, DWARFUnit *U, unsigned Indent, DIDumpOptions DumpOpts)
Definition: DWARFDie.cpp:91
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:583
static DWARFDie resolveReferencedType(DWARFDie D, DWARFFormValue F)
Definition: DWARFDie.cpp:105
static void dumpLocationList(raw_ostream &OS, const DWARFFormValue &FormValue, DWARFUnit *U, unsigned Indent, DIDumpOptions DumpOpts)
Definition: DWARFDie.cpp:71
static std::optional< uint64_t > getTypeSizeImpl(DWARFDie Die, uint64_t PointerSize, SmallPtrSetImpl< const DWARFDebugInfoEntry * > &Visited)
Definition: DWARFDie.cpp:511
static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val)
Definition: DWARFDie.cpp:39
static void dumpRanges(const DWARFObject &Obj, raw_ostream &OS, const DWARFAddressRangesVector &Ranges, unsigned AddressSize, unsigned Indent, const DIDumpOptions &DumpOpts)
Definition: DWARFDie.cpp:57
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:168
const T * data() const
Definition: ArrayRef.h:165
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:404
DWARFTypeUnit * getTypeUnitForHash(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:730
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:232
DWARFDie resolveTypeUnitReference() const
Definition: DWARFDie.cpp:328
std::optional< uint64_t > getLocBaseAttribute() const
Definition: DWARFDie.cpp:350
uint64_t getOffset() const
Get the absolute offset into the debug info or types section.
Definition: DWARFDie.h:67
const char * getShortName() const
Return the DIE short name resolving DW_AT_specification or DW_AT_abstract_origin references if necess...
Definition: DWARFDie.cpp:473
Expected< DWARFAddressRangesVector > getAddressRanges() const
Get the address ranges for this DIE.
Definition: DWARFDie.cpp:386
DWARFDie getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE as the referenced DIE.
Definition: DWARFDie.cpp:305
DWARFDie getParent() const
Get the parent of this DIE object.
Definition: DWARFDie.cpp:654
std::optional< DWARFFormValue > find(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE.
Definition: DWARFDie.cpp:249
DWARFUnit * getDwarfUnit() const
Definition: DWARFDie.h:54
const DWARFDebugInfoEntry * getDebugInfoEntry() const
Definition: DWARFDie.h:53
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:456
DWARFDie getSibling() const
Get the sibling of this DIE object.
Definition: DWARFDie.cpp:660
bool isSubroutineDIE() const
Returns true if DIE represents a subprogram or an inlined subroutine.
Definition: DWARFDie.cpp:244
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:371
LLVM_DUMP_METHOD void dump() const
Convenience zero-argument overload for debugging.
Definition: DWARFDie.cpp:652
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:501
bool isSubprogramDIE() const
Returns true if DIE represents a subprogram (not inlined).
Definition: DWARFDie.cpp:242
bool addressRangeContainsAddress(const uint64_t Address) const
Definition: DWARFDie.cpp:403
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:273
std::optional< uint64_t > getHighPC(uint64_t LowPC) const
Get the DW_AT_high_pc attribute value as an address.
Definition: DWARFDie.cpp:354
std::optional< uint64_t > getTypeSize(uint64_t PointerSize)
Gets the type size (in bytes) for this DIE.
Definition: DWARFDie.cpp:577
DWARFDie resolveReferencedType(dwarf::Attribute Attr) const
Definition: DWARFDie.cpp:339
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:462
DWARFDie getLastChild() const
Get the last child of this DIE object.
Definition: DWARFDie.cpp:678
DWARFDie getPreviousSibling() const
Get the previous sibling of this DIE object.
Definition: DWARFDie.cpp:666
const DWARFAbbreviationDeclaration * getAbbreviationDeclarationPtr() const
Get the abbreviation declaration for this DIE.
Definition: DWARFDie.h:59
DWARFDie()=default
std::string getDeclFile(DILineInfoSpecifier::FileLineInfoKind Kind) const
Definition: DWARFDie.cpp:494
DWARFDie getFirstChild() const
Get the first child of this DIE object.
Definition: DWARFDie.cpp:672
uint64_t getDeclLine() const
Returns the declaration line (start line) for a DIE, assuming it specifies a subprogram.
Definition: DWARFDie.cpp:489
dwarf::Tag getTag() const
Definition: DWARFDie.h:72
const char * getLinkageName() const
Return the DIE linkage name resolving DW_AT_specification or DW_AT_abstract_origin references if nece...
Definition: DWARFDie.cpp:480
Expected< DWARFLocationExpressionsVector > getLocations(dwarf::Attribute Attr) const
Definition: DWARFDie.cpp:426
std::optional< uint64_t > getRangesBaseAttribute() const
Extract the range base attribute from this DIE as absolute section offset.
Definition: DWARFDie.cpp:346
bool isNULL() const
Returns true for a valid DIE that terminates a sibling chain.
Definition: DWARFDie.h:85
std::optional< uint64_t > getLanguage() const
Definition: DWARFDie.cpp:416
bool isValid() const
Definition: DWARFDie.h:51
iterator_range< attribute_iterator > attributes() const
Get an iterator range to all attributes in the current DIE only.
Definition: DWARFDie.cpp:684
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:594
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
DWARFDie getUnitDIE(bool ExtractUnitDIEOnly=true)
Definition: DWARFUnit.h:443
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
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:363
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:384
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:132
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:181
bool empty() const
Definition: SmallVector.h:81
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:144
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:1212
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.
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:1291
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.
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
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:794
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:798
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:736
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:753
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.