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