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