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"
67 #define LLDB_PROPERTIES_modulelist
68 #include "CoreProperties.inc"
71 #define LLDB_PROPERTIES_modulelist
72 #include "CorePropertiesEnum.inc"
77 ModuleListProperties::ModuleListProperties() {
79 std::make_shared<OptionValueProperties>(
ConstString(
"symbols"));
80 m_collection_sp->Initialize(g_modulelist_properties);
81 m_collection_sp->SetValueChangedCallback(ePropertySymLinkPaths,
82 [
this] { UpdateSymlinkMappings(); });
84 llvm::SmallString<128> path;
85 if (clang::driver::Driver::getDefaultModuleCachePath(path)) {
90 if (llvm::sys::path::cache_directory(path)) {
91 llvm::sys::path::append(path,
"lldb");
92 llvm::sys::path::append(path,
"IndexCache");
98 bool 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);
104 bool ModuleListProperties::SetEnableExternalLookup(
bool new_value) {
105 return m_collection_sp->SetPropertyAtIndexAsBoolean(
106 nullptr, ePropertyEnableExternalLookup, new_value);
109 FileSpec ModuleListProperties::GetClangModulesCachePath()
const {
110 return m_collection_sp
111 ->GetPropertyAtIndexAsOptionValueFileSpec(
nullptr,
false,
112 ePropertyClangModulesCachePath)
116 bool ModuleListProperties::SetClangModulesCachePath(
const FileSpec &path) {
117 return m_collection_sp->SetPropertyAtIndexAsFileSpec(
118 nullptr, ePropertyClangModulesCachePath, path);
121 FileSpec ModuleListProperties::GetLLDBIndexCachePath()
const {
122 return m_collection_sp
123 ->GetPropertyAtIndexAsOptionValueFileSpec(
nullptr,
false,
124 ePropertyLLDBIndexCachePath)
128 bool ModuleListProperties::SetLLDBIndexCachePath(
const FileSpec &path) {
129 return m_collection_sp->SetPropertyAtIndexAsFileSpec(
130 nullptr, ePropertyLLDBIndexCachePath, path);
133 bool ModuleListProperties::GetEnableLLDBIndexCache()
const {
134 const uint32_t idx = ePropertyEnableLLDBIndexCache;
135 return m_collection_sp->GetPropertyAtIndexAsBoolean(
136 nullptr, idx, g_modulelist_properties[idx].default_uint_value != 0);
139 bool ModuleListProperties::SetEnableLLDBIndexCache(
bool new_value) {
140 return m_collection_sp->SetPropertyAtIndexAsBoolean(
141 nullptr, ePropertyEnableLLDBIndexCache, new_value);
144 uint64_t ModuleListProperties::GetLLDBIndexCacheMaxByteSize() {
145 const uint32_t idx = ePropertyLLDBIndexCacheMaxByteSize;
146 return m_collection_sp->GetPropertyAtIndexAsUInt64(
147 nullptr, idx, g_modulelist_properties[idx].default_uint_value);
150 uint64_t ModuleListProperties::GetLLDBIndexCacheMaxPercent() {
151 const uint32_t idx = ePropertyLLDBIndexCacheMaxPercent;
152 return m_collection_sp->GetPropertyAtIndexAsUInt64(
153 nullptr, idx, g_modulelist_properties[idx].default_uint_value);
156 uint64_t ModuleListProperties::GetLLDBIndexCacheExpirationDays() {
157 const uint32_t idx = ePropertyLLDBIndexCacheExpirationDays;
158 return m_collection_sp->GetPropertyAtIndexAsUInt64(
159 nullptr, idx, g_modulelist_properties[idx].default_uint_value);
162 void ModuleListProperties::UpdateSymlinkMappings() {
163 FileSpecList
list = m_collection_sp
164 ->GetPropertyAtIndexAsOptionValueFileSpecList(
165 nullptr,
false, ePropertySymLinkPaths)
167 llvm::sys::ScopedWriter lock(m_symlink_paths_mutex);
168 const bool notify =
false;
169 m_symlink_paths.Clear(notify);
172 Status status = FileSystem::Instance().Readlink(symlink, resolved);
174 m_symlink_paths.Append(symlink.GetPath(), resolved.
GetPath(), notify);
179 llvm::sys::ScopedReader lock(m_symlink_paths_mutex);
180 return m_symlink_paths;
183 bool ModuleListProperties::GetLoadSymbolOnDemand() {
184 const uint32_t idx = ePropertyLoadSymbolOnDemand;
185 return m_collection_sp->GetPropertyAtIndexAsBoolean(
186 nullptr, idx, g_modulelist_properties[idx].default_uint_value != 0);
189 ModuleList::ModuleList() : m_modules(), m_modules_mutex() {}
198 : m_modules(), m_modules_mutex(), m_notifier(notifier) {}
228 const ModuleSP &module_sp,
235 ModuleSpec equivalent_module_spec(module_sp->GetFileSpec(),
236 module_sp->GetArchitecture());
238 module_sp->GetPlatformFileSpec();
243 if (test_module_sp->MatchesModuleSpec(equivalent_module_spec)) {
245 old_modules->push_back(test_module_sp);
259 for (
const ModuleSP &module_sp :
m_modules) {
260 if (module_sp.get() == new_module.get())
264 Append(new_module, notify);
287 collection::iterator pos, end =
m_modules.end();
288 for (pos =
m_modules.begin(); pos != end; ++pos) {
289 if (pos->get() == module_sp.get()) {
300 ModuleList::collection::iterator
303 ModuleSP module_sp(*pos);
304 collection::iterator retval =
m_modules.erase(pos);
315 const lldb::ModuleSP &new_module_sp) {
327 collection::iterator pos, end =
m_modules.end();
328 for (pos =
m_modules.begin(); pos != end; ++pos) {
329 if (pos->get() == module_ptr) {
342 std::unique_lock<std::recursive_mutex> lock(
m_modules_mutex, std::defer_lock);
348 if (!lock.try_lock())
351 size_t remove_count = 0;
355 bool made_progress =
true;
356 while (made_progress) {
358 made_progress =
false;
359 collection::iterator pos =
m_modules.begin();
365 made_progress =
true;
376 size_t num_removed = 0;
377 collection::iterator pos, end = module_list.
m_modules.end();
378 for (pos = module_list.
m_modules.begin(); pos != end; ++pos) {
418 FunctionNameType name_type_mask,
421 const size_t old_size = sc_list.
GetSize();
423 if (name_type_mask & eFunctionNameTypeAuto) {
427 for (
const ModuleSP &module_sp :
m_modules) {
428 module_sp->FindFunctions(lookup_info.GetLookupName(),
430 lookup_info.GetNameTypeMask(), options, sc_list);
433 const size_t new_size = sc_list.
GetSize();
435 if (old_size < new_size)
436 lookup_info.Prune(sc_list, old_size);
439 for (
const ModuleSP &module_sp :
m_modules) {
447 lldb::FunctionNameType name_type_mask,
449 const size_t old_size = sc_list.
GetSize();
451 if (name_type_mask & eFunctionNameTypeAuto) {
455 for (
const ModuleSP &module_sp :
m_modules) {
460 const size_t new_size = sc_list.
GetSize();
462 if (old_size < new_size)
463 lookup_info.
Prune(sc_list, old_size);
466 for (
const ModuleSP &module_sp :
m_modules) {
467 module_sp->FindFunctionSymbols(name, name_type_mask, sc_list);
476 for (
const ModuleSP &module_sp :
m_modules)
477 module_sp->FindFunctions(name, options, sc_list);
483 for (
const ModuleSP &module_sp :
m_modules)
484 module_sp->FindCompileUnits(path, sc_list);
490 for (
const ModuleSP &module_sp :
m_modules) {
500 for (
const ModuleSP &module_sp :
m_modules)
501 module_sp->FindGlobalVariables(regex, max_matches, variable_list);
508 for (
const ModuleSP &module_sp :
m_modules)
509 module_sp->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
516 for (
const ModuleSP &module_sp :
m_modules)
517 module_sp->FindSymbolsMatchingRegExAndType(regex, symbol_type, sc_list);
523 for (
const ModuleSP &module_sp :
m_modules) {
524 if (module_sp->MatchesModuleSpec(module_spec))
525 matching_module_list.
Append(module_sp);
535 collection::const_iterator pos, end =
m_modules.end();
537 for (pos =
m_modules.begin(); pos != end; ++pos) {
538 if ((*pos).get() == module_ptr) {
552 collection::const_iterator pos, end =
m_modules.end();
554 for (pos =
m_modules.begin(); pos != end; ++pos) {
555 if ((*pos)->GetUUID() == uuid) {
565 bool name_is_fully_qualified,
size_t max_matches,
566 llvm::DenseSet<SymbolFile *> &searched_symbol_files,
570 collection::const_iterator pos, end =
m_modules.end();
572 for (pos =
m_modules.begin(); pos != end; ++pos) {
573 if (search_first == pos->get()) {
574 search_first->
FindTypes(name, name_is_fully_qualified, max_matches,
575 searched_symbol_files, types);
577 if (types.
GetSize() >= max_matches)
583 for (pos =
m_modules.begin(); pos != end; ++pos) {
587 if (search_first != pos->get())
588 (*pos)->FindTypes(name, name_is_fully_qualified, max_matches,
589 searched_symbol_files, types);
591 if (types.
GetSize() >= max_matches)
599 for (
const ModuleSP &module_sp :
m_modules) {
600 if (module_sp->FindSourceFile(orig_spec, new_spec))
609 std::vector<Address> &output_local,
610 std::vector<Address> &output_extern) {
612 for (
const ModuleSP &module_sp :
m_modules) {
613 module_sp->FindAddressesForLine(target_sp, file, line,
function,
614 output_local, output_extern);
621 collection::const_iterator pos, end =
m_modules.end();
622 for (pos =
m_modules.begin(); pos != end; ++pos) {
623 ModuleSP module_sp(*pos);
624 if (module_sp->MatchesModuleSpec(module_spec))
641 for (
const ModuleSP &module_sp :
m_modules)
646 if (log !=
nullptr) {
648 collection::const_iterator pos, begin =
m_modules.begin(),
650 for (pos = begin; pos != end; ++pos) {
651 Module *module = pos->get();
653 LLDB_LOGF(log,
"%s[%u] %s (%s) \"%s\"", prefix_cstr ? prefix_cstr :
"",
654 (
uint32_t)std::distance(begin, pos),
657 module_file_spec.
GetPath().c_str());
665 for (
const ModuleSP &module_sp :
m_modules) {
666 if (module_sp->ResolveFileAddress(vm_addr, so_addr))
675 SymbolContextItem resolve_scope,
682 module_sp->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
685 collection::const_iterator pos, end =
m_modules.end();
686 for (pos =
m_modules.begin(); pos != end; ++pos) {
688 (*pos)->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
689 if (resolved_flags != 0)
694 return resolved_flags;
698 const char *file_path,
uint32_t line,
bool check_inlines,
702 resolve_scope, sc_list);
709 for (
const ModuleSP &module_sp :
m_modules) {
710 module_sp->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
711 resolve_scope, sc_list);
720 collection::const_iterator pos;
721 collection::const_iterator begin =
m_modules.begin();
722 collection::const_iterator end =
m_modules.end();
723 for (pos = begin; pos != end; ++pos) {
724 if ((*pos).get() == module)
725 return std::distance(begin, pos);
732 struct SharedModuleListInfo {
739 static SharedModuleListInfo *g_shared_module_list_info =
nullptr;
740 static llvm::once_flag g_once_flag;
741 llvm::call_once(g_once_flag, []() {
745 if (g_shared_module_list_info ==
nullptr)
746 g_shared_module_list_info =
new SharedModuleListInfo();
748 return *g_shared_module_list_info;
762 return shared_module_list.
FindModule(module_ptr).get() !=
nullptr;
778 const FileSpecList *module_search_paths_ptr,
780 bool *did_create_ptr,
bool always_create) {
782 std::lock_guard<std::recursive_mutex> guard(
791 *did_create_ptr =
false;
800 if (!always_create) {
802 shared_module_list.
FindModules(module_spec, matching_module_list);
803 const size_t num_matching_modules = matching_module_list.
GetSize();
805 if (num_matching_modules > 0) {
806 for (
size_t module_idx = 0; module_idx < num_matching_modules;
811 if (module_sp->FileHasChanged()) {
813 old_modules->push_back(module_sp);
818 log,
"%p '%s' module changed: removing from global module list",
819 static_cast<void *
>(module_sp.get()),
820 module_sp->GetFileSpec().GetFilename().GetCString());
822 shared_module_list.
Remove(module_sp);
836 module_sp = std::make_shared<Module>(module_spec);
840 if (module_sp->GetObjectFile()) {
843 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
846 if (module_sp->GetObjectFile() &&
847 module_sp->GetObjectFile()->GetType() ==
851 if (did_create_ptr) {
852 *did_create_ptr =
true;
863 if (module_search_paths_ptr) {
864 const auto num_directories = module_search_paths_ptr->GetSize();
865 for (
size_t idx = 0; idx < num_directories; ++idx) {
866 auto search_path_spec = module_search_paths_ptr->GetFileSpecAtIndex(idx);
868 namespace fs = llvm::sys::fs;
871 search_path_spec.AppendPathComponent(
876 auto resolved_module_spec(module_spec);
877 resolved_module_spec.GetFileSpec() = search_path_spec;
878 module_sp = std::make_shared<Module>(resolved_module_spec);
879 if (module_sp->GetObjectFile()) {
882 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
885 if (module_sp->GetObjectFile()->GetType() ==
890 *did_create_ptr =
true;
913 if (located_binary_modulespec.
GetFileSpec() != module_file_spec) {
918 module_file_spec.
GetPath(path,
sizeof(path));
924 if (uuid_ptr && uuid_ptr->
IsValid())
928 if (!uuid_str.empty())
929 error.SetErrorStringWithFormat(
930 "'%s' does not contain the %s architecture and UUID %s", path,
933 error.SetErrorStringWithFormat(
934 "'%s' does not contain the %s architecture.", path,
938 error.SetErrorStringWithFormat(
"'%s' does not exist", path);
956 shared_module_list.
FindModules(platform_module_spec, matching_module_list);
957 if (!matching_module_list.
IsEmpty()) {
962 if (platform_module_spec.
GetUUIDPtr() ==
nullptr) {
965 if (file_spec_mod_time != llvm::sys::TimePoint<>()) {
966 if (file_spec_mod_time != module_sp->GetModificationTime()) {
968 old_modules->push_back(module_sp);
969 shared_module_list.
Remove(module_sp);
977 module_sp = std::make_shared<Module>(platform_module_spec);
982 if (module_sp && module_sp->GetObjectFile()) {
983 if (module_sp->GetObjectFile()->GetType() ==
988 *did_create_ptr =
true;
997 error.SetErrorStringWithFormat(
998 "unable to open %s architecture in '%s'",
1001 error.SetErrorStringWithFormat(
"unable to open '%s'", path);
1004 if (uuid_ptr && uuid_ptr->
IsValid())
1007 if (!uuid_str.empty())
1008 error.SetErrorStringWithFormat(
1009 "cannot locate a module for UUID '%s'", uuid_str.c_str());
1011 error.SetErrorString(
"cannot locate a module");
1029 std::list<Status> &errors,
1031 bool continue_on_error) {
1038 if (!module->LoadScriptingResourceInTarget(target,
error,
1041 error.SetErrorStringWithFormat(
"unable to load scripting data for "
1042 "module %s - error reported was %s",
1043 module->GetFileSpec()
1044 .GetFileNameStrippingExtension()
1047 errors.push_back(
error);
1049 if (!continue_on_error)
1055 return errors.empty();
1059 std::function<
bool(
const ModuleSP &module_sp)>
const &callback)
const {
1063 if (!callback(module))