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/FileSpecList.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/ModuleSpec.h"
13 #include "lldb/Host/FileSystem.h"
14 #include "lldb/Interpreter/OptionValueFileSpec.h"
15 #include "lldb/Interpreter/OptionValueFileSpecList.h"
16 #include "lldb/Interpreter/OptionValueProperties.h"
17 #include "lldb/Interpreter/Property.h"
18 #include "lldb/Symbol/LocateSymbolFile.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Symbol/SymbolContext.h"
21 #include "lldb/Symbol/TypeList.h"
22 #include "lldb/Symbol/VariableList.h"
23 #include "lldb/Utility/ArchSpec.h"
24 #include "lldb/Utility/ConstString.h"
25 #include "lldb/Utility/Log.h"
26 #include "lldb/Utility/Logging.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() {
79 std::make_shared<OptionValueProperties>(ConstString("symbols"));
80 m_collection_sp->Initialize(g_modulelist_properties);
81 m_collection_sp->SetValueChangedCallback(ePropertySymLinkPaths,
82 [this] { UpdateSymlinkMappings(); });
84 llvm::SmallString<128> path;
85 clang::driver::Driver::getDefaultModuleCachePath(path);
86 SetClangModulesCachePath(path);
89 bool ModuleListProperties::GetEnableExternalLookup() const {
90 const uint32_t idx = ePropertyEnableExternalLookup;
91 return m_collection_sp->GetPropertyAtIndexAsBoolean(
92 nullptr, idx, g_modulelist_properties[idx].default_uint_value != 0);
95 bool ModuleListProperties::SetEnableExternalLookup(bool new_value) {
96 return m_collection_sp->SetPropertyAtIndexAsBoolean(
97 nullptr, ePropertyEnableExternalLookup, new_value);
100 FileSpec ModuleListProperties::GetClangModulesCachePath() const {
101 return m_collection_sp
102 ->GetPropertyAtIndexAsOptionValueFileSpec(nullptr, false,
103 ePropertyClangModulesCachePath)
107 bool ModuleListProperties::SetClangModulesCachePath(llvm::StringRef path) {
108 return m_collection_sp->SetPropertyAtIndexAsString(
109 nullptr, ePropertyClangModulesCachePath, path);
112 void ModuleListProperties::UpdateSymlinkMappings() {
113 FileSpecList list = m_collection_sp
114 ->GetPropertyAtIndexAsOptionValueFileSpecList(
115 nullptr, false, ePropertySymLinkPaths)
117 llvm::sys::ScopedWriter lock(m_symlink_paths_mutex);
118 const bool notify = false;
119 m_symlink_paths.Clear(notify);
120 for (FileSpec symlink : list) {
122 Status status = FileSystem::Instance().Readlink(symlink, resolved);
123 if (status.Success())
124 m_symlink_paths.Append(ConstString(symlink.GetPath()),
125 ConstString(resolved.GetPath()), notify);
129 PathMappingList ModuleListProperties::GetSymlinkMappings() const {
130 llvm::sys::ScopedReader lock(m_symlink_paths_mutex);
131 return m_symlink_paths;
134 ModuleList::ModuleList()
135 : m_modules(), m_modules_mutex(), m_notifier(nullptr) {}
137 ModuleList::ModuleList(const ModuleList &rhs)
138 : m_modules(), m_modules_mutex(), m_notifier(nullptr) {
139 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
140 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
141 m_modules = rhs.m_modules;
144 ModuleList::ModuleList(ModuleList::Notifier *notifier)
145 : m_modules(), m_modules_mutex(), m_notifier(notifier) {}
147 const ModuleList &ModuleList::operator=(const ModuleList &rhs) {
149 std::lock(m_modules_mutex, rhs.m_modules_mutex);
150 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex,
152 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex,
154 m_modules = rhs.m_modules;
159 ModuleList::~ModuleList() = default;
161 void ModuleList::AppendImpl(const ModuleSP &module_sp, bool use_notifier) {
163 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
164 m_modules.push_back(module_sp);
165 if (use_notifier && m_notifier)
166 m_notifier->NotifyModuleAdded(*this, module_sp);
170 void ModuleList::Append(const ModuleSP &module_sp, bool notify) {
171 AppendImpl(module_sp, notify);
174 void ModuleList::ReplaceEquivalent(
175 const ModuleSP &module_sp,
176 llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules) {
178 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
180 // First remove any equivalent modules. Equivalent modules are modules
181 // whose path, platform path and architecture match.
182 ModuleSpec equivalent_module_spec(module_sp->GetFileSpec(),
183 module_sp->GetArchitecture());
184 equivalent_module_spec.GetPlatformFileSpec() =
185 module_sp->GetPlatformFileSpec();
188 while (idx < m_modules.size()) {
189 ModuleSP test_module_sp(m_modules[idx]);
190 if (test_module_sp->MatchesModuleSpec(equivalent_module_spec)) {
192 old_modules->push_back(test_module_sp);
193 RemoveImpl(m_modules.begin() + idx);
198 // Now add the new module to the list
203 bool ModuleList::AppendIfNeeded(const ModuleSP &module_sp, bool notify) {
205 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
206 collection::iterator pos, end = m_modules.end();
207 for (pos = m_modules.begin(); pos != end; ++pos) {
208 if (pos->get() == module_sp.get())
209 return false; // Already in the list
211 // Only push module_sp on the list if it wasn't already in there.
212 Append(module_sp, notify);
218 void ModuleList::Append(const ModuleList &module_list) {
219 for (auto pos : module_list.m_modules)
223 bool ModuleList::AppendIfNeeded(const ModuleList &module_list) {
225 for (auto pos : module_list.m_modules) {
226 if (AppendIfNeeded(pos))
232 bool ModuleList::RemoveImpl(const ModuleSP &module_sp, bool use_notifier) {
234 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
235 collection::iterator pos, end = m_modules.end();
236 for (pos = m_modules.begin(); pos != end; ++pos) {
237 if (pos->get() == module_sp.get()) {
238 m_modules.erase(pos);
239 if (use_notifier && m_notifier)
240 m_notifier->NotifyModuleRemoved(*this, module_sp);
248 ModuleList::collection::iterator
249 ModuleList::RemoveImpl(ModuleList::collection::iterator pos,
251 ModuleSP module_sp(*pos);
252 collection::iterator retval = m_modules.erase(pos);
253 if (use_notifier && m_notifier)
254 m_notifier->NotifyModuleRemoved(*this, module_sp);
258 bool ModuleList::Remove(const ModuleSP &module_sp, bool notify) {
259 return RemoveImpl(module_sp, notify);
262 bool ModuleList::ReplaceModule(const lldb::ModuleSP &old_module_sp,
263 const lldb::ModuleSP &new_module_sp) {
264 if (!RemoveImpl(old_module_sp, false))
266 AppendImpl(new_module_sp, false);
268 m_notifier->NotifyModuleUpdated(*this, old_module_sp, new_module_sp);
272 bool ModuleList::RemoveIfOrphaned(const Module *module_ptr) {
274 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
275 collection::iterator pos, end = m_modules.end();
276 for (pos = m_modules.begin(); pos != end; ++pos) {
277 if (pos->get() == module_ptr) {
279 pos = RemoveImpl(pos);
289 size_t ModuleList::RemoveOrphans(bool mandatory) {
290 std::unique_lock<std::recursive_mutex> lock(m_modules_mutex, std::defer_lock);
295 // Not mandatory, remove orphans if we can get the mutex
296 if (!lock.try_lock())
299 collection::iterator pos = m_modules.begin();
300 size_t remove_count = 0;
301 while (pos != m_modules.end()) {
303 pos = RemoveImpl(pos);
312 size_t ModuleList::Remove(ModuleList &module_list) {
313 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
314 size_t num_removed = 0;
315 collection::iterator pos, end = module_list.m_modules.end();
316 for (pos = module_list.m_modules.begin(); pos != end; ++pos) {
317 if (Remove(*pos, false /* notify */))
321 m_notifier->NotifyModulesRemoved(module_list);
325 void ModuleList::Clear() { ClearImpl(); }
327 void ModuleList::Destroy() { ClearImpl(); }
329 void ModuleList::ClearImpl(bool use_notifier) {
330 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
331 if (use_notifier && m_notifier)
332 m_notifier->NotifyWillClearList(*this);
336 Module *ModuleList::GetModulePointerAtIndex(size_t idx) const {
337 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
338 return GetModulePointerAtIndexUnlocked(idx);
341 Module *ModuleList::GetModulePointerAtIndexUnlocked(size_t idx) const {
342 if (idx < m_modules.size())
343 return m_modules[idx].get();
347 ModuleSP ModuleList::GetModuleAtIndex(size_t idx) const {
348 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
349 return GetModuleAtIndexUnlocked(idx);
352 ModuleSP ModuleList::GetModuleAtIndexUnlocked(size_t idx) const {
354 if (idx < m_modules.size())
355 module_sp = m_modules[idx];
359 void ModuleList::FindFunctions(ConstString name,
360 FunctionNameType name_type_mask,
361 bool include_symbols, bool include_inlines,
362 SymbolContextList &sc_list) const {
363 const size_t old_size = sc_list.GetSize();
365 if (name_type_mask & eFunctionNameTypeAuto) {
366 Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
368 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
369 collection::const_iterator pos, end = m_modules.end();
370 for (pos = m_modules.begin(); pos != end; ++pos) {
371 (*pos)->FindFunctions(lookup_info.GetLookupName(), CompilerDeclContext(),
372 lookup_info.GetNameTypeMask(), include_symbols,
373 include_inlines, sc_list);
376 const size_t new_size = sc_list.GetSize();
378 if (old_size < new_size)
379 lookup_info.Prune(sc_list, old_size);
381 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
382 collection::const_iterator pos, end = m_modules.end();
383 for (pos = m_modules.begin(); pos != end; ++pos) {
384 (*pos)->FindFunctions(name, CompilerDeclContext(), name_type_mask,
385 include_symbols, include_inlines, sc_list);
390 void ModuleList::FindFunctionSymbols(ConstString name,
391 lldb::FunctionNameType name_type_mask,
392 SymbolContextList &sc_list) {
393 const size_t old_size = sc_list.GetSize();
395 if (name_type_mask & eFunctionNameTypeAuto) {
396 Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
398 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
399 collection::const_iterator pos, end = m_modules.end();
400 for (pos = m_modules.begin(); pos != end; ++pos) {
401 (*pos)->FindFunctionSymbols(lookup_info.GetLookupName(),
402 lookup_info.GetNameTypeMask(), sc_list);
405 const size_t new_size = sc_list.GetSize();
407 if (old_size < new_size)
408 lookup_info.Prune(sc_list, old_size);
410 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
411 collection::const_iterator pos, end = m_modules.end();
412 for (pos = m_modules.begin(); pos != end; ++pos) {
413 (*pos)->FindFunctionSymbols(name, name_type_mask, sc_list);
418 void ModuleList::FindFunctions(const RegularExpression &name,
419 bool include_symbols, bool include_inlines,
420 SymbolContextList &sc_list) {
421 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
422 collection::const_iterator pos, end = m_modules.end();
423 for (pos = m_modules.begin(); pos != end; ++pos) {
424 (*pos)->FindFunctions(name, include_symbols, include_inlines, sc_list);
428 void ModuleList::FindCompileUnits(const FileSpec &path,
429 SymbolContextList &sc_list) const {
430 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
431 collection::const_iterator pos, end = m_modules.end();
432 for (pos = m_modules.begin(); pos != end; ++pos) {
433 (*pos)->FindCompileUnits(path, sc_list);
437 void ModuleList::FindGlobalVariables(ConstString name, size_t max_matches,
438 VariableList &variable_list) const {
439 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
440 collection::const_iterator pos, end = m_modules.end();
441 for (pos = m_modules.begin(); pos != end; ++pos) {
442 (*pos)->FindGlobalVariables(name, CompilerDeclContext(), max_matches,
447 void ModuleList::FindGlobalVariables(const RegularExpression ®ex,
449 VariableList &variable_list) const {
450 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
451 collection::const_iterator pos, end = m_modules.end();
452 for (pos = m_modules.begin(); pos != end; ++pos) {
453 (*pos)->FindGlobalVariables(regex, max_matches, variable_list);
457 void ModuleList::FindSymbolsWithNameAndType(ConstString name,
458 SymbolType symbol_type,
459 SymbolContextList &sc_list) const {
460 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
461 collection::const_iterator pos, end = m_modules.end();
462 for (pos = m_modules.begin(); pos != end; ++pos)
463 (*pos)->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
466 void ModuleList::FindSymbolsMatchingRegExAndType(
467 const RegularExpression ®ex, lldb::SymbolType symbol_type,
468 SymbolContextList &sc_list) const {
469 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
470 collection::const_iterator pos, end = m_modules.end();
471 for (pos = m_modules.begin(); pos != end; ++pos)
472 (*pos)->FindSymbolsMatchingRegExAndType(regex, symbol_type, sc_list);
475 void ModuleList::FindModules(const ModuleSpec &module_spec,
476 ModuleList &matching_module_list) const {
477 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
478 collection::const_iterator pos, end = m_modules.end();
479 for (pos = m_modules.begin(); pos != end; ++pos) {
480 ModuleSP module_sp(*pos);
481 if (module_sp->MatchesModuleSpec(module_spec))
482 matching_module_list.Append(module_sp);
486 ModuleSP ModuleList::FindModule(const Module *module_ptr) const {
489 // Scope for "locker"
491 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
492 collection::const_iterator pos, end = m_modules.end();
494 for (pos = m_modules.begin(); pos != end; ++pos) {
495 if ((*pos).get() == module_ptr) {
504 ModuleSP ModuleList::FindModule(const UUID &uuid) const {
507 if (uuid.IsValid()) {
508 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
509 collection::const_iterator pos, end = m_modules.end();
511 for (pos = m_modules.begin(); pos != end; ++pos) {
512 if ((*pos)->GetUUID() == uuid) {
521 void ModuleList::FindTypes(Module *search_first, ConstString name,
522 bool name_is_fully_qualified, size_t max_matches,
523 llvm::DenseSet<SymbolFile *> &searched_symbol_files,
524 TypeList &types) const {
525 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
527 collection::const_iterator pos, end = m_modules.end();
529 for (pos = m_modules.begin(); pos != end; ++pos) {
530 if (search_first == pos->get()) {
531 search_first->FindTypes(name, name_is_fully_qualified, max_matches,
532 searched_symbol_files, types);
534 if (types.GetSize() >= max_matches)
540 for (pos = m_modules.begin(); pos != end; ++pos) {
541 // Search the module if the module is not equal to the one in the symbol
542 // context "sc". If "sc" contains a empty module shared pointer, then the
543 // comparison will always be true (valid_module_ptr != nullptr).
544 if (search_first != pos->get())
545 (*pos)->FindTypes(name, name_is_fully_qualified, max_matches,
546 searched_symbol_files, types);
548 if (types.GetSize() >= max_matches)
553 bool ModuleList::FindSourceFile(const FileSpec &orig_spec,
554 FileSpec &new_spec) const {
555 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
556 collection::const_iterator pos, end = m_modules.end();
557 for (pos = m_modules.begin(); pos != end; ++pos) {
558 if ((*pos)->FindSourceFile(orig_spec, new_spec))
564 void ModuleList::FindAddressesForLine(const lldb::TargetSP target_sp,
565 const FileSpec &file, uint32_t line,
567 std::vector<Address> &output_local,
568 std::vector<Address> &output_extern) {
569 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
570 collection::const_iterator pos, end = m_modules.end();
571 for (pos = m_modules.begin(); pos != end; ++pos) {
572 (*pos)->FindAddressesForLine(target_sp, file, line, function, output_local,
577 ModuleSP ModuleList::FindFirstModule(const ModuleSpec &module_spec) const {
579 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
580 collection::const_iterator pos, end = m_modules.end();
581 for (pos = m_modules.begin(); pos != end; ++pos) {
582 ModuleSP module_sp(*pos);
583 if (module_sp->MatchesModuleSpec(module_spec))
589 size_t ModuleList::GetSize() const {
592 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
593 size = m_modules.size();
598 void ModuleList::Dump(Stream *s) const {
599 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
600 collection::const_iterator pos, end = m_modules.end();
601 for (pos = m_modules.begin(); pos != end; ++pos) {
606 void ModuleList::LogUUIDAndPaths(Log *log, const char *prefix_cstr) {
607 if (log != nullptr) {
608 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
609 collection::const_iterator pos, begin = m_modules.begin(),
610 end = m_modules.end();
611 for (pos = begin; pos != end; ++pos) {
612 Module *module = pos->get();
613 const FileSpec &module_file_spec = module->GetFileSpec();
614 LLDB_LOGF(log, "%s[%u] %s (%s) \"%s\"", prefix_cstr ? prefix_cstr : "",
615 (uint32_t)std::distance(begin, pos),
616 module->GetUUID().GetAsString().c_str(),
617 module->GetArchitecture().GetArchitectureName(),
618 module_file_spec.GetPath().c_str());
623 bool ModuleList::ResolveFileAddress(lldb::addr_t vm_addr,
624 Address &so_addr) const {
625 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
626 collection::const_iterator pos, end = m_modules.end();
627 for (pos = m_modules.begin(); pos != end; ++pos) {
628 if ((*pos)->ResolveFileAddress(vm_addr, so_addr))
636 ModuleList::ResolveSymbolContextForAddress(const Address &so_addr,
637 SymbolContextItem resolve_scope,
638 SymbolContext &sc) const {
639 // The address is already section offset so it has a module
640 uint32_t resolved_flags = 0;
641 ModuleSP module_sp(so_addr.GetModule());
644 module_sp->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
646 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
647 collection::const_iterator pos, end = m_modules.end();
648 for (pos = m_modules.begin(); pos != end; ++pos) {
650 (*pos)->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
651 if (resolved_flags != 0)
656 return resolved_flags;
659 uint32_t ModuleList::ResolveSymbolContextForFilePath(
660 const char *file_path, uint32_t line, bool check_inlines,
661 SymbolContextItem resolve_scope, SymbolContextList &sc_list) const {
662 FileSpec file_spec(file_path);
663 return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
664 resolve_scope, sc_list);
667 uint32_t ModuleList::ResolveSymbolContextsForFileSpec(
668 const FileSpec &file_spec, uint32_t line, bool check_inlines,
669 SymbolContextItem resolve_scope, SymbolContextList &sc_list) const {
670 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
671 collection::const_iterator pos, end = m_modules.end();
672 for (pos = m_modules.begin(); pos != end; ++pos) {
673 (*pos)->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
674 resolve_scope, sc_list);
677 return sc_list.GetSize();
680 size_t ModuleList::GetIndexForModule(const Module *module) const {
682 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
683 collection::const_iterator pos;
684 collection::const_iterator begin = m_modules.begin();
685 collection::const_iterator end = m_modules.end();
686 for (pos = begin; pos != end; ++pos) {
687 if ((*pos).get() == module)
688 return std::distance(begin, pos);
691 return LLDB_INVALID_INDEX32;
695 struct SharedModuleListInfo {
696 ModuleList module_list;
697 ModuleListProperties module_list_properties;
700 static SharedModuleListInfo &GetSharedModuleListInfo()
702 static SharedModuleListInfo *g_shared_module_list_info = nullptr;
703 static llvm::once_flag g_once_flag;
704 llvm::call_once(g_once_flag, []() {
705 // NOTE: Intentionally leak the module list so a program doesn't have to
706 // cleanup all modules and object files as it exits. This just wastes time
707 // doing a bunch of cleanup that isn't required.
708 if (g_shared_module_list_info == nullptr)
709 g_shared_module_list_info = new SharedModuleListInfo();
711 return *g_shared_module_list_info;
714 static ModuleList &GetSharedModuleList() {
715 return GetSharedModuleListInfo().module_list;
718 ModuleListProperties &ModuleList::GetGlobalModuleListProperties() {
719 return GetSharedModuleListInfo().module_list_properties;
722 bool ModuleList::ModuleIsInCache(const Module *module_ptr) {
724 ModuleList &shared_module_list = GetSharedModuleList();
725 return shared_module_list.FindModule(module_ptr).get() != nullptr;
730 void ModuleList::FindSharedModules(const ModuleSpec &module_spec,
731 ModuleList &matching_module_list) {
732 GetSharedModuleList().FindModules(module_spec, matching_module_list);
735 size_t ModuleList::RemoveOrphanSharedModules(bool mandatory) {
736 return GetSharedModuleList().RemoveOrphans(mandatory);
740 ModuleList::GetSharedModule(const ModuleSpec &module_spec, ModuleSP &module_sp,
741 const FileSpecList *module_search_paths_ptr,
742 llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules,
743 bool *did_create_ptr, bool always_create) {
744 ModuleList &shared_module_list = GetSharedModuleList();
745 std::lock_guard<std::recursive_mutex> guard(
746 shared_module_list.m_modules_mutex);
754 *did_create_ptr = false;
756 const UUID *uuid_ptr = module_spec.GetUUIDPtr();
757 const FileSpec &module_file_spec = module_spec.GetFileSpec();
758 const ArchSpec &arch = module_spec.GetArchitecture();
760 // Make sure no one else can try and get or create a module while this
761 // function is actively working on it by doing an extra lock on the global
763 if (!always_create) {
764 ModuleList matching_module_list;
765 shared_module_list.FindModules(module_spec, matching_module_list);
766 const size_t num_matching_modules = matching_module_list.GetSize();
768 if (num_matching_modules > 0) {
769 for (size_t module_idx = 0; module_idx < num_matching_modules;
771 module_sp = matching_module_list.GetModuleAtIndex(module_idx);
773 // Make sure the file for the module hasn't been modified
774 if (module_sp->FileHasChanged()) {
776 old_modules->push_back(module_sp);
778 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_MODULES));
781 log, "%p '%s' module changed: removing from global module list",
782 static_cast<void *>(module_sp.get()),
783 module_sp->GetFileSpec().GetFilename().GetCString());
785 shared_module_list.Remove(module_sp);
788 // The module matches and the module was not modified from when it
799 module_sp = std::make_shared<Module>(module_spec);
800 // Make sure there are a module and an object file since we can specify a
801 // valid file path with an architecture that might not be in that file. By
802 // getting the object file we can guarantee that the architecture matches
803 if (module_sp->GetObjectFile()) {
804 // If we get in here we got the correct arch, now we just need to verify
805 // the UUID if one was given
806 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
809 if (module_sp->GetObjectFile() &&
810 module_sp->GetObjectFile()->GetType() ==
811 ObjectFile::eTypeStubLibrary) {
814 if (did_create_ptr) {
815 *did_create_ptr = true;
818 shared_module_list.ReplaceEquivalent(module_sp, old_modules);
826 if (module_search_paths_ptr) {
827 const auto num_directories = module_search_paths_ptr->GetSize();
828 for (size_t idx = 0; idx < num_directories; ++idx) {
829 auto search_path_spec = module_search_paths_ptr->GetFileSpecAtIndex(idx);
830 FileSystem::Instance().Resolve(search_path_spec);
831 namespace fs = llvm::sys::fs;
832 if (!FileSystem::Instance().IsDirectory(search_path_spec))
834 search_path_spec.AppendPathComponent(
835 module_spec.GetFileSpec().GetFilename().GetStringRef());
836 if (!FileSystem::Instance().Exists(search_path_spec))
839 auto resolved_module_spec(module_spec);
840 resolved_module_spec.GetFileSpec() = search_path_spec;
841 module_sp = std::make_shared<Module>(resolved_module_spec);
842 if (module_sp->GetObjectFile()) {
843 // If we get in here we got the correct arch, now we just need to
844 // verify the UUID if one was given
845 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
848 if (module_sp->GetObjectFile()->GetType() ==
849 ObjectFile::eTypeStubLibrary) {
853 *did_create_ptr = true;
855 shared_module_list.ReplaceEquivalent(module_sp, old_modules);
865 // Either the file didn't exist where at the path, or no path was given, so
866 // we now have to use more extreme measures to try and find the appropriate
869 // Fixup the incoming path in case the path points to a valid file, yet the
870 // arch or UUID (if one was passed in) don't match.
871 ModuleSpec located_binary_modulespec =
872 Symbols::LocateExecutableObjectFile(module_spec);
874 // Don't look for the file if it appears to be the same one we already
875 // checked for above...
876 if (located_binary_modulespec.GetFileSpec() != module_file_spec) {
877 if (!FileSystem::Instance().Exists(
878 located_binary_modulespec.GetFileSpec())) {
879 located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path));
881 module_file_spec.GetPath(path, sizeof(path));
882 // How can this check ever be true? This branch it is false, and we
883 // haven't modified file_spec.
884 if (FileSystem::Instance().Exists(
885 located_binary_modulespec.GetFileSpec())) {
886 std::string uuid_str;
887 if (uuid_ptr && uuid_ptr->IsValid())
888 uuid_str = uuid_ptr->GetAsString();
890 if (arch.IsValid()) {
891 if (!uuid_str.empty())
892 error.SetErrorStringWithFormat(
893 "'%s' does not contain the %s architecture and UUID %s", path,
894 arch.GetArchitectureName(), uuid_str.c_str());
896 error.SetErrorStringWithFormat(
897 "'%s' does not contain the %s architecture.", path,
898 arch.GetArchitectureName());
901 error.SetErrorStringWithFormat("'%s' does not exist", path);
908 // Make sure no one else can try and get or create a module while this
909 // function is actively working on it by doing an extra lock on the global
911 ModuleSpec platform_module_spec(module_spec);
912 platform_module_spec.GetFileSpec() =
913 located_binary_modulespec.GetFileSpec();
914 platform_module_spec.GetPlatformFileSpec() =
915 located_binary_modulespec.GetFileSpec();
916 platform_module_spec.GetSymbolFileSpec() =
917 located_binary_modulespec.GetSymbolFileSpec();
918 ModuleList matching_module_list;
919 shared_module_list.FindModules(platform_module_spec, matching_module_list);
920 if (!matching_module_list.IsEmpty()) {
921 module_sp = matching_module_list.GetModuleAtIndex(0);
923 // If we didn't have a UUID in mind when looking for the object file,
924 // then we should make sure the modification time hasn't changed!
925 if (platform_module_spec.GetUUIDPtr() == nullptr) {
926 auto file_spec_mod_time = FileSystem::Instance().GetModificationTime(
927 located_binary_modulespec.GetFileSpec());
928 if (file_spec_mod_time != llvm::sys::TimePoint<>()) {
929 if (file_spec_mod_time != module_sp->GetModificationTime()) {
931 old_modules->push_back(module_sp);
932 shared_module_list.Remove(module_sp);
940 module_sp = std::make_shared<Module>(platform_module_spec);
941 // Make sure there are a module and an object file since we can specify a
942 // valid file path with an architecture that might not be in that file.
943 // By getting the object file we can guarantee that the architecture
945 if (module_sp && module_sp->GetObjectFile()) {
946 if (module_sp->GetObjectFile()->GetType() ==
947 ObjectFile::eTypeStubLibrary) {
951 *did_create_ptr = true;
953 shared_module_list.ReplaceEquivalent(module_sp, old_modules);
956 located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path));
958 if (located_binary_modulespec.GetFileSpec()) {
960 error.SetErrorStringWithFormat(
961 "unable to open %s architecture in '%s'",
962 arch.GetArchitectureName(), path);
964 error.SetErrorStringWithFormat("unable to open '%s'", path);
966 std::string uuid_str;
967 if (uuid_ptr && uuid_ptr->IsValid())
968 uuid_str = uuid_ptr->GetAsString();
970 if (!uuid_str.empty())
971 error.SetErrorStringWithFormat(
972 "cannot locate a module for UUID '%s'", uuid_str.c_str());
974 error.SetErrorStringWithFormat("cannot locate a module");
983 bool ModuleList::RemoveSharedModule(lldb::ModuleSP &module_sp) {
984 return GetSharedModuleList().Remove(module_sp);
987 bool ModuleList::RemoveSharedModuleIfOrphaned(const Module *module_ptr) {
988 return GetSharedModuleList().RemoveIfOrphaned(module_ptr);
991 bool ModuleList::LoadScriptingResourcesInTarget(Target *target,
992 std::list<Status> &errors,
993 Stream *feedback_stream,
994 bool continue_on_error) {
997 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
998 for (auto module : m_modules) {
1001 if (!module->LoadScriptingResourceInTarget(target, error,
1003 if (error.Fail() && error.AsCString()) {
1004 error.SetErrorStringWithFormat("unable to load scripting data for "
1005 "module %s - error reported was %s",
1006 module->GetFileSpec()
1007 .GetFileNameStrippingExtension()
1010 errors.push_back(error);
1012 if (!continue_on_error)
1018 return errors.empty();
1021 void ModuleList::ForEach(
1022 std::function<bool(const ModuleSP &module_sp)> const &callback) const {
1023 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
1024 for (const auto &module : m_modules) {
1025 // If the callback returns false, then stop iterating and break out
1026 if (!callback(module))