1 //===-- GDBRemoteCommunicationServerCommon.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 "GDBRemoteCommunicationServerCommon.h"
11 
12 #include <errno.h>
13 
14 // C Includes
15 // C++ Includes
16 #include <cstring>
17 #include <chrono>
18 
19 // Other libraries and framework includes
20 #include "llvm/ADT/Triple.h"
21 #include "lldb/Core/Log.h"
22 #include "lldb/Core/ModuleSpec.h"
23 #include "lldb/Core/StreamGDBRemote.h"
24 #include "lldb/Core/StreamString.h"
25 #include "lldb/Host/Config.h"
26 #include "lldb/Host/Endian.h"
27 #include "lldb/Host/File.h"
28 #include "lldb/Host/FileSystem.h"
29 #include "lldb/Host/Host.h"
30 #include "lldb/Host/HostInfo.h"
31 #include "lldb/Host/StringConvert.h"
32 #include "lldb/Interpreter/Args.h"
33 #include "lldb/Symbol/ObjectFile.h"
34 #include "lldb/Target/FileAction.h"
35 #include "lldb/Target/Platform.h"
36 #include "lldb/Target/Process.h"
37 
38 // Project includes
39 #include "ProcessGDBRemoteLog.h"
40 #include "Utility/StringExtractorGDBRemote.h"
41 
42 #ifdef __ANDROID__
43 #include "lldb/Host/android/HostInfoAndroid.h"
44 #endif
45 
46 using namespace lldb;
47 using namespace lldb_private;
48 using namespace lldb_private::process_gdb_remote;
49 
50 #ifdef __ANDROID__
51     const static uint32_t g_default_packet_timeout_sec = 20; // seconds
52 #else
53     const static uint32_t g_default_packet_timeout_sec = 0; // not specified
54 #endif
55 
56 //----------------------------------------------------------------------
57 // GDBRemoteCommunicationServerCommon constructor
58 //----------------------------------------------------------------------
59 GDBRemoteCommunicationServerCommon::GDBRemoteCommunicationServerCommon(const char *comm_name, const char *listener_name) :
60     GDBRemoteCommunicationServer (comm_name, listener_name),
61     m_spawned_pids (),
62     m_spawned_pids_mutex (Mutex::eMutexTypeRecursive),
63     m_process_launch_info (),
64     m_process_launch_error (),
65     m_proc_infos (),
66     m_proc_infos_index (0),
67     m_thread_suffix_supported (false),
68     m_list_threads_in_stop_reply (false)
69 {
70     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_A,
71                                   &GDBRemoteCommunicationServerCommon::Handle_A);
72     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QEnvironment,
73                                   &GDBRemoteCommunicationServerCommon::Handle_QEnvironment);
74     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QEnvironmentHexEncoded,
75                                   &GDBRemoteCommunicationServerCommon::Handle_QEnvironmentHexEncoded);
76     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qfProcessInfo,
77                                   &GDBRemoteCommunicationServerCommon::Handle_qfProcessInfo);
78     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qGroupName,
79                                   &GDBRemoteCommunicationServerCommon::Handle_qGroupName);
80     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qHostInfo,
81                                   &GDBRemoteCommunicationServerCommon::Handle_qHostInfo);
82     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qKillSpawnedProcess,
83                                   &GDBRemoteCommunicationServerCommon::Handle_qKillSpawnedProcess);
84     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QLaunchArch,
85                                   &GDBRemoteCommunicationServerCommon::Handle_QLaunchArch);
86     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qLaunchSuccess,
87                                   &GDBRemoteCommunicationServerCommon::Handle_qLaunchSuccess);
88     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QListThreadsInStopReply,
89                                   &GDBRemoteCommunicationServerCommon::Handle_QListThreadsInStopReply);
90     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qModuleInfo,
91                                   &GDBRemoteCommunicationServerCommon::Handle_qModuleInfo);
92     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qPlatform_chmod,
93                                   &GDBRemoteCommunicationServerCommon::Handle_qPlatform_chmod);
94     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qPlatform_mkdir,
95                                   &GDBRemoteCommunicationServerCommon::Handle_qPlatform_mkdir);
96     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qPlatform_shell,
97                                   &GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell);
98     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qProcessInfoPID,
99                                   &GDBRemoteCommunicationServerCommon::Handle_qProcessInfoPID);
100     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QSetDetachOnError,
101                                   &GDBRemoteCommunicationServerCommon::Handle_QSetDetachOnError);
102     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QSetSTDERR,
103                                   &GDBRemoteCommunicationServerCommon::Handle_QSetSTDERR);
104     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QSetSTDIN,
105                                   &GDBRemoteCommunicationServerCommon::Handle_QSetSTDIN);
106     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QSetSTDOUT,
107                                   &GDBRemoteCommunicationServerCommon::Handle_QSetSTDOUT);
108     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qSpeedTest,
109                                   &GDBRemoteCommunicationServerCommon::Handle_qSpeedTest);
110     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qsProcessInfo,
111                                   &GDBRemoteCommunicationServerCommon::Handle_qsProcessInfo);
112     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QStartNoAckMode,
113                                   &GDBRemoteCommunicationServerCommon::Handle_QStartNoAckMode);
114     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qSupported,
115                                   &GDBRemoteCommunicationServerCommon::Handle_qSupported);
116     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QThreadSuffixSupported,
117                                   &GDBRemoteCommunicationServerCommon::Handle_QThreadSuffixSupported);
118     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qUserName,
119                                   &GDBRemoteCommunicationServerCommon::Handle_qUserName);
120     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_close,
121                                   &GDBRemoteCommunicationServerCommon::Handle_vFile_Close);
122     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_exists,
123                                   &GDBRemoteCommunicationServerCommon::Handle_vFile_Exists);
124     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_md5,
125                                   &GDBRemoteCommunicationServerCommon::Handle_vFile_MD5);
126     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_mode,
127                                   &GDBRemoteCommunicationServerCommon::Handle_vFile_Mode);
128     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_open,
129                                   &GDBRemoteCommunicationServerCommon::Handle_vFile_Open);
130     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_pread,
131                                   &GDBRemoteCommunicationServerCommon::Handle_vFile_pRead);
132     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_pwrite,
133                                   &GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite);
134     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_size,
135                                   &GDBRemoteCommunicationServerCommon::Handle_vFile_Size);
136     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_stat,
137                                   &GDBRemoteCommunicationServerCommon::Handle_vFile_Stat);
138     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_symlink,
139                                   &GDBRemoteCommunicationServerCommon::Handle_vFile_symlink);
140     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vFile_unlink,
141                                   &GDBRemoteCommunicationServerCommon::Handle_vFile_unlink);
142 }
143 
144 //----------------------------------------------------------------------
145 // Destructor
146 //----------------------------------------------------------------------
147 GDBRemoteCommunicationServerCommon::~GDBRemoteCommunicationServerCommon()
148 {
149 }
150 
151 GDBRemoteCommunication::PacketResult
152 GDBRemoteCommunicationServerCommon::Handle_qHostInfo (StringExtractorGDBRemote &packet)
153 {
154     StreamString response;
155 
156     // $cputype:16777223;cpusubtype:3;ostype:Darwin;vendor:apple;endian:little;ptrsize:8;#00
157 
158     ArchSpec host_arch(HostInfo::GetArchitecture());
159     const llvm::Triple &host_triple = host_arch.GetTriple();
160     response.PutCString("triple:");
161     response.PutCStringAsRawHex8(host_triple.getTriple().c_str());
162     response.Printf (";ptrsize:%u;",host_arch.GetAddressByteSize());
163 
164     const char* distribution_id = host_arch.GetDistributionId ().AsCString ();
165     if (distribution_id)
166     {
167         response.PutCString("distribution_id:");
168         response.PutCStringAsRawHex8(distribution_id);
169         response.PutCString(";");
170     }
171 
172     // Only send out MachO info when lldb-platform/llgs is running on a MachO host.
173 #if defined(__APPLE__)
174     uint32_t cpu = host_arch.GetMachOCPUType();
175     uint32_t sub = host_arch.GetMachOCPUSubType();
176     if (cpu != LLDB_INVALID_CPUTYPE)
177         response.Printf ("cputype:%u;", cpu);
178     if (sub != LLDB_INVALID_CPUTYPE)
179         response.Printf ("cpusubtype:%u;", sub);
180 
181     if (cpu == ArchSpec::kCore_arm_any)
182         response.Printf("watchpoint_exceptions_received:before;");   // On armv7 we use "synchronous" watchpoints which means the exception is delivered before the instruction executes.
183     else
184         response.Printf("watchpoint_exceptions_received:after;");
185 #else
186     response.Printf("watchpoint_exceptions_received:after;");
187 #endif
188 
189     switch (lldb::endian::InlHostByteOrder())
190     {
191     case eByteOrderBig:     response.PutCString ("endian:big;"); break;
192     case eByteOrderLittle:  response.PutCString ("endian:little;"); break;
193     case eByteOrderPDP:     response.PutCString ("endian:pdp;"); break;
194     default:                response.PutCString ("endian:unknown;"); break;
195     }
196 
197     uint32_t major = UINT32_MAX;
198     uint32_t minor = UINT32_MAX;
199     uint32_t update = UINT32_MAX;
200     if (HostInfo::GetOSVersion(major, minor, update))
201     {
202         if (major != UINT32_MAX)
203         {
204             response.Printf("os_version:%u", major);
205             if (minor != UINT32_MAX)
206             {
207                 response.Printf(".%u", minor);
208                 if (update != UINT32_MAX)
209                     response.Printf(".%u", update);
210             }
211             response.PutChar(';');
212         }
213     }
214 
215     std::string s;
216     if (HostInfo::GetOSBuildString(s))
217     {
218         response.PutCString ("os_build:");
219         response.PutCStringAsRawHex8(s.c_str());
220         response.PutChar(';');
221     }
222     if (HostInfo::GetOSKernelDescription(s))
223     {
224         response.PutCString ("os_kernel:");
225         response.PutCStringAsRawHex8(s.c_str());
226         response.PutChar(';');
227     }
228 
229 #if defined(__APPLE__)
230 
231 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
232     // For iOS devices, we are connected through a USB Mux so we never pretend
233     // to actually have a hostname as far as the remote lldb that is connecting
234     // to this lldb-platform is concerned
235     response.PutCString ("hostname:");
236     response.PutCStringAsRawHex8("127.0.0.1");
237     response.PutChar(';');
238 #else   // #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
239     if (HostInfo::GetHostname(s))
240     {
241         response.PutCString ("hostname:");
242         response.PutCStringAsRawHex8(s.c_str());
243         response.PutChar(';');
244     }
245 #endif  // #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
246 
247 #else   // #if defined(__APPLE__)
248     if (HostInfo::GetHostname(s))
249     {
250         response.PutCString ("hostname:");
251         response.PutCStringAsRawHex8(s.c_str());
252         response.PutChar(';');
253     }
254 #endif  // #if defined(__APPLE__)
255 
256     if (g_default_packet_timeout_sec > 0)
257         response.Printf ("default_packet_timeout:%u;", g_default_packet_timeout_sec);
258 
259     return SendPacketNoLock (response.GetData(), response.GetSize());
260 }
261 
262 GDBRemoteCommunication::PacketResult
263 GDBRemoteCommunicationServerCommon::Handle_qProcessInfoPID (StringExtractorGDBRemote &packet)
264 {
265     // Packet format: "qProcessInfoPID:%i" where %i is the pid
266     packet.SetFilePos (::strlen ("qProcessInfoPID:"));
267     lldb::pid_t pid = packet.GetU32 (LLDB_INVALID_PROCESS_ID);
268     if (pid != LLDB_INVALID_PROCESS_ID)
269     {
270         ProcessInstanceInfo proc_info;
271         if (Host::GetProcessInfo (pid, proc_info))
272         {
273             StreamString response;
274             CreateProcessInfoResponse (proc_info, response);
275             return SendPacketNoLock (response.GetData(), response.GetSize());
276         }
277     }
278     return SendErrorResponse (1);
279 }
280 
281 GDBRemoteCommunication::PacketResult
282 GDBRemoteCommunicationServerCommon::Handle_qfProcessInfo (StringExtractorGDBRemote &packet)
283 {
284     m_proc_infos_index = 0;
285     m_proc_infos.Clear();
286 
287     ProcessInstanceInfoMatch match_info;
288     packet.SetFilePos(::strlen ("qfProcessInfo"));
289     if (packet.GetChar() == ':')
290     {
291 
292         std::string key;
293         std::string value;
294         while (packet.GetNameColonValue(key, value))
295         {
296             bool success = true;
297             if (key.compare("name") == 0)
298             {
299                 StringExtractor extractor;
300                 extractor.GetStringRef().swap(value);
301                 extractor.GetHexByteString (value);
302                 match_info.GetProcessInfo().GetExecutableFile().SetFile(value.c_str(), false);
303             }
304             else if (key.compare("name_match") == 0)
305             {
306                 if (value.compare("equals") == 0)
307                 {
308                     match_info.SetNameMatchType (eNameMatchEquals);
309                 }
310                 else if (value.compare("starts_with") == 0)
311                 {
312                     match_info.SetNameMatchType (eNameMatchStartsWith);
313                 }
314                 else if (value.compare("ends_with") == 0)
315                 {
316                     match_info.SetNameMatchType (eNameMatchEndsWith);
317                 }
318                 else if (value.compare("contains") == 0)
319                 {
320                     match_info.SetNameMatchType (eNameMatchContains);
321                 }
322                 else if (value.compare("regex") == 0)
323                 {
324                     match_info.SetNameMatchType (eNameMatchRegularExpression);
325                 }
326                 else
327                 {
328                     success = false;
329                 }
330             }
331             else if (key.compare("pid") == 0)
332             {
333                 match_info.GetProcessInfo().SetProcessID (StringConvert::ToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0, &success));
334             }
335             else if (key.compare("parent_pid") == 0)
336             {
337                 match_info.GetProcessInfo().SetParentProcessID (StringConvert::ToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0, &success));
338             }
339             else if (key.compare("uid") == 0)
340             {
341                 match_info.GetProcessInfo().SetUserID (StringConvert::ToUInt32(value.c_str(), UINT32_MAX, 0, &success));
342             }
343             else if (key.compare("gid") == 0)
344             {
345                 match_info.GetProcessInfo().SetGroupID (StringConvert::ToUInt32(value.c_str(), UINT32_MAX, 0, &success));
346             }
347             else if (key.compare("euid") == 0)
348             {
349                 match_info.GetProcessInfo().SetEffectiveUserID (StringConvert::ToUInt32(value.c_str(), UINT32_MAX, 0, &success));
350             }
351             else if (key.compare("egid") == 0)
352             {
353                 match_info.GetProcessInfo().SetEffectiveGroupID (StringConvert::ToUInt32(value.c_str(), UINT32_MAX, 0, &success));
354             }
355             else if (key.compare("all_users") == 0)
356             {
357                 match_info.SetMatchAllUsers(Args::StringToBoolean(value.c_str(), false, &success));
358             }
359             else if (key.compare("triple") == 0)
360             {
361                 match_info.GetProcessInfo().GetArchitecture().SetTriple (value.c_str(), NULL);
362             }
363             else
364             {
365                 success = false;
366             }
367 
368             if (!success)
369                 return SendErrorResponse (2);
370         }
371     }
372 
373     if (Host::FindProcesses (match_info, m_proc_infos))
374     {
375         // We found something, return the first item by calling the get
376         // subsequent process info packet handler...
377         return Handle_qsProcessInfo (packet);
378     }
379     return SendErrorResponse (3);
380 }
381 
382 GDBRemoteCommunication::PacketResult
383 GDBRemoteCommunicationServerCommon::Handle_qsProcessInfo (StringExtractorGDBRemote &packet)
384 {
385     if (m_proc_infos_index < m_proc_infos.GetSize())
386     {
387         StreamString response;
388         CreateProcessInfoResponse (m_proc_infos.GetProcessInfoAtIndex(m_proc_infos_index), response);
389         ++m_proc_infos_index;
390         return SendPacketNoLock (response.GetData(), response.GetSize());
391     }
392     return SendErrorResponse (4);
393 }
394 
395 GDBRemoteCommunication::PacketResult
396 GDBRemoteCommunicationServerCommon::Handle_qUserName (StringExtractorGDBRemote &packet)
397 {
398 #if !defined(LLDB_DISABLE_POSIX)
399     // Packet format: "qUserName:%i" where %i is the uid
400     packet.SetFilePos(::strlen ("qUserName:"));
401     uint32_t uid = packet.GetU32 (UINT32_MAX);
402     if (uid != UINT32_MAX)
403     {
404         std::string name;
405         if (HostInfo::LookupUserName(uid, name))
406         {
407             StreamString response;
408             response.PutCStringAsRawHex8 (name.c_str());
409             return SendPacketNoLock (response.GetData(), response.GetSize());
410         }
411     }
412 #endif
413     return SendErrorResponse (5);
414 
415 }
416 
417 GDBRemoteCommunication::PacketResult
418 GDBRemoteCommunicationServerCommon::Handle_qGroupName (StringExtractorGDBRemote &packet)
419 {
420 #if !defined(LLDB_DISABLE_POSIX)
421     // Packet format: "qGroupName:%i" where %i is the gid
422     packet.SetFilePos(::strlen ("qGroupName:"));
423     uint32_t gid = packet.GetU32 (UINT32_MAX);
424     if (gid != UINT32_MAX)
425     {
426         std::string name;
427         if (HostInfo::LookupGroupName(gid, name))
428         {
429             StreamString response;
430             response.PutCStringAsRawHex8 (name.c_str());
431             return SendPacketNoLock (response.GetData(), response.GetSize());
432         }
433     }
434 #endif
435     return SendErrorResponse (6);
436 }
437 
438 GDBRemoteCommunication::PacketResult
439 GDBRemoteCommunicationServerCommon::Handle_qSpeedTest (StringExtractorGDBRemote &packet)
440 {
441     packet.SetFilePos(::strlen ("qSpeedTest:"));
442 
443     std::string key;
444     std::string value;
445     bool success = packet.GetNameColonValue(key, value);
446     if (success && key.compare("response_size") == 0)
447     {
448         uint32_t response_size = StringConvert::ToUInt32(value.c_str(), 0, 0, &success);
449         if (success)
450         {
451             if (response_size == 0)
452                 return SendOKResponse();
453             StreamString response;
454             uint32_t bytes_left = response_size;
455             response.PutCString("data:");
456             while (bytes_left > 0)
457             {
458                 if (bytes_left >= 26)
459                 {
460                     response.PutCString("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
461                     bytes_left -= 26;
462                 }
463                 else
464                 {
465                     response.Printf ("%*.*s;", bytes_left, bytes_left, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
466                     bytes_left = 0;
467                 }
468             }
469             return SendPacketNoLock (response.GetData(), response.GetSize());
470         }
471     }
472     return SendErrorResponse (7);
473 }
474 
475 GDBRemoteCommunication::PacketResult
476 GDBRemoteCommunicationServerCommon::Handle_qKillSpawnedProcess (StringExtractorGDBRemote &packet)
477 {
478     packet.SetFilePos(::strlen ("qKillSpawnedProcess:"));
479 
480     lldb::pid_t pid = packet.GetU64(LLDB_INVALID_PROCESS_ID);
481 
482     // verify that we know anything about this pid.
483     // Scope for locker
484     {
485         Mutex::Locker locker (m_spawned_pids_mutex);
486         if (m_spawned_pids.find(pid) == m_spawned_pids.end())
487         {
488             // not a pid we know about
489             return SendErrorResponse (10);
490         }
491     }
492 
493     // go ahead and attempt to kill the spawned process
494     if (KillSpawnedProcess (pid))
495         return SendOKResponse ();
496     else
497         return SendErrorResponse (11);
498 }
499 
500 bool
501 GDBRemoteCommunicationServerCommon::KillSpawnedProcess (lldb::pid_t pid)
502 {
503     // make sure we know about this process
504     {
505         Mutex::Locker locker (m_spawned_pids_mutex);
506         if (m_spawned_pids.find(pid) == m_spawned_pids.end())
507             return false;
508     }
509 
510     // first try a SIGTERM (standard kill)
511     Host::Kill (pid, SIGTERM);
512 
513     // check if that worked
514     for (size_t i=0; i<10; ++i)
515     {
516         {
517             Mutex::Locker locker (m_spawned_pids_mutex);
518             if (m_spawned_pids.find(pid) == m_spawned_pids.end())
519             {
520                 // it is now killed
521                 return true;
522             }
523         }
524         usleep (10000);
525     }
526 
527     // check one more time after the final usleep
528     {
529         Mutex::Locker locker (m_spawned_pids_mutex);
530         if (m_spawned_pids.find(pid) == m_spawned_pids.end())
531             return true;
532     }
533 
534     // the launched process still lives.  Now try killing it again,
535     // this time with an unblockable signal.
536     Host::Kill (pid, SIGKILL);
537 
538     for (size_t i=0; i<10; ++i)
539     {
540         {
541             Mutex::Locker locker (m_spawned_pids_mutex);
542             if (m_spawned_pids.find(pid) == m_spawned_pids.end())
543             {
544                 // it is now killed
545                 return true;
546             }
547         }
548         usleep (10000);
549     }
550 
551     // check one more time after the final usleep
552     // Scope for locker
553     {
554         Mutex::Locker locker (m_spawned_pids_mutex);
555         if (m_spawned_pids.find(pid) == m_spawned_pids.end())
556             return true;
557     }
558 
559     // no luck - the process still lives
560     return false;
561 }
562 
563 GDBRemoteCommunication::PacketResult
564 GDBRemoteCommunicationServerCommon::Handle_vFile_Open (StringExtractorGDBRemote &packet)
565 {
566     packet.SetFilePos(::strlen("vFile:open:"));
567     std::string path;
568     packet.GetHexByteStringTerminatedBy(path,',');
569     if (!path.empty())
570     {
571         if (packet.GetChar() == ',')
572         {
573             uint32_t flags = File::ConvertOpenOptionsForPOSIXOpen(
574                 packet.GetHexMaxU32(false, 0));
575             if (packet.GetChar() == ',')
576             {
577                 mode_t mode = packet.GetHexMaxU32(false, 0600);
578                 Error error;
579                 const FileSpec path_spec(path.c_str(), true);
580                 int fd = ::open (path_spec.GetPath().c_str(), flags, mode);
581                 const int save_errno = fd == -1 ? errno : 0;
582                 StreamString response;
583                 response.PutChar('F');
584                 response.Printf("%i", fd);
585                 if (save_errno)
586                     response.Printf(",%i", save_errno);
587                 return SendPacketNoLock(response.GetData(), response.GetSize());
588             }
589         }
590     }
591     return SendErrorResponse(18);
592 }
593 
594 GDBRemoteCommunication::PacketResult
595 GDBRemoteCommunicationServerCommon::Handle_vFile_Close (StringExtractorGDBRemote &packet)
596 {
597     packet.SetFilePos(::strlen("vFile:close:"));
598     int fd = packet.GetS32(-1);
599     Error error;
600     int err = -1;
601     int save_errno = 0;
602     if (fd >= 0)
603     {
604         err = close(fd);
605         save_errno = err == -1 ? errno : 0;
606     }
607     else
608     {
609         save_errno = EINVAL;
610     }
611     StreamString response;
612     response.PutChar('F');
613     response.Printf("%i", err);
614     if (save_errno)
615         response.Printf(",%i", save_errno);
616     return SendPacketNoLock(response.GetData(), response.GetSize());
617 }
618 
619 GDBRemoteCommunication::PacketResult
620 GDBRemoteCommunicationServerCommon::Handle_vFile_pRead (StringExtractorGDBRemote &packet)
621 {
622 #ifdef _WIN32
623     // Not implemented on Windows
624     return SendUnimplementedResponse("GDBRemoteCommunicationServerCommon::Handle_vFile_pRead() unimplemented");
625 #else
626     StreamGDBRemote response;
627     packet.SetFilePos(::strlen("vFile:pread:"));
628     int fd = packet.GetS32(-1);
629     if (packet.GetChar() == ',')
630     {
631         uint64_t count = packet.GetU64(UINT64_MAX);
632         if (packet.GetChar() == ',')
633         {
634             uint64_t offset = packet.GetU64(UINT32_MAX);
635             if (count == UINT64_MAX)
636             {
637                 response.Printf("F-1:%i", EINVAL);
638                 return SendPacketNoLock(response.GetData(), response.GetSize());
639             }
640 
641             std::string buffer(count, 0);
642             const ssize_t bytes_read = ::pread (fd, &buffer[0], buffer.size(), offset);
643             const int save_errno = bytes_read == -1 ? errno : 0;
644             response.PutChar('F');
645             response.Printf("%zi", bytes_read);
646             if (save_errno)
647                 response.Printf(",%i", save_errno);
648             else
649             {
650                 response.PutChar(';');
651                 response.PutEscapedBytes(&buffer[0], bytes_read);
652             }
653             return SendPacketNoLock(response.GetData(), response.GetSize());
654         }
655     }
656     return SendErrorResponse(21);
657 
658 #endif
659 }
660 
661 GDBRemoteCommunication::PacketResult
662 GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite (StringExtractorGDBRemote &packet)
663 {
664 #ifdef _WIN32
665     return SendUnimplementedResponse("GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite() unimplemented");
666 #else
667     packet.SetFilePos(::strlen("vFile:pwrite:"));
668 
669     StreamGDBRemote response;
670     response.PutChar('F');
671 
672     int fd = packet.GetU32(UINT32_MAX);
673     if (packet.GetChar() == ',')
674     {
675         off_t offset = packet.GetU64(UINT32_MAX);
676         if (packet.GetChar() == ',')
677         {
678             std::string buffer;
679             if (packet.GetEscapedBinaryData(buffer))
680             {
681                 const ssize_t bytes_written = ::pwrite (fd, buffer.data(), buffer.size(), offset);
682                 const int save_errno = bytes_written == -1 ? errno : 0;
683                 response.Printf("%zi", bytes_written);
684                 if (save_errno)
685                     response.Printf(",%i", save_errno);
686             }
687             else
688             {
689                 response.Printf ("-1,%i", EINVAL);
690             }
691             return SendPacketNoLock(response.GetData(), response.GetSize());
692         }
693     }
694     return SendErrorResponse(27);
695 #endif
696 }
697 
698 GDBRemoteCommunication::PacketResult
699 GDBRemoteCommunicationServerCommon::Handle_vFile_Size (StringExtractorGDBRemote &packet)
700 {
701     packet.SetFilePos(::strlen("vFile:size:"));
702     std::string path;
703     packet.GetHexByteString(path);
704     if (!path.empty())
705     {
706         lldb::user_id_t retcode = FileSystem::GetFileSize(FileSpec(path.c_str(), false));
707         StreamString response;
708         response.PutChar('F');
709         response.PutHex64(retcode);
710         if (retcode == UINT64_MAX)
711         {
712             response.PutChar(',');
713             response.PutHex64(retcode); // TODO: replace with Host::GetSyswideErrorCode()
714         }
715         return SendPacketNoLock(response.GetData(), response.GetSize());
716     }
717     return SendErrorResponse(22);
718 }
719 
720 GDBRemoteCommunication::PacketResult
721 GDBRemoteCommunicationServerCommon::Handle_vFile_Mode (StringExtractorGDBRemote &packet)
722 {
723     packet.SetFilePos(::strlen("vFile:mode:"));
724     std::string path;
725     packet.GetHexByteString(path);
726     if (!path.empty())
727     {
728         Error error;
729         const uint32_t mode = File::GetPermissions(path.c_str(), error);
730         StreamString response;
731         response.Printf("F%u", mode);
732         if (mode == 0 || error.Fail())
733             response.Printf(",%i", (int)error.GetError());
734         return SendPacketNoLock(response.GetData(), response.GetSize());
735     }
736     return SendErrorResponse(23);
737 }
738 
739 GDBRemoteCommunication::PacketResult
740 GDBRemoteCommunicationServerCommon::Handle_vFile_Exists (StringExtractorGDBRemote &packet)
741 {
742     packet.SetFilePos(::strlen("vFile:exists:"));
743     std::string path;
744     packet.GetHexByteString(path);
745     if (!path.empty())
746     {
747         bool retcode = FileSystem::GetFileExists(FileSpec(path.c_str(), false));
748         StreamString response;
749         response.PutChar('F');
750         response.PutChar(',');
751         if (retcode)
752             response.PutChar('1');
753         else
754             response.PutChar('0');
755         return SendPacketNoLock(response.GetData(), response.GetSize());
756     }
757     return SendErrorResponse(24);
758 }
759 
760 GDBRemoteCommunication::PacketResult
761 GDBRemoteCommunicationServerCommon::Handle_vFile_symlink (StringExtractorGDBRemote &packet)
762 {
763     packet.SetFilePos(::strlen("vFile:symlink:"));
764     std::string dst, src;
765     packet.GetHexByteStringTerminatedBy(dst, ',');
766     packet.GetChar(); // Skip ',' char
767     packet.GetHexByteString(src);
768     Error error = FileSystem::Symlink(src.c_str(), dst.c_str());
769     StreamString response;
770     response.Printf("F%u,%u", error.GetError(), error.GetError());
771     return SendPacketNoLock(response.GetData(), response.GetSize());
772 }
773 
774 GDBRemoteCommunication::PacketResult
775 GDBRemoteCommunicationServerCommon::Handle_vFile_unlink (StringExtractorGDBRemote &packet)
776 {
777     packet.SetFilePos(::strlen("vFile:unlink:"));
778     std::string path;
779     packet.GetHexByteString(path);
780     Error error = FileSystem::Unlink(path.c_str());
781     StreamString response;
782     response.Printf("F%u,%u", error.GetError(), error.GetError());
783     return SendPacketNoLock(response.GetData(), response.GetSize());
784 }
785 
786 GDBRemoteCommunication::PacketResult
787 GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell (StringExtractorGDBRemote &packet)
788 {
789     packet.SetFilePos(::strlen("qPlatform_shell:"));
790     std::string path;
791     std::string working_dir;
792     packet.GetHexByteStringTerminatedBy(path,',');
793     if (!path.empty())
794     {
795         if (packet.GetChar() == ',')
796         {
797             // FIXME: add timeout to qPlatform_shell packet
798             // uint32_t timeout = packet.GetHexMaxU32(false, 32);
799             uint32_t timeout = 10;
800             if (packet.GetChar() == ',')
801                 packet.GetHexByteString(working_dir);
802             int status, signo;
803             std::string output;
804             Error err = Host::RunShellCommand(path.c_str(),
805                                               working_dir.empty() ? NULL : working_dir.c_str(),
806                                               &status, &signo, &output, timeout);
807             StreamGDBRemote response;
808             if (err.Fail())
809             {
810                 response.PutCString("F,");
811                 response.PutHex32(UINT32_MAX);
812             }
813             else
814             {
815                 response.PutCString("F,");
816                 response.PutHex32(status);
817                 response.PutChar(',');
818                 response.PutHex32(signo);
819                 response.PutChar(',');
820                 response.PutEscapedBytes(output.c_str(), output.size());
821             }
822             return SendPacketNoLock(response.GetData(), response.GetSize());
823         }
824     }
825     return SendErrorResponse(24);
826 }
827 
828 
829 GDBRemoteCommunication::PacketResult
830 GDBRemoteCommunicationServerCommon::Handle_vFile_Stat (StringExtractorGDBRemote &packet)
831 {
832     return SendUnimplementedResponse("GDBRemoteCommunicationServerCommon::Handle_vFile_Stat() unimplemented");
833 }
834 
835 GDBRemoteCommunication::PacketResult
836 GDBRemoteCommunicationServerCommon::Handle_vFile_MD5 (StringExtractorGDBRemote &packet)
837 {
838     packet.SetFilePos(::strlen("vFile:MD5:"));
839     std::string path;
840     packet.GetHexByteString(path);
841     if (!path.empty())
842     {
843         uint64_t a,b;
844         StreamGDBRemote response;
845         if (!FileSystem::CalculateMD5(FileSpec(path.c_str(), false), a, b))
846         {
847             response.PutCString("F,");
848             response.PutCString("x");
849         }
850         else
851         {
852             response.PutCString("F,");
853             response.PutHex64(a);
854             response.PutHex64(b);
855         }
856         return SendPacketNoLock(response.GetData(), response.GetSize());
857     }
858     return SendErrorResponse(25);
859 }
860 
861 GDBRemoteCommunication::PacketResult
862 GDBRemoteCommunicationServerCommon::Handle_qPlatform_mkdir (StringExtractorGDBRemote &packet)
863 {
864     packet.SetFilePos(::strlen("qPlatform_mkdir:"));
865     mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX);
866     if (packet.GetChar() == ',')
867     {
868         std::string path;
869         packet.GetHexByteString(path);
870         Error error = FileSystem::MakeDirectory(path.c_str(), mode);
871 
872         StreamGDBRemote response;
873         response.Printf("F%u", error.GetError());
874 
875         return SendPacketNoLock(response.GetData(), response.GetSize());
876     }
877     return SendErrorResponse(20);
878 }
879 
880 GDBRemoteCommunication::PacketResult
881 GDBRemoteCommunicationServerCommon::Handle_qPlatform_chmod (StringExtractorGDBRemote &packet)
882 {
883     packet.SetFilePos(::strlen("qPlatform_chmod:"));
884 
885     mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX);
886     if (packet.GetChar() == ',')
887     {
888         std::string path;
889         packet.GetHexByteString(path);
890         Error error = FileSystem::SetFilePermissions(path.c_str(), mode);
891 
892         StreamGDBRemote response;
893         response.Printf("F%u", error.GetError());
894 
895         return SendPacketNoLock(response.GetData(), response.GetSize());
896     }
897     return SendErrorResponse(19);
898 }
899 
900 GDBRemoteCommunication::PacketResult
901 GDBRemoteCommunicationServerCommon::Handle_qSupported (StringExtractorGDBRemote &packet)
902 {
903     StreamGDBRemote response;
904 
905     // Features common to lldb-platform and llgs.
906     uint32_t max_packet_size = 128 * 1024;  // 128KBytes is a reasonable max packet size--debugger can always use less
907     response.Printf ("PacketSize=%x", max_packet_size);
908 
909     response.PutCString (";QStartNoAckMode+");
910     response.PutCString (";QThreadSuffixSupported+");
911     response.PutCString (";QListThreadsInStopReply+");
912 #if defined(__linux__)
913     response.PutCString (";qXfer:auxv:read+");
914 #endif
915 
916     return SendPacketNoLock(response.GetData(), response.GetSize());
917 }
918 
919 GDBRemoteCommunication::PacketResult
920 GDBRemoteCommunicationServerCommon::Handle_QThreadSuffixSupported (StringExtractorGDBRemote &packet)
921 {
922     m_thread_suffix_supported = true;
923     return SendOKResponse();
924 }
925 
926 GDBRemoteCommunication::PacketResult
927 GDBRemoteCommunicationServerCommon::Handle_QListThreadsInStopReply (StringExtractorGDBRemote &packet)
928 {
929     m_list_threads_in_stop_reply = true;
930     return SendOKResponse();
931 }
932 
933 GDBRemoteCommunication::PacketResult
934 GDBRemoteCommunicationServerCommon::Handle_QSetDetachOnError (StringExtractorGDBRemote &packet)
935 {
936     packet.SetFilePos(::strlen ("QSetDetachOnError:"));
937     if (packet.GetU32(0))
938         m_process_launch_info.GetFlags().Set (eLaunchFlagDetachOnError);
939     else
940         m_process_launch_info.GetFlags().Clear (eLaunchFlagDetachOnError);
941     return SendOKResponse ();
942 }
943 
944 GDBRemoteCommunication::PacketResult
945 GDBRemoteCommunicationServerCommon::Handle_QStartNoAckMode (StringExtractorGDBRemote &packet)
946 {
947     // Send response first before changing m_send_acks to we ack this packet
948     PacketResult packet_result = SendOKResponse ();
949     m_send_acks = false;
950     return packet_result;
951 }
952 
953 GDBRemoteCommunication::PacketResult
954 GDBRemoteCommunicationServerCommon::Handle_QSetSTDIN (StringExtractorGDBRemote &packet)
955 {
956     packet.SetFilePos(::strlen ("QSetSTDIN:"));
957     FileAction file_action;
958     std::string path;
959     packet.GetHexByteString(path);
960     const bool read = false;
961     const bool write = true;
962     if (file_action.Open(STDIN_FILENO, path.c_str(), read, write))
963     {
964         m_process_launch_info.AppendFileAction(file_action);
965         return SendOKResponse ();
966     }
967     return SendErrorResponse (15);
968 }
969 
970 GDBRemoteCommunication::PacketResult
971 GDBRemoteCommunicationServerCommon::Handle_QSetSTDOUT (StringExtractorGDBRemote &packet)
972 {
973     packet.SetFilePos(::strlen ("QSetSTDOUT:"));
974     FileAction file_action;
975     std::string path;
976     packet.GetHexByteString(path);
977     const bool read = true;
978     const bool write = false;
979     if (file_action.Open(STDOUT_FILENO, path.c_str(), read, write))
980     {
981         m_process_launch_info.AppendFileAction(file_action);
982         return SendOKResponse ();
983     }
984     return SendErrorResponse (16);
985 }
986 
987 GDBRemoteCommunication::PacketResult
988 GDBRemoteCommunicationServerCommon::Handle_QSetSTDERR (StringExtractorGDBRemote &packet)
989 {
990     packet.SetFilePos(::strlen ("QSetSTDERR:"));
991     FileAction file_action;
992     std::string path;
993     packet.GetHexByteString(path);
994     const bool read = true;
995     const bool write = false;
996     if (file_action.Open(STDERR_FILENO, path.c_str(), read, write))
997     {
998         m_process_launch_info.AppendFileAction(file_action);
999         return SendOKResponse ();
1000     }
1001     return SendErrorResponse (17);
1002 }
1003 
1004 GDBRemoteCommunication::PacketResult
1005 GDBRemoteCommunicationServerCommon::Handle_qLaunchSuccess (StringExtractorGDBRemote &packet)
1006 {
1007     if (m_process_launch_error.Success())
1008         return SendOKResponse();
1009     StreamString response;
1010     response.PutChar('E');
1011     response.PutCString(m_process_launch_error.AsCString("<unknown error>"));
1012     return SendPacketNoLock (response.GetData(), response.GetSize());
1013 }
1014 
1015 GDBRemoteCommunication::PacketResult
1016 GDBRemoteCommunicationServerCommon::Handle_QEnvironment (StringExtractorGDBRemote &packet)
1017 {
1018     packet.SetFilePos(::strlen ("QEnvironment:"));
1019     const uint32_t bytes_left = packet.GetBytesLeft();
1020     if (bytes_left > 0)
1021     {
1022         m_process_launch_info.GetEnvironmentEntries ().AppendArgument (packet.Peek());
1023         return SendOKResponse ();
1024     }
1025     return SendErrorResponse (12);
1026 }
1027 
1028 GDBRemoteCommunication::PacketResult
1029 GDBRemoteCommunicationServerCommon::Handle_QEnvironmentHexEncoded (StringExtractorGDBRemote &packet)
1030 {
1031     packet.SetFilePos(::strlen("QEnvironmentHexEncoded:"));
1032     const uint32_t bytes_left = packet.GetBytesLeft();
1033     if (bytes_left > 0)
1034     {
1035         std::string str;
1036         packet.GetHexByteString(str);
1037         m_process_launch_info.GetEnvironmentEntries().AppendArgument(str.c_str());
1038         return SendOKResponse();
1039     }
1040     return SendErrorResponse(12);
1041 }
1042 
1043 GDBRemoteCommunication::PacketResult
1044 GDBRemoteCommunicationServerCommon::Handle_QLaunchArch (StringExtractorGDBRemote &packet)
1045 {
1046     packet.SetFilePos(::strlen ("QLaunchArch:"));
1047     const uint32_t bytes_left = packet.GetBytesLeft();
1048     if (bytes_left > 0)
1049     {
1050         const char* arch_triple = packet.Peek();
1051         ArchSpec arch_spec(arch_triple,NULL);
1052         m_process_launch_info.SetArchitecture(arch_spec);
1053         return SendOKResponse();
1054     }
1055     return SendErrorResponse(13);
1056 }
1057 
1058 GDBRemoteCommunication::PacketResult
1059 GDBRemoteCommunicationServerCommon::Handle_A (StringExtractorGDBRemote &packet)
1060 {
1061     // The 'A' packet is the most over designed packet ever here with
1062     // redundant argument indexes, redundant argument lengths and needed hex
1063     // encoded argument string values. Really all that is needed is a comma
1064     // separated hex encoded argument value list, but we will stay true to the
1065     // documented version of the 'A' packet here...
1066 
1067     Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1068     int actual_arg_index = 0;
1069 
1070     packet.SetFilePos(1); // Skip the 'A'
1071     bool success = true;
1072     while (success && packet.GetBytesLeft() > 0)
1073     {
1074         // Decode the decimal argument string length. This length is the
1075         // number of hex nibbles in the argument string value.
1076         const uint32_t arg_len = packet.GetU32(UINT32_MAX);
1077         if (arg_len == UINT32_MAX)
1078             success = false;
1079         else
1080         {
1081             // Make sure the argument hex string length is followed by a comma
1082             if (packet.GetChar() != ',')
1083                 success = false;
1084             else
1085             {
1086                 // Decode the argument index. We ignore this really because
1087                 // who would really send down the arguments in a random order???
1088                 const uint32_t arg_idx = packet.GetU32(UINT32_MAX);
1089                 if (arg_idx == UINT32_MAX)
1090                     success = false;
1091                 else
1092                 {
1093                     // Make sure the argument index is followed by a comma
1094                     if (packet.GetChar() != ',')
1095                         success = false;
1096                     else
1097                     {
1098                         // Decode the argument string value from hex bytes
1099                         // back into a UTF8 string and make sure the length
1100                         // matches the one supplied in the packet
1101                         std::string arg;
1102                         if (packet.GetHexByteStringFixedLength(arg, arg_len) != (arg_len / 2))
1103                             success = false;
1104                         else
1105                         {
1106                             // If there are any bytes left
1107                             if (packet.GetBytesLeft())
1108                             {
1109                                 if (packet.GetChar() != ',')
1110                                     success = false;
1111                             }
1112 
1113                             if (success)
1114                             {
1115                                 if (arg_idx == 0)
1116                                     m_process_launch_info.GetExecutableFile().SetFile(arg.c_str(), false);
1117                                 m_process_launch_info.GetArguments().AppendArgument(arg.c_str());
1118                                 if (log)
1119                                     log->Printf ("LLGSPacketHandler::%s added arg %d: \"%s\"", __FUNCTION__, actual_arg_index, arg.c_str ());
1120                                 ++actual_arg_index;
1121                             }
1122                         }
1123                     }
1124                 }
1125             }
1126         }
1127     }
1128 
1129     if (success)
1130     {
1131         m_process_launch_error = LaunchProcess ();
1132         if (m_process_launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
1133         {
1134             return SendOKResponse ();
1135         }
1136         else
1137         {
1138             Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1139             if (log)
1140                 log->Printf("LLGSPacketHandler::%s failed to launch exe: %s",
1141                         __FUNCTION__,
1142                         m_process_launch_error.AsCString());
1143 
1144         }
1145     }
1146     return SendErrorResponse (8);
1147 }
1148 
1149 GDBRemoteCommunication::PacketResult
1150 GDBRemoteCommunicationServerCommon::Handle_qModuleInfo (StringExtractorGDBRemote &packet)
1151 {
1152     packet.SetFilePos(::strlen ("qModuleInfo:"));
1153 
1154     std::string module_path;
1155     packet.GetHexByteStringTerminatedBy(module_path, ';');
1156     if (module_path.empty())
1157         return SendErrorResponse (1);
1158 
1159     if (packet.GetChar() != ';')
1160         return SendErrorResponse (2);
1161 
1162     std::string triple;
1163     packet.GetHexByteString(triple);
1164     ArchSpec arch(triple.c_str());
1165 
1166     const FileSpec req_module_path_spec(module_path.c_str(), true);
1167     const FileSpec module_path_spec = FindModuleFile(req_module_path_spec.GetPath(), arch);
1168     const ModuleSpec module_spec(module_path_spec, arch);
1169 
1170     ModuleSpecList module_specs;
1171     if (!ObjectFile::GetModuleSpecifications(module_path_spec, 0, 0, module_specs))
1172         return SendErrorResponse (3);
1173 
1174     ModuleSpec matched_module_spec;
1175     if (!module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec))
1176         return SendErrorResponse (4);
1177 
1178     const auto file_offset = matched_module_spec.GetObjectOffset();
1179     const auto file_size = matched_module_spec.GetObjectSize();
1180     const auto uuid_str = matched_module_spec.GetUUID().GetAsString("");
1181 
1182     StreamGDBRemote response;
1183 
1184     if (uuid_str.empty())
1185     {
1186         std::string md5_hash;
1187         if (!FileSystem::CalculateMD5AsString(matched_module_spec.GetFileSpec(), file_offset, file_size, md5_hash))
1188             return SendErrorResponse (5);
1189         response.PutCString ("md5:");
1190         response.PutCStringAsRawHex8(md5_hash.c_str());
1191     }
1192     else{
1193         response.PutCString ("uuid:");
1194         response.PutCStringAsRawHex8(uuid_str.c_str());
1195     }
1196     response.PutChar(';');
1197 
1198     const auto &module_arch = matched_module_spec.GetArchitecture();
1199     response.PutCString("triple:");
1200     response.PutCStringAsRawHex8( module_arch.GetTriple().getTriple().c_str());
1201     response.PutChar(';');
1202 
1203     response.PutCString("file_path:");
1204     response.PutCStringAsRawHex8(module_path_spec.GetPath().c_str());
1205     response.PutChar(';');
1206     response.PutCString("file_offset:");
1207     response.PutHex64(file_offset);
1208     response.PutChar(';');
1209     response.PutCString("file_size:");
1210     response.PutHex64(file_size);
1211     response.PutChar(';');
1212 
1213     return SendPacketNoLock(response.GetData(), response.GetSize());
1214 }
1215 
1216 void
1217 GDBRemoteCommunicationServerCommon::CreateProcessInfoResponse (const ProcessInstanceInfo &proc_info,
1218                                                     StreamString &response)
1219 {
1220     response.Printf ("pid:%" PRIu64 ";ppid:%" PRIu64 ";uid:%i;gid:%i;euid:%i;egid:%i;",
1221                      proc_info.GetProcessID(),
1222                      proc_info.GetParentProcessID(),
1223                      proc_info.GetUserID(),
1224                      proc_info.GetGroupID(),
1225                      proc_info.GetEffectiveUserID(),
1226                      proc_info.GetEffectiveGroupID());
1227     response.PutCString ("name:");
1228     response.PutCStringAsRawHex8(proc_info.GetExecutableFile().GetPath().c_str());
1229     response.PutChar(';');
1230     const ArchSpec &proc_arch = proc_info.GetArchitecture();
1231     if (proc_arch.IsValid())
1232     {
1233         const llvm::Triple &proc_triple = proc_arch.GetTriple();
1234         response.PutCString("triple:");
1235         response.PutCStringAsRawHex8(proc_triple.getTriple().c_str());
1236         response.PutChar(';');
1237     }
1238 }
1239 
1240 void
1241 GDBRemoteCommunicationServerCommon::CreateProcessInfoResponse_DebugServerStyle (
1242     const ProcessInstanceInfo &proc_info, StreamString &response)
1243 {
1244     response.Printf ("pid:%" PRIx64 ";parent-pid:%" PRIx64 ";real-uid:%x;real-gid:%x;effective-uid:%x;effective-gid:%x;",
1245                      proc_info.GetProcessID(),
1246                      proc_info.GetParentProcessID(),
1247                      proc_info.GetUserID(),
1248                      proc_info.GetGroupID(),
1249                      proc_info.GetEffectiveUserID(),
1250                      proc_info.GetEffectiveGroupID());
1251 
1252     const ArchSpec &proc_arch = proc_info.GetArchitecture();
1253     if (proc_arch.IsValid())
1254     {
1255         const llvm::Triple &proc_triple = proc_arch.GetTriple();
1256 #if defined(__APPLE__)
1257         // We'll send cputype/cpusubtype.
1258         const uint32_t cpu_type = proc_arch.GetMachOCPUType();
1259         if (cpu_type != 0)
1260             response.Printf ("cputype:%" PRIx32 ";", cpu_type);
1261 
1262         const uint32_t cpu_subtype = proc_arch.GetMachOCPUSubType();
1263         if (cpu_subtype != 0)
1264             response.Printf ("cpusubtype:%" PRIx32 ";", cpu_subtype);
1265 
1266         const std::string vendor = proc_triple.getVendorName ();
1267         if (!vendor.empty ())
1268             response.Printf ("vendor:%s;", vendor.c_str ());
1269 #else
1270         // We'll send the triple.
1271         response.PutCString("triple:");
1272         response.PutCStringAsRawHex8(proc_triple.getTriple().c_str());
1273         response.PutChar(';');
1274 #endif
1275         std::string ostype = proc_triple.getOSName ();
1276         // Adjust so ostype reports ios for Apple/ARM and Apple/ARM64.
1277         if (proc_triple.getVendor () == llvm::Triple::Apple)
1278         {
1279             switch (proc_triple.getArch ())
1280             {
1281                 case llvm::Triple::arm:
1282                 case llvm::Triple::aarch64:
1283                     ostype = "ios";
1284                     break;
1285                 default:
1286                     // No change.
1287                     break;
1288             }
1289         }
1290         response.Printf ("ostype:%s;", ostype.c_str ());
1291 
1292 
1293         switch (proc_arch.GetByteOrder ())
1294         {
1295             case lldb::eByteOrderLittle: response.PutCString ("endian:little;"); break;
1296             case lldb::eByteOrderBig:    response.PutCString ("endian:big;");    break;
1297             case lldb::eByteOrderPDP:    response.PutCString ("endian:pdp;");    break;
1298             default:
1299                 // Nothing.
1300                 break;
1301         }
1302 
1303         if (proc_triple.isArch64Bit ())
1304             response.PutCString ("ptrsize:8;");
1305         else if (proc_triple.isArch32Bit ())
1306             response.PutCString ("ptrsize:4;");
1307         else if (proc_triple.isArch16Bit ())
1308             response.PutCString ("ptrsize:2;");
1309     }
1310 }
1311 
1312 FileSpec
1313 GDBRemoteCommunicationServerCommon::FindModuleFile(const std::string& module_path,
1314                                                    const ArchSpec& arch)
1315 {
1316 #ifdef __ANDROID__
1317     return HostInfoAndroid::ResolveLibraryPath(module_path, arch);
1318 #else
1319     return FileSpec(module_path.c_str(), true);
1320 #endif
1321 }
1322