1 //===-- PlatformRemoteGDBServer.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 "PlatformRemoteGDBServer.h"
10 #include "lldb/Host/Config.h"
11 
12 #include "lldb/Breakpoint/BreakpointLocation.h"
13 #include "lldb/Core/Debugger.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/ModuleList.h"
16 #include "lldb/Core/ModuleSpec.h"
17 #include "lldb/Core/PluginManager.h"
18 #include "lldb/Core/StreamFile.h"
19 #include "lldb/Host/ConnectionFileDescriptor.h"
20 #include "lldb/Host/Host.h"
21 #include "lldb/Host/HostInfo.h"
22 #include "lldb/Host/PosixApi.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Utility/FileSpec.h"
26 #include "lldb/Utility/LLDBLog.h"
27 #include "lldb/Utility/Log.h"
28 #include "lldb/Utility/ProcessInfo.h"
29 #include "lldb/Utility/Status.h"
30 #include "lldb/Utility/StreamString.h"
31 #include "lldb/Utility/UriParser.h"
32 #include "llvm/Support/FormatAdapters.h"
33 
34 #include "Plugins/Process/Utility/GDBRemoteSignals.h"
35 #include "Plugins/Process/gdb-remote/ProcessGDBRemote.h"
36 #include <optional>
37 
38 using namespace lldb;
39 using namespace lldb_private;
40 using namespace lldb_private::platform_gdb_server;
41 
42 LLDB_PLUGIN_DEFINE_ADV(PlatformRemoteGDBServer, PlatformGDB)
43 
44 static bool g_initialized = false;
45 
Initialize()46 void PlatformRemoteGDBServer::Initialize() {
47   Platform::Initialize();
48 
49   if (!g_initialized) {
50     g_initialized = true;
51     PluginManager::RegisterPlugin(
52         PlatformRemoteGDBServer::GetPluginNameStatic(),
53         PlatformRemoteGDBServer::GetDescriptionStatic(),
54         PlatformRemoteGDBServer::CreateInstance);
55   }
56 }
57 
Terminate()58 void PlatformRemoteGDBServer::Terminate() {
59   if (g_initialized) {
60     g_initialized = false;
61     PluginManager::UnregisterPlugin(PlatformRemoteGDBServer::CreateInstance);
62   }
63 
64   Platform::Terminate();
65 }
66 
CreateInstance(bool force,const ArchSpec * arch)67 PlatformSP PlatformRemoteGDBServer::CreateInstance(bool force,
68                                                    const ArchSpec *arch) {
69   bool create = force;
70   if (!create) {
71     create = !arch->TripleVendorWasSpecified() && !arch->TripleOSWasSpecified();
72   }
73   if (create)
74     return PlatformSP(new PlatformRemoteGDBServer());
75   return PlatformSP();
76 }
77 
GetDescriptionStatic()78 llvm::StringRef PlatformRemoteGDBServer::GetDescriptionStatic() {
79   return "A platform that uses the GDB remote protocol as the communication "
80          "transport.";
81 }
82 
GetDescription()83 llvm::StringRef PlatformRemoteGDBServer::GetDescription() {
84   if (m_platform_description.empty()) {
85     if (IsConnected()) {
86       // Send the get description packet
87     }
88   }
89 
90   if (!m_platform_description.empty())
91     return m_platform_description.c_str();
92   return GetDescriptionStatic();
93 }
94 
GetModuleSpec(const FileSpec & module_file_spec,const ArchSpec & arch,ModuleSpec & module_spec)95 bool PlatformRemoteGDBServer::GetModuleSpec(const FileSpec &module_file_spec,
96                                             const ArchSpec &arch,
97                                             ModuleSpec &module_spec) {
98   Log *log = GetLog(LLDBLog::Platform);
99 
100   const auto module_path = module_file_spec.GetPath(false);
101 
102   if (!m_gdb_client_up ||
103       !m_gdb_client_up->GetModuleInfo(module_file_spec, arch, module_spec)) {
104     LLDB_LOGF(
105         log,
106         "PlatformRemoteGDBServer::%s - failed to get module info for %s:%s",
107         __FUNCTION__, module_path.c_str(),
108         arch.GetTriple().getTriple().c_str());
109     return false;
110   }
111 
112   if (log) {
113     StreamString stream;
114     module_spec.Dump(stream);
115     LLDB_LOGF(log,
116               "PlatformRemoteGDBServer::%s - got module info for (%s:%s) : %s",
117               __FUNCTION__, module_path.c_str(),
118               arch.GetTriple().getTriple().c_str(), stream.GetData());
119   }
120 
121   return true;
122 }
123 
GetFileWithUUID(const FileSpec & platform_file,const UUID * uuid_ptr,FileSpec & local_file)124 Status PlatformRemoteGDBServer::GetFileWithUUID(const FileSpec &platform_file,
125                                                 const UUID *uuid_ptr,
126                                                 FileSpec &local_file) {
127   // Default to the local case
128   local_file = platform_file;
129   return Status();
130 }
131 
132 /// Default Constructor
PlatformRemoteGDBServer()133 PlatformRemoteGDBServer::PlatformRemoteGDBServer()
134     : Platform(/*is_host=*/false) {}
135 
136 /// Destructor.
137 ///
138 /// The destructor is virtual since this class is designed to be
139 /// inherited from by the plug-in instance.
140 PlatformRemoteGDBServer::~PlatformRemoteGDBServer() = default;
141 
GetSoftwareBreakpointTrapOpcode(Target & target,BreakpointSite * bp_site)142 size_t PlatformRemoteGDBServer::GetSoftwareBreakpointTrapOpcode(
143     Target &target, BreakpointSite *bp_site) {
144   // This isn't needed if the z/Z packets are supported in the GDB remote
145   // server. But we might need a packet to detect this.
146   return 0;
147 }
148 
GetRemoteOSVersion()149 bool PlatformRemoteGDBServer::GetRemoteOSVersion() {
150   if (m_gdb_client_up)
151     m_os_version = m_gdb_client_up->GetOSVersion();
152   return !m_os_version.empty();
153 }
154 
GetRemoteOSBuildString()155 std::optional<std::string> PlatformRemoteGDBServer::GetRemoteOSBuildString() {
156   if (!m_gdb_client_up)
157     return std::nullopt;
158   return m_gdb_client_up->GetOSBuildString();
159 }
160 
161 std::optional<std::string>
GetRemoteOSKernelDescription()162 PlatformRemoteGDBServer::GetRemoteOSKernelDescription() {
163   if (!m_gdb_client_up)
164     return std::nullopt;
165   return m_gdb_client_up->GetOSKernelDescription();
166 }
167 
168 // Remote Platform subclasses need to override this function
GetRemoteSystemArchitecture()169 ArchSpec PlatformRemoteGDBServer::GetRemoteSystemArchitecture() {
170   if (!m_gdb_client_up)
171     return ArchSpec();
172   return m_gdb_client_up->GetSystemArchitecture();
173 }
174 
GetRemoteWorkingDirectory()175 FileSpec PlatformRemoteGDBServer::GetRemoteWorkingDirectory() {
176   if (IsConnected()) {
177     Log *log = GetLog(LLDBLog::Platform);
178     FileSpec working_dir;
179     if (m_gdb_client_up->GetWorkingDir(working_dir) && log)
180       LLDB_LOGF(log,
181                 "PlatformRemoteGDBServer::GetRemoteWorkingDirectory() -> '%s'",
182                 working_dir.GetPath().c_str());
183     return working_dir;
184   } else {
185     return Platform::GetRemoteWorkingDirectory();
186   }
187 }
188 
SetRemoteWorkingDirectory(const FileSpec & working_dir)189 bool PlatformRemoteGDBServer::SetRemoteWorkingDirectory(
190     const FileSpec &working_dir) {
191   if (IsConnected()) {
192     // Clear the working directory it case it doesn't get set correctly. This
193     // will for use to re-read it
194     Log *log = GetLog(LLDBLog::Platform);
195     LLDB_LOGF(log, "PlatformRemoteGDBServer::SetRemoteWorkingDirectory('%s')",
196               working_dir.GetPath().c_str());
197     return m_gdb_client_up->SetWorkingDir(working_dir) == 0;
198   } else
199     return Platform::SetRemoteWorkingDirectory(working_dir);
200 }
201 
IsConnected() const202 bool PlatformRemoteGDBServer::IsConnected() const {
203   if (m_gdb_client_up) {
204     assert(m_gdb_client_up->IsConnected());
205     return true;
206   }
207   return false;
208 }
209 
ConnectRemote(Args & args)210 Status PlatformRemoteGDBServer::ConnectRemote(Args &args) {
211   Status error;
212   if (IsConnected()) {
213     error.SetErrorStringWithFormat("the platform is already connected to '%s', "
214                                    "execute 'platform disconnect' to close the "
215                                    "current connection",
216                                    GetHostname());
217     return error;
218   }
219 
220   if (args.GetArgumentCount() != 1) {
221     error.SetErrorString(
222         "\"platform connect\" takes a single argument: <connect-url>");
223     return error;
224   }
225 
226   const char *url = args.GetArgumentAtIndex(0);
227   if (!url)
228     return Status("URL is null.");
229 
230   std::optional<URI> parsed_url = URI::Parse(url);
231   if (!parsed_url)
232     return Status("Invalid URL: %s", url);
233 
234   // We're going to reuse the hostname when we connect to the debugserver.
235   m_platform_scheme = parsed_url->scheme.str();
236   m_platform_hostname = parsed_url->hostname.str();
237 
238   auto client_up =
239       std::make_unique<process_gdb_remote::GDBRemoteCommunicationClient>();
240   client_up->SetPacketTimeout(
241       process_gdb_remote::ProcessGDBRemote::GetPacketTimeout());
242   client_up->SetConnection(std::make_unique<ConnectionFileDescriptor>());
243   client_up->Connect(url, &error);
244 
245   if (error.Fail())
246     return error;
247 
248   if (client_up->HandshakeWithServer(&error)) {
249     m_gdb_client_up = std::move(client_up);
250     m_gdb_client_up->GetHostInfo();
251     // If a working directory was set prior to connecting, send it down
252     // now.
253     if (m_working_dir)
254       m_gdb_client_up->SetWorkingDir(m_working_dir);
255 
256     m_supported_architectures.clear();
257     ArchSpec remote_arch = m_gdb_client_up->GetSystemArchitecture();
258     if (remote_arch) {
259       m_supported_architectures.push_back(remote_arch);
260       if (remote_arch.GetTriple().isArch64Bit())
261         m_supported_architectures.push_back(
262             ArchSpec(remote_arch.GetTriple().get32BitArchVariant()));
263     }
264   } else {
265     client_up->Disconnect();
266     if (error.Success())
267       error.SetErrorString("handshake failed");
268   }
269   return error;
270 }
271 
DisconnectRemote()272 Status PlatformRemoteGDBServer::DisconnectRemote() {
273   Status error;
274   m_gdb_client_up.reset();
275   m_remote_signals_sp.reset();
276   return error;
277 }
278 
GetHostname()279 const char *PlatformRemoteGDBServer::GetHostname() {
280   if (m_gdb_client_up)
281     m_gdb_client_up->GetHostname(m_hostname);
282   if (m_hostname.empty())
283     return nullptr;
284   return m_hostname.c_str();
285 }
286 
287 std::optional<std::string>
DoGetUserName(UserIDResolver::id_t uid)288 PlatformRemoteGDBServer::DoGetUserName(UserIDResolver::id_t uid) {
289   std::string name;
290   if (m_gdb_client_up && m_gdb_client_up->GetUserName(uid, name))
291     return std::move(name);
292   return std::nullopt;
293 }
294 
295 std::optional<std::string>
DoGetGroupName(UserIDResolver::id_t gid)296 PlatformRemoteGDBServer::DoGetGroupName(UserIDResolver::id_t gid) {
297   std::string name;
298   if (m_gdb_client_up && m_gdb_client_up->GetGroupName(gid, name))
299     return std::move(name);
300   return std::nullopt;
301 }
302 
FindProcesses(const ProcessInstanceInfoMatch & match_info,ProcessInstanceInfoList & process_infos)303 uint32_t PlatformRemoteGDBServer::FindProcesses(
304     const ProcessInstanceInfoMatch &match_info,
305     ProcessInstanceInfoList &process_infos) {
306   if (m_gdb_client_up)
307     return m_gdb_client_up->FindProcesses(match_info, process_infos);
308   return 0;
309 }
310 
GetProcessInfo(lldb::pid_t pid,ProcessInstanceInfo & process_info)311 bool PlatformRemoteGDBServer::GetProcessInfo(
312     lldb::pid_t pid, ProcessInstanceInfo &process_info) {
313   if (m_gdb_client_up)
314     return m_gdb_client_up->GetProcessInfo(pid, process_info);
315   return false;
316 }
317 
LaunchProcess(ProcessLaunchInfo & launch_info)318 Status PlatformRemoteGDBServer::LaunchProcess(ProcessLaunchInfo &launch_info) {
319   Log *log = GetLog(LLDBLog::Platform);
320   Status error;
321 
322   LLDB_LOGF(log, "PlatformRemoteGDBServer::%s() called", __FUNCTION__);
323 
324   if (!IsConnected())
325     return Status("Not connected.");
326   auto num_file_actions = launch_info.GetNumFileActions();
327   for (decltype(num_file_actions) i = 0; i < num_file_actions; ++i) {
328     const auto file_action = launch_info.GetFileActionAtIndex(i);
329     if (file_action->GetAction() != FileAction::eFileActionOpen)
330       continue;
331     switch (file_action->GetFD()) {
332     case STDIN_FILENO:
333       m_gdb_client_up->SetSTDIN(file_action->GetFileSpec());
334       break;
335     case STDOUT_FILENO:
336       m_gdb_client_up->SetSTDOUT(file_action->GetFileSpec());
337       break;
338     case STDERR_FILENO:
339       m_gdb_client_up->SetSTDERR(file_action->GetFileSpec());
340       break;
341     }
342   }
343 
344   m_gdb_client_up->SetDisableASLR(
345       launch_info.GetFlags().Test(eLaunchFlagDisableASLR));
346   m_gdb_client_up->SetDetachOnError(
347       launch_info.GetFlags().Test(eLaunchFlagDetachOnError));
348 
349   FileSpec working_dir = launch_info.GetWorkingDirectory();
350   if (working_dir) {
351     m_gdb_client_up->SetWorkingDir(working_dir);
352   }
353 
354   // Send the environment and the program + arguments after we connect
355   m_gdb_client_up->SendEnvironment(launch_info.GetEnvironment());
356 
357   ArchSpec arch_spec = launch_info.GetArchitecture();
358   const char *arch_triple = arch_spec.GetTriple().str().c_str();
359 
360   m_gdb_client_up->SendLaunchArchPacket(arch_triple);
361   LLDB_LOGF(
362       log,
363       "PlatformRemoteGDBServer::%s() set launch architecture triple to '%s'",
364       __FUNCTION__, arch_triple ? arch_triple : "<NULL>");
365 
366   {
367     // Scope for the scoped timeout object
368     process_gdb_remote::GDBRemoteCommunication::ScopedTimeout timeout(
369         *m_gdb_client_up, std::chrono::seconds(5));
370     // Since we can't send argv0 separate from the executable path, we need to
371     // make sure to use the actual executable path found in the launch_info...
372     Args args = launch_info.GetArguments();
373     if (FileSpec exe_file = launch_info.GetExecutableFile())
374       args.ReplaceArgumentAtIndex(0, exe_file.GetPath(false));
375     if (llvm::Error err = m_gdb_client_up->LaunchProcess(args)) {
376       error.SetErrorStringWithFormatv("Cannot launch '{0}': {1}",
377                                       args.GetArgumentAtIndex(0),
378                                       llvm::fmt_consume(std::move(err)));
379       return error;
380     }
381   }
382 
383   const auto pid = m_gdb_client_up->GetCurrentProcessID(false);
384   if (pid != LLDB_INVALID_PROCESS_ID) {
385     launch_info.SetProcessID(pid);
386     LLDB_LOGF(log,
387               "PlatformRemoteGDBServer::%s() pid %" PRIu64
388               " launched successfully",
389               __FUNCTION__, pid);
390   } else {
391     LLDB_LOGF(log,
392               "PlatformRemoteGDBServer::%s() launch succeeded but we "
393               "didn't get a valid process id back!",
394               __FUNCTION__);
395     error.SetErrorString("failed to get PID");
396   }
397   return error;
398 }
399 
KillProcess(const lldb::pid_t pid)400 Status PlatformRemoteGDBServer::KillProcess(const lldb::pid_t pid) {
401   if (!KillSpawnedProcess(pid))
402     return Status("failed to kill remote spawned process");
403   return Status();
404 }
405 
406 lldb::ProcessSP
DebugProcess(ProcessLaunchInfo & launch_info,Debugger & debugger,Target & target,Status & error)407 PlatformRemoteGDBServer::DebugProcess(ProcessLaunchInfo &launch_info,
408                                       Debugger &debugger, Target &target,
409                                       Status &error) {
410   lldb::ProcessSP process_sp;
411   if (IsRemote()) {
412     if (IsConnected()) {
413       lldb::pid_t debugserver_pid = LLDB_INVALID_PROCESS_ID;
414       std::string connect_url;
415       if (!LaunchGDBServer(debugserver_pid, connect_url)) {
416         error.SetErrorStringWithFormat("unable to launch a GDB server on '%s'",
417                                        GetHostname());
418       } else {
419         // The darwin always currently uses the GDB remote debugger plug-in
420         // so even when debugging locally we are debugging remotely!
421         process_sp = target.CreateProcess(launch_info.GetListener(),
422                                           "gdb-remote", nullptr, true);
423 
424         if (process_sp) {
425           process_sp->HijackProcessEvents(launch_info.GetHijackListener());
426 
427           error = process_sp->ConnectRemote(connect_url.c_str());
428           // Retry the connect remote one time...
429           if (error.Fail())
430             error = process_sp->ConnectRemote(connect_url.c_str());
431           if (error.Success())
432             error = process_sp->Launch(launch_info);
433           else if (debugserver_pid != LLDB_INVALID_PROCESS_ID) {
434             printf("error: connect remote failed (%s)\n", error.AsCString());
435             KillSpawnedProcess(debugserver_pid);
436           }
437         }
438       }
439     } else {
440       error.SetErrorString("not connected to remote gdb server");
441     }
442   }
443   return process_sp;
444 }
445 
LaunchGDBServer(lldb::pid_t & pid,std::string & connect_url)446 bool PlatformRemoteGDBServer::LaunchGDBServer(lldb::pid_t &pid,
447                                               std::string &connect_url) {
448   assert(IsConnected());
449 
450   ArchSpec remote_arch = GetRemoteSystemArchitecture();
451   llvm::Triple &remote_triple = remote_arch.GetTriple();
452 
453   uint16_t port = 0;
454   std::string socket_name;
455   bool launch_result = false;
456   if (remote_triple.getVendor() == llvm::Triple::Apple &&
457       remote_triple.getOS() == llvm::Triple::IOS) {
458     // When remote debugging to iOS, we use a USB mux that always talks to
459     // localhost, so we will need the remote debugserver to accept connections
460     // only from localhost, no matter what our current hostname is
461     launch_result =
462         m_gdb_client_up->LaunchGDBServer("127.0.0.1", pid, port, socket_name);
463   } else {
464     // All other hosts should use their actual hostname
465     launch_result =
466         m_gdb_client_up->LaunchGDBServer(nullptr, pid, port, socket_name);
467   }
468 
469   if (!launch_result)
470     return false;
471 
472   connect_url =
473       MakeGdbServerUrl(m_platform_scheme, m_platform_hostname, port,
474                        (socket_name.empty()) ? nullptr : socket_name.c_str());
475   return true;
476 }
477 
KillSpawnedProcess(lldb::pid_t pid)478 bool PlatformRemoteGDBServer::KillSpawnedProcess(lldb::pid_t pid) {
479   assert(IsConnected());
480   return m_gdb_client_up->KillSpawnedProcess(pid);
481 }
482 
Attach(ProcessAttachInfo & attach_info,Debugger & debugger,Target * target,Status & error)483 lldb::ProcessSP PlatformRemoteGDBServer::Attach(
484     ProcessAttachInfo &attach_info, Debugger &debugger,
485     Target *target, // Can be NULL, if NULL create a new target, else use
486                     // existing one
487     Status &error) {
488   lldb::ProcessSP process_sp;
489   if (IsRemote()) {
490     if (IsConnected()) {
491       lldb::pid_t debugserver_pid = LLDB_INVALID_PROCESS_ID;
492       std::string connect_url;
493       if (!LaunchGDBServer(debugserver_pid, connect_url)) {
494         error.SetErrorStringWithFormat("unable to launch a GDB server on '%s'",
495                                        GetHostname());
496       } else {
497         if (target == nullptr) {
498           TargetSP new_target_sp;
499 
500           error = debugger.GetTargetList().CreateTarget(
501               debugger, "", "", eLoadDependentsNo, nullptr, new_target_sp);
502           target = new_target_sp.get();
503         } else
504           error.Clear();
505 
506         if (target && error.Success()) {
507           // The darwin always currently uses the GDB remote debugger plug-in
508           // so even when debugging locally we are debugging remotely!
509           process_sp =
510               target->CreateProcess(attach_info.GetListenerForProcess(debugger),
511                                     "gdb-remote", nullptr, true);
512           if (process_sp) {
513             error = process_sp->ConnectRemote(connect_url.c_str());
514             if (error.Success()) {
515               ListenerSP listener_sp = attach_info.GetHijackListener();
516               if (listener_sp)
517                 process_sp->HijackProcessEvents(listener_sp);
518               error = process_sp->Attach(attach_info);
519             }
520 
521             if (error.Fail() && debugserver_pid != LLDB_INVALID_PROCESS_ID) {
522               KillSpawnedProcess(debugserver_pid);
523             }
524           }
525         }
526       }
527     } else {
528       error.SetErrorString("not connected to remote gdb server");
529     }
530   }
531   return process_sp;
532 }
533 
MakeDirectory(const FileSpec & file_spec,uint32_t mode)534 Status PlatformRemoteGDBServer::MakeDirectory(const FileSpec &file_spec,
535                                               uint32_t mode) {
536   if (!IsConnected())
537     return Status("Not connected.");
538   Status error = m_gdb_client_up->MakeDirectory(file_spec, mode);
539   Log *log = GetLog(LLDBLog::Platform);
540   LLDB_LOGF(log,
541             "PlatformRemoteGDBServer::MakeDirectory(path='%s', mode=%o) "
542             "error = %u (%s)",
543             file_spec.GetPath().c_str(), mode, error.GetError(),
544             error.AsCString());
545   return error;
546 }
547 
GetFilePermissions(const FileSpec & file_spec,uint32_t & file_permissions)548 Status PlatformRemoteGDBServer::GetFilePermissions(const FileSpec &file_spec,
549                                                    uint32_t &file_permissions) {
550   if (!IsConnected())
551     return Status("Not connected.");
552   Status error =
553       m_gdb_client_up->GetFilePermissions(file_spec, file_permissions);
554   Log *log = GetLog(LLDBLog::Platform);
555   LLDB_LOGF(log,
556             "PlatformRemoteGDBServer::GetFilePermissions(path='%s', "
557             "file_permissions=%o) error = %u (%s)",
558             file_spec.GetPath().c_str(), file_permissions, error.GetError(),
559             error.AsCString());
560   return error;
561 }
562 
SetFilePermissions(const FileSpec & file_spec,uint32_t file_permissions)563 Status PlatformRemoteGDBServer::SetFilePermissions(const FileSpec &file_spec,
564                                                    uint32_t file_permissions) {
565   if (!IsConnected())
566     return Status("Not connected.");
567   Status error =
568       m_gdb_client_up->SetFilePermissions(file_spec, file_permissions);
569   Log *log = GetLog(LLDBLog::Platform);
570   LLDB_LOGF(log,
571             "PlatformRemoteGDBServer::SetFilePermissions(path='%s', "
572             "file_permissions=%o) error = %u (%s)",
573             file_spec.GetPath().c_str(), file_permissions, error.GetError(),
574             error.AsCString());
575   return error;
576 }
577 
OpenFile(const FileSpec & file_spec,File::OpenOptions flags,uint32_t mode,Status & error)578 lldb::user_id_t PlatformRemoteGDBServer::OpenFile(const FileSpec &file_spec,
579                                                   File::OpenOptions flags,
580                                                   uint32_t mode,
581                                                   Status &error) {
582   if (IsConnected())
583     return m_gdb_client_up->OpenFile(file_spec, flags, mode, error);
584   return LLDB_INVALID_UID;
585 }
586 
CloseFile(lldb::user_id_t fd,Status & error)587 bool PlatformRemoteGDBServer::CloseFile(lldb::user_id_t fd, Status &error) {
588   if (IsConnected())
589     return m_gdb_client_up->CloseFile(fd, error);
590   error = Status("Not connected.");
591   return false;
592 }
593 
594 lldb::user_id_t
GetFileSize(const FileSpec & file_spec)595 PlatformRemoteGDBServer::GetFileSize(const FileSpec &file_spec) {
596   if (IsConnected())
597     return m_gdb_client_up->GetFileSize(file_spec);
598   return LLDB_INVALID_UID;
599 }
600 
AutoCompleteDiskFileOrDirectory(CompletionRequest & request,bool only_dir)601 void PlatformRemoteGDBServer::AutoCompleteDiskFileOrDirectory(
602     CompletionRequest &request, bool only_dir) {
603   if (IsConnected())
604     m_gdb_client_up->AutoCompleteDiskFileOrDirectory(request, only_dir);
605 }
606 
ReadFile(lldb::user_id_t fd,uint64_t offset,void * dst,uint64_t dst_len,Status & error)607 uint64_t PlatformRemoteGDBServer::ReadFile(lldb::user_id_t fd, uint64_t offset,
608                                            void *dst, uint64_t dst_len,
609                                            Status &error) {
610   if (IsConnected())
611     return m_gdb_client_up->ReadFile(fd, offset, dst, dst_len, error);
612   error = Status("Not connected.");
613   return 0;
614 }
615 
WriteFile(lldb::user_id_t fd,uint64_t offset,const void * src,uint64_t src_len,Status & error)616 uint64_t PlatformRemoteGDBServer::WriteFile(lldb::user_id_t fd, uint64_t offset,
617                                             const void *src, uint64_t src_len,
618                                             Status &error) {
619   if (IsConnected())
620     return m_gdb_client_up->WriteFile(fd, offset, src, src_len, error);
621   error = Status("Not connected.");
622   return 0;
623 }
624 
PutFile(const FileSpec & source,const FileSpec & destination,uint32_t uid,uint32_t gid)625 Status PlatformRemoteGDBServer::PutFile(const FileSpec &source,
626                                         const FileSpec &destination,
627                                         uint32_t uid, uint32_t gid) {
628   return Platform::PutFile(source, destination, uid, gid);
629 }
630 
CreateSymlink(const FileSpec & src,const FileSpec & dst)631 Status PlatformRemoteGDBServer::CreateSymlink(
632     const FileSpec &src, // The name of the link is in src
633     const FileSpec &dst) // The symlink points to dst
634 {
635   if (!IsConnected())
636     return Status("Not connected.");
637   Status error = m_gdb_client_up->CreateSymlink(src, dst);
638   Log *log = GetLog(LLDBLog::Platform);
639   LLDB_LOGF(log,
640             "PlatformRemoteGDBServer::CreateSymlink(src='%s', dst='%s') "
641             "error = %u (%s)",
642             src.GetPath().c_str(), dst.GetPath().c_str(), error.GetError(),
643             error.AsCString());
644   return error;
645 }
646 
Unlink(const FileSpec & file_spec)647 Status PlatformRemoteGDBServer::Unlink(const FileSpec &file_spec) {
648   if (!IsConnected())
649     return Status("Not connected.");
650   Status error = m_gdb_client_up->Unlink(file_spec);
651   Log *log = GetLog(LLDBLog::Platform);
652   LLDB_LOGF(log, "PlatformRemoteGDBServer::Unlink(path='%s') error = %u (%s)",
653             file_spec.GetPath().c_str(), error.GetError(), error.AsCString());
654   return error;
655 }
656 
GetFileExists(const FileSpec & file_spec)657 bool PlatformRemoteGDBServer::GetFileExists(const FileSpec &file_spec) {
658   if (IsConnected())
659     return m_gdb_client_up->GetFileExists(file_spec);
660   return false;
661 }
662 
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)663 Status PlatformRemoteGDBServer::RunShellCommand(
664     llvm::StringRef shell, llvm::StringRef command,
665     const FileSpec &
666         working_dir, // Pass empty FileSpec to use the current working directory
667     int *status_ptr, // Pass NULL if you don't want the process exit status
668     int *signo_ptr,  // Pass NULL if you don't want the signal that caused the
669                      // process to exit
670     std::string
671         *command_output, // Pass NULL if you don't want the command output
672     const Timeout<std::micro> &timeout) {
673   if (!IsConnected())
674     return Status("Not connected.");
675   return m_gdb_client_up->RunShellCommand(command, working_dir, status_ptr,
676                                           signo_ptr, command_output, timeout);
677 }
678 
CalculateTrapHandlerSymbolNames()679 void PlatformRemoteGDBServer::CalculateTrapHandlerSymbolNames() {
680   m_trap_handlers.push_back(ConstString("_sigtramp"));
681 }
682 
GetRemoteUnixSignals()683 const UnixSignalsSP &PlatformRemoteGDBServer::GetRemoteUnixSignals() {
684   if (!IsConnected())
685     return Platform::GetRemoteUnixSignals();
686 
687   if (m_remote_signals_sp)
688     return m_remote_signals_sp;
689 
690   // If packet not implemented or JSON failed to parse, we'll guess the signal
691   // set based on the remote architecture.
692   m_remote_signals_sp = UnixSignals::Create(GetRemoteSystemArchitecture());
693 
694   StringExtractorGDBRemote response;
695   auto result =
696       m_gdb_client_up->SendPacketAndWaitForResponse("jSignalsInfo", response);
697 
698   if (result != decltype(result)::Success ||
699       response.GetResponseType() != response.eResponse)
700     return m_remote_signals_sp;
701 
702   auto object_sp =
703       StructuredData::ParseJSON(std::string(response.GetStringRef()));
704   if (!object_sp || !object_sp->IsValid())
705     return m_remote_signals_sp;
706 
707   auto array_sp = object_sp->GetAsArray();
708   if (!array_sp || !array_sp->IsValid())
709     return m_remote_signals_sp;
710 
711   auto remote_signals_sp = std::make_shared<lldb_private::GDBRemoteSignals>();
712 
713   bool done = array_sp->ForEach(
714       [&remote_signals_sp](StructuredData::Object *object) -> bool {
715         if (!object || !object->IsValid())
716           return false;
717 
718         auto dict = object->GetAsDictionary();
719         if (!dict || !dict->IsValid())
720           return false;
721 
722         // Signal number and signal name are required.
723         int signo;
724         if (!dict->GetValueForKeyAsInteger("signo", signo))
725           return false;
726 
727         llvm::StringRef name;
728         if (!dict->GetValueForKeyAsString("name", name))
729           return false;
730 
731         // We can live without short_name, description, etc.
732         bool suppress{false};
733         auto object_sp = dict->GetValueForKey("suppress");
734         if (object_sp && object_sp->IsValid())
735           suppress = object_sp->GetBooleanValue();
736 
737         bool stop{false};
738         object_sp = dict->GetValueForKey("stop");
739         if (object_sp && object_sp->IsValid())
740           stop = object_sp->GetBooleanValue();
741 
742         bool notify{false};
743         object_sp = dict->GetValueForKey("notify");
744         if (object_sp && object_sp->IsValid())
745           notify = object_sp->GetBooleanValue();
746 
747         std::string description;
748         object_sp = dict->GetValueForKey("description");
749         if (object_sp && object_sp->IsValid())
750           description = std::string(object_sp->GetStringValue());
751 
752         remote_signals_sp->AddSignal(signo, name.str().c_str(), suppress, stop,
753                                      notify, description.c_str());
754         return true;
755       });
756 
757   if (done)
758     m_remote_signals_sp = std::move(remote_signals_sp);
759 
760   return m_remote_signals_sp;
761 }
762 
MakeGdbServerUrl(const std::string & platform_scheme,const std::string & platform_hostname,uint16_t port,const char * socket_name)763 std::string PlatformRemoteGDBServer::MakeGdbServerUrl(
764     const std::string &platform_scheme, const std::string &platform_hostname,
765     uint16_t port, const char *socket_name) {
766   const char *override_scheme =
767       getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_SCHEME");
768   const char *override_hostname =
769       getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_HOSTNAME");
770   const char *port_offset_c_str =
771       getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_PORT_OFFSET");
772   int port_offset = port_offset_c_str ? ::atoi(port_offset_c_str) : 0;
773 
774   return MakeUrl(override_scheme ? override_scheme : platform_scheme.c_str(),
775                  override_hostname ? override_hostname
776                                    : platform_hostname.c_str(),
777                  port + port_offset, socket_name);
778 }
779 
MakeUrl(const char * scheme,const char * hostname,uint16_t port,const char * path)780 std::string PlatformRemoteGDBServer::MakeUrl(const char *scheme,
781                                              const char *hostname,
782                                              uint16_t port, const char *path) {
783   StreamString result;
784   result.Printf("%s://[%s]", scheme, hostname);
785   if (port != 0)
786     result.Printf(":%u", port);
787   if (path)
788     result.Write(path, strlen(path));
789   return std::string(result.GetString());
790 }
791 
ConnectToWaitingProcesses(Debugger & debugger,Status & error)792 size_t PlatformRemoteGDBServer::ConnectToWaitingProcesses(Debugger &debugger,
793                                                           Status &error) {
794   std::vector<std::string> connection_urls;
795   GetPendingGdbServerList(connection_urls);
796 
797   for (size_t i = 0; i < connection_urls.size(); ++i) {
798     ConnectProcess(connection_urls[i].c_str(), "gdb-remote", debugger, nullptr, error);
799     if (error.Fail())
800       return i; // We already connected to i process successfully
801   }
802   return connection_urls.size();
803 }
804 
GetPendingGdbServerList(std::vector<std::string> & connection_urls)805 size_t PlatformRemoteGDBServer::GetPendingGdbServerList(
806     std::vector<std::string> &connection_urls) {
807   std::vector<std::pair<uint16_t, std::string>> remote_servers;
808   if (!IsConnected())
809     return 0;
810   m_gdb_client_up->QueryGDBServer(remote_servers);
811   for (const auto &gdbserver : remote_servers) {
812     const char *socket_name_cstr =
813         gdbserver.second.empty() ? nullptr : gdbserver.second.c_str();
814     connection_urls.emplace_back(
815         MakeGdbServerUrl(m_platform_scheme, m_platform_hostname,
816                          gdbserver.first, socket_name_cstr));
817   }
818   return connection_urls.size();
819 }
820