1 //===-- DWARFDIE.cpp --------------------------------------------*- 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 #include "DWARFDIE.h"
10 
11 #include "DWARFASTParser.h"
12 #include "DWARFDebugInfo.h"
13 #include "DWARFDebugInfoEntry.h"
14 #include "DWARFDeclContext.h"
15 #include "DWARFUnit.h"
16 
17 using namespace lldb_private;
18 
19 namespace {
20 
21 /// Iterate through all DIEs elaborating (i.e. reachable by a chain of
22 /// DW_AT_specification and DW_AT_abstract_origin attributes) a given DIE. For
23 /// convenience, the starting die is included in the sequence as the first
24 /// item.
25 class ElaboratingDIEIterator
26     : public std::iterator<std::input_iterator_tag, DWARFDIE> {
27 
28   // The operating invariant is: top of m_worklist contains the "current" item
29   // and the rest of the list are items yet to be visited. An empty worklist
30   // means we've reached the end.
31   // Infinite recursion is prevented by maintaining a list of seen DIEs.
32   // Container sizes are optimized for the case of following DW_AT_specification
33   // and DW_AT_abstract_origin just once.
34   llvm::SmallVector<DWARFDIE, 2> m_worklist;
35   llvm::SmallSet<lldb::user_id_t, 3> m_seen;
36 
37   void Next() {
38     assert(!m_worklist.empty() && "Incrementing end iterator?");
39 
40     // Pop the current item from the list.
41     DWARFDIE die = m_worklist.back();
42     m_worklist.pop_back();
43 
44     // And add back any items that elaborate it.
45     for (dw_attr_t attr : {DW_AT_specification, DW_AT_abstract_origin}) {
46       if (DWARFDIE d = die.GetReferencedDIE(attr))
47         if (m_seen.insert(die.GetID()).second)
48           m_worklist.push_back(d);
49     }
50   }
51 
52 public:
53   /// An iterator starting at die d.
54   explicit ElaboratingDIEIterator(DWARFDIE d) : m_worklist(1, d) {}
55 
56   /// End marker
57   ElaboratingDIEIterator() {}
58 
59   const DWARFDIE &operator*() const { return m_worklist.back(); }
60   ElaboratingDIEIterator &operator++() {
61     Next();
62     return *this;
63   }
64   ElaboratingDIEIterator operator++(int) {
65     ElaboratingDIEIterator I = *this;
66     Next();
67     return I;
68   }
69 
70   friend bool operator==(const ElaboratingDIEIterator &a,
71                          const ElaboratingDIEIterator &b) {
72     if (a.m_worklist.empty() || b.m_worklist.empty())
73       return a.m_worklist.empty() == b.m_worklist.empty();
74     return a.m_worklist.back() == b.m_worklist.back();
75   }
76   friend bool operator!=(const ElaboratingDIEIterator &a,
77                          const ElaboratingDIEIterator &b) {
78     return !(a == b);
79   }
80 };
81 
82 llvm::iterator_range<ElaboratingDIEIterator>
83 elaborating_dies(const DWARFDIE &die) {
84   return llvm::make_range(ElaboratingDIEIterator(die),
85                           ElaboratingDIEIterator());
86 }
87 } // namespace
88 
89 DWARFDIE
90 DWARFDIE::GetParent() const {
91   if (IsValid())
92     return DWARFDIE(m_cu, m_die->GetParent());
93   else
94     return DWARFDIE();
95 }
96 
97 DWARFDIE
98 DWARFDIE::GetFirstChild() const {
99   if (IsValid())
100     return DWARFDIE(m_cu, m_die->GetFirstChild());
101   else
102     return DWARFDIE();
103 }
104 
105 DWARFDIE
106 DWARFDIE::GetSibling() const {
107   if (IsValid())
108     return DWARFDIE(m_cu, m_die->GetSibling());
109   else
110     return DWARFDIE();
111 }
112 
113 DWARFDIE
114 DWARFDIE::GetReferencedDIE(const dw_attr_t attr) const {
115   if (IsValid())
116     return m_die->GetAttributeValueAsReference(GetCU(), attr);
117   else
118     return {};
119 }
120 
121 DWARFDIE
122 DWARFDIE::GetDIE(dw_offset_t die_offset) const {
123   if (IsValid())
124     return m_cu->GetDIE(die_offset);
125   else
126     return DWARFDIE();
127 }
128 
129 DWARFDIE
130 DWARFDIE::GetAttributeValueAsReferenceDIE(const dw_attr_t attr) const {
131   if (IsValid()) {
132     DWARFUnit *cu = GetCU();
133     const bool check_specification_or_abstract_origin = true;
134     DWARFFormValue form_value;
135     if (m_die->GetAttributeValue(cu, attr, form_value, nullptr,
136                                  check_specification_or_abstract_origin))
137       return form_value.Reference();
138   }
139   return DWARFDIE();
140 }
141 
142 DWARFDIE
143 DWARFDIE::LookupDeepestBlock(lldb::addr_t file_addr) const {
144   if (IsValid()) {
145     SymbolFileDWARF *dwarf = GetDWARF();
146     DWARFUnit *cu = GetCU();
147     DWARFDebugInfoEntry *function_die = nullptr;
148     DWARFDebugInfoEntry *block_die = nullptr;
149     if (m_die->LookupAddress(file_addr, cu, &function_die, &block_die)) {
150       if (block_die && block_die != function_die) {
151         if (cu->ContainsDIEOffset(block_die->GetOffset()))
152           return DWARFDIE(cu, block_die);
153         else
154           return DWARFDIE(dwarf->DebugInfo()->GetUnit(
155                               DIERef(cu->GetDebugSection(), cu->GetOffset(),
156                                      block_die->GetOffset())),
157                           block_die);
158       }
159     }
160   }
161   return DWARFDIE();
162 }
163 
164 const char *DWARFDIE::GetMangledName() const {
165   if (IsValid())
166     return m_die->GetMangledName(m_cu);
167   else
168     return nullptr;
169 }
170 
171 const char *DWARFDIE::GetPubname() const {
172   if (IsValid())
173     return m_die->GetPubname(m_cu);
174   else
175     return nullptr;
176 }
177 
178 const char *DWARFDIE::GetQualifiedName(std::string &storage) const {
179   if (IsValid())
180     return m_die->GetQualifiedName(m_cu, storage);
181   else
182     return nullptr;
183 }
184 
185 lldb_private::Type *DWARFDIE::ResolveType() const {
186   if (IsValid())
187     return GetDWARF()->ResolveType(*this, true);
188   else
189     return nullptr;
190 }
191 
192 lldb_private::Type *DWARFDIE::ResolveTypeUID(const DIERef &die_ref) const {
193   SymbolFileDWARF *dwarf = GetDWARF();
194   if (dwarf)
195     return dwarf->ResolveTypeUID(dwarf->GetDIE(die_ref), true);
196   else
197     return nullptr;
198 }
199 
200 std::vector<DWARFDIE> DWARFDIE::GetDeclContextDIEs() const {
201   if (!IsValid())
202     return {};
203 
204   std::vector<DWARFDIE> result;
205   DWARFDIE parent = GetParentDeclContextDIE();
206   while (parent.IsValid() && parent.GetDIE() != GetDIE()) {
207     result.push_back(std::move(parent));
208     parent = parent.GetParentDeclContextDIE();
209   }
210 
211   return result;
212 }
213 
214 void DWARFDIE::GetDWARFDeclContext(DWARFDeclContext &dwarf_decl_ctx) const {
215   if (IsValid()) {
216     dwarf_decl_ctx.SetLanguage(GetLanguage());
217     m_die->GetDWARFDeclContext(GetCU(), dwarf_decl_ctx);
218   } else {
219     dwarf_decl_ctx.Clear();
220   }
221 }
222 
223 void DWARFDIE::GetDeclContext(std::vector<CompilerContext> &context) const {
224   const dw_tag_t tag = Tag();
225   if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
226     return;
227   DWARFDIE parent = GetParent();
228   if (parent)
229     parent.GetDeclContext(context);
230   switch (tag) {
231   case DW_TAG_module:
232     context.push_back(
233         CompilerContext(CompilerContextKind::Module, ConstString(GetName())));
234     break;
235   case DW_TAG_namespace:
236     context.push_back(CompilerContext(CompilerContextKind::Namespace,
237                                       ConstString(GetName())));
238     break;
239   case DW_TAG_structure_type:
240     context.push_back(CompilerContext(CompilerContextKind::Structure,
241                                       ConstString(GetName())));
242     break;
243   case DW_TAG_union_type:
244     context.push_back(
245         CompilerContext(CompilerContextKind::Union, ConstString(GetName())));
246     break;
247   case DW_TAG_class_type:
248     context.push_back(
249         CompilerContext(CompilerContextKind::Class, ConstString(GetName())));
250     break;
251   case DW_TAG_enumeration_type:
252     context.push_back(CompilerContext(CompilerContextKind::Enumeration,
253                                       ConstString(GetName())));
254     break;
255   case DW_TAG_subprogram:
256     context.push_back(CompilerContext(CompilerContextKind::Function,
257                                       ConstString(GetPubname())));
258     break;
259   case DW_TAG_variable:
260     context.push_back(CompilerContext(CompilerContextKind::Variable,
261                                       ConstString(GetPubname())));
262     break;
263   case DW_TAG_typedef:
264     context.push_back(
265         CompilerContext(CompilerContextKind::Typedef, ConstString(GetName())));
266     break;
267   default:
268     break;
269   }
270 }
271 
272 DWARFDIE
273 DWARFDIE::GetParentDeclContextDIE() const {
274   if (IsValid())
275     return m_die->GetParentDeclContextDIE(m_cu);
276   else
277     return DWARFDIE();
278 }
279 
280 bool DWARFDIE::IsStructUnionOrClass() const {
281   const dw_tag_t tag = Tag();
282   return tag == DW_TAG_class_type || tag == DW_TAG_structure_type ||
283          tag == DW_TAG_union_type;
284 }
285 
286 bool DWARFDIE::IsMethod() const {
287   for (DWARFDIE d : elaborating_dies(*this))
288     if (d.GetParent().IsStructUnionOrClass())
289       return true;
290   return false;
291 }
292 
293 DWARFDIE
294 DWARFDIE::GetContainingDWOModuleDIE() const {
295   if (IsValid()) {
296     DWARFDIE top_module_die;
297     // Now make sure this DIE is scoped in a DW_TAG_module tag and return true
298     // if so
299     for (DWARFDIE parent = GetParent(); parent.IsValid();
300          parent = parent.GetParent()) {
301       const dw_tag_t tag = parent.Tag();
302       if (tag == DW_TAG_module)
303         top_module_die = parent;
304       else if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
305         break;
306     }
307 
308     return top_module_die;
309   }
310   return DWARFDIE();
311 }
312 
313 lldb::ModuleSP DWARFDIE::GetContainingDWOModule() const {
314   if (IsValid()) {
315     DWARFDIE dwo_module_die = GetContainingDWOModuleDIE();
316 
317     if (dwo_module_die) {
318       const char *module_name = dwo_module_die.GetName();
319       if (module_name)
320         return GetDWARF()->GetDWOModule(lldb_private::ConstString(module_name));
321     }
322   }
323   return lldb::ModuleSP();
324 }
325 
326 bool DWARFDIE::GetDIENamesAndRanges(
327     const char *&name, const char *&mangled, DWARFRangeList &ranges,
328     int &decl_file, int &decl_line, int &decl_column, int &call_file,
329     int &call_line, int &call_column,
330     lldb_private::DWARFExpression *frame_base) const {
331   if (IsValid()) {
332     return m_die->GetDIENamesAndRanges(
333         GetCU(), name, mangled, ranges, decl_file, decl_line, decl_column,
334         call_file, call_line, call_column, frame_base);
335   } else
336     return false;
337 }
338 
339 CompilerDecl DWARFDIE::GetDecl() const {
340   DWARFASTParser *dwarf_ast = GetDWARFParser();
341   if (dwarf_ast)
342     return dwarf_ast->GetDeclForUIDFromDWARF(*this);
343   else
344     return CompilerDecl();
345 }
346 
347 CompilerDeclContext DWARFDIE::GetDeclContext() const {
348   DWARFASTParser *dwarf_ast = GetDWARFParser();
349   if (dwarf_ast)
350     return dwarf_ast->GetDeclContextForUIDFromDWARF(*this);
351   else
352     return CompilerDeclContext();
353 }
354 
355 CompilerDeclContext DWARFDIE::GetContainingDeclContext() const {
356   DWARFASTParser *dwarf_ast = GetDWARFParser();
357   if (dwarf_ast)
358     return dwarf_ast->GetDeclContextContainingUIDFromDWARF(*this);
359   else
360     return CompilerDeclContext();
361 }
362