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