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