LCOV - code coverage report
Current view: top level - lib/DebugInfo/DWARF - DWARFDie.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 278 290 95.9 %
Date: 2018-07-13 00:08:38 Functions: 31 32 96.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- DWARFDie.cpp -------------------------------------------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : 
      10             : #include "llvm/DebugInfo/DWARF/DWARFDie.h"
      11             : #include "llvm/ADT/None.h"
      12             : #include "llvm/ADT/Optional.h"
      13             : #include "llvm/ADT/SmallSet.h"
      14             : #include "llvm/ADT/StringRef.h"
      15             : #include "llvm/BinaryFormat/Dwarf.h"
      16             : #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
      17             : #include "llvm/DebugInfo/DWARF/DWARFContext.h"
      18             : #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
      19             : #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
      20             : #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
      21             : #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
      22             : #include "llvm/Object/ObjectFile.h"
      23             : #include "llvm/Support/DataExtractor.h"
      24             : #include "llvm/Support/Format.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          25 : static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
      41          25 :   OS << " (";
      42             :   do {
      43             :     uint64_t Shift = countTrailingZeros(Val);
      44             :     assert(Shift < 64 && "undefined behavior");
      45          98 :     uint64_t Bit = 1ULL << Shift;
      46          98 :     auto PropName = ApplePropertyString(Bit);
      47          98 :     if (!PropName.empty())
      48          98 :       OS << PropName;
      49             :     else
      50           0 :       OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
      51          98 :     if (!(Val ^= Bit))
      52             :       break;
      53          73 :     OS << ", ";
      54             :   } while (true);
      55          25 :   OS << ")";
      56          25 : }
      57             : 
      58          45 : static void dumpRanges(const DWARFObject &Obj, raw_ostream &OS,
      59             :                        const DWARFAddressRangesVector &Ranges,
      60             :                        unsigned AddressSize, unsigned Indent,
      61             :                        const DIDumpOptions &DumpOpts) {
      62             :   ArrayRef<SectionName> SectionNames;
      63          45 :   if (DumpOpts.Verbose)
      64          28 :     SectionNames = Obj.getSectionNames();
      65             : 
      66          45 :   for (const DWARFAddressRange &R : Ranges) {
      67             : 
      68             :     OS << '\n';
      69         113 :     OS.indent(Indent);
      70         226 :     R.dump(OS, AddressSize);
      71             : 
      72         183 :     if (SectionNames.empty() || R.SectionIndex == -1ULL)
      73          70 :       continue;
      74             : 
      75          43 :     StringRef Name = SectionNames[R.SectionIndex].Name;
      76          43 :     OS << " \"" << Name << '\"';
      77             : 
      78             :     // Print section index if name is not unique.
      79          86 :     if (!SectionNames[R.SectionIndex].IsNameUnique)
      80           2 :       OS << format(" [%" PRIu64 "]", R.SectionIndex);
      81             :   }
      82          45 : }
      83             : 
      84        2235 : static void dumpLocation(raw_ostream &OS, DWARFFormValue &FormValue,
      85             :                          DWARFUnit *U, unsigned Indent,
      86             :                          DIDumpOptions DumpOpts) {
      87        2235 :   DWARFContext &Ctx = U->getContext();
      88             :   const DWARFObject &Obj = Ctx.getDWARFObj();
      89             :   const MCRegisterInfo *MRI = Ctx.getRegisterInfo();
      90        3688 :   if (FormValue.isFormClass(DWARFFormValue::FC_Block) ||
      91        1453 :       FormValue.isFormClass(DWARFFormValue::FC_Exprloc)) {
      92        1846 :     ArrayRef<uint8_t> Expr = *FormValue.getAsBlock();
      93             :     DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()),
      94             :                        Ctx.isLittleEndian(), 0);
      95        1846 :     DWARFExpression(Data, U->getVersion(), U->getAddressByteSize())
      96        1846 :         .print(OS, MRI);
      97             :     return;
      98             :   }
      99             : 
     100         389 :   FormValue.dump(OS, DumpOpts);
     101         389 :   if (FormValue.isFormClass(DWARFFormValue::FC_SectionOffset)) {
     102         163 :     const DWARFSection &LocSection = Obj.getLocSection();
     103         163 :     const DWARFSection &LocDWOSection = Obj.getLocDWOSection();
     104         163 :     uint32_t Offset = *FormValue.getAsSectionOffset();
     105         163 :     if (!LocSection.Data.empty()) {
     106             :       DWARFDebugLoc DebugLoc;
     107             :       DWARFDataExtractor Data(Obj, LocSection, Ctx.isLittleEndian(),
     108         158 :                               Obj.getAddressSize());
     109         158 :       auto LL = DebugLoc.parseOneLocationList(Data, &Offset);
     110         158 :       if (LL) {
     111             :         uint64_t BaseAddr = 0;
     112         158 :         if (Optional<BaseAddress> BA = U->getBaseAddress())
     113         157 :           BaseAddr = BA->Address;
     114         316 :         LL->dump(OS, Ctx.isLittleEndian(), Obj.getAddressSize(), MRI, BaseAddr,
     115             :                  Indent);
     116             :       } else
     117           0 :         OS << "error extracting location list.";
     118           5 :     } else if (!LocDWOSection.Data.empty()) {
     119             :       DataExtractor Data(LocDWOSection.Data, Ctx.isLittleEndian(), 0);
     120           5 :       auto LL = DWARFDebugLocDWO::parseOneLocationList(Data, &Offset);
     121           5 :       if (LL)
     122          10 :         LL->dump(OS, Ctx.isLittleEndian(), Obj.getAddressSize(), MRI, Indent);
     123             :       else
     124           0 :         OS << "error extracting location list.";
     125             :     }
     126             :   }
     127             : }
     128             : 
     129             : /// Dump the name encoded in the type tag.
     130         299 : static void dumpTypeTagName(raw_ostream &OS, dwarf::Tag T) {
     131         299 :   StringRef TagStr = TagString(T);
     132             :   if (!TagStr.startswith("DW_TAG_") || !TagStr.endswith("_type"))
     133           0 :     return;
     134         598 :   OS << TagStr.substr(7, TagStr.size() - 12) << " ";
     135             : }
     136             : 
     137             : /// Recursively dump the DIE type name when applicable.
     138        3791 : static void dumpTypeName(raw_ostream &OS, const DWARFDie &Die) {
     139        3791 :   DWARFDie D = Die.getAttributeValueAsReferencedDie(DW_AT_type);
     140             : 
     141             :   if (!D.isValid())
     142        2856 :     return;
     143             : 
     144        3670 :   if (const char *Name = D.getName(DINameKind::LinkageName)) {
     145        2735 :     OS << Name;
     146        2735 :     return;
     147             :   }
     148             : 
     149             :   // FIXME: We should have pretty printers per language. Currently we print
     150             :   // everything as if it was C++ and fall back to the TAG type name.
     151             :   const dwarf::Tag T = D.getTag();
     152         935 :   switch (T) {
     153             :   case DW_TAG_array_type:
     154             :   case DW_TAG_pointer_type:
     155             :   case DW_TAG_ptr_to_member_type:
     156             :   case DW_TAG_reference_type:
     157             :   case DW_TAG_rvalue_reference_type:
     158             :     break;
     159         299 :   default:
     160         299 :     dumpTypeTagName(OS, T);
     161             :   }
     162             : 
     163             :   // Follow the DW_AT_type if possible.
     164         935 :   dumpTypeName(OS, D);
     165             : 
     166         935 :   switch (T) {
     167          40 :   case DW_TAG_array_type:
     168          40 :     OS << "[]";
     169          40 :     break;
     170             :   case DW_TAG_pointer_type:
     171             :     OS << '*';
     172             :     break;
     173             :   case DW_TAG_ptr_to_member_type:
     174             :     OS << '*';
     175             :     break;
     176             :   case DW_TAG_reference_type:
     177             :     OS << '&';
     178             :     break;
     179           1 :   case DW_TAG_rvalue_reference_type:
     180           1 :     OS << "&&";
     181           1 :     break;
     182             :   default:
     183             :     break;
     184             :   }
     185             : }
     186             : 
     187       26978 : static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
     188             :                           uint32_t *OffsetPtr, dwarf::Attribute Attr,
     189             :                           dwarf::Form Form, unsigned Indent,
     190             :                           DIDumpOptions DumpOpts) {
     191             :   if (!Die.isValid())
     192           0 :     return;
     193       26978 :   const char BaseIndent[] = "            ";
     194       26978 :   OS << BaseIndent;
     195       26978 :   OS.indent(Indent + 2);
     196       80934 :   WithColor(OS, HighlightColor::Attribute) << formatv("{0}", Attr);
     197             : 
     198       26978 :   if (DumpOpts.Verbose || DumpOpts.ShowForm)
     199       35554 :     OS << formatv(" [{0}]", Form);
     200             : 
     201       26978 :   DWARFUnit *U = Die.getDwarfUnit();
     202       26978 :   DWARFFormValue formValue(Form);
     203             : 
     204       53956 :   if (!formValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr,
     205             :                               U->getFormParams(), U))
     206             :     return;
     207             : 
     208       26978 :   OS << "\t(";
     209             : 
     210       26978 :   StringRef Name;
     211             :   std::string File;
     212             :   auto Color = HighlightColor::Enumerator;
     213       26978 :   if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
     214             :     Color = HighlightColor::String;
     215        2782 :     if (const auto *LT = U->getContext().getLineTableForUnit(U))
     216        7830 :       if (LT->getFileNameByIndex(
     217        5220 :               formValue.getAsUnsignedConstant().getValue(),
     218             :               U->getCompilationDir(),
     219             :               DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
     220        7830 :         File = '"' + File + '"';
     221        2610 :         Name = File;
     222             :       }
     223       24196 :   } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
     224        9864 :     Name = AttributeValueString(Attr, *Val);
     225             : 
     226       26978 :   if (!Name.empty())
     227        8418 :     WithColor(OS, Color) << Name;
     228       22769 :   else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
     229        2822 :     OS << *formValue.getAsUnsignedConstant();
     230       40371 :   else if (Attr == DW_AT_high_pc && !DumpOpts.ShowForm && !DumpOpts.Verbose &&
     231       20164 :            formValue.getAsUnsignedConstant()) {
     232         260 :     if (DumpOpts.ShowAddresses) {
     233             :       // Print the actual address rather than the offset.
     234             :       uint64_t LowPC, HighPC, Index;
     235         258 :       if (Die.getLowAndHighPC(LowPC, HighPC, Index))
     236         512 :         OS << format("0x%016" PRIx64, HighPC);
     237             :       else
     238           2 :         formValue.dump(OS, DumpOpts);
     239             :     }
     240       19687 :   } else if (Attr == DW_AT_location || Attr == DW_AT_frame_base ||
     241       17453 :              Attr == DW_AT_data_member_location ||
     242             :              Attr == DW_AT_GNU_call_site_value)
     243        2235 :     dumpLocation(OS, formValue, U, sizeof(BaseIndent) + Indent + 4, DumpOpts);
     244             :   else
     245       17452 :     formValue.dump(OS, DumpOpts);
     246             : 
     247             :   // We have dumped the attribute raw value. For some attributes
     248             :   // having both the raw value and the pretty-printed value is
     249             :   // interesting. These attributes are handled below.
     250       26978 :   if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
     251         668 :     if (const char *Name = Die.getAttributeValueAsReferencedDie(Attr).getName(
     252         334 :             DINameKind::LinkageName))
     253         331 :       OS << " \"" << Name << '\"';
     254       26644 :   } else if (Attr == DW_AT_type) {
     255        2856 :     OS << " \"";
     256        2856 :     dumpTypeName(OS, Die);
     257             :     OS << '"';
     258       23788 :   } else if (Attr == DW_AT_APPLE_property_attribute) {
     259          25 :     if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
     260          25 :       dumpApplePropertyAttribute(OS, *OptVal);
     261       23763 :   } else if (Attr == DW_AT_ranges) {
     262          51 :     const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
     263             :     // For DW_FORM_rnglistx we need to dump the offset separately, since
     264             :     // we have only dumped the index so far.
     265          51 :     Optional<DWARFFormValue> Value = Die.find(DW_AT_ranges);
     266          51 :     if (Value && Value->getForm() == DW_FORM_rnglistx)
     267           3 :       if (auto RangeListOffset =
     268           6 :               U->getRnglistOffset(*Value->getAsSectionOffset())) {
     269             :         DWARFFormValue FV(dwarf::DW_FORM_sec_offset);
     270           2 :         FV.setUValue(*RangeListOffset);
     271           2 :         FV.dump(OS, DumpOpts);
     272             :       }
     273         153 :     if (auto RangesOrError = Die.getAddressRanges())
     274          90 :       dumpRanges(Obj, OS, RangesOrError.get(), U->getAddressByteSize(),
     275             :                  sizeof(BaseIndent) + Indent + 4, DumpOpts);
     276             :     else
     277           6 :       WithColor::error() << "decoding address ranges: "
     278          18 :                          << toString(RangesOrError.takeError()) << '\n';
     279             :   }
     280             : 
     281       26978 :   OS << ")\n";
     282             : }
     283             : 
     284        3520 : bool DWARFDie::isSubprogramDIE() const { return getTag() == DW_TAG_subprogram; }
     285             : 
     286       22570 : bool DWARFDie::isSubroutineDIE() const {
     287             :   auto Tag = getTag();
     288       22570 :   return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
     289             : }
     290             : 
     291       68475 : Optional<DWARFFormValue> DWARFDie::find(dwarf::Attribute Attr) const {
     292             :   if (!isValid())
     293             :     return None;
     294             :   auto AbbrevDecl = getAbbreviationDeclarationPtr();
     295       68475 :   if (AbbrevDecl)
     296      132798 :     return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
     297             :   return None;
     298             : }
     299             : 
     300             : Optional<DWARFFormValue>
     301       18186 : DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const {
     302             :   if (!isValid())
     303             :     return None;
     304             :   auto AbbrevDecl = getAbbreviationDeclarationPtr();
     305       18186 :   if (AbbrevDecl) {
     306       47356 :     for (auto Attr : Attrs) {
     307       48306 :       if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
     308             :         return Value;
     309             :     }
     310             :   }
     311             :   return None;
     312             : }
     313             : 
     314             : Optional<DWARFFormValue>
     315       16371 : DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const {
     316             :   std::vector<DWARFDie> Worklist;
     317       16371 :   Worklist.push_back(*this);
     318             : 
     319             :   // Keep track if DIEs already seen to prevent infinite recursion.
     320             :   // Empirically we rarely see a depth of more than 3 when dealing with valid
     321             :   // DWARF. This corresponds to following the DW_AT_abstract_origin and
     322             :   // DW_AT_specification just once.
     323       16371 :   SmallSet<DWARFDie, 3> Seen;
     324             : 
     325       25206 :   while (!Worklist.empty()) {
     326       16686 :     DWARFDie Die = Worklist.back();
     327             :     Worklist.pop_back();
     328             : 
     329           0 :     if (!Die.isValid())
     330           2 :       continue;
     331             : 
     332       16688 :     if (Seen.count(Die))
     333           2 :       continue;
     334             : 
     335       16684 :     Seen.insert(Die);
     336             : 
     337       16684 :     if (auto Value = Die.find(Attrs))
     338             :       return Value;
     339             : 
     340        8833 :     if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
     341         270 :       Worklist.push_back(D);
     342             : 
     343        8833 :     if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification))
     344          46 :       Worklist.push_back(D);
     345             :   }
     346             : 
     347             :   return None;
     348             : }
     349             : 
     350             : DWARFDie
     351       21798 : DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
     352       43596 :   if (auto SpecRef = toReference(find(Attr))) {
     353        4330 :     if (auto SpecUnit = U->getUnitSection().getUnitForOffset(*SpecRef))
     354        4322 :       return SpecUnit->getDIEForOffset(*SpecRef);
     355             :   }
     356       17476 :   return DWARFDie();
     357             : }
     358             : 
     359          19 : Optional<uint64_t> DWARFDie::getRangesBaseAttribute() const {
     360          38 :   return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
     361             : }
     362             : 
     363        2827 : Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
     364        2827 :   if (auto FormValue = find(DW_AT_high_pc)) {
     365        1643 :     if (auto Address = FormValue->getAsAddress()) {
     366             :       // High PC is an address.
     367             :       return Address;
     368             :     }
     369        1243 :     if (auto Offset = FormValue->getAsUnsignedConstant()) {
     370             :       // High PC is an offset from LowPC.
     371        1243 :       return LowPC + *Offset;
     372             :     }
     373             :   }
     374             :   return None;
     375             : }
     376             : 
     377        6756 : bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
     378             :                                uint64_t &SectionIndex) const {
     379        6756 :   auto F = find(DW_AT_low_pc);
     380             :   auto LowPcAddr = toAddress(F);
     381        6756 :   if (!LowPcAddr)
     382             :     return false;
     383        2569 :   if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
     384        1398 :     LowPC = *LowPcAddr;
     385        1398 :     HighPC = *HighPcAddr;
     386        1398 :     SectionIndex = F->getSectionIndex();
     387             :     return true;
     388             :   }
     389        1171 :   return false;
     390             : }
     391             : 
     392        6476 : Expected<DWARFAddressRangesVector> DWARFDie::getAddressRanges() const {
     393        6476 :   if (isNULL())
     394             :     return DWARFAddressRangesVector();
     395             :   // Single range specified by low/high PC.
     396             :   uint64_t LowPC, HighPC, Index;
     397        6474 :   if (getLowAndHighPC(LowPC, HighPC, Index))
     398        2272 :     return DWARFAddressRangesVector{{LowPC, HighPC, Index}};
     399             : 
     400        5338 :   Optional<DWARFFormValue> Value = find(DW_AT_ranges);
     401        5338 :   if (Value) {
     402        1174 :     if (Value->getForm() == DW_FORM_rnglistx)
     403           5 :       return U->findRnglistFromIndex(*Value->getAsSectionOffset());
     404        1169 :     return U->findRnglistFromOffset(*Value->getAsSectionOffset());
     405             :   }
     406             :   return DWARFAddressRangesVector();
     407             : }
     408             : 
     409        1008 : void DWARFDie::collectChildrenAddressRanges(
     410             :     DWARFAddressRangesVector &Ranges) const {
     411        1008 :   if (isNULL())
     412             :     return;
     413        1008 :   if (isSubprogramDIE()) {
     414         222 :     if (auto DIERangesOrError = getAddressRanges())
     415          74 :       Ranges.insert(Ranges.end(), DIERangesOrError.get().begin(),
     416          74 :                     DIERangesOrError.get().end());
     417             :     else
     418           0 :       llvm::consumeError(DIERangesOrError.takeError());
     419             :   }
     420             : 
     421        1940 :   for (auto Child : children())
     422         466 :     Child.collectChildrenAddressRanges(Ranges);
     423             : }
     424             : 
     425           3 : bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
     426           6 :   auto RangesOrError = getAddressRanges();
     427           3 :   if (!RangesOrError) {
     428           0 :     llvm::consumeError(RangesOrError.takeError());
     429           0 :     return false;
     430             :   }
     431             : 
     432           4 :   for (const auto &R : RangesOrError.get())
     433           3 :     if (R.LowPC <= Address && Address < R.HighPC)
     434             :       return true;
     435             :   return false;
     436             : }
     437             : 
     438         583 : const char *DWARFDie::getSubroutineName(DINameKind Kind) const {
     439         583 :   if (!isSubroutineDIE())
     440             :     return nullptr;
     441         583 :   return getName(Kind);
     442             : }
     443             : 
     444        8870 : const char *DWARFDie::getName(DINameKind Kind) const {
     445        8870 :   if (!isValid() || Kind == DINameKind::None)
     446             :     return nullptr;
     447             :   // Try to get mangled name only if it was asked for.
     448        8870 :   if (Kind == DINameKind::LinkageName) {
     449         320 :     if (auto Name = dwarf::toString(
     450       13570 :             findRecursively({DW_AT_MIPS_linkage_name, DW_AT_linkage_name}),
     451             :             nullptr))
     452             :       return Name;
     453             :   }
     454       25650 :   if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr))
     455             :     return Name;
     456             :   return nullptr;
     457             : }
     458             : 
     459         746 : uint64_t DWARFDie::getDeclLine() const {
     460        2238 :   return toUnsigned(findRecursively(DW_AT_decl_line), 0);
     461             : }
     462             : 
     463          55 : void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
     464             :                               uint32_t &CallColumn,
     465             :                               uint32_t &CallDiscriminator) const {
     466         110 :   CallFile = toUnsigned(find(DW_AT_call_file), 0);
     467         110 :   CallLine = toUnsigned(find(DW_AT_call_line), 0);
     468         110 :   CallColumn = toUnsigned(find(DW_AT_call_column), 0);
     469         110 :   CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
     470          55 : }
     471             : 
     472             : /// Helper to dump a DIE with all of its parents, but no siblings.
     473           8 : static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent,
     474             :                                 DIDumpOptions DumpOpts) {
     475             :   if (!Die)
     476             :     return Indent;
     477           4 :   Indent = dumpParentChain(Die.getParent(), OS, Indent, DumpOpts);
     478           4 :   Die.dump(OS, Indent, DumpOpts);
     479           4 :   return Indent + 2;
     480             : }
     481             : 
     482        7821 : void DWARFDie::dump(raw_ostream &OS, unsigned Indent,
     483             :                     DIDumpOptions DumpOpts) const {
     484             :   if (!isValid())
     485           1 :     return;
     486        7820 :   DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
     487        7820 :   const uint32_t Offset = getOffset();
     488        7820 :   uint32_t offset = Offset;
     489        7820 :   if (DumpOpts.ShowParents) {
     490           4 :     DIDumpOptions ParentDumpOpts = DumpOpts;
     491           4 :     ParentDumpOpts.ShowParents = false;
     492           4 :     ParentDumpOpts.ShowChildren = false;
     493           4 :     Indent = dumpParentChain(getParent(), OS, Indent, ParentDumpOpts);
     494             :   }
     495             : 
     496       15640 :   if (debug_info_data.isValidOffset(offset)) {
     497        7820 :     uint32_t abbrCode = debug_info_data.getULEB128(&offset);
     498        7820 :     if (DumpOpts.ShowAddresses)
     499       15632 :       WithColor(OS, HighlightColor::Address).get()
     500        7816 :           << format("\n0x%8.8x: ", Offset);
     501             : 
     502        7820 :     if (abbrCode) {
     503        5964 :       auto AbbrevDecl = getAbbreviationDeclarationPtr();
     504        5964 :       if (AbbrevDecl) {
     505       17892 :         WithColor(OS, HighlightColor::Tag).get().indent(Indent)
     506       11928 :             << formatv("{0}", getTag());
     507        5964 :         if (DumpOpts.Verbose)
     508        4040 :           OS << format(" [%u] %c", abbrCode,
     509        4040 :                        AbbrevDecl->hasChildren() ? '*' : ' ');
     510             :         OS << '\n';
     511             : 
     512             :         // Dump all data in the DIE for the attributes.
     513       59920 :         for (const auto &AttrSpec : AbbrevDecl->attributes()) {
     514       26978 :           if (AttrSpec.Form == DW_FORM_implicit_const) {
     515             :             // We are dumping .debug_info section ,
     516             :             // implicit_const attribute values are not really stored here,
     517             :             // but in .debug_abbrev section. So we just skip such attrs.
     518           0 :             continue;
     519             :           }
     520       26978 :           dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
     521             :                         Indent, DumpOpts);
     522             :         }
     523             : 
     524        5964 :         DWARFDie child = getFirstChild();
     525        5964 :         if (DumpOpts.ShowChildren && DumpOpts.RecurseDepth > 0 && child) {
     526        1856 :           DumpOpts.RecurseDepth--;
     527        1856 :           DIDumpOptions ChildDumpOpts = DumpOpts;
     528        1856 :           ChildDumpOpts.ShowParents = false;
     529        6932 :           while (child) {
     530        6932 :             child.dump(OS, Indent + 2, ChildDumpOpts);
     531        6932 :             child = child.getSibling();
     532             :           }
     533             :         }
     534             :       } else {
     535           0 :         OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
     536             :            << abbrCode << '\n';
     537             :       }
     538             :     } else {
     539        1856 :       OS.indent(Indent) << "NULL\n";
     540             :     }
     541             :   }
     542             : }
     543             : 
     544           0 : LLVM_DUMP_METHOD void DWARFDie::dump() const { dump(llvm::errs(), 0); }
     545             : 
     546          77 : DWARFDie DWARFDie::getParent() const {
     547             :   if (isValid())
     548          76 :     return U->getParent(Die);
     549           1 :   return DWARFDie();
     550             : }
     551             : 
     552       34397 : DWARFDie DWARFDie::getSibling() const {
     553             :   if (isValid())
     554       34396 :     return U->getSibling(Die);
     555           1 :   return DWARFDie();
     556             : }
     557             : 
     558       32299 : DWARFDie DWARFDie::getFirstChild() const {
     559             :   if (isValid())
     560       32297 :     return U->getFirstChild(Die);
     561           2 :   return DWARFDie();
     562             : }
     563             : 
     564         400 : iterator_range<DWARFDie::attribute_iterator> DWARFDie::attributes() const {
     565             :   return make_range(attribute_iterator(*this, false),
     566         400 :                     attribute_iterator(*this, true));
     567             : }
     568             : 
     569         800 : DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End)
     570        1600 :     : Die(D), AttrValue(0), Index(0) {
     571         800 :   auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
     572             :   assert(AbbrDecl && "Must have abbreviation declaration");
     573         800 :   if (End) {
     574             :     // This is the end iterator so we set the index to the attribute count.
     575         400 :     Index = AbbrDecl->getNumAttributes();
     576             :   } else {
     577             :     // This is the begin iterator so we extract the value for this->Index.
     578         800 :     AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
     579         400 :     updateForIndex(*AbbrDecl, 0);
     580             :   }
     581         800 : }
     582             : 
     583        2188 : void DWARFDie::attribute_iterator::updateForIndex(
     584             :     const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
     585        2188 :   Index = I;
     586             :   // AbbrDecl must be valid before calling this function.
     587             :   auto NumAttrs = AbbrDecl.getNumAttributes();
     588        2188 :   if (Index < NumAttrs) {
     589        1788 :     AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
     590             :     // Add the previous byte size of any previous attribute value.
     591        1788 :     AttrValue.Offset += AttrValue.ByteSize;
     592             :     AttrValue.Value.setForm(AbbrDecl.getFormByIndex(Index));
     593        1788 :     uint32_t ParseOffset = AttrValue.Offset;
     594        1788 :     auto U = Die.getDwarfUnit();
     595             :     assert(U && "Die must have valid DWARF unit");
     596        3576 :     bool b = AttrValue.Value.extractValue(U->getDebugInfoExtractor(),
     597             :                                           &ParseOffset, U->getFormParams(), U);
     598             :     (void)b;
     599             :     assert(b && "extractValue cannot fail on fully parsed DWARF");
     600        1788 :     AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
     601             :   } else {
     602             :     assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
     603             :     AttrValue.clear();
     604             :   }
     605        2188 : }
     606             : 
     607        1788 : DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
     608        3576 :   if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
     609        1788 :     updateForIndex(*AbbrDecl, Index + 1);
     610        1788 :   return *this;
     611             : }

Generated by: LCOV version 1.13