File: DWARFDIE.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (593 lines) | stat: -rw-r--r-- 17,495 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
//===-- DWARFDIE.cpp ------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "DWARFDIE.h"

#include "DWARFASTParser.h"
#include "DWARFDebugInfo.h"
#include "DWARFDebugInfoEntry.h"
#include "DWARFDeclContext.h"
#include "DWARFUnit.h"
#include "lldb/Symbol/Type.h"

#include "llvm/ADT/iterator.h"
#include "llvm/BinaryFormat/Dwarf.h"

using namespace lldb_private;
using namespace lldb_private::dwarf;
using namespace lldb_private::plugin::dwarf;

namespace {

/// Iterate through all DIEs elaborating (i.e. reachable by a chain of
/// DW_AT_specification and DW_AT_abstract_origin attributes) a given DIE. For
/// convenience, the starting die is included in the sequence as the first
/// item.
class ElaboratingDIEIterator
    : public llvm::iterator_facade_base<
          ElaboratingDIEIterator, std::input_iterator_tag, DWARFDIE,
          std::ptrdiff_t, DWARFDIE *, DWARFDIE *> {

  // The operating invariant is: top of m_worklist contains the "current" item
  // and the rest of the list are items yet to be visited. An empty worklist
  // means we've reached the end.
  // Infinite recursion is prevented by maintaining a list of seen DIEs.
  // Container sizes are optimized for the case of following DW_AT_specification
  // and DW_AT_abstract_origin just once.
  llvm::SmallVector<DWARFDIE, 2> m_worklist;
  llvm::SmallSet<DWARFDebugInfoEntry *, 3> m_seen;

  void Next() {
    assert(!m_worklist.empty() && "Incrementing end iterator?");

    // Pop the current item from the list.
    DWARFDIE die = m_worklist.back();
    m_worklist.pop_back();

    // And add back any items that elaborate it.
    for (dw_attr_t attr : {DW_AT_specification, DW_AT_abstract_origin}) {
      if (DWARFDIE d = die.GetReferencedDIE(attr))
        if (m_seen.insert(die.GetDIE()).second)
          m_worklist.push_back(d);
    }
  }

public:
  /// An iterator starting at die d.
  explicit ElaboratingDIEIterator(DWARFDIE d) : m_worklist(1, d) {}

  /// End marker
  ElaboratingDIEIterator() = default;

  const DWARFDIE &operator*() const { return m_worklist.back(); }
  ElaboratingDIEIterator &operator++() {
    Next();
    return *this;
  }

  friend bool operator==(const ElaboratingDIEIterator &a,
                         const ElaboratingDIEIterator &b) {
    if (a.m_worklist.empty() || b.m_worklist.empty())
      return a.m_worklist.empty() == b.m_worklist.empty();
    return a.m_worklist.back() == b.m_worklist.back();
  }
};

llvm::iterator_range<ElaboratingDIEIterator>
elaborating_dies(const DWARFDIE &die) {
  return llvm::make_range(ElaboratingDIEIterator(die),
                          ElaboratingDIEIterator());
}
} // namespace

DWARFDIE
DWARFDIE::GetParent() const {
  if (IsValid())
    return DWARFDIE(m_cu, m_die->GetParent());
  else
    return DWARFDIE();
}

DWARFDIE
DWARFDIE::GetFirstChild() const {
  if (IsValid())
    return DWARFDIE(m_cu, m_die->GetFirstChild());
  else
    return DWARFDIE();
}

DWARFDIE
DWARFDIE::GetSibling() const {
  if (IsValid())
    return DWARFDIE(m_cu, m_die->GetSibling());
  else
    return DWARFDIE();
}

DWARFDIE
DWARFDIE::GetReferencedDIE(const dw_attr_t attr) const {
  if (IsValid())
    return m_die->GetAttributeValueAsReference(GetCU(), attr);
  else
    return {};
}

