xref: /src/contrib/llvm-project/lldb/source/Target/RemoteAwarePlatform.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1cfca06d7SDimitry Andric //===-- RemoteAwarePlatform.cpp -------------------------------------------===//
25f29bb8aSDimitry Andric //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65f29bb8aSDimitry Andric //
75f29bb8aSDimitry Andric //===----------------------------------------------------------------------===//
85f29bb8aSDimitry Andric 
95f29bb8aSDimitry Andric #include "lldb/Target/RemoteAwarePlatform.h"
10cfca06d7SDimitry Andric #include "lldb/Core/Module.h"
11cfca06d7SDimitry Andric #include "lldb/Core/ModuleList.h"
12cfca06d7SDimitry Andric #include "lldb/Core/ModuleSpec.h"
135f29bb8aSDimitry Andric #include "lldb/Host/FileSystem.h"
145f29bb8aSDimitry Andric #include "lldb/Host/Host.h"
155f29bb8aSDimitry Andric #include "lldb/Host/HostInfo.h"
16cfca06d7SDimitry Andric #include "lldb/Utility/StreamString.h"
17e3b55780SDimitry Andric #include <optional>
185f29bb8aSDimitry Andric 
195f29bb8aSDimitry Andric using namespace lldb_private;
20cfca06d7SDimitry Andric using namespace lldb;
215f29bb8aSDimitry Andric 
GetModuleSpec(const FileSpec & module_file_spec,const ArchSpec & arch,ModuleSpec & module_spec)225f29bb8aSDimitry Andric bool RemoteAwarePlatform::GetModuleSpec(const FileSpec &module_file_spec,
235f29bb8aSDimitry Andric                                         const ArchSpec &arch,
245f29bb8aSDimitry Andric                                         ModuleSpec &module_spec) {
255f29bb8aSDimitry Andric   if (m_remote_platform_sp)
265f29bb8aSDimitry Andric     return m_remote_platform_sp->GetModuleSpec(module_file_spec, arch,
275f29bb8aSDimitry Andric                                                module_spec);
285f29bb8aSDimitry Andric 
29cfca06d7SDimitry Andric   return false;
30cfca06d7SDimitry Andric }
31cfca06d7SDimitry Andric 
ResolveExecutable(const ModuleSpec & module_spec,lldb::ModuleSP & exe_module_sp,const FileSpecList * module_search_paths_ptr)32cfca06d7SDimitry Andric Status RemoteAwarePlatform::ResolveExecutable(
33ac9a064cSDimitry Andric     const ModuleSpec &module_spec, lldb::ModuleSP &exe_module_sp,
34cfca06d7SDimitry Andric     const FileSpecList *module_search_paths_ptr) {
35cfca06d7SDimitry Andric   ModuleSpec resolved_module_spec(module_spec);
36cfca06d7SDimitry Andric 
37ac9a064cSDimitry Andric   // The host platform can resolve the path more aggressively.
38cfca06d7SDimitry Andric   if (IsHost()) {
39ac9a064cSDimitry Andric     FileSpec &resolved_file_spec = resolved_module_spec.GetFileSpec();
40ac9a064cSDimitry Andric 
41ac9a064cSDimitry Andric     if (!FileSystem::Instance().Exists(resolved_file_spec)) {
42ac9a064cSDimitry Andric       resolved_module_spec.GetFileSpec().SetFile(resolved_file_spec.GetPath(),
43cfca06d7SDimitry Andric                                                  FileSpec::Style::native);
44ac9a064cSDimitry Andric       FileSystem::Instance().Resolve(resolved_file_spec);
45cfca06d7SDimitry Andric     }
46cfca06d7SDimitry Andric 
47ac9a064cSDimitry Andric     if (!FileSystem::Instance().Exists(resolved_file_spec))
48ac9a064cSDimitry Andric       FileSystem::Instance().ResolveExecutableLocation(resolved_file_spec);
49ac9a064cSDimitry Andric   } else if (m_remote_platform_sp) {
50cfca06d7SDimitry Andric     return GetCachedExecutable(resolved_module_spec, exe_module_sp,
51c0981da4SDimitry Andric                                module_search_paths_ptr);
52cfca06d7SDimitry Andric   }
53cfca06d7SDimitry Andric 
54ac9a064cSDimitry Andric   return Platform::ResolveExecutable(resolved_module_spec, exe_module_sp,
55ac9a064cSDimitry Andric                                      module_search_paths_ptr);
565f29bb8aSDimitry Andric }
575f29bb8aSDimitry Andric 
RunShellCommand(llvm::StringRef command,const FileSpec & working_dir,int * status_ptr,int * signo_ptr,std::string * command_output,const Timeout<std::micro> & timeout)585f29bb8aSDimitry Andric Status RemoteAwarePlatform::RunShellCommand(
59b60736ecSDimitry Andric     llvm::StringRef command, const FileSpec &working_dir, int *status_ptr,
605f29bb8aSDimitry Andric     int *signo_ptr, std::string *command_output,
615f29bb8aSDimitry Andric     const Timeout<std::micro> &timeout) {
62b60736ecSDimitry Andric   return RunShellCommand(llvm::StringRef(), command, working_dir, status_ptr,
63b60736ecSDimitry Andric                          signo_ptr, command_output, timeout);
64b60736ecSDimitry Andric }
65b60736ecSDimitry Andric 
RunShellCommand(llvm::StringRef shell,llvm::StringRef command,const FileSpec & working_dir,int * status_ptr,int * signo_ptr,std::string * command_output,const Timeout<std::micro> & timeout)66b60736ecSDimitry Andric Status RemoteAwarePlatform::RunShellCommand(
67b60736ecSDimitry Andric     llvm::StringRef shell, llvm::StringRef command, const FileSpec &working_dir,
68b60736ecSDimitry Andric     int *status_ptr, int *signo_ptr, std::string *command_output,
69b60736ecSDimitry Andric     const Timeout<std::micro> &timeout) {
705f29bb8aSDimitry Andric   if (m_remote_platform_sp)
71b60736ecSDimitry Andric     return m_remote_platform_sp->RunShellCommand(shell, command, working_dir,
72b60736ecSDimitry Andric                                                  status_ptr, signo_ptr,
73b60736ecSDimitry Andric                                                  command_output, timeout);
74145449b1SDimitry Andric   return Platform::RunShellCommand(shell, command, working_dir, status_ptr,
75145449b1SDimitry Andric                                    signo_ptr, command_output, timeout);
765f29bb8aSDimitry Andric }
775f29bb8aSDimitry Andric 
MakeDirectory(const FileSpec & file_spec,uint32_t file_permissions)785f29bb8aSDimitry Andric Status RemoteAwarePlatform::MakeDirectory(const FileSpec &file_spec,
795f29bb8aSDimitry Andric                                           uint32_t file_permissions) {
805f29bb8aSDimitry Andric   if (m_remote_platform_sp)
815f29bb8aSDimitry Andric     return m_remote_platform_sp->MakeDirectory(file_spec, file_permissions);
825f29bb8aSDimitry Andric   return Platform::MakeDirectory(file_spec, file_permissions);
835f29bb8aSDimitry Andric }
845f29bb8aSDimitry Andric 
GetFilePermissions(const FileSpec & file_spec,uint32_t & file_permissions)855f29bb8aSDimitry Andric Status RemoteAwarePlatform::GetFilePermissions(const FileSpec &file_spec,
865f29bb8aSDimitry Andric                                                uint32_t &file_permissions) {
875f29bb8aSDimitry Andric   if (m_remote_platform_sp)
885f29bb8aSDimitry Andric     return m_remote_platform_sp->GetFilePermissions(file_spec,
895f29bb8aSDimitry Andric                                                     file_permissions);
905f29bb8aSDimitry Andric   return Platform::GetFilePermissions(file_spec, file_permissions);
915f29bb8aSDimitry Andric }
925f29bb8aSDimitry Andric 
SetFilePermissions(const FileSpec & file_spec,uint32_t file_permissions)935f29bb8aSDimitry Andric Status RemoteAwarePlatform::SetFilePermissions(const FileSpec &file_spec,
945f29bb8aSDimitry Andric                                                uint32_t file_permissions) {
955f29bb8aSDimitry Andric   if (m_remote_platform_sp)
965f29bb8aSDimitry Andric     return m_remote_platform_sp->SetFilePermissions(file_spec,
975f29bb8aSDimitry Andric                                                     file_permissions);
985f29bb8aSDimitry Andric   return Platform::SetFilePermissions(file_spec, file_permissions);
995f29bb8aSDimitry Andric }
1005f29bb8aSDimitry Andric 
OpenFile(const FileSpec & file_spec,File::OpenOptions flags,uint32_t mode,Status & error)1015f29bb8aSDimitry Andric lldb::user_id_t RemoteAwarePlatform::OpenFile(const FileSpec &file_spec,
102ead24645SDimitry Andric                                               File::OpenOptions flags,
103ead24645SDimitry Andric                                               uint32_t mode, Status &error) {
1045f29bb8aSDimitry Andric   if (m_remote_platform_sp)
1055f29bb8aSDimitry Andric     return m_remote_platform_sp->OpenFile(file_spec, flags, mode, error);
1065f29bb8aSDimitry Andric   return Platform::OpenFile(file_spec, flags, mode, error);
1075f29bb8aSDimitry Andric }
1085f29bb8aSDimitry Andric 
CloseFile(lldb::user_id_t fd,Status & error)1095f29bb8aSDimitry Andric bool RemoteAwarePlatform::CloseFile(lldb::user_id_t fd, Status &error) {
1105f29bb8aSDimitry Andric   if (m_remote_platform_sp)
1115f29bb8aSDimitry Andric     return m_remote_platform_sp->CloseFile(fd, error);
1125f29bb8aSDimitry Andric   return Platform::CloseFile(fd, error);
1135f29bb8aSDimitry Andric }
1145f29bb8aSDimitry Andric 
ReadFile(lldb::user_id_t fd,uint64_t offset,void * dst,uint64_t dst_len,Status & error)1155f29bb8aSDimitry Andric uint64_t RemoteAwarePlatform::ReadFile(lldb::user_id_t fd, uint64_t offset,
1165f29bb8aSDimitry Andric                                        void *dst, uint64_t dst_len,
1175f29bb8aSDimitry Andric                                        Status &error) {
1185f29bb8aSDimitry Andric   if (m_remote_platform_sp)
1195f29bb8aSDimitry Andric     return m_remote_platform_sp->ReadFile(fd, offset, dst, dst_len, error);
1205f29bb8aSDimitry Andric   return Platform::ReadFile(fd, offset, dst, dst_len, error);
1215f29bb8aSDimitry Andric }
1225f29bb8aSDimitry Andric 
WriteFile(lldb::user_id_t fd,uint64_t offset,const void * src,uint64_t src_len,Status & error)1235f29bb8aSDimitry Andric uint64_t RemoteAwarePlatform::WriteFile(lldb::user_id_t fd, uint64_t offset,
1245f29bb8aSDimitry Andric                                         const void *src, uint64_t src_len,
1255f29bb8aSDimitry Andric                                         Status &error) {
1265f29bb8aSDimitry Andric   if (m_remote_platform_sp)
1275f29bb8aSDimitry Andric     return m_remote_platform_sp->WriteFile(fd, offset, src, src_len, error);
1285f29bb8aSDimitry Andric   return Platform::WriteFile(fd, offset, src, src_len, error);
1295f29bb8aSDimitry Andric }
1305f29bb8aSDimitry Andric 
GetFileSize(const FileSpec & file_spec)1315f29bb8aSDimitry Andric lldb::user_id_t RemoteAwarePlatform::GetFileSize(const FileSpec &file_spec) {
1325f29bb8aSDimitry Andric   if (m_remote_platform_sp)
1335f29bb8aSDimitry Andric     return m_remote_platform_sp->GetFileSize(file_spec);
1345f29bb8aSDimitry Andric   return Platform::GetFileSize(file_spec);
1355f29bb8aSDimitry Andric }
1365f29bb8aSDimitry Andric 
CreateSymlink(const FileSpec & src,const FileSpec & dst)1375f29bb8aSDimitry Andric Status RemoteAwarePlatform::CreateSymlink(const FileSpec &src,
1385f29bb8aSDimitry Andric                                           const FileSpec &dst) {
1395f29bb8aSDimitry Andric   if (m_remote_platform_sp)
1405f29bb8aSDimitry Andric     return m_remote_platform_sp->CreateSymlink(src, dst);
1415f29bb8aSDimitry Andric   return Platform::CreateSymlink(src, dst);
1425f29bb8aSDimitry Andric }
1435f29bb8aSDimitry Andric 
GetFileExists(const FileSpec & file_spec)1445f29bb8aSDimitry Andric bool RemoteAwarePlatform::GetFileExists(const FileSpec &file_spec) {
1455f29bb8aSDimitry Andric   if (m_remote_platform_sp)
1465f29bb8aSDimitry Andric     return m_remote_platform_sp->GetFileExists(file_spec);
1475f29bb8aSDimitry Andric   return Platform::GetFileExists(file_spec);
1485f29bb8aSDimitry Andric }
1495f29bb8aSDimitry Andric 
Unlink(const FileSpec & file_spec)1505f29bb8aSDimitry Andric Status RemoteAwarePlatform::Unlink(const FileSpec &file_spec) {
1515f29bb8aSDimitry Andric   if (m_remote_platform_sp)
1525f29bb8aSDimitry Andric     return m_remote_platform_sp->Unlink(file_spec);
1535f29bb8aSDimitry Andric   return Platform::Unlink(file_spec);
1545f29bb8aSDimitry Andric }
1555f29bb8aSDimitry Andric 
156ac9a064cSDimitry Andric llvm::ErrorOr<llvm::MD5::MD5Result>
CalculateMD5(const FileSpec & file_spec)157ac9a064cSDimitry Andric RemoteAwarePlatform::CalculateMD5(const FileSpec &file_spec) {
1585f29bb8aSDimitry Andric   if (m_remote_platform_sp)
159ac9a064cSDimitry Andric     return m_remote_platform_sp->CalculateMD5(file_spec);
160ac9a064cSDimitry Andric   return Platform::CalculateMD5(file_spec);
1615f29bb8aSDimitry Andric }
1625f29bb8aSDimitry Andric 
GetRemoteWorkingDirectory()1635f29bb8aSDimitry Andric FileSpec RemoteAwarePlatform::GetRemoteWorkingDirectory() {
1645f29bb8aSDimitry Andric   if (IsRemote() && m_remote_platform_sp)
1655f29bb8aSDimitry Andric     return m_remote_platform_sp->GetRemoteWorkingDirectory();
1665f29bb8aSDimitry Andric   return Platform::GetRemoteWorkingDirectory();
1675f29bb8aSDimitry Andric }
1685f29bb8aSDimitry Andric 
SetRemoteWorkingDirectory(const FileSpec & working_dir)1695f29bb8aSDimitry Andric bool RemoteAwarePlatform::SetRemoteWorkingDirectory(
1705f29bb8aSDimitry Andric     const FileSpec &working_dir) {
1715f29bb8aSDimitry Andric   if (IsRemote() && m_remote_platform_sp)
1725f29bb8aSDimitry Andric     return m_remote_platform_sp->SetRemoteWorkingDirectory(working_dir);
1735f29bb8aSDimitry Andric   return Platform::SetRemoteWorkingDirectory(working_dir);
1745f29bb8aSDimitry Andric }
1755f29bb8aSDimitry Andric 
GetFileWithUUID(const FileSpec & platform_file,const UUID * uuid_ptr,FileSpec & local_file)1765f29bb8aSDimitry Andric Status RemoteAwarePlatform::GetFileWithUUID(const FileSpec &platform_file,
1775f29bb8aSDimitry Andric                                             const UUID *uuid_ptr,
1785f29bb8aSDimitry Andric                                             FileSpec &local_file) {
1795f29bb8aSDimitry Andric   if (IsRemote() && m_remote_platform_sp)
1805f29bb8aSDimitry Andric     return m_remote_platform_sp->GetFileWithUUID(platform_file, uuid_ptr,
1815f29bb8aSDimitry Andric                                                  local_file);
1825f29bb8aSDimitry Andric 
1835f29bb8aSDimitry Andric   // Default to the local case
1845f29bb8aSDimitry Andric   local_file = platform_file;
1855f29bb8aSDimitry Andric   return Status();
1865f29bb8aSDimitry Andric }
1875f29bb8aSDimitry Andric 
GetRemoteOSVersion()1885f29bb8aSDimitry Andric bool RemoteAwarePlatform::GetRemoteOSVersion() {
1895f29bb8aSDimitry Andric   if (m_remote_platform_sp) {
1905f29bb8aSDimitry Andric     m_os_version = m_remote_platform_sp->GetOSVersion();
1915f29bb8aSDimitry Andric     return !m_os_version.empty();
1925f29bb8aSDimitry Andric   }
1935f29bb8aSDimitry Andric   return false;
1945f29bb8aSDimitry Andric }
1955f29bb8aSDimitry Andric 
GetRemoteOSBuildString()196e3b55780SDimitry Andric std::optional<std::string> RemoteAwarePlatform::GetRemoteOSBuildString() {
1975f29bb8aSDimitry Andric   if (m_remote_platform_sp)
198c0981da4SDimitry Andric     return m_remote_platform_sp->GetRemoteOSBuildString();
199e3b55780SDimitry Andric   return std::nullopt;
2005f29bb8aSDimitry Andric }
2015f29bb8aSDimitry Andric 
GetRemoteOSKernelDescription()202e3b55780SDimitry Andric std::optional<std::string> RemoteAwarePlatform::GetRemoteOSKernelDescription() {
2035f29bb8aSDimitry Andric   if (m_remote_platform_sp)
204c0981da4SDimitry Andric     return m_remote_platform_sp->GetRemoteOSKernelDescription();
205e3b55780SDimitry Andric   return std::nullopt;
2065f29bb8aSDimitry Andric }
2075f29bb8aSDimitry Andric 
GetRemoteSystemArchitecture()2085f29bb8aSDimitry Andric ArchSpec RemoteAwarePlatform::GetRemoteSystemArchitecture() {
2095f29bb8aSDimitry Andric   if (m_remote_platform_sp)
2105f29bb8aSDimitry Andric     return m_remote_platform_sp->GetRemoteSystemArchitecture();
2115f29bb8aSDimitry Andric   return ArchSpec();
2125f29bb8aSDimitry Andric }
2135f29bb8aSDimitry Andric 
GetHostname()2145f29bb8aSDimitry Andric const char *RemoteAwarePlatform::GetHostname() {
2155f29bb8aSDimitry Andric   if (m_remote_platform_sp)
2165f29bb8aSDimitry Andric     return m_remote_platform_sp->GetHostname();
217145449b1SDimitry Andric   return Platform::GetHostname();
2185f29bb8aSDimitry Andric }
2195f29bb8aSDimitry Andric 
GetUserIDResolver()2205f29bb8aSDimitry Andric UserIDResolver &RemoteAwarePlatform::GetUserIDResolver() {
2215f29bb8aSDimitry Andric   if (m_remote_platform_sp)
2225f29bb8aSDimitry Andric     return m_remote_platform_sp->GetUserIDResolver();
223145449b1SDimitry Andric   return Platform::GetUserIDResolver();
2245f29bb8aSDimitry Andric }
2255f29bb8aSDimitry Andric 
GetEnvironment()2265f29bb8aSDimitry Andric Environment RemoteAwarePlatform::GetEnvironment() {
2275f29bb8aSDimitry Andric   if (m_remote_platform_sp)
2285f29bb8aSDimitry Andric     return m_remote_platform_sp->GetEnvironment();
229145449b1SDimitry Andric   return Platform::GetEnvironment();
2305f29bb8aSDimitry Andric }
2315f29bb8aSDimitry Andric 
IsConnected() const2325f29bb8aSDimitry Andric bool RemoteAwarePlatform::IsConnected() const {
233145449b1SDimitry Andric   if (m_remote_platform_sp)
2345f29bb8aSDimitry Andric     return m_remote_platform_sp->IsConnected();
235145449b1SDimitry Andric   return Platform::IsConnected();
2365f29bb8aSDimitry Andric }
2375f29bb8aSDimitry Andric 
GetProcessInfo(lldb::pid_t pid,ProcessInstanceInfo & process_info)2385f29bb8aSDimitry Andric bool RemoteAwarePlatform::GetProcessInfo(lldb::pid_t pid,
2395f29bb8aSDimitry Andric                                          ProcessInstanceInfo &process_info) {
2405f29bb8aSDimitry Andric   if (m_remote_platform_sp)
2415f29bb8aSDimitry Andric     return m_remote_platform_sp->GetProcessInfo(pid, process_info);
242145449b1SDimitry Andric   return Platform::GetProcessInfo(pid, process_info);
2435f29bb8aSDimitry Andric }
2445f29bb8aSDimitry Andric 
2455f29bb8aSDimitry Andric uint32_t
FindProcesses(const ProcessInstanceInfoMatch & match_info,ProcessInstanceInfoList & process_infos)2465f29bb8aSDimitry Andric RemoteAwarePlatform::FindProcesses(const ProcessInstanceInfoMatch &match_info,
2475f29bb8aSDimitry Andric                                    ProcessInstanceInfoList &process_infos) {
2485f29bb8aSDimitry Andric   if (m_remote_platform_sp)
2495f29bb8aSDimitry Andric     return m_remote_platform_sp->FindProcesses(match_info, process_infos);
250145449b1SDimitry Andric   return Platform::FindProcesses(match_info, process_infos);
2515f29bb8aSDimitry Andric }
2525f29bb8aSDimitry Andric 
ConnectProcess(llvm::StringRef connect_url,llvm::StringRef plugin_name,Debugger & debugger,Target * target,Status & error)2535f29bb8aSDimitry Andric lldb::ProcessSP RemoteAwarePlatform::ConnectProcess(llvm::StringRef connect_url,
2545f29bb8aSDimitry Andric                                                     llvm::StringRef plugin_name,
2555f29bb8aSDimitry Andric                                                     Debugger &debugger,
2565f29bb8aSDimitry Andric                                                     Target *target,
2575f29bb8aSDimitry Andric                                                     Status &error) {
2585f29bb8aSDimitry Andric   if (m_remote_platform_sp)
2595f29bb8aSDimitry Andric     return m_remote_platform_sp->ConnectProcess(connect_url, plugin_name,
2605f29bb8aSDimitry Andric                                                 debugger, target, error);
2615f29bb8aSDimitry Andric   return Platform::ConnectProcess(connect_url, plugin_name, debugger, target,
2625f29bb8aSDimitry Andric                                   error);
2635f29bb8aSDimitry Andric }
2645f29bb8aSDimitry Andric 
LaunchProcess(ProcessLaunchInfo & launch_info)2655f29bb8aSDimitry Andric Status RemoteAwarePlatform::LaunchProcess(ProcessLaunchInfo &launch_info) {
2665f29bb8aSDimitry Andric   if (m_remote_platform_sp)
267145449b1SDimitry Andric     return m_remote_platform_sp->LaunchProcess(launch_info);
268145449b1SDimitry Andric   return Platform::LaunchProcess(launch_info);
2695f29bb8aSDimitry Andric }
2705f29bb8aSDimitry Andric 
KillProcess(const lldb::pid_t pid)2715f29bb8aSDimitry Andric Status RemoteAwarePlatform::KillProcess(const lldb::pid_t pid) {
2725f29bb8aSDimitry Andric   if (m_remote_platform_sp)
2735f29bb8aSDimitry Andric     return m_remote_platform_sp->KillProcess(pid);
274145449b1SDimitry Andric   return Platform::KillProcess(pid);
2755f29bb8aSDimitry Andric }
276344a3780SDimitry Andric 
ConnectToWaitingProcesses(Debugger & debugger,Status & error)277344a3780SDimitry Andric size_t RemoteAwarePlatform::ConnectToWaitingProcesses(Debugger &debugger,
278344a3780SDimitry Andric                                                 Status &error) {
279344a3780SDimitry Andric   if (m_remote_platform_sp)
280344a3780SDimitry Andric     return m_remote_platform_sp->ConnectToWaitingProcesses(debugger, error);
281344a3780SDimitry Andric   return Platform::ConnectToWaitingProcesses(debugger, error);
282344a3780SDimitry Andric }
283