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