DWARFDie.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- DWARFDie.h -----------------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEBUGINFO_DWARF_DWARFDIE_H
  14. #define LLVM_DEBUGINFO_DWARF_DWARFDIE_H
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/Optional.h"
  17. #include "llvm/ADT/iterator.h"
  18. #include "llvm/ADT/iterator_range.h"
  19. #include "llvm/BinaryFormat/Dwarf.h"
  20. #include "llvm/DebugInfo/DIContext.h"
  21. #include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
  22. #include "llvm/DebugInfo/DWARF/DWARFAttribute.h"
  23. #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
  24. #include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
  25. #include <cassert>
  26. #include <cstdint>
  27. #include <iterator>
  28. namespace llvm {
  29. class DWARFUnit;
  30. class raw_ostream;
  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. public:
  47. DWARFDie() = default;
  48. DWARFDie(DWARFUnit *Unit, const DWARFDebugInfoEntry *D) : U(Unit), Die(D) {}
  49. bool isValid() const { return U && Die; }
  50. explicit operator bool() const { return isValid(); }
  51. const DWARFDebugInfoEntry *getDebugInfoEntry() const { return Die; }
  52. DWARFUnit *getDwarfUnit() const { return U; }
  53. /// Get the abbreviation declaration for this DIE.
  54. ///
  55. /// \returns the abbreviation declaration or NULL for null tags.
  56. const DWARFAbbreviationDeclaration *getAbbreviationDeclarationPtr() const {
  57. assert(isValid() && "must check validity prior to calling");
  58. return Die->getAbbreviationDeclarationPtr();
  59. }
  60. /// Get the absolute offset into the debug info or types section.
  61. ///
  62. /// \returns the DIE offset or -1U if invalid.
  63. uint64_t getOffset() const {
  64. assert(isValid() && "must check validity prior to calling");
  65. return Die->getOffset();
  66. }
  67. dwarf::Tag getTag() const {
  68. auto AbbrevDecl = getAbbreviationDeclarationPtr();
  69. if (AbbrevDecl)
  70. return AbbrevDecl->getTag();
  71. return dwarf::DW_TAG_null;
  72. }
  73. bool hasChildren() const {
  74. assert(isValid() && "must check validity prior to calling");
  75. return Die->hasChildren();
  76. }
  77. /// Returns true for a valid DIE that terminates a sibling chain.
  78. bool isNULL() const { return getAbbreviationDeclarationPtr() == nullptr; }
  79. /// Returns true if DIE represents a subprogram (not inlined).
  80. bool isSubprogramDIE() const;
  81. /// Returns true if DIE represents a subprogram or an inlined subroutine.
  82. bool isSubroutineDIE() const;
  83. /// Get the parent of this DIE object.
  84. ///
  85. /// \returns a valid DWARFDie instance if this object has a parent or an
  86. /// invalid DWARFDie instance if it doesn't.
  87. DWARFDie getParent() const;
  88. /// Get the sibling of this DIE object.
  89. ///
  90. /// \returns a valid DWARFDie instance if this object has a sibling or an
  91. /// invalid DWARFDie instance if it doesn't.
  92. DWARFDie getSibling() const;
  93. /// Get the previous sibling of this DIE object.
  94. ///
  95. /// \returns a valid DWARFDie instance if this object has a sibling or an
  96. /// invalid DWARFDie instance if it doesn't.
  97. DWARFDie getPreviousSibling() const;
  98. /// Get the first child of this DIE object.
  99. ///
  100. /// \returns a valid DWARFDie instance if this object has children or an
  101. /// invalid DWARFDie instance if it doesn't.
  102. DWARFDie getFirstChild() const;
  103. /// Get the last child of this DIE object.
  104. ///
  105. /// \returns a valid null DWARFDie instance if this object has children or an
  106. /// invalid DWARFDie instance if it doesn't.
  107. DWARFDie getLastChild() const;
  108. /// Dump the DIE and all of its attributes to the supplied stream.
  109. ///
  110. /// \param OS the stream to use for output.
  111. /// \param indent the number of characters to indent each line that is output.
  112. void dump(raw_ostream &OS, unsigned indent = 0,
  113. DIDumpOptions DumpOpts = DIDumpOptions()) const;
  114. /// Convenience zero-argument overload for debugging.
  115. LLVM_DUMP_METHOD void dump() const;
  116. /// Extract the specified attribute from this DIE.
  117. ///
  118. /// Extract an attribute value from this DIE only. This call doesn't look
  119. /// for the attribute value in any DW_AT_specification or
  120. /// DW_AT_abstract_origin referenced DIEs.
  121. ///
  122. /// \param Attr the attribute to extract.
  123. /// \returns an optional DWARFFormValue that will have the form value if the
  124. /// attribute was successfully extracted.
  125. Optional<DWARFFormValue> find(dwarf::Attribute Attr) const;
  126. /// Extract the first value of any attribute in Attrs from this DIE.
  127. ///
  128. /// Extract the first attribute that matches from this DIE only. This call
  129. /// doesn't look for the attribute value in any DW_AT_specification or
  130. /// DW_AT_abstract_origin referenced DIEs. The attributes will be searched
  131. /// linearly in the order they are specified within Attrs.
  132. ///
  133. /// \param Attrs an array of DWARF attribute to look for.
  134. /// \returns an optional that has a valid DWARFFormValue for the first
  135. /// matching attribute in Attrs, or None if none of the attributes in Attrs
  136. /// exist in this DIE.
  137. Optional<DWARFFormValue> find(ArrayRef<dwarf::Attribute> Attrs) const;
  138. /// Extract the first value of any attribute in Attrs from this DIE and
  139. /// recurse into any DW_AT_specification or DW_AT_abstract_origin referenced
  140. /// DIEs.
  141. ///
  142. /// \param Attrs an array of DWARF attribute to look for.
  143. /// \returns an optional that has a valid DWARFFormValue for the first
  144. /// matching attribute in Attrs, or None if none of the attributes in Attrs
  145. /// exist in this DIE or in any DW_AT_specification or DW_AT_abstract_origin
  146. /// DIEs.
  147. Optional<DWARFFormValue>
  148. findRecursively(ArrayRef<dwarf::Attribute> Attrs) const;
  149. /// Extract the specified attribute from this DIE as the referenced DIE.
  150. ///
  151. /// Regardless of the reference type, return the correct DWARFDie instance if
  152. /// the attribute exists. The returned DWARFDie object might be from another
  153. /// DWARFUnit, but that is all encapsulated in the new DWARFDie object.
  154. ///
  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.
  158. ///
  159. /// \param Attr the attribute to extract.
  160. /// \returns a valid DWARFDie instance if the attribute exists, or an invalid
  161. /// DWARFDie object if it doesn't.
  162. DWARFDie getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const;
  163. DWARFDie getAttributeValueAsReferencedDie(const DWARFFormValue &V) const;
  164. DWARFDie resolveTypeUnitReference() const;
  165. /// Extract the range base attribute from this DIE as absolute section offset.
  166. ///
  167. /// This is a utility function that checks for either the DW_AT_rnglists_base
  168. /// or DW_AT_GNU_ranges_base attribute.
  169. ///
  170. /// \returns anm optional absolute section offset value for the attribute.
  171. Optional<uint64_t> getRangesBaseAttribute() const;
  172. Optional<uint64_t> getLocBaseAttribute() const;
  173. /// Get the DW_AT_high_pc attribute value as an address.
  174. ///
  175. /// In DWARF version 4 and later the high PC can be encoded as an offset from
  176. /// the DW_AT_low_pc. This function takes care of extracting the value as an
  177. /// address or offset and adds it to the low PC if needed and returns the
  178. /// value as an optional in case the DIE doesn't have a DW_AT_high_pc
  179. /// attribute.
  180. ///
  181. /// \param LowPC the low PC that might be needed to calculate the high PC.
  182. /// \returns an optional address value for the attribute.
  183. Optional<uint64_t> getHighPC(uint64_t LowPC) const;
  184. /// Retrieves DW_AT_low_pc and DW_AT_high_pc from CU.
  185. /// Returns true if both attributes are present.
  186. bool getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
  187. uint64_t &SectionIndex) const;
  188. /// Get the address ranges for this DIE.
  189. ///
  190. /// Get the hi/low PC range if both attributes are available or exrtracts the
  191. /// non-contiguous address ranges from the DW_AT_ranges attribute.
  192. ///
  193. /// Extracts the range information from this DIE only. This call doesn't look
  194. /// for the range in any DW_AT_specification or DW_AT_abstract_origin DIEs.
  195. ///
  196. /// \returns a address range vector that might be empty if no address range
  197. /// information is available.
  198. Expected<DWARFAddressRangesVector> getAddressRanges() const;
  199. bool addressRangeContainsAddress(const uint64_t Address) const;
  200. Expected<DWARFLocationExpressionsVector>
  201. getLocations(dwarf::Attribute Attr) const;
  202. /// If a DIE represents a subprogram (or inlined subroutine), returns its
  203. /// mangled name (or short name, if mangled is missing). This name may be
  204. /// fetched from specification or abstract origin for this subprogram.
  205. /// Returns null if no name is found.
  206. const char *getSubroutineName(DINameKind Kind) const;
  207. /// Return the DIE name resolving DW_AT_specification or DW_AT_abstract_origin
  208. /// references if necessary. For the LinkageName case it additionaly searches
  209. /// for ShortName if LinkageName is not found.
  210. /// Returns null if no name is found.
  211. const char *getName(DINameKind Kind) const;
  212. void getFullName(raw_string_ostream &,
  213. std::string *OriginalFullName = nullptr) const;
  214. /// Return the DIE short name resolving DW_AT_specification or
  215. /// DW_AT_abstract_origin references if necessary. Returns null if no name
  216. /// is found.
  217. const char *getShortName() const;
  218. /// Return the DIE linkage name resolving DW_AT_specification or
  219. /// DW_AT_abstract_origin references if necessary. Returns null if no name
  220. /// is found.
  221. const char *getLinkageName() const;
  222. /// Returns the declaration line (start line) for a DIE, assuming it specifies
  223. /// a subprogram. This may be fetched from specification or abstract origin
  224. /// for this subprogram by resolving DW_AT_sepcification or
  225. /// DW_AT_abstract_origin references if necessary.
  226. uint64_t getDeclLine() const;
  227. std::string getDeclFile(DILineInfoSpecifier::FileLineInfoKind Kind) const;
  228. /// Retrieves values of DW_AT_call_file, DW_AT_call_line and DW_AT_call_column
  229. /// from DIE (or zeroes if they are missing). This function looks for
  230. /// DW_AT_call attributes in this DIE only, it will not resolve the attribute
  231. /// values in any DW_AT_specification or DW_AT_abstract_origin DIEs.
  232. /// \param CallFile filled in with non-zero if successful, zero if there is no
  233. /// DW_AT_call_file attribute in this DIE.
  234. /// \param CallLine filled in with non-zero if successful, zero if there is no
  235. /// DW_AT_call_line attribute in this DIE.
  236. /// \param CallColumn filled in with non-zero if successful, zero if there is
  237. /// no DW_AT_call_column attribute in this DIE.
  238. /// \param CallDiscriminator filled in with non-zero if successful, zero if
  239. /// there is no DW_AT_GNU_discriminator attribute in this DIE.
  240. void getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
  241. uint32_t &CallColumn, uint32_t &CallDiscriminator) const;
  242. class attribute_iterator;
  243. /// Get an iterator range to all attributes in the current DIE only.
  244. ///
  245. /// \returns an iterator range for the attributes of the current DIE.
  246. iterator_range<attribute_iterator> attributes() const;
  247. class iterator;
  248. iterator begin() const;
  249. iterator end() const;
  250. std::reverse_iterator<iterator> rbegin() const;
  251. std::reverse_iterator<iterator> rend() const;
  252. iterator_range<iterator> children() const;
  253. };
  254. class DWARFDie::attribute_iterator
  255. : public iterator_facade_base<attribute_iterator, std::forward_iterator_tag,
  256. const DWARFAttribute> {
  257. /// The DWARF DIE we are extracting attributes from.
  258. DWARFDie Die;
  259. /// The value vended to clients via the operator*() or operator->().
  260. DWARFAttribute AttrValue;
  261. /// The attribute index within the abbreviation declaration in Die.
  262. uint32_t Index;
  263. friend bool operator==(const attribute_iterator &LHS,
  264. const attribute_iterator &RHS);
  265. /// Update the attribute index and attempt to read the attribute value. If the
  266. /// attribute is able to be read, update AttrValue and the Index member
  267. /// variable. If the attribute value is not able to be read, an appropriate
  268. /// error will be set if the Err member variable is non-NULL and the iterator
  269. /// will be set to the end value so iteration stops.
  270. void updateForIndex(const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I);
  271. public:
  272. attribute_iterator() = delete;
  273. explicit attribute_iterator(DWARFDie D, bool End);
  274. attribute_iterator &operator++();
  275. attribute_iterator &operator--();
  276. explicit operator bool() const { return AttrValue.isValid(); }
  277. const DWARFAttribute &operator*() const { return AttrValue; }
  278. };
  279. inline bool operator==(const DWARFDie::attribute_iterator &LHS,
  280. const DWARFDie::attribute_iterator &RHS) {
  281. return LHS.Index == RHS.Index;
  282. }
  283. inline bool operator!=(const DWARFDie::attribute_iterator &LHS,
  284. const DWARFDie::attribute_iterator &RHS) {
  285. return !(LHS == RHS);
  286. }
  287. inline bool operator==(const DWARFDie &LHS, const DWARFDie &RHS) {
  288. return LHS.getDebugInfoEntry() == RHS.getDebugInfoEntry() &&
  289. LHS.getDwarfUnit() == RHS.getDwarfUnit();
  290. }
  291. inline bool operator!=(const DWARFDie &LHS, const DWARFDie &RHS) {
  292. return !(LHS == RHS);
  293. }
  294. inline bool operator<(const DWARFDie &LHS, const DWARFDie &RHS) {
  295. return LHS.getOffset() < RHS.getOffset();
  296. }
  297. class DWARFDie::iterator
  298. : public iterator_facade_base<iterator, std::bidirectional_iterator_tag,
  299. const DWARFDie> {
  300. DWARFDie Die;
  301. friend std::reverse_iterator<llvm::DWARFDie::iterator>;
  302. friend bool operator==(const DWARFDie::iterator &LHS,
  303. const DWARFDie::iterator &RHS);
  304. public:
  305. iterator() = default;
  306. explicit iterator(DWARFDie D) : Die(D) {}
  307. iterator &operator++() {
  308. Die = Die.getSibling();
  309. return *this;
  310. }
  311. iterator &operator--() {
  312. Die = Die.getPreviousSibling();
  313. return *this;
  314. }
  315. const DWARFDie &operator*() const { return Die; }
  316. };
  317. inline bool operator==(const DWARFDie::iterator &LHS,
  318. const DWARFDie::iterator &RHS) {
  319. return LHS.Die == RHS.Die;
  320. }
  321. // These inline functions must follow the DWARFDie::iterator definition above
  322. // as they use functions from that class.
  323. inline DWARFDie::iterator DWARFDie::begin() const {
  324. return iterator(getFirstChild());
  325. }
  326. inline DWARFDie::iterator DWARFDie::end() const {
  327. return iterator(getLastChild());
  328. }
  329. inline iterator_range<DWARFDie::iterator> DWARFDie::children() const {
  330. return make_range(begin(), end());
  331. }
  332. } // end namespace llvm
  333. namespace std {
  334. template <>
  335. class reverse_iterator<llvm::DWARFDie::iterator>
  336. : public llvm::iterator_facade_base<
  337. reverse_iterator<llvm::DWARFDie::iterator>,
  338. bidirectional_iterator_tag, const llvm::DWARFDie> {
  339. private:
  340. llvm::DWARFDie Die;
  341. bool AtEnd;
  342. public:
  343. reverse_iterator(llvm::DWARFDie::iterator It)
  344. : Die(It.Die), AtEnd(!It.Die.getPreviousSibling()) {
  345. if (!AtEnd)
  346. Die = Die.getPreviousSibling();
  347. }
  348. llvm::DWARFDie::iterator base() const {
  349. return llvm::DWARFDie::iterator(AtEnd ? Die : Die.getSibling());
  350. }
  351. reverse_iterator<llvm::DWARFDie::iterator> &operator++() {
  352. assert(!AtEnd && "Incrementing rend");
  353. llvm::DWARFDie D = Die.getPreviousSibling();
  354. if (D)
  355. Die = D;
  356. else
  357. AtEnd = true;
  358. return *this;
  359. }
  360. reverse_iterator<llvm::DWARFDie::iterator> &operator--() {
  361. if (AtEnd) {
  362. AtEnd = false;
  363. return *this;
  364. }
  365. Die = Die.getSibling();
  366. assert(!Die.isNULL() && "Decrementing rbegin");
  367. return *this;
  368. }
  369. const llvm::DWARFDie &operator*() const {
  370. assert(Die.isValid());
  371. return Die;
  372. }
  373. // FIXME: We should be able to specify the equals operator as a friend, but
  374. // that causes the compiler to think the operator overload is ambiguous
  375. // with the friend declaration and the actual definition as candidates.
  376. bool equals(const reverse_iterator<llvm::DWARFDie::iterator> &RHS) const {
  377. return Die == RHS.Die && AtEnd == RHS.AtEnd;
  378. }
  379. };
  380. } // namespace std
  381. namespace llvm {
  382. inline bool operator==(const std::reverse_iterator<DWARFDie::iterator> &LHS,
  383. const std::reverse_iterator<DWARFDie::iterator> &RHS) {
  384. return LHS.equals(RHS);
  385. }
  386. inline bool operator!=(const std::reverse_iterator<DWARFDie::iterator> &LHS,
  387. const std::reverse_iterator<DWARFDie::iterator> &RHS) {
  388. return !(LHS == RHS);
  389. }
  390. inline std::reverse_iterator<DWARFDie::iterator> DWARFDie::rbegin() const {
  391. return std::make_reverse_iterator(end());
  392. }
  393. inline std::reverse_iterator<DWARFDie::iterator> DWARFDie::rend() const {
  394. return std::make_reverse_iterator(begin());
  395. }
  396. void dumpTypeQualifiedName(const DWARFDie &DIE, raw_ostream &OS);
  397. void dumpTypeUnqualifiedName(const DWARFDie &DIE, raw_ostream &OS,
  398. std::string *OriginalFullName = nullptr);
  399. } // end namespace llvm
  400. #endif // LLVM_DEBUGINFO_DWARF_DWARFDIE_H
  401. #ifdef __GNUC__
  402. #pragma GCC diagnostic pop
  403. #endif