LLVM  8.0.1
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 { return getAbbreviationDeclarationPtr() == nullptr; }
86 
87  /// Returns true if DIE represents a subprogram (not inlined).
88  bool isSubprogramDIE() const;
89 
90  /// Returns true if DIE represents a subprogram or an inlined subroutine.
91  bool isSubroutineDIE() const;
92 
93  /// Get the parent of this DIE object.
94  ///
95  /// \returns a valid DWARFDie instance if this object has a parent or an
96  /// invalid DWARFDie instance if it doesn't.
97  DWARFDie getParent() const;
98 
99  /// Get the sibling of this DIE object.
100  ///
101  /// \returns a valid DWARFDie instance if this object has a sibling or an
102  /// invalid DWARFDie instance if it doesn't.
103  DWARFDie getSibling() const;
104 
105  /// Get the previous sibling of this DIE object.
106  ///
107  /// \returns a valid DWARFDie instance if this object has a sibling or an
108  /// invalid DWARFDie instance if it doesn't.
110 
111  /// Get the first child of this DIE object.
112  ///
113  /// \returns a valid DWARFDie instance if this object has children or an
114  /// invalid DWARFDie instance if it doesn't.
115  DWARFDie getFirstChild() const;
116 
117  /// Get the last child of this DIE object.
118  ///
119  /// \returns a valid null DWARFDie instance if this object has children or an
120  /// invalid DWARFDie instance if it doesn't.
121  DWARFDie getLastChild() const;
122 
123  /// Dump the DIE and all of its attributes to the supplied stream.
124  ///
125  /// \param OS the stream to use for output.
126  /// \param indent the number of characters to indent each line that is output.
127  void dump(raw_ostream &OS, unsigned indent = 0,
128  DIDumpOptions DumpOpts = DIDumpOptions()) const;
129 
130  /// Convenience zero-argument overload for debugging.
131  LLVM_DUMP_METHOD void dump() const;
132 
133  /// Extract the specified attribute from this DIE.
134  ///
135  /// Extract an attribute value from this DIE only. This call doesn't look
136  /// for the attribute value in any DW_AT_specification or
137  /// DW_AT_abstract_origin referenced DIEs.
138  ///
139  /// \param Attr the attribute to extract.
140  /// \returns an optional DWARFFormValue that will have the form value if the
141  /// attribute was successfully extracted.
143 
144  /// Extract the first value of any attribute in Attrs from this DIE.
145  ///
146  /// Extract the first attribute that matches from this DIE only. This call
147  /// doesn't look for the attribute value in any DW_AT_specification or
148  /// DW_AT_abstract_origin referenced DIEs. The attributes will be searched
149  /// linearly in the order they are specified within Attrs.
150  ///
151  /// \param Attrs an array of DWARF attribute to look for.
152  /// \returns an optional that has a valid DWARFFormValue for the first
153  /// matching attribute in Attrs, or None if none of the attributes in Attrs
154  /// exist in this DIE.
156 
157  /// Extract the first value of any attribute in Attrs from this DIE and
158  /// recurse into any DW_AT_specification or DW_AT_abstract_origin referenced
159  /// DIEs.
160  ///
161  /// \param Attrs an array of DWARF attribute to look for.
162  /// \returns an optional that has a valid DWARFFormValue for the first
163  /// matching attribute in Attrs, or None if none of the attributes in Attrs
164  /// exist in this DIE or in any DW_AT_specification or DW_AT_abstract_origin
165  /// DIEs.
168 
169  /// Extract the specified attribute from this DIE as the referenced DIE.
170  ///
171  /// Regardless of the reference type, return the correct DWARFDie instance if
172  /// the attribute exists. The returned DWARFDie object might be from another
173  /// DWARFUnit, but that is all encapsulated in the new DWARFDie object.
174  ///
175  /// Extract an attribute value from this DIE only. This call doesn't look
176  /// for the attribute value in any DW_AT_specification or
177  /// DW_AT_abstract_origin referenced DIEs.
178  ///
179  /// \param Attr the attribute to extract.
180  /// \returns a valid DWARFDie instance if the attribute exists, or an invalid
181  /// DWARFDie object if it doesn't.
184 
185  /// Extract the range base attribute from this DIE as absolute section offset.
186  ///
187  /// This is a utility function that checks for either the DW_AT_rnglists_base
188  /// or DW_AT_GNU_ranges_base attribute.
189  ///
190  /// \returns anm optional absolute section offset value for the attribute.
192 
193  /// Get the DW_AT_high_pc attribute value as an address.
194  ///
195  /// In DWARF version 4 and later the high PC can be encoded as an offset from
196  /// the DW_AT_low_pc. This function takes care of extracting the value as an
197  /// address or offset and adds it to the low PC if needed and returns the
198  /// value as an optional in case the DIE doesn't have a DW_AT_high_pc
199  /// attribute.
200  ///
201  /// \param LowPC the low PC that might be needed to calculate the high PC.
202  /// \returns an optional address value for the attribute.
203  Optional<uint64_t> getHighPC(uint64_t LowPC) const;
204 
205  /// Retrieves DW_AT_low_pc and DW_AT_high_pc from CU.
206  /// Returns true if both attributes are present.
207  bool getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
208  uint64_t &SectionIndex) const;
209 
210  /// Get the address ranges for this DIE.
211  ///
212  /// Get the hi/low PC range if both attributes are available or exrtracts the
213  /// non-contiguous address ranges from the DW_AT_ranges attribute.
214  ///
215  /// Extracts the range information from this DIE only. This call doesn't look
216  /// for the range in any DW_AT_specification or DW_AT_abstract_origin DIEs.
217  ///
218  /// \returns a address range vector that might be empty if no address range
219  /// information is available.
221 
222  /// Get all address ranges for any DW_TAG_subprogram DIEs in this DIE or any
223  /// of its children.
224  ///
225  /// Get the hi/low PC range if both attributes are available or exrtracts the
226  /// non-contiguous address ranges from the DW_AT_ranges attribute for this DIE
227  /// and all children.
228  ///
229  /// \param Ranges the addres range vector to fill in.
231 
232  bool addressRangeContainsAddress(const uint64_t Address) const;
233 
234  /// If a DIE represents a subprogram (or inlined subroutine), returns its
235  /// mangled name (or short name, if mangled is missing). This name may be
236  /// fetched from specification or abstract origin for this subprogram.
237  /// Returns null if no name is found.
238  const char *getSubroutineName(DINameKind Kind) const;
239 
240  /// Return the DIE name resolving DW_AT_sepcification or DW_AT_abstract_origin
241  /// references if necessary. Returns null if no name is found.
242  const char *getName(DINameKind Kind) const;
243 
244  /// Returns the declaration line (start line) for a DIE, assuming it specifies
245  /// a subprogram. This may be fetched from specification or abstract origin
246  /// for this subprogram by resolving DW_AT_sepcification or
247  /// DW_AT_abstract_origin references if necessary.
248  uint64_t getDeclLine() const;
249 
250  /// Retrieves values of DW_AT_call_file, DW_AT_call_line and DW_AT_call_column
251  /// from DIE (or zeroes if they are missing). This function looks for
252  /// DW_AT_call attributes in this DIE only, it will not resolve the attribute
253  /// values in any DW_AT_specification or DW_AT_abstract_origin DIEs.
254  /// \param CallFile filled in with non-zero if successful, zero if there is no
255  /// DW_AT_call_file attribute in this DIE.
256  /// \param CallLine filled in with non-zero if successful, zero if there is no
257  /// DW_AT_call_line attribute in this DIE.
258  /// \param CallColumn filled in with non-zero if successful, zero if there is
259  /// no DW_AT_call_column attribute in this DIE.
260  /// \param CallDiscriminator filled in with non-zero if successful, zero if
261  /// there is no DW_AT_GNU_discriminator attribute in this DIE.
262  void getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
263  uint32_t &CallColumn, uint32_t &CallDiscriminator) const;
264 
265  class attribute_iterator;
266 
267  /// Get an iterator range to all attributes in the current DIE only.
268  ///
269  /// \returns an iterator range for the attributes of the current DIE.
271 
272  class iterator;
273 
274  iterator begin() const;
275  iterator end() const;
276 
277  std::reverse_iterator<iterator> rbegin() const;
278  std::reverse_iterator<iterator> rend() const;
279 
281 };
282 
284  : public iterator_facade_base<attribute_iterator, std::forward_iterator_tag,
285  const DWARFAttribute> {
286  /// The DWARF DIE we are extracting attributes from.
287  DWARFDie Die;
288  /// The value vended to clients via the operator*() or operator->().
289  DWARFAttribute AttrValue;
290  /// The attribute index within the abbreviation declaration in Die.
291  uint32_t Index;
292 
293  friend bool operator==(const attribute_iterator &LHS,
294  const attribute_iterator &RHS);
295 
296  /// Update the attribute index and attempt to read the attribute value. If the
297  /// attribute is able to be read, update AttrValue and the Index member
298  /// variable. If the attribute value is not able to be read, an appropriate
299  /// error will be set if the Err member variable is non-NULL and the iterator
300  /// will be set to the end value so iteration stops.
301  void updateForIndex(const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I);
302 
303 public:
304  attribute_iterator() = delete;
305  explicit attribute_iterator(DWARFDie D, bool End);
306 
307  attribute_iterator &operator++();
308  attribute_iterator &operator--();
309  explicit operator bool() const { return AttrValue.isValid(); }
310  const DWARFAttribute &operator*() const { return AttrValue; }
311 };
312 
314  const DWARFDie::attribute_iterator &RHS) {
315  return LHS.Index == RHS.Index;
316 }
317 
319  const DWARFDie::attribute_iterator &RHS) {
320  return !(LHS == RHS);
321 }
322 
323 inline bool operator==(const DWARFDie &LHS, const DWARFDie &RHS) {
324  return LHS.getDebugInfoEntry() == RHS.getDebugInfoEntry() &&
325  LHS.getDwarfUnit() == RHS.getDwarfUnit();
326 }
327 
328 inline bool operator!=(const DWARFDie &LHS, const DWARFDie &RHS) {
329  return !(LHS == RHS);
330 }
331 
332 inline bool operator<(const DWARFDie &LHS, const DWARFDie &RHS) {
333  return LHS.getOffset() < RHS.getOffset();
334 }
335 
337  : public iterator_facade_base<iterator, std::bidirectional_iterator_tag,
338  const DWARFDie> {
339  DWARFDie Die;
340 
342  friend bool operator==(const DWARFDie::iterator &LHS,
343  const DWARFDie::iterator &RHS);
344 
345 public:
346  iterator() = default;
347 
348  explicit iterator(DWARFDie D) : Die(D) {}
349 
351  Die = Die.getSibling();
352  return *this;
353  }
354 
356  Die = Die.getPreviousSibling();
357  return *this;
358  }
359 
360  const DWARFDie &operator*() const { return Die; }
361 };
362 
363 inline bool operator==(const DWARFDie::iterator &LHS,
364  const DWARFDie::iterator &RHS) {
365  return LHS.Die == RHS.Die;
366 }
367 
368 inline bool operator!=(const DWARFDie::iterator &LHS,
369  const DWARFDie::iterator &RHS) {
370  return !(LHS == RHS);
371 }
372 
373 // These inline functions must follow the DWARFDie::iterator definition above
374 // as they use functions from that class.
376  return iterator(getFirstChild());
377 }
378 
380  return iterator(getLastChild());
381 }
382 
384  return make_range(begin(), end());
385 }
386 
387 } // end namespace llvm
388 
389 namespace std {
390 
391 template <>
392 class reverse_iterator<llvm::DWARFDie::iterator>
394  reverse_iterator<llvm::DWARFDie::iterator>,
395  bidirectional_iterator_tag, const llvm::DWARFDie> {
396 
397 private:
398  llvm::DWARFDie Die;
399  bool AtEnd;
400 
401 public:
403  : Die(It.Die), AtEnd(!It.Die.getPreviousSibling()) {
404  if (!AtEnd)
405  Die = Die.getPreviousSibling();
406  }
407 
409  return llvm::DWARFDie::iterator(AtEnd ? Die : Die.getSibling());
410  }
411 
413  assert(!AtEnd && "Incrementing rend");
415  if (D)
416  Die = D;
417  else
418  AtEnd = true;
419  return *this;
420  }
421 
423  if (AtEnd) {
424  AtEnd = false;
425  return *this;
426  }
427  Die = Die.getSibling();
428  assert(!Die.isNULL() && "Decrementing rbegin");
429  return *this;
430  }
431 
432  const llvm::DWARFDie &operator*() const {
433  assert(Die.isValid());
434  return Die;
435  }
436 
437  // FIXME: We should be able to specify the equals operator as a friend, but
438  // that causes the compiler to think the operator overload is ambiguous
439  // with the friend declaration and the actual definition as candidates.
441  return Die == RHS.Die && AtEnd == RHS.AtEnd;
442  }
443 };
444 
445 } // namespace std
446 
447 namespace llvm {
448 
449 inline bool operator==(const std::reverse_iterator<DWARFDie::iterator> &LHS,
450  const std::reverse_iterator<DWARFDie::iterator> &RHS) {
451  return LHS.equals(RHS);
452 }
453 
454 inline bool operator!=(const std::reverse_iterator<DWARFDie::iterator> &LHS,
455  const std::reverse_iterator<DWARFDie::iterator> &RHS) {
456  return !(LHS == RHS);
457 }
458 
459 inline std::reverse_iterator<DWARFDie::iterator> DWARFDie::rbegin() const {
461 }
462 
463 inline std::reverse_iterator<DWARFDie::iterator> DWARFDie::rend() const {
465 }
466 
467 } // end namespace llvm
468 
469 #endif // LLVM_DEBUGINFO_DWARFDIE_H
DWARFDie()=default
DWARFUnit * getDwarfUnit() const
Definition: DWARFDie.h:54
bool isValid() const
Definition: DWARFDie.h:51
This class represents lattice values for constants.
Definition: AllocatorList.h:24
bool isSubprogramDIE() const
Returns true if DIE represents a subprogram (not inlined).
Definition: DWARFDie.cpp:366
bool addressRangeContainsAddress(const uint64_t Address) const
Definition: DWARFDie.cpp:514
Attribute
Attributes.
Definition: Dwarf.h:115
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
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:498
const DWARFAbbreviationDeclaration * getAbbreviationDeclarationPtr() const
Get the abbreviation declaration for this DIE.
Definition: DWARFDie.h:59
DWARFDie getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE as the referenced DIE.
Definition: DWARFDie.cpp:433
DINameKind
A DINameKind is passed to name search methods to specify a preference regarding the type of name reso...
Definition: DIContext.h:119
Definition: BitVector.h:938
iterator_range< iterator > children() const
Definition: DWARFDie.h:383
Optional< uint64_t > getRangesBaseAttribute() const
Extract the range base attribute from this DIE as absolute section offset.
Definition: DWARFDie.cpp:448
reverse_iterator(llvm::DWARFDie::iterator It)
Definition: DWARFDie.h:402
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:552
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
#define LLVM_DUMP_METHOD
Definition: Compiler.h:74
const DWARFDebugInfoEntry * getDebugInfoEntry() const
Definition: DWARFDie.h:53
uint32_t getOffset() const
Get the absolute offset into the debug info or types section.
Definition: DWARFDie.h:67
Encapsulates a DWARF attribute value and all of the data required to describe the attribute value...
const DWARFDie & operator*() const
Definition: DWARFDie.h:360
bool hasChildren() const
Definition: DWARFDie.h:79
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
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:466
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
DWARFDie getSibling() const
Get the sibling of this DIE object.
Definition: DWARFDie.cpp:641
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:68
uint64_t getDeclLine() const
Returns the declaration line (start line) for a DIE, assuming it specifies a subprogram.
Definition: DWARFDie.cpp:548
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:159
Utility class that carries the DWARF compile/type unit and the debug info entry in an object...
Definition: DWARFDie.h:43
reverse_iterator< llvm::DWARFDie::iterator > & operator++()
Definition: DWARFDie.h:412
DWARFDie getLastChild() const
Get the last child of this DIE object.
Definition: DWARFDie.cpp:659
struct UnitT Unit
bool isSubroutineDIE() const
Returns true if DIE represents a subprogram or an inlined subroutine.
Definition: DWARFDie.cpp:368
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:397
bool isNULL() const
Returns true for a valid DIE that terminates a sibling chain.
Definition: DWARFDie.h:85
std::reverse_iterator< iterator > rend() const
Definition: DWARFDie.h:463
iterator end() const
Definition: DWARFDie.h:379
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
const DWARFAbbreviationDeclaration * getAbbreviationDeclarationPtr() const
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:533
DWARFDie getPreviousSibling() const
Get the previous sibling of this DIE object.
Definition: DWARFDie.cpp:647
reverse_iterator< llvm::DWARFDie::iterator > & operator--()
Definition: DWARFDie.h:422
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
iterator_range< attribute_iterator > attributes() const
Get an iterator range to all attributes in the current DIE only.
Definition: DWARFDie.cpp:665
A range adaptor for a pair of iterators.
This file contains constants used for implementing Dwarf debug support.
const DWARFAttribute & operator*() const
Definition: DWARFDie.h:310
const llvm::DWARFDie & operator*() const
Definition: DWARFDie.h:432
bool operator!=(uint64_t V1, const APInt &V2)
Definition: APInt.h:1969
LLVM_DUMP_METHOD void dump() const
Convenience zero-argument overload for debugging.
Definition: DWARFDie.cpp:633
dwarf::Tag getTag() const
Definition: DWARFDie.h:72
#define I(x, y, z)
Definition: MD5.cpp:58
Expected< DWARFAddressRangesVector > getAddressRanges() const
Get the address ranges for this DIE.
Definition: DWARFDie.cpp:481
llvm::DWARFDie::iterator base() const
Definition: DWARFDie.h:408
Optional< uint64_t > getHighPC(uint64_t LowPC) const
Get the DW_AT_high_pc attribute value as an address.
Definition: DWARFDie.cpp:452
iterator & operator++()
Definition: DWARFDie.h:350
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool isValid() const
std::reverse_iterator< IteratorTy > make_reverse_iterator(IteratorTy It)
Definition: STLExtras.h:275
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:326
Optional< DWARFFormValue > find(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE.
Definition: DWARFDie.cpp:373
std::vector< DWARFAddressRange > DWARFAddressRangesVector
DWARFAddressRangesVector - represents a set of absolute address ranges.
DWARFDebugInfoEntry - A DIE with only the minimum required data.
DWARFDie getFirstChild() const
Get the first child of this DIE object.
Definition: DWARFDie.cpp:653
iterator(DWARFDie D)
Definition: DWARFDie.h:348
std::reverse_iterator< iterator > rbegin() const
Definition: DWARFDie.h:459
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
bool operator==(uint64_t V1, const APInt &V2)
Definition: APInt.h:1967
bool equals(const reverse_iterator< llvm::DWARFDie::iterator > &RHS) const
Definition: DWARFDie.h:440
iterator begin() const
Definition: DWARFDie.h:375
DWARFDie(DWARFUnit *Unit, const DWARFDebugInfoEntry *D)
Definition: DWARFDie.h:49
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:527
DWARFDie getParent() const
Get the parent of this DIE object.
Definition: DWARFDie.cpp:635
iterator & operator--()
Definition: DWARFDie.h:355