1 //===-- DWARFDebugInfoEntry.h -----------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef SymbolFileDWARF_DWARFDebugInfoEntry_h_
10 #define SymbolFileDWARF_DWARFDebugInfoEntry_h_
11 
12 #include "SymbolFileDWARF.h"
13 #include "llvm/ADT/SmallVector.h"
14 
15 #include "DWARFAbbreviationDeclaration.h"
16 #include "DWARFDebugAbbrev.h"
17 #include "DWARFDebugRanges.h"
18 #include <map>
19 #include <set>
20 #include <vector>
21 
22 class DWARFDeclContext;
23 
24 #define DIE_SIBLING_IDX_BITSIZE 31
25 
26 /// DWARFDebugInfoEntry objects assume that they are living in one big
27 /// vector and do pointer arithmetic on their this pointers. Don't
28 /// pass them by value. Due to the way they are constructed in a
29 /// std::vector, we cannot delete the copy constructor.
30 class DWARFDebugInfoEntry {
31 public:
32   typedef std::vector<DWARFDebugInfoEntry> collection;
33   typedef collection::iterator iterator;
34   typedef collection::const_iterator const_iterator;
35 
36   DWARFDebugInfoEntry()
37       : m_offset(DW_INVALID_OFFSET), m_parent_idx(0), m_sibling_idx(0),
38         m_has_children(false), m_abbr_idx(0), m_tag(llvm::dwarf::DW_TAG_null) {}
39 
40   explicit operator bool() const { return m_offset != DW_INVALID_OFFSET; }
41   bool operator==(const DWARFDebugInfoEntry &rhs) const;
42   bool operator!=(const DWARFDebugInfoEntry &rhs) const;
43 
44   void BuildAddressRangeTable(const DWARFUnit *cu,
45                               DWARFDebugAranges *debug_aranges) const;
46 
47   void BuildFunctionAddressRangeTable(const DWARFUnit *cu,
48                                       DWARFDebugAranges *debug_aranges) const;
49 
50   bool Extract(const lldb_private::DWARFDataExtractor &data,
51                const DWARFUnit *cu, lldb::offset_t *offset_ptr);
52 
53   bool LookupAddress(const dw_addr_t address, DWARFUnit *cu,
54                      DWARFDebugInfoEntry **function_die,
55                      DWARFDebugInfoEntry **block_die);
56 
57   size_t GetAttributes(const DWARFUnit *cu,
58                        DWARFAttributes &attrs,
59                        uint32_t curr_depth = 0)
60       const; // "curr_depth" for internal use only, don't set this yourself!!!
61 
62   dw_offset_t
63   GetAttributeValue(const DWARFUnit *cu, const dw_attr_t attr,
64                     DWARFFormValue &formValue,
65                     dw_offset_t *end_attr_offset_ptr = nullptr,
66                     bool check_specification_or_abstract_origin = false) const;
67 
68   const char *GetAttributeValueAsString(
69       const DWARFUnit *cu, const dw_attr_t attr, const char *fail_value,
70       bool check_specification_or_abstract_origin = false) const;
71 
72   uint64_t GetAttributeValueAsUnsigned(
73       const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value,
74       bool check_specification_or_abstract_origin = false) const;
75 
76   DWARFDIE GetAttributeValueAsReference(
77       const DWARFUnit *cu, const dw_attr_t attr,
78       bool check_specification_or_abstract_origin = false) const;
79 
80   uint64_t GetAttributeValueAsAddress(
81       const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value,
82       bool check_specification_or_abstract_origin = false) const;
83 
84   dw_addr_t
85   GetAttributeHighPC(const DWARFUnit *cu, dw_addr_t lo_pc, uint64_t fail_value,
86                      bool check_specification_or_abstract_origin = false) const;
87 
88   bool GetAttributeAddressRange(
89       const DWARFUnit *cu, dw_addr_t &lo_pc, dw_addr_t &hi_pc,
90       uint64_t fail_value,
91       bool check_specification_or_abstract_origin = false) const;
92 
93   size_t GetAttributeAddressRanges(
94       DWARFUnit *cu, DWARFRangeList &ranges, bool check_hi_lo_pc,
95       bool check_specification_or_abstract_origin = false) const;
96 
97   const char *GetName(const DWARFUnit *cu) const;
98 
99   const char *GetMangledName(const DWARFUnit *cu,
100                              bool substitute_name_allowed = true) const;
101 
102   const char *GetPubname(const DWARFUnit *cu) const;
103 
104   const char *GetQualifiedName(DWARFUnit *cu, std::string &storage) const;
105 
106   const char *GetQualifiedName(DWARFUnit *cu, const DWARFAttributes &attributes,
107                                std::string &storage) const;
108 
109   void Dump(const DWARFUnit *cu, lldb_private::Stream &s,
110             uint32_t recurse_depth) const;
111 
112   static void
113   DumpAttribute(const DWARFUnit *cu,
114                 const lldb_private::DWARFDataExtractor &data,
115                 lldb::offset_t *offset_ptr, lldb_private::Stream &s,
116                 dw_attr_t attr, DWARFFormValue &form_value);
117 
118   bool GetDIENamesAndRanges(
119       DWARFUnit *cu, const char *&name, const char *&mangled,
120       DWARFRangeList &rangeList, int &decl_file, int &decl_line,
121       int &decl_column, int &call_file, int &call_line, int &call_column,
122       lldb_private::DWARFExpression *frame_base = nullptr) const;
123 
124   const DWARFAbbreviationDeclaration *
125   GetAbbreviationDeclarationPtr(const DWARFUnit *cu) const;
126 
127   lldb::offset_t GetFirstAttributeOffset() const;
128 
129   dw_tag_t Tag() const { return m_tag; }
130 
131   bool IsNULL() const { return m_abbr_idx == 0; }
132 
133   dw_offset_t GetOffset() const { return m_offset; }
134 
135   bool HasChildren() const { return m_has_children; }
136 
137   void SetHasChildren(bool b) { m_has_children = b; }
138 
139   // We know we are kept in a vector of contiguous entries, so we know
140   // our parent will be some index behind "this".
141   DWARFDebugInfoEntry *GetParent() {
142     return m_parent_idx > 0 ? this - m_parent_idx : nullptr;
143   }
144   const DWARFDebugInfoEntry *GetParent() const {
145     return m_parent_idx > 0 ? this - m_parent_idx : nullptr;
146   }
147   // We know we are kept in a vector of contiguous entries, so we know
148   // our sibling will be some index after "this".
149   DWARFDebugInfoEntry *GetSibling() {
150     return m_sibling_idx > 0 ? this + m_sibling_idx : nullptr;
151   }
152   const DWARFDebugInfoEntry *GetSibling() const {
153     return m_sibling_idx > 0 ? this + m_sibling_idx : nullptr;
154   }
155   // We know we are kept in a vector of contiguous entries, so we know
156   // we don't need to store our child pointer, if we have a child it will
157   // be the next entry in the list...
158   DWARFDebugInfoEntry *GetFirstChild() {
159     return HasChildren() ? this + 1 : nullptr;
160   }
161   const DWARFDebugInfoEntry *GetFirstChild() const {
162     return HasChildren() ? this + 1 : nullptr;
163   }
164 
165   void GetDWARFDeclContext(DWARFUnit *cu,
166                            DWARFDeclContext &dwarf_decl_ctx) const;
167 
168   DWARFDIE GetParentDeclContextDIE(DWARFUnit *cu) const;
169   DWARFDIE GetParentDeclContextDIE(DWARFUnit *cu,
170                                    const DWARFAttributes &attributes) const;
171 
172   void SetSiblingIndex(uint32_t idx) { m_sibling_idx = idx; }
173   void SetParentIndex(uint32_t idx) { m_parent_idx = idx; }
174 
175 protected:
176   dw_offset_t m_offset; // Offset within the .debug_info/.debug_types
177   uint32_t m_parent_idx; // How many to subtract from "this" to get the parent.
178                          // If zero this die has no parent
179   uint32_t m_sibling_idx : 31, // How many to add to "this" to get the sibling.
180       // If it is zero, then the DIE doesn't have children, or the
181       // DWARF claimed it had children but the DIE only contained
182       // a single NULL terminating child.
183       m_has_children : 1;
184   uint16_t m_abbr_idx;
185   /// A copy of the DW_TAG value so we don't have to go through the compile
186   /// unit abbrev table
187   dw_tag_t m_tag = llvm::dwarf::DW_TAG_null;
188 };
189 
190 #endif // SymbolFileDWARF_DWARFDebugInfoEntry_h_
191