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