-
Notifications
You must be signed in to change notification settings - Fork 12.3k
/
SymbolFileDWARF.cpp
4489 lines (3917 loc) · 161 KB
/
SymbolFileDWARF.cpp
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===-- SymbolFileDWARF.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 "SymbolFileDWARF.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/Threading.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleList.h"
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Core/Progress.h"
#include "lldb/Core/Section.h"
#include "lldb/Core/Value.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/LLDBLog.h"
#include "lldb/Utility/RegularExpression.h"
#include "lldb/Utility/Scalar.h"
#include "lldb/Utility/StreamString.h"
#include "lldb/Utility/StructuredData.h"
#include "lldb/Utility/Timer.h"
#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/Host.h"
#include "lldb/Interpreter/OptionValueFileSpecList.h"
#include "lldb/Interpreter/OptionValueProperties.h"
#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
#include "Plugins/SymbolFile/DWARF/DWARFDebugInfoEntry.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
#include "lldb/Symbol/Block.h"
#include "lldb/Symbol/CompileUnit.h"
#include "lldb/Symbol/CompilerDecl.h"
#include "lldb/Symbol/CompilerDeclContext.h"
#include "lldb/Symbol/DebugMacros.h"
#include "lldb/Symbol/LineTable.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/SymbolFile.h"
#include "lldb/Symbol/TypeMap.h"
#include "lldb/Symbol/TypeSystem.h"
#include "lldb/Symbol/VariableList.h"
#include "lldb/Target/Language.h"
#include "lldb/Target/Target.h"
#include "AppleDWARFIndex.h"
#include "DWARFASTParser.h"
#include "DWARFASTParserClang.h"
#include "DWARFCompileUnit.h"
#include "DWARFDebugAranges.h"
#include "DWARFDebugInfo.h"
#include "DWARFDebugMacro.h"
#include "DWARFDebugRanges.h"
#include "DWARFDeclContext.h"
#include "DWARFFormValue.h"
#include "DWARFTypeUnit.h"
#include "DWARFUnit.h"
#include "DebugNamesDWARFIndex.h"
#include "LogChannelDWARF.h"
#include "ManualDWARFIndex.h"
#include "SymbolFileDWARFDebugMap.h"
#include "SymbolFileDWARFDwo.h"
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/FormatVariadic.h"
#include <algorithm>
#include <map>
#include <memory>
#include <optional>
#include <cctype>
#include <cstring>
//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
#ifdef ENABLE_DEBUG_PRINTF
#include <cstdio>
#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
#else
#define DEBUG_PRINTF(fmt, ...)
#endif
using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::dwarf;
using namespace lldb_private::plugin::dwarf;
LLDB_PLUGIN_DEFINE(SymbolFileDWARF)
char SymbolFileDWARF::ID;
namespace {
#define LLDB_PROPERTIES_symbolfiledwarf
#include "SymbolFileDWARFProperties.inc"
enum {
#define LLDB_PROPERTIES_symbolfiledwarf
#include "SymbolFileDWARFPropertiesEnum.inc"
};
class PluginProperties : public Properties {
public:
static llvm::StringRef GetSettingName() {
return SymbolFileDWARF::GetPluginNameStatic();
}
PluginProperties() {
m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
m_collection_sp->Initialize(g_symbolfiledwarf_properties);
}
bool IgnoreFileIndexes() const {
return GetPropertyAtIndexAs<bool>(ePropertyIgnoreIndexes, false);
}
};
} // namespace
static PluginProperties &GetGlobalPluginProperties() {
static PluginProperties g_settings;
return g_settings;
}
static const llvm::DWARFDebugLine::LineTable *
ParseLLVMLineTable(DWARFContext &context, llvm::DWARFDebugLine &line,
dw_offset_t line_offset, dw_offset_t unit_offset) {
Log *log = GetLog(DWARFLog::DebugInfo);
llvm::DWARFDataExtractor data = context.getOrLoadLineData().GetAsLLVMDWARF();
llvm::DWARFContext &ctx = context.GetAsLLVM();
llvm::Expected<const llvm::DWARFDebugLine::LineTable *> line_table =
line.getOrParseLineTable(
data, line_offset, ctx, nullptr, [&](llvm::Error e) {
LLDB_LOG_ERROR(
log, std::move(e),
"SymbolFileDWARF::ParseLineTable failed to parse: {0}");
});
if (!line_table) {
LLDB_LOG_ERROR(log, line_table.takeError(),
"SymbolFileDWARF::ParseLineTable failed to parse: {0}");
return nullptr;
}
return *line_table;
}
static bool ParseLLVMLineTablePrologue(DWARFContext &context,
llvm::DWARFDebugLine::Prologue &prologue,
dw_offset_t line_offset,
dw_offset_t unit_offset) {
Log *log = GetLog(DWARFLog::DebugInfo);
bool success = true;
llvm::DWARFDataExtractor data = context.getOrLoadLineData().GetAsLLVMDWARF();
llvm::DWARFContext &ctx = context.GetAsLLVM();
uint64_t offset = line_offset;
llvm::Error error = prologue.parse(
data, &offset,
[&](llvm::Error e) {
success = false;
LLDB_LOG_ERROR(log, std::move(e),
"SymbolFileDWARF::ParseSupportFiles failed to parse "
"line table prologue: {0}");
},
ctx, nullptr);
if (error) {
LLDB_LOG_ERROR(log, std::move(error),
"SymbolFileDWARF::ParseSupportFiles failed to parse line "
"table prologue: {0}");
return false;
}
return success;
}
static std::optional<std::string>
GetFileByIndex(const llvm::DWARFDebugLine::Prologue &prologue, size_t idx,
llvm::StringRef compile_dir, FileSpec::Style style) {
// Try to get an absolute path first.
std::string abs_path;
auto absolute = llvm::DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath;
if (prologue.getFileNameByIndex(idx, compile_dir, absolute, abs_path, style))
return std::move(abs_path);
// Otherwise ask for a relative path.
std::string rel_path;
auto relative = llvm::DILineInfoSpecifier::FileLineInfoKind::RawValue;
if (!prologue.getFileNameByIndex(idx, compile_dir, relative, rel_path, style))
return {};
return std::move(rel_path);
}
static FileSpecList
ParseSupportFilesFromPrologue(const lldb::ModuleSP &module,
const llvm::DWARFDebugLine::Prologue &prologue,
FileSpec::Style style,
llvm::StringRef compile_dir = {}) {
FileSpecList support_files;
// Handle the case where there are no files first to avoid having to special
// case this later.
if (prologue.FileNames.empty())
return support_files;
// Before DWARF v5, the line table indexes were one based.
const bool is_one_based = prologue.getVersion() < 5;
const size_t file_names = prologue.FileNames.size();
const size_t first_file_idx = is_one_based ? 1 : 0;
const size_t last_file_idx = is_one_based ? file_names : file_names - 1;
// Add a dummy entry to ensure the support file list indices match those we
// get from the debug info and line tables.
if (is_one_based)
support_files.Append(FileSpec());
for (size_t idx = first_file_idx; idx <= last_file_idx; ++idx) {
std::string remapped_file;
if (auto file_path = GetFileByIndex(prologue, idx, compile_dir, style)) {
if (auto remapped = module->RemapSourceFile(llvm::StringRef(*file_path)))
remapped_file = *remapped;
else
remapped_file = std::move(*file_path);
}
Checksum checksum;
if (prologue.ContentTypes.HasMD5) {
const llvm::DWARFDebugLine::FileNameEntry &file_name_entry =
prologue.getFileNameEntry(idx);
checksum = file_name_entry.Checksum;
}
// Unconditionally add an entry, so the indices match up.
support_files.EmplaceBack(remapped_file, style, checksum);
}
return support_files;
}
void SymbolFileDWARF::Initialize() {
LogChannelDWARF::Initialize();
PluginManager::RegisterPlugin(GetPluginNameStatic(),
GetPluginDescriptionStatic(), CreateInstance,
DebuggerInitialize);
SymbolFileDWARFDebugMap::Initialize();
}
void SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) {
if (!PluginManager::GetSettingForSymbolFilePlugin(
debugger, PluginProperties::GetSettingName())) {
const bool is_global_setting = true;
PluginManager::CreateSettingForSymbolFilePlugin(
debugger, GetGlobalPluginProperties().GetValueProperties(),
"Properties for the dwarf symbol-file plug-in.", is_global_setting);
}
}
void SymbolFileDWARF::Terminate() {
SymbolFileDWARFDebugMap::Terminate();
PluginManager::UnregisterPlugin(CreateInstance);
LogChannelDWARF::Terminate();
}
llvm::StringRef SymbolFileDWARF::GetPluginDescriptionStatic() {
return "DWARF and DWARF3 debug symbol file reader.";
}
SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFileSP objfile_sp) {
return new SymbolFileDWARF(std::move(objfile_sp),
/*dwo_section_list*/ nullptr);
}
TypeList &SymbolFileDWARF::GetTypeList() {
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
if (SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile())
return debug_map_symfile->GetTypeList();
return SymbolFileCommon::GetTypeList();
}
void SymbolFileDWARF::GetTypes(const DWARFDIE &die, dw_offset_t min_die_offset,
dw_offset_t max_die_offset, uint32_t type_mask,
TypeSet &type_set) {
if (die) {
const dw_offset_t die_offset = die.GetOffset();
if (die_offset >= max_die_offset)
return;
if (die_offset >= min_die_offset) {
const dw_tag_t tag = die.Tag();
bool add_type = false;
switch (tag) {
case DW_TAG_array_type:
add_type = (type_mask & eTypeClassArray) != 0;
break;
case DW_TAG_unspecified_type:
case DW_TAG_base_type:
add_type = (type_mask & eTypeClassBuiltin) != 0;
break;
case DW_TAG_class_type:
add_type = (type_mask & eTypeClassClass) != 0;
break;
case DW_TAG_structure_type:
add_type = (type_mask & eTypeClassStruct) != 0;
break;
case DW_TAG_union_type:
add_type = (type_mask & eTypeClassUnion) != 0;
break;
case DW_TAG_enumeration_type:
add_type = (type_mask & eTypeClassEnumeration) != 0;
break;
case DW_TAG_subroutine_type:
case DW_TAG_subprogram:
case DW_TAG_inlined_subroutine:
add_type = (type_mask & eTypeClassFunction) != 0;
break;
case DW_TAG_pointer_type:
add_type = (type_mask & eTypeClassPointer) != 0;
break;
case DW_TAG_rvalue_reference_type:
case DW_TAG_reference_type:
add_type = (type_mask & eTypeClassReference) != 0;
break;
case DW_TAG_typedef:
add_type = (type_mask & eTypeClassTypedef) != 0;
break;
case DW_TAG_ptr_to_member_type:
add_type = (type_mask & eTypeClassMemberPointer) != 0;
break;
default:
break;
}
if (add_type) {
const bool assert_not_being_parsed = true;
Type *type = ResolveTypeUID(die, assert_not_being_parsed);
if (type)
type_set.insert(type);
}
}
for (DWARFDIE child_die : die.children()) {
GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
}
}
}
void SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
TypeClass type_mask, TypeList &type_list)
{
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
TypeSet type_set;
CompileUnit *comp_unit = nullptr;
if (sc_scope)
comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
const auto &get = [&](DWARFUnit *unit) {
if (!unit)
return;
unit = &unit->GetNonSkeletonUnit();
GetTypes(unit->DIE(), unit->GetOffset(), unit->GetNextUnitOffset(),
type_mask, type_set);
};
if (comp_unit) {
get(GetDWARFCompileUnit(comp_unit));
} else {
DWARFDebugInfo &info = DebugInfo();
const size_t num_cus = info.GetNumUnits();
for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx)
get(info.GetUnitAtIndex(cu_idx));
}
std::set<CompilerType> compiler_type_set;
for (Type *type : type_set) {
CompilerType compiler_type = type->GetForwardCompilerType();
if (compiler_type_set.find(compiler_type) == compiler_type_set.end()) {
compiler_type_set.insert(compiler_type);
type_list.Insert(type->shared_from_this());
}
}
}
// Gets the first parent that is a lexical block, function or inlined
// subroutine, or compile unit.
DWARFDIE
SymbolFileDWARF::GetParentSymbolContextDIE(const DWARFDIE &child_die) {
DWARFDIE die;
for (die = child_die.GetParent(); die; die = die.GetParent()) {
dw_tag_t tag = die.Tag();
switch (tag) {
case DW_TAG_compile_unit:
case DW_TAG_partial_unit:
case DW_TAG_subprogram:
case DW_TAG_inlined_subroutine:
case DW_TAG_lexical_block:
return die;
default:
break;
}
}
return DWARFDIE();
}
SymbolFileDWARF::SymbolFileDWARF(ObjectFileSP objfile_sp,
SectionList *dwo_section_list)
: SymbolFileCommon(std::move(objfile_sp)), m_debug_map_module_wp(),
m_debug_map_symfile(nullptr),
m_context(m_objfile_sp->GetModule()->GetSectionList(), dwo_section_list),
m_fetched_external_modules(false),
m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate) {}
SymbolFileDWARF::~SymbolFileDWARF() = default;
static ConstString GetDWARFMachOSegmentName() {
static ConstString g_dwarf_section_name("__DWARF");
return g_dwarf_section_name;
}
UniqueDWARFASTTypeMap &SymbolFileDWARF::GetUniqueDWARFASTTypeMap() {
SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
if (debug_map_symfile)
return debug_map_symfile->GetUniqueDWARFASTTypeMap();
else
return m_unique_ast_type_map;
}
llvm::Expected<lldb::TypeSystemSP>
SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
if (SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile())
return debug_map_symfile->GetTypeSystemForLanguage(language);
auto type_system_or_err =
m_objfile_sp->GetModule()->GetTypeSystemForLanguage(language);
if (type_system_or_err)
if (auto ts = *type_system_or_err)
ts->SetSymbolFile(this);
return type_system_or_err;
}
void SymbolFileDWARF::InitializeObject() {
Log *log = GetLog(DWARFLog::DebugInfo);
InitializeFirstCodeAddress();
if (!GetGlobalPluginProperties().IgnoreFileIndexes()) {
StreamString module_desc;
GetObjectFile()->GetModule()->GetDescription(module_desc.AsRawOstream(),
lldb::eDescriptionLevelBrief);
DWARFDataExtractor apple_names, apple_namespaces, apple_types, apple_objc;
LoadSectionData(eSectionTypeDWARFAppleNames, apple_names);
LoadSectionData(eSectionTypeDWARFAppleNamespaces, apple_namespaces);
LoadSectionData(eSectionTypeDWARFAppleTypes, apple_types);
LoadSectionData(eSectionTypeDWARFAppleObjC, apple_objc);
if (apple_names.GetByteSize() > 0 || apple_namespaces.GetByteSize() > 0 ||
apple_types.GetByteSize() > 0 || apple_objc.GetByteSize() > 0) {
Progress progress(llvm::formatv("Loading Apple DWARF index for {0}",
module_desc.GetData()));
m_index = AppleDWARFIndex::Create(
*GetObjectFile()->GetModule(), apple_names, apple_namespaces,
apple_types, apple_objc, m_context.getOrLoadStrData());
if (m_index)
return;
}
DWARFDataExtractor debug_names;
LoadSectionData(eSectionTypeDWARFDebugNames, debug_names);
if (debug_names.GetByteSize() > 0) {
Progress progress(
llvm::formatv("Loading DWARF5 index for {0}", module_desc.GetData()));
llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>> index_or =
DebugNamesDWARFIndex::Create(*GetObjectFile()->GetModule(),
debug_names,
m_context.getOrLoadStrData(), *this);
if (index_or) {
m_index = std::move(*index_or);
return;
}
LLDB_LOG_ERROR(log, index_or.takeError(),
"Unable to read .debug_names data: {0}");
}
}
m_index =
std::make_unique<ManualDWARFIndex>(*GetObjectFile()->GetModule(), *this);
}
void SymbolFileDWARF::InitializeFirstCodeAddress() {
InitializeFirstCodeAddressRecursive(
*m_objfile_sp->GetModule()->GetSectionList());
if (m_first_code_address == LLDB_INVALID_ADDRESS)
m_first_code_address = 0;
}
void SymbolFileDWARF::InitializeFirstCodeAddressRecursive(
const lldb_private::SectionList §ion_list) {
for (SectionSP section_sp : section_list) {
if (section_sp->GetChildren().GetSize() > 0) {
InitializeFirstCodeAddressRecursive(section_sp->GetChildren());
} else if (section_sp->GetType() == eSectionTypeCode) {
m_first_code_address =
std::min(m_first_code_address, section_sp->GetFileAddress());
}
}
}
bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
return version >= 2 && version <= 5;
}
static std::set<dw_form_t>
GetUnsupportedForms(llvm::DWARFDebugAbbrev *debug_abbrev) {
if (!debug_abbrev)
return {};
std::set<dw_form_t> unsupported_forms;
for (const auto &[_, decl_set] : *debug_abbrev)
for (const auto &decl : decl_set)
for (const auto &attr : decl.attributes())
if (!DWARFFormValue::FormIsSupported(attr.Form))
unsupported_forms.insert(attr.Form);
return unsupported_forms;
}
uint32_t SymbolFileDWARF::CalculateAbilities() {
uint32_t abilities = 0;
if (m_objfile_sp != nullptr) {
const Section *section = nullptr;
const SectionList *section_list = m_objfile_sp->GetSectionList();
if (section_list == nullptr)
return 0;
uint64_t debug_abbrev_file_size = 0;
uint64_t debug_info_file_size = 0;
uint64_t debug_line_file_size = 0;
section = section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
if (section)
section_list = §ion->GetChildren();
section =
section_list->FindSectionByType(eSectionTypeDWARFDebugInfo, true).get();
if (section != nullptr) {
debug_info_file_size = section->GetFileSize();
section =
section_list->FindSectionByType(eSectionTypeDWARFDebugAbbrev, true)
.get();
if (section)
debug_abbrev_file_size = section->GetFileSize();
llvm::DWARFDebugAbbrev *abbrev = DebugAbbrev();
std::set<dw_form_t> unsupported_forms = GetUnsupportedForms(abbrev);
if (!unsupported_forms.empty()) {
StreamString error;
error.Printf("unsupported DW_FORM value%s:",
unsupported_forms.size() > 1 ? "s" : "");
for (auto form : unsupported_forms)
error.Printf(" %#x", form);
m_objfile_sp->GetModule()->ReportWarning("{0}", error.GetString());
return 0;
}
section =
section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
.get();
if (section)
debug_line_file_size = section->GetFileSize();
} else {
llvm::StringRef symfile_dir =
m_objfile_sp->GetFileSpec().GetDirectory().GetStringRef();
if (symfile_dir.contains_insensitive(".dsym")) {
if (m_objfile_sp->GetType() == ObjectFile::eTypeDebugInfo) {
// We have a dSYM file that didn't have a any debug info. If the
// string table has a size of 1, then it was made from an
// executable with no debug info, or from an executable that was
// stripped.
section =
section_list->FindSectionByType(eSectionTypeDWARFDebugStr, true)
.get();
if (section && section->GetFileSize() == 1) {
m_objfile_sp->GetModule()->ReportWarning(
"empty dSYM file detected, dSYM was created with an "
"executable with no debug info.");
}
}
}
}
constexpr uint64_t MaxDebugInfoSize = (1ull) << DW_DIE_OFFSET_MAX_BITSIZE;
if (debug_info_file_size >= MaxDebugInfoSize) {
m_objfile_sp->GetModule()->ReportWarning(
"SymbolFileDWARF can't load this DWARF. It's larger then {0:x+16}",
MaxDebugInfoSize);
return 0;
}
if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
abilities |= CompileUnits | Functions | Blocks | GlobalVariables |
LocalVariables | VariableTypes;
if (debug_line_file_size > 0)
abilities |= LineTables;
}
return abilities;
}
void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
DWARFDataExtractor &data) {
ModuleSP module_sp(m_objfile_sp->GetModule());
const SectionList *section_list = module_sp->GetSectionList();
if (!section_list)
return;
SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
if (!section_sp)
return;
data.Clear();
m_objfile_sp->ReadSectionData(section_sp.get(), data);
}
llvm::DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() {
if (m_abbr)
return m_abbr.get();
const DWARFDataExtractor &debug_abbrev_data = m_context.getOrLoadAbbrevData();
if (debug_abbrev_data.GetByteSize() == 0)
return nullptr;
auto abbr =
std::make_unique<llvm::DWARFDebugAbbrev>(debug_abbrev_data.GetAsLLVM());
llvm::Error error = abbr->parse();
if (error) {
Log *log = GetLog(DWARFLog::DebugInfo);
LLDB_LOG_ERROR(log, std::move(error),
"Unable to read .debug_abbrev section: {0}");
return nullptr;
}
m_abbr = std::move(abbr);
return m_abbr.get();
}
DWARFDebugInfo &SymbolFileDWARF::DebugInfo() {
llvm::call_once(m_info_once_flag, [&] {
LLDB_SCOPED_TIMERF("%s this = %p", LLVM_PRETTY_FUNCTION,
static_cast<void *>(this));
m_info = std::make_unique<DWARFDebugInfo>(*this, m_context);
});
return *m_info;
}
DWARFCompileUnit *SymbolFileDWARF::GetDWARFCompileUnit(CompileUnit *comp_unit) {
if (!comp_unit)
return nullptr;
// The compile unit ID is the index of the DWARF unit.
DWARFUnit *dwarf_cu = DebugInfo().GetUnitAtIndex(comp_unit->GetID());
if (dwarf_cu && dwarf_cu->GetUserData() == nullptr)
dwarf_cu->SetUserData(comp_unit);
// It must be DWARFCompileUnit when it created a CompileUnit.
return llvm::cast_or_null<DWARFCompileUnit>(dwarf_cu);
}
DWARFDebugRanges *SymbolFileDWARF::GetDebugRanges() {
if (!m_ranges) {
LLDB_SCOPED_TIMERF("%s this = %p", LLVM_PRETTY_FUNCTION,
static_cast<void *>(this));
if (m_context.getOrLoadRangesData().GetByteSize() > 0)
m_ranges = std::make_unique<DWARFDebugRanges>();
if (m_ranges)
m_ranges->Extract(m_context);
}
return m_ranges.get();
}
/// Make an absolute path out of \p file_spec and remap it using the
/// module's source remapping dictionary.
static void MakeAbsoluteAndRemap(FileSpec &file_spec, DWARFUnit &dwarf_cu,
const ModuleSP &module_sp) {
if (!file_spec)
return;
// If we have a full path to the compile unit, we don't need to
// resolve the file. This can be expensive e.g. when the source
// files are NFS mounted.
file_spec.MakeAbsolute(dwarf_cu.GetCompilationDirectory());
if (auto remapped_file = module_sp->RemapSourceFile(file_spec.GetPath()))
file_spec.SetFile(*remapped_file, FileSpec::Style::native);
}
/// Return the DW_AT_(GNU_)dwo_name.
static const char *GetDWOName(DWARFCompileUnit &dwarf_cu,
const DWARFDebugInfoEntry &cu_die) {
const char *dwo_name =
cu_die.GetAttributeValueAsString(&dwarf_cu, DW_AT_GNU_dwo_name, nullptr);
if (!dwo_name)
dwo_name =
cu_die.GetAttributeValueAsString(&dwarf_cu, DW_AT_dwo_name, nullptr);
return dwo_name;
}
lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFCompileUnit &dwarf_cu) {
CompUnitSP cu_sp;
CompileUnit *comp_unit = (CompileUnit *)dwarf_cu.GetUserData();
if (comp_unit) {
// We already parsed this compile unit, had out a shared pointer to it
cu_sp = comp_unit->shared_from_this();
} else {
if (GetDebugMapSymfile()) {
// Let the debug map create the compile unit
cu_sp = m_debug_map_symfile->GetCompileUnit(this, dwarf_cu);
dwarf_cu.SetUserData(cu_sp.get());
} else {
ModuleSP module_sp(m_objfile_sp->GetModule());
if (module_sp) {
auto initialize_cu = [&](const FileSpec &file_spec,
LanguageType cu_language) {
BuildCuTranslationTable();
cu_sp = std::make_shared<CompileUnit>(
module_sp, &dwarf_cu, file_spec,
*GetDWARFUnitIndex(dwarf_cu.GetID()), cu_language,
eLazyBoolCalculate);
dwarf_cu.SetUserData(cu_sp.get());
SetCompileUnitAtIndex(dwarf_cu.GetID(), cu_sp);
};
auto lazy_initialize_cu = [&]() {
// If the version is < 5, we can't do lazy initialization.
if (dwarf_cu.GetVersion() < 5)
return false;
// If there is no DWO, there is no reason to initialize
// lazily; we will do eager initialization in that case.
if (GetDebugMapSymfile())
return false;
const DWARFBaseDIE cu_die = dwarf_cu.GetUnitDIEOnly();
if (!cu_die)
return false;
if (!GetDWOName(dwarf_cu, *cu_die.GetDIE()))
return false;
// With DWARFv5 we can assume that the first support
// file is also the name of the compile unit. This
// allows us to avoid loading the non-skeleton unit,
// which may be in a separate DWO file.
FileSpecList support_files;
if (!ParseSupportFiles(dwarf_cu, module_sp, support_files))
return false;
if (support_files.GetSize() == 0)
return false;
initialize_cu(support_files.GetFileSpecAtIndex(0),
eLanguageTypeUnknown);
cu_sp->SetSupportFiles(std::move(support_files));
return true;
};
if (!lazy_initialize_cu()) {
// Eagerly initialize compile unit
const DWARFBaseDIE cu_die =
dwarf_cu.GetNonSkeletonUnit().GetUnitDIEOnly();
if (cu_die) {
LanguageType cu_language = SymbolFileDWARF::LanguageTypeFromDWARF(
dwarf_cu.GetDWARFLanguageType());
FileSpec cu_file_spec(cu_die.GetName(), dwarf_cu.GetPathStyle());
// Path needs to be remapped in this case. In the support files
// case ParseSupportFiles takes care of the remapping.
MakeAbsoluteAndRemap(cu_file_spec, dwarf_cu, module_sp);
initialize_cu(cu_file_spec, cu_language);
}
}
}
}
}
return cu_sp;
}
void SymbolFileDWARF::BuildCuTranslationTable() {
if (!m_lldb_cu_to_dwarf_unit.empty())
return;
DWARFDebugInfo &info = DebugInfo();
if (!info.ContainsTypeUnits()) {
// We can use a 1-to-1 mapping. No need to build a translation table.
return;
}
for (uint32_t i = 0, num = info.GetNumUnits(); i < num; ++i) {
if (auto *cu = llvm::dyn_cast<DWARFCompileUnit>(info.GetUnitAtIndex(i))) {
cu->SetID(m_lldb_cu_to_dwarf_unit.size());
m_lldb_cu_to_dwarf_unit.push_back(i);
}
}
}
std::optional<uint32_t> SymbolFileDWARF::GetDWARFUnitIndex(uint32_t cu_idx) {
BuildCuTranslationTable();
if (m_lldb_cu_to_dwarf_unit.empty())
return cu_idx;
if (cu_idx >= m_lldb_cu_to_dwarf_unit.size())
return std::nullopt;
return m_lldb_cu_to_dwarf_unit[cu_idx];
}
uint32_t SymbolFileDWARF::CalculateNumCompileUnits() {
BuildCuTranslationTable();
return m_lldb_cu_to_dwarf_unit.empty() ? DebugInfo().GetNumUnits()
: m_lldb_cu_to_dwarf_unit.size();
}
CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
ASSERT_MODULE_LOCK(this);
if (std::optional<uint32_t> dwarf_idx = GetDWARFUnitIndex(cu_idx)) {
if (auto *dwarf_cu = llvm::cast_or_null<DWARFCompileUnit>(
DebugInfo().GetUnitAtIndex(*dwarf_idx)))
return ParseCompileUnit(*dwarf_cu);
}
return {};
}
Function *SymbolFileDWARF::ParseFunction(CompileUnit &comp_unit,
const DWARFDIE &die) {
ASSERT_MODULE_LOCK(this);
if (!die.IsValid())
return nullptr;
auto type_system_or_err = GetTypeSystemForLanguage(GetLanguage(*die.GetCU()));
if (auto err = type_system_or_err.takeError()) {
LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
"Unable to parse function: {0}");
return nullptr;
}
auto ts = *type_system_or_err;
if (!ts)
return nullptr;
DWARFASTParser *dwarf_ast = ts->GetDWARFParser();
if (!dwarf_ast)
return nullptr;
DWARFRangeList ranges = die.GetDIE()->GetAttributeAddressRanges(
die.GetCU(), /*check_hi_lo_pc=*/true);
if (ranges.IsEmpty())
return nullptr;
// Union of all ranges in the function DIE (if the function is
// discontiguous)
lldb::addr_t lowest_func_addr = ranges.GetMinRangeBase(0);
lldb::addr_t highest_func_addr = ranges.GetMaxRangeEnd(0);
if (lowest_func_addr == LLDB_INVALID_ADDRESS ||
lowest_func_addr >= highest_func_addr ||
lowest_func_addr < m_first_code_address)
return nullptr;
ModuleSP module_sp(die.GetModule());
AddressRange func_range;
func_range.GetBaseAddress().ResolveAddressUsingFileSections(
lowest_func_addr, module_sp->GetSectionList());
if (!func_range.GetBaseAddress().IsValid())
return nullptr;
func_range.SetByteSize(highest_func_addr - lowest_func_addr);
if (!FixupAddress(func_range.GetBaseAddress()))
return nullptr;
return dwarf_ast->ParseFunctionFromDWARF(comp_unit, die, func_range);
}
ConstString
SymbolFileDWARF::ConstructFunctionDemangledName(const DWARFDIE &die) {
ASSERT_MODULE_LOCK(this);
if (!die.IsValid()) {
return ConstString();
}
auto type_system_or_err = GetTypeSystemForLanguage(GetLanguage(*die.GetCU()));
if (auto err = type_system_or_err.takeError()) {
LLDB_LOG_ERROR(GetLog(LLDBLog::Symbols), std::move(err),
"Unable to construct demangled name for function: {0}");
return ConstString();
}
auto ts = *type_system_or_err;
if (!ts) {
LLDB_LOG(GetLog(LLDBLog::Symbols), "Type system no longer live");
return ConstString();
}
DWARFASTParser *dwarf_ast = ts->GetDWARFParser();
if (!dwarf_ast)
return ConstString();
return dwarf_ast->ConstructDemangledNameFromDWARF(die);
}
lldb::addr_t SymbolFileDWARF::FixupAddress(lldb::addr_t file_addr) {
SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
if (debug_map_symfile)
return debug_map_symfile->LinkOSOFileAddress(this, file_addr);
return file_addr;
}
bool SymbolFileDWARF::FixupAddress(Address &addr) {
SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
if (debug_map_symfile) {
return debug_map_symfile->LinkOSOAddress(addr);
}
// This is a normal DWARF file, no address fixups need to happen
return true;
}
lldb::LanguageType SymbolFileDWARF::ParseLanguage(CompileUnit &comp_unit) {
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
if (dwarf_cu)
return GetLanguage(dwarf_cu->GetNonSkeletonUnit());
else
return eLanguageTypeUnknown;
}
XcodeSDK SymbolFileDWARF::ParseXcodeSDK(CompileUnit &comp_unit) {
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
if (!dwarf_cu)
return {};
const DWARFBaseDIE cu_die = dwarf_cu->GetNonSkeletonUnit().GetUnitDIEOnly();
if (!cu_die)
return {};
const char *sdk = cu_die.GetAttributeValueAsString(DW_AT_APPLE_sdk, nullptr);
if (!sdk)
return {};
const char *sysroot =
cu_die.GetAttributeValueAsString(DW_AT_LLVM_sysroot, "");
// Register the sysroot path remapping with the module belonging to
// the CU as well as the one belonging to the symbol file. The two
// would be different if this is an OSO object and module is the
// corresponding debug map, in which case both should be updated.
ModuleSP module_sp = comp_unit.GetModule();
if (module_sp)
module_sp->RegisterXcodeSDK(sdk, sysroot);
ModuleSP local_module_sp = m_objfile_sp->GetModule();
if (local_module_sp && local_module_sp != module_sp)
local_module_sp->RegisterXcodeSDK(sdk, sysroot);
return {sdk};
}
size_t SymbolFileDWARF::ParseFunctions(CompileUnit &comp_unit) {
LLDB_SCOPED_TIMER();
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
if (!dwarf_cu)
return 0;
size_t functions_added = 0;
dwarf_cu = &dwarf_cu->GetNonSkeletonUnit();
for (DWARFDebugInfoEntry &entry : dwarf_cu->dies()) {
if (entry.Tag() != DW_TAG_subprogram)
continue;
DWARFDIE die(dwarf_cu, &entry);
if (comp_unit.FindFunctionByUID(die.GetID()))
continue;
if (ParseFunction(comp_unit, die))
++functions_added;
}
// FixupTypes();
return functions_added;
}
bool SymbolFileDWARF::ForEachExternalModule(
CompileUnit &comp_unit,
llvm::DenseSet<lldb_private::SymbolFile *> &visited_symbol_files,