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