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