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