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