LLDB  mainline
DWARFDIE.cpp
Go to the documentation of this file.
1 //===-- DWARFDIE.cpp ------------------------------------------------------===//
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 using namespace lldb_private::dwarf;
19 
20 namespace {
21 
22 /// Iterate through all DIEs elaborating (i.e. reachable by a chain of
23 /// DW_AT_specification and DW_AT_abstract_origin attributes) a given DIE. For
24 /// convenience, the starting die is included in the sequence as the first
25 /// item.
26 class ElaboratingDIEIterator
27  : public std::iterator<std::input_iterator_tag, DWARFDIE> {
28 
29  // The operating invariant is: top of m_worklist contains the "current" item
30  // and the rest of the list are items yet to be visited. An empty worklist
31  // means we've reached the end.
32  // Infinite recursion is prevented by maintaining a list of seen DIEs.
33  // Container sizes are optimized for the case of following DW_AT_specification
34  // and DW_AT_abstract_origin just once.
35  llvm::SmallVector<DWARFDIE, 2> m_worklist;
36  llvm::SmallSet<DWARFDebugInfoEntry *, 3> m_seen;
37 
38  void Next() {
39  assert(!m_worklist.empty() && "Incrementing end iterator?");
40 
41  // Pop the current item from the list.
42  DWARFDIE die = m_worklist.back();
43  m_worklist.pop_back();
44 
45  // And add back any items that elaborate it.
46  for (dw_attr_t attr : {DW_AT_specification, DW_AT_abstract_origin}) {
47  if (DWARFDIE d = die.GetReferencedDIE(attr))
48  if (m_seen.insert(die.GetDIE()).second)
49  m_worklist.push_back(d);
50  }
51  }
52 
53 public:
54  /// An iterator starting at die d.
55  explicit ElaboratingDIEIterator(DWARFDIE d) : m_worklist(1, d) {}
56 
57  /// End marker
58  ElaboratingDIEIterator() = default;
59 
60  const DWARFDIE &operator*() const { return m_worklist.back(); }
61  ElaboratingDIEIterator &operator++() {
62  Next();
63  return *this;
64  }
65  ElaboratingDIEIterator operator++(int) {
66  ElaboratingDIEIterator I = *this;
67  Next();
68  return I;
69  }
70 
71  friend bool operator==(const ElaboratingDIEIterator &a,
72  const ElaboratingDIEIterator &b) {
73  if (a.m_worklist.empty() || b.m_worklist.empty())
74  return a.m_worklist.empty() == b.m_worklist.empty();
75  return a.m_worklist.back() == b.m_worklist.back();
76  }
77  friend bool operator!=(const ElaboratingDIEIterator &a,
78  const ElaboratingDIEIterator &b) {
79  return !(a == b);
80  }
81 };
82 
83 llvm::iterator_range<ElaboratingDIEIterator>
84 elaborating_dies(const DWARFDIE &die) {
85  return llvm::make_range(ElaboratingDIEIterator(die),
86  ElaboratingDIEIterator());
87 }
88 } // namespace
89 
92  if (IsValid())
93  return DWARFDIE(m_cu, m_die->GetParent());
94  else
95  return DWARFDIE();
96 }
97 
100  if (IsValid())
101  return DWARFDIE(m_cu, m_die->GetFirstChild());
102  else
103  return DWARFDIE();
104 }
105 
106 DWARFDIE
108  if (IsValid())
109  return DWARFDIE(m_cu, m_die->GetSibling());
110  else
111  return DWARFDIE();
112 }
113 
114 DWARFDIE
116  if (IsValid())
117  return m_die->GetAttributeValueAsReference(GetCU(), attr);
118  else
119  return {};
120 }
121 
122 DWARFDIE
123 DWARFDIE::GetDIE(dw_offset_t die_offset) const {
124  if (IsValid())
125  return m_cu->GetDIE(die_offset);
126  else
127  return DWARFDIE();
128 }
129 
130 DWARFDIE
132  if (IsValid()) {
133  DWARFUnit *cu = GetCU();
134  const bool check_specification_or_abstract_origin = true;
135  DWARFFormValue form_value;
136  if (m_die->GetAttributeValue(cu, attr, form_value, nullptr,
137  check_specification_or_abstract_origin))
138  return form_value.Reference();
139  }
140  return DWARFDIE();
141 }
142 
143 DWARFDIE
145  if (!IsValid())
146  return DWARFDIE();
147 
148  DWARFDIE result;
149  bool check_children = false;
150  bool match_addr_range = false;
151  switch (Tag()) {
152  case DW_TAG_class_type:
153  case DW_TAG_namespace:
154  case DW_TAG_structure_type:
155  case DW_TAG_common_block:
156  check_children = true;
157  break;
158  case DW_TAG_compile_unit:
159  case DW_TAG_module:
160  case DW_TAG_catch_block:
161  case DW_TAG_subprogram:
162  case DW_TAG_try_block:
163  case DW_TAG_partial_unit:
164  match_addr_range = true;
165  break;
166  case DW_TAG_lexical_block:
167  case DW_TAG_inlined_subroutine:
168  check_children = true;
169  match_addr_range = true;
170  break;
171  default:
172  break;
173  }
174 
175  if (match_addr_range) {
176  DWARFRangeList ranges;
177  if (m_die->GetAttributeAddressRanges(m_cu, ranges,
178  /*check_hi_lo_pc=*/true) &&
179  ranges.FindEntryThatContains(address)) {
180  check_children = true;
181  switch (Tag()) {
182  default:
183  break;
184 
185  case DW_TAG_inlined_subroutine: // Inlined Function
186  case DW_TAG_lexical_block: // Block { } in code
187  result = *this;
188  break;
189  }
190  } else {
191  check_children = false;
192  }
193  }
194 
195  if (check_children) {
196  for (DWARFDIE child : children()) {
197  if (DWARFDIE child_result = child.LookupDeepestBlock(address))
198  return child_result;
199  }
200  }
201  return result;
202 }
203 
204 const char *DWARFDIE::GetMangledName() const {
205  if (IsValid())
206  return m_die->GetMangledName(m_cu);
207  else
208  return nullptr;
209 }
210 
211 const char *DWARFDIE::GetPubname() const {
212  if (IsValid())
213  return m_die->GetPubname(m_cu);
214  else
215  return nullptr;
216 }
217 
218 const char *DWARFDIE::GetQualifiedName(std::string &storage) const {
219  if (IsValid())
220  return m_die->GetQualifiedName(m_cu, storage);
221  else
222  return nullptr;
223 }
224 
225 // GetName
226 //
227 // Get value of the DW_AT_name attribute and place that value into the supplied
228 // stream object. If the DIE is a NULL object "NULL" is placed into the stream,
229 // and if no DW_AT_name attribute exists for the DIE then nothing is printed.
230 void DWARFDIE::GetName(Stream &s) const {
231  if (!IsValid())
232  return;
233  if (GetDIE()->IsNULL()) {
234  s.PutCString("NULL");
235  return;
236  }
237  const char *name = GetDIE()->GetAttributeValueAsString(GetCU(), DW_AT_name, nullptr, true);
238  if (!name)
239  return;
240  s.PutCString(name);
241 }
242 
243 // AppendTypeName
244 //
245 // Follows the type name definition down through all needed tags to end up with
246 // a fully qualified type name and dump the results to the supplied stream.
247 // This is used to show the name of types given a type identifier.
249  if (!IsValid())
250  return;
251  if (GetDIE()->IsNULL()) {
252  s.PutCString("NULL");
253  return;
254  }
255  if (const char *name = GetPubname()) {
256  s.PutCString(name);
257  return;
258  }
259  switch (Tag()) {
260  case DW_TAG_array_type:
261  break; // print out a "[]" after printing the full type of the element
262  // below
263  case DW_TAG_base_type:
264  s.PutCString("base ");
265  break;
266  case DW_TAG_class_type:
267  s.PutCString("class ");
268  break;
269  case DW_TAG_const_type:
270  s.PutCString("const ");
271  break;
272  case DW_TAG_enumeration_type:
273  s.PutCString("enum ");
274  break;
275  case DW_TAG_file_type:
276  s.PutCString("file ");
277  break;
278  case DW_TAG_interface_type:
279  s.PutCString("interface ");
280  break;
281  case DW_TAG_packed_type:
282  s.PutCString("packed ");
283  break;
284  case DW_TAG_pointer_type:
285  break; // print out a '*' after printing the full type below
286  case DW_TAG_ptr_to_member_type:
287  break; // print out a '*' after printing the full type below
288  case DW_TAG_reference_type:
289  break; // print out a '&' after printing the full type below
290  case DW_TAG_restrict_type:
291  s.PutCString("restrict ");
292  break;
293  case DW_TAG_set_type:
294  s.PutCString("set ");
295  break;
296  case DW_TAG_shared_type:
297  s.PutCString("shared ");
298  break;
299  case DW_TAG_string_type:
300  s.PutCString("string ");
301  break;
302  case DW_TAG_structure_type:
303  s.PutCString("struct ");
304  break;
305  case DW_TAG_subrange_type:
306  s.PutCString("subrange ");
307  break;
308  case DW_TAG_subroutine_type:
309  s.PutCString("function ");
310  break;
311  case DW_TAG_thrown_type:
312  s.PutCString("thrown ");
313  break;
314  case DW_TAG_union_type:
315  s.PutCString("union ");
316  break;
317  case DW_TAG_unspecified_type:
318  s.PutCString("unspecified ");
319  break;
320  case DW_TAG_volatile_type:
321  s.PutCString("volatile ");
322  break;
323  case DW_TAG_LLVM_ptrauth_type: {
324  unsigned key = GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_key, 0);
325  bool isAddressDiscriminated = GetAttributeValueAsUnsigned(
326  DW_AT_LLVM_ptrauth_address_discriminated, 0);
327  unsigned extraDiscriminator =
328  GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_extra_discriminator, 0);
329  bool isaPointer =
330  GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_isa_pointer, 0);
331  s.Printf("__ptrauth(%d, %d, 0x0%x, %d)", key, isAddressDiscriminated,
332  extraDiscriminator, isaPointer);
333  break;
334  }
335  default:
336  return;
337  }
338 
339  // Follow the DW_AT_type if possible
340  if (DWARFDIE next_die = GetAttributeValueAsReferenceDIE(DW_AT_type))
341  next_die.AppendTypeName(s);
342 
343  switch (Tag()) {
344  case DW_TAG_array_type:
345  s.PutCString("[]");
346  break;
347  case DW_TAG_pointer_type:
348  s.PutChar('*');
349  break;
350  case DW_TAG_ptr_to_member_type:
351  s.PutChar('*');
352  break;
353  case DW_TAG_reference_type:
354  s.PutChar('&');
355  break;
356  default:
357  break;
358  }
359 }
360 
362  if (IsValid())
363  return GetDWARF()->ResolveType(*this, true);
364  else
365  return nullptr;
366 }
367 
369  if (SymbolFileDWARF *dwarf = GetDWARF())
370  return dwarf->ResolveTypeUID(die, true);
371  return nullptr;
372 }
373 
374 std::vector<DWARFDIE> DWARFDIE::GetDeclContextDIEs() const {
375  if (!IsValid())
376  return {};
377 
378  std::vector<DWARFDIE> result;
379  DWARFDIE parent = GetParentDeclContextDIE();
380  while (parent.IsValid() && parent.GetDIE() != GetDIE()) {
381  result.push_back(std::move(parent));
382  parent = parent.GetParentDeclContextDIE();
383  }
384 
385  return result;
386 }
387 
390  const dw_tag_t tag = Tag();
391  if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
392  return;
393  DWARFDIE parent = GetParent();
394  if (parent)
395  parent.GetDeclContext(context);
396  switch (tag) {
397  case DW_TAG_module:
398  context.push_back({CompilerContextKind::Module, ConstString(GetName())});
399  break;
400  case DW_TAG_namespace:
401  context.push_back({CompilerContextKind::Namespace, ConstString(GetName())});
402  break;
403  case DW_TAG_structure_type:
404  context.push_back({CompilerContextKind::Struct, ConstString(GetName())});
405  break;
406  case DW_TAG_union_type:
407  context.push_back({CompilerContextKind::Union, ConstString(GetName())});
408  break;
409  case DW_TAG_class_type:
410  context.push_back({CompilerContextKind::Class, ConstString(GetName())});
411  break;
412  case DW_TAG_enumeration_type:
413  context.push_back({CompilerContextKind::Enum, ConstString(GetName())});
414  break;
415  case DW_TAG_subprogram:
416  context.push_back(
417  {CompilerContextKind::Function, ConstString(GetPubname())});
418  break;
419  case DW_TAG_variable:
420  context.push_back(
421  {CompilerContextKind::Variable, ConstString(GetPubname())});
422  break;
423  case DW_TAG_typedef:
424  context.push_back({CompilerContextKind::Typedef, ConstString(GetName())});
425  break;
426  default:
427  break;
428  }
429 }
430 
431 DWARFDIE
433  if (IsValid())
434  return m_die->GetParentDeclContextDIE(m_cu);
435  else
436  return DWARFDIE();
437 }
438 
440  const dw_tag_t tag = Tag();
441  return tag == DW_TAG_class_type || tag == DW_TAG_structure_type ||
442  tag == DW_TAG_union_type;
443 }
444 
445 bool DWARFDIE::IsMethod() const {
446  for (DWARFDIE d : elaborating_dies(*this))
447  if (d.GetParent().IsStructUnionOrClass())
448  return true;
449  return false;
450 }
451 
453  const char *&name, const char *&mangled, DWARFRangeList &ranges,
454  int &decl_file, int &decl_line, int &decl_column, int &call_file,
455  int &call_line, int &call_column,
456  lldb_private::DWARFExpressionList *frame_base) const {
457  if (IsValid()) {
458  return m_die->GetDIENamesAndRanges(
459  GetCU(), name, mangled, ranges, decl_file, decl_line, decl_column,
460  call_file, call_line, call_column, frame_base);
461  } else
462  return false;
463 }
464 
465 llvm::iterator_range<DWARFDIE::child_iterator> DWARFDIE::children() const {
466  return llvm::make_range(child_iterator(*this), child_iterator());
467 }
DWARFFormValue::Reference
DWARFDIE Reference() const
Definition: DWARFFormValue.cpp:502
DWARFDIE::GetParent
DWARFDIE GetParent() const
Definition: DWARFDIE.cpp:91
DWARFDIE.h
dw_tag_t
llvm::dwarf::Tag dw_tag_t
Definition: dwarf.h:28
DWARFDIE::GetDeclContextDIEs
std::vector< DWARFDIE > GetDeclContextDIEs() const
Definition: DWARFDIE.cpp:374
SymbolFileDWARF
Definition: SymbolFileDWARF.h:60
lldb_private::CompilerContextKind::Module
@ Module
DWARFDIE::GetDeclContext
void GetDeclContext(llvm::SmallVectorImpl< lldb_private::CompilerContext > &context) const
Return this DIE's decl context as it is needed to look up types in Clang's -gmodules debug info forma...
Definition: DWARFDIE.cpp:388
DWARFDIE::AppendTypeName
void AppendTypeName(lldb_private::Stream &s) const
Definition: DWARFDIE.cpp:248
lldb_private::Stream
Definition: Stream.h:28
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
DWARFDIE::ResolveType
lldb_private::Type * ResolveType() const
Definition: DWARFDIE.cpp:361
lldb_private::CompilerContextKind::Enum
@ Enum
DWARFDIE::ResolveTypeUID
lldb_private::Type * ResolveTypeUID(const DWARFDIE &die) const
Definition: DWARFDIE.cpp:368
DWARFDIE::GetAttributeValueAsReferenceDIE
DWARFDIE GetAttributeValueAsReferenceDIE(const dw_attr_t attr) const
Definition: DWARFDIE.cpp:131
DWARFDeclContext.h
DWARFDIE
Definition: DWARFDIE.h:16
lldb_private::operator==
bool operator==(const Address &lhs, const Address &rhs)
Definition: Address.cpp:1016
DWARFDIE::GetPubname
const char * GetPubname() const
Definition: DWARFDIE.cpp:211
DWARFUnit
Definition: DWARFUnit.h:83
lldb_private::CompilerContextKind::Typedef
@ Typedef
DWARFDIE::GetQualifiedName
const char * GetQualifiedName(std::string &storage) const
Definition: DWARFDIE.cpp:218
dwarf
Definition: ABISysV_arc.cpp:61
DWARFBaseDIE::GetDIE
DWARFDebugInfoEntry * GetDIE() const
Definition: DWARFBaseDIE.h:56
GetName
static llvm::StringRef GetName(XcodeSDK::Type type)
Definition: XcodeSDK.cpp:21
DWARFASTParser.h
DWARFDIE::IsStructUnionOrClass
bool IsStructUnionOrClass() const
Definition: DWARFDIE.cpp:439
DWARFDIE::GetMangledName
const char * GetMangledName() const
Definition: DWARFDIE.cpp:204
lldb_private::CompilerContextKind::Union
@ Union
DWARFDIE::child_iterator
Definition: DWARFDIE.h:99
lldb_private::ConstString
Definition: ConstString.h:39
DWARFFormValue
Definition: DWARFFormValue.h:20
DWARFDIE::GetReferencedDIE
DWARFDIE GetReferencedDIE(const dw_attr_t attr) const
Definition: DWARFDIE.cpp:115
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
DWARFDIE::GetFirstChild
DWARFDIE GetFirstChild() const
Definition: DWARFDIE.cpp:99
DWARFDebugInfo.h
DWARFUnit.h
lldb_private::Stream::PutChar
size_t PutChar(char ch)
Definition: Stream.cpp:104
lldb_private::RangeVector
Definition: RangeMap.h:136
lldb_private::operator!=
bool operator!=(const Address &lhs, const Address &rhs)
Definition: Address.cpp:1022
lldb_private::CompilerContextKind::Class
@ Class
uint32_t
DWARFDebugInfoEntry.h
lldb_private::CompilerContextKind::Variable
@ Variable
lldb_private::Type
Definition: Type.h:66
DWARFBaseDIE::GetName
const char * GetName() const
Definition: DWARFBaseDIE.cpp:77
DWARFDIE::GetDIE
DWARFDIE GetDIE(dw_offset_t die_offset) const
Definition: DWARFDIE.cpp:123
uint16_t
DWARFDIE::GetParentDeclContextDIE
DWARFDIE GetParentDeclContextDIE() const
Definition: DWARFDIE.cpp:432
lldb_private::Stream::Printf
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
DWARFDIE::children
llvm::iterator_range< child_iterator > children() const
The range of all the children of this DIE.
Definition: DWARFDIE.cpp:465
DWARFDIE::GetSibling
DWARFDIE GetSibling() const
Definition: DWARFDIE.cpp:107
lldb_private::RangeVector::FindEntryThatContains
const Entry * FindEntryThatContains(B addr) const
Definition: RangeMap.h:338
lldb_private::DWARFExpressionList
Definition: DWARFExpressionList.h:24
DWARFDIE::LookupDeepestBlock
DWARFDIE LookupDeepestBlock(lldb::addr_t file_addr) const
Definition: DWARFDIE.cpp:144
DWARFDIE::GetDIENamesAndRanges
bool GetDIENamesAndRanges(const char *&name, const char *&mangled, DWARFRangeList &ranges, int &decl_file, int &decl_line, int &decl_column, int &call_file, int &call_line, int &call_column, lldb_private::DWARFExpressionList *frame_base) const
Definition: DWARFDIE.cpp:452
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
llvm::SmallVectorImpl
Definition: Disassembler.h:42
lldb_private::CompilerContextKind::Namespace
@ Namespace
lldb_private::operator*
const Scalar operator*(Scalar lhs, Scalar rhs)
Definition: Scalar.cpp:556
DWARFDIE::IsMethod
bool IsMethod() const
Definition: DWARFDIE.cpp:445
lldb_private::CompilerContextKind::Struct
@ Struct
lldb_private::dwarf
Definition: dwarf.h:19
lldb_private::CompilerContextKind::Function
@ Function
DWARFBaseDIE::IsValid
bool IsValid() const
Definition: DWARFBaseDIE.h:45