xref: /freebsd-src/contrib/llvm-project/lldb/source/Target/RemoteAwarePlatform.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
15ffd83dbSDimitry Andric //===-- RemoteAwarePlatform.cpp -------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/Target/RemoteAwarePlatform.h"
105ffd83dbSDimitry Andric #include "lldb/Core/Module.h"
115ffd83dbSDimitry Andric #include "lldb/Core/ModuleList.h"
125ffd83dbSDimitry Andric #include "lldb/Core/ModuleSpec.h"
130b57cec5SDimitry Andric #include "lldb/Host/FileSystem.h"
140b57cec5SDimitry Andric #include "lldb/Host/Host.h"
150b57cec5SDimitry Andric #include "lldb/Host/HostInfo.h"
165ffd83dbSDimitry Andric #include "lldb/Utility/StreamString.h"
17bdd1243dSDimitry Andric #include <optional>
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric using namespace lldb_private;
205ffd83dbSDimitry Andric using namespace lldb;
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric bool RemoteAwarePlatform::GetModuleSpec(const FileSpec &module_file_spec,
230b57cec5SDimitry Andric                                         const ArchSpec &arch,
240b57cec5SDimitry Andric                                         ModuleSpec &module_spec) {
250b57cec5SDimitry Andric   if (m_remote_platform_sp)
260b57cec5SDimitry Andric     return m_remote_platform_sp->GetModuleSpec(module_file_spec, arch,
270b57cec5SDimitry Andric                                                module_spec);
280b57cec5SDimitry Andric 
295ffd83dbSDimitry Andric   return false;
305ffd83dbSDimitry Andric }
315ffd83dbSDimitry Andric 
325ffd83dbSDimitry Andric Status RemoteAwarePlatform::ResolveExecutable(
33*0fca6ea1SDimitry Andric     const ModuleSpec &module_spec, lldb::ModuleSP &exe_module_sp,
345ffd83dbSDimitry Andric     const FileSpecList *module_search_paths_ptr) {
355ffd83dbSDimitry Andric   ModuleSpec resolved_module_spec(module_spec);
365ffd83dbSDimitry Andric 
37*0fca6ea1SDimitry Andric   // The host platform can resolve the path more aggressively.
385ffd83dbSDimitry Andric   if (IsHost()) {
39*0fca6ea1SDimitry Andric     FileSpec &resolved_file_spec = resolved_module_spec.GetFileSpec();
40*0fca6ea1SDimitry Andric 
41*0fca6ea1SDimitry Andric     if (!FileSystem::Instance().Exists(resolved_file_spec)) {
42*0fca6ea1SDimitry Andric       resolved_module_spec.GetFileSpec().SetFile(resolved_file_spec.GetPath(),
435ffd83dbSDimitry Andric                                                  FileSpec::Style::native);
44*0fca6ea1SDimitry Andric       FileSystem::Instance().Resolve(resolved_file_spec);
455ffd83dbSDimitry Andric     }
465ffd83dbSDimitry Andric 
47*0fca6ea1SDimitry Andric     if (!FileSystem::Instance().Exists(resolved_file_spec))
48*0fca6ea1SDimitry Andric       FileSystem::Instance().ResolveExecutableLocation(resolved_file_spec);
49*0fca6ea1SDimitry Andric   } else if (m_remote_platform_sp) {
505ffd83dbSDimitry Andric     return GetCachedExecutable(resolved_module_spec, exe_module_sp,
51349cc55cSDimitry Andric                                module_search_paths_ptr);
525ffd83dbSDimitry Andric   }
535ffd83dbSDimitry Andric 
54*0fca6ea1SDimitry Andric   return Platform::ResolveExecutable(resolved_module_spec, exe_module_sp,
55*0fca6ea1SDimitry Andric                                      module_search_paths_ptr);
560b57cec5SDimitry Andric }
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric Status RemoteAwarePlatform::RunShellCommand(
59e8d8bef9SDimitry Andric     llvm::StringRef command, const FileSpec &working_dir, int *status_ptr,
600b57cec5SDimitry Andric     int *signo_ptr, std::string *command_output,
610b57cec5SDimitry Andric     const Timeout<std::micro> &timeout) {
62e8d8bef9SDimitry Andric   return RunShellCommand(llvm::StringRef(), command, working_dir, status_ptr,
63e8d8bef9SDimitry Andric                          signo_ptr, command_output, timeout);
64e8d8bef9SDimitry Andric }
65e8d8bef9SDimitry Andric 
66e8d8bef9SDimitry Andric Status RemoteAwarePlatform::RunShellCommand(
67e8d8bef9SDimitry Andric     llvm::StringRef shell, llvm::StringRef command, const FileSpec &working_dir,
68e8d8bef9SDimitry Andric     int *status_ptr, int *signo_ptr, std::string *command_output,
69e8d8bef9SDimitry Andric     const Timeout<std::micro> &timeout) {
700b57cec5SDimitry Andric   if (m_remote_platform_sp)
71e8d8bef9SDimitry Andric     return m_remote_platform_sp->RunShellCommand(shell, command, working_dir,
72e8d8bef9SDimitry Andric                                                  status_ptr, signo_ptr,
73e8d8bef9SDimitry Andric                                                  command_output, timeout);
7481ad6265SDimitry Andric   return Platform::RunShellCommand(shell, command, working_dir, status_ptr,
7581ad6265SDimitry Andric                                    signo_ptr, command_output, timeout);
760b57cec5SDimitry Andric }
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric Status RemoteAwarePlatform::MakeDirectory(const FileSpec &file_spec,
790b57cec5SDimitry Andric                                           uint32_t file_permissions) {
800b57cec5SDimitry Andric   if (m_remote_platform_sp)
810b57cec5SDimitry Andric     return m_remote_platform_sp->MakeDirectory(file_spec, file_permissions);
820b57cec5SDimitry Andric   return Platform::MakeDirectory(file_spec, file_permissions);
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric Status RemoteAwarePlatform::GetFilePermissions(const FileSpec &file_spec,
860b57cec5SDimitry Andric                                                uint32_t &file_permissions) {
870b57cec5SDimitry Andric   if (m_remote_platform_sp)
880b57cec5SDimitry Andric     return m_remote_platform_sp->GetFilePermissions(file_spec,
890b57cec5SDimitry Andric                                                     file_permissions);
900b57cec5SDimitry Andric   return Platform::GetFilePermissions(file_spec, file_permissions);
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric Status RemoteAwarePlatform::SetFilePermissions(const FileSpec &file_spec,
940b57cec5SDimitry Andric                                                uint32_t file_permissions) {
950b57cec5SDimitry Andric   if (m_remote_platform_sp)
960b57cec5SDimitry Andric     return m_remote_platform_sp->SetFilePermissions(file_spec,
970b57cec5SDimitry Andric                                                     file_permissions);
980b57cec5SDimitry Andric   return Platform::SetFilePermissions(file_spec, file_permissions);
990b57cec5SDimitry Andric }
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric lldb::user_id_t RemoteAwarePlatform::OpenFile(const FileSpec &file_spec,
1029dba64beSDimitry Andric                                               File::OpenOptions flags,
1039dba64beSDimitry Andric                                               uint32_t mode, Status &error) {
1040b57cec5SDimitry Andric   if (m_remote_platform_sp)
1050b57cec5SDimitry Andric     return m_remote_platform_sp->OpenFile(file_spec, flags, mode, error);
1060b57cec5SDimitry Andric   return Platform::OpenFile(file_spec, flags, mode, error);
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric 
1090b57cec5SDimitry Andric bool RemoteAwarePlatform::CloseFile(lldb::user_id_t fd, Status &error) {
1100b57cec5SDimitry Andric   if (m_remote_platform_sp)
1110b57cec5SDimitry Andric     return m_remote_platform_sp->CloseFile(fd, error);
1120b57cec5SDimitry Andric   return Platform::CloseFile(fd, error);
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric uint64_t RemoteAwarePlatform::ReadFile(lldb::user_id_t fd, uint64_t offset,
1160b57cec5SDimitry Andric                                        void *dst, uint64_t dst_len,
1170b57cec5SDimitry Andric                                        Status &error) {
1180b57cec5SDimitry Andric   if (m_remote_platform_sp)
1190b57cec5SDimitry Andric     return m_remote_platform_sp->ReadFile(fd, offset, dst, dst_len, error);
1200b57cec5SDimitry Andric   return Platform::ReadFile(fd, offset, dst, dst_len, error);
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric uint64_t RemoteAwarePlatform::WriteFile(lldb::user_id_t fd, uint64_t offset,
1240b57cec5SDimitry Andric                                         const void *src, uint64_t src_len,
1250b57cec5SDimitry Andric                                         Status &error) {
1260b57cec5SDimitry Andric   if (m_remote_platform_sp)
1270b57cec5SDimitry Andric     return m_remote_platform_sp->WriteFile(fd, offset, src, src_len, error);
1280b57cec5SDimitry Andric   return Platform::WriteFile(fd, offset, src, src_len, error);
1290b57cec5SDimitry Andric }
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric lldb::user_id_t RemoteAwarePlatform::GetFileSize(const FileSpec &file_spec) {
1320b57cec5SDimitry Andric   if (m_remote_platform_sp)
1330b57cec5SDimitry Andric     return m_remote_platform_sp->GetFileSize(file_spec);
1340b57cec5SDimitry Andric   return Platform::GetFileSize(file_spec);
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric Status RemoteAwarePlatform::CreateSymlink(const FileSpec &src,
1380b57cec5SDimitry Andric                                           const FileSpec &dst) {
1390b57cec5SDimitry Andric   if (m_remote_platform_sp)
1400b57cec5SDimitry Andric     return m_remote_platform_sp->CreateSymlink(src, dst);
1410b57cec5SDimitry Andric   return Platform::CreateSymlink(src, dst);
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric bool RemoteAwarePlatform::GetFileExists(const FileSpec &file_spec) {
1450b57cec5SDimitry Andric   if (m_remote_platform_sp)
1460b57cec5SDimitry Andric     return m_remote_platform_sp->GetFileExists(file_spec);
1470b57cec5SDimitry Andric   return Platform::GetFileExists(file_spec);
1480b57cec5SDimitry Andric }
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric Status RemoteAwarePlatform::Unlink(const FileSpec &file_spec) {
1510b57cec5SDimitry Andric   if (m_remote_platform_sp)
1520b57cec5SDimitry Andric     return m_remote_platform_sp->Unlink(file_spec);
1530b57cec5SDimitry Andric   return Platform::Unlink(file_spec);
1540b57cec5SDimitry Andric }
1550b57cec5SDimitry Andric 
156*0fca6ea1SDimitry Andric llvm::ErrorOr<llvm::MD5::MD5Result>
157*0fca6ea1SDimitry Andric RemoteAwarePlatform::CalculateMD5(const FileSpec &file_spec) {
1580b57cec5SDimitry Andric   if (m_remote_platform_sp)
159*0fca6ea1SDimitry Andric     return m_remote_platform_sp->CalculateMD5(file_spec);
160*0fca6ea1SDimitry Andric   return Platform::CalculateMD5(file_spec);
1610b57cec5SDimitry Andric }
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric FileSpec RemoteAwarePlatform::GetRemoteWorkingDirectory() {
1640b57cec5SDimitry Andric   if (IsRemote() && m_remote_platform_sp)
1650b57cec5SDimitry Andric     return m_remote_platform_sp->GetRemoteWorkingDirectory();
1660b57cec5SDimitry Andric   return Platform::GetRemoteWorkingDirectory();
1670b57cec5SDimitry Andric }
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric bool RemoteAwarePlatform::SetRemoteWorkingDirectory(
1700b57cec5SDimitry Andric     const FileSpec &working_dir) {
1710b57cec5SDimitry Andric   if (IsRemote() && m_remote_platform_sp)
1720b57cec5SDimitry Andric     return m_remote_platform_sp->SetRemoteWorkingDirectory(working_dir);
1730b57cec5SDimitry Andric   return Platform::SetRemoteWorkingDirectory(working_dir);
1740b57cec5SDimitry Andric }
1750b57cec5SDimitry Andric 
1760b57cec5SDimitry Andric Status RemoteAwarePlatform::GetFileWithUUID(const FileSpec &platform_file,
1770b57cec5SDimitry Andric                                             const UUID *uuid_ptr,
1780b57cec5SDimitry Andric                                             FileSpec &local_file) {
1790b57cec5SDimitry Andric   if (IsRemote() && m_remote_platform_sp)
1800b57cec5SDimitry Andric     return m_remote_platform_sp->GetFileWithUUID(platform_file, uuid_ptr,
1810b57cec5SDimitry Andric                                                  local_file);
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric   // Default to the local case
1840b57cec5SDimitry Andric   local_file = platform_file;
1850b57cec5SDimitry Andric   return Status();
1860b57cec5SDimitry Andric }
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric bool RemoteAwarePlatform::GetRemoteOSVersion() {
1890b57cec5SDimitry Andric   if (m_remote_platform_sp) {
1900b57cec5SDimitry Andric     m_os_version = m_remote_platform_sp->GetOSVersion();
1910b57cec5SDimitry Andric     return !m_os_version.empty();
1920b57cec5SDimitry Andric   }
1930b57cec5SDimitry Andric   return false;
1940b57cec5SDimitry Andric }
1950b57cec5SDimitry Andric 
196bdd1243dSDimitry Andric std::optional<std::string> RemoteAwarePlatform::GetRemoteOSBuildString() {
1970b57cec5SDimitry Andric   if (m_remote_platform_sp)
198349cc55cSDimitry Andric     return m_remote_platform_sp->GetRemoteOSBuildString();
199bdd1243dSDimitry Andric   return std::nullopt;
2000b57cec5SDimitry Andric }
2010b57cec5SDimitry Andric 
202bdd1243dSDimitry Andric std::optional<std::string> RemoteAwarePlatform::GetRemoteOSKernelDescription() {
2030b57cec5SDimitry Andric   if (m_remote_platform_sp)
204349cc55cSDimitry Andric     return m_remote_platform_sp->GetRemoteOSKernelDescription();
205bdd1243dSDimitry Andric   return std::nullopt;
2060b57cec5SDimitry Andric }
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric ArchSpec RemoteAwarePlatform::GetRemoteSystemArchitecture() {
2090b57cec5SDimitry Andric   if (m_remote_platform_sp)
2100b57cec5SDimitry Andric     return m_remote_platform_sp->GetRemoteSystemArchitecture();
2110b57cec5SDimitry Andric   return ArchSpec();
2120b57cec5SDimitry Andric }
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric const char *RemoteAwarePlatform::GetHostname() {
2150b57cec5SDimitry Andric   if (m_remote_platform_sp)
2160b57cec5SDimitry Andric     return m_remote_platform_sp->GetHostname();
21781ad6265SDimitry Andric   return Platform::GetHostname();
2180b57cec5SDimitry Andric }
2190b57cec5SDimitry Andric 
2200b57cec5SDimitry Andric UserIDResolver &RemoteAwarePlatform::GetUserIDResolver() {
2210b57cec5SDimitry Andric   if (m_remote_platform_sp)
2220b57cec5SDimitry Andric     return m_remote_platform_sp->GetUserIDResolver();
22381ad6265SDimitry Andric   return Platform::GetUserIDResolver();
2240b57cec5SDimitry Andric }
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric Environment RemoteAwarePlatform::GetEnvironment() {
2270b57cec5SDimitry Andric   if (m_remote_platform_sp)
2280b57cec5SDimitry Andric     return m_remote_platform_sp->GetEnvironment();
22981ad6265SDimitry Andric   return Platform::GetEnvironment();
2300b57cec5SDimitry Andric }
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric bool RemoteAwarePlatform::IsConnected() const {
23381ad6265SDimitry Andric   if (m_remote_platform_sp)
2340b57cec5SDimitry Andric     return m_remote_platform_sp->IsConnected();
23581ad6265SDimitry Andric   return Platform::IsConnected();
2360b57cec5SDimitry Andric }
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric bool RemoteAwarePlatform::GetProcessInfo(lldb::pid_t pid,
2390b57cec5SDimitry Andric                                          ProcessInstanceInfo &process_info) {
2400b57cec5SDimitry Andric   if (m_remote_platform_sp)
2410b57cec5SDimitry Andric     return m_remote_platform_sp->GetProcessInfo(pid, process_info);
24281ad6265SDimitry Andric   return Platform::GetProcessInfo(pid, process_info);
2430b57cec5SDimitry Andric }
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric uint32_t
2460b57cec5SDimitry Andric RemoteAwarePlatform::FindProcesses(const ProcessInstanceInfoMatch &match_info,
2470b57cec5SDimitry Andric                                    ProcessInstanceInfoList &process_infos) {
2480b57cec5SDimitry Andric   if (m_remote_platform_sp)
2490b57cec5SDimitry Andric     return m_remote_platform_sp->FindProcesses(match_info, process_infos);
25081ad6265SDimitry Andric   return Platform::FindProcesses(match_info, process_infos);
2510b57cec5SDimitry Andric }
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric lldb::ProcessSP RemoteAwarePlatform::ConnectProcess(llvm::StringRef connect_url,
2540b57cec5SDimitry Andric                                                     llvm::StringRef plugin_name,
2550b57cec5SDimitry Andric                                                     Debugger &debugger,
2560b57cec5SDimitry Andric                                                     Target *target,
2570b57cec5SDimitry Andric                                                     Status &error) {
2580b57cec5SDimitry Andric   if (m_remote_platform_sp)
2590b57cec5SDimitry Andric     return m_remote_platform_sp->ConnectProcess(connect_url, plugin_name,
2600b57cec5SDimitry Andric                                                 debugger, target, error);
2610b57cec5SDimitry Andric   return Platform::ConnectProcess(connect_url, plugin_name, debugger, target,
2620b57cec5SDimitry Andric                                   error);
2630b57cec5SDimitry Andric }
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric Status RemoteAwarePlatform::LaunchProcess(ProcessLaunchInfo &launch_info) {
2660b57cec5SDimitry Andric   if (m_remote_platform_sp)
26781ad6265SDimitry Andric     return m_remote_platform_sp->LaunchProcess(launch_info);
26881ad6265SDimitry Andric   return Platform::LaunchProcess(launch_info);
2690b57cec5SDimitry Andric }
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric Status RemoteAwarePlatform::KillProcess(const lldb::pid_t pid) {
2720b57cec5SDimitry Andric   if (m_remote_platform_sp)
2730b57cec5SDimitry Andric     return m_remote_platform_sp->KillProcess(pid);
27481ad6265SDimitry Andric   return Platform::KillProcess(pid);
2750b57cec5SDimitry Andric }
276fe6060f1SDimitry Andric 
277fe6060f1SDimitry Andric size_t RemoteAwarePlatform::ConnectToWaitingProcesses(Debugger &debugger,
278fe6060f1SDimitry Andric                                                 Status &error) {
279fe6060f1SDimitry Andric   if (m_remote_platform_sp)
280fe6060f1SDimitry Andric     return m_remote_platform_sp->ConnectToWaitingProcesses(debugger, error);
281fe6060f1SDimitry Andric   return Platform::ConnectToWaitingProcesses(debugger, error);
282fe6060f1SDimitry Andric }
283