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 "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 BuildFunctionAddressRangeTable(const DWARFUnit *cu,
45                                       DWARFDebugAranges *debug_aranges) const;
46 
47   bool Extract(const lldb_private::DWARFDataExtractor &data,
48                const DWARFUnit *cu, lldb::offset_t *offset_ptr);
49 
50   size_t GetAttributes(const DWARFUnit *cu,
51                        DWARFAttributes &attrs,
52                        uint32_t curr_depth = 0)
53       const; // "curr_depth" for internal use only, don't set this yourself!!!
54 
55   dw_offset_t
56   GetAttributeValue(const DWARFUnit *cu, const dw_attr_t attr,
57                     DWARFFormValue &formValue,
58                     dw_offset_t *end_attr_offset_ptr = nullptr,
59                     bool check_specification_or_abstract_origin = false) const;
60 
61   const char *GetAttributeValueAsString(
62       const DWARFUnit *cu, const dw_attr_t attr, const char *fail_value,
63       bool check_specification_or_abstract_origin = false) const;
64 
65   uint64_t GetAttributeValueAsUnsigned(
66       const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value,
67       bool check_specification_or_abstract_origin = false) const;
68 
69   DWARFDIE GetAttributeValueAsReference(
70       const DWARFUnit *cu, const dw_attr_t attr,
71       bool check_specification_or_abstract_origin = false) const;
72 
73   uint64_t GetAttributeValueAsAddress(
74       const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value,
75       bool check_specification_or_abstract_origin = false) const;
76 
77   dw_addr_t
78   GetAttributeHighPC(const DWARFUnit *cu, dw_addr_t lo_pc, uint64_t fail_value,
79                      bool check_specification_or_abstract_origin = false) const;
80 
81   bool GetAttributeAddressRange(
82       const DWARFUnit *cu, dw_addr_t &lo_pc, dw_addr_t &hi_pc,
83       uint64_t fail_value,
84       bool check_specification_or_abstract_origin = false) const;
85 
86   size_t GetAttributeAddressRanges(
87       DWARFUnit *cu, DWARFRangeList &ranges, bool check_hi_lo_pc,
88       bool check_specification_or_abstract_origin = false) const;
89 
90   const char *GetName(const DWARFUnit *cu) const;
91 
92   const char *GetMangledName(const DWARFUnit *cu,
93                              bool substitute_name_allowed = true) const;
94 
95   const char *GetPubname(const DWARFUnit *cu) const;
96 
97   const char *GetQualifiedName(DWARFUnit *cu, std::string &storage) const;
98 
99   const char *GetQualifiedName(DWARFUnit *cu, const DWARFAttributes &attributes,
100                                std::string &storage) const;
101 
102   void Dump(const DWARFUnit *cu, lldb_private::Stream &s,
103             uint32_t recurse_depth) const;
104 
105   static void
106   DumpAttribute(const DWARFUnit *cu,
107                 const lldb_private::DWARFDataExtractor &data,
108                 lldb::offset_t *offset_ptr, lldb_private::Stream &s,
109                 dw_attr_t attr, DWARFFormValue &form_value);
110 
111   bool GetDIENamesAndRanges(
112       DWARFUnit *cu, const char *&name, const char *&mangled,
113       DWARFRangeList &rangeList, int &decl_file, int &decl_line,
114       int &decl_column, int &call_file, int &call_line, int &call_column,
115       lldb_private::DWARFExpression *frame_base = nullptr) const;
116 
117   const DWARFAbbreviationDeclaration *
118   GetAbbreviationDeclarationPtr(const DWARFUnit *cu) const;
119 
120   lldb::offset_t GetFirstAttributeOffset() const;
121 
122   dw_tag_t Tag() const { return m_tag; }
123 
124   bool IsNULL() const { return m_abbr_idx == 0; }
125 
126   dw_offset_t GetOffset() const { return m_offset; }
127 
128   bool HasChildren() const { return m_has_children; }
129 
130   void SetHasChildren(bool b) { m_has_children = b; }
131 
132   // We know we are kept in a vector of contiguous entries, so we know
133   // our parent will be some index behind "this".
134   DWARFDebugInfoEntry *GetParent() {
135     return m_parent_idx > 0 ? this - m_parent_idx : nullptr;
136   }
137   const DWARFDebugInfoEntry *GetParent() const {
138     return m_parent_idx > 0 ? this - m_parent_idx : nullptr;
139   }
140   // We know we are kept in a vector of contiguous entries, so we know
141   // our sibling will be some index after "this".
142   DWARFDebugInfoEntry *GetSibling() {
143     return m_sibling_idx > 0 ? this + m_sibling_idx : nullptr;
144   }
145   const DWARFDebugInfoEntry *GetSibling() const {
146     return m_sibling_idx > 0 ? this + m_sibling_idx : nullptr;
147   }
148   // We know we are kept in a vector of contiguous entries, so we know
149   // we don't need to store our child pointer, if we have a child it will
150   // be the next entry in the list...
151   DWARFDebugInfoEntry *GetFirstChild() {
152     return HasChildren() ? this + 1 : nullptr;
153   }
154   const DWARFDebugInfoEntry *GetFirstChild() const {
155     return HasChildren() ? this + 1 : nullptr;
156   }
157 
158   DWARFDeclContext GetDWARFDeclContext(DWARFUnit *cu) const;
159 
160   DWARFDIE GetParentDeclContextDIE(DWARFUnit *cu) const;
161   DWARFDIE GetParentDeclContextDIE(DWARFUnit *cu,
162                                    const DWARFAttributes &attributes) const;
163 
164   void SetSiblingIndex(uint32_t idx) { m_sibling_idx = idx; }
165   void SetParentIndex(uint32_t idx) { m_parent_idx = idx; }
166 
167   // This function returns true if the variable scope is either
168   // global or (file-static). It will return false for static variables
169   // that are local to a function, as they have local scope.
170   bool IsGlobalOrStaticScopeVariable() const;
171 
172 protected:
173   static DWARFDeclContext
174   GetDWARFDeclContextStatic(const DWARFDebugInfoEntry *die, DWARFUnit *cu);
175 
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 // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFDEBUGINFOENTRY_H
191