1 //===-- ModuleList.cpp ----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Core/ModuleList.h"
10 #include "lldb/Core/FileSpecList.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/ModuleSpec.h"
13 #include "lldb/Host/FileSystem.h"
14 #include "lldb/Interpreter/OptionValueFileSpec.h"
15 #include "lldb/Interpreter/OptionValueFileSpecList.h"
16 #include "lldb/Interpreter/OptionValueProperties.h"
17 #include "lldb/Interpreter/Property.h"
18 #include "lldb/Symbol/LocateSymbolFile.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Symbol/SymbolContext.h"
21 #include "lldb/Symbol/TypeList.h"
22 #include "lldb/Symbol/VariableList.h"
23 #include "lldb/Utility/ArchSpec.h"
24 #include "lldb/Utility/ConstString.h"
25 #include "lldb/Utility/LLDBLog.h"
26 #include "lldb/Utility/Log.h"
27 #include "lldb/Utility/UUID.h"
28 #include "lldb/lldb-defines.h"
29 
30 #if defined(_WIN32)
31 #include "lldb/Host/windows/PosixApi.h"
32 #endif
33 
34 #include "clang/Driver/Driver.h"
35 #include "llvm/ADT/StringRef.h"
36 #include "llvm/Support/FileSystem.h"
37 #include "llvm/Support/Threading.h"
38 #include "llvm/Support/raw_ostream.h"
39 
40 #include <chrono>
41 #include <memory>
42 #include <mutex>
43 #include <string>
44 #include <utility>
45 
46 namespace lldb_private {
47 class Function;
48 }
49 namespace lldb_private {
50 class RegularExpression;
51 }
52 namespace lldb_private {
53 class Stream;
54 }
55 namespace lldb_private {
56 class SymbolFile;
57 }
58 namespace lldb_private {
59 class Target;
60 }
61 
62 using namespace lldb;
63 using namespace lldb_private;
64 
65 namespace {
66 
67 #define LLDB_PROPERTIES_modulelist
68 #include "CoreProperties.inc"
69 
70 enum {
71 #define LLDB_PROPERTIES_modulelist
72 #include "CorePropertiesEnum.inc"
73 };
74 
75 } // namespace
76 
77 ModuleListProperties::ModuleListProperties() {
78   m_collection_sp =
79       std::make_shared<OptionValueProperties>(ConstString("symbols"));
80   m_collection_sp->Initialize(g_modulelist_properties);
81   m_collection_sp->SetValueChangedCallback(ePropertySymLinkPaths,
82                                            [this] { UpdateSymlinkMappings(); });
83 
84   llvm::SmallString<128> path;
85   if (clang::driver::Driver::getDefaultModuleCachePath(path)) {
86     lldbassert(SetClangModulesCachePath(FileSpec(path)));
87   }
88 
89   path.clear();
90   if (llvm::sys::path::cache_directory(path)) {
91     llvm::sys::path::append(path, "lldb");
92     llvm::sys::path::append(path, "IndexCache");
93     lldbassert(SetLLDBIndexCachePath(FileSpec(path)));
94   }
95 
96 }
97 
98 bool ModuleListProperties::GetEnableExternalLookup() const {
99   const uint32_t idx = ePropertyEnableExternalLookup;
100   return m_collection_sp->GetPropertyAtIndexAsBoolean(
101       nullptr, idx, g_modulelist_properties[idx].default_uint_value != 0);
102 }
103 
104 bool ModuleListProperties::SetEnableExternalLookup(bool new_value) {
105   return m_collection_sp->SetPropertyAtIndexAsBoolean(
106       nullptr, ePropertyEnableExternalLookup, new_value);
107 }
108 
109 FileSpec ModuleListProperties::GetClangModulesCachePath() const {
110   return m_collection_sp
111       ->GetPropertyAtIndexAsOptionValueFileSpec(nullptr, false,
112                                                 ePropertyClangModulesCachePath)
113       ->GetCurrentValue();
114 }
115 
116 bool ModuleListProperties::SetClangModulesCachePath(const FileSpec &path) {
117   return m_collection_sp->SetPropertyAtIndexAsFileSpec(
118       nullptr, ePropertyClangModulesCachePath, path);
119 }
120 
121 FileSpec ModuleListProperties::GetLLDBIndexCachePath() const {
122   return m_collection_sp
123       ->GetPropertyAtIndexAsOptionValueFileSpec(nullptr, false,
124                                                 ePropertyLLDBIndexCachePath)
125       ->GetCurrentValue();
126 }
127 
128 bool ModuleListProperties::SetLLDBIndexCachePath(const FileSpec &path) {
129   return m_collection_sp->SetPropertyAtIndexAsFileSpec(
130       nullptr, ePropertyLLDBIndexCachePath, path);
131 }
132 
133 bool ModuleListProperties::GetEnableLLDBIndexCache() const {
134   const uint32_t idx = ePropertyEnableLLDBIndexCache;
135   return m_collection_sp->GetPropertyAtIndexAsBoolean(
136       nullptr, idx, g_modulelist_properties[idx].default_uint_value != 0);
137 }
138 
139 bool ModuleListProperties::SetEnableLLDBIndexCache(bool new_value) {
140   return m_collection_sp->SetPropertyAtIndexAsBoolean(
141       nullptr, ePropertyEnableLLDBIndexCache, new_value);
142 }
143 
144 uint64_t ModuleListProperties::GetLLDBIndexCacheMaxByteSize() {
145   const uint32_t idx = ePropertyLLDBIndexCacheMaxByteSize;
146   return m_collection_sp->GetPropertyAtIndexAsUInt64(
147       nullptr, idx, g_modulelist_properties[idx].default_uint_value);
148 }
149 
150 uint64_t ModuleListProperties::GetLLDBIndexCacheMaxPercent() {
151   const uint32_t idx = ePropertyLLDBIndexCacheMaxPercent;
152   return m_collection_sp->GetPropertyAtIndexAsUInt64(
153       nullptr, idx, g_modulelist_properties[idx].default_uint_value);
154 }
155 
156 uint64_t ModuleListProperties::GetLLDBIndexCacheExpirationDays() {
157   const uint32_t idx = ePropertyLLDBIndexCacheExpirationDays;
158   return m_collection_sp->GetPropertyAtIndexAsUInt64(
159       nullptr, idx, g_modulelist_properties[idx].default_uint_value);
160 }
161 
162 void ModuleListProperties::UpdateSymlinkMappings() {
163   FileSpecList list = m_collection_sp
164                           ->GetPropertyAtIndexAsOptionValueFileSpecList(
165                               nullptr, false, ePropertySymLinkPaths)
166                           ->GetCurrentValue();
167   llvm::sys::ScopedWriter lock(m_symlink_paths_mutex);
168   const bool notify = false;
169   m_symlink_paths.Clear(notify);
170   for (FileSpec symlink : list) {
171     FileSpec resolved;
172     Status status = FileSystem::Instance().Readlink(symlink, resolved);
173     if (status.Success())
174       m_symlink_paths.Append(symlink.GetPath(), resolved.GetPath(), notify);
175   }
176 }
177 
178 PathMappingList ModuleListProperties::GetSymlinkMappings() const {
179   llvm::sys::ScopedReader lock(m_symlink_paths_mutex);
180   return m_symlink_paths;
181 }
182 
183 ModuleList::ModuleList() : m_modules(), m_modules_mutex() {}
184 
185 ModuleList::ModuleList(const ModuleList &rhs) : m_modules(), m_modules_mutex() {
186   std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex);
187   std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex);
188   m_modules = rhs.m_modules;
189 }
190 
191 ModuleList::ModuleList(ModuleList::Notifier *notifier)
192     : m_modules(), m_modules_mutex(), m_notifier(notifier) {}
193 
194 const ModuleList &ModuleList::operator=(const ModuleList &rhs) {
195   if (this != &rhs) {
196     std::lock(m_modules_mutex, rhs.m_modules_mutex);
197     std::lock_guard<std::recursive_mutex> lhs_guard(m_modules_mutex,
198                                                     std::adopt_lock);
199     std::lock_guard<std::recursive_mutex> rhs_guard(rhs.m_modules_mutex,
200                                                     std::adopt_lock);
201     m_modules = rhs.m_modules;
202   }
203   return *this;
204 }
205 
206 ModuleList::~ModuleList() = default;
207 
208 void ModuleList::AppendImpl(const ModuleSP &module_sp, bool use_notifier) {
209   if (module_sp) {
210     std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
211     m_modules.push_back(module_sp);
212     if (use_notifier && m_notifier)
213       m_notifier->NotifyModuleAdded(*this, module_sp);
214   }
215 }
216 
217 void ModuleList::Append(const ModuleSP &module_sp, bool notify) {
218   AppendImpl(module_sp, notify);
219 }
220 
221 void ModuleList::ReplaceEquivalent(
222     const ModuleSP &module_sp,
223     llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules) {
224   if (module_sp) {
225     std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
226 
227     // First remove any equivalent modules. Equivalent modules are modules
228     // whose path, platform path and architecture match.
229     ModuleSpec equivalent_module_spec(module_sp->GetFileSpec(),
230                                       module_sp->GetArchitecture());
231     equivalent_module_spec.GetPlatformFileSpec() =
232         module_sp->GetPlatformFileSpec();
233 
234     size_t idx = 0;
235     while (idx < m_modules.size()) {
236       ModuleSP test_module_sp(m_modules[idx]);
237       if (test_module_sp->MatchesModuleSpec(equivalent_module_spec)) {
238         if (old_modules)
239           old_modules->push_back(test_module_sp);
240         RemoveImpl(m_modules.begin() + idx);
241       } else {
242         ++idx;
243       }
244     }
245     // Now add the new module to the list
246     Append(module_sp);
247   }
248 }
249 
250 bool ModuleList::AppendIfNeeded(const ModuleSP &new_module, bool notify) {
251   if (new_module) {
252     std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
253     for (const ModuleSP &module_sp : m_modules) {
254       if (module_sp.get() == new_module.get())
255         return false; // Already in the list
256     }
257     // Only push module_sp on the list if it wasn't already in there.
258     Append(new_module, notify);
259     return true;
260   }
261   return false;
262 }
263 
264 void ModuleList::Append(const ModuleList &module_list) {
265   for (auto pos : module_list.m_modules)
266     Append(pos);
267 }
268 
269 bool ModuleList::AppendIfNeeded(const ModuleList &module_list) {
270   bool any_in = false;
271   for (auto pos : module_list.m_modules) {
272     if (AppendIfNeeded(pos))
273       any_in = true;
274   }
275   return any_in;
276 }
277 
278 bool ModuleList::RemoveImpl(const ModuleSP &module_sp, bool use_notifier) {
279   if (module_sp) {
280     std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
281     collection::iterator pos, end = m_modules.end();
282     for (pos = m_modules.begin(); pos != end; ++pos) {
283       if (pos->get() == module_sp.get()) {
284         m_modules.erase(pos);
285         if (use_notifier && m_notifier)
286           m_notifier->NotifyModuleRemoved(*this, module_sp);
287         return true;
288       }
289     }
290   }
291   return false;
292 }
293 
294 ModuleList::collection::iterator
295 ModuleList::RemoveImpl(ModuleList::collection::iterator pos,
296                        bool use_notifier) {
297   ModuleSP module_sp(*pos);
298   collection::iterator retval = m_modules.erase(pos);
299   if (use_notifier && m_notifier)
300     m_notifier->NotifyModuleRemoved(*this, module_sp);
301   return retval;
302 }
303 
304 bool ModuleList::Remove(const ModuleSP &module_sp, bool notify) {
305   return RemoveImpl(module_sp, notify);
306 }
307 
308 bool ModuleList::ReplaceModule(const lldb::ModuleSP &old_module_sp,
309                                const lldb::ModuleSP &new_module_sp) {
310   if (!RemoveImpl(old_module_sp, false))
311     return false;
312   AppendImpl(new_module_sp, false);
313   if (m_notifier)
314     m_notifier->NotifyModuleUpdated(*this, old_module_sp, new_module_sp);
315   return true;
316 }
317 
318 bool ModuleList::RemoveIfOrphaned(const Module *module_ptr) {
319   if (module_ptr) {
320     std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
321     collection::iterator pos, end = m_modules.end();
322     for (pos = m_modules.begin(); pos != end; ++pos) {
323       if (pos->get() == module_ptr) {
324         if (pos->unique()) {
325           pos = RemoveImpl(pos);
326           return true;
327         } else
328           return false;
329       }
330     }
331   }
332   return false;
333 }
334 
335 size_t ModuleList::RemoveOrphans(bool mandatory) {
336   std::unique_lock<std::recursive_mutex> lock(m_modules_mutex, std::defer_lock);
337 
338   if (mandatory) {
339     lock.lock();
340   } else {
341     // Not mandatory, remove orphans if we can get the mutex
342     if (!lock.try_lock())
343       return 0;
344   }
345   size_t remove_count = 0;
346   // Modules might hold shared pointers to other modules, so removing one
347   // module might make other other modules orphans. Keep removing modules until
348   // there are no further modules that can be removed.
349   bool made_progress = true;
350   while (made_progress) {
351     // Keep track if we make progress this iteration.
352     made_progress = false;
353     collection::iterator pos = m_modules.begin();
354     while (pos != m_modules.end()) {
355       if (pos->unique()) {
356         pos = RemoveImpl(pos);
357         ++remove_count;
358         // We did make progress.
359         made_progress = true;
360       } else {
361         ++pos;
362       }
363     }
364   }
365   return remove_count;
366 }
367 
368 size_t ModuleList::Remove(ModuleList &module_list) {
369   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
370   size_t num_removed = 0;
371   collection::iterator pos, end = module_list.m_modules.end();
372   for (pos = module_list.m_modules.begin(); pos != end; ++pos) {
373     if (Remove(*pos, false /* notify */))
374       ++num_removed;
375   }
376   if (m_notifier)
377     m_notifier->NotifyModulesRemoved(module_list);
378   return num_removed;
379 }
380 
381 void ModuleList::Clear() { ClearImpl(); }
382 
383 void ModuleList::Destroy() { ClearImpl(); }
384 
385 void ModuleList::ClearImpl(bool use_notifier) {
386   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
387   if (use_notifier && m_notifier)
388     m_notifier->NotifyWillClearList(*this);
389   m_modules.clear();
390 }
391 
392 Module *ModuleList::GetModulePointerAtIndex(size_t idx) const {
393   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
394   if (idx < m_modules.size())
395     return m_modules[idx].get();
396   return nullptr;
397 }
398 
399 ModuleSP ModuleList::GetModuleAtIndex(size_t idx) const {
400   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
401   return GetModuleAtIndexUnlocked(idx);
402 }
403 
404 ModuleSP ModuleList::GetModuleAtIndexUnlocked(size_t idx) const {
405   ModuleSP module_sp;
406   if (idx < m_modules.size())
407     module_sp = m_modules[idx];
408   return module_sp;
409 }
410 
411 void ModuleList::FindFunctions(ConstString name,
412                                FunctionNameType name_type_mask,
413                                const ModuleFunctionSearchOptions &options,
414                                SymbolContextList &sc_list) const {
415   const size_t old_size = sc_list.GetSize();
416 
417   if (name_type_mask & eFunctionNameTypeAuto) {
418     Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
419 
420     std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
421     for (const ModuleSP &module_sp : m_modules) {
422       module_sp->FindFunctions(lookup_info.GetLookupName(),
423                                CompilerDeclContext(),
424                                lookup_info.GetNameTypeMask(), options, sc_list);
425     }
426 
427     const size_t new_size = sc_list.GetSize();
428 
429     if (old_size < new_size)
430       lookup_info.Prune(sc_list, old_size);
431   } else {
432     std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
433     for (const ModuleSP &module_sp : m_modules) {
434       module_sp->FindFunctions(name, CompilerDeclContext(), name_type_mask,
435                                options, sc_list);
436     }
437   }
438 }
439 
440 void ModuleList::FindFunctionSymbols(ConstString name,
441                                      lldb::FunctionNameType name_type_mask,
442                                      SymbolContextList &sc_list) {
443   const size_t old_size = sc_list.GetSize();
444 
445   if (name_type_mask & eFunctionNameTypeAuto) {
446     Module::LookupInfo lookup_info(name, name_type_mask, eLanguageTypeUnknown);
447 
448     std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
449     for (const ModuleSP &module_sp : m_modules) {
450       module_sp->FindFunctionSymbols(lookup_info.GetLookupName(),
451                                      lookup_info.GetNameTypeMask(), sc_list);
452     }
453 
454     const size_t new_size = sc_list.GetSize();
455 
456     if (old_size < new_size)
457       lookup_info.Prune(sc_list, old_size);
458   } else {
459     std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
460     for (const ModuleSP &module_sp : m_modules) {
461       module_sp->FindFunctionSymbols(name, name_type_mask, sc_list);
462     }
463   }
464 }
465 
466 void ModuleList::FindFunctions(const RegularExpression &name,
467                                const ModuleFunctionSearchOptions &options,
468                                SymbolContextList &sc_list) {
469   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
470   for (const ModuleSP &module_sp : m_modules)
471     module_sp->FindFunctions(name, options, sc_list);
472 }
473 
474 void ModuleList::FindCompileUnits(const FileSpec &path,
475                                   SymbolContextList &sc_list) const {
476   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
477   for (const ModuleSP &module_sp : m_modules)
478     module_sp->FindCompileUnits(path, sc_list);
479 }
480 
481 void ModuleList::FindGlobalVariables(ConstString name, size_t max_matches,
482                                      VariableList &variable_list) const {
483   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
484   for (const ModuleSP &module_sp : m_modules) {
485     module_sp->FindGlobalVariables(name, CompilerDeclContext(), max_matches,
486                                    variable_list);
487   }
488 }
489 
490 void ModuleList::FindGlobalVariables(const RegularExpression &regex,
491                                      size_t max_matches,
492                                      VariableList &variable_list) const {
493   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
494   for (const ModuleSP &module_sp : m_modules)
495     module_sp->FindGlobalVariables(regex, max_matches, variable_list);
496 }
497 
498 void ModuleList::FindSymbolsWithNameAndType(ConstString name,
499                                             SymbolType symbol_type,
500                                             SymbolContextList &sc_list) const {
501   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
502   for (const ModuleSP &module_sp : m_modules)
503     module_sp->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
504 }
505 
506 void ModuleList::FindSymbolsMatchingRegExAndType(
507     const RegularExpression &regex, lldb::SymbolType symbol_type,
508     SymbolContextList &sc_list) const {
509   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
510   for (const ModuleSP &module_sp : m_modules)
511     module_sp->FindSymbolsMatchingRegExAndType(regex, symbol_type, sc_list);
512 }
513 
514 void ModuleList::FindModules(const ModuleSpec &module_spec,
515                              ModuleList &matching_module_list) const {
516   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
517   for (const ModuleSP &module_sp : m_modules) {
518     if (module_sp->MatchesModuleSpec(module_spec))
519       matching_module_list.Append(module_sp);
520   }
521 }
522 
523 ModuleSP ModuleList::FindModule(const Module *module_ptr) const {
524   ModuleSP module_sp;
525 
526   // Scope for "locker"
527   {
528     std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
529     collection::const_iterator pos, end = m_modules.end();
530 
531     for (pos = m_modules.begin(); pos != end; ++pos) {
532       if ((*pos).get() == module_ptr) {
533         module_sp = (*pos);
534         break;
535       }
536     }
537   }
538   return module_sp;
539 }
540 
541 ModuleSP ModuleList::FindModule(const UUID &uuid) const {
542   ModuleSP module_sp;
543 
544   if (uuid.IsValid()) {
545     std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
546     collection::const_iterator pos, end = m_modules.end();
547 
548     for (pos = m_modules.begin(); pos != end; ++pos) {
549       if ((*pos)->GetUUID() == uuid) {
550         module_sp = (*pos);
551         break;
552       }
553     }
554   }
555   return module_sp;
556 }
557 
558 void ModuleList::FindTypes(Module *search_first, ConstString name,
559                            bool name_is_fully_qualified, size_t max_matches,
560                            llvm::DenseSet<SymbolFile *> &searched_symbol_files,
561                            TypeList &types) const {
562   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
563 
564   collection::const_iterator pos, end = m_modules.end();
565   if (search_first) {
566     for (pos = m_modules.begin(); pos != end; ++pos) {
567       if (search_first == pos->get()) {
568         search_first->FindTypes(name, name_is_fully_qualified, max_matches,
569                                 searched_symbol_files, types);
570 
571         if (types.GetSize() >= max_matches)
572           return;
573       }
574     }
575   }
576 
577   for (pos = m_modules.begin(); pos != end; ++pos) {
578     // Search the module if the module is not equal to the one in the symbol
579     // context "sc". If "sc" contains a empty module shared pointer, then the
580     // comparison will always be true (valid_module_ptr != nullptr).
581     if (search_first != pos->get())
582       (*pos)->FindTypes(name, name_is_fully_qualified, max_matches,
583                         searched_symbol_files, types);
584 
585     if (types.GetSize() >= max_matches)
586       return;
587   }
588 }
589 
590 bool ModuleList::FindSourceFile(const FileSpec &orig_spec,
591                                 FileSpec &new_spec) const {
592   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
593   for (const ModuleSP &module_sp : m_modules) {
594     if (module_sp->FindSourceFile(orig_spec, new_spec))
595       return true;
596   }
597   return false;
598 }
599 
600 void ModuleList::FindAddressesForLine(const lldb::TargetSP target_sp,
601                                       const FileSpec &file, uint32_t line,
602                                       Function *function,
603                                       std::vector<Address> &output_local,
604                                       std::vector<Address> &output_extern) {
605   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
606   for (const ModuleSP &module_sp : m_modules) {
607     module_sp->FindAddressesForLine(target_sp, file, line, function,
608                                     output_local, output_extern);
609   }
610 }
611 
612 ModuleSP ModuleList::FindFirstModule(const ModuleSpec &module_spec) const {
613   ModuleSP module_sp;
614   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
615   collection::const_iterator pos, end = m_modules.end();
616   for (pos = m_modules.begin(); pos != end; ++pos) {
617     ModuleSP module_sp(*pos);
618     if (module_sp->MatchesModuleSpec(module_spec))
619       return module_sp;
620   }
621   return module_sp;
622 }
623 
624 size_t ModuleList::GetSize() const {
625   size_t size = 0;
626   {
627     std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
628     size = m_modules.size();
629   }
630   return size;
631 }
632 
633 void ModuleList::Dump(Stream *s) const {
634   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
635   for (const ModuleSP &module_sp : m_modules)
636     module_sp->Dump(s);
637 }
638 
639 void ModuleList::LogUUIDAndPaths(Log *log, const char *prefix_cstr) {
640   if (log != nullptr) {
641     std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
642     collection::const_iterator pos, begin = m_modules.begin(),
643                                     end = m_modules.end();
644     for (pos = begin; pos != end; ++pos) {
645       Module *module = pos->get();
646       const FileSpec &module_file_spec = module->GetFileSpec();
647       LLDB_LOGF(log, "%s[%u] %s (%s) \"%s\"", prefix_cstr ? prefix_cstr : "",
648                 (uint32_t)std::distance(begin, pos),
649                 module->GetUUID().GetAsString().c_str(),
650                 module->GetArchitecture().GetArchitectureName(),
651                 module_file_spec.GetPath().c_str());
652     }
653   }
654 }
655 
656 bool ModuleList::ResolveFileAddress(lldb::addr_t vm_addr,
657                                     Address &so_addr) const {
658   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
659   for (const ModuleSP &module_sp : m_modules) {
660     if (module_sp->ResolveFileAddress(vm_addr, so_addr))
661       return true;
662   }
663 
664   return false;
665 }
666 
667 uint32_t
668 ModuleList::ResolveSymbolContextForAddress(const Address &so_addr,
669                                            SymbolContextItem resolve_scope,
670                                            SymbolContext &sc) const {
671   // The address is already section offset so it has a module
672   uint32_t resolved_flags = 0;
673   ModuleSP module_sp(so_addr.GetModule());
674   if (module_sp) {
675     resolved_flags =
676         module_sp->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
677   } else {
678     std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
679     collection::const_iterator pos, end = m_modules.end();
680     for (pos = m_modules.begin(); pos != end; ++pos) {
681       resolved_flags =
682           (*pos)->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
683       if (resolved_flags != 0)
684         break;
685     }
686   }
687 
688   return resolved_flags;
689 }
690 
691 uint32_t ModuleList::ResolveSymbolContextForFilePath(
692     const char *file_path, uint32_t line, bool check_inlines,
693     SymbolContextItem resolve_scope, SymbolContextList &sc_list) const {
694   FileSpec file_spec(file_path);
695   return ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
696                                           resolve_scope, sc_list);
697 }
698 
699 uint32_t ModuleList::ResolveSymbolContextsForFileSpec(
700     const FileSpec &file_spec, uint32_t line, bool check_inlines,
701     SymbolContextItem resolve_scope, SymbolContextList &sc_list) const {
702   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
703   for (const ModuleSP &module_sp : m_modules) {
704     module_sp->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
705                                                 resolve_scope, sc_list);
706   }
707 
708   return sc_list.GetSize();
709 }
710 
711 size_t ModuleList::GetIndexForModule(const Module *module) const {
712   if (module) {
713     std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
714     collection::const_iterator pos;
715     collection::const_iterator begin = m_modules.begin();
716     collection::const_iterator end = m_modules.end();
717     for (pos = begin; pos != end; ++pos) {
718       if ((*pos).get() == module)
719         return std::distance(begin, pos);
720     }
721   }
722   return LLDB_INVALID_INDEX32;
723 }
724 
725 namespace {
726 struct SharedModuleListInfo {
727   ModuleList module_list;
728   ModuleListProperties module_list_properties;
729 };
730 }
731 static SharedModuleListInfo &GetSharedModuleListInfo()
732 {
733   static SharedModuleListInfo *g_shared_module_list_info = nullptr;
734   static llvm::once_flag g_once_flag;
735   llvm::call_once(g_once_flag, []() {
736     // NOTE: Intentionally leak the module list so a program doesn't have to
737     // cleanup all modules and object files as it exits. This just wastes time
738     // doing a bunch of cleanup that isn't required.
739     if (g_shared_module_list_info == nullptr)
740       g_shared_module_list_info = new SharedModuleListInfo();
741   });
742   return *g_shared_module_list_info;
743 }
744 
745 static ModuleList &GetSharedModuleList() {
746   return GetSharedModuleListInfo().module_list;
747 }
748 
749 ModuleListProperties &ModuleList::GetGlobalModuleListProperties() {
750   return GetSharedModuleListInfo().module_list_properties;
751 }
752 
753 bool ModuleList::ModuleIsInCache(const Module *module_ptr) {
754   if (module_ptr) {
755     ModuleList &shared_module_list = GetSharedModuleList();
756     return shared_module_list.FindModule(module_ptr).get() != nullptr;
757   }
758   return false;
759 }
760 
761 void ModuleList::FindSharedModules(const ModuleSpec &module_spec,
762                                    ModuleList &matching_module_list) {
763   GetSharedModuleList().FindModules(module_spec, matching_module_list);
764 }
765 
766 size_t ModuleList::RemoveOrphanSharedModules(bool mandatory) {
767   return GetSharedModuleList().RemoveOrphans(mandatory);
768 }
769 
770 Status
771 ModuleList::GetSharedModule(const ModuleSpec &module_spec, ModuleSP &module_sp,
772                             const FileSpecList *module_search_paths_ptr,
773                             llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules,
774                             bool *did_create_ptr, bool always_create) {
775   ModuleList &shared_module_list = GetSharedModuleList();
776   std::lock_guard<std::recursive_mutex> guard(
777       shared_module_list.m_modules_mutex);
778   char path[PATH_MAX];
779 
780   Status error;
781 
782   module_sp.reset();
783 
784   if (did_create_ptr)
785     *did_create_ptr = false;
786 
787   const UUID *uuid_ptr = module_spec.GetUUIDPtr();
788   const FileSpec &module_file_spec = module_spec.GetFileSpec();
789   const ArchSpec &arch = module_spec.GetArchitecture();
790 
791   // Make sure no one else can try and get or create a module while this
792   // function is actively working on it by doing an extra lock on the global
793   // mutex list.
794   if (!always_create) {
795     ModuleList matching_module_list;
796     shared_module_list.FindModules(module_spec, matching_module_list);
797     const size_t num_matching_modules = matching_module_list.GetSize();
798 
799     if (num_matching_modules > 0) {
800       for (size_t module_idx = 0; module_idx < num_matching_modules;
801            ++module_idx) {
802         module_sp = matching_module_list.GetModuleAtIndex(module_idx);
803 
804         // Make sure the file for the module hasn't been modified
805         if (module_sp->FileHasChanged()) {
806           if (old_modules)
807             old_modules->push_back(module_sp);
808 
809           Log *log = GetLog(LLDBLog::Modules);
810           if (log != nullptr)
811             LLDB_LOGF(
812                 log, "%p '%s' module changed: removing from global module list",
813                 static_cast<void *>(module_sp.get()),
814                 module_sp->GetFileSpec().GetFilename().GetCString());
815 
816           shared_module_list.Remove(module_sp);
817           module_sp.reset();
818         } else {
819           // The module matches and the module was not modified from when it
820           // was last loaded.
821           return error;
822         }
823       }
824     }
825   }
826 
827   if (module_sp)
828     return error;
829 
830   module_sp = std::make_shared<Module>(module_spec);
831   // Make sure there are a module and an object file since we can specify a
832   // valid file path with an architecture that might not be in that file. By
833   // getting the object file we can guarantee that the architecture matches
834   if (module_sp->GetObjectFile()) {
835     // If we get in here we got the correct arch, now we just need to verify
836     // the UUID if one was given
837     if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
838       module_sp.reset();
839     } else {
840       if (module_sp->GetObjectFile() &&
841           module_sp->GetObjectFile()->GetType() ==
842               ObjectFile::eTypeStubLibrary) {
843         module_sp.reset();
844       } else {
845         if (did_create_ptr) {
846           *did_create_ptr = true;
847         }
848 
849         shared_module_list.ReplaceEquivalent(module_sp, old_modules);
850         return error;
851       }
852     }
853   } else {
854     module_sp.reset();
855   }
856 
857   if (module_search_paths_ptr) {
858     const auto num_directories = module_search_paths_ptr->GetSize();
859     for (size_t idx = 0; idx < num_directories; ++idx) {
860       auto search_path_spec = module_search_paths_ptr->GetFileSpecAtIndex(idx);
861       FileSystem::Instance().Resolve(search_path_spec);
862       namespace fs = llvm::sys::fs;
863       if (!FileSystem::Instance().IsDirectory(search_path_spec))
864         continue;
865       search_path_spec.AppendPathComponent(
866           module_spec.GetFileSpec().GetFilename().GetStringRef());
867       if (!FileSystem::Instance().Exists(search_path_spec))
868         continue;
869 
870       auto resolved_module_spec(module_spec);
871       resolved_module_spec.GetFileSpec() = search_path_spec;
872       module_sp = std::make_shared<Module>(resolved_module_spec);
873       if (module_sp->GetObjectFile()) {
874         // If we get in here we got the correct arch, now we just need to
875         // verify the UUID if one was given
876         if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
877           module_sp.reset();
878         } else {
879           if (module_sp->GetObjectFile()->GetType() ==
880               ObjectFile::eTypeStubLibrary) {
881             module_sp.reset();
882           } else {
883             if (did_create_ptr)
884               *did_create_ptr = true;
885 
886             shared_module_list.ReplaceEquivalent(module_sp, old_modules);
887             return Status();
888           }
889         }
890       } else {
891         module_sp.reset();
892       }
893     }
894   }
895 
896   // Either the file didn't exist where at the path, or no path was given, so
897   // we now have to use more extreme measures to try and find the appropriate
898   // module.
899 
900   // Fixup the incoming path in case the path points to a valid file, yet the
901   // arch or UUID (if one was passed in) don't match.
902   ModuleSpec located_binary_modulespec =
903       Symbols::LocateExecutableObjectFile(module_spec);
904 
905   // Don't look for the file if it appears to be the same one we already
906   // checked for above...
907   if (located_binary_modulespec.GetFileSpec() != module_file_spec) {
908     if (!FileSystem::Instance().Exists(
909             located_binary_modulespec.GetFileSpec())) {
910       located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path));
911       if (path[0] == '\0')
912         module_file_spec.GetPath(path, sizeof(path));
913       // How can this check ever be true? This branch it is false, and we
914       // haven't modified file_spec.
915       if (FileSystem::Instance().Exists(
916               located_binary_modulespec.GetFileSpec())) {
917         std::string uuid_str;
918         if (uuid_ptr && uuid_ptr->IsValid())
919           uuid_str = uuid_ptr->GetAsString();
920 
921         if (arch.IsValid()) {
922           if (!uuid_str.empty())
923             error.SetErrorStringWithFormat(
924                 "'%s' does not contain the %s architecture and UUID %s", path,
925                 arch.GetArchitectureName(), uuid_str.c_str());
926           else
927             error.SetErrorStringWithFormat(
928                 "'%s' does not contain the %s architecture.", path,
929                 arch.GetArchitectureName());
930         }
931       } else {
932         error.SetErrorStringWithFormat("'%s' does not exist", path);
933       }
934       if (error.Fail())
935         module_sp.reset();
936       return error;
937     }
938 
939     // Make sure no one else can try and get or create a module while this
940     // function is actively working on it by doing an extra lock on the global
941     // mutex list.
942     ModuleSpec platform_module_spec(module_spec);
943     platform_module_spec.GetFileSpec() =
944         located_binary_modulespec.GetFileSpec();
945     platform_module_spec.GetPlatformFileSpec() =
946         located_binary_modulespec.GetFileSpec();
947     platform_module_spec.GetSymbolFileSpec() =
948         located_binary_modulespec.GetSymbolFileSpec();
949     ModuleList matching_module_list;
950     shared_module_list.FindModules(platform_module_spec, matching_module_list);
951     if (!matching_module_list.IsEmpty()) {
952       module_sp = matching_module_list.GetModuleAtIndex(0);
953 
954       // If we didn't have a UUID in mind when looking for the object file,
955       // then we should make sure the modification time hasn't changed!
956       if (platform_module_spec.GetUUIDPtr() == nullptr) {
957         auto file_spec_mod_time = FileSystem::Instance().GetModificationTime(
958             located_binary_modulespec.GetFileSpec());
959         if (file_spec_mod_time != llvm::sys::TimePoint<>()) {
960           if (file_spec_mod_time != module_sp->GetModificationTime()) {
961             if (old_modules)
962               old_modules->push_back(module_sp);
963             shared_module_list.Remove(module_sp);
964             module_sp.reset();
965           }
966         }
967       }
968     }
969 
970     if (!module_sp) {
971       module_sp = std::make_shared<Module>(platform_module_spec);
972       // Make sure there are a module and an object file since we can specify a
973       // valid file path with an architecture that might not be in that file.
974       // By getting the object file we can guarantee that the architecture
975       // matches
976       if (module_sp && module_sp->GetObjectFile()) {
977         if (module_sp->GetObjectFile()->GetType() ==
978             ObjectFile::eTypeStubLibrary) {
979           module_sp.reset();
980         } else {
981           if (did_create_ptr)
982             *did_create_ptr = true;
983 
984           shared_module_list.ReplaceEquivalent(module_sp, old_modules);
985         }
986       } else {
987         located_binary_modulespec.GetFileSpec().GetPath(path, sizeof(path));
988 
989         if (located_binary_modulespec.GetFileSpec()) {
990           if (arch.IsValid())
991             error.SetErrorStringWithFormat(
992                 "unable to open %s architecture in '%s'",
993                 arch.GetArchitectureName(), path);
994           else
995             error.SetErrorStringWithFormat("unable to open '%s'", path);
996         } else {
997           std::string uuid_str;
998           if (uuid_ptr && uuid_ptr->IsValid())
999             uuid_str = uuid_ptr->GetAsString();
1000 
1001           if (!uuid_str.empty())
1002             error.SetErrorStringWithFormat(
1003                 "cannot locate a module for UUID '%s'", uuid_str.c_str());
1004           else
1005             error.SetErrorString("cannot locate a module");
1006         }
1007       }
1008     }
1009   }
1010 
1011   return error;
1012 }
1013 
1014 bool ModuleList::RemoveSharedModule(lldb::ModuleSP &module_sp) {
1015   return GetSharedModuleList().Remove(module_sp);
1016 }
1017 
1018 bool ModuleList::RemoveSharedModuleIfOrphaned(const Module *module_ptr) {
1019   return GetSharedModuleList().RemoveIfOrphaned(module_ptr);
1020 }
1021 
1022 bool ModuleList::LoadScriptingResourcesInTarget(Target *target,
1023                                                 std::list<Status> &errors,
1024                                                 Stream *feedback_stream,
1025                                                 bool continue_on_error) {
1026   if (!target)
1027     return false;
1028   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
1029   for (auto module : m_modules) {
1030     Status error;
1031     if (module) {
1032       if (!module->LoadScriptingResourceInTarget(target, error,
1033                                                  feedback_stream)) {
1034         if (error.Fail() && error.AsCString()) {
1035           error.SetErrorStringWithFormat("unable to load scripting data for "
1036                                          "module %s - error reported was %s",
1037                                          module->GetFileSpec()
1038                                              .GetFileNameStrippingExtension()
1039                                              .GetCString(),
1040                                          error.AsCString());
1041           errors.push_back(error);
1042 
1043           if (!continue_on_error)
1044             return false;
1045         }
1046       }
1047     }
1048   }
1049   return errors.empty();
1050 }
1051 
1052 void ModuleList::ForEach(
1053     std::function<bool(const ModuleSP &module_sp)> const &callback) const {
1054   std::lock_guard<std::recursive_mutex> guard(m_modules_mutex);
1055   for (const auto &module : m_modules) {
1056     // If the callback returns false, then stop iterating and break out
1057     if (!callback(module))
1058       break;
1059   }
1060 }
1061