This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 226
/
SymbolFileDWARF.cpp
3739 lines (3297 loc) · 135 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 ------------------------------------*- C++ -*-===//
//
// 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/Support/Casting.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/Section.h"
#include "lldb/Core/StreamFile.h"
#include "lldb/Core/Value.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/RegularExpression.h"
#include "lldb/Utility/Scalar.h"
#include "lldb/Utility/StreamString.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 "lldb/Symbol/Block.h"
#include "lldb/Symbol/ClangASTContext.h"
#include "lldb/Symbol/ClangUtil.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/LocateSymbolFile.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/SymbolVendor.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 "DWARFDebugAbbrev.h"
#include "DWARFDebugAranges.h"
#include "DWARFDebugInfo.h"
#include "DWARFDebugLine.h"
#include "DWARFDebugMacro.h"
#include "DWARFDebugRanges.h"
#include "DWARFDeclContext.h"
#include "DWARFFormValue.h"
#include "DWARFUnit.h"
#include "DebugNamesDWARFIndex.h"
#include "LogChannelDWARF.h"
#include "ManualDWARFIndex.h"
#include "SymbolFileDWARFDebugMap.h"
#include "SymbolFileDWARFDwo.h"
#include "SymbolFileDWARFDwp.h"
#include "llvm/Support/FileSystem.h"
#include <algorithm>
#include <map>
#include <memory>
#include <ctype.h>
#include <string.h>
//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
#ifdef ENABLE_DEBUG_PRINTF
#include <stdio.h>
#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
#else
#define DEBUG_PRINTF(fmt, ...)
#endif
using namespace lldb;
using namespace lldb_private;
// static inline bool
// child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
//{
// switch (tag)
// {
// default:
// break;
// case DW_TAG_subprogram:
// case DW_TAG_inlined_subroutine:
// case DW_TAG_class_type:
// case DW_TAG_structure_type:
// case DW_TAG_union_type:
// return true;
// }
// return false;
//}
//
namespace {
static constexpr PropertyDefinition g_properties[] = {
{"comp-dir-symlink-paths", OptionValue::eTypeFileSpecList, true, 0, nullptr,
{},
"If the DW_AT_comp_dir matches any of these paths the symbolic "
"links will be resolved at DWARF parse time."},
{"ignore-file-indexes", OptionValue::eTypeBoolean, true, 0, nullptr, {},
"Ignore indexes present in the object files and always index DWARF "
"manually."}};
enum {
ePropertySymLinkPaths,
ePropertyIgnoreIndexes,
};
class PluginProperties : public Properties {
public:
static ConstString GetSettingName() {
return SymbolFileDWARF::GetPluginNameStatic();
}
PluginProperties() {
m_collection_sp = std::make_shared<OptionValueProperties>(GetSettingName());
m_collection_sp->Initialize(g_properties);
}
FileSpecList GetSymLinkPaths() {
const OptionValueFileSpecList *option_value =
m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(
nullptr, true, ePropertySymLinkPaths);
assert(option_value);
return option_value->GetCurrentValue();
}
bool IgnoreFileIndexes() const {
return m_collection_sp->GetPropertyAtIndexAsBoolean(
nullptr, ePropertyIgnoreIndexes, false);
}
};
typedef std::shared_ptr<PluginProperties> SymbolFileDWARFPropertiesSP;
static const SymbolFileDWARFPropertiesSP &GetGlobalPluginProperties() {
static const auto g_settings_sp(std::make_shared<PluginProperties>());
return g_settings_sp;
}
} // anonymous namespace end
FileSpecList SymbolFileDWARF::GetSymlinkPaths() {
return GetGlobalPluginProperties()->GetSymLinkPaths();
}
DWARFUnit *SymbolFileDWARF::GetBaseCompileUnit() {
return nullptr;
}
void SymbolFileDWARF::Initialize() {
LogChannelDWARF::Initialize();
PluginManager::RegisterPlugin(GetPluginNameStatic(),
GetPluginDescriptionStatic(), CreateInstance,
DebuggerInitialize);
}
void SymbolFileDWARF::DebuggerInitialize(Debugger &debugger) {
if (!PluginManager::GetSettingForSymbolFilePlugin(
debugger, PluginProperties::GetSettingName())) {
const bool is_global_setting = true;
PluginManager::CreateSettingForSymbolFilePlugin(
debugger, GetGlobalPluginProperties()->GetValueProperties(),
ConstString("Properties for the dwarf symbol-file plug-in."),
is_global_setting);
}
}
void SymbolFileDWARF::Terminate() {
PluginManager::UnregisterPlugin(CreateInstance);
LogChannelDWARF::Terminate();
}
lldb_private::ConstString SymbolFileDWARF::GetPluginNameStatic() {
static ConstString g_name("dwarf");
return g_name;
}
const char *SymbolFileDWARF::GetPluginDescriptionStatic() {
return "DWARF and DWARF3 debug symbol file reader.";
}
SymbolFile *SymbolFileDWARF::CreateInstance(ObjectFile *obj_file) {
return new SymbolFileDWARF(obj_file,
/*dwo_section_list*/ nullptr);
}
TypeList *SymbolFileDWARF::GetTypeList() {
// This method can be called without going through the symbol vendor so we
// need to lock the module.
std::lock_guard<std::recursive_mutex> guard(GetModuleMutex());
SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
if (debug_map_symfile)
return debug_map_symfile->GetTypeList();
else
return m_obj_file->GetModule()->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;
}
if (add_type) {
const bool assert_not_being_parsed = true;
Type *type = ResolveTypeUID(die, assert_not_being_parsed);
if (type) {
if (type_set.find(type) == type_set.end())
type_set.insert(type);
}
}
}
for (DWARFDIE child_die = die.GetFirstChild(); child_die.IsValid();
child_die = child_die.GetSibling()) {
GetTypes(child_die, min_die_offset, max_die_offset, type_mask, type_set);
}
}
}
size_t SymbolFileDWARF::GetTypes(SymbolContextScope *sc_scope,
TypeClass type_mask, TypeList &type_list)
{
ASSERT_MODULE_LOCK(this);
TypeSet type_set;
CompileUnit *comp_unit = nullptr;
DWARFUnit *dwarf_cu = nullptr;
if (sc_scope)
comp_unit = sc_scope->CalculateSymbolContextCompileUnit();
if (comp_unit) {
dwarf_cu = GetDWARFCompileUnit(comp_unit);
if (dwarf_cu == nullptr)
return 0;
GetTypes(dwarf_cu->DIE(), dwarf_cu->GetOffset(),
dwarf_cu->GetNextUnitOffset(), type_mask, type_set);
} else {
DWARFDebugInfo *info = DebugInfo();
if (info) {
const size_t num_cus = info->GetNumUnits();
for (size_t cu_idx = 0; cu_idx < num_cus; ++cu_idx) {
dwarf_cu = info->GetUnitAtIndex(cu_idx);
if (dwarf_cu) {
GetTypes(dwarf_cu->DIE(), 0, UINT32_MAX, type_mask, type_set);
}
}
}
}
std::set<CompilerType> compiler_type_set;
size_t num_types_added = 0;
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());
++num_types_added;
}
}
return num_types_added;
}
// 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;
}
}
return DWARFDIE();
}
SymbolFileDWARF::SymbolFileDWARF(ObjectFile *objfile,
SectionList *dwo_section_list)
: SymbolFile(objfile),
UserID(0x7fffffff00000000), // Used by SymbolFileDWARFDebugMap to
// when this class parses .o files to
// contain the .o file index/ID
m_debug_map_module_wp(), m_debug_map_symfile(nullptr),
m_context(objfile->GetModule()->GetSectionList(), dwo_section_list),
m_data_debug_loc(), m_data_debug_ranges(), m_data_debug_rnglists(),
m_abbr(), m_info(), m_line(), m_fetched_external_modules(false),
m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate), m_ranges(),
m_unique_ast_type_map() {}
SymbolFileDWARF::~SymbolFileDWARF() {}
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;
}
TypeSystem *SymbolFileDWARF::GetTypeSystemForLanguage(LanguageType language) {
SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
TypeSystem *type_system;
if (debug_map_symfile) {
type_system = debug_map_symfile->GetTypeSystemForLanguage(language);
} else {
type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
if (type_system)
type_system->SetSymbolFile(this);
}
return type_system;
}
void SymbolFileDWARF::InitializeObject() {
Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
if (!GetGlobalPluginProperties()->IgnoreFileIndexes()) {
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);
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) {
llvm::Expected<std::unique_ptr<DebugNamesDWARFIndex>> index_or =
DebugNamesDWARFIndex::Create(
*GetObjectFile()->GetModule(), debug_names,
m_context.getOrLoadStrData(), DebugInfo());
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 = llvm::make_unique<ManualDWARFIndex>(*GetObjectFile()->GetModule(),
DebugInfo());
}
bool SymbolFileDWARF::SupportedVersion(uint16_t version) {
return version >= 2 && version <= 5;
}
uint32_t SymbolFileDWARF::CalculateAbilities() {
uint32_t abilities = 0;
if (m_obj_file != nullptr) {
const Section *section = nullptr;
const SectionList *section_list = m_obj_file->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();
DWARFDebugAbbrev *abbrev = DebugAbbrev();
if (abbrev) {
std::set<dw_form_t> invalid_forms;
abbrev->GetUnsupportedForms(invalid_forms);
if (!invalid_forms.empty()) {
StreamString error;
error.Printf("unsupported DW_FORM value%s:", invalid_forms.size() > 1 ? "s" : "");
for (auto form : invalid_forms)
error.Printf(" %#x", form);
m_obj_file->GetModule()->ReportWarning("%s", error.GetString().str().c_str());
return 0;
}
}
section =
section_list->FindSectionByType(eSectionTypeDWARFDebugLine, true)
.get();
if (section)
debug_line_file_size = section->GetFileSize();
} else {
const char *symfile_dir_cstr =
m_obj_file->GetFileSpec().GetDirectory().GetCString();
if (symfile_dir_cstr) {
if (strcasestr(symfile_dir_cstr, ".dsym")) {
if (m_obj_file->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_obj_file->GetModule()->ReportWarning(
"empty dSYM file detected, dSYM was created with an "
"executable with no debug info.");
}
}
}
}
}
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;
}
const DWARFDataExtractor &
SymbolFileDWARF::GetCachedSectionData(lldb::SectionType sect_type,
DWARFDataSegment &data_segment) {
llvm::call_once(data_segment.m_flag, [this, sect_type, &data_segment] {
this->LoadSectionData(sect_type, std::ref(data_segment.m_data));
});
return data_segment.m_data;
}
void SymbolFileDWARF::LoadSectionData(lldb::SectionType sect_type,
DWARFDataExtractor &data) {
ModuleSP module_sp(m_obj_file->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_obj_file->ReadSectionData(section_sp.get(), data);
}
const DWARFDataExtractor &SymbolFileDWARF::DebugLocData() {
const DWARFDataExtractor &debugLocData = get_debug_loc_data();
if (debugLocData.GetByteSize() > 0)
return debugLocData;
return get_debug_loclists_data();
}
const DWARFDataExtractor &SymbolFileDWARF::get_debug_loc_data() {
return GetCachedSectionData(eSectionTypeDWARFDebugLoc, m_data_debug_loc);
}
const DWARFDataExtractor &SymbolFileDWARF::get_debug_loclists_data() {
return GetCachedSectionData(eSectionTypeDWARFDebugLocLists,
m_data_debug_loclists);
}
const DWARFDataExtractor &SymbolFileDWARF::get_debug_ranges_data() {
return GetCachedSectionData(eSectionTypeDWARFDebugRanges,
m_data_debug_ranges);
}
const DWARFDataExtractor &SymbolFileDWARF::get_debug_rnglists_data() {
return GetCachedSectionData(eSectionTypeDWARFDebugRngLists,
m_data_debug_rnglists);
}
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 = llvm::make_unique<DWARFDebugAbbrev>();
llvm::Error error = abbr->parse(debug_abbrev_data);
if (error) {
Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
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();
}
const DWARFDebugAbbrev *SymbolFileDWARF::DebugAbbrev() const {
return m_abbr.get();
}
DWARFDebugInfo *SymbolFileDWARF::DebugInfo() {
if (m_info == nullptr) {
static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
static_cast<void *>(this));
if (m_context.getOrLoadDebugInfoData().GetByteSize() > 0) {
m_info = llvm::make_unique<DWARFDebugInfo>(m_context);
m_info->SetDwarfData(this);
}
}
return m_info.get();
}
const DWARFDebugInfo *SymbolFileDWARF::DebugInfo() const {
return m_info.get();
}
DWARFUnit *
SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit) {
if (!comp_unit)
return nullptr;
DWARFDebugInfo *info = DebugInfo();
if (info) {
// The compile unit ID is the index of the DWARF unit.
DWARFUnit *dwarf_cu = info->GetUnitAtIndex(comp_unit->GetID());
if (dwarf_cu && dwarf_cu->GetUserData() == nullptr)
dwarf_cu->SetUserData(comp_unit);
return dwarf_cu;
}
return nullptr;
}
DWARFDebugRangesBase *SymbolFileDWARF::DebugRanges() {
if (m_ranges == nullptr) {
static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
static_cast<void *>(this));
if (get_debug_ranges_data().GetByteSize() > 0)
m_ranges.reset(new DWARFDebugRanges());
else if (get_debug_rnglists_data().GetByteSize() > 0)
m_ranges.reset(new DWARFDebugRngLists());
if (m_ranges)
m_ranges->Extract(this);
}
return m_ranges.get();
}
const DWARFDebugRangesBase *SymbolFileDWARF::DebugRanges() const {
return m_ranges.get();
}
lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFUnit *dwarf_cu) {
CompUnitSP cu_sp;
if (dwarf_cu) {
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 (dwarf_cu->GetSymbolFileDWARF() != this) {
return dwarf_cu->GetSymbolFileDWARF()->ParseCompileUnit(dwarf_cu);
} else if (dwarf_cu->GetOffset() == 0 && GetDebugMapSymfile()) {
// Let the debug map create the compile unit
cu_sp = m_debug_map_symfile->GetCompileUnit(this);
dwarf_cu->SetUserData(cu_sp.get());
} else {
ModuleSP module_sp(m_obj_file->GetModule());
if (module_sp) {
const DWARFDIE cu_die = dwarf_cu->DIE();
if (cu_die) {
FileSpec cu_file_spec(cu_die.GetName(), dwarf_cu->GetPathStyle());
if (cu_file_spec) {
// 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.
cu_file_spec.MakeAbsolute(dwarf_cu->GetCompilationDirectory());
std::string remapped_file;
if (module_sp->RemapSourceFile(cu_file_spec.GetPath(),
remapped_file))
cu_file_spec.SetFile(remapped_file, FileSpec::Style::native);
}
LanguageType cu_language = DWARFUnit::LanguageTypeFromDWARF(
cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0));
bool is_optimized = dwarf_cu->GetIsOptimized();
cu_sp = std::make_shared<CompileUnit>(
module_sp, dwarf_cu, cu_file_spec, dwarf_cu->GetID(),
cu_language, is_optimized ? eLazyBoolYes : eLazyBoolNo);
// If we just created a compile unit with an invalid file spec,
// try and get the first entry in the supports files from the
// line table as that should be the compile unit.
if (!cu_file_spec) {
cu_file_spec = cu_sp->GetSupportFiles().GetFileSpecAtIndex(1);
if (cu_file_spec) {
(FileSpec &)(*cu_sp) = cu_file_spec;
// Also fix the invalid file spec which was copied from the
// compile unit.
cu_sp->GetSupportFiles().Replace(0, cu_file_spec);
}
}
dwarf_cu->SetUserData(cu_sp.get());
m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(
dwarf_cu->GetID(), cu_sp);
}
}
}
}
}
return cu_sp;
}
uint32_t SymbolFileDWARF::GetNumCompileUnits() {
DWARFDebugInfo *info = DebugInfo();
if (info)
return info->GetNumUnits();
return 0;
}
CompUnitSP SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx) {
ASSERT_MODULE_LOCK(this);
CompUnitSP cu_sp;
DWARFDebugInfo *info = DebugInfo();
if (info) {
DWARFUnit *dwarf_cu = info->GetUnitAtIndex(cu_idx);
if (dwarf_cu)
cu_sp = ParseCompileUnit(dwarf_cu);
}
return cu_sp;
}
Function *SymbolFileDWARF::ParseFunction(CompileUnit &comp_unit,
const DWARFDIE &die) {
ASSERT_MODULE_LOCK(this);
if (die.IsValid()) {
TypeSystem *type_system =
GetTypeSystemForLanguage(die.GetCU()->GetLanguageType());
if (type_system) {
DWARFASTParser *dwarf_ast = type_system->GetDWARFParser();
if (dwarf_ast)
return dwarf_ast->ParseFunctionFromDWARF(comp_unit, die);
}
}
return nullptr;
}
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) {
ASSERT_MODULE_LOCK(this);
DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
if (dwarf_cu)
return dwarf_cu->GetLanguageType();
else
return eLanguageTypeUnknown;
}
size_t SymbolFileDWARF::ParseFunctions(CompileUnit &comp_unit) {
ASSERT_MODULE_LOCK(this);
DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
if (!dwarf_cu)
return 0;
size_t functions_added = 0;
std::vector<DWARFDIE> function_dies;
dwarf_cu->AppendDIEsWithTag(DW_TAG_subprogram, function_dies);
for (const DWARFDIE &die : function_dies) {
if (comp_unit.FindFunctionByUID(die.GetID()))
continue;
if (ParseFunction(comp_unit, die))
++functions_added;
}
// FixupTypes();
return functions_added;
}
bool SymbolFileDWARF::ParseSupportFiles(CompileUnit &comp_unit,
FileSpecList &support_files) {
ASSERT_MODULE_LOCK(this);
DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
if (dwarf_cu) {
const DWARFBaseDIE cu_die = dwarf_cu->GetUnitDIEOnly();
if (cu_die) {
const dw_offset_t stmt_list = cu_die.GetAttributeValueAsUnsigned(
DW_AT_stmt_list, DW_INVALID_OFFSET);
if (stmt_list != DW_INVALID_OFFSET) {
// All file indexes in DWARF are one based and a file of index zero is
// supposed to be the compile unit itself.
support_files.Append(comp_unit);
return DWARFDebugLine::ParseSupportFiles(
comp_unit.GetModule(), m_context.getOrLoadLineData(), stmt_list,
support_files, dwarf_cu);
}
}
}
return false;
}
bool SymbolFileDWARF::ParseIsOptimized(CompileUnit &comp_unit) {
ASSERT_MODULE_LOCK(this);
DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
if (dwarf_cu)
return dwarf_cu->GetIsOptimized();
return false;
}
bool SymbolFileDWARF::ParseImportedModules(
const lldb_private::SymbolContext &sc,
std::vector<SourceModule> &imported_modules) {
ASSERT_MODULE_LOCK(this);
assert(sc.comp_unit);
DWARFUnit *dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
if (!dwarf_cu)
return false;
if (!ClangModulesDeclVendor::LanguageSupportsClangModules(
sc.comp_unit->GetLanguage()))
return false;
UpdateExternalModuleListIfNeeded();
const DWARFDIE die = dwarf_cu->DIE();
if (!die)
return false;
for (DWARFDIE child_die = die.GetFirstChild(); child_die;
child_die = child_die.GetSibling()) {
if (child_die.Tag() != DW_TAG_imported_declaration)
continue;
DWARFDIE module_die = child_die.GetReferencedDIE(DW_AT_import);
if (module_die.Tag() != DW_TAG_module)
continue;
if (const char *name =
module_die.GetAttributeValueAsString(DW_AT_name, nullptr)) {
SourceModule module;
module.path.push_back(ConstString(name));
DWARFDIE parent_die = module_die;
while ((parent_die = parent_die.GetParent())) {
if (parent_die.Tag() != DW_TAG_module)
break;
if (const char *name =
parent_die.GetAttributeValueAsString(DW_AT_name, nullptr))
module.path.push_back(ConstString(name));
}
std::reverse(module.path.begin(), module.path.end());
if (const char *include_path = module_die.GetAttributeValueAsString(
DW_AT_LLVM_include_path, nullptr))
module.search_path = ConstString(include_path);
if (const char *sysroot = module_die.GetAttributeValueAsString(
DW_AT_LLVM_isysroot, nullptr))
module.sysroot = ConstString(sysroot);
imported_modules.push_back(module);
}
}
return true;
}
struct ParseDWARFLineTableCallbackInfo {
LineTable *line_table;
std::unique_ptr<LineSequence> sequence_up;
lldb::addr_t addr_mask;
};
// ParseStatementTableCallback
static void ParseDWARFLineTableCallback(dw_offset_t offset,
const DWARFDebugLine::State &state,
void *userData) {
if (state.row == DWARFDebugLine::State::StartParsingLineTable) {
// Just started parsing the line table
} else if (state.row == DWARFDebugLine::State::DoneParsingLineTable) {
// Done parsing line table, nothing to do for the cleanup
} else {
ParseDWARFLineTableCallbackInfo *info =
(ParseDWARFLineTableCallbackInfo *)userData;
LineTable *line_table = info->line_table;
// If this is our first time here, we need to create a sequence container.
if (!info->sequence_up) {
info->sequence_up.reset(line_table->CreateLineSequenceContainer());
assert(info->sequence_up.get());
}
line_table->AppendLineEntryToSequence(
info->sequence_up.get(), state.address & info->addr_mask, state.line,
state.column, state.file, state.is_stmt, state.basic_block,
state.prologue_end, state.epilogue_begin, state.end_sequence);
if (state.end_sequence) {
// First, put the current sequence into the line table.
line_table->InsertSequence(info->sequence_up.get());
// Then, empty it to prepare for the next sequence.
info->sequence_up->Clear();
}
}
}
bool SymbolFileDWARF::ParseLineTable(CompileUnit &comp_unit) {
ASSERT_MODULE_LOCK(this);
if (comp_unit.GetLineTable() != nullptr)
return true;
DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
if (dwarf_cu) {
const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
if (dwarf_cu_die) {
const dw_offset_t cu_line_offset =
dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_stmt_list,
DW_INVALID_OFFSET);
if (cu_line_offset != DW_INVALID_OFFSET) {
std::unique_ptr<LineTable> line_table_up(new LineTable(&comp_unit));
if (line_table_up) {
ParseDWARFLineTableCallbackInfo info;
info.line_table = line_table_up.get();
/*
* MIPS:
* The SymbolContext may not have a valid target, thus we may not be
* able
* to call Address::GetOpcodeLoadAddress() which would clear the bit
* #0
* for MIPS. Use ArchSpec to clear the bit #0.
*/
switch (GetObjectFile()->GetArchitecture().GetMachine()) {
case llvm::Triple::mips:
case llvm::Triple::mipsel:
case llvm::Triple::mips64:
case llvm::Triple::mips64el:
info.addr_mask = ~((lldb::addr_t)1);
break;
default:
info.addr_mask = ~((lldb::addr_t)0);
break;
}
lldb::offset_t offset = cu_line_offset;
DWARFDebugLine::ParseStatementTable(
m_context.getOrLoadLineData(), &offset,
ParseDWARFLineTableCallback, &info, dwarf_cu);
SymbolFileDWARFDebugMap *debug_map_symfile = GetDebugMapSymfile();
if (debug_map_symfile) {
// We have an object file that has a line table with addresses that
// are not linked. We need to link the line table and convert the
// addresses that are relative to the .o file into addresses for
// the main executable.
comp_unit.SetLineTable(
debug_map_symfile->LinkOSOLineTable(this, line_table_up.get()));
} else {
comp_unit.SetLineTable(line_table_up.release());
return true;
}
}
}
}
}
return false;
}
lldb_private::DebugMacrosSP
SymbolFileDWARF::ParseDebugMacros(lldb::offset_t *offset) {
auto iter = m_debug_macros_map.find(*offset);
if (iter != m_debug_macros_map.end())
return iter->second;
const DWARFDataExtractor &debug_macro_data = m_context.getOrLoadMacroData();
if (debug_macro_data.GetByteSize() == 0)
return DebugMacrosSP();
lldb_private::DebugMacrosSP debug_macros_sp(new lldb_private::DebugMacros());
m_debug_macros_map[*offset] = debug_macros_sp;
const DWARFDebugMacroHeader &header =
DWARFDebugMacroHeader::ParseHeader(debug_macro_data, offset);
DWARFDebugMacroEntry::ReadMacroEntries(
debug_macro_data, m_context.getOrLoadStrData(), header.OffsetIs64Bit(),
offset, this, debug_macros_sp);
return debug_macros_sp;
}
bool SymbolFileDWARF::ParseDebugMacros(CompileUnit &comp_unit) {
ASSERT_MODULE_LOCK(this);
DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit);
if (dwarf_cu == nullptr)
return false;
const DWARFBaseDIE dwarf_cu_die = dwarf_cu->GetUnitDIEOnly();
if (!dwarf_cu_die)
return false;
lldb::offset_t sect_offset =
dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_macros, DW_INVALID_OFFSET);
if (sect_offset == DW_INVALID_OFFSET)
sect_offset = dwarf_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_macros,
DW_INVALID_OFFSET);
if (sect_offset == DW_INVALID_OFFSET)
return false;