LLVM  5.0.0
DWARFDie.h
Go to the documentation of this file.
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"
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  DWARFDie() = default;
49  DWARFDie(DWARFUnit *Unit, const DWARFDebugInfoEntry * D) : U(Unit), Die(D) {}
50 
51  bool isValid() const { return U && Die; }
52  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.
60  assert(isValid() && "must check validity prior to calling");
61  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  return Die->getOffset();
70  }
71 
72  dwarf::Tag getTag() const {
73  auto AbbrevDecl = getAbbreviationDeclarationPtr();
74  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  return Die->hasChildren();
82  }
83 
84  /// Returns true for a valid DIE that terminates a sibling chain.
85  bool isNULL() const {
86  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.
112  if (isValid() && Die->hasChildren())
113  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  /// Extract the specified attribute from this DIE.
127  ///
128  /// Extract an attribute value from this DIE only. This call doesn't look
129  /// for the attribute value in any DW_AT_specification or
130  /// DW_AT_abstract_origin referenced DIEs.
131  ///
132  /// \param Attr the attribute to extract.
133  /// \returns an optional DWARFFormValue that will have the form value if the
134  /// attribute was successfully extracted.
136 
137  /// Extract the first value of any attribute in Attrs from this DIE.
138  ///
139  /// Extract the first attribute that matches from this DIE only. This call
140  /// doesn't look for the attribute value in any DW_AT_specification or
141  /// DW_AT_abstract_origin referenced DIEs. The attributes will be searched
142  /// linearly in the order they are specified within Attrs.
143  ///
144  /// \param Attrs an array of DWARF attribute to look for.
145  /// \returns an optional that has a valid DWARFFormValue for the first
146  /// matching attribute in Attrs, or None if none of the attributes in Attrs
147  /// exist in this DIE.
149 
150  /// Extract the first value of any attribute in Attrs from this DIE and
151  /// recurse into any DW_AT_specification or DW_AT_abstract_origin referenced
152  /// DIEs.
153  ///
154  /// \param Attrs an array of DWARF attribute to look for.
155  /// \returns an optional that has a valid DWARFFormValue for the first
156  /// matching attribute in Attrs, or None if none of the attributes in Attrs
157  /// exist in this DIE or in any DW_AT_specification or DW_AT_abstract_origin
158  /// DIEs.
161 
162  /// Extract the specified attribute from this DIE as the referenced DIE.
163  ///
164  /// Regardless of the reference type, return the correct DWARFDie instance if
165  /// the attribute exists. The returned DWARFDie object might be from another
166  /// DWARFUnit, but that is all encapsulated in the new DWARFDie object.
167  ///
168  /// Extract an attribute value from this DIE only. This call doesn't look
169  /// for the attribute value in any DW_AT_specification or
170  /// DW_AT_abstract_origin referenced DIEs.
171  ///
172  /// \param Attr the attribute to extract.
173  /// \returns a valid DWARFDie instance if the attribute exists, or an invalid
174  /// DWARFDie object if it doesn't.
176 
177  /// Extract the range base attribute from this DIE as absolute section offset.
178  ///
179  /// This is a utility function that checks for either the DW_AT_rnglists_base
180  /// or DW_AT_GNU_ranges_base attribute.
181  ///
182  /// \returns anm optional absolute section offset value for the attribute.
184 
185  /// Get the DW_AT_high_pc attribute value as an address.
186  ///
187  /// In DWARF version 4 and later the high PC can be encoded as an offset from
188  /// the DW_AT_low_pc. This function takes care of extracting the value as an
189  /// address or offset and adds it to the low PC if needed and returns the
190  /// value as an optional in case the DIE doesn't have a DW_AT_high_pc
191  /// attribute.
192  ///
193  /// \param LowPC the low PC that might be needed to calculate the high PC.
194  /// \returns an optional address value for the attribute.
195  Optional<uint64_t> getHighPC(uint64_t LowPC) const;
196 
197  /// Retrieves DW_AT_low_pc and DW_AT_high_pc from CU.
198  /// Returns true if both attributes are present.
199  bool getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
200  uint64_t &SectionIndex) const;
201 
202  /// Get the address ranges for this DIE.
203  ///
204  /// Get the hi/low PC range if both attributes are available or exrtracts the
205  /// non-contiguous address ranges from the DW_AT_ranges attribute.
206  ///
207  /// Extracts the range information from this DIE only. This call doesn't look
208  /// for the range in any DW_AT_specification or DW_AT_abstract_origin DIEs.
209  ///
210  /// \returns a address range vector that might be empty if no address range
211  /// information is available.
213 
214  /// Get all address ranges for any DW_TAG_subprogram DIEs in this DIE or any
215  /// of its children.
216  ///
217  /// Get the hi/low PC range if both attributes are available or exrtracts the
218  /// non-contiguous address ranges from the DW_AT_ranges attribute for this DIE
219  /// and all children.
220  ///
221  /// \param Ranges the addres range vector to fill in.
223 
224  bool addressRangeContainsAddress(const uint64_t Address) const;
225 
226  /// If a DIE represents a subprogram (or inlined subroutine), returns its
227  /// mangled name (or short name, if mangled is missing). This name may be
228  /// fetched from specification or abstract origin for this subprogram.
229  /// Returns null if no name is found.
230  const char *getSubroutineName(DINameKind Kind) const;
231 
232  /// Return the DIE name resolving DW_AT_sepcification or DW_AT_abstract_origin
233  /// references if necessary. Returns null if no name is found.
234  const char *getName(DINameKind Kind) const;
235 
236  /// Returns the declaration line (start line) for a DIE, assuming it specifies
237  /// a subprogram. This may be fetched from specification or abstract origin
238  /// for this subprogram by resolving DW_AT_sepcification or
239  /// DW_AT_abstract_origin references if necessary.
240  uint64_t getDeclLine() const;
241 
242  /// Retrieves values of DW_AT_call_file, DW_AT_call_line and DW_AT_call_column
243  /// from DIE (or zeroes if they are missing). This function looks for
244  /// DW_AT_call attributes in this DIE only, it will not resolve the attribute
245  /// values in any DW_AT_specification or DW_AT_abstract_origin DIEs.
246  /// \param CallFile filled in with non-zero if successful, zero if there is no
247  /// DW_AT_call_file attribute in this DIE.
248  /// \param CallLine filled in with non-zero if successful, zero if there is no
249  /// DW_AT_call_line attribute in this DIE.
250  /// \param CallColumn filled in with non-zero if successful, zero if there is
251  /// no DW_AT_call_column attribute in this DIE.
252  /// \param CallDiscriminator filled in with non-zero if successful, zero if
253  /// there is no DW_AT_GNU_discriminator attribute in this DIE.
254  void getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
255  uint32_t &CallColumn, uint32_t &CallDiscriminator) const;
256 
257  class attribute_iterator;
258 
259  /// Get an iterator range to all attributes in the current DIE only.
260  ///
261  /// \returns an iterator range for the attributes of the current DIE.
263 
264  class iterator;
265 
266  iterator begin() const;
267  iterator end() const;
269 };
270 
272  public iterator_facade_base<attribute_iterator, std::forward_iterator_tag,
273  const DWARFAttribute> {
274  /// The DWARF DIE we are extracting attributes from.
275  DWARFDie Die;
276  /// The value vended to clients via the operator*() or operator->().
277  DWARFAttribute AttrValue;
278  /// The attribute index within the abbreviation declaration in Die.
279  uint32_t Index;
280 
281  /// Update the attribute index and attempt to read the attribute value. If the
282  /// attribute is able to be read, update AttrValue and the Index member
283  /// variable. If the attribute value is not able to be read, an appropriate
284  /// error will be set if the Err member variable is non-NULL and the iterator
285  /// will be set to the end value so iteration stops.
286  void updateForIndex(const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I);
287 
288 public:
289  attribute_iterator() = delete;
290  explicit attribute_iterator(DWARFDie D, bool End);
291 
293  explicit operator bool() const { return AttrValue.isValid(); }
294  const DWARFAttribute &operator*() const { return AttrValue; }
295  bool operator==(const attribute_iterator &X) const { return Index == X.Index; }
296 };
297 
298 inline bool operator==(const DWARFDie &LHS, const DWARFDie &RHS) {
299  return LHS.getDebugInfoEntry() == RHS.getDebugInfoEntry() &&
300  LHS.getDwarfUnit() == RHS.getDwarfUnit();
301 }
302 
303 inline bool operator!=(const DWARFDie &LHS, const DWARFDie &RHS) {
304  return !(LHS == RHS);
305 }
306 
307 class DWARFDie::iterator : public iterator_facade_base<iterator,
308  std::forward_iterator_tag,
309  const DWARFDie> {
310  DWARFDie Die;
311  void skipNull() {
312  if (Die && Die.isNULL())
313  Die = DWARFDie();
314  }
315 public:
316  iterator() = default;
317 
318  explicit iterator(DWARFDie D) : Die(D) {
319  // If we start out with only a Null DIE then invalidate.
320  skipNull();
321  }
322 
324  Die = Die.getSibling();
325  // Don't include the NULL die when iterating.
326  skipNull();
327  return *this;
328  }
329 
330  explicit operator bool() const { return Die.isValid(); }
331  const DWARFDie &operator*() const { return Die; }
332  bool operator==(const iterator &X) const { return Die == X.Die; }
333 };
334 
335 // These inline functions must follow the DWARFDie::iterator definition above
336 // as they use functions from that class.
338  return iterator(getFirstChild());
339 }
340 
342  return iterator();
343 }
344 
346  return make_range(begin(), end());
347 }
348 
349 } // end namespace llvm
350 
351 #endif // LLVM_DEBUGINFO_DWARFDIE_H
DWARFDie()=default
const DWARFDie & operator*() const
Definition: DWARFDie.h:331
DWARFDie getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE as the referenced DIE.
Definition: DWARFDie.cpp:188
DWARFAddressRangesVector getAddressRanges() const
Get the address ranges for this DIE.
Definition: DWARFDie.cpp:233
Optional< uint64_t > getHighPC(uint64_t LowPC) const
Get the DW_AT_high_pc attribute value as an address.
Definition: DWARFDie.cpp:203
Attribute
Attributes.
Definition: Dwarf.h:105
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
attribute_iterator & operator++()
Definition: DWARFDie.cpp:419
void collectChildrenAddressRanges(DWARFAddressRangesVector &Ranges) const
Get all address ranges for any DW_TAG_subprogram DIEs in this DIE or any of its children.
Definition: DWARFDie.cpp:252
bool isValid() const
const DWARFDebugInfoEntry * getDebugInfoEntry() const
Definition: DWARFDie.h:53
DINameKind
A DINameKind is passed to name search methods to specify a preference regarding the type of name reso...
Definition: DIContext.h:99
bool isValid() const
Definition: DWARFDie.h:51
bool addressRangeContainsAddress(const uint64_t Address) const
Definition: DWARFDie.cpp:264
const char * getSubroutineName(DINameKind Kind) const
If a DIE represents a subprogram (or inlined subroutine), returns its mangled name (or short name...
Definition: DWARFDie.cpp:273
uint32_t getOffset() const
Get the absolute offset into the debug info or types section.
Definition: DWARFDie.h:67
void getCallerFrame(uint32_t &CallFile, uint32_t &CallLine, uint32_t &CallColumn, uint32_t &CallDiscriminator) const
Retrieves values of DW_AT_call_file, DW_AT_call_line and DW_AT_call_column from DIE (or zeroes if the...
Definition: DWARFDie.cpp:298
bool getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC, uint64_t &SectionIndex) const
Retrieves DW_AT_low_pc and DW_AT_high_pc from CU.
Definition: DWARFDie.cpp:217
Encapsulates a DWARF attribute value and all of the data required to describe the attribute value...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:68
DWARFDie getParent() const
Get the parent of this DIE object.
Definition: DWARFDie.cpp:363
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:152
DWARFDie getSibling() const
Get the sibling of this DIE object.
Definition: DWARFDie.cpp:369
DWARFUnit * getDwarfUnit() const
Definition: DWARFDie.h:54
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
Utility class that carries the DWARF compile/type unit and the debug info entry in an object...
Definition: DWARFDie.h:43
iterator end() const
Definition: DWARFDie.h:341
iterator_range< iterator > children() const
Definition: DWARFDie.h:345
bool operator==(const attribute_iterator &X) const
Definition: DWARFDie.h:295
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
static const unsigned End
Optional< DWARFFormValue > find(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE.
Definition: DWARFDie.cpp:146
bool operator==(const iterator &X) const
Definition: DWARFDie.h:332
const DWARFAttribute & operator*() const
Definition: DWARFDie.h:294
bool hasChildren() const
Definition: DWARFDie.h:79
DWARFDie getFirstChild() const
Get the first child of this DIE object.
Definition: DWARFDie.h:111
const DWARFAbbreviationDeclaration * getAbbreviationDeclarationPtr() const
Get the abbreviation declaration for this DIE.
Definition: DWARFDie.h:59
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Optional< DWARFFormValue > findRecursively(ArrayRef< dwarf::Attribute > Attrs) const
Extract the first value of any attribute in Attrs from this DIE and recurse into any DW_AT_specificat...
Definition: DWARFDie.cpp:170
uint64_t getDeclLine() const
Returns the declaration line (start line) for a DIE, assuming it specifies a subprogram.
Definition: DWARFDie.cpp:294
const char * getName(DINameKind Kind) const
Return the DIE name resolving DW_AT_sepcification or DW_AT_abstract_origin references if necessary...
Definition: DWARFDie.cpp:280
A range adaptor for a pair of iterators.
iterator begin() const
Definition: DWARFDie.h:337
Optional< uint64_t > getRangesBaseAttribute() const
Extract the range base attribute from this DIE as absolute section offset.
Definition: DWARFDie.cpp:199
bool isSubroutineDIE() const
Returns true if DIE represents a subprogram or an inlined subroutine.
Definition: DWARFDie.cpp:140
bool operator!=(uint64_t V1, const APInt &V2)
Definition: APInt.h:1948
bool isSubprogramDIE() const
Returns true if DIE represents a subprogram (not inlined).
Definition: DWARFDie.cpp:136
#define I(x, y, z)
Definition: MD5.cpp:58
const DWARFAbbreviationDeclaration * getAbbreviationDeclarationPtr() const
iterator_range< attribute_iterator > attributes() const
Get an iterator range to all attributes in the current DIE only.
Definition: DWARFDie.cpp:376
iterator & operator++()
Definition: DWARFDie.h:323
void dump(raw_ostream &OS, unsigned recurseDepth, unsigned indent=0, DIDumpOptions DumpOpts=DIDumpOptions()) const
Dump the DIE and all of its attributes to the supplied stream.
Definition: DWARFDie.cpp:307
std::vector< uint8_t > Unit
Definition: FuzzerDefs.h:105
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
dwarf::Tag getTag() const
Definition: DWARFDie.h:72
std::vector< DWARFAddressRange > DWARFAddressRangesVector
DWARFAddressRangesVector - represents a set of absolute address ranges.
DWARFDebugInfoEntry - A DIE with only the minimum required data.
iterator(DWARFDie D)
Definition: DWARFDie.h:318
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1946
DWARFDie(DWARFUnit *Unit, const DWARFDebugInfoEntry *D)
Definition: DWARFDie.h:49
bool isNULL() const
Returns true for a valid DIE that terminates a sibling chain.
Definition: DWARFDie.h:85
#define D
Definition: LargeTest.cpp:26