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
1001
1002
1003
| //===-- ModuleList.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 "lldb/Core/ModuleList.h"
#include "lldb/Core/FileSpecList.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/ModuleSpec.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Interpreter/OptionValueFileSpec.h"
#include "lldb/Interpreter/OptionValueProperties.h"
#include "lldb/Interpreter/Property.h"
#include "lldb/Symbol/LocateSymbolFile.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Symbol/TypeList.h"
#include "lldb/Symbol/VariableList.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/Logging.h"
#include "lldb/Utility/UUID.h"
#include "lldb/lldb-defines.h"
#if defined(_WIN32)
#include "lldb/Host/windows/PosixApi.h"
#endif
#include "clang/Driver/Driver.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Threading.h"
#include "llvm/Support/raw_ostream.h"
#include <chrono>
#include <memory>
#include <mutex>
#include <string>
#include <utility>
namespace lldb_private {
class Function;
}
namespace lldb_private {
class RegularExpression;
}
namespace lldb_private {
class Stream;
}
namespace lldb_private {
class SymbolFile;
}
namespace lldb_private {
class Target;
}
using namespace lldb;
using namespace lldb_private;
namespace {
#define LLDB_PROPERTIES_modulelist
#include "CoreProperties.inc"
enum {
#define LLDB_PROPERTIES_modulelist
#include "CorePropertiesEnum.inc"
};
} // namespace
ModuleListProperties::ModuleListProperties() {
m_collection_sp =
std::make_shared<OptionValueProperties>(ConstString("symbols"));
m_collection_sp->Initialize(g_modulelist_properties);
llvm::SmallString<128> path;
clang::driver::Driver::getDefaultModuleCachePath(path);
SetClangModulesCachePath(path);
}
bool ModuleListProperties::GetEnableExternalLookup() const {
const uint32_t idx = ePropertyEnableExternalLookup;
return m_collection_sp->GetPropertyAtIndexAsBoolean(
nullptr, idx, g_modulelist_properties[idx].default_uint_value != 0);
}
bool ModuleListProperties::SetEnableExternalLookup(bool new_value) {
return m_collection_sp->SetPropertyAtIndexAsBoolean(
nullptr, ePropertyEnableExternalLookup, new_value);
}
FileSpec ModuleListProperties::GetClangModulesCachePath() const {
return m_collection_sp
->GetPropertyAtIndexAsOptionValueFileSpec(nullptr, false,
ePropertyClangModulesCachePath)
->GetCurrentValue();
}
bool ModuleListProperties::SetClangModulesCachePath(llvm::StringRef path) {
return m_collection_sp->SetPropertyAtIndexAsString(
nullptr, ePropertyClangModulesCachePath, path);
}
ModuleList::ModuleList()
: m_modules(), m_modules_mutex(), m_notifier(nullptr) {}
ModuleList::ModuleList(const ModuleList &rhs)
: m_modules(), m_modules_mutex(), m_notifier(nullptr) {
std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
m_modules = rhs.m_modules;
}
ModuleList::ModuleList(ModuleList::Notifier *notifier)
: m_modules(), m_modules_mutex(), m_notifier(notifier) {}
const ModuleList &ModuleList::operator=(const ModuleList &rhs) {
if (this != &rhs) {
std::lock(m_modules_mutex, rhs.m_modules_mutex);
std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex,
std::adopt_lock);
std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex,
std::adopt_lock);
m_modules = rhs.m_modules;
}
return *this;
}
ModuleList::~ModuleList() = default;
void ModuleList::AppendImpl(const ModuleSP &module_sp, bool use_notifier) {
if (module_sp) {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
m_modules.push_back(module_sp);
if (use_notifier && m_notifier)
m_notifier->NotifyModuleAdded(*this, module_sp);
}
}
void ModuleList::Append(const ModuleSP &module_sp, bool notify) {
AppendImpl(module_sp, notify);
}
void ModuleList::ReplaceEquivalent(const ModuleSP &module_sp) {
if (module_sp) {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
// First remove any equivalent modules. Equivalent modules are modules
// whose path, platform path and architecture match.
ModuleSpec equivalent_module_spec(module_sp->GetFileSpec(),
module_sp->GetArchitecture());
equivalent_module_spec.GetPlatformFileSpec() =
module_sp->GetPlatformFileSpec();
size_t idx = 0;
while (idx < m_modules.size()) {
ModuleSP module_sp(m_modules[idx]);
if (module_sp->MatchesModuleSpec(equivalent_module_spec))
RemoveImpl(m_modules.begin() + idx);
else
++idx;
}
// Now add the new module to the list
Append(module_sp);
}
}
bool ModuleList::AppendIfNeeded(const ModuleSP &module_sp, bool notify) {
if (module_sp) {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
if (pos->get() == module_sp.get())
return false; // Already in the list
}
// Only push module_sp on the list if it wasn't already in there.
Append(module_sp, notify);
return true;
}
return false;
}
void ModuleList::Append(const ModuleList &module_list) {
for (auto pos : module_list.m_modules)
Append(pos);
}
bool ModuleList::AppendIfNeeded(const ModuleList &module_list) {
bool any_in = false;
for (auto pos : module_list.m_modules) {
if (AppendIfNeeded(pos))
any_in = true;
}
return any_in;
}
bool ModuleList::RemoveImpl(const ModuleSP &module_sp, bool use_notifier) {
if (module_sp) {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
if (pos->get() == module_sp.get()) {
m_modules.erase(pos);
if (use_notifier && m_notifier)
m_notifier->NotifyModuleRemoved(*this, module_sp);
return true;
}
}
}
return false;
}
ModuleList::collection::iterator
ModuleList::RemoveImpl(ModuleList::collection::iterator pos,
bool use_notifier) {
ModuleSP module_sp(*pos);
collection::iterator retval = m_modules.erase(pos);
if (use_notifier && m_notifier)
m_notifier->NotifyModuleRemoved(*this, module_sp);
return retval;
}
bool ModuleList::Remove(const ModuleSP &module_sp, bool notify) {
return RemoveImpl(module_sp, notify);
}
bool ModuleList::ReplaceModule(const lldb::ModuleSP &old_module_sp,
const lldb::ModuleSP &new_module_sp) {
if (!RemoveImpl(old_module_sp, false))
return false;
AppendImpl(new_module_sp, false);
if (m_notifier)
m_notifier->NotifyModuleUpdated(*this, old_module_sp, new_module_sp);
return true;
}
bool ModuleList::RemoveIfOrphaned(const Module *module_ptr) {
if (module_ptr) {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
if (pos->get() == module_ptr) {
if (pos->unique()) {
pos = RemoveImpl(pos);
return true;
} else
return false;
}
}
}
return false;
}
size_t ModuleList::RemoveOrphans(bool mandatory) {
std::unique_lock<std::recursive_mutex> lock(m_modules_mutex, std::defer_lock);
if (mandatory) {
lock.lock();
} else {
// Not mandatory, remove orphans if we can get the mutex
if (!lock.try_lock())
return 0;
}
collection::iterator pos = m_modules.begin();
size_t remove_count = 0;
while (pos != m_modules.end()) {
if (pos->unique()) {
pos = RemoveImpl(pos);
++remove_count;
} else {
++pos;
}
}
return remove_count;
}
size_t ModuleList::Remove(ModuleList &module_list) {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
size_t num_removed = 0;
collection::iterator pos, end = module_list.m_modules.end();
for (pos = module_list.m_modules.begin(); pos != end; ++pos) {
if (Remove(*pos, false /* notify */))
++num_removed;
}
if (m_notifier)
m_notifier->NotifyModulesRemoved(module_list);
return num_removed;
}
void ModuleList::Clear() { ClearImpl(); }
void ModuleList::Destroy() { ClearImpl(); }
void ModuleList::ClearImpl(bool use_notifier) {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
if (use_notifier && m_notifier)
m_notifier->NotifyWillClearList(*this);
m_modules.clear();
}
Module *ModuleList::GetModulePointerAtIndex(size_t idx) const {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
return GetModulePointerAtIndexUnlocked(idx);
}
Module *ModuleList::GetModulePointerAtIndexUnlocked(size_t idx) const {
if (idx < m_modules.size())
return m_modules[idx].get();
return nullptr;
}
ModuleSP ModuleList::GetModuleAtIndex(size_t idx) const {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
return GetModuleAtIndexUnlocked(idx);
}
ModuleSP ModuleList::GetModuleAtIndexUnlocked(size_t idx) const {
ModuleSP module_sp;
if (idx < m_modules.size())
module_sp = m_modules[idx];
return module_sp;
}
void ModuleList::FindFunctions(ConstString name,
FunctionNameType name_type_mask,
bool include_symbols, bool include_inlines,
SymbolContextList &sc_list) const {
const size_t old_size = sc_list.GetSize();
if (name_type_mask & eFunctionNameTypeAuto) {
Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
(*pos)->FindFunctions(lookup_info.GetLookupName(), nullptr,
lookup_info.GetNameTypeMask(), include_symbols,
include_inlines, sc_list);
}
const size_t new_size = sc_list.GetSize();
if (old_size < new_size)
lookup_info.Prune(sc_list, old_size);
} else {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
(*pos)->FindFunctions(name, nullptr, name_type_mask, include_symbols,
include_inlines, sc_list);
}
}
}
void ModuleList::FindFunctionSymbols(ConstString name,
lldb::FunctionNameType name_type_mask,
SymbolContextList &sc_list) {
const size_t old_size = sc_list.GetSize();
if (name_type_mask & eFunctionNameTypeAuto) {
Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
(*pos)->FindFunctionSymbols(lookup_info.GetLookupName(),
lookup_info.GetNameTypeMask(), sc_list);
}
const size_t new_size = sc_list.GetSize();
if (old_size < new_size)
lookup_info.Prune(sc_list, old_size);
} else {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
(*pos)->FindFunctionSymbols(name, name_type_mask, sc_list);
}
}
}
void ModuleList::FindFunctions(const RegularExpression &name,
bool include_symbols, bool include_inlines,
SymbolContextList &sc_list) {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
(*pos)->FindFunctions(name, include_symbols, include_inlines, sc_list);
}
}
void ModuleList::FindCompileUnits(const FileSpec &path,
SymbolContextList &sc_list) const {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
(*pos)->FindCompileUnits(path, sc_list);
}
}
void ModuleList::FindGlobalVariables(ConstString name, size_t max_matches,
VariableList &variable_list) const {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
(*pos)->FindGlobalVariables(name, nullptr, max_matches, variable_list);
}
}
void ModuleList::FindGlobalVariables(const RegularExpression ®ex,
size_t max_matches,
VariableList &variable_list) const {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
(*pos)->FindGlobalVariables(regex, max_matches, variable_list);
}
}
void ModuleList::FindSymbolsWithNameAndType(ConstString name,
SymbolType symbol_type,
SymbolContextList &sc_list) const {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos)
(*pos)->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
}
void ModuleList::FindSymbolsMatchingRegExAndType(
const RegularExpression ®ex, lldb::SymbolType symbol_type,
SymbolContextList &sc_list) const {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos)
(*pos)->FindSymbolsMatchingRegExAndType(regex, symbol_type, sc_list);
}
void ModuleList::FindModules(const ModuleSpec &module_spec,
ModuleList &matching_module_list) const {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
ModuleSP module_sp(*pos);
if (module_sp->MatchesModuleSpec(module_spec))
matching_module_list.Append(module_sp);
}
}
ModuleSP ModuleList::FindModule(const Module *module_ptr) const {
ModuleSP module_sp;
// Scope for "locker"
{
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
if ((*pos).get() == module_ptr) {
module_sp = (*pos);
break;
}
}
}
return module_sp;
}
ModuleSP ModuleList::FindModule(const UUID &uuid) const {
ModuleSP module_sp;
if (uuid.IsValid()) {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
if ((*pos)->GetUUID() == uuid) {
module_sp = (*pos);
break;
}
}
}
return module_sp;
}
void ModuleList::FindTypes(Module *search_first, ConstString name,
bool name_is_fully_qualified, size_t max_matches,
llvm::DenseSet<SymbolFile *> &searched_symbol_files,
TypeList &types) const {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
if (search_first) {
for (pos = m_modules.begin(); pos != end; ++pos) {
if (search_first == pos->get()) {
search_first->FindTypes(name, name_is_fully_qualified, max_matches,
searched_symbol_files, types);
if (types.GetSize() >= max_matches)
return;
}
}
}
for (pos = m_modules.begin(); pos != end; ++pos) {
// Search the module if the module is not equal to the one in the symbol
// context "sc". If "sc" contains a empty module shared pointer, then the
// comparison will always be true (valid_module_ptr != nullptr).
if (search_first != pos->get())
(*pos)->FindTypes(name, name_is_fully_qualified, max_matches,
searched_symbol_files, types);
if (types.GetSize() >= max_matches)
return;
}
}
bool ModuleList::FindSourceFile(const FileSpec &orig_spec,
FileSpec &new_spec) const {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
if ((*pos)->FindSourceFile(orig_spec, new_spec))
return true;
}
return false;
}
void ModuleList::FindAddressesForLine(const lldb::TargetSP target_sp,
const FileSpec &file, uint32_t line,
Function *function,
std::vector<Address> &output_local,
std::vector<Address> &output_extern) {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
(*pos)->FindAddressesForLine(target_sp, file, line, function, output_local,
output_extern);
}
}
ModuleSP ModuleList::FindFirstModule(const ModuleSpec &module_spec) const {
ModuleSP module_sp;
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
ModuleSP module_sp(*pos);
if (module_sp->MatchesModuleSpec(module_spec))
return module_sp;
}
return module_sp;
}
size_t ModuleList::GetSize() const {
size_t size = 0;
{
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
size = m_modules.size();
}
return size;
}
void ModuleList::Dump(Stream *s) const {
// s.Printf("%.*p: ", (int)sizeof(void*) * 2, this);
// s.Indent();
// s << "ModuleList\n";
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
(*pos)->Dump(s);
}
}
void ModuleList::LogUUIDAndPaths(Log *log, const char *prefix_cstr) {
if (log != nullptr) {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, begin = m_modules.begin(),
end = m_modules.end();
for (pos = begin; pos != end; ++pos) {
Module *module = pos->get();
const FileSpec &module_file_spec = module->GetFileSpec();
LLDB_LOGF(log, "%s[%u] %s (%s) \"%s\"", prefix_cstr ? prefix_cstr : "",
(uint32_t)std::distance(begin, pos),
module->GetUUID().GetAsString().c_str(),
module->GetArchitecture().GetArchitectureName(),
module_file_spec.GetPath().c_str());
}
}
}
bool ModuleList::ResolveFileAddress(lldb::addr_t vm_addr,
Address &so_addr) const {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
if ((*pos)->ResolveFileAddress(vm_addr, so_addr))
return true;
}
return false;
}
uint32_t
ModuleList::ResolveSymbolContextForAddress(const Address &so_addr,
SymbolContextItem resolve_scope,
SymbolContext &sc) const {
// The address is already section offset so it has a module
uint32_t resolved_flags = 0;
ModuleSP module_sp(so_addr.GetModule());
if (module_sp) {
resolved_flags =
module_sp->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
} else {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
resolved_flags =
(*pos)->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
if (resolved_flags != 0)
break;
}
}
return resolved_flags;
}
uint32_t ModuleList::ResolveSymbolContextForFilePath(
const char *file_path, uint32_t line, bool check_inlines,
SymbolContextItem resolve_scope, SymbolContextList &sc_list) const {
FileSpec file_spec(file_path);
return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
resolve_scope, sc_list);
}
uint32_t ModuleList::ResolveSymbolContextsForFileSpec(
const FileSpec &file_spec, uint32_t line, bool check_inlines,
SymbolContextItem resolve_scope, SymbolContextList &sc_list) const {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos, end = m_modules.end();
for (pos = m_modules.begin(); pos != end; ++pos) {
(*pos)->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
resolve_scope, sc_list);
}
return sc_list.GetSize();
}
size_t ModuleList::GetIndexForModule(const Module *module) const {
if (module) {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
collection::const_iterator pos;
collection::const_iterator begin = m_modules.begin();
collection::const_iterator end = m_modules.end();
for (pos = begin; pos != end; ++pos) {
if ((*pos).get() == module)
return std::distance(begin, pos);
}
}
return LLDB_INVALID_INDEX32;
}
namespace {
struct SharedModuleListInfo {
ModuleList module_list;
ModuleListProperties module_list_properties;
};
}
static SharedModuleListInfo &GetSharedModuleListInfo()
{
static SharedModuleListInfo *g_shared_module_list_info = nullptr;
static llvm::once_flag g_once_flag;
llvm::call_once(g_once_flag, []() {
// NOTE: Intentionally leak the module list so a program doesn't have to
// cleanup all modules and object files as it exits. This just wastes time
// doing a bunch of cleanup that isn't required.
if (g_shared_module_list_info == nullptr)
g_shared_module_list_info = new SharedModuleListInfo();
});
return *g_shared_module_list_info;
}
static ModuleList &GetSharedModuleList() {
return GetSharedModuleListInfo().module_list;
}
ModuleListProperties &ModuleList::GetGlobalModuleListProperties() {
return GetSharedModuleListInfo().module_list_properties;
}
bool ModuleList::ModuleIsInCache(const Module *module_ptr) {
if (module_ptr) {
ModuleList &shared_module_list = GetSharedModuleList();
return shared_module_list.FindModule(module_ptr).get() != nullptr;
}
return false;
}
void ModuleList::FindSharedModules(const ModuleSpec &module_spec,
ModuleList &matching_module_list) {
GetSharedModuleList().FindModules(module_spec, matching_module_list);
}
size_t ModuleList::RemoveOrphanSharedModules(bool mandatory) {
return GetSharedModuleList().RemoveOrphans(mandatory);
}
Status ModuleList::GetSharedModule(const ModuleSpec &module_spec,
ModuleSP &module_sp,
const FileSpecList *module_search_paths_ptr,
ModuleSP *old_module_sp_ptr,
bool *did_create_ptr, bool always_create) {
ModuleList &shared_module_list = GetSharedModuleList();
std::lock_guard<std::recursive_mutex> guard(
shared_module_list.m_modules_mutex);
char path[PATH_MAX];
Status error;
module_sp.reset();
if (did_create_ptr)
*did_create_ptr = false;
if (old_module_sp_ptr)
old_module_sp_ptr->reset();
const UUID *uuid_ptr = module_spec.GetUUIDPtr();
const FileSpec &module_file_spec = module_spec.GetFileSpec();
const ArchSpec &arch = module_spec.GetArchitecture();
// Make sure no one else can try and get or create a module while this
// function is actively working on it by doing an extra lock on the global
// mutex list.
if (!always_create) {
ModuleList matching_module_list;
shared_module_list.FindModules(module_spec, matching_module_list);
const size_t num_matching_modules = matching_module_list.GetSize();
if (num_matching_modules > 0) {
for (size_t module_idx = 0; module_idx < num_matching_modules;
++module_idx) {
module_sp = matching_module_list.GetModuleAtIndex(module_idx);
// Make sure the file for the module hasn't been modified
if (module_sp->FileHasChanged()) {
if (old_module_sp_ptr && !*old_module_sp_ptr)
*old_module_sp_ptr = module_sp;
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_MODULES));
if (log != nullptr)
LLDB_LOGF(log,
"module changed: %p, removing from global module list",
static_cast<void *>(module_sp.get()));
shared_module_list.Remove(module_sp);
module_sp.reset();
} else {
// The module matches and the module was not modified from when it
// was last loaded.
return error;
}
}
}
}
if (module_sp)
return error;
module_sp = std::make_shared<Module>(module_spec);
// Make sure there are a module and an object file since we can specify a
// valid file path with an architecture that might not be in that file. By
// getting the object file we can guarantee that the architecture matches
if (module_sp->GetObjectFile()) {
// If we get in here we got the correct arch, now we just need to verify
// the UUID if one was given
if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
module_sp.reset();
} else {
if (module_sp->GetObjectFile() &&
module_sp->GetObjectFile()->GetType() ==
ObjectFile::eTypeStubLibrary) {
module_sp.reset();
} else {
if (did_create_ptr) {
*did_create_ptr = true;
}
shared_module_list.ReplaceEquivalent(module_sp);
return error;
}
}
} else {
module_sp.reset();
}
if (module_search_paths_ptr) {
const auto num_directories = module_search_paths_ptr->GetSize();
for (size_t idx = 0; idx < num_directories; ++idx) {
auto search_path_spec = module_search_paths_ptr->GetFileSpecAtIndex(idx);
FileSystem::Instance().Resolve(search_path_spec);
namespace fs = llvm::sys::fs;
if (!FileSystem::Instance().IsDirectory(search_path_spec))
continue;
search_path_spec.AppendPathComponent(
module_spec.GetFileSpec().GetFilename().AsCString());
if (!FileSystem::Instance().Exists(search_path_spec))
continue;
auto resolved_module_spec(module_spec);
resolved_module_spec.GetFileSpec() = search_path_spec;
module_sp = std::make_shared<Module>(resolved_module_spec);
if (module_sp->GetObjectFile()) {
// If we get in here we got the correct arch, now we just need to
// verify the UUID if one was given
if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
module_sp.reset();
} else {
if (module_sp->GetObjectFile()->GetType() ==
ObjectFile::eTypeStubLibrary) {
module_sp.reset();
} else {
if (did_create_ptr)
*did_create_ptr = true;
shared_module_list.ReplaceEquivalent(module_sp);
return Status();
}
}
} else {
module_sp.reset();
}
}
}
// Either the file didn't exist where at the path, or no path was given, so
// we now have to use more extreme measures to try and find the appropriate
// module.
// Fixup the incoming path in case the path points to a valid file, yet the
// arch or UUID (if one was passed in) don't match.
ModuleSpec located_binary_modulespec =
Symbols::LocateExecutableObjectFile(module_spec);
// Don't look for the file if it appears to be the same one we already
// checked for above...
if (located_binary_modulespec.GetFileSpec() != module_file_spec) {
if (!FileSystem::Instance().Exists(
located_binary_modulespec.GetFileSpec())) {
located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path));
if (path[0] == '\0')
module_file_spec.GetPath(path, sizeof(path));
// How can this check ever be true? This branch it is false, and we
// haven't modified file_spec.
if (FileSystem::Instance().Exists(
located_binary_modulespec.GetFileSpec())) {
std::string uuid_str;
if (uuid_ptr && uuid_ptr->IsValid())
uuid_str = uuid_ptr->GetAsString();
if (arch.IsValid()) {
if (!uuid_str.empty())
error.SetErrorStringWithFormat(
"'%s' does not contain the %s architecture and UUID %s", path,
arch.GetArchitectureName(), uuid_str.c_str());
else
error.SetErrorStringWithFormat(
"'%s' does not contain the %s architecture.", path,
arch.GetArchitectureName());
}
} else {
error.SetErrorStringWithFormat("'%s' does not exist", path);
}
if (error.Fail())
module_sp.reset();
return error;
}
// Make sure no one else can try and get or create a module while this
// function is actively working on it by doing an extra lock on the global
// mutex list.
ModuleSpec platform_module_spec(module_spec);
platform_module_spec.GetFileSpec() =
located_binary_modulespec.GetFileSpec();
platform_module_spec.GetPlatformFileSpec() =
located_binary_modulespec.GetFileSpec();
platform_module_spec.GetSymbolFileSpec() =
located_binary_modulespec.GetSymbolFileSpec();
ModuleList matching_module_list;
shared_module_list.FindModules(platform_module_spec, matching_module_list);
if (!matching_module_list.IsEmpty()) {
module_sp = matching_module_list.GetModuleAtIndex(0);
// If we didn't have a UUID in mind when looking for the object file,
// then we should make sure the modification time hasn't changed!
if (platform_module_spec.GetUUIDPtr() == nullptr) {
auto file_spec_mod_time = FileSystem::Instance().GetModificationTime(
located_binary_modulespec.GetFileSpec());
if (file_spec_mod_time != llvm::sys::TimePoint<>()) {
if (file_spec_mod_time != module_sp->GetModificationTime()) {
if (old_module_sp_ptr)
*old_module_sp_ptr = module_sp;
shared_module_list.Remove(module_sp);
module_sp.reset();
}
}
}
}
if (!module_sp) {
module_sp = std::make_shared<Module>(platform_module_spec);
// Make sure there are a module and an object file since we can specify a
// valid file path with an architecture that might not be in that file.
// By getting the object file we can guarantee that the architecture
// matches
if (module_sp && module_sp->GetObjectFile()) {
if (module_sp->GetObjectFile()->GetType() ==
ObjectFile::eTypeStubLibrary) {
module_sp.reset();
} else {
if (did_create_ptr)
*did_create_ptr = true;
shared_module_list.ReplaceEquivalent(module_sp);
}
} else {
located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path));
if (located_binary_modulespec.GetFileSpec()) {
if (arch.IsValid())
error.SetErrorStringWithFormat(
"unable to open %s architecture in '%s'",
arch.GetArchitectureName(), path);
else
error.SetErrorStringWithFormat("unable to open '%s'", path);
} else {
std::string uuid_str;
if (uuid_ptr && uuid_ptr->IsValid())
uuid_str = uuid_ptr->GetAsString();
if (!uuid_str.empty())
error.SetErrorStringWithFormat(
"cannot locate a module for UUID '%s'", uuid_str.c_str());
else
error.SetErrorStringWithFormat("cannot locate a module");
}
}
}
}
return error;
}
bool ModuleList::RemoveSharedModule(lldb::ModuleSP &module_sp) {
return GetSharedModuleList().Remove(module_sp);
}
bool ModuleList::RemoveSharedModuleIfOrphaned(const Module *module_ptr) {
return GetSharedModuleList().RemoveIfOrphaned(module_ptr);
}
bool ModuleList::LoadScriptingResourcesInTarget(Target *target,
std::list<Status> &errors,
Stream *feedback_stream,
bool continue_on_error) {
if (!target)
return false;
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
for (auto module : m_modules) {
Status error;
if (module) {
if (!module->LoadScriptingResourceInTarget(target, error,
feedback_stream)) {
if (error.Fail() && error.AsCString()) {
error.SetErrorStringWithFormat("unable to load scripting data for "
"module %s - error reported was %s",
module->GetFileSpec()
.GetFileNameStrippingExtension()
.GetCString(),
error.AsCString());
errors.push_back(error);
if (!continue_on_error)
return false;
}
}
}
}
return errors.empty();
}
void ModuleList::ForEach(
std::function<bool(const ModuleSP &module_sp)> const &callback) const {
std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
for (const auto &module : m_modules) {
// If the callback returns false, then stop iterating and break out
if (!callback(module))
break;
}
}
|