xref: /freebsd/contrib/llvm-project/lldb/source/Target/RemoteAwarePlatform.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===-- RemoteAwarePlatform.cpp -------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/Target/RemoteAwarePlatform.h"
10 #include "lldb/Core/Module.h"
11 #include "lldb/Core/ModuleList.h"
12 #include "lldb/Core/ModuleSpec.h"
13 #include "lldb/Host/FileSystem.h"
14 #include "lldb/Host/Host.h"
15 #include "lldb/Host/HostInfo.h"
16 #include "lldb/Utility/StreamString.h"
17 #include <optional>
18 
19 using namespace lldb_private;
20 using namespace lldb;
21 
GetModuleSpec(const FileSpec & module_file_spec,const ArchSpec & arch,ModuleSpec & module_spec)22 bool RemoteAwarePlatform::GetModuleSpec(const FileSpec &module_file_spec,
23                                         const ArchSpec &arch,
24                                         ModuleSpec &module_spec) {
25   if (m_remote_platform_sp)
26     return m_remote_platform_sp->GetModuleSpec(module_file_spec, arch,
27                                                module_spec);
28 
29   return false;
30 }
31 
ResolveExecutable(const ModuleSpec & module_spec,lldb::ModuleSP & exe_module_sp,const FileSpecList * module_search_paths_ptr)32 Status RemoteAwarePlatform::ResolveExecutable(
33     const ModuleSpec &module_spec, lldb::ModuleSP &exe_module_sp,
34     const FileSpecList *module_search_paths_ptr) {
35   ModuleSpec resolved_module_spec(module_spec);
36 
37   // The host platform can resolve the path more aggressively.
38   if (IsHost()) {
39     FileSpec &resolved_file_spec = resolved_module_spec.GetFileSpec();
40 
41     if (!FileSystem::Instance().Exists(resolved_file_spec)) {
42       resolved_module_spec.GetFileSpec().SetFile(resolved_file_spec.GetPath(),
43                                                  FileSpec::Style::native);
44       FileSystem::Instance().Resolve(resolved_file_spec);
45     }
46 
47     if (!FileSystem::Instance().Exists(resolved_file_spec))
48       FileSystem::Instance().ResolveExecutableLocation(resolved_file_spec);
49   } else if (m_remote_platform_sp) {
50     return GetCachedExecutable(resolved_module_spec, exe_module_sp,
51                                module_search_paths_ptr);
52   }
53 
54   return Platform::ResolveExecutable(resolved_module_spec, exe_module_sp,
55                                      module_search_paths_ptr);
56 }
57 
RunShellCommand(llvm::StringRef command,const FileSpec & working_dir,int * status_ptr,int * signo_ptr,std::string * command_output,const Timeout<std::micro> & timeout)58 Status RemoteAwarePlatform::RunShellCommand(
59     llvm::StringRef command, const FileSpec &working_dir, int *status_ptr,
60     int *signo_ptr, std::string *command_output,
61     const Timeout<std::micro> &timeout) {
62   return RunShellCommand(llvm::StringRef(), command, working_dir, status_ptr,
63                          signo_ptr, command_output, timeout);
64 }
65 
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)66 Status RemoteAwarePlatform::RunShellCommand(
67     llvm::StringRef shell, llvm::StringRef command, const FileSpec &working_dir,
68     int *status_ptr, int *signo_ptr, std::string *command_output,
69     const Timeout<std::micro> &timeout) {
70   if (m_remote_platform_sp)
71     return m_remote_platform_sp->RunShellCommand(shell, command, working_dir,
72                                                  status_ptr, signo_ptr,
73                                                  command_output, timeout);
74   return Platform::RunShellCommand(shell, command, working_dir, status_ptr,
75                                    signo_ptr, command_output, timeout);
76 }
77 
MakeDirectory(const FileSpec & file_spec,uint32_t file_permissions)78 Status RemoteAwarePlatform::MakeDirectory(const FileSpec &file_spec,
79                                           uint32_t file_permissions) {
80   if (m_remote_platform_sp)
81     return m_remote_platform_sp->MakeDirectory(file_spec, file_permissions);
82   return Platform::MakeDirectory(file_spec, file_permissions);
83 }
84 
GetFilePermissions(const FileSpec & file_spec,uint32_t & file_permissions)85 Status RemoteAwarePlatform::GetFilePermissions(const FileSpec &file_spec,
86                                                uint32_t &file_permissions) {
87   if (m_remote_platform_sp)
88     return m_remote_platform_sp->GetFilePermissions(file_spec,
89                                                     file_permissions);
90   return Platform::GetFilePermissions(file_spec, file_permissions);
91 }
92 
SetFilePermissions(const FileSpec & file_spec,uint32_t file_permissions)93 Status RemoteAwarePlatform::SetFilePermissions(const FileSpec &file_spec,
94                                                uint32_t file_permissions) {
95   if (m_remote_platform_sp)
96     return m_remote_platform_sp->SetFilePermissions(file_spec,
97                                                     file_permissions);
98   return Platform::SetFilePermissions(file_spec, file_permissions);
99 }
100 
OpenFile(const FileSpec & file_spec,File::OpenOptions flags,uint32_t mode,Status & error)101 lldb::user_id_t RemoteAwarePlatform::OpenFile(const FileSpec &file_spec,
102                                               File::OpenOptions flags,
103                                               uint32_t mode, Status &error) {
104   if (m_remote_platform_sp)
105     return m_remote_platform_sp->OpenFile(file_spec, flags, mode, error);
106   return Platform::OpenFile(file_spec, flags, mode, error);
107 }
108 
CloseFile(lldb::user_id_t fd,Status & error)109 bool RemoteAwarePlatform::CloseFile(lldb::user_id_t fd, Status &error) {
110   if (m_remote_platform_sp)
111     return m_remote_platform_sp->CloseFile(fd, error);
112   return Platform::CloseFile(fd, error);
113 }
114 
ReadFile(lldb::user_id_t fd,uint64_t offset,void * dst,uint64_t dst_len,Status & error)115 uint64_t RemoteAwarePlatform::ReadFile(lldb::user_id_t fd, uint64_t offset,
116                                        void *dst, uint64_t dst_len,
117                                        Status &error) {
118   if (m_remote_platform_sp)
119     return m_remote_platform_sp->ReadFile(fd, offset, dst, dst_len, error);
120   return Platform::ReadFile(fd, offset, dst, dst_len, error);
121 }
122 
WriteFile(lldb::user_id_t fd,uint64_t offset,const void * src,uint64_t src_len,Status & error)123 uint64_t RemoteAwarePlatform::WriteFile(lldb::user_id_t fd, uint64_t offset,
124                                         const void *src, uint64_t src_len,
125                                         Status &error) {
126   if (m_remote_platform_sp)
127     return m_remote_platform_sp->WriteFile(fd, offset, src, src_len, error);
128   return Platform::WriteFile(fd, offset, src, src_len, error);
129 }
130 
GetFileSize(const FileSpec & file_spec)131 lldb::user_id_t RemoteAwarePlatform::GetFileSize(const FileSpec &file_spec) {
132   if (m_remote_platform_sp)
133     return m_remote_platform_sp->GetFileSize(file_spec);
134   return Platform::GetFileSize(file_spec);
135 }
136 
CreateSymlink(const FileSpec & src,const FileSpec & dst)137 Status RemoteAwarePlatform::CreateSymlink(const FileSpec &src,
138                                           const FileSpec &dst) {
139   if (m_remote_platform_sp)
140     return m_remote_platform_sp->CreateSymlink(src, dst);
141   return Platform::CreateSymlink(src, dst);
142 }
143 
GetFileExists(const FileSpec & file_spec)144 bool RemoteAwarePlatform::GetFileExists(const FileSpec &file_spec) {
145   if (m_remote_platform_sp)
146     return m_remote_platform_sp->GetFileExists(file_spec);
147   return Platform::GetFileExists(file_spec);
148 }
149 
Unlink(const FileSpec & file_spec)150 Status RemoteAwarePlatform::Unlink(const FileSpec &file_spec) {
151   if (m_remote_platform_sp)
152     return m_remote_platform_sp->Unlink(file_spec);
153   return Platform::Unlink(file_spec);
154 }
155 
156 llvm::ErrorOr<llvm::MD5::MD5Result>
CalculateMD5(const FileSpec & file_spec)157 RemoteAwarePlatform::CalculateMD5(const FileSpec &file_spec) {
158   if (m_remote_platform_sp)
159     return m_remote_platform_sp->CalculateMD5(file_spec);
160   return Platform::CalculateMD5(file_spec);
161 }
162 
GetRemoteWorkingDirectory()163 FileSpec RemoteAwarePlatform::GetRemoteWorkingDirectory() {
164   if (IsRemote() && m_remote_platform_sp)
165     return m_remote_platform_sp->GetRemoteWorkingDirectory();
166   return Platform::GetRemoteWorkingDirectory();
167 }
168 
SetRemoteWorkingDirectory(const FileSpec & working_dir)169 bool RemoteAwarePlatform::SetRemoteWorkingDirectory(
170     const FileSpec &working_dir) {
171   if (IsRemote() && m_remote_platform_sp)
172     return m_remote_platform_sp->SetRemoteWorkingDirectory(working_dir);
173   return Platform::SetRemoteWorkingDirectory(working_dir);
174 }
175 
GetFileWithUUID(const FileSpec & platform_file,const UUID * uuid_ptr,FileSpec & local_file)176 Status RemoteAwarePlatform::GetFileWithUUID(const FileSpec &platform_file,
177                                             const UUID *uuid_ptr,
178                                             FileSpec &local_file) {
179   if (IsRemote() && m_remote_platform_sp)
180     return m_remote_platform_sp->GetFileWithUUID(platform_file, uuid_ptr,
181                                                  local_file);
182 
183   // Default to the local case
184   local_file = platform_file;
185   return Status();
186 }
187 
GetRemoteOSVersion()188 bool RemoteAwarePlatform::GetRemoteOSVersion() {
189   if (m_remote_platform_sp) {
190     m_os_version = m_remote_platform_sp->GetOSVersion();
191     return !m_os_version.empty();
192   }
193   return false;
194 }
195 
GetRemoteOSBuildString()196 std::optional<std::string> RemoteAwarePlatform::GetRemoteOSBuildString() {
197   if (m_remote_platform_sp)
198     return m_remote_platform_sp->GetRemoteOSBuildString();
199   return std::nullopt;
200 }
201 
GetRemoteOSKernelDescription()202 std::optional<std::string> RemoteAwarePlatform::GetRemoteOSKernelDescription() {
203   if (m_remote_platform_sp)
204     return m_remote_platform_sp->GetRemoteOSKernelDescription();
205   return std::nullopt;
206 }
207 
GetRemoteSystemArchitecture()208 ArchSpec RemoteAwarePlatform::GetRemoteSystemArchitecture() {
209   if (m_remote_platform_sp)
210     return m_remote_platform_sp->GetRemoteSystemArchitecture();
211   return ArchSpec();
212 }
213 
GetHostname()214 const char *RemoteAwarePlatform::GetHostname() {
215   if (m_remote_platform_sp)
216     return m_remote_platform_sp->GetHostname();
217   return Platform::GetHostname();
218 }
219 
GetUserIDResolver()220 UserIDResolver &RemoteAwarePlatform::GetUserIDResolver() {
221   if (m_remote_platform_sp)
222     return m_remote_platform_sp->GetUserIDResolver();
223   return Platform::GetUserIDResolver();
224 }
225 
GetEnvironment()226 Environment RemoteAwarePlatform::GetEnvironment() {
227   if (m_remote_platform_sp)
228     return m_remote_platform_sp->GetEnvironment();
229   return Platform::GetEnvironment();
230 }
231 
IsConnected() const232 bool RemoteAwarePlatform::IsConnected() const {
233   if (m_remote_platform_sp)
234     return m_remote_platform_sp->IsConnected();
235   return Platform::IsConnected();
236 }
237 
GetProcessInfo(lldb::pid_t pid,ProcessInstanceInfo & process_info)238 bool RemoteAwarePlatform::GetProcessInfo(lldb::pid_t pid,
239                                          ProcessInstanceInfo &process_info) {
240   if (m_remote_platform_sp)
241     return m_remote_platform_sp->GetProcessInfo(pid, process_info);
242   return Platform::GetProcessInfo(pid, process_info);
243 }
244 
245 uint32_t
FindProcesses(const ProcessInstanceInfoMatch & match_info,ProcessInstanceInfoList & process_infos)246 RemoteAwarePlatform::FindProcesses(const ProcessInstanceInfoMatch &match_info,
247                                    ProcessInstanceInfoList &process_infos) {
248   if (m_remote_platform_sp)
249     return m_remote_platform_sp->FindProcesses(match_info, process_infos);
250   return Platform::FindProcesses(match_info, process_infos);
251 }
252 
ConnectProcess(llvm::StringRef connect_url,llvm::StringRef plugin_name,Debugger & debugger,Target * target,Status & error)253 lldb::ProcessSP RemoteAwarePlatform::ConnectProcess(llvm::StringRef connect_url,
254                                                     llvm::StringRef plugin_name,
255                                                     Debugger &debugger,
256                                                     Target *target,
257                                                     Status &error) {
258   if (m_remote_platform_sp)
259     return m_remote_platform_sp->ConnectProcess(connect_url, plugin_name,
260                                                 debugger, target, error);
261   return Platform::ConnectProcess(connect_url, plugin_name, debugger, target,
262                                   error);
263 }
264 
LaunchProcess(ProcessLaunchInfo & launch_info)265 Status RemoteAwarePlatform::LaunchProcess(ProcessLaunchInfo &launch_info) {
266   if (m_remote_platform_sp)
267     return m_remote_platform_sp->LaunchProcess(launch_info);
268   return Platform::LaunchProcess(launch_info);
269 }
270 
KillProcess(const lldb::pid_t pid)271 Status RemoteAwarePlatform::KillProcess(const lldb::pid_t pid) {
272   if (m_remote_platform_sp)
273     return m_remote_platform_sp->KillProcess(pid);
274   return Platform::KillProcess(pid);
275 }
276 
ConnectToWaitingProcesses(Debugger & debugger,Status & error)277 size_t RemoteAwarePlatform::ConnectToWaitingProcesses(Debugger &debugger,
278                                                 Status &error) {
279   if (m_remote_platform_sp)
280     return m_remote_platform_sp->ConnectToWaitingProcesses(debugger, error);
281   return Platform::ConnectToWaitingProcesses(debugger, error);
282 }
283