LCOV - code coverage report
Current view: top level - lib/DebugInfo/DWARF - DWARFDie.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 281 304 92.4 %
Date: 2018-10-20 13:21:21 Functions: 32 34 94.1 %
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           0 : 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           0 :   if (DumpOpts.Verbose)
      64           0 :     SectionNames = Obj.getSectionNames();
      65             : 
      66           0 :   for (const DWARFAddressRange &R : Ranges) {
      67             : 
      68             :     OS << '\n';
      69           0 :     OS.indent(Indent);
      70           0 :     R.dump(OS, AddressSize);
      71             : 
      72           0 :     if (SectionNames.empty() || R.SectionIndex == -1ULL)
      73           0 :       continue;
      74             : 
      75           0 :     StringRef Name = SectionNames[R.SectionIndex].Name;
      76           0 :     OS << " \"" << Name << '\"';
      77             : 
      78             :     // Print section index if name is not unique.
      79           0 :     if (!SectionNames[R.SectionIndex].IsNameUnique)
      80           0 :       OS << format(" [%" PRIu64 "]", R.SectionIndex);
      81             :   }
      82           0 : }
      83             : 
      84        2303 : static void dumpLocation(raw_ostream &OS, DWARFFormValue &FormValue,
      85             :                          DWARFUnit *U, unsigned Indent,
      86             :                          DIDumpOptions DumpOpts) {
      87        2303 :   DWARFContext &Ctx = U->getContext();
      88             :   const DWARFObject &Obj = Ctx.getDWARFObj();
      89             :   const MCRegisterInfo *MRI = Ctx.getRegisterInfo();
      90        3828 :   if (FormValue.isFormClass(DWARFFormValue::FC_Block) ||
      91        1525 :       FormValue.isFormClass(DWARFFormValue::FC_Exprloc)) {
      92        1905 :     ArrayRef<uint8_t> Expr = *FormValue.getAsBlock();
      93             :     DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()),
      94             :                        Ctx.isLittleEndian(), 0);
      95        1905 :     DWARFExpression(Data, U->getVersion(), U->getAddressByteSize())
      96        1905 :         .print(OS, MRI);
      97             :     return;
      98             :   }
      99             : 
     100         398 :   FormValue.dump(OS, DumpOpts);
     101         398 :   if (FormValue.isFormClass(DWARFFormValue::FC_SectionOffset)) {
     102         171 :     const DWARFSection &LocSection = Obj.getLocSection();
     103         171 :     const DWARFSection &LocDWOSection = Obj.getLocDWOSection();
     104         171 :     uint32_t Offset = *FormValue.getAsSectionOffset();
     105         171 :     if (!LocSection.Data.empty()) {
     106             :       DWARFDebugLoc DebugLoc;
     107             :       DWARFDataExtractor Data(Obj, LocSection, Ctx.isLittleEndian(),
     108         166 :                               Obj.getAddressSize());
     109         166 :       auto LL = DebugLoc.parseOneLocationList(Data, &Offset);
     110         166 :       if (LL) {
     111             :         uint64_t BaseAddr = 0;
     112         166 :         if (Optional<BaseAddress> BA = U->getBaseAddress())
     113         164 :           BaseAddr = BA->Address;
     114         166 :         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           5 :         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           3 :     return;
     134         592 :   OS << TagStr.substr(7, TagStr.size() - 12) << " ";
     135             : }
     136             : 
     137             : /// Recursively dump the DIE type name when applicable.
     138        3846 : static void dumpTypeName(raw_ostream &OS, const DWARFDie &Die) {
     139        3846 :   DWARFDie D = Die.getAttributeValueAsReferencedDie(DW_AT_type);
     140             : 
     141        3846 :   if (!D.isValid())
     142        2909 :     return;
     143             : 
     144        3705 :   if (const char *Name = D.getName(DINameKind::LinkageName)) {
     145        2768 :     OS << Name;
     146        2768 :     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         934 :   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         937 :   dumpTypeName(OS, D);
     165             : 
     166         937 :   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       27889 : 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       27889 :   if (!Die.isValid())
     192           0 :     return;
     193       27889 :   const char BaseIndent[] = "            ";
     194       27889 :   OS << BaseIndent;
     195       27889 :   OS.indent(Indent + 2);
     196       55778 :   WithColor(OS, HighlightColor::Attribute) << formatv("{0}", Attr);
     197             : 
     198       27889 :   if (DumpOpts.Verbose || DumpOpts.ShowForm)
     199       36498 :     OS << formatv(" [{0}]", Form);
     200             : 
     201       27889 :   DWARFUnit *U = Die.getDwarfUnit();
     202       27889 :   DWARFFormValue formValue(Form);
     203             : 
     204       55778 :   if (!formValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr,
     205             :                               U->getFormParams(), U))
     206             :     return;
     207             : 
     208       27889 :   OS << "\t(";
     209             : 
     210       27889 :   StringRef Name;
     211             :   std::string File;
     212             :   auto Color = HighlightColor::Enumerator;
     213       27889 :   if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
     214             :     Color = HighlightColor::String;
     215        2860 :     if (const auto *LT = U->getContext().getLineTableForUnit(U))
     216        5306 :       if (LT->getFileNameByIndex(
     217        5306 :               formValue.getAsUnsignedConstant().getValue(),
     218             :               U->getCompilationDir(),
     219             :               DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
     220        5306 :         File = '"' + File + '"';
     221        2653 :         Name = File;
     222             :       }
     223       25029 :   } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
     224       10182 :     Name = AttributeValueString(Attr, *Val);
     225             : 
     226       27889 :   if (!Name.empty())
     227        4330 :     WithColor(OS, Color) << Name;
     228       23559 :   else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
     229        2900 :     OS << *formValue.getAsUnsignedConstant();
     230       20659 :   else if (Attr == DW_AT_high_pc && !DumpOpts.ShowForm && !DumpOpts.Verbose &&
     231       21170 :            formValue.getAsUnsignedConstant()) {
     232         291 :     if (DumpOpts.ShowAddresses) {
     233             :       // Print the actual address rather than the offset.
     234             :       uint64_t LowPC, HighPC, Index;
     235         284 :       if (Die.getLowAndHighPC(LowPC, HighPC, Index))
     236         564 :         OS << format("0x%016" PRIx64, HighPC);
     237             :       else
     238           2 :         formValue.dump(OS, DumpOpts);
     239             :     }
     240       20368 :   } else if (Attr == DW_AT_location || Attr == DW_AT_frame_base ||
     241       18066 :              Attr == DW_AT_data_member_location ||
     242             :              Attr == DW_AT_GNU_call_site_value)
     243        2303 :     dumpLocation(OS, formValue, U, sizeof(BaseIndent) + Indent + 4, DumpOpts);
     244             :   else
     245       18065 :     formValue.dump(OS, DumpOpts);
     246             : 
     247       27984 :   std::string Space = DumpOpts.ShowAddresses ? " " : "";
     248             : 
     249             :   // We have dumped the attribute raw value. For some attributes
     250             :   // having both the raw value and the pretty-printed value is
     251             :   // interesting. These attributes are handled below.
     252       27889 :   if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
     253         339 :     if (const char *Name = Die.getAttributeValueAsReferencedDie(Attr).getName(
     254             :             DINameKind::LinkageName))
     255         336 :       OS << Space << "\"" << Name << '\"';
     256       27550 :   } else if (Attr == DW_AT_type) {
     257        2909 :     OS << Space << "\"";
     258        2909 :     dumpTypeName(OS, Die);
     259             :     OS << '"';
     260       24641 :   } else if (Attr == DW_AT_APPLE_property_attribute) {
     261          25 :     if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
     262          25 :       dumpApplePropertyAttribute(OS, *OptVal);
     263       24616 :   } else if (Attr == DW_AT_ranges) {
     264          55 :     const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
     265             :     // For DW_FORM_rnglistx we need to dump the offset separately, since
     266             :     // we have only dumped the index so far.
     267          55 :     Optional<DWARFFormValue> Value = Die.find(DW_AT_ranges);
     268          55 :     if (Value && Value->getForm() == DW_FORM_rnglistx)
     269           3 :       if (auto RangeListOffset =
     270           5 :               U->getRnglistOffset(*Value->getAsSectionOffset())) {
     271             :         DWARFFormValue FV(dwarf::DW_FORM_sec_offset);
     272           2 :         FV.setUValue(*RangeListOffset);
     273           2 :         FV.dump(OS, DumpOpts);
     274             :       }
     275         110 :     if (auto RangesOrError = Die.getAddressRanges())
     276         147 :       dumpRanges(Obj, OS, RangesOrError.get(), U->getAddressByteSize(),
     277             :                  sizeof(BaseIndent) + Indent + 4, DumpOpts);
     278             :     else
     279           6 :       WithColor::error() << "decoding address ranges: "
     280          12 :                          << toString(RangesOrError.takeError()) << '\n';
     281             :   }
     282             : 
     283       27889 :   OS << ")\n";
     284             : }
     285             : 
     286        1769 : bool DWARFDie::isSubprogramDIE() const { return getTag() == DW_TAG_subprogram; }
     287             : 
     288       22590 : bool DWARFDie::isSubroutineDIE() const {
     289             :   auto Tag = getTag();
     290       22590 :   return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
     291             : }
     292             : 
     293       68051 : Optional<DWARFFormValue> DWARFDie::find(dwarf::Attribute Attr) const {
     294       68051 :   if (!isValid())
     295             :     return None;
     296             :   auto AbbrevDecl = getAbbreviationDeclarationPtr();
     297       68051 :   if (AbbrevDecl)
     298      133524 :     return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
     299             :   return None;
     300             : }
     301             : 
     302             : Optional<DWARFFormValue>
     303       17855 : DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const {
     304       17855 :   if (!isValid())
     305             :     return None;
     306             :   auto AbbrevDecl = getAbbreviationDeclarationPtr();
     307       17855 :   if (AbbrevDecl) {
     308       32470 :     for (auto Attr : Attrs) {
     309       48528 :       if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
     310             :         return Value;
     311             :     }
     312             :   }
     313             :   return None;
     314             : }
     315             : 
     316             : Optional<DWARFFormValue>
     317       15781 : DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const {
     318             :   std::vector<DWARFDie> Worklist;
     319       15781 :   Worklist.push_back(*this);
     320             : 
     321             :   // Keep track if DIEs already seen to prevent infinite recursion.
     322             :   // Empirically we rarely see a depth of more than 3 when dealing with valid
     323             :   // DWARF. This corresponds to following the DW_AT_abstract_origin and
     324             :   // DW_AT_specification just once.
     325       15781 :   SmallSet<DWARFDie, 3> Seen;
     326             : 
     327       24035 :   while (!Worklist.empty()) {
     328       16106 :     DWARFDie Die = Worklist.back();
     329             :     Worklist.pop_back();
     330             : 
     331       16106 :     if (!Die.isValid())
     332           2 :       continue;
     333             : 
     334       16106 :     if (Seen.count(Die))
     335             :       continue;
     336             : 
     337       16104 :     Seen.insert(Die);
     338             : 
     339       16104 :     if (auto Value = Die.find(Attrs))
     340             :       return Value;
     341             : 
     342        8252 :     if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
     343         283 :       Worklist.push_back(D);
     344             : 
     345        8252 :     if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification))
     346          43 :       Worklist.push_back(D);
     347             :   }
     348             : 
     349             :   return None;
     350             : }
     351             : 
     352             : DWARFDie
     353       21024 : DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
     354       42048 :   if (auto SpecRef = toReference(find(Attr))) {
     355        4725 :     if (auto SpecUnit = U->getUnitVector().getUnitForOffset(*SpecRef))
     356        4708 :       return SpecUnit->getDIEForOffset(*SpecRef);
     357             :   }
     358       16316 :   return DWARFDie();
     359             : }
     360             : 
     361          19 : Optional<uint64_t> DWARFDie::getRangesBaseAttribute() const {
     362          19 :   return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
     363             : }
     364             : 
     365        2966 : Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
     366        2966 :   if (auto FormValue = find(DW_AT_high_pc)) {
     367        1768 :     if (auto Address = FormValue->getAsAddress()) {
     368             :       // High PC is an address.
     369             :       return Address;
     370             :     }
     371        1333 :     if (auto Offset = FormValue->getAsUnsignedConstant()) {
     372             :       // High PC is an offset from LowPC.
     373        1333 :       return LowPC + *Offset;
     374             :     }
     375             :   }
     376             :   return None;
     377             : }
     378             : 
     379        6962 : bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
     380             :                                uint64_t &SectionIndex) const {
     381        6962 :   auto F = find(DW_AT_low_pc);
     382             :   auto LowPcAddr = toAddress(F);
     383        6962 :   if (!LowPcAddr)
     384             :     return false;
     385        2692 :   if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
     386        1507 :     LowPC = *LowPcAddr;
     387        1507 :     HighPC = *HighPcAddr;
     388        1507 :     SectionIndex = F->getSectionIndex();
     389             :     return true;
     390             :   }
     391        1185 :   return false;
     392             : }
     393             : 
     394        6656 : Expected<DWARFAddressRangesVector> DWARFDie::getAddressRanges() const {
     395        6656 :   if (isNULL())
     396             :     return DWARFAddressRangesVector();
     397             :   // Single range specified by low/high PC.
     398             :   uint64_t LowPC, HighPC, Index;
     399        6654 :   if (getLowAndHighPC(LowPC, HighPC, Index))
     400        1219 :     return DWARFAddressRangesVector{{LowPC, HighPC, Index}};
     401             : 
     402        5435 :   Optional<DWARFFormValue> Value = find(DW_AT_ranges);
     403        5435 :   if (Value) {
     404        1178 :     if (Value->getForm() == DW_FORM_rnglistx)
     405           5 :       return U->findRnglistFromIndex(*Value->getAsSectionOffset());
     406        1173 :     return U->findRnglistFromOffset(*Value->getAsSectionOffset());
     407             :   }
     408             :   return DWARFAddressRangesVector();
     409             : }
     410             : 
     411        1010 : void DWARFDie::collectChildrenAddressRanges(
     412             :     DWARFAddressRangesVector &Ranges) const {
     413        1010 :   if (isNULL())
     414             :     return;
     415        1010 :   if (isSubprogramDIE()) {
     416         148 :     if (auto DIERangesOrError = getAddressRanges())
     417             :       Ranges.insert(Ranges.end(), DIERangesOrError.get().begin(),
     418          74 :                     DIERangesOrError.get().end());
     419             :     else
     420           0 :       llvm::consumeError(DIERangesOrError.takeError());
     421             :   }
     422             : 
     423        1010 :   for (auto Child : children())
     424         468 :     Child.collectChildrenAddressRanges(Ranges);
     425             : }
     426             : 
     427           3 : bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
     428           6 :   auto RangesOrError = getAddressRanges();
     429           3 :   if (!RangesOrError) {
     430           0 :     llvm::consumeError(RangesOrError.takeError());
     431           0 :     return false;
     432             :   }
     433             : 
     434           4 :   for (const auto &R : RangesOrError.get())
     435           3 :     if (R.LowPC <= Address && Address < R.HighPC)
     436             :       return true;
     437             :   return false;
     438             : }
     439             : 
     440         584 : const char *DWARFDie::getSubroutineName(DINameKind Kind) const {
     441         584 :   if (!isSubroutineDIE())
     442             :     return nullptr;
     443         584 :   return getName(Kind);
     444             : }
     445             : 
     446        8746 : const char *DWARFDie::getName(DINameKind Kind) const {
     447       17492 :   if (!isValid() || Kind == DINameKind::None)
     448             :     return nullptr;
     449             :   // Try to get mangled name only if it was asked for.
     450        8746 :   if (Kind == DINameKind::LinkageName) {
     451         380 :     if (auto Name = dwarf::toString(
     452       12696 :             findRecursively({DW_AT_MIPS_linkage_name, DW_AT_linkage_name}),
     453             :             nullptr))
     454             :       return Name;
     455             :   }
     456       25098 :   if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr))
     457        6440 :     return Name;
     458             :   return nullptr;
     459             : }
     460             : 
     461         747 : uint64_t DWARFDie::getDeclLine() const {
     462        1494 :   return toUnsigned(findRecursively(DW_AT_decl_line), 0);
     463             : }
     464             : 
     465          55 : void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
     466             :                               uint32_t &CallColumn,
     467             :                               uint32_t &CallDiscriminator) const {
     468          55 :   CallFile = toUnsigned(find(DW_AT_call_file), 0);
     469          55 :   CallLine = toUnsigned(find(DW_AT_call_line), 0);
     470          55 :   CallColumn = toUnsigned(find(DW_AT_call_column), 0);
     471          55 :   CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
     472          55 : }
     473             : 
     474             : /// Helper to dump a DIE with all of its parents, but no siblings.
     475          14 : static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent,
     476             :                                 DIDumpOptions DumpOpts) {
     477             :   if (!Die)
     478             :     return Indent;
     479           9 :   Indent = dumpParentChain(Die.getParent(), OS, Indent, DumpOpts);
     480           9 :   Die.dump(OS, Indent, DumpOpts);
     481           9 :   return Indent + 2;
     482             : }
     483             : 
     484        8057 : void DWARFDie::dump(raw_ostream &OS, unsigned Indent,
     485             :                     DIDumpOptions DumpOpts) const {
     486        8057 :   if (!isValid())
     487           1 :     return;
     488        8056 :   DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
     489        8056 :   const uint32_t Offset = getOffset();
     490        8056 :   uint32_t offset = Offset;
     491        8056 :   if (DumpOpts.ShowParents) {
     492           5 :     DIDumpOptions ParentDumpOpts = DumpOpts;
     493           5 :     ParentDumpOpts.ShowParents = false;
     494           5 :     ParentDumpOpts.ShowChildren = false;
     495           5 :     Indent = dumpParentChain(getParent(), OS, Indent, ParentDumpOpts);
     496             :   }
     497             : 
     498       16112 :   if (debug_info_data.isValidOffset(offset)) {
     499        8056 :     uint32_t abbrCode = debug_info_data.getULEB128(&offset);
     500        8056 :     if (DumpOpts.ShowAddresses)
     501       16058 :       WithColor(OS, HighlightColor::Address).get()
     502        8029 :           << format("\n0x%8.8x: ", Offset);
     503             : 
     504        8056 :     if (abbrCode) {
     505        6141 :       auto AbbrevDecl = getAbbreviationDeclarationPtr();
     506        6141 :       if (AbbrevDecl) {
     507        6141 :         WithColor(OS, HighlightColor::Tag).get().indent(Indent)
     508       12282 :             << formatv("{0}", getTag());
     509        6141 :         if (DumpOpts.Verbose)
     510        4131 :           OS << format(" [%u] %c", abbrCode,
     511        6910 :                        AbbrevDecl->hasChildren() ? '*' : ' ');
     512             :         OS << '\n';
     513             : 
     514             :         // Dump all data in the DIE for the attributes.
     515       34030 :         for (const auto &AttrSpec : AbbrevDecl->attributes()) {
     516       27889 :           if (AttrSpec.Form == DW_FORM_implicit_const) {
     517             :             // We are dumping .debug_info section ,
     518             :             // implicit_const attribute values are not really stored here,
     519             :             // but in .debug_abbrev section. So we just skip such attrs.
     520             :             continue;
     521             :           }
     522       27889 :           dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
     523             :                         Indent, DumpOpts);
     524             :         }
     525             : 
     526        6141 :         DWARFDie child = getFirstChild();
     527        6141 :         if (DumpOpts.ShowChildren && DumpOpts.RecurseDepth > 0 && child) {
     528        1915 :           DumpOpts.RecurseDepth--;
     529        1915 :           DIDumpOptions ChildDumpOpts = DumpOpts;
     530        1915 :           ChildDumpOpts.ShowParents = false;
     531        7113 :           while (child) {
     532        7113 :             child.dump(OS, Indent + 2, ChildDumpOpts);
     533        7113 :             child = child.getSibling();
     534             :           }
     535             :         }
     536             :       } else {
     537           0 :         OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
     538             :            << abbrCode << '\n';
     539             :       }
     540             :     } else {
     541        1915 :       OS.indent(Indent) << "NULL\n";
     542             :     }
     543             :   }
     544             : }
     545             : 
     546           0 : LLVM_DUMP_METHOD void DWARFDie::dump() const { dump(llvm::errs(), 0); }
     547             : 
     548          89 : DWARFDie DWARFDie::getParent() const {
     549          89 :   if (isValid())
     550          88 :     return U->getParent(Die);
     551           1 :   return DWARFDie();
     552             : }
     553             : 
     554       33487 : DWARFDie DWARFDie::getSibling() const {
     555       33487 :   if (isValid())
     556       33486 :     return U->getSibling(Die);
     557           1 :   return DWARFDie();
     558             : }
     559             : 
     560        3131 : DWARFDie DWARFDie::getPreviousSibling() const {
     561        3131 :   if (isValid())
     562        3131 :     return U->getPreviousSibling(Die);
     563           0 :   return DWARFDie();
     564             : }
     565             : 
     566       32940 : DWARFDie DWARFDie::getFirstChild() const {
     567       32940 :   if (isValid())
     568       32938 :     return U->getFirstChild(Die);
     569           2 :   return DWARFDie();
     570             : }
     571             : 
     572        4658 : DWARFDie DWARFDie::getLastChild() const {
     573        4658 :   if (isValid())
     574        4657 :     return U->getLastChild(Die);
     575           1 :   return DWARFDie();
     576             : }
     577             : 
     578         526 : iterator_range<DWARFDie::attribute_iterator> DWARFDie::attributes() const {
     579             :   return make_range(attribute_iterator(*this, false),
     580         526 :                     attribute_iterator(*this, true));
     581             : }
     582             : 
     583        1052 : DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End)
     584        1052 :     : Die(D), AttrValue(0), Index(0) {
     585        1052 :   auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
     586             :   assert(AbbrDecl && "Must have abbreviation declaration");
     587        1052 :   if (End) {
     588             :     // This is the end iterator so we set the index to the attribute count.
     589         526 :     Index = AbbrDecl->getNumAttributes();
     590             :   } else {
     591             :     // This is the begin iterator so we extract the value for this->Index.
     592         526 :     AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
     593         526 :     updateForIndex(*AbbrDecl, 0);
     594             :   }
     595        1052 : }
     596             : 
     597        2942 : void DWARFDie::attribute_iterator::updateForIndex(
     598             :     const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
     599        2942 :   Index = I;
     600             :   // AbbrDecl must be valid before calling this function.
     601             :   auto NumAttrs = AbbrDecl.getNumAttributes();
     602        2942 :   if (Index < NumAttrs) {
     603        2416 :     AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
     604             :     // Add the previous byte size of any previous attribute value.
     605        2416 :     AttrValue.Offset += AttrValue.ByteSize;
     606             :     AttrValue.Value.setForm(AbbrDecl.getFormByIndex(Index));
     607        2416 :     uint32_t ParseOffset = AttrValue.Offset;
     608        2416 :     auto U = Die.getDwarfUnit();
     609             :     assert(U && "Die must have valid DWARF unit");
     610        2416 :     bool b = AttrValue.Value.extractValue(U->getDebugInfoExtractor(),
     611             :                                           &ParseOffset, U->getFormParams(), U);
     612             :     (void)b;
     613             :     assert(b && "extractValue cannot fail on fully parsed DWARF");
     614        2416 :     AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
     615             :   } else {
     616             :     assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
     617             :     AttrValue.clear();
     618             :   }
     619        2942 : }
     620             : 
     621        2416 : DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
     622        4832 :   if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
     623        2416 :     updateForIndex(*AbbrDecl, Index + 1);
     624        2416 :   return *this;
     625             : }

Generated by: LCOV version 1.13