DWARFDIE
DWARFDIE::GetDIE(dw_offset_t die_offset) const {
  if (IsValid())
    return m_cu->GetDIE(die_offset);
  else
    return DWARFDIE();
}

DWARFDIE
DWARFDIE::GetAttributeValueAsReferenceDIE(const dw_attr_t attr) const {
  if (IsValid()) {
    DWARFUnit *cu = GetCU();
    const bool check_specification_or_abstract_origin = true;
    DWARFFormValue form_value;
    if (m_die->GetAttributeValue(cu, attr, form_value, nullptr,
                                 check_specification_or_abstract_origin))
      return form_value.Reference();
  }
  return DWARFDIE();
}

DWARFDIE
DWARFDIE::LookupDeepestBlock(lldb::addr_t address) const {
  if (!IsValid())
    return DWARFDIE();

  DWARFDIE result;
  bool check_children = false;
  bool match_addr_range = false;
  switch (Tag()) {
  case DW_TAG_class_type:
  case DW_TAG_namespace:
  case DW_TAG_structure_type:
  case DW_TAG_common_block:
    check_children = true;
    break;
  case DW_TAG_compile_unit:
  case DW_TAG_module:
  case DW_TAG_catch_block:
  case DW_TAG_subprogram:
  case DW_TAG_try_block:
  case DW_TAG_partial_unit:
    match_addr_range = true;
    break;
  case DW_TAG_lexical_block:
  case DW_TAG_inlined_subroutine:
    check_children = true;
    match_addr_range = true;
    break;
  default:
    break;
  }

  if (match_addr_range) {
    DWARFRangeList ranges =
        m_die->GetAttributeAddressRanges(m_cu, /*check_hi_lo_pc=*/true);
    if (ranges.FindEntryThatContains(address)) {
      check_children = true;
      switch (Tag()) {
      default:
        break;

      case DW_TAG_inlined_subroutine: // Inlined Function
      case DW_TAG_lexical_block:      // Block { } in code
        result = *this;
        break;
      }
    } else {
      check_children = false;
    }
  }

  if (check_children) {
    for (DWARFDIE child : children()) {
      if (DWARFDIE child_result = child.LookupDeepestBlock(address))
        return child_result;
    }
  }
  return result;
}

const char *DWARFDIE::GetMangledName() const {
  if (IsValid())
    return m_die->GetMangledName(m_cu);
  else
    return nullptr;
}

const char *DWARFDIE::GetPubname() const {
  if (IsValid())
    return m_die->GetPubname(m_cu);
  else
    return nullptr;
}

// GetName
//
// Get value of the DW_AT_name attribute and place that value into the supplied
// stream object. If the DIE is a NULL object "NULL" is placed into the stream,
// and if no DW_AT_name attribute exists for the DIE then nothing is printed.
void DWARFDIE::GetName(Stream &s) const {
  if (!IsValid())
    return;
  if (GetDIE()->IsNULL()) {
    s.PutCString("NULL");
    return;
  }
  const char *name = GetDIE()->GetAttributeValueAsString(GetCU(), DW_AT_name, nullptr, true);
  if (!name)
    return;
  s.PutCString(name);
}

