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