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