// AppendTypeName
//
// Follows the type name definition down through all needed tags to end up with
// a fully qualified type name and dump the results to the supplied stream.
// This is used to show the name of types given a type identifier.
void DWARFDIE::AppendTypeName(Stream &s) const {
  if (!IsValid())
    return;
  if (GetDIE()->IsNULL()) {
    s.PutCString("NULL");
    return;
  }
  if (const char *name = GetPubname()) {
    s.PutCString(name);
    return;
  }
  switch (Tag()) {
  case DW_TAG_array_type:
    break; // print out a "[]" after printing the full type of the element
           // below
  case DW_TAG_base_type:
    s.PutCString("base ");
    break;
  case DW_TAG_class_type:
    s.PutCString("class ");
    break;
  case DW_TAG_const_type:
    s.PutCString("const ");
    break;
  case DW_TAG_enumeration_type:
    s.PutCString("enum ");
    break;
  case DW_TAG_file_type:
    s.PutCString("file ");
    break;
  case DW_TAG_interface_type:
    s.PutCString("interface ");
    break;
  case DW_TAG_packed_type:
    s.PutCString("packed ");
    break;
  case DW_TAG_pointer_type:
    break; // print out a '*' after printing the full type below
  case DW_TAG_ptr_to_member_type:
    break; // print out a '*' after printing the full type below
  case DW_TAG_reference_type:
    break; // print out a '&' after printing the full type below
  case DW_TAG_restrict_type:
    s.PutCString("restrict ");
    break;
  case DW_TAG_set_type:
    s.PutCString("set ");
    break;
  case DW_TAG_shared_type:
    s.PutCString("shared ");
    break;
  case DW_TAG_string_type:
    s.PutCString("string ");
    break;
  case DW_TAG_structure_type:
    s.PutCString("struct ");
    break;
  case DW_TAG_subrange_type:
    s.PutCString("subrange ");
    break;
  case DW_TAG_subroutine_type:
    s.PutCString("function ");
    break;
  case DW_TAG_thrown_type:
    s.PutCString("thrown ");
    break;
  case DW_TAG_union_type:
    s.PutCString("union ");
    break;
  case DW_TAG_unspecified_type:
    s.PutCString("unspecified ");
    break;
  case DW_TAG_volatile_type:
    s.PutCString("volatile ");
    break;
  case DW_TAG_LLVM_ptrauth_type: {
    unsigned key = GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_key, 0);
    bool isAddressDiscriminated = GetAttributeValueAsUnsigned(
        DW_AT_LLVM_ptrauth_address_discriminated, 0);
    unsigned extraDiscriminator =
        GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_extra_discriminator, 0);
    bool isaPointer =
        GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_isa_pointer, 0);
    bool authenticatesNullValues = GetAttributeValueAsUnsigned(
        DW_AT_LLVM_ptrauth_authenticates_null_values, 0);
    unsigned authenticationMode =
        GetAttributeValueAsUnsigned(DW_AT_LLVM_ptrauth_authentication_mode, 3);

    s.Printf("__ptrauth(%d, %d, 0x0%x, %d, %d, %d)", key,
             isAddressDiscriminated, extraDiscriminator, isaPointer,
             authenticatesNullValues, authenticationMode);
    break;
  }
  default:
    return;
  }

  // Follow the DW_AT_type if possible
  if (DWARFDIE next_die = GetAttributeValueAsReferenceDIE(DW_AT_type))
    next_die.AppendTypeName(s);

  switch (Tag()) {
  case DW_TAG_array_type:
    s.PutCString("[]");
    break;
  case DW_TAG_pointer_type:
    s.PutChar('*');
    break;
  case DW_TAG_ptr_to_member_type:
    s.PutChar('*');
    break;
  case DW_TAG_reference_type:
    s.PutChar('&');
    break;
  default:
    break;
  }
}

lldb_private::Type *DWARFDIE::ResolveType() const {
  if (IsValid())
    return GetDWARF()->ResolveType(*this, true);
  else
    return nullptr;
}

lldb_private::Type *DWARFDIE::ResolveTypeUID(const DWARFDIE &die) const {
  if (SymbolFileDWARF *dwarf = GetDWARF())
    return dwarf->ResolveTypeUID(die, true);
  return nullptr;
}

