1 //===-- ModuleList.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/ModuleList.h"
11 #include "lldb/Core/FileSpecList.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Core/ModuleSpec.h"
14 #include "lldb/Host/FileSystem.h"
15 #include "lldb/Host/Symbols.h"
16 #include "lldb/Interpreter/OptionValueFileSpec.h"
17 #include "lldb/Interpreter/OptionValueProperties.h"
18 #include "lldb/Interpreter/Property.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Symbol/SymbolContext.h"
21 #include "lldb/Symbol/VariableList.h"
22 #include "lldb/Utility/ArchSpec.h"
23 #include "lldb/Utility/ConstString.h"
24 #include "lldb/Utility/Log.h"
25 #include "lldb/Utility/Logging.h"
26 #include "lldb/Utility/UUID.h"
27 #include "lldb/lldb-defines.h"
30 #include "lldb/Host/windows/PosixApi.h"
33 #include "clang/Driver/Driver.h"
34 #include "llvm/ADT/StringRef.h"
35 #include "llvm/Support/FileSystem.h"
36 #include "llvm/Support/Threading.h"
37 #include "llvm/Support/raw_ostream.h"
45 namespace lldb_private {
48 namespace lldb_private {
49 class RegularExpression;
51 namespace lldb_private {
54 namespace lldb_private {
57 namespace lldb_private {
60 namespace lldb_private {
65 using namespace lldb_private;
69 static constexpr PropertyDefinition g_properties[] = {
70 {"enable-external-lookup", OptionValue::eTypeBoolean, true, true, nullptr,
72 "Control the use of external tools and repositories to locate symbol "
73 "files. Directories listed in target.debug-file-search-paths and "
74 "directory of the executable are always checked first for separate debug "
75 "info files. Then depending on this setting: "
76 "On macOS, Spotlight would be also used to locate a matching .dSYM "
77 "bundle based on the UUID of the executable. "
78 "On NetBSD, directory /usr/libdata/debug would be also searched. "
79 "On platforms other than NetBSD directory /usr/lib/debug would be "
82 {"clang-modules-cache-path", OptionValue::eTypeFileSpec, true, 0, nullptr,
84 "The path to the clang modules cache directory (-fmodules-cache-path)."}};
86 enum { ePropertyEnableExternalLookup, ePropertyClangModulesCachePath };
90 ModuleListProperties::ModuleListProperties() {
91 m_collection_sp.reset(new OptionValueProperties(ConstString("symbols")));
92 m_collection_sp->Initialize(g_properties);
94 llvm::SmallString<128> path;
95 clang::driver::Driver::getDefaultModuleCachePath(path);
96 SetClangModulesCachePath(path);
99 bool ModuleListProperties::GetEnableExternalLookup() const {
100 const uint32_t idx = ePropertyEnableExternalLookup;
101 return m_collection_sp->GetPropertyAtIndexAsBoolean(
102 nullptr, idx, g_properties[idx].default_uint_value != 0);
105 FileSpec ModuleListProperties::GetClangModulesCachePath() const {
106 return m_collection_sp
107 ->GetPropertyAtIndexAsOptionValueFileSpec(nullptr, false,
108 ePropertyClangModulesCachePath)
112 bool ModuleListProperties::SetClangModulesCachePath(llvm::StringRef path) {
113 return m_collection_sp->SetPropertyAtIndexAsString(
114 nullptr, ePropertyClangModulesCachePath, path);
118 ModuleList::ModuleList()
119 : m_modules(), m_modules_mutex(), m_notifier(nullptr) {}
121 ModuleList::ModuleList(const ModuleList &rhs)
122 : m_modules(), m_modules_mutex(), m_notifier(nullptr) {
123 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
124 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
125 m_modules = rhs.m_modules;
128 ModuleList::ModuleList(ModuleList::Notifier *notifier)
129 : m_modules(), m_modules_mutex(), m_notifier(notifier) {}
131 const ModuleList &ModuleList::operator=(const ModuleList &rhs) {
133 // That's probably me nit-picking, but in theoretical situation:
135 // * that two threads A B and
136 // * two ModuleList's x y do opposite assignments ie.:
138 // in thread A: | in thread B:
141 // This establishes correct(same) lock taking order and thus avoids
142 // priority inversion.
143 if (uintptr_t(this) > uintptr_t(&rhs)) {
144 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
145 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
146 m_modules = rhs.m_modules;
148 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
149 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
150 m_modules = rhs.m_modules;
156 ModuleList::~ModuleList() = default;
158 void ModuleList::AppendImpl(const ModuleSP &module_sp, bool use_notifier) {
160 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
161 m_modules.push_back(module_sp);
162 if (use_notifier && m_notifier)
163 m_notifier->ModuleAdded(*this, module_sp);
167 void ModuleList::Append(const ModuleSP &module_sp) { AppendImpl(module_sp); }
169 void ModuleList::ReplaceEquivalent(const ModuleSP &module_sp) {
171 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
173 // First remove any equivalent modules. Equivalent modules are modules
174 // whose path, platform path and architecture match.
175 ModuleSpec equivalent_module_spec(module_sp->GetFileSpec(),
176 module_sp->GetArchitecture());
177 equivalent_module_spec.GetPlatformFileSpec() =
178 module_sp->GetPlatformFileSpec();
181 while (idx < m_modules.size()) {
182 ModuleSP module_sp(m_modules[idx]);
183 if (module_sp->MatchesModuleSpec(equivalent_module_spec))
184 RemoveImpl(m_modules.begin() + idx);
188 // Now add the new module to the list
193 bool ModuleList::AppendIfNeeded(const ModuleSP &module_sp) {
195 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
196 collection::iterator pos, end = m_modules.end();
197 for (pos = m_modules.begin(); pos != end; ++pos) {
198 if (pos->get() == module_sp.get())
199 return false; // Already in the list
201 // Only push module_sp on the list if it wasn't already in there.
208 void ModuleList::Append(const ModuleList &module_list) {
209 for (auto pos : module_list.m_modules)
213 bool ModuleList::AppendIfNeeded(const ModuleList &module_list) {
215 for (auto pos : module_list.m_modules) {
216 if (AppendIfNeeded(pos))
222 bool ModuleList::RemoveImpl(const ModuleSP &module_sp, bool use_notifier) {
224 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
225 collection::iterator pos, end = m_modules.end();
226 for (pos = m_modules.begin(); pos != end; ++pos) {
227 if (pos->get() == module_sp.get()) {
228 m_modules.erase(pos);
229 if (use_notifier && m_notifier)
230 m_notifier->ModuleRemoved(*this, module_sp);
238 ModuleList::collection::iterator
239 ModuleList::RemoveImpl(ModuleList::collection::iterator pos,
241 ModuleSP module_sp(*pos);
242 collection::iterator retval = m_modules.erase(pos);
243 if (use_notifier && m_notifier)
244 m_notifier->ModuleRemoved(*this, module_sp);
248 bool ModuleList::Remove(const ModuleSP &module_sp) {
249 return RemoveImpl(module_sp);
252 bool ModuleList::ReplaceModule(const lldb::ModuleSP &old_module_sp,
253 const lldb::ModuleSP &new_module_sp) {
254 if (!RemoveImpl(old_module_sp, false))
256 AppendImpl(new_module_sp, false);
258 m_notifier->ModuleUpdated(*this, old_module_sp, new_module_sp);
262 bool ModuleList::RemoveIfOrphaned(const Module *module_ptr) {
264 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
265 collection::iterator pos, end = m_modules.end();
266 for (pos = m_modules.begin(); pos != end; ++pos) {
267 if (pos->get() == module_ptr) {
269 pos = RemoveImpl(pos);
279 size_t ModuleList::RemoveOrphans(bool mandatory) {
280 std::unique_lock<std::recursive_mutex> lock(m_modules_mutex, std::defer_lock);
285 // Not mandatory, remove orphans if we can get the mutex
286 if (!lock.try_lock())
289 collection::iterator pos = m_modules.begin();
290 size_t remove_count = 0;
291 while (pos != m_modules.end()) {
293 pos = RemoveImpl(pos);
302 size_t ModuleList::Remove(ModuleList &module_list) {
303 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
304 size_t num_removed = 0;
305 collection::iterator pos, end = module_list.m_modules.end();
306 for (pos = module_list.m_modules.begin(); pos != end; ++pos) {
313 void ModuleList::Clear() { ClearImpl(); }
315 void ModuleList::Destroy() { ClearImpl(); }
317 void ModuleList::ClearImpl(bool use_notifier) {
318 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
319 if (use_notifier && m_notifier)
320 m_notifier->WillClearList(*this);
324 Module *ModuleList::GetModulePointerAtIndex(size_t idx) const {
325 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
326 return GetModulePointerAtIndexUnlocked(idx);
329 Module *ModuleList::GetModulePointerAtIndexUnlocked(size_t idx) const {
330 if (idx < m_modules.size())
331 return m_modules[idx].get();
335 ModuleSP ModuleList::GetModuleAtIndex(size_t idx) const {
336 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
337 return GetModuleAtIndexUnlocked(idx);
340 ModuleSP ModuleList::GetModuleAtIndexUnlocked(size_t idx) const {
342 if (idx < m_modules.size())
343 module_sp = m_modules[idx];
347 size_t ModuleList::FindFunctions(const ConstString &name,
348 FunctionNameType name_type_mask,
349 bool include_symbols, bool include_inlines,
351 SymbolContextList &sc_list) const {
355 const size_t old_size = sc_list.GetSize();
357 if (name_type_mask & eFunctionNameTypeAuto) {
358 Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
360 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
361 collection::const_iterator pos, end = m_modules.end();
362 for (pos = m_modules.begin(); pos != end; ++pos) {
363 (*pos)->FindFunctions(lookup_info.GetLookupName(), nullptr,
364 lookup_info.GetNameTypeMask(), include_symbols,
365 include_inlines, true, sc_list);
368 const size_t new_size = sc_list.GetSize();
370 if (old_size < new_size)
371 lookup_info.Prune(sc_list, old_size);
373 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
374 collection::const_iterator pos, end = m_modules.end();
375 for (pos = m_modules.begin(); pos != end; ++pos) {
376 (*pos)->FindFunctions(name, nullptr, name_type_mask, include_symbols,
377 include_inlines, true, sc_list);
380 return sc_list.GetSize() - old_size;
383 size_t ModuleList::FindFunctionSymbols(const ConstString &name,
384 lldb::FunctionNameType name_type_mask,
385 SymbolContextList &sc_list) {
386 const size_t old_size = sc_list.GetSize();
388 if (name_type_mask & eFunctionNameTypeAuto) {
389 Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
391 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
392 collection::const_iterator pos, end = m_modules.end();
393 for (pos = m_modules.begin(); pos != end; ++pos) {
394 (*pos)->FindFunctionSymbols(lookup_info.GetLookupName(),
395 lookup_info.GetNameTypeMask(), sc_list);
398 const size_t new_size = sc_list.GetSize();
400 if (old_size < new_size)
401 lookup_info.Prune(sc_list, old_size);
403 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
404 collection::const_iterator pos, end = m_modules.end();
405 for (pos = m_modules.begin(); pos != end; ++pos) {
406 (*pos)->FindFunctionSymbols(name, name_type_mask, sc_list);
410 return sc_list.GetSize() - old_size;
413 size_t ModuleList::FindFunctions(const RegularExpression &name,
414 bool include_symbols, bool include_inlines,
415 bool append, SymbolContextList &sc_list) {
416 const size_t old_size = sc_list.GetSize();
418 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
419 collection::const_iterator pos, end = m_modules.end();
420 for (pos = m_modules.begin(); pos != end; ++pos) {
421 (*pos)->FindFunctions(name, include_symbols, include_inlines, append,
425 return sc_list.GetSize() - old_size;
428 size_t ModuleList::FindCompileUnits(const FileSpec &path, bool append,
429 SymbolContextList &sc_list) const {
433 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
434 collection::const_iterator pos, end = m_modules.end();
435 for (pos = m_modules.begin(); pos != end; ++pos) {
436 (*pos)->FindCompileUnits(path, true, sc_list);
439 return sc_list.GetSize();
442 size_t ModuleList::FindGlobalVariables(const ConstString &name,
444 VariableList &variable_list) const {
445 size_t initial_size = variable_list.GetSize();
446 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
447 collection::const_iterator pos, end = m_modules.end();
448 for (pos = m_modules.begin(); pos != end; ++pos) {
449 (*pos)->FindGlobalVariables(name, nullptr, max_matches, variable_list);
451 return variable_list.GetSize() - initial_size;
454 size_t ModuleList::FindGlobalVariables(const RegularExpression ®ex,
456 VariableList &variable_list) const {
457 size_t initial_size = variable_list.GetSize();
458 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
459 collection::const_iterator pos, end = m_modules.end();
460 for (pos = m_modules.begin(); pos != end; ++pos) {
461 (*pos)->FindGlobalVariables(regex, max_matches, variable_list);
463 return variable_list.GetSize() - initial_size;
466 size_t ModuleList::FindSymbolsWithNameAndType(const ConstString &name,
467 SymbolType symbol_type,
468 SymbolContextList &sc_list,
470 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
473 size_t initial_size = sc_list.GetSize();
475 collection::const_iterator pos, end = m_modules.end();
476 for (pos = m_modules.begin(); pos != end; ++pos)
477 (*pos)->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
478 return sc_list.GetSize() - initial_size;
481 size_t ModuleList::FindSymbolsMatchingRegExAndType(
482 const RegularExpression ®ex, lldb::SymbolType symbol_type,
483 SymbolContextList &sc_list, bool append) const {
484 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
487 size_t initial_size = sc_list.GetSize();
489 collection::const_iterator pos, end = m_modules.end();
490 for (pos = m_modules.begin(); pos != end; ++pos)
491 (*pos)->FindSymbolsMatchingRegExAndType(regex, symbol_type, sc_list);
492 return sc_list.GetSize() - initial_size;
495 size_t ModuleList::FindModules(const ModuleSpec &module_spec,
496 ModuleList &matching_module_list) const {
497 size_t existing_matches = matching_module_list.GetSize();
499 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
500 collection::const_iterator pos, end = m_modules.end();
501 for (pos = m_modules.begin(); pos != end; ++pos) {
502 ModuleSP module_sp(*pos);
503 if (module_sp->MatchesModuleSpec(module_spec))
504 matching_module_list.Append(module_sp);
506 return matching_module_list.GetSize() - existing_matches;
509 ModuleSP ModuleList::FindModule(const Module *module_ptr) const {
512 // Scope for "locker"
514 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
515 collection::const_iterator pos, end = m_modules.end();
517 for (pos = m_modules.begin(); pos != end; ++pos) {
518 if ((*pos).get() == module_ptr) {
527 ModuleSP ModuleList::FindModule(const UUID &uuid) const {
530 if (uuid.IsValid()) {
531 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
532 collection::const_iterator pos, end = m_modules.end();
534 for (pos = m_modules.begin(); pos != end; ++pos) {
535 if ((*pos)->GetUUID() == uuid) {
545 ModuleList::FindTypes(Module *search_first, const ConstString &name,
546 bool name_is_fully_qualified, size_t max_matches,
547 llvm::DenseSet<SymbolFile *> &searched_symbol_files,
548 TypeList &types) const {
549 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
551 size_t total_matches = 0;
552 collection::const_iterator pos, end = m_modules.end();
554 for (pos = m_modules.begin(); pos != end; ++pos) {
555 if (search_first == pos->get()) {
557 search_first->FindTypes(name, name_is_fully_qualified, max_matches,
558 searched_symbol_files, types);
560 if (total_matches >= max_matches)
566 if (total_matches < max_matches) {
567 for (pos = m_modules.begin(); pos != end; ++pos) {
568 // Search the module if the module is not equal to the one in the symbol
569 // context "sc". If "sc" contains a empty module shared pointer, then the
570 // comparison will always be true (valid_module_ptr != nullptr).
571 if (search_first != pos->get())
573 (*pos)->FindTypes(name, name_is_fully_qualified, max_matches,
574 searched_symbol_files, types);
576 if (total_matches >= max_matches)
581 return total_matches;
584 bool ModuleList::FindSourceFile(const FileSpec &orig_spec,
585 FileSpec &new_spec) const {
586 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
587 collection::const_iterator pos, end = m_modules.end();
588 for (pos = m_modules.begin(); pos != end; ++pos) {
589 if ((*pos)->FindSourceFile(orig_spec, new_spec))
595 void ModuleList::FindAddressesForLine(const lldb::TargetSP target_sp,
596 const FileSpec &file, uint32_t line,
598 std::vector<Address> &output_local,
599 std::vector<Address> &output_extern) {
600 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
601 collection::const_iterator pos, end = m_modules.end();
602 for (pos = m_modules.begin(); pos != end; ++pos) {
603 (*pos)->FindAddressesForLine(target_sp, file, line, function, output_local,
608 ModuleSP ModuleList::FindFirstModule(const ModuleSpec &module_spec) const {
610 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
611 collection::const_iterator pos, end = m_modules.end();
612 for (pos = m_modules.begin(); pos != end; ++pos) {
613 ModuleSP module_sp(*pos);
614 if (module_sp->MatchesModuleSpec(module_spec))
620 size_t ModuleList::GetSize() const {
623 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
624 size = m_modules.size();
629 void ModuleList::Dump(Stream *s) const {
630 // s.Printf("%.*p: ", (int)sizeof(void*) * 2, this);
632 // s << "ModuleList\n";
634 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
635 collection::const_iterator pos, end = m_modules.end();
636 for (pos = m_modules.begin(); pos != end; ++pos) {
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 log->Printf("%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 collection::const_iterator pos, end = m_modules.end();
662 for (pos = m_modules.begin(); pos != end; ++pos) {
663 if ((*pos)->ResolveFileAddress(vm_addr, so_addr))
671 ModuleList::ResolveSymbolContextForAddress(const Address &so_addr,
672 SymbolContextItem resolve_scope,
673 SymbolContext &sc) const {
674 // The address is already section offset so it has a module
675 uint32_t resolved_flags = 0;
676 ModuleSP module_sp(so_addr.GetModule());
679 module_sp->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
681 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
682 collection::const_iterator pos, end = m_modules.end();
683 for (pos = m_modules.begin(); pos != end; ++pos) {
685 (*pos)->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
686 if (resolved_flags != 0)
691 return resolved_flags;
694 uint32_t ModuleList::ResolveSymbolContextForFilePath(
695 const char *file_path, uint32_t line, bool check_inlines,
696 SymbolContextItem resolve_scope, SymbolContextList &sc_list) const {
697 FileSpec file_spec(file_path);
698 return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
699 resolve_scope, sc_list);
702 uint32_t ModuleList::ResolveSymbolContextsForFileSpec(
703 const FileSpec &file_spec, uint32_t line, bool check_inlines,
704 SymbolContextItem resolve_scope, SymbolContextList &sc_list) const {
705 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
706 collection::const_iterator pos, end = m_modules.end();
707 for (pos = m_modules.begin(); pos != end; ++pos) {
708 (*pos)->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
709 resolve_scope, sc_list);
712 return sc_list.GetSize();
715 size_t ModuleList::GetIndexForModule(const Module *module) const {
717 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
718 collection::const_iterator pos;
719 collection::const_iterator begin = m_modules.begin();
720 collection::const_iterator end = m_modules.end();
721 for (pos = begin; pos != end; ++pos) {
722 if ((*pos).get() == module)
723 return std::distance(begin, pos);
726 return LLDB_INVALID_INDEX32;
730 struct SharedModuleListInfo {
731 ModuleList module_list;
732 ModuleListProperties module_list_properties;
735 static SharedModuleListInfo &GetSharedModuleListInfo()
737 static SharedModuleListInfo *g_shared_module_list_info = nullptr;
738 static llvm::once_flag g_once_flag;
739 llvm::call_once(g_once_flag, []() {
740 // NOTE: Intentionally leak the module list so a program doesn't have to
741 // cleanup all modules and object files as it exits. This just wastes time
742 // doing a bunch of cleanup that isn't required.
743 if (g_shared_module_list_info == nullptr)
744 g_shared_module_list_info = new SharedModuleListInfo();
746 return *g_shared_module_list_info;
749 static ModuleList &GetSharedModuleList() {
750 return GetSharedModuleListInfo().module_list;
753 ModuleListProperties &ModuleList::GetGlobalModuleListProperties() {
754 return GetSharedModuleListInfo().module_list_properties;
757 bool ModuleList::ModuleIsInCache(const Module *module_ptr) {
759 ModuleList &shared_module_list = GetSharedModuleList();
760 return shared_module_list.FindModule(module_ptr).get() != nullptr;
765 size_t ModuleList::FindSharedModules(const ModuleSpec &module_spec,
766 ModuleList &matching_module_list) {
767 return GetSharedModuleList().FindModules(module_spec, matching_module_list);
770 size_t ModuleList::RemoveOrphanSharedModules(bool mandatory) {
771 return GetSharedModuleList().RemoveOrphans(mandatory);
774 Status ModuleList::GetSharedModule(const ModuleSpec &module_spec,
776 const FileSpecList *module_search_paths_ptr,
777 ModuleSP *old_module_sp_ptr,
778 bool *did_create_ptr, bool always_create) {
779 ModuleList &shared_module_list = GetSharedModuleList();
780 std::lock_guard<std::recursive_mutex> guard(
781 shared_module_list.m_modules_mutex);
789 *did_create_ptr = false;
790 if (old_module_sp_ptr)
791 old_module_sp_ptr->reset();
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 const size_t num_matching_modules =
803 shared_module_list.FindModules(module_spec, matching_module_list);
804 if (num_matching_modules > 0) {
805 for (size_t module_idx = 0; module_idx < num_matching_modules;
807 module_sp = matching_module_list.GetModuleAtIndex(module_idx);
809 // Make sure the file for the module hasn't been modified
810 if (module_sp->FileHasChanged()) {
811 if (old_module_sp_ptr && !*old_module_sp_ptr)
812 *old_module_sp_ptr = module_sp;
814 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_MODULES));
816 log->Printf("module changed: %p, removing from global module list",
817 static_cast<void *>(module_sp.get()));
819 shared_module_list.Remove(module_sp);
822 // The module matches and the module was not modified from when it
833 module_sp.reset(new Module(module_spec));
834 // Make sure there are a module and an object file since we can specify a
835 // valid file path with an architecture that might not be in that file. By
836 // getting the object file we can guarantee that the architecture matches
837 if (module_sp->GetObjectFile()) {
838 // If we get in here we got the correct arch, now we just need to verify
839 // the UUID if one was given
840 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
843 if (module_sp->GetObjectFile() &&
844 module_sp->GetObjectFile()->GetType() ==
845 ObjectFile::eTypeStubLibrary) {
848 if (did_create_ptr) {
849 *did_create_ptr = true;
852 shared_module_list.ReplaceEquivalent(module_sp);
860 if (module_search_paths_ptr) {
861 const auto num_directories = module_search_paths_ptr->GetSize();
862 for (size_t idx = 0; idx < num_directories; ++idx) {
863 auto search_path_spec = module_search_paths_ptr->GetFileSpecAtIndex(idx);
864 FileSystem::Instance().Resolve(search_path_spec);
865 namespace fs = llvm::sys::fs;
866 if (!FileSystem::Instance().IsDirectory(search_path_spec))
868 search_path_spec.AppendPathComponent(
869 module_spec.GetFileSpec().GetFilename().AsCString());
870 if (!FileSystem::Instance().Exists(search_path_spec))
873 auto resolved_module_spec(module_spec);
874 resolved_module_spec.GetFileSpec() = search_path_spec;
875 module_sp.reset(new Module(resolved_module_spec));
876 if (module_sp->GetObjectFile()) {
877 // If we get in here we got the correct arch, now we just need to
878 // verify the UUID if one was given
879 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
882 if (module_sp->GetObjectFile()->GetType() ==
883 ObjectFile::eTypeStubLibrary) {
887 *did_create_ptr = true;
889 shared_module_list.ReplaceEquivalent(module_sp);
899 // Either the file didn't exist where at the path, or no path was given, so
900 // we now have to use more extreme measures to try and find the appropriate
903 // Fixup the incoming path in case the path points to a valid file, yet the
904 // arch or UUID (if one was passed in) don't match.
905 ModuleSpec located_binary_modulespec =
906 Symbols::LocateExecutableObjectFile(module_spec);
908 // Don't look for the file if it appears to be the same one we already
909 // checked for above...
910 if (located_binary_modulespec.GetFileSpec() != module_file_spec) {
911 if (!FileSystem::Instance().Exists(
912 located_binary_modulespec.GetFileSpec())) {
913 located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path));
915 module_file_spec.GetPath(path, sizeof(path));
916 // How can this check ever be true? This branch it is false, and we
917 // haven't modified file_spec.
918 if (FileSystem::Instance().Exists(
919 located_binary_modulespec.GetFileSpec())) {
920 std::string uuid_str;
921 if (uuid_ptr && uuid_ptr->IsValid())
922 uuid_str = uuid_ptr->GetAsString();
924 if (arch.IsValid()) {
925 if (!uuid_str.empty())
926 error.SetErrorStringWithFormat(
927 "'%s' does not contain the %s architecture and UUID %s", path,
928 arch.GetArchitectureName(), uuid_str.c_str());
930 error.SetErrorStringWithFormat(
931 "'%s' does not contain the %s architecture.", path,
932 arch.GetArchitectureName());
935 error.SetErrorStringWithFormat("'%s' does not exist", path);
942 // Make sure no one else can try and get or create a module while this
943 // function is actively working on it by doing an extra lock on the global
945 ModuleSpec platform_module_spec(module_spec);
946 platform_module_spec.GetFileSpec() =
947 located_binary_modulespec.GetFileSpec();
948 platform_module_spec.GetPlatformFileSpec() =
949 located_binary_modulespec.GetFileSpec();
950 platform_module_spec.GetSymbolFileSpec() =
951 located_binary_modulespec.GetSymbolFileSpec();
952 ModuleList matching_module_list;
953 if (shared_module_list.FindModules(platform_module_spec,
954 matching_module_list) > 0) {
955 module_sp = matching_module_list.GetModuleAtIndex(0);
957 // If we didn't have a UUID in mind when looking for the object file,
958 // then we should make sure the modification time hasn't changed!
959 if (platform_module_spec.GetUUIDPtr() == nullptr) {
960 auto file_spec_mod_time = FileSystem::Instance().GetModificationTime(
961 located_binary_modulespec.GetFileSpec());
962 if (file_spec_mod_time != llvm::sys::TimePoint<>()) {
963 if (file_spec_mod_time != module_sp->GetModificationTime()) {
964 if (old_module_sp_ptr)
965 *old_module_sp_ptr = module_sp;
966 shared_module_list.Remove(module_sp);
974 module_sp.reset(new Module(platform_module_spec));
975 // Make sure there are a module and an object file since we can specify a
976 // valid file path with an architecture that might not be in that file.
977 // By getting the object file we can guarantee that the architecture
979 if (module_sp && module_sp->GetObjectFile()) {
980 if (module_sp->GetObjectFile()->GetType() ==
981 ObjectFile::eTypeStubLibrary) {
985 *did_create_ptr = true;
987 shared_module_list.ReplaceEquivalent(module_sp);
990 located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path));
992 if (located_binary_modulespec.GetFileSpec()) {
994 error.SetErrorStringWithFormat(
995 "unable to open %s architecture in '%s'",
996 arch.GetArchitectureName(), path);
998 error.SetErrorStringWithFormat("unable to open '%s'", path);
1000 std::string uuid_str;
1001 if (uuid_ptr && uuid_ptr->IsValid())
1002 uuid_str = uuid_ptr->GetAsString();
1004 if (!uuid_str.empty())
1005 error.SetErrorStringWithFormat(
1006 "cannot locate a module for UUID '%s'", uuid_str.c_str());
1008 error.SetErrorStringWithFormat("cannot locate a module");
1017 bool ModuleList::RemoveSharedModule(lldb::ModuleSP &module_sp) {
1018 return GetSharedModuleList().Remove(module_sp);
1021 bool ModuleList::RemoveSharedModuleIfOrphaned(const Module *module_ptr) {
1022 return GetSharedModuleList().RemoveIfOrphaned(module_ptr);
1025 bool ModuleList::LoadScriptingResourcesInTarget(Target *target,
1026 std::list<Status> &errors,
1027 Stream *feedback_stream,
1028 bool continue_on_error) {
1031 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
1032 for (auto module : m_modules) {
1035 if (!module->LoadScriptingResourceInTarget(target, error,
1037 if (error.Fail() && error.AsCString()) {
1038 error.SetErrorStringWithFormat("unable to load scripting data for "
1039 "module %s - error reported was %s",
1040 module->GetFileSpec()
1041 .GetFileNameStrippingExtension()
1044 errors.push_back(error);
1046 if (!continue_on_error)
1052 return errors.empty();
1055 void ModuleList::ForEach(
1056 std::function<bool(const ModuleSP &module_sp)> const &callback) const {
1057 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
1058 for (const auto &module : m_modules) {
1059 // If the callback returns false, then stop iterating and break out
1060 if (!callback(module))