xref: /llvm-project/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.cpp (revision 106d02866d4d54b09c08e6a12915dba58e709294)
1 //===-- GDBRemoteCommunicationServer.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 <errno.h>
11 
12 #include "GDBRemoteCommunicationServer.h"
13 #include "lldb/Core/StreamGDBRemote.h"
14 
15 // C Includes
16 // C++ Includes
17 // Other libraries and framework includes
18 #include "llvm/ADT/Triple.h"
19 #include "lldb/Interpreter/Args.h"
20 #include "lldb/Core/ConnectionFileDescriptor.h"
21 #include "lldb/Core/Log.h"
22 #include "lldb/Core/State.h"
23 #include "lldb/Core/StreamString.h"
24 #include "lldb/Host/Endian.h"
25 #include "lldb/Host/File.h"
26 #include "lldb/Host/Host.h"
27 #include "lldb/Host/TimeValue.h"
28 #include "lldb/Target/Platform.h"
29 #include "lldb/Target/Process.h"
30 
31 // Project includes
32 #include "Utility/StringExtractorGDBRemote.h"
33 #include "ProcessGDBRemote.h"
34 #include "ProcessGDBRemoteLog.h"
35 
36 using namespace lldb;
37 using namespace lldb_private;
38 
39 //----------------------------------------------------------------------
40 // GDBRemoteCommunicationServer constructor
41 //----------------------------------------------------------------------
42 GDBRemoteCommunicationServer::GDBRemoteCommunicationServer(bool is_platform) :
43     GDBRemoteCommunication ("gdb-remote.server", "gdb-remote.server.rx_packet", is_platform),
44     m_platform_sp (Platform::GetDefaultPlatform ()),
45     m_async_thread (LLDB_INVALID_HOST_THREAD),
46     m_process_launch_info (),
47     m_process_launch_error (),
48     m_spawned_pids (),
49     m_spawned_pids_mutex (Mutex::eMutexTypeRecursive),
50     m_proc_infos (),
51     m_proc_infos_index (0),
52     m_port_map (),
53     m_port_offset(0)
54 {
55 }
56 
57 GDBRemoteCommunicationServer::GDBRemoteCommunicationServer(bool is_platform,
58                                                            const lldb::PlatformSP& platform_sp) :
59     GDBRemoteCommunication ("gdb-remote.server", "gdb-remote.server.rx_packet", is_platform),
60     m_platform_sp (platform_sp),
61     m_async_thread (LLDB_INVALID_HOST_THREAD),
62     m_process_launch_info (),
63     m_process_launch_error (),
64     m_spawned_pids (),
65     m_spawned_pids_mutex (Mutex::eMutexTypeRecursive),
66     m_proc_infos (),
67     m_proc_infos_index (0),
68     m_port_map (),
69     m_port_offset(0)
70 {
71     assert(platform_sp);
72 }
73 
74 //----------------------------------------------------------------------
75 // Destructor
76 //----------------------------------------------------------------------
77 GDBRemoteCommunicationServer::~GDBRemoteCommunicationServer()
78 {
79 }
80 
81 
82 //void *
83 //GDBRemoteCommunicationServer::AsyncThread (void *arg)
84 //{
85 //    GDBRemoteCommunicationServer *server = (GDBRemoteCommunicationServer*) arg;
86 //
87 //    Log *log;// (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
88 //    if (log)
89 //        log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %i) thread starting...", __FUNCTION__, arg, process->GetID());
90 //
91 //    StringExtractorGDBRemote packet;
92 //
93 //    while ()
94 //    {
95 //        if (packet.
96 //    }
97 //
98 //    if (log)
99 //        log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %i) thread exiting...", __FUNCTION__, arg, process->GetID());
100 //
101 //    process->m_async_thread = LLDB_INVALID_HOST_THREAD;
102 //    return NULL;
103 //}
104 //
105 bool
106 GDBRemoteCommunicationServer::GetPacketAndSendResponse (uint32_t timeout_usec,
107                                                         Error &error,
108                                                         bool &interrupt,
109                                                         bool &quit)
110 {
111     StringExtractorGDBRemote packet;
112     PacketResult packet_result = WaitForPacketWithTimeoutMicroSecondsNoLock (packet, timeout_usec);
113     if (packet_result == PacketResult::Success)
114     {
115         const StringExtractorGDBRemote::ServerPacketType packet_type = packet.GetServerPacketType ();
116         switch (packet_type)
117         {
118         case StringExtractorGDBRemote::eServerPacketType_nack:
119         case StringExtractorGDBRemote::eServerPacketType_ack:
120             break;
121 
122         case StringExtractorGDBRemote::eServerPacketType_invalid:
123             error.SetErrorString("invalid packet");
124             quit = true;
125             break;
126 
127         case StringExtractorGDBRemote::eServerPacketType_interrupt:
128             error.SetErrorString("interrupt received");
129             interrupt = true;
130             break;
131 
132         default:
133         case StringExtractorGDBRemote::eServerPacketType_unimplemented:
134             packet_result = SendUnimplementedResponse (packet.GetStringRef().c_str());
135             break;
136 
137         case StringExtractorGDBRemote::eServerPacketType_A:
138             packet_result = Handle_A (packet);
139             break;
140 
141         case StringExtractorGDBRemote::eServerPacketType_qfProcessInfo:
142             packet_result = Handle_qfProcessInfo (packet);
143             break;
144 
145         case StringExtractorGDBRemote::eServerPacketType_qsProcessInfo:
146             packet_result = Handle_qsProcessInfo (packet);
147             break;
148 
149         case StringExtractorGDBRemote::eServerPacketType_qC:
150             packet_result = Handle_qC (packet);
151             break;
152 
153         case StringExtractorGDBRemote::eServerPacketType_qHostInfo:
154             packet_result = Handle_qHostInfo (packet);
155             break;
156 
157         case StringExtractorGDBRemote::eServerPacketType_qLaunchGDBServer:
158             packet_result = Handle_qLaunchGDBServer (packet);
159             break;
160 
161         case StringExtractorGDBRemote::eServerPacketType_qKillSpawnedProcess:
162             packet_result = Handle_qKillSpawnedProcess (packet);
163             break;
164 
165         case StringExtractorGDBRemote::eServerPacketType_k:
166             packet_result = Handle_k (packet);
167             break;
168 
169         case StringExtractorGDBRemote::eServerPacketType_qLaunchSuccess:
170             packet_result = Handle_qLaunchSuccess (packet);
171             break;
172 
173         case StringExtractorGDBRemote::eServerPacketType_qGroupName:
174             packet_result = Handle_qGroupName (packet);
175             break;
176 
177         case StringExtractorGDBRemote::eServerPacketType_qProcessInfoPID:
178             packet_result = Handle_qProcessInfoPID (packet);
179             break;
180 
181         case StringExtractorGDBRemote::eServerPacketType_qSpeedTest:
182             packet_result = Handle_qSpeedTest (packet);
183             break;
184 
185         case StringExtractorGDBRemote::eServerPacketType_qUserName:
186             packet_result = Handle_qUserName (packet);
187             break;
188 
189         case StringExtractorGDBRemote::eServerPacketType_qGetWorkingDir:
190             packet_result = Handle_qGetWorkingDir(packet);
191             break;
192 
193         case StringExtractorGDBRemote::eServerPacketType_QEnvironment:
194             packet_result = Handle_QEnvironment (packet);
195             break;
196 
197         case StringExtractorGDBRemote::eServerPacketType_QLaunchArch:
198             packet_result = Handle_QLaunchArch (packet);
199             break;
200 
201         case StringExtractorGDBRemote::eServerPacketType_QSetDisableASLR:
202             packet_result = Handle_QSetDisableASLR (packet);
203             break;
204 
205         case StringExtractorGDBRemote::eServerPacketType_QSetDetachOnError:
206             packet_result = Handle_QSetDetachOnError (packet);
207             break;
208 
209         case StringExtractorGDBRemote::eServerPacketType_QSetSTDIN:
210             packet_result = Handle_QSetSTDIN (packet);
211             break;
212 
213         case StringExtractorGDBRemote::eServerPacketType_QSetSTDOUT:
214             packet_result = Handle_QSetSTDOUT (packet);
215             break;
216 
217         case StringExtractorGDBRemote::eServerPacketType_QSetSTDERR:
218             packet_result = Handle_QSetSTDERR (packet);
219             break;
220 
221         case StringExtractorGDBRemote::eServerPacketType_QSetWorkingDir:
222             packet_result = Handle_QSetWorkingDir (packet);
223             break;
224 
225         case StringExtractorGDBRemote::eServerPacketType_QStartNoAckMode:
226             packet_result = Handle_QStartNoAckMode (packet);
227             break;
228 
229         case StringExtractorGDBRemote::eServerPacketType_qPlatform_mkdir:
230             packet_result = Handle_qPlatform_mkdir (packet);
231             break;
232 
233         case StringExtractorGDBRemote::eServerPacketType_qPlatform_chmod:
234             packet_result = Handle_qPlatform_chmod (packet);
235             break;
236 
237         case StringExtractorGDBRemote::eServerPacketType_qPlatform_shell:
238             packet_result = Handle_qPlatform_shell (packet);
239             break;
240 
241         case StringExtractorGDBRemote::eServerPacketType_vFile_open:
242             packet_result = Handle_vFile_Open (packet);
243             break;
244 
245         case StringExtractorGDBRemote::eServerPacketType_vFile_close:
246             packet_result = Handle_vFile_Close (packet);
247             break;
248 
249         case StringExtractorGDBRemote::eServerPacketType_vFile_pread:
250             packet_result = Handle_vFile_pRead (packet);
251             break;
252 
253         case StringExtractorGDBRemote::eServerPacketType_vFile_pwrite:
254             packet_result = Handle_vFile_pWrite (packet);
255             break;
256 
257         case StringExtractorGDBRemote::eServerPacketType_vFile_size:
258             packet_result = Handle_vFile_Size (packet);
259             break;
260 
261         case StringExtractorGDBRemote::eServerPacketType_vFile_mode:
262             packet_result = Handle_vFile_Mode (packet);
263             break;
264 
265         case StringExtractorGDBRemote::eServerPacketType_vFile_exists:
266             packet_result = Handle_vFile_Exists (packet);
267             break;
268 
269         case StringExtractorGDBRemote::eServerPacketType_vFile_stat:
270             packet_result = Handle_vFile_Stat (packet);
271             break;
272 
273         case StringExtractorGDBRemote::eServerPacketType_vFile_md5:
274             packet_result = Handle_vFile_MD5 (packet);
275             break;
276 
277         case StringExtractorGDBRemote::eServerPacketType_vFile_symlink:
278             packet_result = Handle_vFile_symlink (packet);
279             break;
280 
281         case StringExtractorGDBRemote::eServerPacketType_vFile_unlink:
282             packet_result = Handle_vFile_unlink (packet);
283             break;
284         }
285     }
286     else
287     {
288         if (!IsConnected())
289         {
290             error.SetErrorString("lost connection");
291             quit = true;
292         }
293         else
294         {
295             error.SetErrorString("timeout");
296         }
297     }
298     return packet_result == PacketResult::Success;
299 }
300 
301 lldb_private::Error
302 GDBRemoteCommunicationServer::SetLaunchArguments (const char *const args[], int argc)
303 {
304     if ((argc < 1) || !args || !args[0] || !args[0][0])
305         return lldb_private::Error ("%s: no process command line specified to launch", __FUNCTION__);
306 
307     m_process_launch_info.SetArguments (const_cast<const char**> (args), true);
308     return lldb_private::Error ();
309 }
310 
311 lldb_private::Error
312 GDBRemoteCommunicationServer::SetLaunchFlags (unsigned int launch_flags)
313 {
314     m_process_launch_info.GetFlags ().Set (launch_flags);
315     return lldb_private::Error ();
316 }
317 
318 lldb_private::Error
319 GDBRemoteCommunicationServer::LaunchProcess ()
320 {
321     if (!m_process_launch_info.GetArguments ().GetArgumentCount ())
322         return lldb_private::Error ("%s: no process command line specified to launch", __FUNCTION__);
323 
324     // specify the process monitor if not already set.  This should
325     // generally be what happens since we need to reap started
326     // processes.
327     if (!m_process_launch_info.GetMonitorProcessCallback ())
328         m_process_launch_info.SetMonitorProcessCallback(ReapDebuggedProcess, this, false);
329 
330     lldb_private::Error error = m_platform_sp->LaunchProcess (m_process_launch_info);
331     if (!error.Success ())
332     {
333         fprintf (stderr, "%s: failed to launch executable %s", __FUNCTION__, m_process_launch_info.GetArguments ().GetArgumentAtIndex (0));
334         return error;
335     }
336 
337     printf ("Launched '%s' as process %" PRIu64 "...\n", m_process_launch_info.GetArguments ().GetArgumentAtIndex (0), m_process_launch_info.GetProcessID());
338 
339     // add to list of spawned processes.  On an lldb-gdbserver, we
340     // would expect there to be only one.
341     lldb::pid_t pid;
342     if ( (pid = m_process_launch_info.GetProcessID()) != LLDB_INVALID_PROCESS_ID )
343     {
344         Mutex::Locker locker (m_spawned_pids_mutex);
345         m_spawned_pids.insert(pid);
346     }
347 
348     return error;
349 }
350 
351 GDBRemoteCommunication::PacketResult
352 GDBRemoteCommunicationServer::SendUnimplementedResponse (const char *)
353 {
354     // TODO: Log the packet we aren't handling...
355     return SendPacketNoLock ("", 0);
356 }
357 
358 GDBRemoteCommunication::PacketResult
359 GDBRemoteCommunicationServer::SendErrorResponse (uint8_t err)
360 {
361     char packet[16];
362     int packet_len = ::snprintf (packet, sizeof(packet), "E%2.2x", err);
363     assert (packet_len < (int)sizeof(packet));
364     return SendPacketNoLock (packet, packet_len);
365 }
366 
367 
368 GDBRemoteCommunication::PacketResult
369 GDBRemoteCommunicationServer::SendOKResponse ()
370 {
371     return SendPacketNoLock ("OK", 2);
372 }
373 
374 bool
375 GDBRemoteCommunicationServer::HandshakeWithClient(Error *error_ptr)
376 {
377     return GetAck() == PacketResult::Success;
378 }
379 
380 GDBRemoteCommunication::PacketResult
381 GDBRemoteCommunicationServer::Handle_qHostInfo (StringExtractorGDBRemote &packet)
382 {
383     StreamString response;
384 
385     // $cputype:16777223;cpusubtype:3;ostype:Darwin;vendor:apple;endian:little;ptrsize:8;#00
386 
387     ArchSpec host_arch (Host::GetArchitecture ());
388     const llvm::Triple &host_triple = host_arch.GetTriple();
389     response.PutCString("triple:");
390     response.PutCStringAsRawHex8(host_triple.getTriple().c_str());
391     response.Printf (";ptrsize:%u;",host_arch.GetAddressByteSize());
392 
393     const char* distribution_id = host_arch.GetDistributionId ().AsCString ();
394     if (distribution_id)
395     {
396         response.PutCString("distribution_id:");
397         response.PutCStringAsRawHex8(distribution_id);
398         response.PutCString(";");
399     }
400 
401     uint32_t cpu = host_arch.GetMachOCPUType();
402     uint32_t sub = host_arch.GetMachOCPUSubType();
403     if (cpu != LLDB_INVALID_CPUTYPE)
404         response.Printf ("cputype:%u;", cpu);
405     if (sub != LLDB_INVALID_CPUTYPE)
406         response.Printf ("cpusubtype:%u;", sub);
407 
408     if (cpu == ArchSpec::kCore_arm_any)
409         response.Printf("watchpoint_exceptions_received:before;");   // On armv7 we use "synchronous" watchpoints which means the exception is delivered before the instruction executes.
410     else
411         response.Printf("watchpoint_exceptions_received:after;");
412 
413     switch (lldb::endian::InlHostByteOrder())
414     {
415     case eByteOrderBig:     response.PutCString ("endian:big;"); break;
416     case eByteOrderLittle:  response.PutCString ("endian:little;"); break;
417     case eByteOrderPDP:     response.PutCString ("endian:pdp;"); break;
418     default:                response.PutCString ("endian:unknown;"); break;
419     }
420 
421     uint32_t major = UINT32_MAX;
422     uint32_t minor = UINT32_MAX;
423     uint32_t update = UINT32_MAX;
424     if (Host::GetOSVersion (major, minor, update))
425     {
426         if (major != UINT32_MAX)
427         {
428             response.Printf("os_version:%u", major);
429             if (minor != UINT32_MAX)
430             {
431                 response.Printf(".%u", minor);
432                 if (update != UINT32_MAX)
433                     response.Printf(".%u", update);
434             }
435             response.PutChar(';');
436         }
437     }
438 
439     std::string s;
440     if (Host::GetOSBuildString (s))
441     {
442         response.PutCString ("os_build:");
443         response.PutCStringAsRawHex8(s.c_str());
444         response.PutChar(';');
445     }
446     if (Host::GetOSKernelDescription (s))
447     {
448         response.PutCString ("os_kernel:");
449         response.PutCStringAsRawHex8(s.c_str());
450         response.PutChar(';');
451     }
452 #if defined(__APPLE__)
453 
454 #if defined(__arm__) || defined(__arm64__)
455     // For iOS devices, we are connected through a USB Mux so we never pretend
456     // to actually have a hostname as far as the remote lldb that is connecting
457     // to this lldb-platform is concerned
458     response.PutCString ("hostname:");
459     response.PutCStringAsRawHex8("127.0.0.1");
460     response.PutChar(';');
461 #else   // #if defined(__arm__) || defined(__arm64__)
462     if (Host::GetHostname (s))
463     {
464         response.PutCString ("hostname:");
465         response.PutCStringAsRawHex8(s.c_str());
466         response.PutChar(';');
467     }
468 #endif  // #if defined(__arm__) || defined(__arm64__)
469 
470 #else   // #if defined(__APPLE__)
471     if (Host::GetHostname (s))
472     {
473         response.PutCString ("hostname:");
474         response.PutCStringAsRawHex8(s.c_str());
475         response.PutChar(';');
476     }
477 #endif  // #if defined(__APPLE__)
478 
479     return SendPacketNoLock (response.GetData(), response.GetSize());
480 }
481 
482 static void
483 CreateProcessInfoResponse (const ProcessInstanceInfo &proc_info, StreamString &response)
484 {
485     response.Printf ("pid:%" PRIu64 ";ppid:%" PRIu64 ";uid:%i;gid:%i;euid:%i;egid:%i;",
486                      proc_info.GetProcessID(),
487                      proc_info.GetParentProcessID(),
488                      proc_info.GetUserID(),
489                      proc_info.GetGroupID(),
490                      proc_info.GetEffectiveUserID(),
491                      proc_info.GetEffectiveGroupID());
492     response.PutCString ("name:");
493     response.PutCStringAsRawHex8(proc_info.GetName());
494     response.PutChar(';');
495     const ArchSpec &proc_arch = proc_info.GetArchitecture();
496     if (proc_arch.IsValid())
497     {
498         const llvm::Triple &proc_triple = proc_arch.GetTriple();
499         response.PutCString("triple:");
500         response.PutCStringAsRawHex8(proc_triple.getTriple().c_str());
501         response.PutChar(';');
502     }
503 }
504 
505 GDBRemoteCommunication::PacketResult
506 GDBRemoteCommunicationServer::Handle_qProcessInfoPID (StringExtractorGDBRemote &packet)
507 {
508     // Packet format: "qProcessInfoPID:%i" where %i is the pid
509     packet.SetFilePos(::strlen ("qProcessInfoPID:"));
510     lldb::pid_t pid = packet.GetU32 (LLDB_INVALID_PROCESS_ID);
511     if (pid != LLDB_INVALID_PROCESS_ID)
512     {
513         ProcessInstanceInfo proc_info;
514         if (Host::GetProcessInfo(pid, proc_info))
515         {
516             StreamString response;
517             CreateProcessInfoResponse (proc_info, response);
518             return SendPacketNoLock (response.GetData(), response.GetSize());
519         }
520     }
521     return SendErrorResponse (1);
522 }
523 
524 GDBRemoteCommunication::PacketResult
525 GDBRemoteCommunicationServer::Handle_qfProcessInfo (StringExtractorGDBRemote &packet)
526 {
527     m_proc_infos_index = 0;
528     m_proc_infos.Clear();
529 
530     ProcessInstanceInfoMatch match_info;
531     packet.SetFilePos(::strlen ("qfProcessInfo"));
532     if (packet.GetChar() == ':')
533     {
534 
535         std::string key;
536         std::string value;
537         while (packet.GetNameColonValue(key, value))
538         {
539             bool success = true;
540             if (key.compare("name") == 0)
541             {
542                 StringExtractor extractor;
543                 extractor.GetStringRef().swap(value);
544                 extractor.GetHexByteString (value);
545                 match_info.GetProcessInfo().GetExecutableFile().SetFile(value.c_str(), false);
546             }
547             else if (key.compare("name_match") == 0)
548             {
549                 if (value.compare("equals") == 0)
550                 {
551                     match_info.SetNameMatchType (eNameMatchEquals);
552                 }
553                 else if (value.compare("starts_with") == 0)
554                 {
555                     match_info.SetNameMatchType (eNameMatchStartsWith);
556                 }
557                 else if (value.compare("ends_with") == 0)
558                 {
559                     match_info.SetNameMatchType (eNameMatchEndsWith);
560                 }
561                 else if (value.compare("contains") == 0)
562                 {
563                     match_info.SetNameMatchType (eNameMatchContains);
564                 }
565                 else if (value.compare("regex") == 0)
566                 {
567                     match_info.SetNameMatchType (eNameMatchRegularExpression);
568                 }
569                 else
570                 {
571                     success = false;
572                 }
573             }
574             else if (key.compare("pid") == 0)
575             {
576                 match_info.GetProcessInfo().SetProcessID (Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0, &success));
577             }
578             else if (key.compare("parent_pid") == 0)
579             {
580                 match_info.GetProcessInfo().SetParentProcessID (Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0, &success));
581             }
582             else if (key.compare("uid") == 0)
583             {
584                 match_info.GetProcessInfo().SetUserID (Args::StringToUInt32(value.c_str(), UINT32_MAX, 0, &success));
585             }
586             else if (key.compare("gid") == 0)
587             {
588                 match_info.GetProcessInfo().SetGroupID (Args::StringToUInt32(value.c_str(), UINT32_MAX, 0, &success));
589             }
590             else if (key.compare("euid") == 0)
591             {
592                 match_info.GetProcessInfo().SetEffectiveUserID (Args::StringToUInt32(value.c_str(), UINT32_MAX, 0, &success));
593             }
594             else if (key.compare("egid") == 0)
595             {
596                 match_info.GetProcessInfo().SetEffectiveGroupID (Args::StringToUInt32(value.c_str(), UINT32_MAX, 0, &success));
597             }
598             else if (key.compare("all_users") == 0)
599             {
600                 match_info.SetMatchAllUsers(Args::StringToBoolean(value.c_str(), false, &success));
601             }
602             else if (key.compare("triple") == 0)
603             {
604                 match_info.GetProcessInfo().GetArchitecture().SetTriple (value.c_str(), NULL);
605             }
606             else
607             {
608                 success = false;
609             }
610 
611             if (!success)
612                 return SendErrorResponse (2);
613         }
614     }
615 
616     if (Host::FindProcesses (match_info, m_proc_infos))
617     {
618         // We found something, return the first item by calling the get
619         // subsequent process info packet handler...
620         return Handle_qsProcessInfo (packet);
621     }
622     return SendErrorResponse (3);
623 }
624 
625 GDBRemoteCommunication::PacketResult
626 GDBRemoteCommunicationServer::Handle_qsProcessInfo (StringExtractorGDBRemote &packet)
627 {
628     if (m_proc_infos_index < m_proc_infos.GetSize())
629     {
630         StreamString response;
631         CreateProcessInfoResponse (m_proc_infos.GetProcessInfoAtIndex(m_proc_infos_index), response);
632         ++m_proc_infos_index;
633         return SendPacketNoLock (response.GetData(), response.GetSize());
634     }
635     return SendErrorResponse (4);
636 }
637 
638 GDBRemoteCommunication::PacketResult
639 GDBRemoteCommunicationServer::Handle_qUserName (StringExtractorGDBRemote &packet)
640 {
641     // Packet format: "qUserName:%i" where %i is the uid
642     packet.SetFilePos(::strlen ("qUserName:"));
643     uint32_t uid = packet.GetU32 (UINT32_MAX);
644     if (uid != UINT32_MAX)
645     {
646         std::string name;
647         if (Host::GetUserName (uid, name))
648         {
649             StreamString response;
650             response.PutCStringAsRawHex8 (name.c_str());
651             return SendPacketNoLock (response.GetData(), response.GetSize());
652         }
653     }
654     return SendErrorResponse (5);
655 
656 }
657 
658 GDBRemoteCommunication::PacketResult
659 GDBRemoteCommunicationServer::Handle_qGroupName (StringExtractorGDBRemote &packet)
660 {
661     // Packet format: "qGroupName:%i" where %i is the gid
662     packet.SetFilePos(::strlen ("qGroupName:"));
663     uint32_t gid = packet.GetU32 (UINT32_MAX);
664     if (gid != UINT32_MAX)
665     {
666         std::string name;
667         if (Host::GetGroupName (gid, name))
668         {
669             StreamString response;
670             response.PutCStringAsRawHex8 (name.c_str());
671             return SendPacketNoLock (response.GetData(), response.GetSize());
672         }
673     }
674     return SendErrorResponse (6);
675 }
676 
677 GDBRemoteCommunication::PacketResult
678 GDBRemoteCommunicationServer::Handle_qSpeedTest (StringExtractorGDBRemote &packet)
679 {
680     packet.SetFilePos(::strlen ("qSpeedTest:"));
681 
682     std::string key;
683     std::string value;
684     bool success = packet.GetNameColonValue(key, value);
685     if (success && key.compare("response_size") == 0)
686     {
687         uint32_t response_size = Args::StringToUInt32(value.c_str(), 0, 0, &success);
688         if (success)
689         {
690             if (response_size == 0)
691                 return SendOKResponse();
692             StreamString response;
693             uint32_t bytes_left = response_size;
694             response.PutCString("data:");
695             while (bytes_left > 0)
696             {
697                 if (bytes_left >= 26)
698                 {
699                     response.PutCString("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
700                     bytes_left -= 26;
701                 }
702                 else
703                 {
704                     response.Printf ("%*.*s;", bytes_left, bytes_left, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
705                     bytes_left = 0;
706                 }
707             }
708             return SendPacketNoLock (response.GetData(), response.GetSize());
709         }
710     }
711     return SendErrorResponse (7);
712 }
713 
714 //
715 //static bool
716 //WaitForProcessToSIGSTOP (const lldb::pid_t pid, const int timeout_in_seconds)
717 //{
718 //    const int time_delta_usecs = 100000;
719 //    const int num_retries = timeout_in_seconds/time_delta_usecs;
720 //    for (int i=0; i<num_retries; i++)
721 //    {
722 //        struct proc_bsdinfo bsd_info;
723 //        int error = ::proc_pidinfo (pid, PROC_PIDTBSDINFO,
724 //                                    (uint64_t) 0,
725 //                                    &bsd_info,
726 //                                    PROC_PIDTBSDINFO_SIZE);
727 //
728 //        switch (error)
729 //        {
730 //            case EINVAL:
731 //            case ENOTSUP:
732 //            case ESRCH:
733 //            case EPERM:
734 //                return false;
735 //
736 //            default:
737 //                break;
738 //
739 //            case 0:
740 //                if (bsd_info.pbi_status == SSTOP)
741 //                    return true;
742 //        }
743 //        ::usleep (time_delta_usecs);
744 //    }
745 //    return false;
746 //}
747 
748 GDBRemoteCommunication::PacketResult
749 GDBRemoteCommunicationServer::Handle_A (StringExtractorGDBRemote &packet)
750 {
751     // The 'A' packet is the most over designed packet ever here with
752     // redundant argument indexes, redundant argument lengths and needed hex
753     // encoded argument string values. Really all that is needed is a comma
754     // separated hex encoded argument value list, but we will stay true to the
755     // documented version of the 'A' packet here...
756 
757     packet.SetFilePos(1); // Skip the 'A'
758     bool success = true;
759     while (success && packet.GetBytesLeft() > 0)
760     {
761         // Decode the decimal argument string length. This length is the
762         // number of hex nibbles in the argument string value.
763         const uint32_t arg_len = packet.GetU32(UINT32_MAX);
764         if (arg_len == UINT32_MAX)
765             success = false;
766         else
767         {
768             // Make sure the argument hex string length is followed by a comma
769             if (packet.GetChar() != ',')
770                 success = false;
771             else
772             {
773                 // Decode the argument index. We ignore this really becuase
774                 // who would really send down the arguments in a random order???
775                 const uint32_t arg_idx = packet.GetU32(UINT32_MAX);
776                 if (arg_idx == UINT32_MAX)
777                     success = false;
778                 else
779                 {
780                     // Make sure the argument index is followed by a comma
781                     if (packet.GetChar() != ',')
782                         success = false;
783                     else
784                     {
785                         // Decode the argument string value from hex bytes
786                         // back into a UTF8 string and make sure the length
787                         // matches the one supplied in the packet
788                         std::string arg;
789                         if (packet.GetHexByteString(arg) != (arg_len / 2))
790                             success = false;
791                         else
792                         {
793                             // If there are any bytes lft
794                             if (packet.GetBytesLeft())
795                             {
796                                 if (packet.GetChar() != ',')
797                                     success = false;
798                             }
799 
800                             if (success)
801                             {
802                                 if (arg_idx == 0)
803                                     m_process_launch_info.GetExecutableFile().SetFile(arg.c_str(), false);
804                                 m_process_launch_info.GetArguments().AppendArgument(arg.c_str());
805                             }
806                         }
807                     }
808                 }
809             }
810         }
811     }
812 
813     if (success)
814     {
815         // FIXME: remove linux restriction once eLaunchFlagDebug is supported
816 #if !defined (__linux__)
817         m_process_launch_info.GetFlags().Set (eLaunchFlagDebug);
818 #endif
819         m_process_launch_error = LaunchProcess ();
820         if (m_process_launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
821         {
822             return SendOKResponse ();
823         }
824     }
825     return SendErrorResponse (8);
826 }
827 
828 GDBRemoteCommunication::PacketResult
829 GDBRemoteCommunicationServer::Handle_qC (StringExtractorGDBRemote &packet)
830 {
831     lldb::pid_t pid = m_process_launch_info.GetProcessID();
832     StreamString response;
833     response.Printf("QC%" PRIx64, pid);
834     if (m_is_platform)
835     {
836         // If we launch a process and this GDB server is acting as a platform,
837         // then we need to clear the process launch state so we can start
838         // launching another process. In order to launch a process a bunch or
839         // packets need to be sent: environment packets, working directory,
840         // disable ASLR, and many more settings. When we launch a process we
841         // then need to know when to clear this information. Currently we are
842         // selecting the 'qC' packet as that packet which seems to make the most
843         // sense.
844         if (pid != LLDB_INVALID_PROCESS_ID)
845         {
846             m_process_launch_info.Clear();
847         }
848     }
849     return SendPacketNoLock (response.GetData(), response.GetSize());
850 }
851 
852 bool
853 GDBRemoteCommunicationServer::DebugserverProcessReaped (lldb::pid_t pid)
854 {
855     Mutex::Locker locker (m_spawned_pids_mutex);
856     FreePortForProcess(pid);
857     return m_spawned_pids.erase(pid) > 0;
858 }
859 bool
860 GDBRemoteCommunicationServer::ReapDebugserverProcess (void *callback_baton,
861                                                       lldb::pid_t pid,
862                                                       bool exited,
863                                                       int signal,    // Zero for no signal
864                                                       int status)    // Exit value of process if signal is zero
865 {
866     GDBRemoteCommunicationServer *server = (GDBRemoteCommunicationServer *)callback_baton;
867     server->DebugserverProcessReaped (pid);
868     return true;
869 }
870 
871 bool
872 GDBRemoteCommunicationServer::DebuggedProcessReaped (lldb::pid_t pid)
873 {
874     // reap a process that we were debugging (but not debugserver)
875     Mutex::Locker locker (m_spawned_pids_mutex);
876     return m_spawned_pids.erase(pid) > 0;
877 }
878 
879 bool
880 GDBRemoteCommunicationServer::ReapDebuggedProcess (void *callback_baton,
881                                                    lldb::pid_t pid,
882                                                    bool exited,
883                                                    int signal,    // Zero for no signal
884                                                    int status)    // Exit value of process if signal is zero
885 {
886     GDBRemoteCommunicationServer *server = (GDBRemoteCommunicationServer *)callback_baton;
887     server->DebuggedProcessReaped (pid);
888     return true;
889 }
890 
891 GDBRemoteCommunication::PacketResult
892 GDBRemoteCommunicationServer::Handle_qLaunchGDBServer (StringExtractorGDBRemote &packet)
893 {
894 #ifdef _WIN32
895     return SendErrorResponse(9);
896 #else
897     // Spawn a local debugserver as a platform so we can then attach or launch
898     // a process...
899 
900     if (m_is_platform)
901     {
902         // Sleep and wait a bit for debugserver to start to listen...
903         ConnectionFileDescriptor file_conn;
904         Error error;
905         std::string hostname;
906         // TODO: /tmp/ should not be hardcoded. User might want to override /tmp
907         // with the TMPDIR environnement variable
908         packet.SetFilePos(::strlen ("qLaunchGDBServer;"));
909         std::string name;
910         std::string value;
911         uint16_t port = UINT16_MAX;
912         while (packet.GetNameColonValue(name, value))
913         {
914             if (name.compare ("host") == 0)
915                 hostname.swap(value);
916             else if (name.compare ("port") == 0)
917                 port = Args::StringToUInt32(value.c_str(), 0, 0);
918         }
919         if (port == UINT16_MAX)
920             port = GetNextAvailablePort();
921 
922         // Spawn a new thread to accept the port that gets bound after
923         // binding to port 0 (zero).
924 
925         if (error.Success())
926         {
927             // Spawn a debugserver and try to get the port it listens to.
928             ProcessLaunchInfo debugserver_launch_info;
929             if (hostname.empty())
930                 hostname = "127.0.0.1";
931             Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
932             if (log)
933                 log->Printf("Launching debugserver with: %s:%u...\n", hostname.c_str(), port);
934 
935             debugserver_launch_info.SetMonitorProcessCallback(ReapDebugserverProcess, this, false);
936 
937             error = StartDebugserverProcess (hostname.empty() ? NULL : hostname.c_str(),
938                                              port,
939                                              debugserver_launch_info,
940                                              port);
941 
942             lldb::pid_t debugserver_pid = debugserver_launch_info.GetProcessID();
943 
944 
945             if (debugserver_pid != LLDB_INVALID_PROCESS_ID)
946             {
947                 Mutex::Locker locker (m_spawned_pids_mutex);
948                 m_spawned_pids.insert(debugserver_pid);
949                 if (port > 0)
950                     AssociatePortWithProcess(port, debugserver_pid);
951             }
952             else
953             {
954                 if (port > 0)
955                     FreePort (port);
956             }
957 
958             if (error.Success())
959             {
960                 char response[256];
961                 const int response_len = ::snprintf (response, sizeof(response), "pid:%" PRIu64 ";port:%u;", debugserver_pid, port + m_port_offset);
962                 assert (response_len < (int)sizeof(response));
963                 PacketResult packet_result = SendPacketNoLock (response, response_len);
964 
965                 if (packet_result != PacketResult::Success)
966                 {
967                     if (debugserver_pid != LLDB_INVALID_PROCESS_ID)
968                         ::kill (debugserver_pid, SIGINT);
969                 }
970                 return packet_result;
971             }
972         }
973     }
974     return SendErrorResponse (9);
975 #endif
976 }
977 
978 bool
979 GDBRemoteCommunicationServer::KillSpawnedProcess (lldb::pid_t pid)
980 {
981     // make sure we know about this process
982     {
983         Mutex::Locker locker (m_spawned_pids_mutex);
984         if (m_spawned_pids.find(pid) == m_spawned_pids.end())
985             return false;
986     }
987 
988     // first try a SIGTERM (standard kill)
989     Host::Kill (pid, SIGTERM);
990 
991     // check if that worked
992     for (size_t i=0; i<10; ++i)
993     {
994         {
995             Mutex::Locker locker (m_spawned_pids_mutex);
996             if (m_spawned_pids.find(pid) == m_spawned_pids.end())
997             {
998                 // it is now killed
999                 return true;
1000             }
1001         }
1002         usleep (10000);
1003     }
1004 
1005     // check one more time after the final usleep
1006     {
1007         Mutex::Locker locker (m_spawned_pids_mutex);
1008         if (m_spawned_pids.find(pid) == m_spawned_pids.end())
1009             return true;
1010     }
1011 
1012     // the launched process still lives.  Now try killling it again,
1013     // this time with an unblockable signal.
1014     Host::Kill (pid, SIGKILL);
1015 
1016     for (size_t i=0; i<10; ++i)
1017     {
1018         {
1019             Mutex::Locker locker (m_spawned_pids_mutex);
1020             if (m_spawned_pids.find(pid) == m_spawned_pids.end())
1021             {
1022                 // it is now killed
1023                 return true;
1024             }
1025         }
1026         usleep (10000);
1027     }
1028 
1029     // check one more time after the final usleep
1030     // Scope for locker
1031     {
1032         Mutex::Locker locker (m_spawned_pids_mutex);
1033         if (m_spawned_pids.find(pid) == m_spawned_pids.end())
1034             return true;
1035     }
1036 
1037     // no luck - the process still lives
1038     return false;
1039 }
1040 
1041 GDBRemoteCommunication::PacketResult
1042 GDBRemoteCommunicationServer::Handle_qKillSpawnedProcess (StringExtractorGDBRemote &packet)
1043 {
1044     packet.SetFilePos(::strlen ("qKillSpawnedProcess:"));
1045 
1046     lldb::pid_t pid = packet.GetU64(LLDB_INVALID_PROCESS_ID);
1047 
1048     // verify that we know anything about this pid.
1049     // Scope for locker
1050     {
1051         Mutex::Locker locker (m_spawned_pids_mutex);
1052         if (m_spawned_pids.find(pid) == m_spawned_pids.end())
1053         {
1054             // not a pid we know about
1055             return SendErrorResponse (10);
1056         }
1057     }
1058 
1059     // go ahead and attempt to kill the spawned process
1060     if (KillSpawnedProcess (pid))
1061         return SendOKResponse ();
1062     else
1063         return SendErrorResponse (11);
1064 }
1065 
1066 GDBRemoteCommunication::PacketResult
1067 GDBRemoteCommunicationServer::Handle_k (StringExtractorGDBRemote &packet)
1068 {
1069     // ignore for now if we're lldb_platform
1070     if (m_is_platform)
1071         return SendUnimplementedResponse (packet.GetStringRef().c_str());
1072 
1073     // shutdown all spawned processes
1074     std::set<lldb::pid_t> spawned_pids_copy;
1075 
1076     // copy pids
1077     {
1078         Mutex::Locker locker (m_spawned_pids_mutex);
1079         spawned_pids_copy.insert (m_spawned_pids.begin (), m_spawned_pids.end ());
1080     }
1081 
1082     // nuke the spawned processes
1083     for (auto it = spawned_pids_copy.begin (); it != spawned_pids_copy.end (); ++it)
1084     {
1085         lldb::pid_t spawned_pid = *it;
1086         if (!KillSpawnedProcess (spawned_pid))
1087         {
1088             fprintf (stderr, "%s: failed to kill spawned pid %" PRIu64 ", ignoring.\n", __FUNCTION__, spawned_pid);
1089         }
1090     }
1091 
1092     // TODO figure out how to shut down gracefully at this point
1093     return SendOKResponse ();
1094 }
1095 
1096 GDBRemoteCommunication::PacketResult
1097 GDBRemoteCommunicationServer::Handle_qLaunchSuccess (StringExtractorGDBRemote &packet)
1098 {
1099     if (m_process_launch_error.Success())
1100         return SendOKResponse();
1101     StreamString response;
1102     response.PutChar('E');
1103     response.PutCString(m_process_launch_error.AsCString("<unknown error>"));
1104     return SendPacketNoLock (response.GetData(), response.GetSize());
1105 }
1106 
1107 GDBRemoteCommunication::PacketResult
1108 GDBRemoteCommunicationServer::Handle_QEnvironment  (StringExtractorGDBRemote &packet)
1109 {
1110     packet.SetFilePos(::strlen ("QEnvironment:"));
1111     const uint32_t bytes_left = packet.GetBytesLeft();
1112     if (bytes_left > 0)
1113     {
1114         m_process_launch_info.GetEnvironmentEntries ().AppendArgument (packet.Peek());
1115         return SendOKResponse ();
1116     }
1117     return SendErrorResponse (12);
1118 }
1119 
1120 GDBRemoteCommunication::PacketResult
1121 GDBRemoteCommunicationServer::Handle_QLaunchArch (StringExtractorGDBRemote &packet)
1122 {
1123     packet.SetFilePos(::strlen ("QLaunchArch:"));
1124     const uint32_t bytes_left = packet.GetBytesLeft();
1125     if (bytes_left > 0)
1126     {
1127         const char* arch_triple = packet.Peek();
1128         ArchSpec arch_spec(arch_triple,NULL);
1129         m_process_launch_info.SetArchitecture(arch_spec);
1130         return SendOKResponse();
1131     }
1132     return SendErrorResponse(13);
1133 }
1134 
1135 GDBRemoteCommunication::PacketResult
1136 GDBRemoteCommunicationServer::Handle_QSetDisableASLR (StringExtractorGDBRemote &packet)
1137 {
1138     packet.SetFilePos(::strlen ("QSetDisableASLR:"));
1139     if (packet.GetU32(0))
1140         m_process_launch_info.GetFlags().Set (eLaunchFlagDisableASLR);
1141     else
1142         m_process_launch_info.GetFlags().Clear (eLaunchFlagDisableASLR);
1143     return SendOKResponse ();
1144 }
1145 
1146 GDBRemoteCommunication::PacketResult
1147 GDBRemoteCommunicationServer::Handle_QSetDetachOnError (StringExtractorGDBRemote &packet)
1148 {
1149     packet.SetFilePos(::strlen ("QSetDetachOnError:"));
1150     if (packet.GetU32(0))
1151         m_process_launch_info.GetFlags().Set (eLaunchFlagDetachOnError);
1152     else
1153         m_process_launch_info.GetFlags().Clear (eLaunchFlagDetachOnError);
1154     return SendOKResponse ();
1155 }
1156 
1157 GDBRemoteCommunication::PacketResult
1158 GDBRemoteCommunicationServer::Handle_QSetWorkingDir (StringExtractorGDBRemote &packet)
1159 {
1160     packet.SetFilePos(::strlen ("QSetWorkingDir:"));
1161     std::string path;
1162     packet.GetHexByteString(path);
1163     if (m_is_platform)
1164     {
1165 #ifdef _WIN32
1166         // Not implemented on Windows
1167         return SendUnimplementedResponse("GDBRemoteCommunicationServer::Handle_QSetWorkingDir unimplemented");
1168 #else
1169         // If this packet is sent to a platform, then change the current working directory
1170         if (::chdir(path.c_str()) != 0)
1171             return SendErrorResponse(errno);
1172 #endif
1173     }
1174     else
1175     {
1176         m_process_launch_info.SwapWorkingDirectory (path);
1177     }
1178     return SendOKResponse ();
1179 }
1180 
1181 GDBRemoteCommunication::PacketResult
1182 GDBRemoteCommunicationServer::Handle_qGetWorkingDir (StringExtractorGDBRemote &packet)
1183 {
1184     StreamString response;
1185 
1186     if (m_is_platform)
1187     {
1188         // If this packet is sent to a platform, then change the current working directory
1189         char cwd[PATH_MAX];
1190         if (getcwd(cwd, sizeof(cwd)) == NULL)
1191         {
1192             return SendErrorResponse(errno);
1193         }
1194         else
1195         {
1196             response.PutBytesAsRawHex8(cwd, strlen(cwd));
1197             return SendPacketNoLock(response.GetData(), response.GetSize());
1198         }
1199     }
1200     else
1201     {
1202         const char *working_dir = m_process_launch_info.GetWorkingDirectory();
1203         if (working_dir && working_dir[0])
1204         {
1205             response.PutBytesAsRawHex8(working_dir, strlen(working_dir));
1206             return SendPacketNoLock(response.GetData(), response.GetSize());
1207         }
1208         else
1209         {
1210             return SendErrorResponse(14);
1211         }
1212     }
1213 }
1214 
1215 GDBRemoteCommunication::PacketResult
1216 GDBRemoteCommunicationServer::Handle_QSetSTDIN (StringExtractorGDBRemote &packet)
1217 {
1218     packet.SetFilePos(::strlen ("QSetSTDIN:"));
1219     ProcessLaunchInfo::FileAction file_action;
1220     std::string path;
1221     packet.GetHexByteString(path);
1222     const bool read = false;
1223     const bool write = true;
1224     if (file_action.Open(STDIN_FILENO, path.c_str(), read, write))
1225     {
1226         m_process_launch_info.AppendFileAction(file_action);
1227         return SendOKResponse ();
1228     }
1229     return SendErrorResponse (15);
1230 }
1231 
1232 GDBRemoteCommunication::PacketResult
1233 GDBRemoteCommunicationServer::Handle_QSetSTDOUT (StringExtractorGDBRemote &packet)
1234 {
1235     packet.SetFilePos(::strlen ("QSetSTDOUT:"));
1236     ProcessLaunchInfo::FileAction file_action;
1237     std::string path;
1238     packet.GetHexByteString(path);
1239     const bool read = true;
1240     const bool write = false;
1241     if (file_action.Open(STDOUT_FILENO, path.c_str(), read, write))
1242     {
1243         m_process_launch_info.AppendFileAction(file_action);
1244         return SendOKResponse ();
1245     }
1246     return SendErrorResponse (16);
1247 }
1248 
1249 GDBRemoteCommunication::PacketResult
1250 GDBRemoteCommunicationServer::Handle_QSetSTDERR (StringExtractorGDBRemote &packet)
1251 {
1252     packet.SetFilePos(::strlen ("QSetSTDERR:"));
1253     ProcessLaunchInfo::FileAction file_action;
1254     std::string path;
1255     packet.GetHexByteString(path);
1256     const bool read = true;
1257     const bool write = false;
1258     if (file_action.Open(STDERR_FILENO, path.c_str(), read, write))
1259     {
1260         m_process_launch_info.AppendFileAction(file_action);
1261         return SendOKResponse ();
1262     }
1263     return SendErrorResponse (17);
1264 }
1265 
1266 GDBRemoteCommunication::PacketResult
1267 GDBRemoteCommunicationServer::Handle_QStartNoAckMode (StringExtractorGDBRemote &packet)
1268 {
1269     // Send response first before changing m_send_acks to we ack this packet
1270     PacketResult packet_result = SendOKResponse ();
1271     m_send_acks = false;
1272     return packet_result;
1273 }
1274 
1275 GDBRemoteCommunication::PacketResult
1276 GDBRemoteCommunicationServer::Handle_qPlatform_mkdir (StringExtractorGDBRemote &packet)
1277 {
1278     packet.SetFilePos(::strlen("qPlatform_mkdir:"));
1279     mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX);
1280     if (packet.GetChar() == ',')
1281     {
1282         std::string path;
1283         packet.GetHexByteString(path);
1284         Error error = Host::MakeDirectory(path.c_str(),mode);
1285         if (error.Success())
1286             return SendPacketNoLock ("OK", 2);
1287         else
1288             return SendErrorResponse(error.GetError());
1289     }
1290     return SendErrorResponse(20);
1291 }
1292 
1293 GDBRemoteCommunication::PacketResult
1294 GDBRemoteCommunicationServer::Handle_qPlatform_chmod (StringExtractorGDBRemote &packet)
1295 {
1296     packet.SetFilePos(::strlen("qPlatform_chmod:"));
1297 
1298     mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX);
1299     if (packet.GetChar() == ',')
1300     {
1301         std::string path;
1302         packet.GetHexByteString(path);
1303         Error error = Host::SetFilePermissions (path.c_str(), mode);
1304         if (error.Success())
1305             return SendPacketNoLock ("OK", 2);
1306         else
1307             return SendErrorResponse(error.GetError());
1308     }
1309     return SendErrorResponse(19);
1310 }
1311 
1312 GDBRemoteCommunication::PacketResult
1313 GDBRemoteCommunicationServer::Handle_vFile_Open (StringExtractorGDBRemote &packet)
1314 {
1315     packet.SetFilePos(::strlen("vFile:open:"));
1316     std::string path;
1317     packet.GetHexByteStringTerminatedBy(path,',');
1318     if (!path.empty())
1319     {
1320         if (packet.GetChar() == ',')
1321         {
1322             uint32_t flags = packet.GetHexMaxU32(false, 0);
1323             if (packet.GetChar() == ',')
1324             {
1325                 mode_t mode = packet.GetHexMaxU32(false, 0600);
1326                 Error error;
1327                 int fd = ::open (path.c_str(), flags, mode);
1328                 const int save_errno = fd == -1 ? errno : 0;
1329                 StreamString response;
1330                 response.PutChar('F');
1331                 response.Printf("%i", fd);
1332                 if (save_errno)
1333                     response.Printf(",%i", save_errno);
1334                 return SendPacketNoLock(response.GetData(), response.GetSize());
1335             }
1336         }
1337     }
1338     return SendErrorResponse(18);
1339 }
1340 
1341 GDBRemoteCommunication::PacketResult
1342 GDBRemoteCommunicationServer::Handle_vFile_Close (StringExtractorGDBRemote &packet)
1343 {
1344     packet.SetFilePos(::strlen("vFile:close:"));
1345     int fd = packet.GetS32(-1);
1346     Error error;
1347     int err = -1;
1348     int save_errno = 0;
1349     if (fd >= 0)
1350     {
1351         err = close(fd);
1352         save_errno = err == -1 ? errno : 0;
1353     }
1354     else
1355     {
1356         save_errno = EINVAL;
1357     }
1358     StreamString response;
1359     response.PutChar('F');
1360     response.Printf("%i", err);
1361     if (save_errno)
1362         response.Printf(",%i", save_errno);
1363     return SendPacketNoLock(response.GetData(), response.GetSize());
1364 }
1365 
1366 GDBRemoteCommunication::PacketResult
1367 GDBRemoteCommunicationServer::Handle_vFile_pRead (StringExtractorGDBRemote &packet)
1368 {
1369 #ifdef _WIN32
1370     // Not implemented on Windows
1371     return SendUnimplementedResponse("GDBRemoteCommunicationServer::Handle_vFile_pRead() unimplemented");
1372 #else
1373     StreamGDBRemote response;
1374     packet.SetFilePos(::strlen("vFile:pread:"));
1375     int fd = packet.GetS32(-1);
1376     if (packet.GetChar() == ',')
1377     {
1378         uint64_t count = packet.GetU64(UINT64_MAX);
1379         if (packet.GetChar() == ',')
1380         {
1381             uint64_t offset = packet.GetU64(UINT32_MAX);
1382             if (count == UINT64_MAX)
1383             {
1384                 response.Printf("F-1:%i", EINVAL);
1385                 return SendPacketNoLock(response.GetData(), response.GetSize());
1386             }
1387 
1388             std::string buffer(count, 0);
1389             const ssize_t bytes_read = ::pread (fd, &buffer[0], buffer.size(), offset);
1390             const int save_errno = bytes_read == -1 ? errno : 0;
1391             response.PutChar('F');
1392             response.Printf("%zi", bytes_read);
1393             if (save_errno)
1394                 response.Printf(",%i", save_errno);
1395             else
1396             {
1397                 response.PutChar(';');
1398                 response.PutEscapedBytes(&buffer[0], bytes_read);
1399             }
1400             return SendPacketNoLock(response.GetData(), response.GetSize());
1401         }
1402     }
1403     return SendErrorResponse(21);
1404 
1405 #endif
1406 }
1407 
1408 GDBRemoteCommunication::PacketResult
1409 GDBRemoteCommunicationServer::Handle_vFile_pWrite (StringExtractorGDBRemote &packet)
1410 {
1411 #ifdef _WIN32
1412     return SendUnimplementedResponse("GDBRemoteCommunicationServer::Handle_vFile_pWrite() unimplemented");
1413 #else
1414     packet.SetFilePos(::strlen("vFile:pwrite:"));
1415 
1416     StreamGDBRemote response;
1417     response.PutChar('F');
1418 
1419     int fd = packet.GetU32(UINT32_MAX);
1420     if (packet.GetChar() == ',')
1421     {
1422         off_t offset = packet.GetU64(UINT32_MAX);
1423         if (packet.GetChar() == ',')
1424         {
1425             std::string buffer;
1426             if (packet.GetEscapedBinaryData(buffer))
1427             {
1428                 const ssize_t bytes_written = ::pwrite (fd, buffer.data(), buffer.size(), offset);
1429                 const int save_errno = bytes_written == -1 ? errno : 0;
1430                 response.Printf("%zi", bytes_written);
1431                 if (save_errno)
1432                     response.Printf(",%i", save_errno);
1433             }
1434             else
1435             {
1436                 response.Printf ("-1,%i", EINVAL);
1437             }
1438             return SendPacketNoLock(response.GetData(), response.GetSize());
1439         }
1440     }
1441     return SendErrorResponse(27);
1442 #endif
1443 }
1444 
1445 GDBRemoteCommunication::PacketResult
1446 GDBRemoteCommunicationServer::Handle_vFile_Size (StringExtractorGDBRemote &packet)
1447 {
1448     packet.SetFilePos(::strlen("vFile:size:"));
1449     std::string path;
1450     packet.GetHexByteString(path);
1451     if (!path.empty())
1452     {
1453         lldb::user_id_t retcode = Host::GetFileSize(FileSpec(path.c_str(), false));
1454         StreamString response;
1455         response.PutChar('F');
1456         response.PutHex64(retcode);
1457         if (retcode == UINT64_MAX)
1458         {
1459             response.PutChar(',');
1460             response.PutHex64(retcode); // TODO: replace with Host::GetSyswideErrorCode()
1461         }
1462         return SendPacketNoLock(response.GetData(), response.GetSize());
1463     }
1464     return SendErrorResponse(22);
1465 }
1466 
1467 GDBRemoteCommunication::PacketResult
1468 GDBRemoteCommunicationServer::Handle_vFile_Mode (StringExtractorGDBRemote &packet)
1469 {
1470     packet.SetFilePos(::strlen("vFile:mode:"));
1471     std::string path;
1472     packet.GetHexByteString(path);
1473     if (!path.empty())
1474     {
1475         Error error;
1476         const uint32_t mode = File::GetPermissions(path.c_str(), error);
1477         StreamString response;
1478         response.Printf("F%u", mode);
1479         if (mode == 0 || error.Fail())
1480             response.Printf(",%i", (int)error.GetError());
1481         return SendPacketNoLock(response.GetData(), response.GetSize());
1482     }
1483     return SendErrorResponse(23);
1484 }
1485 
1486 GDBRemoteCommunication::PacketResult
1487 GDBRemoteCommunicationServer::Handle_vFile_Exists (StringExtractorGDBRemote &packet)
1488 {
1489     packet.SetFilePos(::strlen("vFile:exists:"));
1490     std::string path;
1491     packet.GetHexByteString(path);
1492     if (!path.empty())
1493     {
1494         bool retcode = Host::GetFileExists(FileSpec(path.c_str(), false));
1495         StreamString response;
1496         response.PutChar('F');
1497         response.PutChar(',');
1498         if (retcode)
1499             response.PutChar('1');
1500         else
1501             response.PutChar('0');
1502         return SendPacketNoLock(response.GetData(), response.GetSize());
1503     }
1504     return SendErrorResponse(24);
1505 }
1506 
1507 GDBRemoteCommunication::PacketResult
1508 GDBRemoteCommunicationServer::Handle_vFile_symlink (StringExtractorGDBRemote &packet)
1509 {
1510     packet.SetFilePos(::strlen("vFile:symlink:"));
1511     std::string dst, src;
1512     packet.GetHexByteStringTerminatedBy(dst, ',');
1513     packet.GetChar(); // Skip ',' char
1514     packet.GetHexByteString(src);
1515     Error error = Host::Symlink(src.c_str(), dst.c_str());
1516     StreamString response;
1517     response.Printf("F%u,%u", error.GetError(), error.GetError());
1518     return SendPacketNoLock(response.GetData(), response.GetSize());
1519 }
1520 
1521 GDBRemoteCommunication::PacketResult
1522 GDBRemoteCommunicationServer::Handle_vFile_unlink (StringExtractorGDBRemote &packet)
1523 {
1524     packet.SetFilePos(::strlen("vFile:unlink:"));
1525     std::string path;
1526     packet.GetHexByteString(path);
1527     Error error = Host::Unlink(path.c_str());
1528     StreamString response;
1529     response.Printf("F%u,%u", error.GetError(), error.GetError());
1530     return SendPacketNoLock(response.GetData(), response.GetSize());
1531 }
1532 
1533 GDBRemoteCommunication::PacketResult
1534 GDBRemoteCommunicationServer::Handle_qPlatform_shell (StringExtractorGDBRemote &packet)
1535 {
1536     packet.SetFilePos(::strlen("qPlatform_shell:"));
1537     std::string path;
1538     std::string working_dir;
1539     packet.GetHexByteStringTerminatedBy(path,',');
1540     if (!path.empty())
1541     {
1542         if (packet.GetChar() == ',')
1543         {
1544             // FIXME: add timeout to qPlatform_shell packet
1545             // uint32_t timeout = packet.GetHexMaxU32(false, 32);
1546             uint32_t timeout = 10;
1547             if (packet.GetChar() == ',')
1548                 packet.GetHexByteString(working_dir);
1549             int status, signo;
1550             std::string output;
1551             Error err = Host::RunShellCommand(path.c_str(),
1552                                               working_dir.empty() ? NULL : working_dir.c_str(),
1553                                               &status, &signo, &output, timeout);
1554             StreamGDBRemote response;
1555             if (err.Fail())
1556             {
1557                 response.PutCString("F,");
1558                 response.PutHex32(UINT32_MAX);
1559             }
1560             else
1561             {
1562                 response.PutCString("F,");
1563                 response.PutHex32(status);
1564                 response.PutChar(',');
1565                 response.PutHex32(signo);
1566                 response.PutChar(',');
1567                 response.PutEscapedBytes(output.c_str(), output.size());
1568             }
1569             return SendPacketNoLock(response.GetData(), response.GetSize());
1570         }
1571     }
1572     return SendErrorResponse(24);
1573 }
1574 
1575 GDBRemoteCommunication::PacketResult
1576 GDBRemoteCommunicationServer::Handle_vFile_Stat (StringExtractorGDBRemote &packet)
1577 {
1578     return SendUnimplementedResponse("GDBRemoteCommunicationServer::Handle_vFile_Stat() unimplemented");
1579 }
1580 
1581 GDBRemoteCommunication::PacketResult
1582 GDBRemoteCommunicationServer::Handle_vFile_MD5 (StringExtractorGDBRemote &packet)
1583 {
1584     packet.SetFilePos(::strlen("vFile:MD5:"));
1585     std::string path;
1586     packet.GetHexByteString(path);
1587     if (!path.empty())
1588     {
1589         uint64_t a,b;
1590         StreamGDBRemote response;
1591         if (Host::CalculateMD5(FileSpec(path.c_str(),false),a,b) == false)
1592         {
1593             response.PutCString("F,");
1594             response.PutCString("x");
1595         }
1596         else
1597         {
1598             response.PutCString("F,");
1599             response.PutHex64(a);
1600             response.PutHex64(b);
1601         }
1602         return SendPacketNoLock(response.GetData(), response.GetSize());
1603     }
1604     return SendErrorResponse(25);
1605 }
1606 
1607