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 LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFDEBUGINFOENTRY_H
10 #define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFDEBUGINFOENTRY_H
11 
12 #include "SymbolFileDWARF.h"
13 #include "llvm/ADT/SmallVector.h"
14 
15 #include "DWARFAttribute.h"
16 #include "DWARFBaseDIE.h"
17 #include "DWARFDebugAbbrev.h"
18 #include "DWARFDebugRanges.h"
19 #include <map>
20 #include <optional>
21 #include <set>
22 #include <vector>
23 
24 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
25 
26 class DWARFDeclContext;
27 
28 #define DIE_SIBLING_IDX_BITSIZE 31
29 
30 /// DWARFDebugInfoEntry objects assume that they are living in one big
31 /// vector and do pointer arithmetic on their this pointers. Don't
32 /// pass them by value. Due to the way they are constructed in a
33 /// std::vector, we cannot delete the copy constructor.
34 class DWARFDebugInfoEntry {
35 public:
36   typedef std::vector<DWARFDebugInfoEntry> collection;
37   typedef collection::iterator iterator;
38   typedef collection::const_iterator const_iterator;
39 
40   DWARFDebugInfoEntry()
41       : m_offset(DW_INVALID_OFFSET), m_parent_idx(0), m_sibling_idx(0),
42         m_has_children(false) {}
43 
44   explicit operator bool() const { return m_offset != DW_INVALID_OFFSET; }
45   bool operator==(const DWARFDebugInfoEntry &rhs) const;
46   bool operator!=(const DWARFDebugInfoEntry &rhs) const;
47 
48   void BuildFunctionAddressRangeTable(DWARFUnit *cu,
49                                       DWARFDebugAranges *debug_aranges) const;
50 
51   bool Extract(const lldb_private::DWARFDataExtractor &data,
52                const DWARFUnit *cu, lldb::offset_t *offset_ptr);
53 
54   using Recurse = DWARFBaseDIE::Recurse;
55   DWARFAttributes GetAttributes(DWARFUnit *cu,
56                                 Recurse recurse = Recurse::yes) const {
57     DWARFAttributes attrs;
58     GetAttributes(cu, attrs, recurse, 0 /* curr_depth */);
59     return attrs;
60   }
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   std::optional<uint64_t> GetAttributeValueAsOptionalUnsigned(
77       const DWARFUnit *cu, const dw_attr_t attr,
78       bool check_specification_or_abstract_origin = false) const;
79 
80   DWARFDIE GetAttributeValueAsReference(
81       const DWARFUnit *cu, const dw_attr_t attr,
82       bool check_specification_or_abstract_origin = false) const;
83 
84   uint64_t GetAttributeValueAsAddress(
85       const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value,
86       bool check_specification_or_abstract_origin = false) const;
87 
88   dw_addr_t
89   GetAttributeHighPC(const DWARFUnit *cu, dw_addr_t lo_pc, uint64_t fail_value,
90                      bool check_specification_or_abstract_origin = false) const;
91 
92   bool GetAttributeAddressRange(
93       const DWARFUnit *cu, dw_addr_t &lo_pc, dw_addr_t &hi_pc,
94       uint64_t fail_value,
95       bool check_specification_or_abstract_origin = false) const;
96 
97   DWARFRangeList GetAttributeAddressRanges(
98       DWARFUnit *cu, bool check_hi_lo_pc,
99       bool check_specification_or_abstract_origin = false) const;
100 
101   const char *GetName(const DWARFUnit *cu) const;
102 
103   const char *GetMangledName(const DWARFUnit *cu,
104                              bool substitute_name_allowed = true) const;
105 
106   const char *GetPubname(const DWARFUnit *cu) const;
107 
108   bool GetDIENamesAndRanges(
109       DWARFUnit *cu, const char *&name, const char *&mangled,
110       DWARFRangeList &rangeList, std::optional<int> &decl_file,
111       std::optional<int> &decl_line, std::optional<int> &decl_column,
112       std::optional<int> &call_file, std::optional<int> &call_line,
113       std::optional<int> &call_column,
114       lldb_private::DWARFExpressionList *frame_base = nullptr) const;
115 
116   const llvm::DWARFAbbreviationDeclaration *
117   GetAbbreviationDeclarationPtr(const DWARFUnit *cu) const;
118 
119   lldb::offset_t GetFirstAttributeOffset() const;
120 
121   dw_tag_t Tag() const { return m_tag; }
122 
123   bool IsNULL() const { return m_abbr_idx == 0; }
124 
125   dw_offset_t GetOffset() const { return m_offset; }
126 
127   bool HasChildren() const { return m_has_children; }
128 
129   void SetHasChildren(bool b) { m_has_children = b; }
130 
131   // We know we are kept in a vector of contiguous entries, so we know
132   // our parent will be some index behind "this".
133   DWARFDebugInfoEntry *GetParent() {
134     return m_parent_idx > 0 ? this - m_parent_idx : nullptr;
135   }
136   const DWARFDebugInfoEntry *GetParent() const {
137     return m_parent_idx > 0 ? this - m_parent_idx : nullptr;
138   }
139   // We know we are kept in a vector of contiguous entries, so we know
140   // our sibling will be some index after "this".
141   DWARFDebugInfoEntry *GetSibling() {
142     return m_sibling_idx > 0 ? this + m_sibling_idx : nullptr;
143   }
144   const DWARFDebugInfoEntry *GetSibling() const {
145     return m_sibling_idx > 0 ? this + m_sibling_idx : nullptr;
146   }
147   // We know we are kept in a vector of contiguous entries, so we know
148   // we don't need to store our child pointer, if we have a child it will
149   // be the next entry in the list...
150   DWARFDebugInfoEntry *GetFirstChild() {
151     return HasChildren() ? this + 1 : nullptr;
152   }
153   const DWARFDebugInfoEntry *GetFirstChild() const {
154     return HasChildren() ? this + 1 : nullptr;
155   }
156 
157   DWARFDeclContext GetDWARFDeclContext(DWARFUnit *cu) const;
158 
159   DWARFDIE GetParentDeclContextDIE(DWARFUnit *cu) const;
160   DWARFDIE GetParentDeclContextDIE(DWARFUnit *cu,
161                                    const DWARFAttributes &attributes) const;
162 
163   void SetSiblingIndex(uint32_t idx) { m_sibling_idx = idx; }
164   void SetParentIndex(uint32_t idx) { m_parent_idx = idx; }
165 
166   // This function returns true if the variable scope is either
167   // global or (file-static). It will return false for static variables
168   // that are local to a function, as they have local scope.
169   bool IsGlobalOrStaticScopeVariable() const;
170 
171 protected:
172   static DWARFDeclContext
173   GetDWARFDeclContextStatic(const DWARFDebugInfoEntry *die, DWARFUnit *cu);
174 
175   // Up to 2TB offset within the .debug_info/.debug_types
176   dw_offset_t m_offset : DW_DIE_OFFSET_MAX_BITSIZE;
177   // How many to subtract from "this" to get the parent. If zero this die has no
178   // parent
179   dw_offset_t m_parent_idx : 64 - DW_DIE_OFFSET_MAX_BITSIZE;
180   // How many to add to "this" to get the sibling.
181   // If it is zero, then the DIE doesn't have children,
182   // or the DWARF claimed it had children but the DIE
183   // only contained a single NULL terminating child.
184   uint32_t m_sibling_idx : 31, m_has_children : 1;
185   uint16_t m_abbr_idx = 0;
186   /// A copy of the DW_TAG value so we don't have to go through the compile
187   /// unit abbrev table
188   dw_tag_t m_tag = llvm::dwarf::DW_TAG_null;
189 
190 private:
191   void GetAttributes(DWARFUnit *cu, DWARFAttributes &attrs, Recurse recurse,
192                      uint32_t curr_depth) const;
193 };
194 
195 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFDEBUGINFOENTRY_H
196