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