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