https://github.com/dlav-sc updated https://github.com/llvm/llvm-project/pull/108996
>From 5161bbab487703278a53bda03e705c5408c80cd8 Mon Sep 17 00:00:00 2001 From: Daniil Avdeev <daniil.avd...@syntacore.com> Date: Tue, 23 Jul 2024 11:08:13 +0000 Subject: [PATCH 1/2] [lldb] refactor Target::Install function --- lldb/source/Target/Target.cpp | 133 ++++++++++++++++++++++++---------- 1 file changed, 93 insertions(+), 40 deletions(-) diff --git a/lldb/source/Target/Target.cpp b/lldb/source/Target/Target.cpp index f1659aae0800db..e8bcbbe7f07eae 100644 --- a/lldb/source/Target/Target.cpp +++ b/lldb/source/Target/Target.cpp @@ -76,6 +76,79 @@ using namespace lldb; using namespace lldb_private; +namespace { + +struct ExecutableInstaller { + + ExecutableInstaller(PlatformSP platform, ModuleSP module) + : m_platform{platform}, m_module{module}, + m_local_file{m_module->GetFileSpec()}, + m_remote_file{m_module->GetRemoteInstallFileSpec()} {} + + void setRemoteFile() const { m_module->SetPlatformFileSpec(m_remote_file); } + + PlatformSP m_platform; + ModuleSP m_module; + const FileSpec m_local_file; + const FileSpec m_remote_file; +}; + +struct MainExecutableInstaller { + + MainExecutableInstaller(PlatformSP platform, TargetSP target, ModuleSP module, + ProcessLaunchInfo *launch_info) + : m_platform{platform}, m_module{module}, + m_local_file{m_module->GetFileSpec()}, + m_remote_file{ + getRemoteFileSpec(m_platform, target, m_module, m_local_file)}, + m_launch_info{launch_info} {} + + void setRemoteFile() const { + m_module->SetPlatformFileSpec(m_remote_file); + m_launch_info->SetExecutableFile(m_remote_file, + false /*add_exe_file_as_first_arg*/); + m_platform->SetFilePermissions(m_remote_file, 0700 /*-rwx------*/); + } + + PlatformSP m_platform; + ModuleSP m_module; + const FileSpec m_local_file; + const FileSpec m_remote_file; + ProcessLaunchInfo *m_launch_info; + +private: + static FileSpec getRemoteFileSpec(PlatformSP platform, TargetSP target, + ModuleSP module, + const FileSpec &local_file) { + FileSpec remote_file = module->GetRemoteInstallFileSpec(); + if (remote_file || !target->GetAutoInstallMainExecutable()) + return remote_file; + + if (!local_file) + return {}; + + remote_file = platform->GetRemoteWorkingDirectory(); + remote_file.AppendPathComponent(local_file.GetFilename().GetCString()); + + return remote_file; + } +}; +} // namespace + +template <typename Installer> +static Status installExecutable(const Installer &installer) { + if (!installer.m_local_file || !installer.m_remote_file) + return Status(); + + Status error = installer.m_platform->Install(installer.m_local_file, + installer.m_remote_file); + if (error.Fail()) + return error; + + installer.setRemoteFile(); + return Status(); +} + constexpr std::chrono::milliseconds EvaluateExpressionOptions::default_timeout; Target::Arch::Arch(const ArchSpec &spec) @@ -3076,48 +3149,28 @@ TargetProperties &Target::GetGlobalProperties() { Status Target::Install(ProcessLaunchInfo *launch_info) { Status error; PlatformSP platform_sp(GetPlatform()); - if (platform_sp) { - if (platform_sp->IsRemote()) { - if (platform_sp->IsConnected()) { - // Install all files that have an install path when connected to a - // remote platform. If target.auto-install-main-executable is set then - // also install the main executable even if it does not have an explicit - // install path specified. - const ModuleList &modules = GetImages(); - const size_t num_images = modules.GetSize(); - for (size_t idx = 0; idx < num_images; ++idx) { - ModuleSP module_sp(modules.GetModuleAtIndex(idx)); - if (module_sp) { - const bool is_main_executable = module_sp == GetExecutableModule(); - FileSpec local_file(module_sp->GetFileSpec()); - if (local_file) { - FileSpec remote_file(module_sp->GetRemoteInstallFileSpec()); - if (!remote_file) { - if (is_main_executable && GetAutoInstallMainExecutable()) { - // Automatically install the main executable. - remote_file = platform_sp->GetRemoteWorkingDirectory(); - remote_file.AppendPathComponent( - module_sp->GetFileSpec().GetFilename().GetCString()); - } - } - if (remote_file) { - error = platform_sp->Install(local_file, remote_file); - if (error.Success()) { - module_sp->SetPlatformFileSpec(remote_file); - if (is_main_executable) { - platform_sp->SetFilePermissions(remote_file, 0700); - if (launch_info) - launch_info->SetExecutableFile(remote_file, false); - } - } else - break; - } - } - } - } - } + if (!platform_sp || !platform_sp->IsRemote() || !platform_sp->IsConnected()) + return error; + + // Install all files that have an install path when connected to a + // remote platform. If target.auto-install-main-executable is set then + // also install the main executable even if it does not have an explicit + // install path specified. + + for (auto module_sp : GetImages().Modules()) { + if (module_sp == GetExecutableModule()) { + MainExecutableInstaller installer{platform_sp, shared_from_this(), + module_sp, launch_info}; + error = installExecutable(installer); + } else { + ExecutableInstaller installer{platform_sp, module_sp}; + error = installExecutable(installer); } + + if (error.Fail()) + return error; } + return error; } >From 33feed8b2fe5075cb8de15561b9227f0a5b4dff4 Mon Sep 17 00:00:00 2001 From: Daniil Avdeev <daniil.avd...@syntacore.com> Date: Wed, 18 Sep 2024 14:00:03 +0300 Subject: [PATCH 2/2] use std::visit --- lldb/source/Target/Target.cpp | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/lldb/source/Target/Target.cpp b/lldb/source/Target/Target.cpp index e8bcbbe7f07eae..6792d8e090c2e2 100644 --- a/lldb/source/Target/Target.cpp +++ b/lldb/source/Target/Target.cpp @@ -135,8 +135,10 @@ struct MainExecutableInstaller { }; } // namespace -template <typename Installer> -static Status installExecutable(const Installer &installer) { +static using Installer = std::variant<ExecutableInstaller, MainExecutableInstaller>; + +template <typename I> +static Status installExecutable(const I &installer) { if (!installer.m_local_file || !installer.m_remote_file) return Status(); @@ -3158,15 +3160,8 @@ Status Target::Install(ProcessLaunchInfo *launch_info) { // install path specified. for (auto module_sp : GetImages().Modules()) { - if (module_sp == GetExecutableModule()) { - MainExecutableInstaller installer{platform_sp, shared_from_this(), - module_sp, launch_info}; - error = installExecutable(installer); - } else { - ExecutableInstaller installer{platform_sp, module_sp}; - error = installExecutable(installer); - } - + Installer installer = (module_sp == GetExecutableModule()) ? MainExecutableInstaller {platform_sp, shared_from_this(),module_sp, launch_info} : ExecutableInstaller {platform_sp, module_sp}; + std::visit([](const auto &&installer) -> Status { return installExecutable(installer) }, installer); if (error.Fail()) return error; } _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits