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 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   size_t GetAttributes(const DWARFUnit *cu,
54                        DWARFAttributes &attrs,
55                        uint32_t curr_depth = 0)
56       const; // "curr_depth" for internal use only, don't set this yourself!!!
57 
58   dw_offset_t
59   GetAttributeValue(const DWARFUnit *cu, const dw_attr_t attr,
60                     DWARFFormValue &formValue,
61                     dw_offset_t *end_attr_offset_ptr = nullptr,
62                     bool check_specification_or_abstract_origin = false) const;
63 
64   const char *GetAttributeValueAsString(
65       const DWARFUnit *cu, const dw_attr_t attr, const char *fail_value,
66       bool check_specification_or_abstract_origin = false) const;
67 
68   uint64_t GetAttributeValueAsUnsigned(
69       const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value,
70       bool check_specification_or_abstract_origin = false) const;
71 
72   DWARFDIE GetAttributeValueAsReference(
73       const DWARFUnit *cu, const dw_attr_t attr,
74       bool check_specification_or_abstract_origin = false) const;
75 
76   uint64_t GetAttributeValueAsAddress(
77       const DWARFUnit *cu, const dw_attr_t attr, uint64_t fail_value,
78       bool check_specification_or_abstract_origin = false) const;
79 
80   dw_addr_t
81   GetAttributeHighPC(const DWARFUnit *cu, dw_addr_t lo_pc, uint64_t fail_value,
82                      bool check_specification_or_abstract_origin = false) const;
83 
84   bool GetAttributeAddressRange(
85       const DWARFUnit *cu, dw_addr_t &lo_pc, dw_addr_t &hi_pc,
86       uint64_t fail_value,
87       bool check_specification_or_abstract_origin = false) const;
88 
89   size_t GetAttributeAddressRanges(
90       DWARFUnit *cu, DWARFRangeList &ranges, bool check_hi_lo_pc,
91       bool check_specification_or_abstract_origin = false) const;
92 
93   const char *GetName(const DWARFUnit *cu) const;
94 
95   const char *GetMangledName(const DWARFUnit *cu,
96                              bool substitute_name_allowed = true) const;
97 
98   const char *GetPubname(const DWARFUnit *cu) const;
99 
100   const char *GetQualifiedName(DWARFUnit *cu, std::string &storage) const;
101 
102   const char *GetQualifiedName(DWARFUnit *cu, const DWARFAttributes &attributes,
103                                std::string &storage) const;
104 
105   void Dump(const DWARFUnit *cu, lldb_private::Stream &s,
106             uint32_t recurse_depth) const;
107 
108   static void
109   DumpAttribute(const DWARFUnit *cu,
110                 const lldb_private::DWARFDataExtractor &data,
111                 lldb::offset_t *offset_ptr, lldb_private::Stream &s,
112                 dw_attr_t attr, DWARFFormValue &form_value);
113 
114   bool GetDIENamesAndRanges(
115       DWARFUnit *cu, const char *&name, const char *&mangled,
116       DWARFRangeList &rangeList, int &decl_file, int &decl_line,
117       int &decl_column, int &call_file, int &call_line, int &call_column,
118       lldb_private::DWARFExpression *frame_base = nullptr) const;
119 
120   const DWARFAbbreviationDeclaration *
121   GetAbbreviationDeclarationPtr(const DWARFUnit *cu) const;
122 
123   lldb::offset_t GetFirstAttributeOffset() const;
124 
125   dw_tag_t Tag() const { return m_tag; }
126 
127   bool IsNULL() const { return m_abbr_idx == 0; }
128 
129   dw_offset_t GetOffset() const { return m_offset; }
130 
131   bool HasChildren() const { return m_has_children; }
132 
133   void SetHasChildren(bool b) { m_has_children = b; }
134 
135   // We know we are kept in a vector of contiguous entries, so we know
136   // our parent will be some index behind "this".
137   DWARFDebugInfoEntry *GetParent() {
138     return m_parent_idx > 0 ? this - m_parent_idx : nullptr;
139   }
140   const DWARFDebugInfoEntry *GetParent() const {
141     return m_parent_idx > 0 ? this - m_parent_idx : nullptr;
142   }
143   // We know we are kept in a vector of contiguous entries, so we know
144   // our sibling will be some index after "this".
145   DWARFDebugInfoEntry *GetSibling() {
146     return m_sibling_idx > 0 ? this + m_sibling_idx : nullptr;
147   }
148   const DWARFDebugInfoEntry *GetSibling() const {
149     return m_sibling_idx > 0 ? this + m_sibling_idx : nullptr;
150   }
151   // We know we are kept in a vector of contiguous entries, so we know
152   // we don't need to store our child pointer, if we have a child it will
153   // be the next entry in the list...
154   DWARFDebugInfoEntry *GetFirstChild() {
155     return HasChildren() ? this + 1 : nullptr;
156   }
157   const DWARFDebugInfoEntry *GetFirstChild() const {
158     return HasChildren() ? this + 1 : nullptr;
159   }
160 
161   DWARFDeclContext GetDWARFDeclContext(DWARFUnit *cu) const;
162 
163   DWARFDIE GetParentDeclContextDIE(DWARFUnit *cu) const;
164   DWARFDIE GetParentDeclContextDIE(DWARFUnit *cu,
165                                    const DWARFAttributes &attributes) const;
166 
167   void SetSiblingIndex(uint32_t idx) { m_sibling_idx = idx; }
168   void SetParentIndex(uint32_t idx) { m_parent_idx = idx; }
169 
170 protected:
171   static DWARFDeclContext
172   GetDWARFDeclContextStatic(const DWARFDebugInfoEntry *die, DWARFUnit *cu);
173 
174   dw_offset_t m_offset; // Offset within the .debug_info/.debug_types
175   uint32_t m_parent_idx; // How many to subtract from "this" to get the parent.
176                          // If zero this die has no parent
177   uint32_t m_sibling_idx : 31, // How many to add to "this" to get the sibling.
178       // If it is zero, then the DIE doesn't have children, or the
179       // DWARF claimed it had children but the DIE only contained
180       // a single NULL terminating child.
181       m_has_children : 1;
182   uint16_t m_abbr_idx;
183   /// A copy of the DW_TAG value so we don't have to go through the compile
184   /// unit abbrev table
185   dw_tag_t m_tag = llvm::dwarf::DW_TAG_null;
186 };
187 
188 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFDEBUGINFOENTRY_H
189