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