Skip to content

Commit bbf9688

Browse files
committed
[lldb] refactor Target::Install function
1 parent 0f97b48 commit bbf9688

File tree

1 file changed

+94
-40
lines changed

1 file changed

+94
-40
lines changed

lldb/source/Target/Target.cpp

Lines changed: 94 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,80 @@
7676
using namespace lldb;
7777
using namespace lldb_private;
7878

79+
namespace {
80+
81+
struct ExecutableInstaller {
82+
83+
ExecutableInstaller(PlatformSP platform, ModuleSP module)
84+
: m_platform{platform}, m_module{module},
85+
m_local_file{m_module->GetFileSpec()},
86+
m_remote_file{m_module->GetRemoteInstallFileSpec()} {}
87+
88+
void setupRemoteFile() const { m_module->SetPlatformFileSpec(m_remote_file); }
89+
90+
PlatformSP m_platform;
91+
ModuleSP m_module;
92+
const FileSpec m_local_file;
93+
const FileSpec m_remote_file;
94+
};
95+
96+
struct MainExecutableInstaller {
97+
98+
MainExecutableInstaller(PlatformSP platform, ModuleSP module, TargetSP target,
99+
ProcessLaunchInfo &launch_info)
100+
: m_platform{platform}, m_module{module},
101+
m_local_file{m_module->GetFileSpec()}, m_remote_file{getRemoteFileSpec(
102+
m_platform, target, m_module,
103+
m_local_file)},
104+
m_launch_info{launch_info} {}
105+
106+
void setupRemoteFile() const {
107+
m_module->SetPlatformFileSpec(m_remote_file);
108+
m_launch_info.SetExecutableFile(m_remote_file,
109+
/*add_exe_file_as_first_arg=*/false);
110+
m_platform->SetFilePermissions(m_remote_file, 0700 /*-rwx------*/);
111+
}
112+
113+
PlatformSP m_platform;
114+
ModuleSP m_module;
115+
const FileSpec m_local_file;
116+
const FileSpec m_remote_file;
117+
118+
private:
119+
static FileSpec getRemoteFileSpec(PlatformSP platform, TargetSP target,
120+
ModuleSP module,
121+
const FileSpec &local_file) {
122+
FileSpec remote_file = module->GetRemoteInstallFileSpec();
123+
if (remote_file || !target->GetAutoInstallMainExecutable())
124+
return remote_file;
125+
126+
if (!local_file)
127+
return {};
128+
129+
remote_file = platform->GetRemoteWorkingDirectory();
130+
remote_file.AppendPathComponent(local_file.GetFilename().GetCString());
131+
132+
return remote_file;
133+
}
134+
135+
ProcessLaunchInfo &m_launch_info;
136+
};
137+
} // namespace
138+
139+
template <typename Installer>
140+
static Status installExecutable(const Installer &installer) {
141+
if (!installer.m_local_file || !installer.m_remote_file)
142+
return Status();
143+
144+
Status error = installer.m_platform->Install(installer.m_local_file,
145+
installer.m_remote_file);
146+
if (error.Fail())
147+
return error;
148+
149+
installer.setupRemoteFile();
150+
return Status();
151+
}
152+
79153
constexpr std::chrono::milliseconds EvaluateExpressionOptions::default_timeout;
80154

81155
Target::Arch::Arch(const ArchSpec &spec)
@@ -3076,48 +3150,28 @@ TargetProperties &Target::GetGlobalProperties() {
30763150
Status Target::Install(ProcessLaunchInfo *launch_info) {
30773151
Status error;
30783152
PlatformSP platform_sp(GetPlatform());
3079-
if (platform_sp) {
3080-
if (platform_sp->IsRemote()) {
3081-
if (platform_sp->IsConnected()) {
3082-
// Install all files that have an install path when connected to a
3083-
// remote platform. If target.auto-install-main-executable is set then
3084-
// also install the main executable even if it does not have an explicit
3085-
// install path specified.
3086-
const ModuleList &modules = GetImages();
3087-
const size_t num_images = modules.GetSize();
3088-
for (size_t idx = 0; idx < num_images; ++idx) {
3089-
ModuleSP module_sp(modules.GetModuleAtIndex(idx));
3090-
if (module_sp) {
3091-
const bool is_main_executable = module_sp == GetExecutableModule();
3092-
FileSpec local_file(module_sp->GetFileSpec());
3093-
if (local_file) {
3094-
FileSpec remote_file(module_sp->GetRemoteInstallFileSpec());
3095-
if (!remote_file) {
3096-
if (is_main_executable && GetAutoInstallMainExecutable()) {
3097-
// Automatically install the main executable.
3098-
remote_file = platform_sp->GetRemoteWorkingDirectory();
3099-
remote_file.AppendPathComponent(
3100-
module_sp->GetFileSpec().GetFilename().GetCString());
3101-
}
3102-
}
3103-
if (remote_file) {
3104-
error = platform_sp->Install(local_file, remote_file);
3105-
if (error.Success()) {
3106-
module_sp->SetPlatformFileSpec(remote_file);
3107-
if (is_main_executable) {
3108-
platform_sp->SetFilePermissions(remote_file, 0700);
3109-
if (launch_info)
3110-
launch_info->SetExecutableFile(remote_file, false);
3111-
}
3112-
} else
3113-
break;
3114-
}
3115-
}
3116-
}
3117-
}
3118-
}
3153+
if (!platform_sp || !platform_sp->IsRemote() || !platform_sp->IsConnected())
3154+
return error;
3155+
3156+
// Install all files that have an install path when connected to a
3157+
// remote platform. If target.auto-install-main-executable is set then
3158+
// also install the main executable even if it does not have an explicit
3159+
// install path specified.
3160+
3161+
for (auto module_sp : GetImages().Modules()) {
3162+
if (module_sp == GetExecutableModule()) {
3163+
MainExecutableInstaller installer{platform_sp, module_sp,
3164+
shared_from_this(), *launch_info};
3165+
error = installExecutable(installer);
3166+
} else {
3167+
ExecutableInstaller installer{platform_sp, module_sp};
3168+
error = installExecutable(installer);
31193169
}
3170+
3171+
if (error.Fail())
3172+
return error;
31203173
}
3174+
31213175
return error;
31223176
}
31233177

0 commit comments

Comments
 (0)