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"
17 // Other libraries and framework includes
19 #include "lldb/Core/Log.h"
20 #include "lldb/Core/Module.h"
21 #include "lldb/Core/ModuleSpec.h"
22 #include "lldb/Host/FileSystem.h"
23 #include "lldb/Host/Host.h"
24 #include "lldb/Host/Symbols.h"
25 #include "lldb/Symbol/ObjectFile.h"
26 #include "lldb/Symbol/SymbolFile.h"
27 #include "lldb/Symbol/VariableList.h"
30 using namespace lldb_private;
32 ModuleList::ModuleList()
33 : m_modules(), m_modules_mutex(), m_notifier(nullptr) {}
35 ModuleList::ModuleList(const ModuleList &rhs)
36 : m_modules(), m_modules_mutex(), m_notifier(nullptr) {
37 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
38 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
39 m_modules = rhs.m_modules;
42 ModuleList::ModuleList(ModuleList::Notifier *notifier)
43 : m_modules(), m_modules_mutex(), m_notifier(notifier) {}
45 const ModuleList &ModuleList::operator=(const ModuleList &rhs) {
47 // That's probably me nit-picking, but in theoretical situation:
49 // * that two threads A B and
50 // * two ModuleList's x y do opposite assignments ie.:
52 // in thread A: | in thread B:
55 // This establishes correct(same) lock taking order and thus
56 // avoids priority inversion.
57 if (uintptr_t(this) > uintptr_t(&rhs)) {
58 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
59 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
60 m_modules = rhs.m_modules;
62 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
63 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
64 m_modules = rhs.m_modules;
70 ModuleList::~ModuleList() = default;
72 void ModuleList::AppendImpl(const ModuleSP &module_sp, bool use_notifier) {
74 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
75 m_modules.push_back(module_sp);
76 if (use_notifier && m_notifier)
77 m_notifier->ModuleAdded(*this, module_sp);
81 void ModuleList::Append(const ModuleSP &module_sp) { AppendImpl(module_sp); }
83 void ModuleList::ReplaceEquivalent(const ModuleSP &module_sp) {
85 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
87 // First remove any equivalent modules. Equivalent modules are modules
88 // whose path, platform path and architecture match.
89 ModuleSpec equivalent_module_spec(module_sp->GetFileSpec(),
90 module_sp->GetArchitecture());
91 equivalent_module_spec.GetPlatformFileSpec() =
92 module_sp->GetPlatformFileSpec();
95 while (idx < m_modules.size()) {
96 ModuleSP module_sp(m_modules[idx]);
97 if (module_sp->MatchesModuleSpec(equivalent_module_spec))
98 RemoveImpl(m_modules.begin() + idx);
102 // Now add the new module to the list
107 bool ModuleList::AppendIfNeeded(const ModuleSP &module_sp) {
109 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
110 collection::iterator pos, end = m_modules.end();
111 for (pos = m_modules.begin(); pos != end; ++pos) {
112 if (pos->get() == module_sp.get())
113 return false; // Already in the list
115 // Only push module_sp on the list if it wasn't already in there.
122 void ModuleList::Append(const ModuleList &module_list) {
123 for (auto pos : module_list.m_modules)
127 bool ModuleList::AppendIfNeeded(const ModuleList &module_list) {
129 for (auto pos : module_list.m_modules) {
130 if (AppendIfNeeded(pos))
136 bool ModuleList::RemoveImpl(const ModuleSP &module_sp, bool use_notifier) {
138 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
139 collection::iterator pos, end = m_modules.end();
140 for (pos = m_modules.begin(); pos != end; ++pos) {
141 if (pos->get() == module_sp.get()) {
142 m_modules.erase(pos);
143 if (use_notifier && m_notifier)
144 m_notifier->ModuleRemoved(*this, module_sp);
152 ModuleList::collection::iterator
153 ModuleList::RemoveImpl(ModuleList::collection::iterator pos,
155 ModuleSP module_sp(*pos);
156 collection::iterator retval = m_modules.erase(pos);
157 if (use_notifier && m_notifier)
158 m_notifier->ModuleRemoved(*this, module_sp);
162 bool ModuleList::Remove(const ModuleSP &module_sp) {
163 return RemoveImpl(module_sp);
166 bool ModuleList::ReplaceModule(const lldb::ModuleSP &old_module_sp,
167 const lldb::ModuleSP &new_module_sp) {
168 if (!RemoveImpl(old_module_sp, false))
170 AppendImpl(new_module_sp, false);
172 m_notifier->ModuleUpdated(*this, old_module_sp, new_module_sp);
176 bool ModuleList::RemoveIfOrphaned(const Module *module_ptr) {
178 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
179 collection::iterator pos, end = m_modules.end();
180 for (pos = m_modules.begin(); pos != end; ++pos) {
181 if (pos->get() == module_ptr) {
183 pos = RemoveImpl(pos);
193 size_t ModuleList::RemoveOrphans(bool mandatory) {
194 std::unique_lock<std::recursive_mutex> lock(m_modules_mutex, std::defer_lock);
199 // Not mandatory, remove orphans if we can get the mutex
200 if (!lock.try_lock())
203 collection::iterator pos = m_modules.begin();
204 size_t remove_count = 0;
205 while (pos != m_modules.end()) {
207 pos = RemoveImpl(pos);
216 size_t ModuleList::Remove(ModuleList &module_list) {
217 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
218 size_t num_removed = 0;
219 collection::iterator pos, end = module_list.m_modules.end();
220 for (pos = module_list.m_modules.begin(); pos != end; ++pos) {
227 void ModuleList::Clear() { ClearImpl(); }
229 void ModuleList::Destroy() { ClearImpl(); }
231 void ModuleList::ClearImpl(bool use_notifier) {
232 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
233 if (use_notifier && m_notifier)
234 m_notifier->WillClearList(*this);
238 Module *ModuleList::GetModulePointerAtIndex(size_t idx) const {
239 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
240 return GetModulePointerAtIndexUnlocked(idx);
243 Module *ModuleList::GetModulePointerAtIndexUnlocked(size_t idx) const {
244 if (idx < m_modules.size())
245 return m_modules[idx].get();
249 ModuleSP ModuleList::GetModuleAtIndex(size_t idx) const {
250 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
251 return GetModuleAtIndexUnlocked(idx);
254 ModuleSP ModuleList::GetModuleAtIndexUnlocked(size_t idx) const {
256 if (idx < m_modules.size())
257 module_sp = m_modules[idx];
261 size_t ModuleList::FindFunctions(const ConstString &name,
262 uint32_t name_type_mask, bool include_symbols,
263 bool include_inlines, bool append,
264 SymbolContextList &sc_list) const {
268 const size_t old_size = sc_list.GetSize();
270 if (name_type_mask & eFunctionNameTypeAuto) {
271 Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
273 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
274 collection::const_iterator pos, end = m_modules.end();
275 for (pos = m_modules.begin(); pos != end; ++pos) {
276 (*pos)->FindFunctions(lookup_info.GetLookupName(), nullptr,
277 lookup_info.GetNameTypeMask(), include_symbols,
278 include_inlines, true, sc_list);
281 const size_t new_size = sc_list.GetSize();
283 if (old_size < new_size)
284 lookup_info.Prune(sc_list, old_size);
286 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
287 collection::const_iterator pos, end = m_modules.end();
288 for (pos = m_modules.begin(); pos != end; ++pos) {
289 (*pos)->FindFunctions(name, nullptr, name_type_mask, include_symbols,
290 include_inlines, true, sc_list);
293 return sc_list.GetSize() - old_size;
296 size_t ModuleList::FindFunctionSymbols(const ConstString &name,
297 uint32_t name_type_mask,
298 SymbolContextList &sc_list) {
299 const size_t old_size = sc_list.GetSize();
301 if (name_type_mask & eFunctionNameTypeAuto) {
302 Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
304 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
305 collection::const_iterator pos, end = m_modules.end();
306 for (pos = m_modules.begin(); pos != end; ++pos) {
307 (*pos)->FindFunctionSymbols(lookup_info.GetLookupName(),
308 lookup_info.GetNameTypeMask(), sc_list);
311 const size_t new_size = sc_list.GetSize();
313 if (old_size < new_size)
314 lookup_info.Prune(sc_list, old_size);
316 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
317 collection::const_iterator pos, end = m_modules.end();
318 for (pos = m_modules.begin(); pos != end; ++pos) {
319 (*pos)->FindFunctionSymbols(name, name_type_mask, sc_list);
323 return sc_list.GetSize() - old_size;
326 size_t ModuleList::FindFunctions(const RegularExpression &name,
327 bool include_symbols, bool include_inlines,
328 bool append, SymbolContextList &sc_list) {
329 const size_t old_size = sc_list.GetSize();
331 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
332 collection::const_iterator pos, end = m_modules.end();
333 for (pos = m_modules.begin(); pos != end; ++pos) {
334 (*pos)->FindFunctions(name, include_symbols, include_inlines, append,
338 return sc_list.GetSize() - old_size;
341 size_t ModuleList::FindCompileUnits(const FileSpec &path, bool append,
342 SymbolContextList &sc_list) const {
346 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
347 collection::const_iterator pos, end = m_modules.end();
348 for (pos = m_modules.begin(); pos != end; ++pos) {
349 (*pos)->FindCompileUnits(path, true, sc_list);
352 return sc_list.GetSize();
355 size_t ModuleList::FindGlobalVariables(const ConstString &name, bool append,
357 VariableList &variable_list) const {
358 size_t initial_size = variable_list.GetSize();
359 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
360 collection::const_iterator pos, end = m_modules.end();
361 for (pos = m_modules.begin(); pos != end; ++pos) {
362 (*pos)->FindGlobalVariables(name, nullptr, append, max_matches,
365 return variable_list.GetSize() - initial_size;
368 size_t ModuleList::FindGlobalVariables(const RegularExpression ®ex,
369 bool append, size_t max_matches,
370 VariableList &variable_list) const {
371 size_t initial_size = variable_list.GetSize();
372 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
373 collection::const_iterator pos, end = m_modules.end();
374 for (pos = m_modules.begin(); pos != end; ++pos) {
375 (*pos)->FindGlobalVariables(regex, append, max_matches, variable_list);
377 return variable_list.GetSize() - initial_size;
380 size_t ModuleList::FindSymbolsWithNameAndType(const ConstString &name,
381 SymbolType symbol_type,
382 SymbolContextList &sc_list,
384 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
387 size_t initial_size = sc_list.GetSize();
389 collection::const_iterator pos, end = m_modules.end();
390 for (pos = m_modules.begin(); pos != end; ++pos)
391 (*pos)->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
392 return sc_list.GetSize() - initial_size;
395 size_t ModuleList::FindSymbolsMatchingRegExAndType(
396 const RegularExpression ®ex, lldb::SymbolType symbol_type,
397 SymbolContextList &sc_list, bool append) const {
398 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
401 size_t initial_size = sc_list.GetSize();
403 collection::const_iterator pos, end = m_modules.end();
404 for (pos = m_modules.begin(); pos != end; ++pos)
405 (*pos)->FindSymbolsMatchingRegExAndType(regex, symbol_type, sc_list);
406 return sc_list.GetSize() - initial_size;
409 size_t ModuleList::FindModules(const ModuleSpec &module_spec,
410 ModuleList &matching_module_list) const {
411 size_t existing_matches = matching_module_list.GetSize();
413 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
414 collection::const_iterator pos, end = m_modules.end();
415 for (pos = m_modules.begin(); pos != end; ++pos) {
416 ModuleSP module_sp(*pos);
417 if (module_sp->MatchesModuleSpec(module_spec))
418 matching_module_list.Append(module_sp);
420 return matching_module_list.GetSize() - existing_matches;
423 ModuleSP ModuleList::FindModule(const Module *module_ptr) const {
426 // Scope for "locker"
428 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
429 collection::const_iterator pos, end = m_modules.end();
431 for (pos = m_modules.begin(); pos != end; ++pos) {
432 if ((*pos).get() == module_ptr) {
441 ModuleSP ModuleList::FindModule(const UUID &uuid) const {
444 if (uuid.IsValid()) {
445 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
446 collection::const_iterator pos, end = m_modules.end();
448 for (pos = m_modules.begin(); pos != end; ++pos) {
449 if ((*pos)->GetUUID() == uuid) {
459 ModuleList::FindTypes(const SymbolContext &sc, const ConstString &name,
460 bool name_is_fully_qualified, size_t max_matches,
461 llvm::DenseSet<SymbolFile *> &searched_symbol_files,
462 TypeList &types) const {
463 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
465 size_t total_matches = 0;
466 collection::const_iterator pos, end = m_modules.end();
468 // The symbol context "sc" contains a module so we want to search that
469 // one first if it is in our list...
470 for (pos = m_modules.begin(); pos != end; ++pos) {
471 if (sc.module_sp.get() == (*pos).get()) {
473 (*pos)->FindTypes(sc, name, name_is_fully_qualified, max_matches,
474 searched_symbol_files, types);
476 if (total_matches >= max_matches)
482 if (total_matches < max_matches) {
483 SymbolContext world_sc;
484 for (pos = m_modules.begin(); pos != end; ++pos) {
485 // Search the module if the module is not equal to the one in the symbol
486 // context "sc". If "sc" contains a empty module shared pointer, then
487 // the comparison will always be true (valid_module_ptr != nullptr).
488 if (sc.module_sp.get() != (*pos).get())
490 (*pos)->FindTypes(world_sc, name, name_is_fully_qualified,
491 max_matches, searched_symbol_files, types);
493 if (total_matches >= max_matches)
498 return total_matches;
501 bool ModuleList::FindSourceFile(const FileSpec &orig_spec,
502 FileSpec &new_spec) const {
503 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
504 collection::const_iterator pos, end = m_modules.end();
505 for (pos = m_modules.begin(); pos != end; ++pos) {
506 if ((*pos)->FindSourceFile(orig_spec, new_spec))
512 void ModuleList::FindAddressesForLine(const lldb::TargetSP target_sp,
513 const FileSpec &file, uint32_t line,
515 std::vector<Address> &output_local,
516 std::vector<Address> &output_extern) {
517 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
518 collection::const_iterator pos, end = m_modules.end();
519 for (pos = m_modules.begin(); pos != end; ++pos) {
520 (*pos)->FindAddressesForLine(target_sp, file, line, function, output_local,
525 ModuleSP ModuleList::FindFirstModule(const ModuleSpec &module_spec) const {
527 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
528 collection::const_iterator pos, end = m_modules.end();
529 for (pos = m_modules.begin(); pos != end; ++pos) {
530 ModuleSP module_sp(*pos);
531 if (module_sp->MatchesModuleSpec(module_spec))
537 size_t ModuleList::GetSize() const {
540 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
541 size = m_modules.size();
546 void ModuleList::Dump(Stream *s) const {
547 // s.Printf("%.*p: ", (int)sizeof(void*) * 2, this);
549 // s << "ModuleList\n";
551 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
552 collection::const_iterator pos, end = m_modules.end();
553 for (pos = m_modules.begin(); pos != end; ++pos) {
558 void ModuleList::LogUUIDAndPaths(Log *log, const char *prefix_cstr) {
559 if (log != nullptr) {
560 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
561 collection::const_iterator pos, begin = m_modules.begin(),
562 end = m_modules.end();
563 for (pos = begin; pos != end; ++pos) {
564 Module *module = pos->get();
565 const FileSpec &module_file_spec = module->GetFileSpec();
566 log->Printf("%s[%u] %s (%s) \"%s\"", prefix_cstr ? prefix_cstr : "",
567 (uint32_t)std::distance(begin, pos),
568 module->GetUUID().GetAsString().c_str(),
569 module->GetArchitecture().GetArchitectureName(),
570 module_file_spec.GetPath().c_str());
575 bool ModuleList::ResolveFileAddress(lldb::addr_t vm_addr,
576 Address &so_addr) const {
577 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
578 collection::const_iterator pos, end = m_modules.end();
579 for (pos = m_modules.begin(); pos != end; ++pos) {
580 if ((*pos)->ResolveFileAddress(vm_addr, so_addr))
587 uint32_t ModuleList::ResolveSymbolContextForAddress(const Address &so_addr,
588 uint32_t resolve_scope,
589 SymbolContext &sc) const {
590 // The address is already section offset so it has a module
591 uint32_t resolved_flags = 0;
592 ModuleSP module_sp(so_addr.GetModule());
595 module_sp->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
597 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
598 collection::const_iterator pos, end = m_modules.end();
599 for (pos = m_modules.begin(); pos != end; ++pos) {
601 (*pos)->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
602 if (resolved_flags != 0)
607 return resolved_flags;
610 uint32_t ModuleList::ResolveSymbolContextForFilePath(
611 const char *file_path, uint32_t line, bool check_inlines,
612 uint32_t resolve_scope, SymbolContextList &sc_list) const {
613 FileSpec file_spec(file_path, false);
614 return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
615 resolve_scope, sc_list);
618 uint32_t ModuleList::ResolveSymbolContextsForFileSpec(
619 const FileSpec &file_spec, uint32_t line, bool check_inlines,
620 uint32_t resolve_scope, SymbolContextList &sc_list) const {
621 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
622 collection::const_iterator pos, end = m_modules.end();
623 for (pos = m_modules.begin(); pos != end; ++pos) {
624 (*pos)->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
625 resolve_scope, sc_list);
628 return sc_list.GetSize();
631 size_t ModuleList::GetIndexForModule(const Module *module) const {
633 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
634 collection::const_iterator pos;
635 collection::const_iterator begin = m_modules.begin();
636 collection::const_iterator end = m_modules.end();
637 for (pos = begin; pos != end; ++pos) {
638 if ((*pos).get() == module)
639 return std::distance(begin, pos);
642 return LLDB_INVALID_INDEX32;
645 static ModuleList &GetSharedModuleList() {
646 static ModuleList *g_shared_module_list = nullptr;
647 static std::once_flag g_once_flag;
648 std::call_once(g_once_flag, []() {
649 // NOTE: Intentionally leak the module list so a program doesn't have to
650 // cleanup all modules and object files as it exits. This just wastes time
651 // doing a bunch of cleanup that isn't required.
652 if (g_shared_module_list == nullptr)
653 g_shared_module_list = new ModuleList(); // <--- Intentional leak!!!
655 return *g_shared_module_list;
658 bool ModuleList::ModuleIsInCache(const Module *module_ptr) {
660 ModuleList &shared_module_list = GetSharedModuleList();
661 return shared_module_list.FindModule(module_ptr).get() != nullptr;
666 size_t ModuleList::FindSharedModules(const ModuleSpec &module_spec,
667 ModuleList &matching_module_list) {
668 return GetSharedModuleList().FindModules(module_spec, matching_module_list);
671 size_t ModuleList::RemoveOrphanSharedModules(bool mandatory) {
672 return GetSharedModuleList().RemoveOrphans(mandatory);
675 Error ModuleList::GetSharedModule(const ModuleSpec &module_spec,
677 const FileSpecList *module_search_paths_ptr,
678 ModuleSP *old_module_sp_ptr,
679 bool *did_create_ptr, bool always_create) {
680 ModuleList &shared_module_list = GetSharedModuleList();
681 std::lock_guard<std::recursive_mutex> guard(
682 shared_module_list.m_modules_mutex);
690 *did_create_ptr = false;
691 if (old_module_sp_ptr)
692 old_module_sp_ptr->reset();
694 const UUID *uuid_ptr = module_spec.GetUUIDPtr();
695 const FileSpec &module_file_spec = module_spec.GetFileSpec();
696 const ArchSpec &arch = module_spec.GetArchitecture();
698 // Make sure no one else can try and get or create a module while this
699 // function is actively working on it by doing an extra lock on the
700 // global mutex list.
701 if (!always_create) {
702 ModuleList matching_module_list;
703 const size_t num_matching_modules =
704 shared_module_list.FindModules(module_spec, matching_module_list);
705 if (num_matching_modules > 0) {
706 for (size_t module_idx = 0; module_idx < num_matching_modules;
708 module_sp = matching_module_list.GetModuleAtIndex(module_idx);
710 // Make sure the file for the module hasn't been modified
711 if (module_sp->FileHasChanged()) {
712 if (old_module_sp_ptr && !*old_module_sp_ptr)
713 *old_module_sp_ptr = module_sp;
715 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_MODULES));
717 log->Printf("module changed: %p, removing from global module list",
718 static_cast<void *>(module_sp.get()));
720 shared_module_list.Remove(module_sp);
723 // The module matches and the module was not modified from
724 // when it was last loaded.
734 module_sp.reset(new Module(module_spec));
735 // Make sure there are a module and an object file since we can specify
736 // a valid file path with an architecture that might not be in that file.
737 // By getting the object file we can guarantee that the architecture matches
738 if (module_sp->GetObjectFile()) {
739 // If we get in here we got the correct arch, now we just need
740 // to verify the UUID if one was given
741 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
744 if (module_sp->GetObjectFile() &&
745 module_sp->GetObjectFile()->GetType() ==
746 ObjectFile::eTypeStubLibrary) {
749 if (did_create_ptr) {
750 *did_create_ptr = true;
753 shared_module_list.ReplaceEquivalent(module_sp);
761 if (module_search_paths_ptr) {
762 const auto num_directories = module_search_paths_ptr->GetSize();
763 for (size_t idx = 0; idx < num_directories; ++idx) {
764 auto search_path_spec = module_search_paths_ptr->GetFileSpecAtIndex(idx);
765 if (!search_path_spec.ResolvePath())
767 if (!search_path_spec.Exists() || !search_path_spec.IsDirectory())
769 search_path_spec.AppendPathComponent(
770 module_spec.GetFileSpec().GetFilename().AsCString());
771 if (!search_path_spec.Exists())
774 auto resolved_module_spec(module_spec);
775 resolved_module_spec.GetFileSpec() = search_path_spec;
776 module_sp.reset(new Module(resolved_module_spec));
777 if (module_sp->GetObjectFile()) {
778 // If we get in here we got the correct arch, now we just need
779 // to verify the UUID if one was given
780 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
783 if (module_sp->GetObjectFile()->GetType() ==
784 ObjectFile::eTypeStubLibrary) {
788 *did_create_ptr = true;
790 shared_module_list.ReplaceEquivalent(module_sp);
800 // Either the file didn't exist where at the path, or no path was given, so
801 // we now have to use more extreme measures to try and find the appropriate
804 // Fixup the incoming path in case the path points to a valid file, yet
805 // the arch or UUID (if one was passed in) don't match.
806 ModuleSpec located_binary_modulespec =
807 Symbols::LocateExecutableObjectFile(module_spec);
809 // Don't look for the file if it appears to be the same one we already
810 // checked for above...
811 if (located_binary_modulespec.GetFileSpec() != module_file_spec) {
812 if (!located_binary_modulespec.GetFileSpec().Exists()) {
813 located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path));
815 module_file_spec.GetPath(path, sizeof(path));
816 // How can this check ever be true? This branch it is false, and we
817 // haven't modified file_spec.
818 if (located_binary_modulespec.GetFileSpec().Exists()) {
819 std::string uuid_str;
820 if (uuid_ptr && uuid_ptr->IsValid())
821 uuid_str = uuid_ptr->GetAsString();
823 if (arch.IsValid()) {
824 if (!uuid_str.empty())
825 error.SetErrorStringWithFormat(
826 "'%s' does not contain the %s architecture and UUID %s", path,
827 arch.GetArchitectureName(), uuid_str.c_str());
829 error.SetErrorStringWithFormat(
830 "'%s' does not contain the %s architecture.", path,
831 arch.GetArchitectureName());
834 error.SetErrorStringWithFormat("'%s' does not exist", path);
841 // Make sure no one else can try and get or create a module while this
842 // function is actively working on it by doing an extra lock on the
843 // global mutex list.
844 ModuleSpec platform_module_spec(module_spec);
845 platform_module_spec.GetFileSpec() =
846 located_binary_modulespec.GetFileSpec();
847 platform_module_spec.GetPlatformFileSpec() =
848 located_binary_modulespec.GetFileSpec();
849 platform_module_spec.GetSymbolFileSpec() =
850 located_binary_modulespec.GetSymbolFileSpec();
851 ModuleList matching_module_list;
852 if (shared_module_list.FindModules(platform_module_spec,
853 matching_module_list) > 0) {
854 module_sp = matching_module_list.GetModuleAtIndex(0);
856 // If we didn't have a UUID in mind when looking for the object file,
857 // then we should make sure the modification time hasn't changed!
858 if (platform_module_spec.GetUUIDPtr() == nullptr) {
859 auto file_spec_mod_time = FileSystem::GetModificationTime(
860 located_binary_modulespec.GetFileSpec());
861 if (file_spec_mod_time != llvm::sys::TimePoint<>()) {
862 if (file_spec_mod_time != module_sp->GetModificationTime()) {
863 if (old_module_sp_ptr)
864 *old_module_sp_ptr = module_sp;
865 shared_module_list.Remove(module_sp);
873 module_sp.reset(new Module(platform_module_spec));
874 // Make sure there are a module and an object file since we can specify
875 // a valid file path with an architecture that might not be in that file.
876 // By getting the object file we can guarantee that the architecture
878 if (module_sp && module_sp->GetObjectFile()) {
879 if (module_sp->GetObjectFile()->GetType() ==
880 ObjectFile::eTypeStubLibrary) {
884 *did_create_ptr = true;
886 shared_module_list.ReplaceEquivalent(module_sp);
889 located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path));
891 if (located_binary_modulespec.GetFileSpec()) {
893 error.SetErrorStringWithFormat(
894 "unable to open %s architecture in '%s'",
895 arch.GetArchitectureName(), path);
897 error.SetErrorStringWithFormat("unable to open '%s'", path);
899 std::string uuid_str;
900 if (uuid_ptr && uuid_ptr->IsValid())
901 uuid_str = uuid_ptr->GetAsString();
903 if (!uuid_str.empty())
904 error.SetErrorStringWithFormat(
905 "cannot locate a module for UUID '%s'", uuid_str.c_str());
907 error.SetErrorStringWithFormat("cannot locate a module");
916 bool ModuleList::RemoveSharedModule(lldb::ModuleSP &module_sp) {
917 return GetSharedModuleList().Remove(module_sp);
920 bool ModuleList::RemoveSharedModuleIfOrphaned(const Module *module_ptr) {
921 return GetSharedModuleList().RemoveIfOrphaned(module_ptr);
924 bool ModuleList::LoadScriptingResourcesInTarget(Target *target,
925 std::list<Error> &errors,
926 Stream *feedback_stream,
927 bool continue_on_error) {
930 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
931 for (auto module : m_modules) {
934 if (!module->LoadScriptingResourceInTarget(target, error,
936 if (error.Fail() && error.AsCString()) {
937 error.SetErrorStringWithFormat("unable to load scripting data for "
938 "module %s - error reported was %s",
939 module->GetFileSpec()
940 .GetFileNameStrippingExtension()
943 errors.push_back(error);
945 if (!continue_on_error)
951 return errors.empty();
954 void ModuleList::ForEach(
955 std::function<bool(const ModuleSP &module_sp)> const &callback) const {
956 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
957 for (const auto &module : m_modules) {
958 // If the callback returns false, then stop iterating and break out
959 if (!callback(module))