LCOV - code coverage report
Current view: top level - include/llvm/DebugInfo/DWARF - DWARFDie.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 28 28 100.0 %
Date: 2017-09-14 15:23:50 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- DWARFDie.h -----------------------------------------------*- C++ -*-===//
       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             : #ifndef LLVM_DEBUGINFO_DWARFDIE_H
      11             : #define LLVM_DEBUGINFO_DWARFDIE_H
      12             : 
      13             : #include "llvm/ADT/ArrayRef.h"
      14             : #include "llvm/ADT/Optional.h"
      15             : #include "llvm/ADT/iterator.h"
      16             : #include "llvm/ADT/iterator_range.h"
      17             : #include "llvm/BinaryFormat/Dwarf.h"
      18             : #include "llvm/DebugInfo/DIContext.h"
      19             : #include "llvm/DebugInfo/DWARF/DWARFAttribute.h"
      20             : #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
      21             : #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
      22             : #include <cassert>
      23             : #include <cstdint>
      24             : #include <iterator>
      25             : 
      26             : namespace llvm {
      27             : 
      28             : class DWARFUnit;
      29             : class raw_ostream;
      30             : 
      31             : //===----------------------------------------------------------------------===//
      32             : /// Utility class that carries the DWARF compile/type unit and the debug info
      33             : /// entry in an object.
      34             : ///
      35             : /// When accessing information from a debug info entry we always need to DWARF
      36             : /// compile/type unit in order to extract the info correctly as some information
      37             : /// is relative to the compile/type unit. Prior to this class the DWARFUnit and
      38             : /// the DWARFDebugInfoEntry was passed around separately and there was the
      39             : /// possibility for error if the wrong DWARFUnit was used to extract a unit
      40             : /// relative offset. This class helps to ensure that this doesn't happen and
      41             : /// also simplifies the attribute extraction calls by not having to specify the
      42             : /// DWARFUnit for each call.
      43             : class DWARFDie {
      44             :   DWARFUnit *U = nullptr;
      45             :   const DWARFDebugInfoEntry *Die = nullptr;
      46             : 
      47             : public:
      48          66 :   DWARFDie() = default;
      49         128 :   DWARFDie(DWARFUnit *Unit, const DWARFDebugInfoEntry * D) : U(Unit), Die(D) {}
      50             : 
      51    79275907 :   bool isValid() const { return U && Die; }
      52    35432840 :   explicit operator bool() const { return isValid(); }
      53             :   const DWARFDebugInfoEntry *getDebugInfoEntry() const { return Die; }
      54             :   DWARFUnit *getDwarfUnit() const { return U; }
      55             : 
      56             :   /// Get the abbreviation declaration for this DIE.
      57             :   ///
      58             :   /// \returns the abbreviation declaration or NULL for null tags.
      59             :   const DWARFAbbreviationDeclaration *getAbbreviationDeclarationPtr() const {
      60             :     assert(isValid() && "must check validity prior to calling");
      61    30573193 :     return Die->getAbbreviationDeclarationPtr();
      62             :   }
      63             : 
      64             :   /// Get the absolute offset into the debug info or types section.
      65             :   ///
      66             :   /// \returns the DIE offset or -1U if invalid.
      67             :   uint32_t getOffset() const {
      68             :     assert(isValid() && "must check validity prior to calling");
      69     7453342 :     return Die->getOffset();
      70             :   }
      71             : 
      72             :   dwarf::Tag getTag() const {
      73    35901942 :     auto AbbrevDecl = getAbbreviationDeclarationPtr();
      74    17946153 :     if (AbbrevDecl)
      75             :       return AbbrevDecl->getTag();
      76             :     return dwarf::DW_TAG_null;
      77             :   }
      78             : 
      79             :   bool hasChildren() const {
      80             :     assert(isValid() && "must check validity prior to calling");
      81        2906 :     return Die->hasChildren();
      82             :   }
      83             : 
      84             :   /// Returns true for a valid DIE that terminates a sibling chain.
      85             :   bool isNULL() const {
      86     5176881 :     return getAbbreviationDeclarationPtr() == nullptr;
      87             :   }
      88             : 
      89             :   /// Returns true if DIE represents a subprogram (not inlined).
      90             :   bool isSubprogramDIE() const;
      91             : 
      92             :   /// Returns true if DIE represents a subprogram or an inlined subroutine.
      93             :   bool isSubroutineDIE() const;
      94             : 
      95             :   /// Get the parent of this DIE object.
      96             :   ///
      97             :   /// \returns a valid DWARFDie instance if this object has a parent or an
      98             :   /// invalid DWARFDie instance if it doesn't.
      99             :   DWARFDie getParent() const;
     100             : 
     101             :   /// Get the sibling of this DIE object.
     102             :   ///
     103             :   /// \returns a valid DWARFDie instance if this object has a sibling or an
     104             :   /// invalid DWARFDie instance if it doesn't.
     105             :   DWARFDie getSibling() const;
     106             : 
     107             :   /// Get the first child of this DIE object.
     108             :   ///
     109             :   /// \returns a valid DWARFDie instance if this object has children or an
     110             :   /// invalid DWARFDie instance if it doesn't.
     111             :   DWARFDie getFirstChild() const {
     112    17940496 :     if (isValid() && Die->hasChildren())
     113     4044214 :       return DWARFDie(U, Die + 1);
     114             :     return DWARFDie();
     115             :   }
     116             : 
     117             :   /// Dump the DIE and all of its attributes to the supplied stream.
     118             :   ///
     119             :   /// \param OS the stream to use for output.
     120             :   /// \param recurseDepth the depth to recurse to when dumping this DIE and its
     121             :   /// children.
     122             :   /// \param indent the number of characters to indent each line that is output.
     123             :   void dump(raw_ostream &OS, unsigned recurseDepth, unsigned indent = 0,
     124             :             DIDumpOptions DumpOpts = DIDumpOptions()) const;
     125             : 
     126             : 
     127             :   /// Convenience zero-argument overload for debugging.
     128             :   LLVM_DUMP_METHOD void dump() const;
     129             : 
     130             :   /// Extract the specified attribute from this DIE.
     131             :   ///
     132             :   /// Extract an attribute value from this DIE only. This call doesn't look
     133             :   /// for the attribute value in any DW_AT_specification or
     134             :   /// DW_AT_abstract_origin referenced DIEs.
     135             :   ///
     136             :   /// \param Attr the attribute to extract.
     137             :   /// \returns an optional DWARFFormValue that will have the form value if the
     138             :   /// attribute was successfully extracted.
     139             :   Optional<DWARFFormValue> find(dwarf::Attribute Attr) const;
     140             : 
     141             :   /// Extract the first value of any attribute in Attrs from this DIE.
     142             :   ///
     143             :   /// Extract the first attribute that matches from this DIE only. This call
     144             :   /// doesn't look for the attribute value in any DW_AT_specification or
     145             :   /// DW_AT_abstract_origin referenced DIEs. The attributes will be searched
     146             :   /// linearly in the order they are specified within Attrs.
     147             :   ///
     148             :   /// \param Attrs an array of DWARF attribute to look for.
     149             :   /// \returns an optional that has a valid DWARFFormValue for the first
     150             :   /// matching attribute in Attrs, or None if none of the attributes in Attrs
     151             :   /// exist in this DIE.
     152             :   Optional<DWARFFormValue> find(ArrayRef<dwarf::Attribute> Attrs) const;
     153             : 
     154             :   /// Extract the first value of any attribute in Attrs from this DIE and
     155             :   /// recurse into any DW_AT_specification or DW_AT_abstract_origin referenced
     156             :   /// DIEs.
     157             :   ///
     158             :   /// \param Attrs an array of DWARF attribute to look for.
     159             :   /// \returns an optional that has a valid DWARFFormValue for the first
     160             :   /// matching attribute in Attrs, or None if none of the attributes in Attrs
     161             :   /// exist in this DIE or in any DW_AT_specification or DW_AT_abstract_origin
     162             :   /// DIEs.
     163             :   Optional<DWARFFormValue>
     164             :   findRecursively(ArrayRef<dwarf::Attribute> Attrs) const;
     165             : 
     166             :   /// Extract the specified attribute from this DIE as the referenced DIE.
     167             :   ///
     168             :   /// Regardless of the reference type, return the correct DWARFDie instance if
     169             :   /// the attribute exists. The returned DWARFDie object might be from another
     170             :   /// DWARFUnit, but that is all encapsulated in the new DWARFDie object.
     171             :   ///
     172             :   /// Extract an attribute value from this DIE only. This call doesn't look
     173             :   /// for the attribute value in any DW_AT_specification or
     174             :   /// DW_AT_abstract_origin referenced DIEs.
     175             :   ///
     176             :   /// \param Attr the attribute to extract.
     177             :   /// \returns a valid DWARFDie instance if the attribute exists, or an invalid
     178             :   /// DWARFDie object if it doesn't.
     179             :   DWARFDie getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const;
     180             : 
     181             :   /// Extract the range base attribute from this DIE as absolute section offset.
     182             :   ///
     183             :   /// This is a utility function that checks for either the DW_AT_rnglists_base
     184             :   /// or DW_AT_GNU_ranges_base attribute.
     185             :   ///
     186             :   /// \returns anm optional absolute section offset value for the attribute.
     187             :   Optional<uint64_t> getRangesBaseAttribute() const;
     188             : 
     189             :   /// Get the DW_AT_high_pc attribute value as an address.
     190             :   ///
     191             :   /// In DWARF version 4 and later the high PC can be encoded as an offset from
     192             :   /// the DW_AT_low_pc. This function takes care of extracting the value as an
     193             :   /// address or offset and adds it to the low PC if needed and returns the
     194             :   /// value as an optional in case the DIE doesn't have a DW_AT_high_pc
     195             :   /// attribute.
     196             :   ///
     197             :   /// \param LowPC the low PC that might be needed to calculate the high PC.
     198             :   /// \returns an optional address value for the attribute.
     199             :   Optional<uint64_t> getHighPC(uint64_t LowPC) const;
     200             : 
     201             :   /// Retrieves DW_AT_low_pc and DW_AT_high_pc from CU.
     202             :   /// Returns true if both attributes are present.
     203             :   bool getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
     204             :                        uint64_t &SectionIndex) const;
     205             : 
     206             :   /// Get the address ranges for this DIE.
     207             :   ///
     208             :   /// Get the hi/low PC range if both attributes are available or exrtracts the
     209             :   /// non-contiguous address ranges from the DW_AT_ranges attribute.
     210             :   ///
     211             :   /// Extracts the range information from this DIE only. This call doesn't look
     212             :   /// for the range in any DW_AT_specification or DW_AT_abstract_origin DIEs.
     213             :   ///
     214             :   /// \returns a address range vector that might be empty if no address range
     215             :   /// information is available.
     216             :   DWARFAddressRangesVector getAddressRanges() const;
     217             : 
     218             :   /// Get all address ranges for any DW_TAG_subprogram DIEs in this DIE or any
     219             :   /// of its children.
     220             :   ///
     221             :   /// Get the hi/low PC range if both attributes are available or exrtracts the
     222             :   /// non-contiguous address ranges from the DW_AT_ranges attribute for this DIE
     223             :   /// and all children.
     224             :   ///
     225             :   /// \param Ranges the addres range vector to fill in.
     226             :   void collectChildrenAddressRanges(DWARFAddressRangesVector &Ranges) const;
     227             : 
     228             :   bool addressRangeContainsAddress(const uint64_t Address) const;
     229             : 
     230             :   /// If a DIE represents a subprogram (or inlined subroutine), returns its
     231             :   /// mangled name (or short name, if mangled is missing). This name may be
     232             :   /// fetched from specification or abstract origin for this subprogram.
     233             :   /// Returns null if no name is found.
     234             :   const char *getSubroutineName(DINameKind Kind) const;
     235             : 
     236             :   /// Return the DIE name resolving DW_AT_sepcification or DW_AT_abstract_origin
     237             :   /// references if necessary. Returns null if no name is found.
     238             :   const char *getName(DINameKind Kind) const;
     239             : 
     240             :   /// Returns the declaration line (start line) for a DIE, assuming it specifies
     241             :   /// a subprogram. This may be fetched from specification or abstract origin
     242             :   /// for this subprogram by resolving DW_AT_sepcification or
     243             :   /// DW_AT_abstract_origin references if necessary.
     244             :   uint64_t getDeclLine() const;
     245             : 
     246             :   /// Retrieves values of DW_AT_call_file, DW_AT_call_line and DW_AT_call_column
     247             :   /// from DIE (or zeroes if they are missing). This function looks for
     248             :   /// DW_AT_call attributes in this DIE only, it will not resolve the attribute
     249             :   /// values in any DW_AT_specification or DW_AT_abstract_origin DIEs.
     250             :   /// \param CallFile filled in with non-zero if successful, zero if there is no
     251             :   /// DW_AT_call_file attribute in this DIE.
     252             :   /// \param CallLine filled in with non-zero if successful, zero if there is no
     253             :   /// DW_AT_call_line attribute in this DIE.
     254             :   /// \param CallColumn filled in with non-zero if successful, zero if there is
     255             :   /// no DW_AT_call_column attribute in this DIE.
     256             :   /// \param CallDiscriminator filled in with non-zero if successful, zero if
     257             :   /// there is no DW_AT_GNU_discriminator attribute in this DIE.
     258             :   void getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
     259             :                       uint32_t &CallColumn, uint32_t &CallDiscriminator) const;
     260             : 
     261             :   class attribute_iterator;
     262             : 
     263             :   /// Get an iterator range to all attributes in the current DIE only.
     264             :   ///
     265             :   /// \returns an iterator range for the attributes of the current DIE.
     266             :   iterator_range<attribute_iterator> attributes() const;
     267             : 
     268             :   class iterator;
     269             : 
     270             :   iterator begin() const;
     271             :   iterator end() const;
     272             :   iterator_range<iterator> children() const;
     273             : };
     274             : 
     275             : class DWARFDie::attribute_iterator :
     276             :     public iterator_facade_base<attribute_iterator, std::forward_iterator_tag,
     277             :                                 const DWARFAttribute> {
     278             :   /// The DWARF DIE we are extracting attributes from.
     279             :   DWARFDie Die;
     280             :   /// The value vended to clients via the operator*() or operator->().
     281             :   DWARFAttribute AttrValue;
     282             :   /// The attribute index within the abbreviation declaration in Die.
     283             :   uint32_t Index;
     284             : 
     285             :   /// Update the attribute index and attempt to read the attribute value. If the
     286             :   /// attribute is able to be read, update AttrValue and the Index member
     287             :   /// variable. If the attribute value is not able to be read, an appropriate
     288             :   /// error will be set if the Err member variable is non-NULL and the iterator
     289             :   /// will be set to the end value so iteration stops.
     290             :   void updateForIndex(const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I);
     291             : 
     292             : public:
     293             :   attribute_iterator() = delete;
     294             :   explicit attribute_iterator(DWARFDie D, bool End);
     295             : 
     296             :   attribute_iterator &operator++();
     297             :   explicit operator bool() const { return AttrValue.isValid(); }
     298             :   const DWARFAttribute &operator*() const { return AttrValue; }
     299             :   bool operator==(const attribute_iterator &X) const { return Index == X.Index; }
     300             : };
     301             : 
     302             : inline bool operator==(const DWARFDie &LHS, const DWARFDie &RHS) {
     303     2022104 :   return LHS.getDebugInfoEntry() == RHS.getDebugInfoEntry() &&
     304      673648 :       LHS.getDwarfUnit() == RHS.getDwarfUnit();
     305             : }
     306             : 
     307             : inline bool operator!=(const DWARFDie &LHS, const DWARFDie &RHS) {
     308             :   return !(LHS == RHS);
     309             : }
     310             : 
     311             : class DWARFDie::iterator : public iterator_facade_base<iterator,
     312             :                                                       std::forward_iterator_tag,
     313             :                                                       const DWARFDie> {
     314             :   DWARFDie Die;
     315             :   void skipNull() {
     316     3080254 :     if (Die && Die.isNULL())
     317      191094 :       Die = DWARFDie();
     318             :   }
     319             : public:
     320      673921 :   iterator() = default;
     321             : 
     322      673918 :   explicit iterator(DWARFDie D) : Die(D) {
     323             :     // If we start out with only a Null DIE then invalidate.
     324      673918 :     skipNull();
     325             :   }
     326             : 
     327      674532 :   iterator &operator++() {
     328      674532 :     Die = Die.getSibling();
     329             :     // Don't include the NULL die when iterating.
     330      674532 :     skipNull();
     331      674532 :     return *this;
     332             :   }
     333             : 
     334             :   explicit operator bool() const { return Die.isValid(); }
     335             :   const DWARFDie &operator*() const { return Die; }
     336     2021465 :   bool operator==(const iterator &X) const { return Die == X.Die; }
     337             : };
     338             : 
     339             : // These inline functions must follow the DWARFDie::iterator definition above
     340             : // as they use functions from that class.
     341      673918 : inline DWARFDie::iterator DWARFDie::begin() const {
     342     1347836 :   return iterator(getFirstChild());
     343             : }
     344             : 
     345             : inline DWARFDie::iterator DWARFDie::end() const {
     346     1347842 :   return iterator();
     347             : }
     348             : 
     349             : inline iterator_range<DWARFDie::iterator> DWARFDie::children() const {
     350     2021742 :   return make_range(begin(), end());
     351             : }
     352             : 
     353             : } // end namespace llvm
     354             : 
     355             : #endif // LLVM_DEBUGINFO_DWARFDIE_H

Generated by: LCOV version 1.13