static void GetDeclContextImpl(DWARFDIE die,
                               llvm::SmallSet<lldb::user_id_t, 4> &seen,
                               std::vector<CompilerContext> &context) {
  // Stop if we hit a cycle.
  while (die && seen.insert(die.GetID()).second) {
    // Handle outline member function DIEs by following the specification.
    if (DWARFDIE spec = die.GetReferencedDIE(DW_AT_specification)) {
      die = spec;
      continue;
    }
    // To find the name of a type in a type unit, we must follow the signature.
    if (DWARFDIE spec = die.GetReferencedDIE(DW_AT_signature)) {
      die = spec;
      continue;
    }

    // Add this DIE's contribution at the end of the chain.
    auto push_ctx = [&](CompilerContextKind kind, llvm::StringRef name) {
      context.push_back({kind, ConstString(name)});
    };
    switch (die.Tag()) {
    case DW_TAG_module:
      push_ctx(CompilerContextKind::Module, die.GetName());
      break;
    case DW_TAG_namespace:
      push_ctx(CompilerContextKind::Namespace, die.GetName());
      break;
    case DW_TAG_class_type:
    case DW_TAG_structure_type:
      push_ctx(CompilerContextKind::ClassOrStruct, die.GetName());
      break;
    case DW_TAG_union_type:
      push_ctx(CompilerContextKind::Union, die.GetName());
      break;
    case DW_TAG_enumeration_type:
      push_ctx(CompilerContextKind::Enum, die.GetName());
      break;
    case DW_TAG_subprogram:
      push_ctx(CompilerContextKind::Function, die.GetName());
      break;
    case DW_TAG_variable:
      push_ctx(CompilerContextKind::Variable, die.GetPubname());
      break;
    case DW_TAG_typedef:
      push_ctx(CompilerContextKind::Typedef, die.GetName());
      break;
    default:
      break;
    }
    // Now process the parent.
    die = die.GetParent();
  }
}

std::vector<CompilerContext> DWARFDIE::GetDeclContext() const {
  llvm::SmallSet<lldb::user_id_t, 4> seen;
  std::vector<CompilerContext> context;
  GetDeclContextImpl(*this, seen, context);
  std::reverse(context.begin(), context.end());
  return context;
}

static void GetTypeLookupContextImpl(DWARFDIE die,
                                     llvm::SmallSet<lldb::user_id_t, 4> &seen,
                                     std::vector<CompilerContext> &context) {
  // Stop if we hit a cycle.
  while (die && seen.insert(die.GetID()).second) {
    // To find the name of a type in a type unit, we must follow the signature.
    if (DWARFDIE spec = die.GetReferencedDIE(DW_AT_signature)) {
      die = spec;
      continue;
    }

    // If there is no name, then there is no need to look anything up for this
    // DIE.
    const char *name = die.GetName();
    if (!name || !name[0])
      return;

    // Add this DIE's contribution at the end of the chain.
    auto push_ctx = [&](CompilerContextKind kind, llvm::StringRef name) {
      context.push_back({kind, ConstString(name)});
    };
    switch (die.Tag()) {
    case DW_TAG_namespace:
      push_ctx(CompilerContextKind::Namespace, die.GetName());
      break;
    case DW_TAG_class_type:
    case DW_TAG_structure_type:
      push_ctx(CompilerContextKind::ClassOrStruct, die.GetName());
      break;
    case DW_TAG_union_type:
      push_ctx(CompilerContextKind::Union, die.GetName());
      break;
    case DW_TAG_enumeration_type:
      push_ctx(CompilerContextKind::Enum, die.GetName());
      break;
    case DW_TAG_variable:
      push_ctx(CompilerContextKind::Variable, die.GetPubname());
      break;
    case DW_TAG_typedef:
      push_ctx(CompilerContextKind::Typedef, die.GetName());
      break;
    case DW_TAG_base_type:
      push_ctx(CompilerContextKind::Builtin, name);
      break;
    // If any of the tags below appear in the parent chain, stop the decl
    // context and return. Prior to these being in here, if a type existed in a
    // namespace "a" like "a::my_struct", but we also have a function in that
    // same namespace "a" which contained a type named "my_struct", both would
    // return "a::my_struct" as the declaration context since the
    // DW_TAG_subprogram would be skipped and its parent would be found.
    case DW_TAG_compile_unit:
    case DW_TAG_type_unit:
    case DW_TAG_subprogram:
    case DW_TAG_lexical_block:
    case DW_TAG_inlined_subroutine:
      return;
    default:
      break;
    }
    // Now process the parent.
    die = die.GetParent();
  }
}

