Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lldb/include/lldb/Core/ModuleList.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,9 @@ class ModuleList {

static Status
GetSharedModule(const ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
const FileSpecList *module_search_paths_ptr,
llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules,
bool *did_create_ptr, bool always_create = false);
bool *did_create_ptr, bool always_create = false,
bool invoke_locate_callback = true);

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

Expand Down
12 changes: 12 additions & 0 deletions lldb/include/lldb/Core/ModuleSpec.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
#include "lldb/Utility/Iterable.h"
#include "lldb/Utility/Stream.h"
#include "lldb/Utility/UUID.h"
#include "lldb/lldb-forward.h"

#include "llvm/Support/Chrono.h"

#include <memory>
#include <mutex>
#include <vector>

Expand Down Expand Up @@ -126,6 +128,12 @@ class ModuleSpec {

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

lldb::TargetSP GetTargetSP() { return m_target.lock(); }

lldb::TargetSP GetTargetSP() const { return m_target.lock(); }

void SetTarget(std::shared_ptr<Target> target) { m_target = target; }

void Clear() {
m_file.Clear();
m_platform_file.Clear();
Expand All @@ -137,6 +145,7 @@ class ModuleSpec {
m_object_size = 0;
m_source_mappings.Clear(false);
m_object_mod_time = llvm::sys::TimePoint<>();
m_target.reset();
}

explicit operator bool() const {
Expand Down Expand Up @@ -265,6 +274,9 @@ class ModuleSpec {
ArchSpec m_arch;
UUID m_uuid;
ConstString m_object_name;
/// This is set to take advantage of the target's search path and platform's
/// locate module callback
std::weak_ptr<Target> m_target;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change the name to m_target_wp to indicate a weak pointer. We name shared pointers with a _sp suffix and weak pointers with a _wp

uint64_t m_object_offset = 0;
uint64_t m_object_size = 0;
llvm::sys::TimePoint<> m_object_mod_time;
Expand Down
16 changes: 8 additions & 8 deletions lldb/include/lldb/Target/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ class Platform : public PluginInterface {
/// Returns \b true if this Platform plug-in was able to find
/// a suitable executable, \b false otherwise.
virtual Status ResolveExecutable(const ModuleSpec &module_spec,
lldb::ModuleSP &exe_module_sp,
const FileSpecList *module_search_paths_ptr);
lldb::ModuleSP &exe_module_sp);

/// Find a symbol file given a symbol file module specification.
///
Expand Down Expand Up @@ -304,10 +303,11 @@ class Platform : public PluginInterface {
/// \return
/// The Status object for any errors found while searching for
/// the binary.
virtual Status GetSharedModule(
const ModuleSpec &module_spec, Process *process,
lldb::ModuleSP &module_sp, const FileSpecList *module_search_paths_ptr,
llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr);
virtual Status
GetSharedModule(const ModuleSpec &module_spec, Process *process,
lldb::ModuleSP &module_sp,
llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules,
bool *did_create_ptr);

void CallLocateModuleCallbackIfSet(const ModuleSpec &module_spec,
lldb::ModuleSP &module_sp,
Expand Down Expand Up @@ -1039,8 +1039,8 @@ class Platform : public PluginInterface {
/// predefined trap handlers, this method may be a no-op.
virtual void CalculateTrapHandlerSymbolNames() = 0;

Status GetCachedExecutable(ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
const FileSpecList *module_search_paths_ptr);
Status GetCachedExecutable(ModuleSpec &module_spec,
lldb::ModuleSP &module_sp);

virtual Status DownloadModuleSlice(const FileSpec &src_file_spec,
const uint64_t src_offset,
Expand Down
6 changes: 2 additions & 4 deletions lldb/include/lldb/Target/RemoteAwarePlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ class RemoteAwarePlatform : public Platform {
public:
using Platform::Platform;

virtual Status
ResolveExecutable(const ModuleSpec &module_spec,
lldb::ModuleSP &exe_module_sp,
const FileSpecList *module_search_paths_ptr) override;
virtual Status ResolveExecutable(const ModuleSpec &module_spec,
lldb::ModuleSP &exe_module_sp) override;

bool GetModuleSpec(const FileSpec &module_file_spec, const ArchSpec &arch,
ModuleSpec &module_spec) override;
Expand Down
4 changes: 2 additions & 2 deletions lldb/source/API/SBModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ SBModule::SBModule(const SBModuleSpec &module_spec) {
LLDB_INSTRUMENT_VA(this, module_spec);

ModuleSP module_sp;
Status error = ModuleList::GetSharedModule(
*module_spec.m_opaque_up, module_sp, nullptr, nullptr, nullptr);
Status error = ModuleList::GetSharedModule(*module_spec.m_opaque_up,
module_sp, nullptr, nullptr);
if (module_sp)
SetSP(module_sp);
}
Expand Down
5 changes: 3 additions & 2 deletions lldb/source/Core/DynamicLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ ModuleSP DynamicLoader::LoadBinaryWithUUIDAndAddress(
}
}
ModuleSpec module_spec;
module_spec.SetTarget(target.shared_from_this());
module_spec.GetUUID() = uuid;
FileSpec name_filespec(name);
if (FileSystem::Instance().Exists(name_filespec))
Expand All @@ -238,8 +239,8 @@ ModuleSP DynamicLoader::LoadBinaryWithUUIDAndAddress(
// Has lldb already seen a module with this UUID?
// Or have external lookup enabled in DebugSymbols on macOS.
if (!module_sp)
error = ModuleList::GetSharedModule(module_spec, module_sp, nullptr,
nullptr, nullptr);
error =
ModuleList::GetSharedModule(module_spec, module_sp, nullptr, nullptr);

// Can lldb's symbol/executable location schemes
// find an executable and symbol file.
Expand Down
31 changes: 29 additions & 2 deletions lldb/source/Core/ModuleList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Symbol/TypeList.h"
#include "lldb/Symbol/VariableList.h"
#include "lldb/Target/Platform.h"
#include "lldb/Target/Target.h"
#include "lldb/Utility/ArchSpec.h"
#include "lldb/Utility/ConstString.h"
#include "lldb/Utility/FileSpecList.h"
Expand Down Expand Up @@ -1038,9 +1040,9 @@ size_t ModuleList::RemoveOrphanSharedModules(bool mandatory) {

Status
ModuleList::GetSharedModule(const ModuleSpec &module_spec, ModuleSP &module_sp,
const FileSpecList *module_search_paths_ptr,
llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules,
bool *did_create_ptr, bool always_create) {
bool *did_create_ptr, bool always_create,
bool invoke_locate_callback) {
SharedModuleList &shared_module_list = GetSharedModuleList();
std::lock_guard<std::recursive_mutex> guard(shared_module_list.GetMutex());
char path[PATH_MAX];
Expand Down Expand Up @@ -1095,6 +1097,22 @@ ModuleList::GetSharedModule(const ModuleSpec &module_spec, ModuleSP &module_sp,
if (module_sp)
return error;

// Try target's platform locate module callback before second attempt.
if (invoke_locate_callback) {
TargetSP target_sp = module_spec.GetTargetSP();
if (target_sp && target_sp->IsValid()) {
if (PlatformSP platform_sp = target_sp->GetPlatform()) {
FileSpec symbol_file_spec;
platform_sp->CallLocateModuleCallbackIfSet(
module_spec, module_sp, symbol_file_spec, did_create_ptr);
if (module_sp) {
// The callback found a module.
return error;
}
}
}
}

module_sp = std::make_shared<Module>(module_spec);
// Make sure there are a module and an object file since we can specify a
// valid file path with an architecture that might not be in that file. By
Expand Down Expand Up @@ -1122,6 +1140,15 @@ ModuleList::GetSharedModule(const ModuleSpec &module_spec, ModuleSP &module_sp,
module_sp.reset();
}

// Get module search paths from the target if available.
lldb::TargetSP target_sp = module_spec.GetTargetSP();
FileSpecList module_search_paths;
FileSpecList *module_search_paths_ptr = nullptr;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this pointer, just use module_search_paths everywhere in this function.

if (target_sp && target_sp->IsValid()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to check target_sp->IsValid() here

module_search_paths = target_sp->GetExecutableSearchPaths();
module_search_paths_ptr = &module_search_paths;
}

if (module_search_paths_ptr) {
const auto num_directories = module_search_paths_ptr->GetSize();
for (size_t idx = 0; idx < num_directories; ++idx) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ bool DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule(
// Search for the kext on the local filesystem via the UUID
if (!m_module_sp && m_uuid.IsValid()) {
ModuleSpec module_spec;
module_spec.SetTarget(target.shared_from_this());
module_spec.GetUUID() = m_uuid;
if (!m_uuid.IsValid())
module_spec.GetArchitecture() = target.GetArchitecture();
Expand All @@ -801,9 +802,8 @@ bool DynamicLoaderDarwinKernel::KextImageInfo::LoadImageUsingMemoryModule(
// system.
PlatformSP platform_sp(target.GetPlatform());
if (platform_sp) {
FileSpecList search_paths = target.GetExecutableSearchPaths();
platform_sp->GetSharedModule(module_spec, process, m_module_sp,
&search_paths, nullptr, nullptr);
platform_sp->GetSharedModule(module_spec, process, m_module_sp, nullptr,
nullptr);
}

// Ask the Target to find this file on the local system, if possible.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -901,10 +901,9 @@ void DynamicLoaderPOSIXDYLD::ResolveExecutableModule(
if (module_sp && module_sp->MatchesModuleSpec(module_spec))
return;

module_spec.SetTarget(target.shared_from_this());
const auto executable_search_paths(Target::GetDefaultExecutableSearchPaths());
auto error = platform_sp->ResolveExecutable(
module_spec, module_sp,
!executable_search_paths.IsEmpty() ? &executable_search_paths : nullptr);
auto error = platform_sp->ResolveExecutable(module_spec, module_sp);
if (error.Fail()) {
StreamString stream;
module_spec.Dump(stream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@ Status PlatformAppleSimulator::GetSymbolFile(const FileSpec &platform_file,

Status PlatformAppleSimulator::GetSharedModule(
const ModuleSpec &module_spec, Process *process, ModuleSP &module_sp,
const FileSpecList *module_search_paths_ptr,
llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr) {
// For iOS/tvOS/watchOS, the SDK files are all cached locally on the
// host system. So first we ask for the file in the cached SDK, then
Expand All @@ -432,12 +431,10 @@ Status PlatformAppleSimulator::GetSharedModule(
error = GetSymbolFile(platform_file, module_spec.GetUUIDPtr(),
platform_module_spec.GetFileSpec());
if (error.Success()) {
error = ResolveExecutable(platform_module_spec, module_sp,
module_search_paths_ptr);
error = ResolveExecutable(platform_module_spec, module_sp);
} else {
const bool always_create = false;
error = ModuleList::GetSharedModule(module_spec, module_sp,
module_search_paths_ptr, old_modules,
error = ModuleList::GetSharedModule(module_spec, module_sp, old_modules,
did_create_ptr, always_create);
}
if (module_sp)
Expand Down Expand Up @@ -660,4 +657,3 @@ void PlatformAppleSimulator::Terminate() {
PlatformDarwin::Terminate();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class PlatformAppleSimulator : public PlatformDarwin {

Status GetSharedModule(const ModuleSpec &module_spec, Process *process,
lldb::ModuleSP &module_sp,
const FileSpecList *module_search_paths_ptr,
llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules,
bool *did_create_ptr) override;

Expand Down
47 changes: 26 additions & 21 deletions lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ Status PlatformDarwin::ResolveSymbolFile(Target &target,

Status PlatformDarwin::GetSharedModule(
const ModuleSpec &module_spec, Process *process, ModuleSP &module_sp,
const FileSpecList *module_search_paths_ptr,
llvm::SmallVectorImpl<ModuleSP> *old_modules, bool *did_create_ptr) {
Status error;
module_sp.reset();
Expand All @@ -341,19 +340,22 @@ Status PlatformDarwin::GetSharedModule(
// module first.
if (m_remote_platform_sp) {
error = m_remote_platform_sp->GetSharedModule(
module_spec, process, module_sp, module_search_paths_ptr, old_modules,
did_create_ptr);
module_spec, process, module_sp, old_modules, did_create_ptr);
}
}

if (!module_sp) {
// Fall back to the local platform and find the file locally
error = Platform::GetSharedModule(module_spec, process, module_sp,
module_search_paths_ptr, old_modules,
did_create_ptr);
old_modules, did_create_ptr);

const FileSpec &platform_file = module_spec.GetFileSpec();
if (!module_sp && module_search_paths_ptr && platform_file) {
// Get module search paths from the target if available.
TargetSP target_sp = module_spec.GetTargetSP();
FileSpecList module_search_paths;
if (target_sp)
module_search_paths = target_sp->GetExecutableSearchPaths();
if (!module_sp && !module_search_paths.IsEmpty() && platform_file) {
// We can try to pull off part of the file path up to the bundle
// directory level and try any module search paths...
FileSpec bundle_directory;
Expand All @@ -362,9 +364,9 @@ Status PlatformDarwin::GetSharedModule(
ModuleSpec new_module_spec(module_spec);
new_module_spec.GetFileSpec() = bundle_directory;
if (Host::ResolveExecutableInBundle(new_module_spec.GetFileSpec())) {
Status new_error(Platform::GetSharedModule(
new_module_spec, process, module_sp, nullptr, old_modules,
did_create_ptr));
Status new_error(Platform::GetSharedModule(new_module_spec, process,
module_sp, old_modules,
did_create_ptr));

if (module_sp)
return new_error;
Expand All @@ -376,10 +378,10 @@ Status PlatformDarwin::GetSharedModule(
const size_t bundle_directory_len =
bundle_directory.GetPath(bundle_dir, sizeof(bundle_dir));
char new_path[PATH_MAX];
size_t num_module_search_paths = module_search_paths_ptr->GetSize();
size_t num_module_search_paths = module_search_paths.GetSize();
for (size_t i = 0; i < num_module_search_paths; ++i) {
const size_t search_path_len =
module_search_paths_ptr->GetFileSpecAtIndex(i).GetPath(
module_search_paths.GetFileSpecAtIndex(i).GetPath(
new_path, sizeof(new_path));
if (search_path_len < sizeof(new_path)) {
snprintf(new_path + search_path_len,
Expand All @@ -390,7 +392,7 @@ Status PlatformDarwin::GetSharedModule(
ModuleSpec new_module_spec(module_spec);
new_module_spec.GetFileSpec() = new_file_spec;
Status new_error(Platform::GetSharedModule(
new_module_spec, process, module_sp, nullptr, old_modules,
new_module_spec, process, module_sp, old_modules,
did_create_ptr));

if (module_sp) {
Expand Down Expand Up @@ -1303,12 +1305,15 @@ PlatformDarwin::LaunchProcess(lldb_private::ProcessLaunchInfo &launch_info) {

lldb_private::Status PlatformDarwin::FindBundleBinaryInExecSearchPaths(
const ModuleSpec &module_spec, Process *process, ModuleSP &module_sp,
const FileSpecList *module_search_paths_ptr,
llvm::SmallVectorImpl<ModuleSP> *old_modules, bool *did_create_ptr) {
const FileSpec &platform_file = module_spec.GetFileSpec();
// See if the file is present in any of the module_search_paths_ptr
TargetSP target_sp = module_spec.GetTargetSP();
FileSpecList module_search_paths;
if (target_sp)
module_search_paths = target_sp->GetExecutableSearchPaths();
// See if the file is present in any of the module_search_paths
// directories.
if (!module_sp && module_search_paths_ptr && platform_file) {
if (!module_sp && !module_search_paths.IsEmpty() && platform_file) {
// create a vector of all the file / directory names in platform_file e.g.
// this might be
// /System/Library/PrivateFrameworks/UIFoundation.framework/UIFoundation
Expand All @@ -1322,21 +1327,21 @@ lldb_private::Status PlatformDarwin::FindBundleBinaryInExecSearchPaths(
std::reverse(path_parts.begin(), path_parts.end());
const size_t path_parts_size = path_parts.size();

size_t num_module_search_paths = module_search_paths_ptr->GetSize();
size_t num_module_search_paths = module_search_paths.GetSize();
for (size_t i = 0; i < num_module_search_paths; ++i) {
Log *log_verbose = GetLog(LLDBLog::Host);
LLDB_LOGF(
log_verbose,
"PlatformRemoteDarwinDevice::GetSharedModule searching for binary in "
"search-path %s",
module_search_paths_ptr->GetFileSpecAtIndex(i).GetPath().c_str());
module_search_paths.GetFileSpecAtIndex(i).GetPath().c_str());
// Create a new FileSpec with this module_search_paths_ptr plus just the
// filename ("UIFoundation"), then the parent dir plus filename
// ("UIFoundation.framework/UIFoundation") etc - up to four names (to
// handle "Foo.framework/Contents/MacOS/Foo")

for (size_t j = 0; j < 4 && j < path_parts_size - 1; ++j) {
FileSpec path_to_try(module_search_paths_ptr->GetFileSpecAtIndex(i));
FileSpec path_to_try(module_search_paths.GetFileSpecAtIndex(i));

// Add the components backwards. For
// .../PrivateFrameworks/UIFoundation.framework/UIFoundation path_parts
Expand All @@ -1356,9 +1361,9 @@ lldb_private::Status PlatformDarwin::FindBundleBinaryInExecSearchPaths(
if (FileSystem::Instance().Exists(path_to_try)) {
ModuleSpec new_module_spec(module_spec);
new_module_spec.GetFileSpec() = path_to_try;
Status new_error(
Platform::GetSharedModule(new_module_spec, process, module_sp,
nullptr, old_modules, did_create_ptr));
Status new_error(Platform::GetSharedModule(new_module_spec, process,
module_sp, old_modules,
did_create_ptr));

if (module_sp) {
module_sp->SetPlatformFileSpec(path_to_try);
Expand Down
Loading
Loading