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 bool 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);
115 FileSpec ModuleListProperties::GetClangModulesCachePath()
const {
116 return m_collection_sp
117 ->GetPropertyAtIndexAsOptionValueFileSpec(
nullptr,
false,
118 ePropertyClangModulesCachePath)
122 bool ModuleListProperties::SetClangModulesCachePath(
const FileSpec &path) {
123 return m_collection_sp->SetPropertyAtIndexAsFileSpec(
124 nullptr, ePropertyClangModulesCachePath, path);
127 FileSpec ModuleListProperties::GetLLDBIndexCachePath()
const {
128 return m_collection_sp
129 ->GetPropertyAtIndexAsOptionValueFileSpec(
nullptr,
false,
130 ePropertyLLDBIndexCachePath)
134 bool ModuleListProperties::SetLLDBIndexCachePath(
const FileSpec &path) {
135 return m_collection_sp->SetPropertyAtIndexAsFileSpec(
136 nullptr, ePropertyLLDBIndexCachePath, path);
139 bool 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);
145 bool ModuleListProperties::SetEnableLLDBIndexCache(
bool new_value) {
146 return m_collection_sp->SetPropertyAtIndexAsBoolean(
147 nullptr, ePropertyEnableLLDBIndexCache, new_value);
150 uint64_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);
156 uint64_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);
162 uint64_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);
168 void ModuleListProperties::UpdateSymlinkMappings() {
169 FileSpecList
list = m_collection_sp
170 ->GetPropertyAtIndexAsOptionValueFileSpecList(
171 nullptr,
false, ePropertySymLinkPaths)
173 llvm::sys::ScopedWriter lock(m_symlink_paths_mutex);
174 const bool notify =
false;
175 m_symlink_paths.Clear(notify);
178 Status status = FileSystem::Instance().Readlink(symlink, resolved);
180 m_symlink_paths.Append(symlink.GetPath(), resolved.
GetPath(), notify);
185 llvm::sys::ScopedReader lock(m_symlink_paths_mutex);
186 return m_symlink_paths;
189 bool 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);
195 ModuleList::ModuleList() : m_modules(), m_modules_mutex() {}
204 : m_modules(), m_modules_mutex(), m_notifier(notifier) {}
234 const ModuleSP &module_sp,
241 ModuleSpec equivalent_module_spec(module_sp->GetFileSpec(),
242 module_sp->GetArchitecture());
244 module_sp->GetPlatformFileSpec();
249 if (test_module_sp->MatchesModuleSpec(equivalent_module_spec)) {
251 old_modules->push_back(test_module_sp);
265 for (
const ModuleSP &module_sp :
m_modules) {
266 if (module_sp.get() == new_module.get())
270 Append(new_module, notify);
293 collection::iterator pos, end =
m_modules.end();
294 for (pos =
m_modules.begin(); pos != end; ++pos) {
295 if (pos->get() == module_sp.get()) {
306 ModuleList::collection::iterator
309 ModuleSP module_sp(*pos);
310 collection::iterator retval =
m_modules.erase(pos);
321 const lldb::ModuleSP &new_module_sp) {
333 collection::iterator pos, end =
m_modules.end();
334 for (pos =
m_modules.begin(); pos != end; ++pos) {
335 if (pos->get() == module_ptr) {
348 std::unique_lock<std::recursive_mutex> lock(
m_modules_mutex, std::defer_lock);
354 if (!lock.try_lock())
357 size_t remove_count = 0;
361 bool made_progress =
true;
362 while (made_progress) {
364 made_progress =
false;
365 collection::iterator pos =
m_modules.begin();
371 made_progress =
true;
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) {
424 FunctionNameType name_type_mask,
427 const size_t old_size = sc_list.
GetSize();
429 if (name_type_mask & eFunctionNameTypeAuto) {
433 for (
const ModuleSP &module_sp :
m_modules) {
438 const size_t new_size = sc_list.
GetSize();
440 if (old_size < new_size)
441 lookup_info.Prune(sc_list, old_size);
444 for (
const ModuleSP &module_sp :
m_modules) {
452 lldb::FunctionNameType name_type_mask,
454 const size_t old_size = sc_list.
GetSize();
456 if (name_type_mask & eFunctionNameTypeAuto) {
460 for (
const ModuleSP &module_sp :
m_modules) {
465 const size_t new_size = sc_list.
GetSize();
467 if (old_size < new_size)
468 lookup_info.
Prune(sc_list, old_size);
471 for (
const ModuleSP &module_sp :
m_modules) {
472 module_sp->FindFunctionSymbols(name, name_type_mask, sc_list);
481 for (
const ModuleSP &module_sp :
m_modules)
482 module_sp->FindFunctions(name, options, sc_list);
488 for (
const ModuleSP &module_sp :
m_modules)
489 module_sp->FindCompileUnits(path, sc_list);
495 for (
const ModuleSP &module_sp :
m_modules) {
505 for (
const ModuleSP &module_sp :
m_modules)
506 module_sp->FindGlobalVariables(regex, max_matches, variable_list);
513 for (
const ModuleSP &module_sp :
m_modules)
514 module_sp->FindSymbolsWithNameAndType(name, symbol_type, sc_list);
521 for (
const ModuleSP &module_sp :
m_modules)
522 module_sp->FindSymbolsMatchingRegExAndType(regex, symbol_type, sc_list);
528 for (
const ModuleSP &module_sp :
m_modules) {
529 if (module_sp->MatchesModuleSpec(module_spec))
530 matching_module_list.
Append(module_sp);
540 collection::const_iterator pos, end =
m_modules.end();
542 for (pos =
m_modules.begin(); pos != end; ++pos) {
543 if ((*pos).get() == module_ptr) {
557 collection::const_iterator pos, end =
m_modules.end();
559 for (pos =
m_modules.begin(); pos != end; ++pos) {
560 if ((*pos)->GetUUID() == uuid) {
570 bool name_is_fully_qualified,
size_t max_matches,
571 llvm::DenseSet<SymbolFile *> &searched_symbol_files,
575 collection::const_iterator pos, end =
m_modules.end();
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);
582 if (types.
GetSize() >= max_matches)
588 for (pos =
m_modules.begin(); pos != end; ++pos) {
592 if (search_first != pos->get())
593 (*pos)->FindTypes(name, name_is_fully_qualified, max_matches,
594 searched_symbol_files, types);
596 if (types.
GetSize() >= max_matches)
604 for (
const ModuleSP &module_sp :
m_modules) {
605 if (module_sp->FindSourceFile(orig_spec, new_spec))
614 std::vector<Address> &output_local,
615 std::vector<Address> &output_extern) {
617 for (
const ModuleSP &module_sp :
m_modules) {
618 module_sp->FindAddressesForLine(target_sp, file, line,
function,
619 output_local, output_extern);
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))
646 for (
const ModuleSP &module_sp :
m_modules)
651 if (log !=
nullptr) {
653 collection::const_iterator pos, begin =
m_modules.begin(),
655 for (pos = begin; pos != end; ++pos) {
656 Module *module = pos->get();
658 LLDB_LOGF(log,
"%s[%u] %s (%s) \"%s\"", prefix_cstr ? prefix_cstr :
"",
659 (
uint32_t)std::distance(begin, pos),
662 module_file_spec.
GetPath().c_str());
670 for (
const ModuleSP &module_sp :
m_modules) {
671 if (module_sp->ResolveFileAddress(vm_addr, so_addr))
680 SymbolContextItem resolve_scope,
687 module_sp->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
690 collection::const_iterator pos, end =
m_modules.end();
691 for (pos =
m_modules.begin(); pos != end; ++pos) {
693 (*pos)->ResolveSymbolContextForAddress(so_addr, resolve_scope, sc);
694 if (resolved_flags != 0)
699 return resolved_flags;
703 const char *file_path,
uint32_t line,
bool check_inlines,
707 resolve_scope, sc_list);
714 for (
const ModuleSP &module_sp :
m_modules) {
715 module_sp->ResolveSymbolContextsForFileSpec(file_spec, line, check_inlines,
716 resolve_scope, sc_list);
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);
737 struct SharedModuleListInfo {
744 static SharedModuleListInfo *g_shared_module_list_info =
nullptr;
745 static llvm::once_flag g_once_flag;
746 llvm::call_once(g_once_flag, []() {
750 if (g_shared_module_list_info ==
nullptr)
751 g_shared_module_list_info =
new SharedModuleListInfo();
753 return *g_shared_module_list_info;
767 return shared_module_list.
FindModule(module_ptr).get() !=
nullptr;
787 const FileSpecList *module_search_paths_ptr,
789 bool *did_create_ptr,
bool always_create) {
791 std::lock_guard<std::recursive_mutex> guard(
800 *did_create_ptr =
false;
809 if (!always_create) {
811 shared_module_list.
FindModules(module_spec, matching_module_list);
812 const size_t num_matching_modules = matching_module_list.
GetSize();
814 if (num_matching_modules > 0) {
815 for (
size_t module_idx = 0; module_idx < num_matching_modules;
820 if (module_sp->FileHasChanged()) {
822 old_modules->push_back(module_sp);
827 log,
"%p '%s' module changed: removing from global module list",
828 static_cast<void *
>(module_sp.get()),
829 module_sp->GetFileSpec().GetFilename().GetCString());
831 shared_module_list.
Remove(module_sp);
845 module_sp = std::make_shared<Module>(module_spec);
849 if (module_sp->GetObjectFile()) {
852 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
855 if (module_sp->GetObjectFile() &&
856 module_sp->GetObjectFile()->GetType() ==
860 if (did_create_ptr) {
861 *did_create_ptr =
true;
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);
877 namespace fs = llvm::sys::fs;
880 search_path_spec.AppendPathComponent(
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()) {
891 if (uuid_ptr && *uuid_ptr != module_sp->GetUUID()) {
894 if (module_sp->GetObjectFile()->GetType() ==
899 *did_create_ptr =
true;
922 if (located_binary_modulespec.
GetFileSpec() != module_file_spec) {
927 module_file_spec.
GetPath(path,
sizeof(path));
933 if (uuid_ptr && uuid_ptr->
IsValid())
937 if (!uuid_str.empty())
938 error.SetErrorStringWithFormat(
939 "'%s' does not contain the %s architecture and UUID %s", path,
942 error.SetErrorStringWithFormat(
943 "'%s' does not contain the %s architecture.", path,
947 error.SetErrorStringWithFormat(
"'%s' does not exist", path);
965 shared_module_list.
FindModules(platform_module_spec, matching_module_list);
966 if (!matching_module_list.
IsEmpty()) {
971 if (platform_module_spec.
GetUUIDPtr() ==
nullptr) {
974 if (file_spec_mod_time != llvm::sys::TimePoint<>()) {
975 if (file_spec_mod_time != module_sp->GetModificationTime()) {
977 old_modules->push_back(module_sp);
978 shared_module_list.
Remove(module_sp);
986 module_sp = std::make_shared<Module>(platform_module_spec);
991 if (module_sp && module_sp->GetObjectFile()) {
992 if (module_sp->GetObjectFile()->GetType() ==
997 *did_create_ptr =
true;
1006 error.SetErrorStringWithFormat(
1007 "unable to open %s architecture in '%s'",
1010 error.SetErrorStringWithFormat(
"unable to open '%s'", path);
1013 if (uuid_ptr && uuid_ptr->
IsValid())
1016 if (!uuid_str.empty())
1017 error.SetErrorStringWithFormat(
1018 "cannot locate a module for UUID '%s'", uuid_str.c_str());
1020 error.SetErrorString(
"cannot locate a module");
1038 std::list<Status> &errors,
1040 bool continue_on_error) {
1047 if (!module->LoadScriptingResourceInTarget(target,
error,
1050 error.SetErrorStringWithFormat(
"unable to load scripting data for "
1051 "module %s - error reported was %s",
1052 module->GetFileSpec()
1053 .GetFileNameStrippingExtension()
1056 errors.push_back(
error);
1058 if (!continue_on_error)
1064 return errors.empty();
1068 std::function<
bool(
const ModuleSP &module_sp)>
const &callback)
const {
1070 for (
const auto &module_sp :
m_modules) {
1071 assert(module_sp !=
nullptr);
1073 if (!callback(module_sp))
1082 for (
const auto &module_sp :
m_modules) {
1083 assert(module_sp !=
nullptr);
1084 if (callback(*module_sp))