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 // GetName
186 //
187 // Get value of the DW_AT_name attribute and place that value into the supplied
188 // stream object. If the DIE is a NULL object "NULL" is placed into the stream,
189 // and if no DW_AT_name attribute exists for the DIE then nothing is printed.
190 void DWARFDIE::GetName(Stream &s) const {
191   if (!IsValid())
192     return;
193   if (GetDIE()->IsNULL()) {
194     s.PutCString("NULL");
195     return;
196   }
197   const char *name = GetDIE()->GetAttributeValueAsString(GetCU(), DW_AT_name, nullptr, true);
198   if (!name)
199     return;
200   s.PutCString(name);
201 }
202 
203 // AppendTypeName
204 //
205 // Follows the type name definition down through all needed tags to end up with
206 // a fully qualified type name and dump the results to the supplied stream.
207 // This is used to show the name of types given a type identifier.
208 void DWARFDIE::AppendTypeName(Stream &s) const {
209   if (!IsValid())
210     return;
211   if (GetDIE()->IsNULL()) {
212     s.PutCString("NULL");
213     return;
214   }
215   if (const char *name = GetPubname()) {
216     s.PutCString(name);
217     return;
218   }
219   switch (Tag()) {
220   case DW_TAG_array_type:
221     break; // print out a "[]" after printing the full type of the element
222            // below
223   case DW_TAG_base_type:
224     s.PutCString("base ");
225     break;
226   case DW_TAG_class_type:
227     s.PutCString("class ");
228     break;
229   case DW_TAG_const_type:
230     s.PutCString("const ");
231     break;
232   case DW_TAG_enumeration_type:
233     s.PutCString("enum ");
234     break;
235   case DW_TAG_file_type:
236     s.PutCString("file ");
237     break;
238   case DW_TAG_interface_type:
239     s.PutCString("interface ");
240     break;
241   case DW_TAG_packed_type:
242     s.PutCString("packed ");
243     break;
244   case DW_TAG_pointer_type:
245     break; // print out a '*' after printing the full type below
246   case DW_TAG_ptr_to_member_type:
247     break; // print out a '*' after printing the full type below
248   case DW_TAG_reference_type:
249     break; // print out a '&' after printing the full type below
250   case DW_TAG_restrict_type:
251     s.PutCString("restrict ");
252     break;
253   case DW_TAG_set_type:
254     s.PutCString("set ");
255     break;
256   case DW_TAG_shared_type:
257     s.PutCString("shared ");
258     break;
259   case DW_TAG_string_type:
260     s.PutCString("string ");
261     break;
262   case DW_TAG_structure_type:
263     s.PutCString("struct ");
264     break;
265   case DW_TAG_subrange_type:
266     s.PutCString("subrange ");
267     break;
268   case DW_TAG_subroutine_type:
269     s.PutCString("function ");
270     break;
271   case DW_TAG_thrown_type:
272     s.PutCString("thrown ");
273     break;
274   case DW_TAG_union_type:
275     s.PutCString("union ");
276     break;
277   case DW_TAG_unspecified_type:
278     s.PutCString("unspecified ");
279     break;
280   case DW_TAG_volatile_type:
281     s.PutCString("volatile ");
282     break;
283   default:
284     return;
285   }
286 
287   // Follow the DW_AT_type if possible
288   if (DWARFDIE next_die = GetAttributeValueAsReferenceDIE(DW_AT_type))
289     next_die.AppendTypeName(s);
290 
291   switch (Tag()) {
292   case DW_TAG_array_type:
293     s.PutCString("[]");
294     break;
295   case DW_TAG_pointer_type:
296     s.PutChar('*');
297     break;
298   case DW_TAG_ptr_to_member_type:
299     s.PutChar('*');
300     break;
301   case DW_TAG_reference_type:
302     s.PutChar('&');
303     break;
304   default:
305     break;
306   }
307 }
308 
309 lldb_private::Type *DWARFDIE::ResolveType() const {
310   if (IsValid())
311     return GetDWARF()->ResolveType(*this, true);
312   else
313     return nullptr;
314 }
315 
316 lldb_private::Type *DWARFDIE::ResolveTypeUID(const DIERef &die_ref) const {
317   SymbolFileDWARF *dwarf = GetDWARF();
318   if (dwarf)
319     return dwarf->ResolveTypeUID(dwarf->GetDIE(die_ref), true);
320   else
321     return nullptr;
322 }
323 
324 std::vector<DWARFDIE> DWARFDIE::GetDeclContextDIEs() const {
325   if (!IsValid())
326     return {};
327 
328   std::vector<DWARFDIE> result;
329   DWARFDIE parent = GetParentDeclContextDIE();
330   while (parent.IsValid() && parent.GetDIE() != GetDIE()) {
331     result.push_back(std::move(parent));
332     parent = parent.GetParentDeclContextDIE();
333   }
334 
335   return result;
336 }
337 
338 void DWARFDIE::GetDWARFDeclContext(DWARFDeclContext &dwarf_decl_ctx) const {
339   if (IsValid()) {
340     dwarf_decl_ctx.SetLanguage(GetLanguage());
341     m_die->GetDWARFDeclContext(GetCU(), dwarf_decl_ctx);
342   } else {
343     dwarf_decl_ctx.Clear();
344   }
345 }
346 
347 void DWARFDIE::GetDeclContext(std::vector<CompilerContext> &context) const {
348   const dw_tag_t tag = Tag();
349   if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
350     return;
351   DWARFDIE parent = GetParent();
352   if (parent)
353     parent.GetDeclContext(context);
354   switch (tag) {
355   case DW_TAG_module:
356     context.push_back(
357         CompilerContext(CompilerContextKind::Module, ConstString(GetName())));
358     break;
359   case DW_TAG_namespace:
360     context.push_back(CompilerContext(CompilerContextKind::Namespace,
361                                       ConstString(GetName())));
362     break;
363   case DW_TAG_structure_type:
364     context.push_back(CompilerContext(CompilerContextKind::Structure,
365                                       ConstString(GetName())));
366     break;
367   case DW_TAG_union_type:
368     context.push_back(
369         CompilerContext(CompilerContextKind::Union, ConstString(GetName())));
370     break;
371   case DW_TAG_class_type:
372     context.push_back(
373         CompilerContext(CompilerContextKind::Class, ConstString(GetName())));
374     break;
375   case DW_TAG_enumeration_type:
376     context.push_back(CompilerContext(CompilerContextKind::Enumeration,
377                                       ConstString(GetName())));
378     break;
379   case DW_TAG_subprogram:
380     context.push_back(CompilerContext(CompilerContextKind::Function,
381                                       ConstString(GetPubname())));
382     break;
383   case DW_TAG_variable:
384     context.push_back(CompilerContext(CompilerContextKind::Variable,
385                                       ConstString(GetPubname())));
386     break;
387   case DW_TAG_typedef:
388     context.push_back(
389         CompilerContext(CompilerContextKind::Typedef, ConstString(GetName())));
390     break;
391   default:
392     break;
393   }
394 }
395 
396 DWARFDIE
397 DWARFDIE::GetParentDeclContextDIE() const {
398   if (IsValid())
399     return m_die->GetParentDeclContextDIE(m_cu);
400   else
401     return DWARFDIE();
402 }
403 
404 bool DWARFDIE::IsStructUnionOrClass() const {
405   const dw_tag_t tag = Tag();
406   return tag == DW_TAG_class_type || tag == DW_TAG_structure_type ||
407          tag == DW_TAG_union_type;
408 }
409 
410 bool DWARFDIE::IsMethod() const {
411   for (DWARFDIE d : elaborating_dies(*this))
412     if (d.GetParent().IsStructUnionOrClass())
413       return true;
414   return false;
415 }
416 
417 DWARFDIE
418 DWARFDIE::GetContainingDWOModuleDIE() const {
419   if (IsValid()) {
420     DWARFDIE top_module_die;
421     // Now make sure this DIE is scoped in a DW_TAG_module tag and return true
422     // if so
423     for (DWARFDIE parent = GetParent(); parent.IsValid();
424          parent = parent.GetParent()) {
425       const dw_tag_t tag = parent.Tag();
426       if (tag == DW_TAG_module)
427         top_module_die = parent;
428       else if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
429         break;
430     }
431 
432     return top_module_die;
433   }
434   return DWARFDIE();
435 }
436 
437 lldb::ModuleSP DWARFDIE::GetContainingDWOModule() const {
438   if (IsValid()) {
439     DWARFDIE dwo_module_die = GetContainingDWOModuleDIE();
440 
441     if (dwo_module_die) {
442       const char *module_name = dwo_module_die.GetName();
443       if (module_name)
444         return GetDWARF()->GetDWOModule(lldb_private::ConstString(module_name));
445     }
446   }
447   return lldb::ModuleSP();
448 }
449 
450 bool DWARFDIE::GetDIENamesAndRanges(
451     const char *&name, const char *&mangled, DWARFRangeList &ranges,
452     int &decl_file, int &decl_line, int &decl_column, int &call_file,
453     int &call_line, int &call_column,
454     lldb_private::DWARFExpression *frame_base) const {
455   if (IsValid()) {
456     return m_die->GetDIENamesAndRanges(
457         GetCU(), name, mangled, ranges, decl_file, decl_line, decl_column,
458         call_file, call_line, call_column, frame_base);
459   } else
460     return false;
461 }
462 
463 CompilerDecl DWARFDIE::GetDecl() const {
464   DWARFASTParser *dwarf_ast = GetDWARFParser();
465   if (dwarf_ast)
466     return dwarf_ast->GetDeclForUIDFromDWARF(*this);
467   else
468     return CompilerDecl();
469 }
470 
471 CompilerDeclContext DWARFDIE::GetDeclContext() const {
472   DWARFASTParser *dwarf_ast = GetDWARFParser();
473   if (dwarf_ast)
474     return dwarf_ast->GetDeclContextForUIDFromDWARF(*this);
475   else
476     return CompilerDeclContext();
477 }
478 
479 CompilerDeclContext DWARFDIE::GetContainingDeclContext() const {
480   DWARFASTParser *dwarf_ast = GetDWARFParser();
481   if (dwarf_ast)
482     return dwarf_ast->GetDeclContextContainingUIDFromDWARF(*this);
483   else
484     return CompilerDeclContext();
485 }
486