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/SmallPtrSet.h" 11 #include "llvm/ADT/SmallSet.h" 12 #include "llvm/ADT/StringRef.h" 13 #include "llvm/BinaryFormat/Dwarf.h" 14 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h" 15 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 16 #include "llvm/DebugInfo/DWARF/DWARFDebugLine.h" 17 #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h" 18 #include "llvm/DebugInfo/DWARF/DWARFExpression.h" 19 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" 20 #include "llvm/DebugInfo/DWARF/DWARFTypePrinter.h" 21 #include "llvm/DebugInfo/DWARF/DWARFTypeUnit.h" 22 #include "llvm/DebugInfo/DWARF/DWARFUnit.h" 23 #include "llvm/Object/ObjectFile.h" 24 #include "llvm/Support/DataExtractor.h" 25 #include "llvm/Support/Format.h" 26 #include "llvm/Support/FormatVariadic.h" 27 #include "llvm/Support/WithColor.h" 28 #include "llvm/Support/raw_ostream.h" 29 #include <cassert> 30 #include <cinttypes> 31 #include <cstdint> 32 #include <string> 33 #include <utility> 34 35 using namespace llvm; 36 using namespace dwarf; 37 using namespace object; 38 39 static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) { 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 57 static 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 71 static void dumpLocationList(raw_ostream &OS, const DWARFFormValue &FormValue, 72 DWARFUnit *U, unsigned Indent, 73 DIDumpOptions DumpOpts) { 74 assert(FormValue.isFormClass(DWARFFormValue::FC_SectionOffset) && 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 91 static void dumpLocationExpr(raw_ostream &OS, const DWARFFormValue &FormValue, 92 DWARFUnit *U, unsigned Indent, 93 DIDumpOptions DumpOpts) { 94 assert((FormValue.isFormClass(DWARFFormValue::FC_Block) || 95 FormValue.isFormClass(DWARFFormValue::FC_Exprloc)) && 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 105 static DWARFDie resolveReferencedType(DWARFDie D, DWARFFormValue F) { 106 return D.getAttributeValueAsReferencedDie(F).resolveTypeUnitReference(); 107 } 108 109 static 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 129 StringRef Name; 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) && 177 FormValue.isFormClass(DWARFFormValue::FC_SectionOffset)) 178 dumpLocationList(OS, FormValue, U, sizeof(BaseIndent) + Indent + 4, 179 DumpOpts); 180 else if (FormValue.isFormClass(DWARFFormValue::FC_Exprloc) || 181 (DWARFAttribute::mayHaveLocationExpr(Attr) && 182 FormValue.isFormClass(DWARFFormValue::FC_Block))) 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 = 196 Die.getAttributeValueAsReferencedDie(FormValue).getName( 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 << "\""; 203 dumpTypeQualifiedName(D, OS); 204 OS << '"'; 205 } 206 } else if (Attr == DW_AT_APPLE_property_attribute) { 207 if (std::optional<uint64_t> OptVal = FormValue.getAsUnsignedConstant()) 208 dumpApplePropertyAttribute(OS, *OptVal); 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())) { 216 DWARFFormValue FV = DWARFFormValue::createFromUValue( 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 224 DumpOpts.RecoverableErrorHandler(createStringError( 225 errc::invalid_argument, "decoding address ranges: %s", 226 toString(RangesOrError.takeError()).c_str())); 227 } 228 229 OS << ")\n"; 230 } 231 232 void DWARFDie::getFullName(raw_string_ostream &OS, 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 242 bool DWARFDie::isSubprogramDIE() const { return getTag() == DW_TAG_subprogram; } 243 244 bool DWARFDie::isSubroutineDIE() const { 245 auto Tag = getTag(); 246 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine; 247 } 248 249 std::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 258 std::optional<DWARFFormValue> 259 DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const { 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 272 std::optional<DWARFFormValue> 273 DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const { 274 SmallVector<DWARFDie, 3> Worklist; 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. 281 SmallSet<DWARFDie, 3> Seen; 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 304 DWARFDie 305 DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const { 306 if (std::optional<DWARFFormValue> F = find(Attr)) 307 return getAttributeValueAsReferencedDie(*F); 308 return DWARFDie(); 309 } 310 311 DWARFDie 312 DWARFDie::getAttributeValueAsReferencedDie(const DWARFFormValue &V) const { 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 328 DWARFDie DWARFDie::resolveTypeUnitReference() const { 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 339 DWARFDie DWARFDie::resolveReferencedType(dwarf::Attribute Attr) const { 340 return getAttributeValueAsReferencedDie(Attr).resolveTypeUnitReference(); 341 } 342 DWARFDie DWARFDie::resolveReferencedType(const DWARFFormValue &V) const { 343 return getAttributeValueAsReferencedDie(V).resolveTypeUnitReference(); 344 } 345 346 std::optional<uint64_t> DWARFDie::getRangesBaseAttribute() const { 347 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base})); 348 } 349 350 std::optional<uint64_t> DWARFDie::getLocBaseAttribute() const { 351 return toSectionOffset(find(DW_AT_loclists_base)); 352 } 353 354 std::optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const { 355 uint64_t Tombstone = dwarf::computeTombstoneAddress(U->getAddressByteSize()); 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 371 bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC, 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 386 Expected<DWARFAddressRangesVector> DWARFDie::getAddressRanges() const { 387 if (isNULL()) 388 return DWARFAddressRangesVector(); 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 } 400 return DWARFAddressRangesVector(); 401 } 402 403 bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const { 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 416 std::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 425 Expected<DWARFLocationExpressionsVector> 426 DWARFDie::getLocations(dwarf::Attribute Attr) const { 427 std::optional<DWARFFormValue> Location = find(Attr); 428 if (!Location) 429 return createStringError(inconvertibleErrorCode(), "No %s", 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 439 return createStringError(inconvertibleErrorCode(), 440 "Loclist table not found"); 441 } 442 return U->findLoclistFromOffset(Offset); 443 } 444 445 if (std::optional<ArrayRef<uint8_t>> Expr = Location->getAsBlock()) { 446 return DWARFLocationExpressionsVector{ 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 456 const char *DWARFDie::getSubroutineName(DINameKind Kind) const { 457 if (!isSubroutineDIE()) 458 return nullptr; 459 return getName(Kind); 460 } 461 462 const char *DWARFDie::getName(DINameKind Kind) const { 463 if (!isValid() || Kind == DINameKind::None) 464 return nullptr; 465 // Try to get mangled name only if it was asked for. 466 if (Kind == DINameKind::LinkageName) { 467 if (auto Name = getLinkageName()) 468 return Name; 469 } 470 return getShortName(); 471 } 472 473 const char *DWARFDie::getShortName() const { 474 if (!isValid()) 475 return nullptr; 476 477 return dwarf::toString(findRecursively(dwarf::DW_AT_name), nullptr); 478 } 479 480 const 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 489 uint64_t DWARFDie::getDeclLine() const { 490 return toUnsigned(findRecursively(DW_AT_decl_line), 0); 491 } 492 493 std::string 494 DWARFDie::getDeclFile(DILineInfoSpecifier::FileLineInfoKind Kind) const { 495 if (auto FormValue = findRecursively(DW_AT_decl_file)) 496 if (auto OptString = FormValue->getAsFile(Kind)) 497 return *OptString; 498 return {}; 499 } 500 501 void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine, 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 510 static std::optional<uint64_t> 511 getTypeSizeImpl(DWARFDie Die, uint64_t PointerSize, 512 SmallPtrSetImpl<const DWARFDebugInfoEntry *> &Visited) { 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: { 526 if (DWARFDie BaseType = Die.getAttributeValueAsReferencedDie(DW_AT_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: { 537 if (DWARFDie BaseType = Die.getAttributeValueAsReferencedDie(DW_AT_type)) 538 return getTypeSizeImpl(BaseType, PointerSize, Visited); 539 break; 540 } 541 case DW_TAG_array_type: { 542 DWARFDie BaseType = Die.getAttributeValueAsReferencedDie(DW_AT_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: 570 if (DWARFDie BaseType = Die.getAttributeValueAsReferencedDie(DW_AT_type)) 571 return getTypeSizeImpl(BaseType, PointerSize, Visited); 572 break; 573 } 574 return std::nullopt; 575 } 576 577 std::optional<uint64_t> DWARFDie::getTypeSize(uint64_t PointerSize) { 578 SmallPtrSet<const DWARFDebugInfoEntry *, 4> Visited; 579 return getTypeSizeImpl(*this, PointerSize, Visited); 580 } 581 582 /// Helper to dump a DIE with all of its parents, but no siblings. 583 static 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 594 void 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) 611 WithColor(OS, HighlightColor::Address).get() 612 << format("\n0x%8.8" PRIx64 ": ", Offset); 613 614 if (abbrCode) { 615 auto AbbrevDecl = getAbbreviationDeclarationPtr(); 616 if (AbbrevDecl) { 617 WithColor(OS, HighlightColor::Tag).get().indent(Indent) 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 652 LLVM_DUMP_METHOD void DWARFDie::dump() const { dump(llvm::errs(), 0); } 653 654 DWARFDie DWARFDie::getParent() const { 655 if (isValid()) 656 return U->getParent(Die); 657 return DWARFDie(); 658 } 659 660 DWARFDie DWARFDie::getSibling() const { 661 if (isValid()) 662 return U->getSibling(Die); 663 return DWARFDie(); 664 } 665 666 DWARFDie DWARFDie::getPreviousSibling() const { 667 if (isValid()) 668 return U->getPreviousSibling(Die); 669 return DWARFDie(); 670 } 671 672 DWARFDie DWARFDie::getFirstChild() const { 673 if (isValid()) 674 return U->getFirstChild(Die); 675 return DWARFDie(); 676 } 677 678 DWARFDie DWARFDie::getLastChild() const { 679 if (isValid()) 680 return U->getLastChild(Die); 681 return DWARFDie(); 682 } 683 684 iterator_range<DWARFDie::attribute_iterator> DWARFDie::attributes() const { 685 return make_range(attribute_iterator(*this, false), 686 attribute_iterator(*this, true)); 687 } 688 689 DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End) 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 703 void 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; 713 if (AbbrDecl.getAttrIsImplicitConstByIndex(Index)) 714 AttrValue.Value = DWARFFormValue::createFromSValue( 715 AbbrDecl.getFormByIndex(Index), 716 AbbrDecl.getAttrImplicitConstValueByIndex(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 730 DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() { 731 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr()) 732 updateForIndex(*AbbrDecl, Index + 1); 733 return *this; 734 } 735 736 bool DWARFAttribute::mayHaveLocationList(dwarf::Attribute Attr) { 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 753 bool DWARFAttribute::mayHaveLocationExpr(dwarf::Attribute Attr) { 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 792 namespace llvm { 793 794 void dumpTypeQualifiedName(const DWARFDie &DIE, raw_ostream &OS) { 795 DWARFTypePrinter<DWARFDie>(OS).appendQualifiedName(DIE); 796 } 797 798 void dumpTypeUnqualifiedName(const DWARFDie &DIE, raw_ostream &OS, 799 std::string *OriginalFullName) { 800 DWARFTypePrinter<DWARFDie>(OS).appendUnqualifiedName(DIE, OriginalFullName); 801 } 802 803 } // namespace llvm 804