1 //===-- DWARFDie.h --------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_LIB_DEBUGINFO_DWARFDIE_H
11 #define LLVM_LIB_DEBUGINFO_DWARFDIE_H
13 #include "llvm/ADT/iterator.h"
14 #include "llvm/ADT/iterator_range.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
21 class DWARFDebugInfoEntry;
24 //===----------------------------------------------------------------------===//
25 /// Utility class that carries the DWARF compile/type unit and the debug info
26 /// entry in an object.
28 /// When accessing information from a debug info entry we always need to DWARF
29 /// compile/type unit in order to extract the info correctly as some information
30 /// is relative to the compile/type unit. Prior to this class the DWARFUnit and
31 /// the DWARFDebugInfoEntry was passed around separately and there was the
32 /// possibility for error if the wrong DWARFUnit was used to extract a unit
33 /// relative offset. This class helps to ensure that this doesn't happen and
34 /// also simplifies the attribute extraction calls by not having to specify the
35 /// DWARFUnit for each call.
38 const DWARFDebugInfoEntry *Die;
40 DWARFDie() : U(nullptr), Die(nullptr) {}
41 DWARFDie(DWARFUnit *Unit, const DWARFDebugInfoEntry * D) : U(Unit), Die(D) {}
43 bool isValid() const { return U && Die; }
44 explicit operator bool() const { return isValid(); }
45 const DWARFDebugInfoEntry *getDebugInfoEntry() const { return Die; }
46 DWARFUnit *getDwarfUnit() const { return U; }
49 /// Get the abbreviation declaration for this DIE.
51 /// \returns the abbreviation declaration or NULL for null tags.
52 const DWARFAbbreviationDeclaration *getAbbreviationDeclarationPtr() const {
53 assert(isValid() && "must check validity prior to calling");
54 return Die->getAbbreviationDeclarationPtr();
57 /// Get the absolute offset into the debug info or types section.
59 /// \returns the DIE offset or -1U if invalid.
60 uint32_t getOffset() const {
61 assert(isValid() && "must check validity prior to calling");
62 return Die->getOffset();
65 dwarf::Tag getTag() const {
66 auto AbbrevDecl = getAbbreviationDeclarationPtr();
68 return AbbrevDecl->getTag();
69 return dwarf::DW_TAG_null;
72 bool hasChildren() const {
73 assert(isValid() && "must check validity prior to calling");
74 return Die->hasChildren();
77 /// Returns true for a valid DIE that terminates a sibling chain.
79 return getAbbreviationDeclarationPtr() == nullptr;
81 /// Returns true if DIE represents a subprogram (not inlined).
82 bool isSubprogramDIE() const;
84 /// Returns true if DIE represents a subprogram or an inlined subroutine.
85 bool isSubroutineDIE() const;
87 /// Get the parent of this DIE object.
89 /// \returns a valid DWARFDie instance if this object has a parent or an
90 /// invalid DWARFDie instance if it doesn't.
91 DWARFDie getParent() const;
93 /// Get the sibling of this DIE object.
95 /// \returns a valid DWARFDie instance if this object has a sibling or an
96 /// invalid DWARFDie instance if it doesn't.
97 DWARFDie getSibling() const;
99 /// Get the first child of this DIE object.
101 /// \returns a valid DWARFDie instance if this object has children or an
102 /// invalid DWARFDie instance if it doesn't.
103 DWARFDie getFirstChild() const {
104 if (isValid() && Die->hasChildren())
105 return DWARFDie(U, Die + 1);
109 /// Dump the DIE and all of its attributes to the supplied stream.
111 /// \param OS the stream to use for output.
112 /// \param recurseDepth the depth to recurse to when dumping this DIE and its
114 /// \param indent the number of characters to indent each line that is output.
115 void dump(raw_ostream &OS, unsigned recurseDepth, unsigned indent = 0) const;
117 /// Extract the specified attribute from this DIE.
119 /// Extract an attribute value from this DIE only. This call doesn't look
120 /// for the attribute value in any DW_AT_specification or
121 /// DW_AT_abstract_origin referenced DIEs.
123 /// \param Attr the attribute to extract.
124 /// \returns an optional DWARFFormValue that will have the form value if the
125 /// attribute was successfully extracted.
126 Optional<DWARFFormValue> getAttributeValue(dwarf::Attribute Attr) const;
128 /// Extract the specified attribute from this DIE as a C string.
130 /// Extract an attribute value from this DIE only. This call doesn't look
131 /// for the attribute value in any DW_AT_specification or
132 /// DW_AT_abstract_origin referenced DIEs.
134 /// \param Attr the attribute to extract.
135 /// \param FailValue the value to return if this DIE doesn't have this
137 /// \returns the NULL terminated C string value owned by the DWARF section
138 /// that contains the string or FailValue if the attribute doesn't exist or
139 /// if the attribute's form isn't a form that describes an string.
140 const char *getAttributeValueAsString(dwarf::Attribute Attr,
141 const char *FailValue) const;
143 /// Extract the specified attribute from this DIE as an address.
145 /// Extract an attribute value from this DIE only. This call doesn't look
146 /// for the attribute value in any DW_AT_specification or
147 /// DW_AT_abstract_origin referenced DIEs.
149 /// \param Attr the attribute to extract.
150 /// \returns an optional value for the attribute.
151 Optional<uint64_t> getAttributeValueAsAddress(dwarf::Attribute Attr) const;
153 /// Extract the specified attribute from this DIE as a signed integer.
155 /// Extract an attribute value from this DIE only. This call doesn't look
156 /// for the attribute value in any DW_AT_specification or
157 /// DW_AT_abstract_origin referenced DIEs.
159 /// \param Attr the attribute to extract.
160 /// \returns an optional value for the attribute.
162 getAttributeValueAsSignedConstant(dwarf::Attribute Attr) const;
164 /// Extract the specified attribute from this DIE as an unsigned integer.
166 /// Extract an attribute value from this DIE only. This call doesn't look
167 /// for the attribute value in any DW_AT_specification or
168 /// DW_AT_abstract_origin referenced DIEs.
170 /// \param Attr the attribute to extract.
171 /// \returns an optional value for the attribute.
173 getAttributeValueAsUnsignedConstant(dwarf::Attribute Attr) const;
175 /// Extract the specified attribute from this DIE as absolute DIE Offset.
177 /// Extract an attribute value from this DIE only. This call doesn't look
178 /// for the attribute value in any DW_AT_specification or
179 /// DW_AT_abstract_origin referenced DIEs.
181 /// \param Attr the attribute to extract.
182 /// \returns an optional value for the attribute.
183 Optional<uint64_t> getAttributeValueAsReference(dwarf::Attribute Attr) const;
185 /// Extract the specified attribute from this DIE as absolute section offset.
187 /// Extract an attribute value from this DIE only. This call doesn't look
188 /// for the attribute value in any DW_AT_specification or
189 /// DW_AT_abstract_origin referenced DIEs.
191 /// \param Attr the attribute to extract.
192 /// \returns an optional value for the attribute.
194 getAttributeValueAsSectionOffset(dwarf::Attribute Attr) const;
196 /// Extract the specified attribute from this DIE as the referenced DIE.
198 /// Regardless of the reference type, return the correct DWARFDie instance if
199 /// the attribute exists. The returned DWARFDie object might be from another
200 /// DWARFUnit, but that is all encapsulated in the new DWARFDie object.
202 /// Extract an attribute value from this DIE only. This call doesn't look
203 /// for the attribute value in any DW_AT_specification or
204 /// DW_AT_abstract_origin referenced DIEs.
206 /// \param Attr the attribute to extract.
207 /// \returns a valid DWARFDie instance if the attribute exists, or an invalid
208 /// DWARFDie object if it doesn't.
209 DWARFDie getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const;
211 /// Extract the range base attribute from this DIE as absolute section offset.
213 /// This is a utility function that checks for either the DW_AT_rnglists_base
214 /// or DW_AT_GNU_ranges_base attribute.
216 /// \returns anm optional absolute section offset value for the attribute.
217 Optional<uint64_t> getRangesBaseAttribute() const;
219 /// Get the DW_AT_high_pc attribute value as an address.
221 /// In DWARF version 4 and later the high PC can be encoded as an offset from
222 /// the DW_AT_low_pc. This function takes care of extracting the value as an
223 /// address or offset and adds it to the low PC if needed and returns the
224 /// value as an optional in case the DIE doesn't have a DW_AT_high_pc
227 /// \param LowPC the low PC that might be needed to calculate the high PC.
228 /// \returns an optional address value for the attribute.
229 Optional<uint64_t> getHighPC(uint64_t LowPC) const;
231 /// Retrieves DW_AT_low_pc and DW_AT_high_pc from CU.
232 /// Returns true if both attributes are present.
233 bool getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC) const;
235 /// Get the address ranges for this DIE.
237 /// Get the hi/low PC range if both attributes are available or exrtracts the
238 /// non-contiguous address ranges from the DW_AT_ranges attribute.
240 /// Extracts the range information from this DIE only. This call doesn't look
241 /// for the range in any DW_AT_specification or DW_AT_abstract_origin DIEs.
243 /// \returns a address range vector that might be empty if no address range
244 /// information is available.
245 DWARFAddressRangesVector getAddressRanges() const;
247 /// Get all address ranges for any DW_TAG_subprogram DIEs in this DIE or any
250 /// Get the hi/low PC range if both attributes are available or exrtracts the
251 /// non-contiguous address ranges from the DW_AT_ranges attribute for this DIE
252 /// and all children.
254 /// \param Ranges the addres range vector to fill in.
255 void collectChildrenAddressRanges(DWARFAddressRangesVector &Ranges) const;
257 bool addressRangeContainsAddress(const uint64_t Address) const;
259 /// If a DIE represents a subprogram (or inlined subroutine), returns its
260 /// mangled name (or short name, if mangled is missing). This name may be
261 /// fetched from specification or abstract origin for this subprogram.
262 /// Returns null if no name is found.
263 const char *getSubroutineName(DINameKind Kind) const;
265 /// Return the DIE name resolving DW_AT_sepcification or DW_AT_abstract_origin
266 /// references if necessary. Returns null if no name is found.
267 const char *getName(DINameKind Kind) const;
269 /// Retrieves values of DW_AT_call_file, DW_AT_call_line and DW_AT_call_column
270 /// from DIE (or zeroes if they are missing). This function looks for
271 /// DW_AT_call attributes in this DIE only, it will not resolve the attribute
272 /// values in any DW_AT_specification or DW_AT_abstract_origin DIEs.
273 /// \param CallFile filled in with non-zero if successful, zero if there is no
274 /// DW_AT_call_file attribute in this DIE.
275 /// \param CallLine filled in with non-zero if successful, zero if there is no
276 /// DW_AT_call_line attribute in this DIE.
277 /// \param CallColumn filled in with non-zero if successful, zero if there is
278 /// no DW_AT_call_column attribute in this DIE.
279 void getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
280 uint32_t &CallColumn) const;
282 /// Get inlined chain for a given address, rooted at the current DIE.
283 /// Returns empty chain if address is not contained in address range
286 getInlinedChainForAddress(const uint64_t Address,
287 SmallVectorImpl<DWARFDie> &InlinedChain) const;
291 iterator begin() const;
292 iterator end() const;
293 iterator_range<iterator> children() const;
297 inline bool operator==(const DWARFDie &LHS, const DWARFDie &RHS) {
298 return LHS.getDebugInfoEntry() == RHS.getDebugInfoEntry() &&
299 LHS.getDwarfUnit() == RHS.getDwarfUnit();
302 inline bool operator!=(const DWARFDie &LHS, const DWARFDie &RHS) {
303 return !(LHS == RHS);
306 class DWARFDie::iterator : public iterator_facade_base<iterator,
307 std::forward_iterator_tag,
311 if (Die && Die.isNULL())
315 iterator() = default;
316 explicit iterator(DWARFDie D) : Die(D) {
317 // If we start out with only a Null DIE then invalidate.
320 iterator &operator++() {
321 Die = Die.getSibling();
322 // Don't include the NULL die when iterating.
326 explicit operator bool() const { return Die.isValid(); }
327 const DWARFDie &operator*() const { return Die; }
328 bool operator==(const iterator &X) const { return Die == X.Die; }
331 // These inline functions must follow the DWARFDie::iterator definition above
332 // as they use functions from that class.
333 inline DWARFDie::iterator DWARFDie::begin() const {
334 return iterator(getFirstChild());
337 inline DWARFDie::iterator DWARFDie::end() const {
341 inline iterator_range<DWARFDie::iterator> DWARFDie::children() const {
342 return make_range(begin(), end());
345 } // end namespace llvm
347 #endif // LLVM_LIB_DEBUGINFO_DWARFDIE_H