1 //===-- ModuleList.cpp ----------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Core/ModuleList.h"
10 #include "lldb/Core/Module.h"
11 #include "lldb/Core/ModuleSpec.h"
12 #include "lldb/Host/FileSystem.h"
13 #include "lldb/Interpreter/OptionValueFileSpec.h"
14 #include "lldb/Interpreter/OptionValueFileSpecList.h"
15 #include "lldb/Interpreter/OptionValueProperties.h"
16 #include "lldb/Interpreter/Property.h"
17 #include "lldb/Symbol/LocateSymbolFile.h"
18 #include "lldb/Symbol/ObjectFile.h"
19 #include "lldb/Symbol/SymbolContext.h"
20 #include "lldb/Symbol/TypeList.h"
21 #include "lldb/Symbol/VariableList.h"
22 #include "lldb/Utility/ArchSpec.h"
23 #include "lldb/Utility/ConstString.h"
24 #include "lldb/Utility/FileSpecList.h"
25 #include "lldb/Utility/LLDBLog.h"
26 #include "lldb/Utility/Log.h"
27 #include "lldb/Utility/UUID.h"
28 #include "lldb/lldb-defines.h"
31 #include "lldb/Host/windows/PosixApi.h"
34 #include "clang/Driver/Driver.h"
35 #include "llvm/ADT/StringRef.h"
36 #include "llvm/Support/FileSystem.h"
37 #include "llvm/Support/Threading.h"
38 #include "llvm/Support/raw_ostream.h"
46 namespace lldb_private
{
49 namespace lldb_private
{
50 class RegularExpression
;
52 namespace lldb_private
{
55 namespace lldb_private
{
58 namespace lldb_private
{
63 using namespace lldb_private
;
67 #define LLDB_PROPERTIES_modulelist
68 #include "CoreProperties.inc"
71 #define LLDB_PROPERTIES_modulelist
72 #include "CorePropertiesEnum.inc"
77 ModuleListProperties::ModuleListProperties() {
78 m_collection_sp
= std::make_shared
<OptionValueProperties
>("symbols");
79 m_collection_sp
->Initialize(g_modulelist_properties
);
80 m_collection_sp
->SetValueChangedCallback(ePropertySymLinkPaths
,
81 [this] { UpdateSymlinkMappings(); });
83 llvm::SmallString
<128> path
;
84 if (clang::driver::Driver::getDefaultModuleCachePath(path
)) {
85 lldbassert(SetClangModulesCachePath(FileSpec(path
)));
89 if (llvm::sys::path::cache_directory(path
)) {
90 llvm::sys::path::append(path
, "lldb");
91 llvm::sys::path::append(path
, "IndexCache");
92 lldbassert(SetLLDBIndexCachePath(FileSpec(path
)));
97 bool ModuleListProperties::GetEnableExternalLookup() const {
98 const uint32_t idx
= ePropertyEnableExternalLookup
;
99 return GetPropertyAtIndexAs
<bool>(
100 idx
, g_modulelist_properties
[idx
].default_uint_value
!= 0);
103 bool ModuleListProperties::SetEnableExternalLookup(bool new_value
) {
104 return SetPropertyAtIndex(ePropertyEnableExternalLookup
, new_value
);
107 bool ModuleListProperties::GetEnableBackgroundLookup() const {
108 const uint32_t idx
= ePropertyEnableBackgroundLookup
;
109 return GetPropertyAtIndexAs
<bool>(
110 idx
, g_modulelist_properties
[idx
].default_uint_value
!= 0);
113 FileSpec
ModuleListProperties::GetClangModulesCachePath() const {
114 const uint32_t idx
= ePropertyClangModulesCachePath
;
115 return GetPropertyAtIndexAs
<FileSpec
>(idx
, {});
118 bool ModuleListProperties::SetClangModulesCachePath(const FileSpec
&path
) {
119 const uint32_t idx
= ePropertyClangModulesCachePath
;
120 return SetPropertyAtIndex(idx
, path
);
123 FileSpec
ModuleListProperties::GetLLDBIndexCachePath() const {
124 const uint32_t idx
= ePropertyLLDBIndexCachePath
;
125 return GetPropertyAtIndexAs
<FileSpec
>(idx
, {});
128 bool ModuleListProperties::SetLLDBIndexCachePath(const FileSpec
&path
) {
129 const uint32_t idx
= ePropertyLLDBIndexCachePath
;
130 return SetPropertyAtIndex(idx
, path
);
133 bool ModuleListProperties::GetEnableLLDBIndexCache() const {
134 const uint32_t idx
= ePropertyEnableLLDBIndexCache
;
135 return GetPropertyAtIndexAs
<bool>(
136 idx
, g_modulelist_properties
[idx
].default_uint_value
!= 0);
139 bool ModuleListProperties::SetEnableLLDBIndexCache(bool new_value
) {
140 return SetPropertyAtIndex(ePropertyEnableLLDBIndexCache
, new_value
);
143 uint64_t ModuleListProperties::GetLLDBIndexCacheMaxByteSize() {
144 const uint32_t idx
= ePropertyLLDBIndexCacheMaxByteSize
;
145 return GetPropertyAtIndexAs
<uint64_t>(
146 idx
, g_modulelist_properties
[idx
].default_uint_value
);
149 uint64_t ModuleListProperties::GetLLDBIndexCacheMaxPercent() {
150 const uint32_t idx
= ePropertyLLDBIndexCacheMaxPercent
;
151 return GetPropertyAtIndexAs
<uint64_t>(
152 idx
, g_modulelist_properties
[idx
].default_uint_value
);
155 uint64_t ModuleListProperties::GetLLDBIndexCacheExpirationDays() {
156 const uint32_t idx
= ePropertyLLDBIndexCacheExpirationDays
;
157 return GetPropertyAtIndexAs
<uint64_t>(
158 idx
, g_modulelist_properties
[idx
].default_uint_value
);
161 void ModuleListProperties::UpdateSymlinkMappings() {
163 GetPropertyAtIndexAs
<FileSpecList
>(ePropertySymLinkPaths
, {});
164 llvm::sys::ScopedWriter
lock(m_symlink_paths_mutex
);
165 const bool notify
= false;
166 m_symlink_paths
.Clear(notify
);
167 for (FileSpec symlink
: list
) {
169 Status status
= FileSystem::Instance().Readlink(symlink
, resolved
);
170 if (status
.Success())
171 m_symlink_paths
.Append(symlink
.GetPath(), resolved
.GetPath(), notify
);
175 PathMappingList
ModuleListProperties::GetSymlinkMappings() const {
176 llvm::sys::ScopedReader
lock(m_symlink_paths_mutex
);
177 return m_symlink_paths
;
180 bool ModuleListProperties::GetLoadSymbolOnDemand() {
181 const uint32_t idx
= ePropertyLoadSymbolOnDemand
;
182 return GetPropertyAtIndexAs
<bool>(
183 idx
, g_modulelist_properties
[idx
].default_uint_value
!= 0);
186 ModuleList::ModuleList() : m_modules(), m_modules_mutex() {}
188 ModuleList::ModuleList(const ModuleList
&rhs
) : m_modules(), m_modules_mutex() {
189 std::lock_guard
<std::recursive_mutex
> lhs_guard(m_modules_mutex
);
190 std::lock_guard
<std::recursive_mutex
> rhs_guard(rhs
.m_modules_mutex
);
191 m_modules
= rhs
.m_modules
;
194 ModuleList::ModuleList(ModuleList::Notifier
*notifier
)
195 : m_modules(), m_modules_mutex(), m_notifier(notifier
) {}
197 const ModuleList
&ModuleList::operator=(const ModuleList
&rhs
) {
199 std::lock(m_modules_mutex
, rhs
.m_modules_mutex
);
200 std::lock_guard
<std::recursive_mutex
> lhs_guard(m_modules_mutex
,
202 std::lock_guard
<std::recursive_mutex
> rhs_guard(rhs
.m_modules_mutex
,
204 m_modules
= rhs
.m_modules
;
209 ModuleList::~ModuleList() = default;
211 void ModuleList::AppendImpl(const ModuleSP
&module_sp
, bool use_notifier
) {
213 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
214 m_modules
.push_back(module_sp
);
215 if (use_notifier
&& m_notifier
)
216 m_notifier
->NotifyModuleAdded(*this, module_sp
);
220 void ModuleList::Append(const ModuleSP
&module_sp
, bool notify
) {
221 AppendImpl(module_sp
, notify
);
224 void ModuleList::ReplaceEquivalent(
225 const ModuleSP
&module_sp
,
226 llvm::SmallVectorImpl
<lldb::ModuleSP
> *old_modules
) {
228 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
230 // First remove any equivalent modules. Equivalent modules are modules
231 // whose path, platform path and architecture match.
232 ModuleSpec
equivalent_module_spec(module_sp
->GetFileSpec(),
233 module_sp
->GetArchitecture());
234 equivalent_module_spec
.GetPlatformFileSpec() =
235 module_sp
->GetPlatformFileSpec();
238 while (idx
< m_modules
.size()) {
239 ModuleSP
test_module_sp(m_modules
[idx
]);
240 if (test_module_sp
->MatchesModuleSpec(equivalent_module_spec
)) {
242 old_modules
->push_back(test_module_sp
);
243 RemoveImpl(m_modules
.begin() + idx
);
248 // Now add the new module to the list
253 bool ModuleList::AppendIfNeeded(const ModuleSP
&new_module
, bool notify
) {
255 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
256 for (const ModuleSP
&module_sp
: m_modules
) {
257 if (module_sp
.get() == new_module
.get())
258 return false; // Already in the list
260 // Only push module_sp on the list if it wasn't already in there.
261 Append(new_module
, notify
);
267 void ModuleList::Append(const ModuleList
&module_list
) {
268 for (auto pos
: module_list
.m_modules
)
272 bool ModuleList::AppendIfNeeded(const ModuleList
&module_list
) {
274 for (auto pos
: module_list
.m_modules
) {
275 if (AppendIfNeeded(pos
))
281 bool ModuleList::RemoveImpl(const ModuleSP
&module_sp
, bool use_notifier
) {
283 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
284 collection::iterator pos
, end
= m_modules
.end();
285 for (pos
= m_modules
.begin(); pos
!= end
; ++pos
) {
286 if (pos
->get() == module_sp
.get()) {
287 m_modules
.erase(pos
);
288 if (use_notifier
&& m_notifier
)
289 m_notifier
->NotifyModuleRemoved(*this, module_sp
);
297 ModuleList::collection::iterator
298 ModuleList::RemoveImpl(ModuleList::collection::iterator pos
,
300 ModuleSP
module_sp(*pos
);
301 collection::iterator retval
= m_modules
.erase(pos
);
302 if (use_notifier
&& m_notifier
)
303 m_notifier
->NotifyModuleRemoved(*this, module_sp
);
307 bool ModuleList::Remove(const ModuleSP
&module_sp
, bool notify
) {
308 return RemoveImpl(module_sp
, notify
);
311 bool ModuleList::ReplaceModule(const lldb::ModuleSP
&old_module_sp
,
312 const lldb::ModuleSP
&new_module_sp
) {
313 if (!RemoveImpl(old_module_sp
, false))
315 AppendImpl(new_module_sp
, false);
317 m_notifier
->NotifyModuleUpdated(*this, old_module_sp
, new_module_sp
);
321 bool ModuleList::RemoveIfOrphaned(const Module
*module_ptr
) {
323 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
324 collection::iterator pos
, end
= m_modules
.end();
325 for (pos
= m_modules
.begin(); pos
!= end
; ++pos
) {
326 if (pos
->get() == module_ptr
) {
328 pos
= RemoveImpl(pos
);
338 size_t ModuleList::RemoveOrphans(bool mandatory
) {
339 std::unique_lock
<std::recursive_mutex
> lock(m_modules_mutex
, std::defer_lock
);
344 // Not mandatory, remove orphans if we can get the mutex
345 if (!lock
.try_lock())
348 size_t remove_count
= 0;
349 // Modules might hold shared pointers to other modules, so removing one
350 // module might make other modules orphans. Keep removing modules until
351 // there are no further modules that can be removed.
352 bool made_progress
= true;
353 while (made_progress
) {
354 // Keep track if we make progress this iteration.
355 made_progress
= false;
356 collection::iterator pos
= m_modules
.begin();
357 while (pos
!= m_modules
.end()) {
359 pos
= RemoveImpl(pos
);
361 // We did make progress.
362 made_progress
= true;
371 size_t ModuleList::Remove(ModuleList
&module_list
) {
372 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
373 size_t num_removed
= 0;
374 collection::iterator pos
, end
= module_list
.m_modules
.end();
375 for (pos
= module_list
.m_modules
.begin(); pos
!= end
; ++pos
) {
376 if (Remove(*pos
, false /* notify */))
380 m_notifier
->NotifyModulesRemoved(module_list
);
384 void ModuleList::Clear() { ClearImpl(); }
386 void ModuleList::Destroy() { ClearImpl(); }
388 void ModuleList::ClearImpl(bool use_notifier
) {
389 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
390 if (use_notifier
&& m_notifier
)
391 m_notifier
->NotifyWillClearList(*this);
395 Module
*ModuleList::GetModulePointerAtIndex(size_t idx
) const {
396 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
397 if (idx
< m_modules
.size())
398 return m_modules
[idx
].get();
402 ModuleSP
ModuleList::GetModuleAtIndex(size_t idx
) const {
403 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
404 return GetModuleAtIndexUnlocked(idx
);
407 ModuleSP
ModuleList::GetModuleAtIndexUnlocked(size_t idx
) const {
409 if (idx
< m_modules
.size())
410 module_sp
= m_modules
[idx
];
414 void ModuleList::FindFunctions(ConstString name
,
415 FunctionNameType name_type_mask
,
416 const ModuleFunctionSearchOptions
&options
,
417 SymbolContextList
&sc_list
) const {
418 const size_t old_size
= sc_list
.GetSize();
420 if (name_type_mask
& eFunctionNameTypeAuto
) {
421 Module::LookupInfo
lookup_info(name
, name_type_mask
, eLanguageTypeUnknown
);
423 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
424 for (const ModuleSP
&module_sp
: m_modules
) {
425 module_sp
->FindFunctions(lookup_info
, CompilerDeclContext(), options
,
429 const size_t new_size
= sc_list
.GetSize();
431 if (old_size
< new_size
)
432 lookup_info
.Prune(sc_list
, old_size
);
434 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
435 for (const ModuleSP
&module_sp
: m_modules
) {
436 module_sp
->FindFunctions(name
, CompilerDeclContext(), name_type_mask
,
442 void ModuleList::FindFunctionSymbols(ConstString name
,
443 lldb::FunctionNameType name_type_mask
,
444 SymbolContextList
&sc_list
) {
445 const size_t old_size
= sc_list
.GetSize();
447 if (name_type_mask
& eFunctionNameTypeAuto
) {
448 Module::LookupInfo
lookup_info(name
, name_type_mask
, eLanguageTypeUnknown
);
450 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
451 for (const ModuleSP
&module_sp
: m_modules
) {
452 module_sp
->FindFunctionSymbols(lookup_info
.GetLookupName(),
453 lookup_info
.GetNameTypeMask(), sc_list
);
456 const size_t new_size
= sc_list
.GetSize();
458 if (old_size
< new_size
)
459 lookup_info
.Prune(sc_list
, old_size
);
461 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
462 for (const ModuleSP
&module_sp
: m_modules
) {
463 module_sp
->FindFunctionSymbols(name
, name_type_mask
, sc_list
);
468 void ModuleList::FindFunctions(const RegularExpression
&name
,
469 const ModuleFunctionSearchOptions
&options
,
470 SymbolContextList
&sc_list
) {
471 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
472 for (const ModuleSP
&module_sp
: m_modules
)
473 module_sp
->FindFunctions(name
, options
, sc_list
);
476 void ModuleList::FindCompileUnits(const FileSpec
&path
,
477 SymbolContextList
&sc_list
) const {
478 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
479 for (const ModuleSP
&module_sp
: m_modules
)
480 module_sp
->FindCompileUnits(path
, sc_list
);
483 void ModuleList::FindGlobalVariables(ConstString name
, size_t max_matches
,
484 VariableList
&variable_list
) const {
485 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
486 for (const ModuleSP
&module_sp
: m_modules
) {
487 module_sp
->FindGlobalVariables(name
, CompilerDeclContext(), max_matches
,
492 void ModuleList::FindGlobalVariables(const RegularExpression
®ex
,
494 VariableList
&variable_list
) const {
495 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
496 for (const ModuleSP
&module_sp
: m_modules
)
497 module_sp
->FindGlobalVariables(regex
, max_matches
, variable_list
);
500 void ModuleList::FindSymbolsWithNameAndType(ConstString name
,
501 SymbolType symbol_type
,
502 SymbolContextList
&sc_list
) const {
503 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
504 for (const ModuleSP
&module_sp
: m_modules
)
505 module_sp
->FindSymbolsWithNameAndType(name
, symbol_type
, sc_list
);
508 void ModuleList::FindSymbolsMatchingRegExAndType(
509 const RegularExpression
®ex
, lldb::SymbolType symbol_type
,
510 SymbolContextList
&sc_list
) const {
511 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
512 for (const ModuleSP
&module_sp
: m_modules
)
513 module_sp
->FindSymbolsMatchingRegExAndType(regex
, symbol_type
, sc_list
);
516 void ModuleList::FindModules(const ModuleSpec
&module_spec
,
517 ModuleList
&matching_module_list
) const {
518 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
519 for (const ModuleSP
&module_sp
: m_modules
) {
520 if (module_sp
->MatchesModuleSpec(module_spec
))
521 matching_module_list
.Append(module_sp
);
525 ModuleSP
ModuleList::FindModule(const Module
*module_ptr
) const {
528 // Scope for "locker"
530 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
531 collection::const_iterator pos
, end
= m_modules
.end();
533 for (pos
= m_modules
.begin(); pos
!= end
; ++pos
) {
534 if ((*pos
).get() == module_ptr
) {
543 ModuleSP
ModuleList::FindModule(const UUID
&uuid
) const {
546 if (uuid
.IsValid()) {
547 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
548 collection::const_iterator pos
, end
= m_modules
.end();
550 for (pos
= m_modules
.begin(); pos
!= end
; ++pos
) {
551 if ((*pos
)->GetUUID() == uuid
) {
560 void ModuleList::FindTypes(Module
*search_first
, ConstString name
,
561 bool name_is_fully_qualified
, size_t max_matches
,
562 llvm::DenseSet
<SymbolFile
*> &searched_symbol_files
,
563 TypeList
&types
) const {
564 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
566 collection::const_iterator pos
, end
= m_modules
.end();
568 for (pos
= m_modules
.begin(); pos
!= end
; ++pos
) {
569 if (search_first
== pos
->get()) {
570 search_first
->FindTypes(name
, name_is_fully_qualified
, max_matches
,
571 searched_symbol_files
, types
);
573 if (types
.GetSize() >= max_matches
)
579 for (pos
= m_modules
.begin(); pos
!= end
; ++pos
) {
580 // Search the module if the module is not equal to the one in the symbol
581 // context "sc". If "sc" contains a empty module shared pointer, then the
582 // comparison will always be true (valid_module_ptr != nullptr).
583 if (search_first
!= pos
->get())
584 (*pos
)->FindTypes(name
, name_is_fully_qualified
, max_matches
,
585 searched_symbol_files
, types
);
587 if (types
.GetSize() >= max_matches
)
592 bool ModuleList::FindSourceFile(const FileSpec
&orig_spec
,
593 FileSpec
&new_spec
) const {
594 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
595 for (const ModuleSP
&module_sp
: m_modules
) {
596 if (module_sp
->FindSourceFile(orig_spec
, new_spec
))
602 void ModuleList::FindAddressesForLine(const lldb::TargetSP target_sp
,
603 const FileSpec
&file
, uint32_t line
,
605 std::vector
<Address
> &output_local
,
606 std::vector
<Address
> &output_extern
) {
607 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
608 for (const ModuleSP
&module_sp
: m_modules
) {
609 module_sp
->FindAddressesForLine(target_sp
, file
, line
, function
,
610 output_local
, output_extern
);
614 ModuleSP
ModuleList::FindFirstModule(const ModuleSpec
&module_spec
) const {
616 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
617 collection::const_iterator pos
, end
= m_modules
.end();
618 for (pos
= m_modules
.begin(); pos
!= end
; ++pos
) {
619 ModuleSP
module_sp(*pos
);
620 if (module_sp
->MatchesModuleSpec(module_spec
))
626 size_t ModuleList::GetSize() const {
629 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
630 size
= m_modules
.size();
635 void ModuleList::Dump(Stream
*s
) const {
636 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
637 for (const ModuleSP
&module_sp
: m_modules
)
641 void ModuleList::LogUUIDAndPaths(Log
*log
, const char *prefix_cstr
) {
642 if (log
!= nullptr) {
643 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
644 collection::const_iterator pos
, begin
= m_modules
.begin(),
645 end
= m_modules
.end();
646 for (pos
= begin
; pos
!= end
; ++pos
) {
647 Module
*module
= pos
->get();
648 const FileSpec
&module_file_spec
= module
->GetFileSpec();
649 LLDB_LOGF(log
, "%s[%u] %s (%s) \"%s\"", prefix_cstr
? prefix_cstr
: "",
650 (uint32_t)std::distance(begin
, pos
),
651 module
->GetUUID().GetAsString().c_str(),
652 module
->GetArchitecture().GetArchitectureName(),
653 module_file_spec
.GetPath().c_str());
658 bool ModuleList::ResolveFileAddress(lldb::addr_t vm_addr
,
659 Address
&so_addr
) const {
660 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
661 for (const ModuleSP
&module_sp
: m_modules
) {
662 if (module_sp
->ResolveFileAddress(vm_addr
, so_addr
))
670 ModuleList::ResolveSymbolContextForAddress(const Address
&so_addr
,
671 SymbolContextItem resolve_scope
,
672 SymbolContext
&sc
) const {
673 // The address is already section offset so it has a module
674 uint32_t resolved_flags
= 0;
675 ModuleSP
module_sp(so_addr
.GetModule());
678 module_sp
->ResolveSymbolContextForAddress(so_addr
, resolve_scope
, sc
);
680 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
681 collection::const_iterator pos
, end
= m_modules
.end();
682 for (pos
= m_modules
.begin(); pos
!= end
; ++pos
) {
684 (*pos
)->ResolveSymbolContextForAddress(so_addr
, resolve_scope
, sc
);
685 if (resolved_flags
!= 0)
690 return resolved_flags
;
693 uint32_t ModuleList::ResolveSymbolContextForFilePath(
694 const char *file_path
, uint32_t line
, bool check_inlines
,
695 SymbolContextItem resolve_scope
, SymbolContextList
&sc_list
) const {
696 FileSpec
file_spec(file_path
);
697 return ResolveSymbolContextsForFileSpec(file_spec
, line
, check_inlines
,
698 resolve_scope
, sc_list
);
701 uint32_t ModuleList::ResolveSymbolContextsForFileSpec(
702 const FileSpec
&file_spec
, uint32_t line
, bool check_inlines
,
703 SymbolContextItem resolve_scope
, SymbolContextList
&sc_list
) const {
704 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
705 for (const ModuleSP
&module_sp
: m_modules
) {
706 module_sp
->ResolveSymbolContextsForFileSpec(file_spec
, line
, check_inlines
,
707 resolve_scope
, sc_list
);
710 return sc_list
.GetSize();
713 size_t ModuleList::GetIndexForModule(const Module
*module
) const {
715 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
716 collection::const_iterator pos
;
717 collection::const_iterator begin
= m_modules
.begin();
718 collection::const_iterator end
= m_modules
.end();
719 for (pos
= begin
; pos
!= end
; ++pos
) {
720 if ((*pos
).get() == module
)
721 return std::distance(begin
, pos
);
724 return LLDB_INVALID_INDEX32
;
728 struct SharedModuleListInfo
{
729 ModuleList module_list
;
730 ModuleListProperties module_list_properties
;
733 static SharedModuleListInfo
&GetSharedModuleListInfo()
735 static SharedModuleListInfo
*g_shared_module_list_info
= nullptr;
736 static llvm::once_flag g_once_flag
;
737 llvm::call_once(g_once_flag
, []() {
738 // NOTE: Intentionally leak the module list so a program doesn't have to
739 // cleanup all modules and object files as it exits. This just wastes time
740 // doing a bunch of cleanup that isn't required.
741 if (g_shared_module_list_info
== nullptr)
742 g_shared_module_list_info
= new SharedModuleListInfo();
744 return *g_shared_module_list_info
;
747 static ModuleList
&GetSharedModuleList() {
748 return GetSharedModuleListInfo().module_list
;
751 ModuleListProperties
&ModuleList::GetGlobalModuleListProperties() {
752 return GetSharedModuleListInfo().module_list_properties
;
755 bool ModuleList::ModuleIsInCache(const Module
*module_ptr
) {
757 ModuleList
&shared_module_list
= GetSharedModuleList();
758 return shared_module_list
.FindModule(module_ptr
).get() != nullptr;
763 void ModuleList::FindSharedModules(const ModuleSpec
&module_spec
,
764 ModuleList
&matching_module_list
) {
765 GetSharedModuleList().FindModules(module_spec
, matching_module_list
);
768 lldb::ModuleSP
ModuleList::FindSharedModule(const UUID
&uuid
) {
769 return GetSharedModuleList().FindModule(uuid
);
772 size_t ModuleList::RemoveOrphanSharedModules(bool mandatory
) {
773 return GetSharedModuleList().RemoveOrphans(mandatory
);
777 ModuleList::GetSharedModule(const ModuleSpec
&module_spec
, ModuleSP
&module_sp
,
778 const FileSpecList
*module_search_paths_ptr
,
779 llvm::SmallVectorImpl
<lldb::ModuleSP
> *old_modules
,
780 bool *did_create_ptr
, bool always_create
) {
781 ModuleList
&shared_module_list
= GetSharedModuleList();
782 std::lock_guard
<std::recursive_mutex
> guard(
783 shared_module_list
.m_modules_mutex
);
791 *did_create_ptr
= false;
793 const UUID
*uuid_ptr
= module_spec
.GetUUIDPtr();
794 const FileSpec
&module_file_spec
= module_spec
.GetFileSpec();
795 const ArchSpec
&arch
= module_spec
.GetArchitecture();
797 // Make sure no one else can try and get or create a module while this
798 // function is actively working on it by doing an extra lock on the global
800 if (!always_create
) {
801 ModuleList matching_module_list
;
802 shared_module_list
.FindModules(module_spec
, matching_module_list
);
803 const size_t num_matching_modules
= matching_module_list
.GetSize();
805 if (num_matching_modules
> 0) {
806 for (size_t module_idx
= 0; module_idx
< num_matching_modules
;
808 module_sp
= matching_module_list
.GetModuleAtIndex(module_idx
);
810 // Make sure the file for the module hasn't been modified
811 if (module_sp
->FileHasChanged()) {
813 old_modules
->push_back(module_sp
);
815 Log
*log
= GetLog(LLDBLog::Modules
);
818 log
, "%p '%s' module changed: removing from global module list",
819 static_cast<void *>(module_sp
.get()),
820 module_sp
->GetFileSpec().GetFilename().GetCString());
822 shared_module_list
.Remove(module_sp
);
825 // The module matches and the module was not modified from when it
836 module_sp
= std::make_shared
<Module
>(module_spec
);
837 // Make sure there are a module and an object file since we can specify a
838 // valid file path with an architecture that might not be in that file. By
839 // getting the object file we can guarantee that the architecture matches
840 if (module_sp
->GetObjectFile()) {
841 // If we get in here we got the correct arch, now we just need to verify
842 // the UUID if one was given
843 if (uuid_ptr
&& *uuid_ptr
!= module_sp
->GetUUID()) {
846 if (module_sp
->GetObjectFile() &&
847 module_sp
->GetObjectFile()->GetType() ==
848 ObjectFile::eTypeStubLibrary
) {
851 if (did_create_ptr
) {
852 *did_create_ptr
= true;
855 shared_module_list
.ReplaceEquivalent(module_sp
, old_modules
);
863 if (module_search_paths_ptr
) {
864 const auto num_directories
= module_search_paths_ptr
->GetSize();
865 for (size_t idx
= 0; idx
< num_directories
; ++idx
) {
866 auto search_path_spec
= module_search_paths_ptr
->GetFileSpecAtIndex(idx
);
867 FileSystem::Instance().Resolve(search_path_spec
);
868 namespace fs
= llvm::sys::fs
;
869 if (!FileSystem::Instance().IsDirectory(search_path_spec
))
871 search_path_spec
.AppendPathComponent(
872 module_spec
.GetFileSpec().GetFilename().GetStringRef());
873 if (!FileSystem::Instance().Exists(search_path_spec
))
876 auto resolved_module_spec(module_spec
);
877 resolved_module_spec
.GetFileSpec() = search_path_spec
;
878 module_sp
= std::make_shared
<Module
>(resolved_module_spec
);
879 if (module_sp
->GetObjectFile()) {
880 // If we get in here we got the correct arch, now we just need to
881 // verify the UUID if one was given
882 if (uuid_ptr
&& *uuid_ptr
!= module_sp
->GetUUID()) {
885 if (module_sp
->GetObjectFile()->GetType() ==
886 ObjectFile::eTypeStubLibrary
) {
890 *did_create_ptr
= true;
892 shared_module_list
.ReplaceEquivalent(module_sp
, old_modules
);
902 // Either the file didn't exist where at the path, or no path was given, so
903 // we now have to use more extreme measures to try and find the appropriate
906 // Fixup the incoming path in case the path points to a valid file, yet the
907 // arch or UUID (if one was passed in) don't match.
908 ModuleSpec located_binary_modulespec
=
909 Symbols::LocateExecutableObjectFile(module_spec
);
911 // Don't look for the file if it appears to be the same one we already
912 // checked for above...
913 if (located_binary_modulespec
.GetFileSpec() != module_file_spec
) {
914 if (!FileSystem::Instance().Exists(
915 located_binary_modulespec
.GetFileSpec())) {
916 located_binary_modulespec
.GetFileSpec().GetPath(path
, sizeof(path
));
918 module_file_spec
.GetPath(path
, sizeof(path
));
919 // How can this check ever be true? This branch it is false, and we
920 // haven't modified file_spec.
921 if (FileSystem::Instance().Exists(
922 located_binary_modulespec
.GetFileSpec())) {
923 std::string uuid_str
;
924 if (uuid_ptr
&& uuid_ptr
->IsValid())
925 uuid_str
= uuid_ptr
->GetAsString();
927 if (arch
.IsValid()) {
928 if (!uuid_str
.empty())
929 error
.SetErrorStringWithFormat(
930 "'%s' does not contain the %s architecture and UUID %s", path
,
931 arch
.GetArchitectureName(), uuid_str
.c_str());
933 error
.SetErrorStringWithFormat(
934 "'%s' does not contain the %s architecture.", path
,
935 arch
.GetArchitectureName());
938 error
.SetErrorStringWithFormat("'%s' does not exist", path
);
945 // Make sure no one else can try and get or create a module while this
946 // function is actively working on it by doing an extra lock on the global
948 ModuleSpec
platform_module_spec(module_spec
);
949 platform_module_spec
.GetFileSpec() =
950 located_binary_modulespec
.GetFileSpec();
951 platform_module_spec
.GetPlatformFileSpec() =
952 located_binary_modulespec
.GetFileSpec();
953 platform_module_spec
.GetSymbolFileSpec() =
954 located_binary_modulespec
.GetSymbolFileSpec();
955 ModuleList matching_module_list
;
956 shared_module_list
.FindModules(platform_module_spec
, matching_module_list
);
957 if (!matching_module_list
.IsEmpty()) {
958 module_sp
= matching_module_list
.GetModuleAtIndex(0);
960 // If we didn't have a UUID in mind when looking for the object file,
961 // then we should make sure the modification time hasn't changed!
962 if (platform_module_spec
.GetUUIDPtr() == nullptr) {
963 auto file_spec_mod_time
= FileSystem::Instance().GetModificationTime(
964 located_binary_modulespec
.GetFileSpec());
965 if (file_spec_mod_time
!= llvm::sys::TimePoint
<>()) {
966 if (file_spec_mod_time
!= module_sp
->GetModificationTime()) {
968 old_modules
->push_back(module_sp
);
969 shared_module_list
.Remove(module_sp
);
977 module_sp
= std::make_shared
<Module
>(platform_module_spec
);
978 // Make sure there are a module and an object file since we can specify a
979 // valid file path with an architecture that might not be in that file.
980 // By getting the object file we can guarantee that the architecture
982 if (module_sp
&& module_sp
->GetObjectFile()) {
983 if (module_sp
->GetObjectFile()->GetType() ==
984 ObjectFile::eTypeStubLibrary
) {
988 *did_create_ptr
= true;
990 shared_module_list
.ReplaceEquivalent(module_sp
, old_modules
);
993 located_binary_modulespec
.GetFileSpec().GetPath(path
, sizeof(path
));
995 if (located_binary_modulespec
.GetFileSpec()) {
997 error
.SetErrorStringWithFormat(
998 "unable to open %s architecture in '%s'",
999 arch
.GetArchitectureName(), path
);
1001 error
.SetErrorStringWithFormat("unable to open '%s'", path
);
1003 std::string uuid_str
;
1004 if (uuid_ptr
&& uuid_ptr
->IsValid())
1005 uuid_str
= uuid_ptr
->GetAsString();
1007 if (!uuid_str
.empty())
1008 error
.SetErrorStringWithFormat(
1009 "cannot locate a module for UUID '%s'", uuid_str
.c_str());
1011 error
.SetErrorString("cannot locate a module");
1020 bool ModuleList::RemoveSharedModule(lldb::ModuleSP
&module_sp
) {
1021 return GetSharedModuleList().Remove(module_sp
);
1024 bool ModuleList::RemoveSharedModuleIfOrphaned(const Module
*module_ptr
) {
1025 return GetSharedModuleList().RemoveIfOrphaned(module_ptr
);
1028 bool ModuleList::LoadScriptingResourcesInTarget(Target
*target
,
1029 std::list
<Status
> &errors
,
1030 Stream
&feedback_stream
,
1031 bool continue_on_error
) {
1034 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
1035 for (auto module
: m_modules
) {
1038 if (!module
->LoadScriptingResourceInTarget(target
, error
,
1040 if (error
.Fail() && error
.AsCString()) {
1041 error
.SetErrorStringWithFormat("unable to load scripting data for "
1042 "module %s - error reported was %s",
1043 module
->GetFileSpec()
1044 .GetFileNameStrippingExtension()
1047 errors
.push_back(error
);
1049 if (!continue_on_error
)
1055 return errors
.empty();
1058 void ModuleList::ForEach(
1059 std::function
<bool(const ModuleSP
&module_sp
)> const &callback
) const {
1060 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
1061 for (const auto &module_sp
: m_modules
) {
1062 assert(module_sp
!= nullptr);
1063 // If the callback returns false, then stop iterating and break out
1064 if (!callback(module_sp
))
1069 bool ModuleList::AnyOf(
1070 std::function
<bool(lldb_private::Module
&module_sp
)> const &callback
)
1072 std::lock_guard
<std::recursive_mutex
> guard(m_modules_mutex
);
1073 for (const auto &module_sp
: m_modules
) {
1074 assert(module_sp
!= nullptr);
1075 if (callback(*module_sp
))
1083 void ModuleList::Swap(ModuleList
&other
) {
1084 // scoped_lock locks both mutexes at once.
1085 std::scoped_lock
<std::recursive_mutex
, std::recursive_mutex
> lock(
1086 m_modules_mutex
, other
.m_modules_mutex
);
1087 m_modules
.swap(other
.m_modules
);