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

source code of lldb/source/Core/ModuleList.cpp