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