Skip to content

Commit cfe7d17

Browse files
author
George Hu
committed
[lldb] Enable locate module callback in GetSharedModule
1 parent 1c8dcac commit cfe7d17

File tree

13 files changed

+65
-38
lines changed

13 files changed

+65
-38
lines changed

lldb/include/lldb/Core/ModuleList.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,8 @@ class ModuleList {
477477
static Status
478478
GetSharedModule(const ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
479479
llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules,
480-
bool *did_create_ptr, bool always_create = false);
480+
bool *did_create_ptr, bool always_create = false,
481+
bool allow_locate_callback = true);
481482

482483
static bool RemoveSharedModule(lldb::ModuleSP &module_sp);
483484

lldb/include/lldb/Core/ModuleSpec.h

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
#include "llvm/Support/Chrono.h"
2121

22+
#include <memory>
2223
#include <mutex>
2324
#include <vector>
2425

@@ -126,11 +127,17 @@ class ModuleSpec {
126127

127128
lldb::DataBufferSP GetData() const { return m_data; }
128129

129-
Target *GetTargetPtr() { return m_target; }
130+
Target *GetTargetPtr() {
131+
auto locked = m_target.lock();
132+
return locked.get();
133+
}
130134

131-
const Target *GetTargetPtr() const { return m_target; }
135+
const Target *GetTargetPtr() const {
136+
auto locked = m_target.lock();
137+
return locked.get();
138+
}
132139

133-
void SetTarget(Target *target) { m_target = target; }
140+
void SetTarget(std::shared_ptr<Target> target) { m_target = target; }
134141

135142
void Clear() {
136143
m_file.Clear();
@@ -143,7 +150,7 @@ class ModuleSpec {
143150
m_object_size = 0;
144151
m_source_mappings.Clear(false);
145152
m_object_mod_time = llvm::sys::TimePoint<>();
146-
m_target = nullptr;
153+
m_target.reset();
147154
}
148155

149156
explicit operator bool() const {
@@ -272,8 +279,9 @@ class ModuleSpec {
272279
ArchSpec m_arch;
273280
UUID m_uuid;
274281
ConstString m_object_name;
275-
Target *m_target; // This is set to take advantage of the target's search path
276-
// and platform's locate module callback
282+
std::weak_ptr<Target>
283+
m_target; // This is set to take advantage of the target's search path
284+
// and platform's locate module callback
277285
uint64_t m_object_offset = 0;
278286
uint64_t m_object_size = 0;
279287
llvm::sys::TimePoint<> m_object_mod_time;

lldb/source/Core/DynamicLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ ModuleSP DynamicLoader::LoadBinaryWithUUIDAndAddress(
227227
}
228228
}
229229
ModuleSpec module_spec;
230-
module_spec.SetTarget(&target);
230+
module_spec.SetTarget(target.shared_from_this());
231231
module_spec.GetUUID() = uuid;
232232
FileSpec name_filespec(name);
233233
if (FileSystem::Instance().Exists(name_filespec))

lldb/source/Core/ModuleList.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "lldb/Symbol/SymbolContext.h"
2020
#include "lldb/Symbol/TypeList.h"
2121
#include "lldb/Symbol/VariableList.h"
22+
#include "lldb/Target/Platform.h"
2223
#include "lldb/Target/Target.h"
2324
#include "lldb/Utility/ArchSpec.h"
2425
#include "lldb/Utility/ConstString.h"
@@ -1031,7 +1032,8 @@ size_t ModuleList::RemoveOrphanSharedModules(bool mandatory) {
10311032
Status
10321033
ModuleList::GetSharedModule(const ModuleSpec &module_spec, ModuleSP &module_sp,
10331034
llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules,
1034-
bool *did_create_ptr, bool always_create) {
1035+
bool *did_create_ptr, bool always_create,
1036+
bool allow_locate_callback) {
10351037
ModuleList &shared_module_list = GetSharedModuleList();
10361038
std::lock_guard<std::recursive_mutex> guard(
10371039
shared_module_list.m_modules_mutex);
@@ -1087,6 +1089,24 @@ ModuleList::GetSharedModule(const ModuleSpec &module_spec, ModuleSP &module_sp,
10871089
if (module_sp)
10881090
return error;
10891091

1092+
// Try target's platform locate module callback before second attempt
1093+
if (allow_locate_callback) {
1094+
ModuleSpec module_spec_copy(module_spec);
1095+
Target *target = module_spec_copy.GetTargetPtr();
1096+
if (target && target->IsValid()) {
1097+
Platform *platform = target->GetPlatform().get();
1098+
if (platform) {
1099+
FileSpec symbol_file_spec;
1100+
platform->CallLocateModuleCallbackIfSet(
1101+
module_spec, module_sp, symbol_file_spec, did_create_ptr);
1102+
if (module_sp) {
1103+
// Success! The callback found a module
1104+
return error;
1105+
}
1106+
}
1107+
}
1108+
}
1109+
10901110
module_sp = std::make_shared<Module>(module_spec);
10911111
// Make sure there are a module and an object file since we can specify a
10921112
// valid file path with an architecture that might not be in that file. By
@@ -1119,7 +1139,7 @@ ModuleList::GetSharedModule(const ModuleSpec &module_spec, ModuleSP &module_sp,
11191139
Target *target = module_spec_copy.GetTargetPtr();
11201140
FileSpecList module_search_paths;
11211141
FileSpecList *module_search_paths_ptr = nullptr;
1122-
if (target) {
1142+
if (target && target->IsValid()) {
11231143
module_search_paths = target->GetExecutableSearchPaths();
11241144
module_search_paths_ptr = &module_search_paths;
11251145
}

lldb/source/Plugins/DynamicLoader/Darwin-Kernel/DynamicLoaderDarwinKernel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ bool DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule(
789789
// Search for the kext on the local filesystem via the UUID
790790
if (!m_module_sp && m_uuid.IsValid()) {
791791
ModuleSpec module_spec;
792-
module_spec.SetTarget(&target);
792+
module_spec.SetTarget(target.shared_from_this());
793793
module_spec.GetUUID() = m_uuid;
794794
if (!m_uuid.IsValid())
795795
module_spec.GetArchitecture() = target.GetArchitecture();

lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ void DynamicLoaderPOSIXDYLD::ResolveExecutableModule(
901901
if (module_sp && module_sp->MatchesModuleSpec(module_spec))
902902
return;
903903

904-
module_spec.SetTarget(&target);
904+
module_spec.SetTarget(target.shared_from_this());
905905
const auto executable_search_paths(Target::GetDefaultExecutableSearchPaths());
906906
auto error = platform_sp->ResolveExecutable(module_spec, module_sp);
907907
if (error.Fail()) {

lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ bool ProcessElfCore::CanDebug(lldb::TargetSP target_sp,
8484
// For now we are just making sure the file exists for a given module
8585
if (!m_core_module_sp && FileSystem::Instance().Exists(m_core_file)) {
8686
ModuleSpec core_module_spec(m_core_file, target_sp->GetArchitecture());
87-
core_module_spec.SetTarget(target_sp.get());
87+
core_module_spec.SetTarget(target_sp);
8888
Status error(ModuleList::GetSharedModule(core_module_spec, m_core_module_sp,
8989
nullptr, nullptr));
9090
if (m_core_module_sp) {

lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ bool ProcessMachCore::CanDebug(lldb::TargetSP target_sp,
9595
// header but we should still try to use it -
9696
// ModuleSpecList::FindMatchingModuleSpec enforces a strict arch mach.
9797
ModuleSpec core_module_spec(m_core_file);
98-
core_module_spec.SetTarget(target_sp.get());
98+
core_module_spec.SetTarget(target_sp);
9999
Status error(ModuleList::GetSharedModule(core_module_spec, m_core_module_sp,
100100
nullptr, nullptr));
101101

lldb/source/Target/Platform.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -747,9 +747,8 @@ Status Platform::ResolveExecutable(const ModuleSpec &module_spec,
747747

748748
if (resolved_module_spec.GetArchitecture().IsValid() ||
749749
resolved_module_spec.GetUUID().IsValid()) {
750-
Status error =
751-
ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
752-
nullptr, nullptr);
750+
Status error = ModuleList::GetSharedModule(resolved_module_spec,
751+
exe_module_sp, nullptr, nullptr);
753752

754753
if (exe_module_sp && exe_module_sp->GetObjectFile())
755754
return error;
@@ -775,9 +774,8 @@ Status Platform::ResolveExecutable(const ModuleSpec &module_spec,
775774
nullptr, nullptr);
776775
}
777776

778-
error =
779-
ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
780-
nullptr, nullptr);
777+
error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
778+
nullptr, nullptr);
781779
if (error.Success()) {
782780
if (exe_module_sp && exe_module_sp->GetObjectFile())
783781
break;
@@ -1678,11 +1676,12 @@ void Platform::CallLocateModuleCallbackIfSet(const ModuleSpec &module_spec,
16781676
cached_module_spec.GetUUID().Clear(); // Clear UUID since it may contain md5
16791677
// content hash instead of real UUID.
16801678
cached_module_spec.GetFileSpec() = module_file_spec;
1679+
cached_module_spec.GetSymbolFileSpec() = symbol_file_spec;
16811680
cached_module_spec.GetPlatformFileSpec() = module_spec.GetFileSpec();
16821681
cached_module_spec.SetObjectOffset(0);
16831682

16841683
error = ModuleList::GetSharedModule(cached_module_spec, module_sp, nullptr,
1685-
did_create_ptr, false);
1684+
did_create_ptr, false, false);
16861685
if (error.Success() && module_sp) {
16871686
// Succeeded to load the module file.
16881687
LLDB_LOGF(log, "%s: locate module callback succeeded: module=%s symbol=%s",

lldb/source/Target/Target.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,7 +1773,7 @@ bool Target::SetArchitecture(const ArchSpec &arch_spec, bool set_platform,
17731773
arch_spec.GetArchitectureName(),
17741774
arch_spec.GetTriple().getTriple().c_str());
17751775
ModuleSpec module_spec(executable_sp->GetFileSpec(), other);
1776-
module_spec.SetTarget(this);
1776+
module_spec.SetTarget(this->shared_from_this());
17771777
Status error = ModuleList::GetSharedModule(module_spec, executable_sp,
17781778
nullptr, nullptr);
17791779

@@ -2344,7 +2344,7 @@ ModuleSP Target::GetOrCreateModule(const ModuleSpec &orig_module_spec,
23442344

23452345
// Apply any remappings specified in target.object-map:
23462346
ModuleSpec module_spec(orig_module_spec);
2347-
module_spec.SetTarget(this);
2347+
module_spec.SetTarget(this->shared_from_this());
23482348
PathMappingList &obj_mapping = GetObjectPathMap();
23492349
if (std::optional<FileSpec> remapped_obj_file =
23502350
obj_mapping.RemapPath(orig_module_spec.GetFileSpec().GetPath(),
@@ -2403,7 +2403,7 @@ ModuleSP Target::GetOrCreateModule(const ModuleSpec &orig_module_spec,
24032403
transformed_spec.GetFileSpec().SetDirectory(transformed_dir);
24042404
transformed_spec.GetFileSpec().SetFilename(
24052405
module_spec.GetFileSpec().GetFilename());
2406-
transformed_spec.SetTarget(this);
2406+
transformed_spec.SetTarget(this->shared_from_this());
24072407
error = ModuleList::GetSharedModule(transformed_spec, module_sp,
24082408
&old_modules, &did_create_module);
24092409
}

0 commit comments

Comments
 (0)