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" // for FileSpecList
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/Symbol/ObjectFile.h"
17 #include "lldb/Symbol/SymbolContext.h" // for SymbolContextList, SymbolCon...
18 #include "lldb/Symbol/VariableList.h"
19 #include "lldb/Utility/ArchSpec.h" // for ArchSpec
20 #include "lldb/Utility/ConstString.h" // for ConstString
21 #include "lldb/Utility/Log.h"
22 #include "lldb/Utility/Logging.h" // for GetLogIfAnyCategoriesSet
23 #include "lldb/Utility/UUID.h" // for UUID, operator!=, operator==
24 #include "lldb/lldb-defines.h" // for LLDB_INVALID_INDEX32
26 #if defined(LLVM_ON_WIN32)
27 #include "lldb/Host/windows/PosixApi.h" // for PATH_MAX
30 #include "llvm/ADT/StringRef.h" // for StringRef
31 #include "llvm/Support/FileSystem.h"
32 #include "llvm/Support/Threading.h"
33 #include "llvm/Support/raw_ostream.h" // for fs
35 #include <chrono> // for operator!=, time_point
36 #include <memory> // for shared_ptr
38 #include <string> // for string
39 #include <utility> // for distance
41 namespace lldb_private {
44 namespace lldb_private {
45 class RegularExpression;
47 namespace lldb_private {
50 namespace lldb_private {
53 namespace lldb_private {
56 namespace lldb_private {
61 using namespace lldb_private;
63 ModuleList::ModuleList()
64 : m_modules(), m_modules_mutex(), m_notifier(nullptr) {}
66 ModuleList::ModuleList(const ModuleList &rhs)
67 : m_modules(), m_modules_mutex(), m_notifier(nullptr) {
68 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
69 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
70 m_modules = rhs.m_modules;
73 ModuleList::ModuleList(ModuleList::Notifier *notifier)
74 : m_modules(), m_modules_mutex(), m_notifier(notifier) {}
76 const ModuleList &ModuleList::operator=(const ModuleList &rhs) {
78 // That's probably me nit-picking, but in theoretical situation:
80 // * that two threads A B and
81 // * two ModuleList's x y do opposite assignments ie.:
83 // in thread A: | in thread B:
86 // This establishes correct(same) lock taking order and thus
87 // avoids priority inversion.
88 if (uintptr_t(this) > uintptr_t(&rhs)) {
89 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
90 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
91 m_modules = rhs.m_modules;
93 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
94 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
95 m_modules = rhs.m_modules;
101 ModuleList::~ModuleList() = default;
103 void ModuleList::AppendImpl(const ModuleSP &module_sp, bool use_notifier) {
105 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
106 m_modules.push_back(module_sp);
107 if (use_notifier && m_notifier)
108 m_notifier->ModuleAdded(*this, module_sp);
112 void ModuleList::Append(const ModuleSP &module_sp) { AppendImpl(module_sp); }
114 void ModuleList::ReplaceEquivalent(const ModuleSP &module_sp) {
116 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
118 // First remove any equivalent modules. Equivalent modules are modules
119 // whose path, platform path and architecture match.
120 ModuleSpec equivalent_module_spec(module_sp->GetFileSpec(),
121 module_sp->GetArchitecture());
122 equivalent_module_spec.GetPlatformFileSpec() =
123 module_sp->GetPlatformFileSpec();
126 while (idx < m_modules.size()) {
127 ModuleSP module_sp(m_modules[idx]);
128 if (module_sp->MatchesModuleSpec(equivalent_module_spec))
129 RemoveImpl(m_modules.begin() + idx);
133 // Now add the new module to the list
138 bool ModuleList::AppendIfNeeded(const ModuleSP &module_sp) {
140 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
141 collection::iterator pos, end = m_modules.end();
142 for (pos = m_modules.begin(); pos != end; ++pos) {
143 if (pos->get() == module_sp.get())
144 return false; // Already in the list
146 // Only push module_sp on the list if it wasn't already in there.
153 void ModuleList::Append(const ModuleList &module_list) {
154 for (auto pos : module_list.m_modules)
158 bool ModuleList::AppendIfNeeded(const ModuleList &module_list) {
160 for (auto pos : module_list.m_modules) {
161 if (AppendIfNeeded(pos))
167 bool ModuleList::RemoveImpl(const ModuleSP &module_sp, bool use_notifier) {
169 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
170 collection::iterator pos, end = m_modules.end();
171 for (pos = m_modules.begin(); pos != end; ++pos) {
172 if (pos->get() == module_sp.get()) {
173 m_modules.erase(pos);
174 if (use_notifier && m_notifier)
175 m_notifier->ModuleRemoved(*this, module_sp);
183 ModuleList::collection::iterator
184 ModuleList::RemoveImpl(ModuleList::collection::iterator pos,
186 ModuleSP module_sp(*pos);
187 collection::iterator retval = m_modules.erase(pos);
188 if (use_notifier && m_notifier)
189 m_notifier->ModuleRemoved(*this, module_sp);
193 bool ModuleList::Remove(const ModuleSP &module_sp) {
194 return RemoveImpl(module_sp);
197 bool ModuleList::ReplaceModule(const lldb::ModuleSP &old_module_sp,
198 const lldb::ModuleSP &new_module_sp) {
199 if (!RemoveImpl(old_module_sp, false))
201 AppendImpl(new_module_sp, false);
203 m_notifier->ModuleUpdated(*this, old_module_sp, new_module_sp);
207 bool ModuleList::RemoveIfOrphaned(const Module *module_ptr) {
209 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
210 collection::iterator pos, end = m_modules.end();
211 for (pos = m_modules.begin(); pos != end; ++pos) {
212 if (pos->get() == module_ptr) {
214 pos = RemoveImpl(pos);
224 size_t ModuleList::RemoveOrphans(bool mandatory) {
225 std::unique_lock<std::recursive_mutex> lock(m_modules_mutex, std::defer_lock);
230 // Not mandatory, remove orphans if we can get the mutex
231 if (!lock.try_lock())
234 collection::iterator pos = m_modules.begin();
235 size_t remove_count = 0;
236 while (pos != m_modules.end()) {
238 pos = RemoveImpl(pos);
247 size_t ModuleList::Remove(ModuleList &module_list) {
248 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
249 size_t num_removed = 0;
250 collection::iterator pos, end = module_list.m_modules.end();
251 for (pos = module_list.m_modules.begin(); pos != end; ++pos) {
258 void ModuleList::Clear() { ClearImpl(); }
260 void ModuleList::Destroy() { ClearImpl(); }
262 void ModuleList::ClearImpl(bool use_notifier) {
263 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
264 if (use_notifier && m_notifier)
265 m_notifier->WillClearList(*this);
269 Module *ModuleList::GetModulePointerAtIndex(size_t idx) const {
270 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
271 return GetModulePointerAtIndexUnlocked(idx);
274 Module *ModuleList::GetModulePointerAtIndexUnlocked(size_t idx) const {
275 if (idx < m_modules.size())
276 return m_modules[idx].get();
280 ModuleSP ModuleList::GetModuleAtIndex(size_t idx) const {
281 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
282 return GetModuleAtIndexUnlocked(idx);
285 ModuleSP ModuleList::GetModuleAtIndexUnlocked(size_t idx) const {
287 if (idx < m_modules.size())
288 module_sp = m_modules[idx];
292 size_t ModuleList::FindFunctions(const ConstString &name,
293 uint32_t name_type_mask, bool include_symbols,
294 bool include_inlines, bool append,
295 SymbolContextList &sc_list) const {
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)->FindFunctions(lookup_info.GetLookupName(), nullptr,
308 lookup_info.GetNameTypeMask(), include_symbols,
309 include_inlines, true, sc_list);
312 const size_t new_size = sc_list.GetSize();
314 if (old_size < new_size)
315 lookup_info.Prune(sc_list, old_size);
317 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
318 collection::const_iterator pos, end = m_modules.end();
319 for (pos = m_modules.begin(); pos != end; ++pos) {
320 (*pos)->FindFunctions(name, nullptr, name_type_mask, include_symbols,
321 include_inlines, true, sc_list);
324 return sc_list.GetSize() - old_size;
327 size_t ModuleList::FindFunctionSymbols(const ConstString &name,
328 uint32_t name_type_mask,
329 SymbolContextList &sc_list) {
330 const size_t old_size = sc_list.GetSize();
332 if (name_type_mask & eFunctionNameTypeAuto) {
333 Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
335 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
336 collection::const_iterator pos, end = m_modules.end();
337 for (pos = m_modules.begin(); pos != end; ++pos) {
338 (*pos)->FindFunctionSymbols(lookup_info.GetLookupName(),
339 lookup_info.GetNameTypeMask(), sc_list);
342 const size_t new_size = sc_list.GetSize();
344 if (old_size < new_size)
345 lookup_info.Prune(sc_list, old_size);
347 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
348 collection::const_iterator pos, end = m_modules.end();
349 for (pos = m_modules.begin(); pos != end; ++pos) {
350 (*pos)->FindFunctionSymbols(name, name_type_mask, sc_list);
354 return sc_list.GetSize() - old_size;
357 size_t ModuleList::FindFunctions(const RegularExpression &name,
358 bool include_symbols, bool include_inlines,
359 bool append, SymbolContextList &sc_list) {
360 const size_t old_size = sc_list.GetSize();
362 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
363 collection::const_iterator pos, end = m_modules.end();
364 for (pos = m_modules.begin(); pos != end; ++pos) {
365 (*pos)->FindFunctions(name, include_symbols, include_inlines, append,
369 return sc_list.GetSize() - old_size;
372 size_t ModuleList::FindCompileUnits(const FileSpec &path, bool append,
373 SymbolContextList &sc_list) const {
377 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
378 collection::const_iterator pos, end = m_modules.end();
379 for (pos = m_modules.begin(); pos != end; ++pos) {
380 (*pos)->FindCompileUnits(path, true, sc_list);
383 return sc_list.GetSize();
386 size_t ModuleList::FindGlobalVariables(const ConstString &name, bool append,
388 VariableList &variable_list) const {
389 size_t initial_size = variable_list.GetSize();
390 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
391 collection::const_iterator pos, end = m_modules.end();
392 for (pos = m_modules.begin(); pos != end; ++pos) {
393 (*pos)->FindGlobalVariables(name, nullptr, append, max_matches,
396 return variable_list.GetSize() - initial_size;
399 size_t ModuleList::FindGlobalVariables(const RegularExpression ®ex,
400 bool append, size_t max_matches,
401 VariableList &variable_list) const {
402 size_t initial_size = variable_list.GetSize();
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)->FindGlobalVariables(regex, append, max_matches, variable_list);
408 return variable_list.GetSize() - initial_size;
411 size_t ModuleList::FindSymbolsWithNameAndType(const ConstString &name,
412 SymbolType symbol_type,
413 SymbolContextList &sc_list,
415 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
418 size_t initial_size = sc_list.GetSize();
420 collection::const_iterator pos, end = m_modules.end();
421 for (pos = m_modules.begin(); pos != end; ++pos)
422 (*pos)->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
423 return sc_list.GetSize() - initial_size;
426 size_t ModuleList::FindSymbolsMatchingRegExAndType(
427 const RegularExpression ®ex, lldb::SymbolType symbol_type,
428 SymbolContextList &sc_list, bool append) const {
429 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
432 size_t initial_size = sc_list.GetSize();
434 collection::const_iterator pos, end = m_modules.end();
435 for (pos = m_modules.begin(); pos != end; ++pos)
436 (*pos)->FindSymbolsMatchingRegExAndType(regex, symbol_type, sc_list);
437 return sc_list.GetSize() - initial_size;
440 size_t ModuleList::FindModules(const ModuleSpec &module_spec,
441 ModuleList &matching_module_list) const {
442 size_t existing_matches = matching_module_list.GetSize();
444 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
445 collection::const_iterator pos, end = m_modules.end();
446 for (pos = m_modules.begin(); pos != end; ++pos) {
447 ModuleSP module_sp(*pos);
448 if (module_sp->MatchesModuleSpec(module_spec))
449 matching_module_list.Append(module_sp);
451 return matching_module_list.GetSize() - existing_matches;
454 ModuleSP ModuleList::FindModule(const Module *module_ptr) const {
457 // Scope for "locker"
459 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
460 collection::const_iterator pos, end = m_modules.end();
462 for (pos = m_modules.begin(); pos != end; ++pos) {
463 if ((*pos).get() == module_ptr) {
472 ModuleSP ModuleList::FindModule(const UUID &uuid) const {
475 if (uuid.IsValid()) {
476 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
477 collection::const_iterator pos, end = m_modules.end();
479 for (pos = m_modules.begin(); pos != end; ++pos) {
480 if ((*pos)->GetUUID() == uuid) {
490 ModuleList::FindTypes(const SymbolContext &sc, const ConstString &name,
491 bool name_is_fully_qualified, size_t max_matches,
492 llvm::DenseSet<SymbolFile *> &searched_symbol_files,
493 TypeList &types) const {
494 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
496 size_t total_matches = 0;
497 collection::const_iterator pos, end = m_modules.end();
499 // The symbol context "sc" contains a module so we want to search that
500 // one first if it is in our list...
501 for (pos = m_modules.begin(); pos != end; ++pos) {
502 if (sc.module_sp.get() == (*pos).get()) {
504 (*pos)->FindTypes(sc, name, name_is_fully_qualified, max_matches,
505 searched_symbol_files, types);
507 if (total_matches >= max_matches)
513 if (total_matches < max_matches) {
514 SymbolContext world_sc;
515 for (pos = m_modules.begin(); pos != end; ++pos) {
516 // Search the module if the module is not equal to the one in the symbol
517 // context "sc". If "sc" contains a empty module shared pointer, then
518 // the comparison will always be true (valid_module_ptr != nullptr).
519 if (sc.module_sp.get() != (*pos).get())
521 (*pos)->FindTypes(world_sc, name, name_is_fully_qualified,
522 max_matches, searched_symbol_files, types);
524 if (total_matches >= max_matches)
529 return total_matches;
532 bool ModuleList::FindSourceFile(const FileSpec &orig_spec,
533 FileSpec &new_spec) const {
534 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
535 collection::const_iterator pos, end = m_modules.end();
536 for (pos = m_modules.begin(); pos != end; ++pos) {
537 if ((*pos)->FindSourceFile(orig_spec, new_spec))
543 void ModuleList::FindAddressesForLine(const lldb::TargetSP target_sp,
544 const FileSpec &file, uint32_t line,
546 std::vector<Address> &output_local,
547 std::vector<Address> &output_extern) {
548 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
549 collection::const_iterator pos, end = m_modules.end();
550 for (pos = m_modules.begin(); pos != end; ++pos) {
551 (*pos)->FindAddressesForLine(target_sp, file, line, function, output_local,
556 ModuleSP ModuleList::FindFirstModule(const ModuleSpec &module_spec) const {
558 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
559 collection::const_iterator pos, end = m_modules.end();
560 for (pos = m_modules.begin(); pos != end; ++pos) {
561 ModuleSP module_sp(*pos);
562 if (module_sp->MatchesModuleSpec(module_spec))
568 size_t ModuleList::GetSize() const {
571 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
572 size = m_modules.size();
577 void ModuleList::Dump(Stream *s) const {
578 // s.Printf("%.*p: ", (int)sizeof(void*) * 2, this);
580 // s << "ModuleList\n";
582 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
583 collection::const_iterator pos, end = m_modules.end();
584 for (pos = m_modules.begin(); pos != end; ++pos) {
589 void ModuleList::LogUUIDAndPaths(Log *log, const char *prefix_cstr) {
590 if (log != nullptr) {
591 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
592 collection::const_iterator pos, begin = m_modules.begin(),
593 end = m_modules.end();
594 for (pos = begin; pos != end; ++pos) {
595 Module *module = pos->get();
596 const FileSpec &module_file_spec = module->GetFileSpec();
597 log->Printf("%s[%u] %s (%s) \"%s\"", prefix_cstr ? prefix_cstr : "",
598 (uint32_t)std::distance(begin, pos),
599 module->GetUUID().GetAsString().c_str(),
600 module->GetArchitecture().GetArchitectureName(),
601 module_file_spec.GetPath().c_str());
606 bool ModuleList::ResolveFileAddress(lldb::addr_t vm_addr,
607 Address &so_addr) const {
608 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
609 collection::const_iterator pos, end = m_modules.end();
610 for (pos = m_modules.begin(); pos != end; ++pos) {
611 if ((*pos)->ResolveFileAddress(vm_addr, so_addr))
618 uint32_t ModuleList::ResolveSymbolContextForAddress(const Address &so_addr,
619 uint32_t resolve_scope,
620 SymbolContext &sc) const {
621 // The address is already section offset so it has a module
622 uint32_t resolved_flags = 0;
623 ModuleSP module_sp(so_addr.GetModule());
626 module_sp->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
628 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
629 collection::const_iterator pos, end = m_modules.end();
630 for (pos = m_modules.begin(); pos != end; ++pos) {
632 (*pos)->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
633 if (resolved_flags != 0)
638 return resolved_flags;
641 uint32_t ModuleList::ResolveSymbolContextForFilePath(
642 const char *file_path, uint32_t line, bool check_inlines,
643 uint32_t resolve_scope, SymbolContextList &sc_list) const {
644 FileSpec file_spec(file_path, false);
645 return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
646 resolve_scope, sc_list);
649 uint32_t ModuleList::ResolveSymbolContextsForFileSpec(
650 const FileSpec &file_spec, uint32_t line, bool check_inlines,
651 uint32_t resolve_scope, SymbolContextList &sc_list) const {
652 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
653 collection::const_iterator pos, end = m_modules.end();
654 for (pos = m_modules.begin(); pos != end; ++pos) {
655 (*pos)->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
656 resolve_scope, sc_list);
659 return sc_list.GetSize();
662 size_t ModuleList::GetIndexForModule(const Module *module) const {
664 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
665 collection::const_iterator pos;
666 collection::const_iterator begin = m_modules.begin();
667 collection::const_iterator end = m_modules.end();
668 for (pos = begin; pos != end; ++pos) {
669 if ((*pos).get() == module)
670 return std::distance(begin, pos);
673 return LLDB_INVALID_INDEX32;
676 static ModuleList &GetSharedModuleList() {
677 static ModuleList *g_shared_module_list = nullptr;
678 static llvm::once_flag g_once_flag;
679 llvm::call_once(g_once_flag, []() {
680 // NOTE: Intentionally leak the module list so a program doesn't have to
681 // cleanup all modules and object files as it exits. This just wastes time
682 // doing a bunch of cleanup that isn't required.
683 if (g_shared_module_list == nullptr)
684 g_shared_module_list = new ModuleList(); // <--- Intentional leak!!!
686 return *g_shared_module_list;
689 bool ModuleList::ModuleIsInCache(const Module *module_ptr) {
691 ModuleList &shared_module_list = GetSharedModuleList();
692 return shared_module_list.FindModule(module_ptr).get() != nullptr;
697 size_t ModuleList::FindSharedModules(const ModuleSpec &module_spec,
698 ModuleList &matching_module_list) {
699 return GetSharedModuleList().FindModules(module_spec, matching_module_list);
702 size_t ModuleList::RemoveOrphanSharedModules(bool mandatory) {
703 return GetSharedModuleList().RemoveOrphans(mandatory);
706 Status ModuleList::GetSharedModule(const ModuleSpec &module_spec,
708 const FileSpecList *module_search_paths_ptr,
709 ModuleSP *old_module_sp_ptr,
710 bool *did_create_ptr, bool always_create) {
711 ModuleList &shared_module_list = GetSharedModuleList();
712 std::lock_guard<std::recursive_mutex> guard(
713 shared_module_list.m_modules_mutex);
721 *did_create_ptr = false;
722 if (old_module_sp_ptr)
723 old_module_sp_ptr->reset();
725 const UUID *uuid_ptr = module_spec.GetUUIDPtr();
726 const FileSpec &module_file_spec = module_spec.GetFileSpec();
727 const ArchSpec &arch = module_spec.GetArchitecture();
729 // Make sure no one else can try and get or create a module while this
730 // function is actively working on it by doing an extra lock on the
731 // global mutex list.
732 if (!always_create) {
733 ModuleList matching_module_list;
734 const size_t num_matching_modules =
735 shared_module_list.FindModules(module_spec, matching_module_list);
736 if (num_matching_modules > 0) {
737 for (size_t module_idx = 0; module_idx < num_matching_modules;
739 module_sp = matching_module_list.GetModuleAtIndex(module_idx);
741 // Make sure the file for the module hasn't been modified
742 if (module_sp->FileHasChanged()) {
743 if (old_module_sp_ptr && !*old_module_sp_ptr)
744 *old_module_sp_ptr = module_sp;
746 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_MODULES));
748 log->Printf("module changed: %p, removing from global module list",
749 static_cast<void *>(module_sp.get()));
751 shared_module_list.Remove(module_sp);
754 // The module matches and the module was not modified from
755 // when it was last loaded.
765 module_sp.reset(new Module(module_spec));
766 // Make sure there are a module and an object file since we can specify
767 // a valid file path with an architecture that might not be in that file.
768 // By getting the object file we can guarantee that the architecture matches
769 if (module_sp->GetObjectFile()) {
770 // If we get in here we got the correct arch, now we just need
771 // to verify the UUID if one was given
772 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
775 if (module_sp->GetObjectFile() &&
776 module_sp->GetObjectFile()->GetType() ==
777 ObjectFile::eTypeStubLibrary) {
780 if (did_create_ptr) {
781 *did_create_ptr = true;
784 shared_module_list.ReplaceEquivalent(module_sp);
792 if (module_search_paths_ptr) {
793 const auto num_directories = module_search_paths_ptr->GetSize();
794 for (size_t idx = 0; idx < num_directories; ++idx) {
795 auto search_path_spec = module_search_paths_ptr->GetFileSpecAtIndex(idx);
796 if (!search_path_spec.ResolvePath())
798 namespace fs = llvm::sys::fs;
799 if (!fs::is_directory(search_path_spec.GetPath()))
801 search_path_spec.AppendPathComponent(
802 module_spec.GetFileSpec().GetFilename().AsCString());
803 if (!search_path_spec.Exists())
806 auto resolved_module_spec(module_spec);
807 resolved_module_spec.GetFileSpec() = search_path_spec;
808 module_sp.reset(new Module(resolved_module_spec));
809 if (module_sp->GetObjectFile()) {
810 // If we get in here we got the correct arch, now we just need
811 // to verify the UUID if one was given
812 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
815 if (module_sp->GetObjectFile()->GetType() ==
816 ObjectFile::eTypeStubLibrary) {
820 *did_create_ptr = true;
822 shared_module_list.ReplaceEquivalent(module_sp);
832 // Either the file didn't exist where at the path, or no path was given, so
833 // we now have to use more extreme measures to try and find the appropriate
836 // Fixup the incoming path in case the path points to a valid file, yet
837 // the arch or UUID (if one was passed in) don't match.
838 ModuleSpec located_binary_modulespec =
839 Symbols::LocateExecutableObjectFile(module_spec);
841 // Don't look for the file if it appears to be the same one we already
842 // checked for above...
843 if (located_binary_modulespec.GetFileSpec() != module_file_spec) {
844 if (!located_binary_modulespec.GetFileSpec().Exists()) {
845 located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path));
847 module_file_spec.GetPath(path, sizeof(path));
848 // How can this check ever be true? This branch it is false, and we
849 // haven't modified file_spec.
850 if (located_binary_modulespec.GetFileSpec().Exists()) {
851 std::string uuid_str;
852 if (uuid_ptr && uuid_ptr->IsValid())
853 uuid_str = uuid_ptr->GetAsString();
855 if (arch.IsValid()) {
856 if (!uuid_str.empty())
857 error.SetErrorStringWithFormat(
858 "'%s' does not contain the %s architecture and UUID %s", path,
859 arch.GetArchitectureName(), uuid_str.c_str());
861 error.SetErrorStringWithFormat(
862 "'%s' does not contain the %s architecture.", path,
863 arch.GetArchitectureName());
866 error.SetErrorStringWithFormat("'%s' does not exist", path);
873 // Make sure no one else can try and get or create a module while this
874 // function is actively working on it by doing an extra lock on the
875 // global mutex list.
876 ModuleSpec platform_module_spec(module_spec);
877 platform_module_spec.GetFileSpec() =
878 located_binary_modulespec.GetFileSpec();
879 platform_module_spec.GetPlatformFileSpec() =
880 located_binary_modulespec.GetFileSpec();
881 platform_module_spec.GetSymbolFileSpec() =
882 located_binary_modulespec.GetSymbolFileSpec();
883 ModuleList matching_module_list;
884 if (shared_module_list.FindModules(platform_module_spec,
885 matching_module_list) > 0) {
886 module_sp = matching_module_list.GetModuleAtIndex(0);
888 // If we didn't have a UUID in mind when looking for the object file,
889 // then we should make sure the modification time hasn't changed!
890 if (platform_module_spec.GetUUIDPtr() == nullptr) {
891 auto file_spec_mod_time = FileSystem::GetModificationTime(
892 located_binary_modulespec.GetFileSpec());
893 if (file_spec_mod_time != llvm::sys::TimePoint<>()) {
894 if (file_spec_mod_time != module_sp->GetModificationTime()) {
895 if (old_module_sp_ptr)
896 *old_module_sp_ptr = module_sp;
897 shared_module_list.Remove(module_sp);
905 module_sp.reset(new Module(platform_module_spec));
906 // Make sure there are a module and an object file since we can specify
907 // a valid file path with an architecture that might not be in that file.
908 // By getting the object file we can guarantee that the architecture
910 if (module_sp && module_sp->GetObjectFile()) {
911 if (module_sp->GetObjectFile()->GetType() ==
912 ObjectFile::eTypeStubLibrary) {
916 *did_create_ptr = true;
918 shared_module_list.ReplaceEquivalent(module_sp);
921 located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path));
923 if (located_binary_modulespec.GetFileSpec()) {
925 error.SetErrorStringWithFormat(
926 "unable to open %s architecture in '%s'",
927 arch.GetArchitectureName(), path);
929 error.SetErrorStringWithFormat("unable to open '%s'", path);
931 std::string uuid_str;
932 if (uuid_ptr && uuid_ptr->IsValid())
933 uuid_str = uuid_ptr->GetAsString();
935 if (!uuid_str.empty())
936 error.SetErrorStringWithFormat(
937 "cannot locate a module for UUID '%s'", uuid_str.c_str());
939 error.SetErrorStringWithFormat("cannot locate a module");
948 bool ModuleList::RemoveSharedModule(lldb::ModuleSP &module_sp) {
949 return GetSharedModuleList().Remove(module_sp);
952 bool ModuleList::RemoveSharedModuleIfOrphaned(const Module *module_ptr) {
953 return GetSharedModuleList().RemoveIfOrphaned(module_ptr);
956 bool ModuleList::LoadScriptingResourcesInTarget(Target *target,
957 std::list<Status> &errors,
958 Stream *feedback_stream,
959 bool continue_on_error) {
962 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
963 for (auto module : m_modules) {
966 if (!module->LoadScriptingResourceInTarget(target, error,
968 if (error.Fail() && error.AsCString()) {
969 error.SetErrorStringWithFormat("unable to load scripting data for "
970 "module %s - error reported was %s",
971 module->GetFileSpec()
972 .GetFileNameStrippingExtension()
975 errors.push_back(error);
977 if (!continue_on_error)
983 return errors.empty();
986 void ModuleList::ForEach(
987 std::function<bool(const ModuleSP &module_sp)> const &callback) const {
988 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
989 for (const auto &module : m_modules) {
990 // If the callback returns false, then stop iterating and break out
991 if (!callback(module))