std::vector<CompilerContext> DWARFDIE::GetTypeLookupContext() const {
  llvm::SmallSet<lldb::user_id_t, 4> seen;
  std::vector<CompilerContext> context;
  GetTypeLookupContextImpl(*this, seen, context);
  std::reverse(context.begin(), context.end());
  return context;
}

static DWARFDeclContext GetDWARFDeclContextImpl(DWARFDIE die) {
  DWARFDeclContext dwarf_decl_ctx;
  while (die) {
    const dw_tag_t tag = die.Tag();
    if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
      break;
    dwarf_decl_ctx.AppendDeclContext(tag, die.GetName());
    DWARFDIE parent_decl_ctx_die = die.GetParentDeclContextDIE();
    if (parent_decl_ctx_die == die)
      break;
    die = parent_decl_ctx_die;
  }
  return dwarf_decl_ctx;
}

DWARFDeclContext DWARFDIE::GetDWARFDeclContext() const {
  return GetDWARFDeclContextImpl(*this);
}

static DWARFDIE GetParentDeclContextDIEImpl(DWARFDIE die) {
  DWARFDIE orig_die = die;
  while (die) {
    // If this is the original DIE that we are searching for a declaration for,
    // then don't look in the cache as we don't want our own decl context to be
    // our decl context...
    if (die != orig_die) {
      switch (die.Tag()) {
      case DW_TAG_compile_unit:
      case DW_TAG_partial_unit:
      case DW_TAG_namespace:
      case DW_TAG_structure_type:
      case DW_TAG_union_type:
      case DW_TAG_class_type:
        return die;

      default:
        break;
      }
    }

    if (DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification)) {
      if (DWARFDIE decl_ctx_die = spec_die.GetParentDeclContextDIE())
        return decl_ctx_die;
    }

    if (DWARFDIE abs_die = die.GetReferencedDIE(DW_AT_abstract_origin)) {
      if (DWARFDIE decl_ctx_die = abs_die.GetParentDeclContextDIE())
        return decl_ctx_die;
    }

    die = die.GetParent();
  }
  return DWARFDIE();
}

DWARFDIE
DWARFDIE::GetParentDeclContextDIE() const {
  return GetParentDeclContextDIEImpl(*this);
}

bool DWARFDIE::IsStructUnionOrClass() const {
  const dw_tag_t tag = Tag();
  return tag == DW_TAG_class_type || tag == DW_TAG_structure_type ||
         tag == DW_TAG_union_type;
}

bool DWARFDIE::IsMethod() const {
  for (DWARFDIE d : elaborating_dies(*this))
    if (d.GetParent().IsStructUnionOrClass())
      return true;
  return false;
}

bool DWARFDIE::GetDIENamesAndRanges(
    const char *&name, const char *&mangled, DWARFRangeList &ranges,
    std::optional<int> &decl_file, std::optional<int> &decl_line,
    std::optional<int> &decl_column, std::optional<int> &call_file,
    std::optional<int> &call_line, std::optional<int> &call_column,
    lldb_private::DWARFExpressionList *frame_base) const {
  if (IsValid()) {
    return m_die->GetDIENamesAndRanges(
        GetCU(), name, mangled, ranges, decl_file, decl_line, decl_column,
        call_file, call_line, call_column, frame_base);
  } else
    return false;
}

llvm::iterator_range<DWARFDIE::child_iterator> DWARFDIE::children() const {
  return llvm::make_range(child_iterator(*this), child_iterator());
}