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                 int fd = ::open (path.c_str(), flags, mode);
580                 const int save_errno = fd == -1 ? errno : 0;
581                 StreamString response;
582                 response.PutChar('F');
583                 response.Printf("%i", fd);
584                 if (save_errno)
585                     response.Printf(",%i", save_errno);
586                 return SendPacketNoLock(response.GetData(), response.GetSize());
587             }
588         }
589     }
590     return SendErrorResponse(18);
591 }
592 
593 GDBRemoteCommunication::PacketResult
594 GDBRemoteCommunicationServerCommon::Handle_vFile_Close (StringExtractorGDBRemote &packet)
595 {
596     packet.SetFilePos(::strlen("vFile:close:"));
597     int fd = packet.GetS32(-1);
598     Error error;
599     int err = -1;
600     int save_errno = 0;
601     if (fd >= 0)
602     {
603         err = close(fd);
604         save_errno = err == -1 ? errno : 0;
605     }
606     else
607     {
608         save_errno = EINVAL;
609     }
610     StreamString response;
611     response.PutChar('F');
612     response.Printf("%i", err);
613     if (save_errno)
614         response.Printf(",%i", save_errno);
615     return SendPacketNoLock(response.GetData(), response.GetSize());
616 }
617 
618 GDBRemoteCommunication::PacketResult
619 GDBRemoteCommunicationServerCommon::Handle_vFile_pRead (StringExtractorGDBRemote &packet)
620 {
621 #ifdef _WIN32
622     // Not implemented on Windows
623     return SendUnimplementedResponse("GDBRemoteCommunicationServerCommon::Handle_vFile_pRead() unimplemented");
624 #else
625     StreamGDBRemote response;
626     packet.SetFilePos(::strlen("vFile:pread:"));
627     int fd = packet.GetS32(-1);
628     if (packet.GetChar() == ',')
629     {
630         uint64_t count = packet.GetU64(UINT64_MAX);
631         if (packet.GetChar() == ',')
632         {
633             uint64_t offset = packet.GetU64(UINT32_MAX);
634             if (count == UINT64_MAX)
635             {
636                 response.Printf("F-1:%i", EINVAL);
637                 return SendPacketNoLock(response.GetData(), response.GetSize());
638             }
639 
640             std::string buffer(count, 0);
641             const ssize_t bytes_read = ::pread (fd, &buffer[0], buffer.size(), offset);
642             const int save_errno = bytes_read == -1 ? errno : 0;
643             response.PutChar('F');
644             response.Printf("%zi", bytes_read);
645             if (save_errno)
646                 response.Printf(",%i", save_errno);
647             else
648             {
649                 response.PutChar(';');
650                 response.PutEscapedBytes(&buffer[0], bytes_read);
651             }
652             return SendPacketNoLock(response.GetData(), response.GetSize());
653         }
654     }
655     return SendErrorResponse(21);
656 
657 #endif
658 }
659 
660 GDBRemoteCommunication::PacketResult
661 GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite (StringExtractorGDBRemote &packet)
662 {
663 #ifdef _WIN32
664     return SendUnimplementedResponse("GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite() unimplemented");
665 #else
666     packet.SetFilePos(::strlen("vFile:pwrite:"));
667 
668     StreamGDBRemote response;
669     response.PutChar('F');
670 
671     int fd = packet.GetU32(UINT32_MAX);
672     if (packet.GetChar() == ',')
673     {
674         off_t offset = packet.GetU64(UINT32_MAX);
675         if (packet.GetChar() == ',')
676         {
677             std::string buffer;
678             if (packet.GetEscapedBinaryData(buffer))
679             {
680                 const ssize_t bytes_written = ::pwrite (fd, buffer.data(), buffer.size(), offset);
681                 const int save_errno = bytes_written == -1 ? errno : 0;
682                 response.Printf("%zi", bytes_written);
683                 if (save_errno)
684                     response.Printf(",%i", save_errno);
685             }
686             else
687             {
688                 response.Printf ("-1,%i", EINVAL);
689             }
690             return SendPacketNoLock(response.GetData(), response.GetSize());
691         }
692     }
693     return SendErrorResponse(27);
694 #endif
695 }
696 
697 GDBRemoteCommunication::PacketResult
698 GDBRemoteCommunicationServerCommon::Handle_vFile_Size (StringExtractorGDBRemote &packet)
699 {
700     packet.SetFilePos(::strlen("vFile:size:"));
701     std::string path;
702     packet.GetHexByteString(path);
703     if (!path.empty())
704     {
705         lldb::user_id_t retcode = FileSystem::GetFileSize(FileSpec(path.c_str(), false));
706         StreamString response;
707         response.PutChar('F');
708         response.PutHex64(retcode);
709         if (retcode == UINT64_MAX)
710         {
711             response.PutChar(',');
712             response.PutHex64(retcode); // TODO: replace with Host::GetSyswideErrorCode()
713         }
714         return SendPacketNoLock(response.GetData(), response.GetSize());
715     }
716     return SendErrorResponse(22);
717 }
718 
719 GDBRemoteCommunication::PacketResult
720 GDBRemoteCommunicationServerCommon::Handle_vFile_Mode (StringExtractorGDBRemote &packet)
721 {
722     packet.SetFilePos(::strlen("vFile:mode:"));
723     std::string path;
724     packet.GetHexByteString(path);
725     if (!path.empty())
726     {
727         Error error;
728         const uint32_t mode = File::GetPermissions(path.c_str(), error);
729         StreamString response;
730         response.Printf("F%u", mode);
731         if (mode == 0 || error.Fail())
732             response.Printf(",%i", (int)error.GetError());
733         return SendPacketNoLock(response.GetData(), response.GetSize());
734     }
735     return SendErrorResponse(23);
736 }
737 
738 GDBRemoteCommunication::PacketResult
739 GDBRemoteCommunicationServerCommon::Handle_vFile_Exists (StringExtractorGDBRemote &packet)
740 {
741     packet.SetFilePos(::strlen("vFile:exists:"));
742     std::string path;
743     packet.GetHexByteString(path);
744     if (!path.empty())
745     {
746         bool retcode = FileSystem::GetFileExists(FileSpec(path.c_str(), false));
747         StreamString response;
748         response.PutChar('F');
749         response.PutChar(',');
750         if (retcode)
751             response.PutChar('1');
752         else
753             response.PutChar('0');
754         return SendPacketNoLock(response.GetData(), response.GetSize());
755     }
756     return SendErrorResponse(24);
757 }
758 
759 GDBRemoteCommunication::PacketResult
760 GDBRemoteCommunicationServerCommon::Handle_vFile_symlink (StringExtractorGDBRemote &packet)
761 {
762     packet.SetFilePos(::strlen("vFile:symlink:"));
763     std::string dst, src;
764     packet.GetHexByteStringTerminatedBy(dst, ',');
765     packet.GetChar(); // Skip ',' char
766     packet.GetHexByteString(src);
767     Error error = FileSystem::Symlink(src.c_str(), dst.c_str());
768     StreamString response;
769     response.Printf("F%u,%u", error.GetError(), error.GetError());
770     return SendPacketNoLock(response.GetData(), response.GetSize());
771 }
772 
773 GDBRemoteCommunication::PacketResult
774 GDBRemoteCommunicationServerCommon::Handle_vFile_unlink (StringExtractorGDBRemote &packet)
775 {
776     packet.SetFilePos(::strlen("vFile:unlink:"));
777     std::string path;
778     packet.GetHexByteString(path);
779     Error error = FileSystem::Unlink(path.c_str());
780     StreamString response;
781     response.Printf("F%u,%u", error.GetError(), error.GetError());
782     return SendPacketNoLock(response.GetData(), response.GetSize());
783 }
784 
785 GDBRemoteCommunication::PacketResult
786 GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell (StringExtractorGDBRemote &packet)
787 {
788     packet.SetFilePos(::strlen("qPlatform_shell:"));
789     std::string path;
790     std::string working_dir;
791     packet.GetHexByteStringTerminatedBy(path,',');
792     if (!path.empty())
793     {
794         if (packet.GetChar() == ',')
795         {
796             // FIXME: add timeout to qPlatform_shell packet
797             // uint32_t timeout = packet.GetHexMaxU32(false, 32);
798             uint32_t timeout = 10;
799             if (packet.GetChar() == ',')
800                 packet.GetHexByteString(working_dir);
801             int status, signo;
802             std::string output;
803             Error err = Host::RunShellCommand(path.c_str(),
804                                               working_dir.empty() ? NULL : working_dir.c_str(),
805                                               &status, &signo, &output, timeout);
806             StreamGDBRemote response;
807             if (err.Fail())
808             {
809                 response.PutCString("F,");
810                 response.PutHex32(UINT32_MAX);
811             }
812             else
813             {
814                 response.PutCString("F,");
815                 response.PutHex32(status);
816                 response.PutChar(',');
817                 response.PutHex32(signo);
818                 response.PutChar(',');
819                 response.PutEscapedBytes(output.c_str(), output.size());
820             }
821             return SendPacketNoLock(response.GetData(), response.GetSize());
822         }
823     }
824     return SendErrorResponse(24);
825 }
826 
827 
828 GDBRemoteCommunication::PacketResult
829 GDBRemoteCommunicationServerCommon::Handle_vFile_Stat (StringExtractorGDBRemote &packet)
830 {
831     return SendUnimplementedResponse("GDBRemoteCommunicationServerCommon::Handle_vFile_Stat() unimplemented");
832 }
833 
834 GDBRemoteCommunication::PacketResult
835 GDBRemoteCommunicationServerCommon::Handle_vFile_MD5 (StringExtractorGDBRemote &packet)
836 {
837     packet.SetFilePos(::strlen("vFile:MD5:"));
838     std::string path;
839     packet.GetHexByteString(path);
840     if (!path.empty())
841     {
842         uint64_t a,b;
843         StreamGDBRemote response;
844         if (!FileSystem::CalculateMD5(FileSpec(path.c_str(), false), a, b))
845         {
846             response.PutCString("F,");
847             response.PutCString("x");
848         }
849         else
850         {
851             response.PutCString("F,");
852             response.PutHex64(a);
853             response.PutHex64(b);
854         }
855         return SendPacketNoLock(response.GetData(), response.GetSize());
856     }
857     return SendErrorResponse(25);
858 }
859 
860 GDBRemoteCommunication::PacketResult
861 GDBRemoteCommunicationServerCommon::Handle_qPlatform_mkdir (StringExtractorGDBRemote &packet)
862 {
863     packet.SetFilePos(::strlen("qPlatform_mkdir:"));
864     mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX);
865     if (packet.GetChar() == ',')
866     {
867         std::string path;
868         packet.GetHexByteString(path);
869         Error error = FileSystem::MakeDirectory(path.c_str(), mode);
870 
871         StreamGDBRemote response;
872         response.Printf("F%u", error.GetError());
873 
874         return SendPacketNoLock(response.GetData(), response.GetSize());
875     }
876     return SendErrorResponse(20);
877 }
878 
879 GDBRemoteCommunication::PacketResult
880 GDBRemoteCommunicationServerCommon::Handle_qPlatform_chmod (StringExtractorGDBRemote &packet)
881 {
882     packet.SetFilePos(::strlen("qPlatform_chmod:"));
883 
884     mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX);
885     if (packet.GetChar() == ',')
886     {
887         std::string path;
888         packet.GetHexByteString(path);
889         Error error = FileSystem::SetFilePermissions(path.c_str(), mode);
890 
891         StreamGDBRemote response;
892         response.Printf("F%u", error.GetError());
893 
894         return SendPacketNoLock(response.GetData(), response.GetSize());
895     }
896     return SendErrorResponse(19);
897 }
898 
899 GDBRemoteCommunication::PacketResult
900 GDBRemoteCommunicationServerCommon::Handle_qSupported (StringExtractorGDBRemote &packet)
901 {
902     StreamGDBRemote response;
903 
904     // Features common to lldb-platform and llgs.
905     uint32_t max_packet_size = 128 * 1024;  // 128KBytes is a reasonable max packet size--debugger can always use less
906     response.Printf ("PacketSize=%x", max_packet_size);
907 
908     response.PutCString (";QStartNoAckMode+");
909     response.PutCString (";QThreadSuffixSupported+");
910     response.PutCString (";QListThreadsInStopReply+");
911 #if defined(__linux__)
912     response.PutCString (";qXfer:auxv:read+");
913 #endif
914 
915     return SendPacketNoLock(response.GetData(), response.GetSize());
916 }
917 
918 GDBRemoteCommunication::PacketResult
919 GDBRemoteCommunicationServerCommon::Handle_QThreadSuffixSupported (StringExtractorGDBRemote &packet)
920 {
921     m_thread_suffix_supported = true;
922     return SendOKResponse();
923 }
924 
925 GDBRemoteCommunication::PacketResult
926 GDBRemoteCommunicationServerCommon::Handle_QListThreadsInStopReply (StringExtractorGDBRemote &packet)
927 {
928     m_list_threads_in_stop_reply = true;
929     return SendOKResponse();
930 }
931 
932 GDBRemoteCommunication::PacketResult
933 GDBRemoteCommunicationServerCommon::Handle_QSetDetachOnError (StringExtractorGDBRemote &packet)
934 {
935     packet.SetFilePos(::strlen ("QSetDetachOnError:"));
936     if (packet.GetU32(0))
937         m_process_launch_info.GetFlags().Set (eLaunchFlagDetachOnError);
938     else
939         m_process_launch_info.GetFlags().Clear (eLaunchFlagDetachOnError);
940     return SendOKResponse ();
941 }
942 
943 GDBRemoteCommunication::PacketResult
944 GDBRemoteCommunicationServerCommon::Handle_QStartNoAckMode (StringExtractorGDBRemote &packet)
945 {
946     // Send response first before changing m_send_acks to we ack this packet
947     PacketResult packet_result = SendOKResponse ();
948     m_send_acks = false;
949     return packet_result;
950 }
951 
952 GDBRemoteCommunication::PacketResult
953 GDBRemoteCommunicationServerCommon::Handle_QSetSTDIN (StringExtractorGDBRemote &packet)
954 {
955     packet.SetFilePos(::strlen ("QSetSTDIN:"));
956     FileAction file_action;
957     std::string path;
958     packet.GetHexByteString(path);
959     const bool read = false;
960     const bool write = true;
961     if (file_action.Open(STDIN_FILENO, path.c_str(), read, write))
962     {
963         m_process_launch_info.AppendFileAction(file_action);
964         return SendOKResponse ();
965     }
966     return SendErrorResponse (15);
967 }
968 
969 GDBRemoteCommunication::PacketResult
970 GDBRemoteCommunicationServerCommon::Handle_QSetSTDOUT (StringExtractorGDBRemote &packet)
971 {
972     packet.SetFilePos(::strlen ("QSetSTDOUT:"));
973     FileAction file_action;
974     std::string path;
975     packet.GetHexByteString(path);
976     const bool read = true;
977     const bool write = false;
978     if (file_action.Open(STDOUT_FILENO, path.c_str(), read, write))
979     {
980         m_process_launch_info.AppendFileAction(file_action);
981         return SendOKResponse ();
982     }
983     return SendErrorResponse (16);
984 }
985 
986 GDBRemoteCommunication::PacketResult
987 GDBRemoteCommunicationServerCommon::Handle_QSetSTDERR (StringExtractorGDBRemote &packet)
988 {
989     packet.SetFilePos(::strlen ("QSetSTDERR:"));
990     FileAction file_action;
991     std::string path;
992     packet.GetHexByteString(path);
993     const bool read = true;
994     const bool write = false;
995     if (file_action.Open(STDERR_FILENO, path.c_str(), read, write))
996     {
997         m_process_launch_info.AppendFileAction(file_action);
998         return SendOKResponse ();
999     }
1000     return SendErrorResponse (17);
1001 }
1002 
1003 GDBRemoteCommunication::PacketResult
1004 GDBRemoteCommunicationServerCommon::Handle_qLaunchSuccess (StringExtractorGDBRemote &packet)
1005 {
1006     if (m_process_launch_error.Success())
1007         return SendOKResponse();
1008     StreamString response;
1009     response.PutChar('E');
1010     response.PutCString(m_process_launch_error.AsCString("<unknown error>"));
1011     return SendPacketNoLock (response.GetData(), response.GetSize());
1012 }
1013 
1014 GDBRemoteCommunication::PacketResult
1015 GDBRemoteCommunicationServerCommon::Handle_QEnvironment (StringExtractorGDBRemote &packet)
1016 {
1017     packet.SetFilePos(::strlen ("QEnvironment:"));
1018     const uint32_t bytes_left = packet.GetBytesLeft();
1019     if (bytes_left > 0)
1020     {
1021         m_process_launch_info.GetEnvironmentEntries ().AppendArgument (packet.Peek());
1022         return SendOKResponse ();
1023     }
1024     return SendErrorResponse (12);
1025 }
1026 
1027 GDBRemoteCommunication::PacketResult
1028 GDBRemoteCommunicationServerCommon::Handle_QEnvironmentHexEncoded (StringExtractorGDBRemote &packet)
1029 {
1030     packet.SetFilePos(::strlen("QEnvironmentHexEncoded:"));
1031     const uint32_t bytes_left = packet.GetBytesLeft();
1032     if (bytes_left > 0)
1033     {
1034         std::string str;
1035         packet.GetHexByteString(str);
1036         m_process_launch_info.GetEnvironmentEntries().AppendArgument(str.c_str());
1037         return SendOKResponse();
1038     }
1039     return SendErrorResponse(12);
1040 }
1041 
1042 GDBRemoteCommunication::PacketResult
1043 GDBRemoteCommunicationServerCommon::Handle_QLaunchArch (StringExtractorGDBRemote &packet)
1044 {
1045     packet.SetFilePos(::strlen ("QLaunchArch:"));
1046     const uint32_t bytes_left = packet.GetBytesLeft();
1047     if (bytes_left > 0)
1048     {
1049         const char* arch_triple = packet.Peek();
1050         ArchSpec arch_spec(arch_triple,NULL);
1051         m_process_launch_info.SetArchitecture(arch_spec);
1052         return SendOKResponse();
1053     }
1054     return SendErrorResponse(13);
1055 }
1056 
1057 GDBRemoteCommunication::PacketResult
1058 GDBRemoteCommunicationServerCommon::Handle_A (StringExtractorGDBRemote &packet)
1059 {
1060     // The 'A' packet is the most over designed packet ever here with
1061     // redundant argument indexes, redundant argument lengths and needed hex
1062     // encoded argument string values. Really all that is needed is a comma
1063     // separated hex encoded argument value list, but we will stay true to the
1064     // documented version of the 'A' packet here...
1065 
1066     Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1067     int actual_arg_index = 0;
1068 
1069     packet.SetFilePos(1); // Skip the 'A'
1070     bool success = true;
1071     while (success && packet.GetBytesLeft() > 0)
1072     {
1073         // Decode the decimal argument string length. This length is the
1074         // number of hex nibbles in the argument string value.
1075         const uint32_t arg_len = packet.GetU32(UINT32_MAX);
1076         if (arg_len == UINT32_MAX)
1077             success = false;
1078         else
1079         {
1080             // Make sure the argument hex string length is followed by a comma
1081             if (packet.GetChar() != ',')
1082                 success = false;
1083             else
1084             {
1085                 // Decode the argument index. We ignore this really because
1086                 // who would really send down the arguments in a random order???
1087                 const uint32_t arg_idx = packet.GetU32(UINT32_MAX);
1088                 if (arg_idx == UINT32_MAX)
1089                     success = false;
1090                 else
1091                 {
1092                     // Make sure the argument index is followed by a comma
1093                     if (packet.GetChar() != ',')
1094                         success = false;
1095                     else
1096                     {
1097                         // Decode the argument string value from hex bytes
1098                         // back into a UTF8 string and make sure the length
1099                         // matches the one supplied in the packet
1100                         std::string arg;
1101                         if (packet.GetHexByteStringFixedLength(arg, arg_len) != (arg_len / 2))
1102                             success = false;
1103                         else
1104                         {
1105                             // If there are any bytes left
1106                             if (packet.GetBytesLeft())
1107                             {
1108                                 if (packet.GetChar() != ',')
1109                                     success = false;
1110                             }
1111 
1112                             if (success)
1113                             {
1114                                 if (arg_idx == 0)
1115                                     m_process_launch_info.GetExecutableFile().SetFile(arg.c_str(), false);
1116                                 m_process_launch_info.GetArguments().AppendArgument(arg.c_str());
1117                                 if (log)
1118                                     log->Printf ("LLGSPacketHandler::%s added arg %d: \"%s\"", __FUNCTION__, actual_arg_index, arg.c_str ());
1119                                 ++actual_arg_index;
1120                             }
1121                         }
1122                     }
1123                 }
1124             }
1125         }
1126     }
1127 
1128     if (success)
1129     {
1130         m_process_launch_error = LaunchProcess ();
1131         if (m_process_launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
1132         {
1133             return SendOKResponse ();
1134         }
1135         else
1136         {
1137             Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS));
1138             if (log)
1139                 log->Printf("LLGSPacketHandler::%s failed to launch exe: %s",
1140                         __FUNCTION__,
1141                         m_process_launch_error.AsCString());
1142 
1143         }
1144     }
1145     return SendErrorResponse (8);
1146 }
1147 
1148 GDBRemoteCommunication::PacketResult
1149 GDBRemoteCommunicationServerCommon::Handle_qModuleInfo (StringExtractorGDBRemote &packet)
1150 {
1151     packet.SetFilePos(::strlen ("qModuleInfo:"));
1152 
1153     std::string module_path;
1154     packet.GetHexByteStringTerminatedBy(module_path, ';');
1155     if (module_path.empty())
1156         return SendErrorResponse (1);
1157 
1158     if (packet.GetChar() != ';')
1159         return SendErrorResponse (2);
1160 
1161     std::string triple;
1162     packet.GetHexByteString(triple);
1163     ArchSpec arch(triple.c_str());
1164 
1165     const FileSpec module_path_spec = FindModuleFile(module_path, arch);
1166     const ModuleSpec module_spec(module_path_spec, arch);
1167 
1168     ModuleSpecList module_specs;
1169     if (!ObjectFile::GetModuleSpecifications(module_path_spec, 0, 0, module_specs))
1170         return SendErrorResponse (3);
1171 
1172     ModuleSpec matched_module_spec;
1173     if (!module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec))
1174         return SendErrorResponse (4);
1175 
1176     const auto file_offset = matched_module_spec.GetObjectOffset();
1177     const auto file_size = matched_module_spec.GetObjectSize();
1178     const auto uuid_str = matched_module_spec.GetUUID().GetAsString("");
1179 
1180     StreamGDBRemote response;
1181 
1182     if (uuid_str.empty())
1183     {
1184         std::string md5_hash;
1185         if (!FileSystem::CalculateMD5AsString(matched_module_spec.GetFileSpec(), file_offset, file_size, md5_hash))
1186             return SendErrorResponse (5);
1187         response.PutCString ("md5:");
1188         response.PutCStringAsRawHex8(md5_hash.c_str());
1189     }
1190     else{
1191         response.PutCString ("uuid:");
1192         response.PutCStringAsRawHex8(uuid_str.c_str());
1193     }
1194     response.PutChar(';');
1195 
1196     const auto &module_arch = matched_module_spec.GetArchitecture();
1197     response.PutCString("triple:");
1198     response.PutCStringAsRawHex8( module_arch.GetTriple().getTriple().c_str());
1199     response.PutChar(';');
1200 
1201     response.PutCString("file_path:");
1202     response.PutCStringAsRawHex8(module_path_spec.GetPath().c_str());
1203     response.PutChar(';');
1204     response.PutCString("file_offset:");
1205     response.PutHex64(file_offset);
1206     response.PutChar(';');
1207     response.PutCString("file_size:");
1208     response.PutHex64(file_size);
1209     response.PutChar(';');
1210 
1211     return SendPacketNoLock(response.GetData(), response.GetSize());
1212 }
1213 
1214 void
1215 GDBRemoteCommunicationServerCommon::CreateProcessInfoResponse (const ProcessInstanceInfo &proc_info,
1216                                                     StreamString &response)
1217 {
1218     response.Printf ("pid:%" PRIu64 ";ppid:%" PRIu64 ";uid:%i;gid:%i;euid:%i;egid:%i;",
1219                      proc_info.GetProcessID(),
1220                      proc_info.GetParentProcessID(),
1221                      proc_info.GetUserID(),
1222                      proc_info.GetGroupID(),
1223                      proc_info.GetEffectiveUserID(),
1224                      proc_info.GetEffectiveGroupID());
1225     response.PutCString ("name:");
1226     response.PutCStringAsRawHex8(proc_info.GetExecutableFile().GetPath().c_str());
1227     response.PutChar(';');
1228     const ArchSpec &proc_arch = proc_info.GetArchitecture();
1229     if (proc_arch.IsValid())
1230     {
1231         const llvm::Triple &proc_triple = proc_arch.GetTriple();
1232         response.PutCString("triple:");
1233         response.PutCStringAsRawHex8(proc_triple.getTriple().c_str());
1234         response.PutChar(';');
1235     }
1236 }
1237 
1238 void
1239 GDBRemoteCommunicationServerCommon::CreateProcessInfoResponse_DebugServerStyle (
1240     const ProcessInstanceInfo &proc_info, StreamString &response)
1241 {
1242     response.Printf ("pid:%" PRIx64 ";parent-pid:%" PRIx64 ";real-uid:%x;real-gid:%x;effective-uid:%x;effective-gid:%x;",
1243                      proc_info.GetProcessID(),
1244                      proc_info.GetParentProcessID(),
1245                      proc_info.GetUserID(),
1246                      proc_info.GetGroupID(),
1247                      proc_info.GetEffectiveUserID(),
1248                      proc_info.GetEffectiveGroupID());
1249 
1250     const ArchSpec &proc_arch = proc_info.GetArchitecture();
1251     if (proc_arch.IsValid())
1252     {
1253         const llvm::Triple &proc_triple = proc_arch.GetTriple();
1254 #if defined(__APPLE__)
1255         // We'll send cputype/cpusubtype.
1256         const uint32_t cpu_type = proc_arch.GetMachOCPUType();
1257         if (cpu_type != 0)
1258             response.Printf ("cputype:%" PRIx32 ";", cpu_type);
1259 
1260         const uint32_t cpu_subtype = proc_arch.GetMachOCPUSubType();
1261         if (cpu_subtype != 0)
1262             response.Printf ("cpusubtype:%" PRIx32 ";", cpu_subtype);
1263 
1264         const std::string vendor = proc_triple.getVendorName ();
1265         if (!vendor.empty ())
1266             response.Printf ("vendor:%s;", vendor.c_str ());
1267 #else
1268         // We'll send the triple.
1269         response.PutCString("triple:");
1270         response.PutCStringAsRawHex8(proc_triple.getTriple().c_str());
1271         response.PutChar(';');
1272 #endif
1273         std::string ostype = proc_triple.getOSName ();
1274         // Adjust so ostype reports ios for Apple/ARM and Apple/ARM64.
1275         if (proc_triple.getVendor () == llvm::Triple::Apple)
1276         {
1277             switch (proc_triple.getArch ())
1278             {
1279                 case llvm::Triple::arm:
1280                 case llvm::Triple::aarch64:
1281                     ostype = "ios";
1282                     break;
1283                 default:
1284                     // No change.
1285                     break;
1286             }
1287         }
1288         response.Printf ("ostype:%s;", ostype.c_str ());
1289 
1290 
1291         switch (proc_arch.GetByteOrder ())
1292         {
1293             case lldb::eByteOrderLittle: response.PutCString ("endian:little;"); break;
1294             case lldb::eByteOrderBig:    response.PutCString ("endian:big;");    break;
1295             case lldb::eByteOrderPDP:    response.PutCString ("endian:pdp;");    break;
1296             default:
1297                 // Nothing.
1298                 break;
1299         }
1300 
1301         if (proc_triple.isArch64Bit ())
1302             response.PutCString ("ptrsize:8;");
1303         else if (proc_triple.isArch32Bit ())
1304             response.PutCString ("ptrsize:4;");
1305         else if (proc_triple.isArch16Bit ())
1306             response.PutCString ("ptrsize:2;");
1307     }
1308 }
1309 
1310 FileSpec
1311 GDBRemoteCommunicationServerCommon::FindModuleFile(const std::string& module_path,
1312                                                    const ArchSpec& arch)
1313 {
1314 #ifdef __ANDROID__
1315     return HostInfoAndroid::ResolveLibraryPath(module_path, arch);
1316 #else
1317     return FileSpec(module_path.c_str(), true);
1318 #endif
1319 }
1320