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