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/Interpreter/OptionValueProperties.h"
17 #include "lldb/Interpreter/OptionValueFileSpec.h"
18 #include "lldb/Interpreter/Property.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Symbol/SymbolContext.h" // for SymbolContextList, SymbolCon...
21 #include "lldb/Symbol/VariableList.h"
22 #include "lldb/Utility/ArchSpec.h" // for ArchSpec
23 #include "lldb/Utility/ConstString.h" // for ConstString
24 #include "lldb/Utility/Log.h"
25 #include "lldb/Utility/Logging.h" // for GetLogIfAnyCategoriesSet
26 #include "lldb/Utility/UUID.h" // for UUID, operator!=, operator==
27 #include "lldb/lldb-defines.h" // for LLDB_INVALID_INDEX32
30 #include "lldb/Host/windows/PosixApi.h" // for PATH_MAX
33 #include "llvm/ADT/StringRef.h" // for StringRef
34 #include "llvm/Support/FileSystem.h"
35 #include "llvm/Support/Threading.h"
36 #include "llvm/Support/raw_ostream.h" // for fs
37 #include "clang/Driver/Driver.h"
39 #include <chrono> // for operator!=, time_point
40 #include <memory> // for shared_ptr
42 #include <string> // for string
43 #include <utility> // for distance
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 PropertyDefinition g_properties[] = {
70 {"enable-external-lookup", OptionValue::eTypeBoolean, true, true, nullptr,
72 "Control the use of external tools or libraries to locate symbol files. "
73 "On macOS, Spotlight is used to locate a matching .dSYM bundle based on "
74 "the UUID of the executable."},
75 {"clang-modules-cache-path", OptionValue::eTypeFileSpec, true, 0, nullptr,
77 "The path to the clang modules cache directory (-fmodules-cache-path)."},
78 {nullptr, OptionValue::eTypeInvalid, false, 0, nullptr, nullptr, nullptr}};
80 enum { ePropertyEnableExternalLookup, ePropertyClangModulesCachePath };
84 ModuleListProperties::ModuleListProperties() {
85 m_collection_sp.reset(new OptionValueProperties(ConstString("symbols")));
86 m_collection_sp->Initialize(g_properties);
88 llvm::SmallString<128> path;
89 clang::driver::Driver::getDefaultModuleCachePath(path);
90 SetClangModulesCachePath(path);
93 bool ModuleListProperties::GetEnableExternalLookup() const {
94 const uint32_t idx = ePropertyEnableExternalLookup;
95 return m_collection_sp->GetPropertyAtIndexAsBoolean(
96 nullptr, idx, g_properties[idx].default_uint_value != 0);
99 FileSpec ModuleListProperties::GetClangModulesCachePath() const {
100 return m_collection_sp
101 ->GetPropertyAtIndexAsOptionValueFileSpec(nullptr, false,
102 ePropertyClangModulesCachePath)
106 bool ModuleListProperties::SetClangModulesCachePath(llvm::StringRef path) {
107 return m_collection_sp->SetPropertyAtIndexAsString(
108 nullptr, ePropertyClangModulesCachePath, path);
112 ModuleList::ModuleList()
113 : m_modules(), m_modules_mutex(), m_notifier(nullptr) {}
115 ModuleList::ModuleList(const ModuleList &rhs)
116 : m_modules(), m_modules_mutex(), m_notifier(nullptr) {
117 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
118 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
119 m_modules = rhs.m_modules;
122 ModuleList::ModuleList(ModuleList::Notifier *notifier)
123 : m_modules(), m_modules_mutex(), m_notifier(notifier) {}
125 const ModuleList &ModuleList::operator=(const ModuleList &rhs) {
127 // That's probably me nit-picking, but in theoretical situation:
129 // * that two threads A B and
130 // * two ModuleList's x y do opposite assignments ie.:
132 // in thread A: | in thread B:
135 // This establishes correct(same) lock taking order and thus avoids
136 // priority inversion.
137 if (uintptr_t(this) > uintptr_t(&rhs)) {
138 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
139 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
140 m_modules = rhs.m_modules;
142 std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
143 std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
144 m_modules = rhs.m_modules;
150 ModuleList::~ModuleList() = default;
152 void ModuleList::AppendImpl(const ModuleSP &module_sp, bool use_notifier) {
154 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
155 m_modules.push_back(module_sp);
156 if (use_notifier && m_notifier)
157 m_notifier->ModuleAdded(*this, module_sp);
161 void ModuleList::Append(const ModuleSP &module_sp) { AppendImpl(module_sp); }
163 void ModuleList::ReplaceEquivalent(const ModuleSP &module_sp) {
165 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
167 // First remove any equivalent modules. Equivalent modules are modules
168 // whose path, platform path and architecture match.
169 ModuleSpec equivalent_module_spec(module_sp->GetFileSpec(),
170 module_sp->GetArchitecture());
171 equivalent_module_spec.GetPlatformFileSpec() =
172 module_sp->GetPlatformFileSpec();
175 while (idx < m_modules.size()) {
176 ModuleSP module_sp(m_modules[idx]);
177 if (module_sp->MatchesModuleSpec(equivalent_module_spec))
178 RemoveImpl(m_modules.begin() + idx);
182 // Now add the new module to the list
187 bool ModuleList::AppendIfNeeded(const ModuleSP &module_sp) {
189 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
190 collection::iterator pos, end = m_modules.end();
191 for (pos = m_modules.begin(); pos != end; ++pos) {
192 if (pos->get() == module_sp.get())
193 return false; // Already in the list
195 // Only push module_sp on the list if it wasn't already in there.
202 void ModuleList::Append(const ModuleList &module_list) {
203 for (auto pos : module_list.m_modules)
207 bool ModuleList::AppendIfNeeded(const ModuleList &module_list) {
209 for (auto pos : module_list.m_modules) {
210 if (AppendIfNeeded(pos))
216 bool ModuleList::RemoveImpl(const ModuleSP &module_sp, bool use_notifier) {
218 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
219 collection::iterator pos, end = m_modules.end();
220 for (pos = m_modules.begin(); pos != end; ++pos) {
221 if (pos->get() == module_sp.get()) {
222 m_modules.erase(pos);
223 if (use_notifier && m_notifier)
224 m_notifier->ModuleRemoved(*this, module_sp);
232 ModuleList::collection::iterator
233 ModuleList::RemoveImpl(ModuleList::collection::iterator pos,
235 ModuleSP module_sp(*pos);
236 collection::iterator retval = m_modules.erase(pos);
237 if (use_notifier && m_notifier)
238 m_notifier->ModuleRemoved(*this, module_sp);
242 bool ModuleList::Remove(const ModuleSP &module_sp) {
243 return RemoveImpl(module_sp);
246 bool ModuleList::ReplaceModule(const lldb::ModuleSP &old_module_sp,
247 const lldb::ModuleSP &new_module_sp) {
248 if (!RemoveImpl(old_module_sp, false))
250 AppendImpl(new_module_sp, false);
252 m_notifier->ModuleUpdated(*this, old_module_sp, new_module_sp);
256 bool ModuleList::RemoveIfOrphaned(const Module *module_ptr) {
258 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
259 collection::iterator pos, end = m_modules.end();
260 for (pos = m_modules.begin(); pos != end; ++pos) {
261 if (pos->get() == module_ptr) {
263 pos = RemoveImpl(pos);
273 size_t ModuleList::RemoveOrphans(bool mandatory) {
274 std::unique_lock<std::recursive_mutex> lock(m_modules_mutex, std::defer_lock);
279 // Not mandatory, remove orphans if we can get the mutex
280 if (!lock.try_lock())
283 collection::iterator pos = m_modules.begin();
284 size_t remove_count = 0;
285 while (pos != m_modules.end()) {
287 pos = RemoveImpl(pos);
296 size_t ModuleList::Remove(ModuleList &module_list) {
297 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
298 size_t num_removed = 0;
299 collection::iterator pos, end = module_list.m_modules.end();
300 for (pos = module_list.m_modules.begin(); pos != end; ++pos) {
307 void ModuleList::Clear() { ClearImpl(); }
309 void ModuleList::Destroy() { ClearImpl(); }
311 void ModuleList::ClearImpl(bool use_notifier) {
312 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
313 if (use_notifier && m_notifier)
314 m_notifier->WillClearList(*this);
318 Module *ModuleList::GetModulePointerAtIndex(size_t idx) const {
319 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
320 return GetModulePointerAtIndexUnlocked(idx);
323 Module *ModuleList::GetModulePointerAtIndexUnlocked(size_t idx) const {
324 if (idx < m_modules.size())
325 return m_modules[idx].get();
329 ModuleSP ModuleList::GetModuleAtIndex(size_t idx) const {
330 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
331 return GetModuleAtIndexUnlocked(idx);
334 ModuleSP ModuleList::GetModuleAtIndexUnlocked(size_t idx) const {
336 if (idx < m_modules.size())
337 module_sp = m_modules[idx];
341 size_t ModuleList::FindFunctions(const ConstString &name,
342 uint32_t name_type_mask, bool include_symbols,
343 bool include_inlines, bool append,
344 SymbolContextList &sc_list) const {
348 const size_t old_size = sc_list.GetSize();
350 if (name_type_mask & eFunctionNameTypeAuto) {
351 Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
353 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
354 collection::const_iterator pos, end = m_modules.end();
355 for (pos = m_modules.begin(); pos != end; ++pos) {
356 (*pos)->FindFunctions(lookup_info.GetLookupName(), nullptr,
357 lookup_info.GetNameTypeMask(), include_symbols,
358 include_inlines, true, sc_list);
361 const size_t new_size = sc_list.GetSize();
363 if (old_size < new_size)
364 lookup_info.Prune(sc_list, old_size);
366 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
367 collection::const_iterator pos, end = m_modules.end();
368 for (pos = m_modules.begin(); pos != end; ++pos) {
369 (*pos)->FindFunctions(name, nullptr, name_type_mask, include_symbols,
370 include_inlines, true, sc_list);
373 return sc_list.GetSize() - old_size;
376 size_t ModuleList::FindFunctionSymbols(const ConstString &name,
377 uint32_t name_type_mask,
378 SymbolContextList &sc_list) {
379 const size_t old_size = sc_list.GetSize();
381 if (name_type_mask & eFunctionNameTypeAuto) {
382 Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
384 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
385 collection::const_iterator pos, end = m_modules.end();
386 for (pos = m_modules.begin(); pos != end; ++pos) {
387 (*pos)->FindFunctionSymbols(lookup_info.GetLookupName(),
388 lookup_info.GetNameTypeMask(), sc_list);
391 const size_t new_size = sc_list.GetSize();
393 if (old_size < new_size)
394 lookup_info.Prune(sc_list, old_size);
396 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
397 collection::const_iterator pos, end = m_modules.end();
398 for (pos = m_modules.begin(); pos != end; ++pos) {
399 (*pos)->FindFunctionSymbols(name, name_type_mask, sc_list);
403 return sc_list.GetSize() - old_size;
406 size_t ModuleList::FindFunctions(const RegularExpression &name,
407 bool include_symbols, bool include_inlines,
408 bool append, SymbolContextList &sc_list) {
409 const size_t old_size = sc_list.GetSize();
411 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
412 collection::const_iterator pos, end = m_modules.end();
413 for (pos = m_modules.begin(); pos != end; ++pos) {
414 (*pos)->FindFunctions(name, include_symbols, include_inlines, append,
418 return sc_list.GetSize() - old_size;
421 size_t ModuleList::FindCompileUnits(const FileSpec &path, bool append,
422 SymbolContextList &sc_list) const {
426 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
427 collection::const_iterator pos, end = m_modules.end();
428 for (pos = m_modules.begin(); pos != end; ++pos) {
429 (*pos)->FindCompileUnits(path, true, sc_list);
432 return sc_list.GetSize();
435 size_t ModuleList::FindGlobalVariables(const ConstString &name,
437 VariableList &variable_list) const {
438 size_t initial_size = variable_list.GetSize();
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, nullptr, max_matches, variable_list);
444 return variable_list.GetSize() - initial_size;
447 size_t ModuleList::FindGlobalVariables(const RegularExpression ®ex,
449 VariableList &variable_list) const {
450 size_t initial_size = variable_list.GetSize();
451 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
452 collection::const_iterator pos, end = m_modules.end();
453 for (pos = m_modules.begin(); pos != end; ++pos) {
454 (*pos)->FindGlobalVariables(regex, max_matches, variable_list);
456 return variable_list.GetSize() - initial_size;
459 size_t ModuleList::FindSymbolsWithNameAndType(const ConstString &name,
460 SymbolType symbol_type,
461 SymbolContextList &sc_list,
463 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
466 size_t initial_size = sc_list.GetSize();
468 collection::const_iterator pos, end = m_modules.end();
469 for (pos = m_modules.begin(); pos != end; ++pos)
470 (*pos)->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
471 return sc_list.GetSize() - initial_size;
474 size_t ModuleList::FindSymbolsMatchingRegExAndType(
475 const RegularExpression ®ex, lldb::SymbolType symbol_type,
476 SymbolContextList &sc_list, bool append) const {
477 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
480 size_t initial_size = sc_list.GetSize();
482 collection::const_iterator pos, end = m_modules.end();
483 for (pos = m_modules.begin(); pos != end; ++pos)
484 (*pos)->FindSymbolsMatchingRegExAndType(regex, symbol_type, sc_list);
485 return sc_list.GetSize() - initial_size;
488 size_t ModuleList::FindModules(const ModuleSpec &module_spec,
489 ModuleList &matching_module_list) const {
490 size_t existing_matches = matching_module_list.GetSize();
492 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
493 collection::const_iterator pos, end = m_modules.end();
494 for (pos = m_modules.begin(); pos != end; ++pos) {
495 ModuleSP module_sp(*pos);
496 if (module_sp->MatchesModuleSpec(module_spec))
497 matching_module_list.Append(module_sp);
499 return matching_module_list.GetSize() - existing_matches;
502 ModuleSP ModuleList::FindModule(const Module *module_ptr) const {
505 // Scope for "locker"
507 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
508 collection::const_iterator pos, end = m_modules.end();
510 for (pos = m_modules.begin(); pos != end; ++pos) {
511 if ((*pos).get() == module_ptr) {
520 ModuleSP ModuleList::FindModule(const UUID &uuid) const {
523 if (uuid.IsValid()) {
524 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
525 collection::const_iterator pos, end = m_modules.end();
527 for (pos = m_modules.begin(); pos != end; ++pos) {
528 if ((*pos)->GetUUID() == uuid) {
538 ModuleList::FindTypes(const SymbolContext &sc, const ConstString &name,
539 bool name_is_fully_qualified, size_t max_matches,
540 llvm::DenseSet<SymbolFile *> &searched_symbol_files,
541 TypeList &types) const {
542 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
544 size_t total_matches = 0;
545 collection::const_iterator pos, end = m_modules.end();
547 // The symbol context "sc" contains a module so we want to search that one
548 // first if it is in our list...
549 for (pos = m_modules.begin(); pos != end; ++pos) {
550 if (sc.module_sp.get() == (*pos).get()) {
552 (*pos)->FindTypes(sc, name, name_is_fully_qualified, max_matches,
553 searched_symbol_files, types);
555 if (total_matches >= max_matches)
561 if (total_matches < max_matches) {
562 SymbolContext world_sc;
563 for (pos = m_modules.begin(); pos != end; ++pos) {
564 // Search the module if the module is not equal to the one in the symbol
565 // context "sc". If "sc" contains a empty module shared pointer, then the
566 // comparison will always be true (valid_module_ptr != nullptr).
567 if (sc.module_sp.get() != (*pos).get())
569 (*pos)->FindTypes(world_sc, name, name_is_fully_qualified,
570 max_matches, searched_symbol_files, types);
572 if (total_matches >= max_matches)
577 return total_matches;
580 bool ModuleList::FindSourceFile(const FileSpec &orig_spec,
581 FileSpec &new_spec) const {
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) {
585 if ((*pos)->FindSourceFile(orig_spec, new_spec))
591 void ModuleList::FindAddressesForLine(const lldb::TargetSP target_sp,
592 const FileSpec &file, uint32_t line,
594 std::vector<Address> &output_local,
595 std::vector<Address> &output_extern) {
596 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
597 collection::const_iterator pos, end = m_modules.end();
598 for (pos = m_modules.begin(); pos != end; ++pos) {
599 (*pos)->FindAddressesForLine(target_sp, file, line, function, output_local,
604 ModuleSP ModuleList::FindFirstModule(const ModuleSpec &module_spec) const {
606 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
607 collection::const_iterator pos, end = m_modules.end();
608 for (pos = m_modules.begin(); pos != end; ++pos) {
609 ModuleSP module_sp(*pos);
610 if (module_sp->MatchesModuleSpec(module_spec))
616 size_t ModuleList::GetSize() const {
619 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
620 size = m_modules.size();
625 void ModuleList::Dump(Stream *s) const {
626 // s.Printf("%.*p: ", (int)sizeof(void*) * 2, this);
628 // s << "ModuleList\n";
630 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
631 collection::const_iterator pos, end = m_modules.end();
632 for (pos = m_modules.begin(); pos != end; ++pos) {
637 void ModuleList::LogUUIDAndPaths(Log *log, const char *prefix_cstr) {
638 if (log != nullptr) {
639 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
640 collection::const_iterator pos, begin = m_modules.begin(),
641 end = m_modules.end();
642 for (pos = begin; pos != end; ++pos) {
643 Module *module = pos->get();
644 const FileSpec &module_file_spec = module->GetFileSpec();
645 log->Printf("%s[%u] %s (%s) \"%s\"", prefix_cstr ? prefix_cstr : "",
646 (uint32_t)std::distance(begin, pos),
647 module->GetUUID().GetAsString().c_str(),
648 module->GetArchitecture().GetArchitectureName(),
649 module_file_spec.GetPath().c_str());
654 bool ModuleList::ResolveFileAddress(lldb::addr_t vm_addr,
655 Address &so_addr) const {
656 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
657 collection::const_iterator pos, end = m_modules.end();
658 for (pos = m_modules.begin(); pos != end; ++pos) {
659 if ((*pos)->ResolveFileAddress(vm_addr, so_addr))
666 uint32_t ModuleList::ResolveSymbolContextForAddress(const Address &so_addr,
667 uint32_t resolve_scope,
668 SymbolContext &sc) const {
669 // The address is already section offset so it has a module
670 uint32_t resolved_flags = 0;
671 ModuleSP module_sp(so_addr.GetModule());
674 module_sp->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
676 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
677 collection::const_iterator pos, end = m_modules.end();
678 for (pos = m_modules.begin(); pos != end; ++pos) {
680 (*pos)->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
681 if (resolved_flags != 0)
686 return resolved_flags;
689 uint32_t ModuleList::ResolveSymbolContextForFilePath(
690 const char *file_path, uint32_t line, bool check_inlines,
691 uint32_t resolve_scope, SymbolContextList &sc_list) const {
692 FileSpec file_spec(file_path, false);
693 return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
694 resolve_scope, sc_list);
697 uint32_t ModuleList::ResolveSymbolContextsForFileSpec(
698 const FileSpec &file_spec, uint32_t line, bool check_inlines,
699 uint32_t resolve_scope, SymbolContextList &sc_list) const {
700 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
701 collection::const_iterator pos, end = m_modules.end();
702 for (pos = m_modules.begin(); pos != end; ++pos) {
703 (*pos)->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
704 resolve_scope, sc_list);
707 return sc_list.GetSize();
710 size_t ModuleList::GetIndexForModule(const Module *module) const {
712 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
713 collection::const_iterator pos;
714 collection::const_iterator begin = m_modules.begin();
715 collection::const_iterator end = m_modules.end();
716 for (pos = begin; pos != end; ++pos) {
717 if ((*pos).get() == module)
718 return std::distance(begin, pos);
721 return LLDB_INVALID_INDEX32;
725 struct SharedModuleListInfo {
726 ModuleList module_list;
727 ModuleListProperties module_list_properties;
730 static SharedModuleListInfo &GetSharedModuleListInfo()
732 static SharedModuleListInfo *g_shared_module_list_info = nullptr;
733 static llvm::once_flag g_once_flag;
734 llvm::call_once(g_once_flag, []() {
735 // NOTE: Intentionally leak the module list so a program doesn't have to
736 // cleanup all modules and object files as it exits. This just wastes time
737 // doing a bunch of cleanup that isn't required.
738 if (g_shared_module_list_info == nullptr)
739 g_shared_module_list_info = new SharedModuleListInfo();
741 return *g_shared_module_list_info;
744 static ModuleList &GetSharedModuleList() {
745 return GetSharedModuleListInfo().module_list;
748 ModuleListProperties &ModuleList::GetGlobalModuleListProperties() {
749 return GetSharedModuleListInfo().module_list_properties;
752 bool ModuleList::ModuleIsInCache(const Module *module_ptr) {
754 ModuleList &shared_module_list = GetSharedModuleList();
755 return shared_module_list.FindModule(module_ptr).get() != nullptr;
760 size_t ModuleList::FindSharedModules(const ModuleSpec &module_spec,
761 ModuleList &matching_module_list) {
762 return GetSharedModuleList().FindModules(module_spec, matching_module_list);
765 size_t ModuleList::RemoveOrphanSharedModules(bool mandatory) {
766 return GetSharedModuleList().RemoveOrphans(mandatory);
769 Status ModuleList::GetSharedModule(const ModuleSpec &module_spec,
771 const FileSpecList *module_search_paths_ptr,
772 ModuleSP *old_module_sp_ptr,
773 bool *did_create_ptr, bool always_create) {
774 ModuleList &shared_module_list = GetSharedModuleList();
775 std::lock_guard<std::recursive_mutex> guard(
776 shared_module_list.m_modules_mutex);
784 *did_create_ptr = false;
785 if (old_module_sp_ptr)
786 old_module_sp_ptr->reset();
788 const UUID *uuid_ptr = module_spec.GetUUIDPtr();
789 const FileSpec &module_file_spec = module_spec.GetFileSpec();
790 const ArchSpec &arch = module_spec.GetArchitecture();
792 // Make sure no one else can try and get or create a module while this
793 // function is actively working on it by doing an extra lock on the global
795 if (!always_create) {
796 ModuleList matching_module_list;
797 const size_t num_matching_modules =
798 shared_module_list.FindModules(module_spec, matching_module_list);
799 if (num_matching_modules > 0) {
800 for (size_t module_idx = 0; module_idx < num_matching_modules;
802 module_sp = matching_module_list.GetModuleAtIndex(module_idx);
804 // Make sure the file for the module hasn't been modified
805 if (module_sp->FileHasChanged()) {
806 if (old_module_sp_ptr && !*old_module_sp_ptr)
807 *old_module_sp_ptr = module_sp;
809 Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_MODULES));
811 log->Printf("module changed: %p, removing from global module list",
812 static_cast<void *>(module_sp.get()));
814 shared_module_list.Remove(module_sp);
817 // The module matches and the module was not modified from when it
828 module_sp.reset(new Module(module_spec));
829 // Make sure there are a module and an object file since we can specify a
830 // valid file path with an architecture that might not be in that file. By
831 // getting the object file we can guarantee that the architecture matches
832 if (module_sp->GetObjectFile()) {
833 // If we get in here we got the correct arch, now we just need to verify
834 // the UUID if one was given
835 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
838 if (module_sp->GetObjectFile() &&
839 module_sp->GetObjectFile()->GetType() ==
840 ObjectFile::eTypeStubLibrary) {
843 if (did_create_ptr) {
844 *did_create_ptr = true;
847 shared_module_list.ReplaceEquivalent(module_sp);
855 if (module_search_paths_ptr) {
856 const auto num_directories = module_search_paths_ptr->GetSize();
857 for (size_t idx = 0; idx < num_directories; ++idx) {
858 auto search_path_spec = module_search_paths_ptr->GetFileSpecAtIndex(idx);
859 if (!search_path_spec.ResolvePath())
861 namespace fs = llvm::sys::fs;
862 if (!fs::is_directory(search_path_spec.GetPath()))
864 search_path_spec.AppendPathComponent(
865 module_spec.GetFileSpec().GetFilename().AsCString());
866 if (!search_path_spec.Exists())
869 auto resolved_module_spec(module_spec);
870 resolved_module_spec.GetFileSpec() = search_path_spec;
871 module_sp.reset(new Module(resolved_module_spec));
872 if (module_sp->GetObjectFile()) {
873 // If we get in here we got the correct arch, now we just need to
874 // verify the UUID if one was given
875 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
878 if (module_sp->GetObjectFile()->GetType() ==
879 ObjectFile::eTypeStubLibrary) {
883 *did_create_ptr = true;
885 shared_module_list.ReplaceEquivalent(module_sp);
895 // Either the file didn't exist where at the path, or no path was given, so
896 // we now have to use more extreme measures to try and find the appropriate
899 // Fixup the incoming path in case the path points to a valid file, yet the
900 // arch or UUID (if one was passed in) don't match.
901 ModuleSpec located_binary_modulespec =
902 Symbols::LocateExecutableObjectFile(module_spec);
904 // Don't look for the file if it appears to be the same one we already
905 // checked for above...
906 if (located_binary_modulespec.GetFileSpec() != module_file_spec) {
907 if (!located_binary_modulespec.GetFileSpec().Exists()) {
908 located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path));
910 module_file_spec.GetPath(path, sizeof(path));
911 // How can this check ever be true? This branch it is false, and we
912 // haven't modified file_spec.
913 if (located_binary_modulespec.GetFileSpec().Exists()) {
914 std::string uuid_str;
915 if (uuid_ptr && uuid_ptr->IsValid())
916 uuid_str = uuid_ptr->GetAsString();
918 if (arch.IsValid()) {
919 if (!uuid_str.empty())
920 error.SetErrorStringWithFormat(
921 "'%s' does not contain the %s architecture and UUID %s", path,
922 arch.GetArchitectureName(), uuid_str.c_str());
924 error.SetErrorStringWithFormat(
925 "'%s' does not contain the %s architecture.", path,
926 arch.GetArchitectureName());
929 error.SetErrorStringWithFormat("'%s' does not exist", path);
936 // Make sure no one else can try and get or create a module while this
937 // function is actively working on it by doing an extra lock on the global
939 ModuleSpec platform_module_spec(module_spec);
940 platform_module_spec.GetFileSpec() =
941 located_binary_modulespec.GetFileSpec();
942 platform_module_spec.GetPlatformFileSpec() =
943 located_binary_modulespec.GetFileSpec();
944 platform_module_spec.GetSymbolFileSpec() =
945 located_binary_modulespec.GetSymbolFileSpec();
946 ModuleList matching_module_list;
947 if (shared_module_list.FindModules(platform_module_spec,
948 matching_module_list) > 0) {
949 module_sp = matching_module_list.GetModuleAtIndex(0);
951 // If we didn't have a UUID in mind when looking for the object file,
952 // then we should make sure the modification time hasn't changed!
953 if (platform_module_spec.GetUUIDPtr() == nullptr) {
954 auto file_spec_mod_time = FileSystem::GetModificationTime(
955 located_binary_modulespec.GetFileSpec());
956 if (file_spec_mod_time != llvm::sys::TimePoint<>()) {
957 if (file_spec_mod_time != module_sp->GetModificationTime()) {
958 if (old_module_sp_ptr)
959 *old_module_sp_ptr = module_sp;
960 shared_module_list.Remove(module_sp);
968 module_sp.reset(new Module(platform_module_spec));
969 // Make sure there are a module and an object file since we can specify a
970 // valid file path with an architecture that might not be in that file.
971 // By getting the object file we can guarantee that the architecture
973 if (module_sp && module_sp->GetObjectFile()) {
974 if (module_sp->GetObjectFile()->GetType() ==
975 ObjectFile::eTypeStubLibrary) {
979 *did_create_ptr = true;
981 shared_module_list.ReplaceEquivalent(module_sp);
984 located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path));
986 if (located_binary_modulespec.GetFileSpec()) {
988 error.SetErrorStringWithFormat(
989 "unable to open %s architecture in '%s'",
990 arch.GetArchitectureName(), path);
992 error.SetErrorStringWithFormat("unable to open '%s'", path);
994 std::string uuid_str;
995 if (uuid_ptr && uuid_ptr->IsValid())
996 uuid_str = uuid_ptr->GetAsString();
998 if (!uuid_str.empty())
999 error.SetErrorStringWithFormat(
1000 "cannot locate a module for UUID '%s'", uuid_str.c_str());
1002 error.SetErrorStringWithFormat("cannot locate a module");
1011 bool ModuleList::RemoveSharedModule(lldb::ModuleSP &module_sp) {
1012 return GetSharedModuleList().Remove(module_sp);
1015 bool ModuleList::RemoveSharedModuleIfOrphaned(const Module *module_ptr) {
1016 return GetSharedModuleList().RemoveIfOrphaned(module_ptr);
1019 bool ModuleList::LoadScriptingResourcesInTarget(Target *target,
1020 std::list<Status> &errors,
1021 Stream *feedback_stream,
1022 bool continue_on_error) {
1025 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
1026 for (auto module : m_modules) {
1029 if (!module->LoadScriptingResourceInTarget(target, error,
1031 if (error.Fail() && error.AsCString()) {
1032 error.SetErrorStringWithFormat("unable to load scripting data for "
1033 "module %s - error reported was %s",
1034 module->GetFileSpec()
1035 .GetFileNameStrippingExtension()
1038 errors.push_back(error);
1040 if (!continue_on_error)
1046 return errors.empty();
1049 void ModuleList::ForEach(
1050 std::function<bool(const ModuleSP &module_sp)> const &callback) const {
1051 std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
1052 for (const auto &module : m_modules) {
1053 // If the callback returns false, then stop iterating and break out
1054 if (!callback(module))