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