1 //===-- PlatformRemoteGDBServer.cpp -----------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/lldb-python.h"
11 
12 #include "PlatformRemoteGDBServer.h"
13 #include "lldb/Host/Config.h"
14 
15 // C++ Includes
16 // Other libraries and framework includes
17 // Project includes
18 #include "lldb/Breakpoint/BreakpointLocation.h"
19 #include "lldb/Core/Debugger.h"
20 #include "lldb/Core/Error.h"
21 #include "lldb/Core/Log.h"
22 #include "lldb/Core/Module.h"
23 #include "lldb/Core/ModuleList.h"
24 #include "lldb/Core/ModuleSpec.h"
25 #include "lldb/Core/PluginManager.h"
26 #include "lldb/Core/StreamString.h"
27 #include "lldb/Host/ConnectionFileDescriptor.h"
28 #include "lldb/Host/FileSpec.h"
29 #include "lldb/Host/Host.h"
30 #include "lldb/Target/Process.h"
31 #include "lldb/Target/Target.h"
32 
33 #include "Utility/UriParser.h"
34 
35 using namespace lldb;
36 using namespace lldb_private;
37 
38 static bool g_initialized = false;
39 
40 void
41 PlatformRemoteGDBServer::Initialize ()
42 {
43     if (g_initialized == false)
44     {
45         g_initialized = true;
46         PluginManager::RegisterPlugin (PlatformRemoteGDBServer::GetPluginNameStatic(),
47                                        PlatformRemoteGDBServer::GetDescriptionStatic(),
48                                        PlatformRemoteGDBServer::CreateInstance);
49     }
50 }
51 
52 void
53 PlatformRemoteGDBServer::Terminate ()
54 {
55     if (g_initialized)
56     {
57         g_initialized = false;
58         PluginManager::UnregisterPlugin (PlatformRemoteGDBServer::CreateInstance);
59     }
60 }
61 
62 PlatformSP
63 PlatformRemoteGDBServer::CreateInstance (bool force, const lldb_private::ArchSpec *arch)
64 {
65     bool create = force;
66     if (!create)
67     {
68         create = !arch->TripleVendorWasSpecified() && !arch->TripleOSWasSpecified();
69     }
70     if (create)
71         return PlatformSP(new PlatformRemoteGDBServer());
72     return PlatformSP();
73 }
74 
75 
76 lldb_private::ConstString
77 PlatformRemoteGDBServer::GetPluginNameStatic()
78 {
79     static ConstString g_name("remote-gdb-server");
80     return g_name;
81 }
82 
83 const char *
84 PlatformRemoteGDBServer::GetDescriptionStatic()
85 {
86     return "A platform that uses the GDB remote protocol as the communication transport.";
87 }
88 
89 const char *
90 PlatformRemoteGDBServer::GetDescription ()
91 {
92     if (m_platform_description.empty())
93     {
94         if (IsConnected())
95         {
96             // Send the get description packet
97         }
98     }
99 
100     if (!m_platform_description.empty())
101         return m_platform_description.c_str();
102     return GetDescriptionStatic();
103 }
104 
105 Error
106 PlatformRemoteGDBServer::ResolveExecutable (const ModuleSpec &module_spec,
107                                             lldb::ModuleSP &exe_module_sp,
108                                             const FileSpecList *module_search_paths_ptr)
109 {
110     // copied from PlatformRemoteiOS
111 
112     Error error;
113     // Nothing special to do here, just use the actual file and architecture
114 
115     ModuleSpec resolved_module_spec(module_spec);
116 
117     // Resolve any executable within an apk on Android?
118     //Host::ResolveExecutableInBundle (resolved_module_spec.GetFileSpec());
119 
120     if (resolved_module_spec.GetFileSpec().Exists())
121     {
122         if (resolved_module_spec.GetArchitecture().IsValid() || resolved_module_spec.GetUUID().IsValid())
123         {
124             error = ModuleList::GetSharedModule (resolved_module_spec,
125                                                  exe_module_sp,
126                                                  NULL,
127                                                  NULL,
128                                                  NULL);
129 
130             if (exe_module_sp && exe_module_sp->GetObjectFile())
131                 return error;
132             exe_module_sp.reset();
133         }
134         // No valid architecture was specified or the exact arch wasn't
135         // found so ask the platform for the architectures that we should be
136         // using (in the correct order) and see if we can find a match that way
137         StreamString arch_names;
138         for (uint32_t idx = 0; GetSupportedArchitectureAtIndex (idx, resolved_module_spec.GetArchitecture()); ++idx)
139         {
140             error = ModuleList::GetSharedModule (resolved_module_spec,
141                                                  exe_module_sp,
142                                                  NULL,
143                                                  NULL,
144                                                  NULL);
145             // Did we find an executable using one of the
146             if (error.Success())
147             {
148                 if (exe_module_sp && exe_module_sp->GetObjectFile())
149                     break;
150                 else
151                     error.SetErrorToGenericError();
152             }
153 
154             if (idx > 0)
155                 arch_names.PutCString (", ");
156             arch_names.PutCString (resolved_module_spec.GetArchitecture().GetArchitectureName());
157         }
158 
159         if (error.Fail() || !exe_module_sp)
160         {
161             if (resolved_module_spec.GetFileSpec().Readable())
162             {
163                 error.SetErrorStringWithFormat ("'%s' doesn't contain any '%s' platform architectures: %s",
164                                                 resolved_module_spec.GetFileSpec().GetPath().c_str(),
165                                                 GetPluginName().GetCString(),
166                                                 arch_names.GetString().c_str());
167             }
168             else
169             {
170                 error.SetErrorStringWithFormat("'%s' is not readable", resolved_module_spec.GetFileSpec().GetPath().c_str());
171             }
172         }
173     }
174     else
175     {
176         error.SetErrorStringWithFormat ("'%s' does not exist",
177                                         resolved_module_spec.GetFileSpec().GetPath().c_str());
178     }
179 
180     return error;
181 }
182 
183 Error
184 PlatformRemoteGDBServer::GetFileWithUUID (const FileSpec &platform_file,
185                                           const UUID *uuid_ptr,
186                                           FileSpec &local_file)
187 {
188     // Default to the local case
189     local_file = platform_file;
190     return Error();
191 }
192 
193 //------------------------------------------------------------------
194 /// Default Constructor
195 //------------------------------------------------------------------
196 PlatformRemoteGDBServer::PlatformRemoteGDBServer () :
197     Platform(false), // This is a remote platform
198     m_gdb_client(true)
199 {
200 }
201 
202 //------------------------------------------------------------------
203 /// Destructor.
204 ///
205 /// The destructor is virtual since this class is designed to be
206 /// inherited from by the plug-in instance.
207 //------------------------------------------------------------------
208 PlatformRemoteGDBServer::~PlatformRemoteGDBServer()
209 {
210 }
211 
212 bool
213 PlatformRemoteGDBServer::GetSupportedArchitectureAtIndex (uint32_t idx, ArchSpec &arch)
214 {
215     ArchSpec remote_arch = m_gdb_client.GetSystemArchitecture();
216 
217     // TODO: 64 bit systems should also advertize support for 32 bit arch
218     // unknown CPU, we just support the one arch
219     if (idx == 0)
220     {
221         arch = remote_arch;
222         return true;
223     }
224     return false;
225 }
226 
227 size_t
228 PlatformRemoteGDBServer::GetSoftwareBreakpointTrapOpcode (Target &target, BreakpointSite *bp_site)
229 {
230     // This isn't needed if the z/Z packets are supported in the GDB remote
231     // server. But we might need a packet to detect this.
232     return 0;
233 }
234 
235 bool
236 PlatformRemoteGDBServer::GetRemoteOSVersion ()
237 {
238     uint32_t major, minor, update;
239     if (m_gdb_client.GetOSVersion (major, minor, update))
240     {
241         m_major_os_version = major;
242         m_minor_os_version = minor;
243         m_update_os_version = update;
244         return true;
245     }
246     return false;
247 }
248 
249 bool
250 PlatformRemoteGDBServer::GetRemoteOSBuildString (std::string &s)
251 {
252     return m_gdb_client.GetOSBuildString (s);
253 }
254 
255 bool
256 PlatformRemoteGDBServer::GetRemoteOSKernelDescription (std::string &s)
257 {
258     return m_gdb_client.GetOSKernelDescription (s);
259 }
260 
261 // Remote Platform subclasses need to override this function
262 ArchSpec
263 PlatformRemoteGDBServer::GetRemoteSystemArchitecture ()
264 {
265     return m_gdb_client.GetSystemArchitecture();
266 }
267 
268 lldb_private::ConstString
269 PlatformRemoteGDBServer::GetRemoteWorkingDirectory()
270 {
271     if (IsConnected())
272     {
273         Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
274         std::string cwd;
275         if (m_gdb_client.GetWorkingDir(cwd))
276         {
277             ConstString working_dir(cwd.c_str());
278             if (log)
279                 log->Printf("PlatformRemoteGDBServer::GetRemoteWorkingDirectory() -> '%s'", working_dir.GetCString());
280             return working_dir;
281         }
282         else
283         {
284             return ConstString();
285         }
286     }
287     else
288     {
289         return Platform::GetRemoteWorkingDirectory();
290     }
291 }
292 
293 bool
294 PlatformRemoteGDBServer::SetRemoteWorkingDirectory(const lldb_private::ConstString &path)
295 {
296     if (IsConnected())
297     {
298         // Clear the working directory it case it doesn't get set correctly. This will
299         // for use to re-read it
300         Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
301         if (log)
302             log->Printf("PlatformRemoteGDBServer::SetRemoteWorkingDirectory('%s')", path.GetCString());
303         return m_gdb_client.SetWorkingDir(path.GetCString()) == 0;
304     }
305     else
306         return Platform::SetRemoteWorkingDirectory(path);
307 }
308 
309 bool
310 PlatformRemoteGDBServer::IsConnected () const
311 {
312     return m_gdb_client.IsConnected();
313 }
314 
315 Error
316 PlatformRemoteGDBServer::ConnectRemote (Args& args)
317 {
318     Error error;
319     if (IsConnected())
320     {
321         error.SetErrorStringWithFormat ("the platform is already connected to '%s', execute 'platform disconnect' to close the current connection",
322                                         GetHostname());
323     }
324     else
325     {
326         if (args.GetArgumentCount() == 1)
327         {
328             const char *url = args.GetArgumentAtIndex(0);
329             m_gdb_client.SetConnection (new ConnectionFileDescriptor());
330 
331             // we're going to reuse the hostname when we connect to the debugserver
332             std::string scheme;
333             int port;
334             std::string path;
335             if ( !UriParser::Parse(url, scheme, m_platform_hostname, port, path) )
336             {
337                 error.SetErrorString("invalid uri");
338                 return error;
339             }
340 
341             const ConnectionStatus status = m_gdb_client.Connect(url, &error);
342             if (status == eConnectionStatusSuccess)
343             {
344                 if (m_gdb_client.HandshakeWithServer(&error))
345                 {
346                     m_gdb_client.GetHostInfo();
347                     // If a working directory was set prior to connecting, send it down now
348                     if (m_working_dir)
349                         m_gdb_client.SetWorkingDir(m_working_dir.GetCString());
350                 }
351                 else
352                 {
353                     m_gdb_client.Disconnect();
354                     if (error.Success())
355                         error.SetErrorString("handshake failed");
356                 }
357             }
358         }
359         else
360         {
361             error.SetErrorString ("\"platform connect\" takes a single argument: <connect-url>");
362         }
363     }
364 
365     return error;
366 }
367 
368 Error
369 PlatformRemoteGDBServer::DisconnectRemote ()
370 {
371     Error error;
372     m_gdb_client.Disconnect(&error);
373     return error;
374 }
375 
376 const char *
377 PlatformRemoteGDBServer::GetHostname ()
378 {
379     m_gdb_client.GetHostname (m_name);
380     if (m_name.empty())
381         return NULL;
382     return m_name.c_str();
383 }
384 
385 const char *
386 PlatformRemoteGDBServer::GetUserName (uint32_t uid)
387 {
388     // Try and get a cache user name first
389     const char *cached_user_name = Platform::GetUserName(uid);
390     if (cached_user_name)
391         return cached_user_name;
392     std::string name;
393     if (m_gdb_client.GetUserName(uid, name))
394         return SetCachedUserName(uid, name.c_str(), name.size());
395 
396     SetUserNameNotFound(uid); // Negative cache so we don't keep sending packets
397     return NULL;
398 }
399 
400 const char *
401 PlatformRemoteGDBServer::GetGroupName (uint32_t gid)
402 {
403     const char *cached_group_name = Platform::GetGroupName(gid);
404     if (cached_group_name)
405         return cached_group_name;
406     std::string name;
407     if (m_gdb_client.GetGroupName(gid, name))
408         return SetCachedGroupName(gid, name.c_str(), name.size());
409 
410     SetGroupNameNotFound(gid); // Negative cache so we don't keep sending packets
411     return NULL;
412 }
413 
414 uint32_t
415 PlatformRemoteGDBServer::FindProcesses (const ProcessInstanceInfoMatch &match_info,
416                                         ProcessInstanceInfoList &process_infos)
417 {
418     return m_gdb_client.FindProcesses (match_info, process_infos);
419 }
420 
421 bool
422 PlatformRemoteGDBServer::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
423 {
424     return m_gdb_client.GetProcessInfo (pid, process_info);
425 }
426 
427 
428 Error
429 PlatformRemoteGDBServer::LaunchProcess (ProcessLaunchInfo &launch_info)
430 {
431     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_PLATFORM));
432     Error error;
433 
434     if (log)
435         log->Printf ("PlatformRemoteGDBServer::%s() called", __FUNCTION__);
436 
437     auto num_file_actions = launch_info.GetNumFileActions ();
438     for (decltype(num_file_actions) i = 0; i < num_file_actions; ++i)
439     {
440         const auto file_action = launch_info.GetFileActionAtIndex (i);
441         if (file_action->GetAction () != FileAction::eFileActionOpen)
442             continue;
443         switch(file_action->GetFD())
444         {
445         case STDIN_FILENO:
446             m_gdb_client.SetSTDIN (file_action->GetPath());
447             break;
448         case STDOUT_FILENO:
449             m_gdb_client.SetSTDOUT (file_action->GetPath());
450             break;
451         case STDERR_FILENO:
452             m_gdb_client.SetSTDERR (file_action->GetPath());
453             break;
454         }
455     }
456 
457     m_gdb_client.SetDisableASLR (launch_info.GetFlags().Test (eLaunchFlagDisableASLR));
458     m_gdb_client.SetDetachOnError (launch_info.GetFlags().Test (eLaunchFlagDetachOnError));
459 
460     const char *working_dir = launch_info.GetWorkingDirectory();
461     if (working_dir && working_dir[0])
462     {
463         m_gdb_client.SetWorkingDir (working_dir);
464     }
465 
466     // Send the environment and the program + arguments after we connect
467     const char **envp = launch_info.GetEnvironmentEntries().GetConstArgumentVector();
468 
469     if (envp)
470     {
471         const char *env_entry;
472         for (int i=0; (env_entry = envp[i]); ++i)
473         {
474             if (m_gdb_client.SendEnvironmentPacket(env_entry) != 0)
475                 break;
476         }
477     }
478 
479     ArchSpec arch_spec = launch_info.GetArchitecture();
480     const char *arch_triple = arch_spec.GetTriple().str().c_str();
481 
482     m_gdb_client.SendLaunchArchPacket(arch_triple);
483     if (log)
484         log->Printf ("PlatformRemoteGDBServer::%s() set launch architecture triple to '%s'", __FUNCTION__, arch_triple ? arch_triple : "<NULL>");
485 
486     const uint32_t old_packet_timeout = m_gdb_client.SetPacketTimeout (5);
487     int arg_packet_err = m_gdb_client.SendArgumentsPacket (launch_info);
488     m_gdb_client.SetPacketTimeout (old_packet_timeout);
489     if (arg_packet_err == 0)
490     {
491         std::string error_str;
492         if (m_gdb_client.GetLaunchSuccess (error_str))
493         {
494             const auto pid = m_gdb_client.GetCurrentProcessID (false);
495             if (pid != LLDB_INVALID_PROCESS_ID)
496             {
497                 launch_info.SetProcessID (pid);
498                 if (log)
499                     log->Printf ("PlatformRemoteGDBServer::%s() pid %" PRIu64 " launched successfully", __FUNCTION__, pid);
500             }
501             else
502             {
503                 if (log)
504                     log->Printf ("PlatformRemoteGDBServer::%s() launch succeeded but we didn't get a valid process id back!", __FUNCTION__);
505                 error.SetErrorString ("failed to get PID");
506             }
507         }
508         else
509         {
510             error.SetErrorString (error_str.c_str());
511             if (log)
512                 log->Printf ("PlatformRemoteGDBServer::%s() launch failed: %s", __FUNCTION__, error.AsCString ());
513         }
514     }
515     else
516     {
517         error.SetErrorStringWithFormat("'A' packet returned an error: %i", arg_packet_err);
518     }
519     return error;
520 }
521 
522 Error
523 PlatformRemoteGDBServer::KillProcess (const lldb::pid_t pid)
524 {
525     if (!m_gdb_client.KillSpawnedProcess(pid))
526         return Error("failed to kill remote spawned process");
527     return Error();
528 }
529 
530 lldb::ProcessSP
531 PlatformRemoteGDBServer::DebugProcess (lldb_private::ProcessLaunchInfo &launch_info,
532                                        lldb_private::Debugger &debugger,
533                                        lldb_private::Target *target,       // Can be NULL, if NULL create a new target, else use existing one
534                                        lldb_private::Error &error)
535 {
536     lldb::ProcessSP process_sp;
537     if (IsRemote())
538     {
539         if (IsConnected())
540         {
541             lldb::pid_t debugserver_pid = LLDB_INVALID_PROCESS_ID;
542             ArchSpec remote_arch = GetRemoteSystemArchitecture();
543             llvm::Triple &remote_triple = remote_arch.GetTriple();
544             uint16_t port = 0;
545             if (remote_triple.getVendor() == llvm::Triple::Apple && remote_triple.getOS() == llvm::Triple::IOS)
546             {
547                 // When remote debugging to iOS, we use a USB mux that always talks
548                 // to localhost, so we will need the remote debugserver to accept connections
549                 // only from localhost, no matter what our current hostname is
550                 port = m_gdb_client.LaunchGDBserverAndGetPort(debugserver_pid, "127.0.0.1");
551             }
552             else
553             {
554                 // All other hosts should use their actual hostname
555                 port = m_gdb_client.LaunchGDBserverAndGetPort(debugserver_pid, NULL);
556             }
557 
558             if (port == 0)
559             {
560                 error.SetErrorStringWithFormat ("unable to launch a GDB server on '%s'", GetHostname ());
561             }
562             else
563             {
564                 if (target == NULL)
565                 {
566                     TargetSP new_target_sp;
567 
568                     error = debugger.GetTargetList().CreateTarget (debugger,
569                                                                    NULL,
570                                                                    NULL,
571                                                                    false,
572                                                                    NULL,
573                                                                    new_target_sp);
574                     target = new_target_sp.get();
575                 }
576                 else
577                     error.Clear();
578 
579                 if (target && error.Success())
580                 {
581                     debugger.GetTargetList().SetSelectedTarget(target);
582 
583                     // The darwin always currently uses the GDB remote debugger plug-in
584                     // so even when debugging locally we are debugging remotely!
585                     process_sp = target->CreateProcess (launch_info.GetListenerForProcess(debugger), "gdb-remote", NULL);
586 
587                     if (process_sp)
588                     {
589                         char connect_url[256];
590                         const char *override_hostname = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_HOSTNAME");
591                         const char *port_offset_c_str = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_PORT_OFFSET");
592                         int port_offset = port_offset_c_str ? ::atoi(port_offset_c_str) : 0;
593                         const int connect_url_len = ::snprintf (connect_url,
594                                                                 sizeof(connect_url),
595                                                                 "connect://%s:%u",
596                                                                 override_hostname ? override_hostname : m_platform_hostname.c_str(),
597                                                                 port + port_offset);
598                         assert (connect_url_len < (int)sizeof(connect_url));
599                         error = process_sp->ConnectRemote (NULL, connect_url);
600                         // Retry the connect remote one time...
601                         if (error.Fail())
602                             error = process_sp->ConnectRemote (NULL, connect_url);
603                         if (error.Success())
604                             error = process_sp->Launch(launch_info);
605                         else if (debugserver_pid != LLDB_INVALID_PROCESS_ID)
606                         {
607                             printf ("error: connect remote failed (%s)\n", error.AsCString());
608                             m_gdb_client.KillSpawnedProcess(debugserver_pid);
609                         }
610                     }
611                 }
612             }
613         }
614         else
615         {
616             error.SetErrorString("not connected to remote gdb server");
617         }
618     }
619     return process_sp;
620 
621 }
622 
623 lldb::ProcessSP
624 PlatformRemoteGDBServer::Attach (lldb_private::ProcessAttachInfo &attach_info,
625                                  Debugger &debugger,
626                                  Target *target,       // Can be NULL, if NULL create a new target, else use existing one
627                                  Error &error)
628 {
629     lldb::ProcessSP process_sp;
630     if (IsRemote())
631     {
632         if (IsConnected())
633         {
634             lldb::pid_t debugserver_pid = LLDB_INVALID_PROCESS_ID;
635             ArchSpec remote_arch = GetRemoteSystemArchitecture();
636             llvm::Triple &remote_triple = remote_arch.GetTriple();
637             uint16_t port = 0;
638             if (remote_triple.getVendor() == llvm::Triple::Apple && remote_triple.getOS() == llvm::Triple::IOS)
639             {
640                 // When remote debugging to iOS, we use a USB mux that always talks
641                 // to localhost, so we will need the remote debugserver to accept connections
642                 // only from localhost, no matter what our current hostname is
643                 port = m_gdb_client.LaunchGDBserverAndGetPort(debugserver_pid, "127.0.0.1");
644             }
645             else
646             {
647                 // All other hosts should use their actual hostname
648                 port = m_gdb_client.LaunchGDBserverAndGetPort(debugserver_pid, NULL);
649             }
650 
651             if (port == 0)
652             {
653                 error.SetErrorStringWithFormat ("unable to launch a GDB server on '%s'", GetHostname ());
654             }
655             else
656             {
657                 if (target == NULL)
658                 {
659                     TargetSP new_target_sp;
660 
661                     error = debugger.GetTargetList().CreateTarget (debugger,
662                                                                    NULL,
663                                                                    NULL,
664                                                                    false,
665                                                                    NULL,
666                                                                    new_target_sp);
667                     target = new_target_sp.get();
668                 }
669                 else
670                     error.Clear();
671 
672                 if (target && error.Success())
673                 {
674                     debugger.GetTargetList().SetSelectedTarget(target);
675 
676                     // The darwin always currently uses the GDB remote debugger plug-in
677                     // so even when debugging locally we are debugging remotely!
678                     process_sp = target->CreateProcess (attach_info.GetListenerForProcess(debugger), "gdb-remote", NULL);
679 
680                     if (process_sp)
681                     {
682                         char connect_url[256];
683                         const char *override_hostname = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_HOSTNAME");
684                         const char *port_offset_c_str = getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_PORT_OFFSET");
685                         int port_offset = port_offset_c_str ? ::atoi(port_offset_c_str) : 0;
686                         const int connect_url_len = ::snprintf (connect_url,
687                                                                 sizeof(connect_url),
688                                                                 "connect://%s:%u",
689                                                                 override_hostname ? override_hostname : m_platform_hostname.c_str(),
690                                                                 port + port_offset);
691                         assert (connect_url_len < (int)sizeof(connect_url));
692                         error = process_sp->ConnectRemote(nullptr, connect_url);
693                         if (error.Success())
694                         {
695                             auto listener = attach_info.GetHijackListener();
696                             if (listener != nullptr)
697                                 process_sp->HijackProcessEvents(listener.get());
698                             error = process_sp->Attach(attach_info);
699                         }
700 
701                         if (error.Fail() && debugserver_pid != LLDB_INVALID_PROCESS_ID)
702                         {
703                             m_gdb_client.KillSpawnedProcess(debugserver_pid);
704                         }
705                     }
706                 }
707             }
708         }
709         else
710         {
711             error.SetErrorString("not connected to remote gdb server");
712         }
713     }
714     return process_sp;
715 }
716 
717 Error
718 PlatformRemoteGDBServer::MakeDirectory (const char *path, uint32_t mode)
719 {
720     Error error = m_gdb_client.MakeDirectory(path,mode);
721     Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
722     if (log)
723         log->Printf ("PlatformRemoteGDBServer::MakeDirectory(path='%s', mode=%o) error = %u (%s)", path, mode, error.GetError(), error.AsCString());
724     return error;
725 }
726 
727 
728 Error
729 PlatformRemoteGDBServer::GetFilePermissions (const char *path, uint32_t &file_permissions)
730 {
731     Error error = m_gdb_client.GetFilePermissions(path, file_permissions);
732     Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
733     if (log)
734         log->Printf ("PlatformRemoteGDBServer::GetFilePermissions(path='%s', file_permissions=%o) error = %u (%s)", path, file_permissions, error.GetError(), error.AsCString());
735     return error;
736 }
737 
738 Error
739 PlatformRemoteGDBServer::SetFilePermissions (const char *path, uint32_t file_permissions)
740 {
741     Error error = m_gdb_client.SetFilePermissions(path, file_permissions);
742     Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
743     if (log)
744         log->Printf ("PlatformRemoteGDBServer::SetFilePermissions(path='%s', file_permissions=%o) error = %u (%s)", path, file_permissions, error.GetError(), error.AsCString());
745     return error;
746 }
747 
748 
749 lldb::user_id_t
750 PlatformRemoteGDBServer::OpenFile (const lldb_private::FileSpec& file_spec,
751                                    uint32_t flags,
752                                    uint32_t mode,
753                                    Error &error)
754 {
755     return m_gdb_client.OpenFile (file_spec, flags, mode, error);
756 }
757 
758 bool
759 PlatformRemoteGDBServer::CloseFile (lldb::user_id_t fd, Error &error)
760 {
761     return m_gdb_client.CloseFile (fd, error);
762 }
763 
764 lldb::user_id_t
765 PlatformRemoteGDBServer::GetFileSize (const lldb_private::FileSpec& file_spec)
766 {
767     return m_gdb_client.GetFileSize(file_spec);
768 }
769 
770 uint64_t
771 PlatformRemoteGDBServer::ReadFile (lldb::user_id_t fd,
772                                    uint64_t offset,
773                                    void *dst,
774                                    uint64_t dst_len,
775                                    Error &error)
776 {
777     return m_gdb_client.ReadFile (fd, offset, dst, dst_len, error);
778 }
779 
780 uint64_t
781 PlatformRemoteGDBServer::WriteFile (lldb::user_id_t fd,
782                                     uint64_t offset,
783                                     const void* src,
784                                     uint64_t src_len,
785                                     Error &error)
786 {
787     return m_gdb_client.WriteFile (fd, offset, src, src_len, error);
788 }
789 
790 lldb_private::Error
791 PlatformRemoteGDBServer::PutFile (const lldb_private::FileSpec& source,
792          const lldb_private::FileSpec& destination,
793          uint32_t uid,
794          uint32_t gid)
795 {
796     return Platform::PutFile(source,destination,uid,gid);
797 }
798 
799 Error
800 PlatformRemoteGDBServer::CreateSymlink (const char *src,    // The name of the link is in src
801                                         const char *dst)    // The symlink points to dst
802 {
803     Error error = m_gdb_client.CreateSymlink (src, dst);
804     Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
805     if (log)
806         log->Printf ("PlatformRemoteGDBServer::CreateSymlink(src='%s', dst='%s') error = %u (%s)", src, dst, error.GetError(), error.AsCString());
807     return error;
808 }
809 
810 Error
811 PlatformRemoteGDBServer::Unlink (const char *path)
812 {
813     Error error = m_gdb_client.Unlink (path);
814     Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
815     if (log)
816         log->Printf ("PlatformRemoteGDBServer::Unlink(path='%s') error = %u (%s)", path, error.GetError(), error.AsCString());
817     return error;
818 }
819 
820 bool
821 PlatformRemoteGDBServer::GetFileExists (const lldb_private::FileSpec& file_spec)
822 {
823     return m_gdb_client.GetFileExists (file_spec);
824 }
825 
826 lldb_private::Error
827 PlatformRemoteGDBServer::RunShellCommand (const char *command,           // Shouldn't be NULL
828                                           const char *working_dir,       // Pass NULL to use the current working directory
829                                           int *status_ptr,               // Pass NULL if you don't want the process exit status
830                                           int *signo_ptr,                // Pass NULL if you don't want the signal that caused the process to exit
831                                           std::string *command_output,   // Pass NULL if you don't want the command output
832                                           uint32_t timeout_sec)          // Timeout in seconds to wait for shell program to finish
833 {
834     return m_gdb_client.RunShellCommand (command, working_dir, status_ptr, signo_ptr, command_output, timeout_sec);
835 }
836 
837 void
838 PlatformRemoteGDBServer::CalculateTrapHandlerSymbolNames ()
839 {
840     m_trap_handlers.push_back (ConstString ("_sigtramp"));
841 }
842