xref: /llvm-project/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp (revision 6c4e70fcbbb62f38a5aab085634de5faaa5cf729)
1 //===-- GDBRemoteCommunicationServerCommon.cpp ----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "GDBRemoteCommunicationServerCommon.h"
10 
11 #include <cerrno>
12 
13 #ifdef __APPLE__
14 #include <TargetConditionals.h>
15 #endif
16 
17 #include <chrono>
18 #include <cstring>
19 #include <optional>
20 
21 #include "lldb/Core/ModuleSpec.h"
22 #include "lldb/Host/Config.h"
23 #include "lldb/Host/File.h"
24 #include "lldb/Host/FileAction.h"
25 #include "lldb/Host/FileSystem.h"
26 #include "lldb/Host/Host.h"
27 #include "lldb/Host/HostInfo.h"
28 #include "lldb/Host/SafeMachO.h"
29 #include "lldb/Interpreter/OptionArgParser.h"
30 #include "lldb/Symbol/ObjectFile.h"
31 #include "lldb/Target/Platform.h"
32 #include "lldb/Utility/Endian.h"
33 #include "lldb/Utility/GDBRemote.h"
34 #include "lldb/Utility/LLDBLog.h"
35 #include "lldb/Utility/Log.h"
36 #include "lldb/Utility/StreamString.h"
37 #include "lldb/Utility/StructuredData.h"
38 #include "llvm/ADT/StringSwitch.h"
39 #include "llvm/Support/JSON.h"
40 #include "llvm/TargetParser/Triple.h"
41 
42 #include "ProcessGDBRemoteLog.h"
43 #include "lldb/Utility/StringExtractorGDBRemote.h"
44 
45 #ifdef __ANDROID__
46 #include "lldb/Host/android/HostInfoAndroid.h"
47 #include "lldb/Host/common/ZipFileResolver.h"
48 #endif
49 
50 using namespace lldb;
51 using namespace lldb_private::process_gdb_remote;
52 using namespace lldb_private;
53 
54 #ifdef __ANDROID__
55 const static uint32_t g_default_packet_timeout_sec = 20; // seconds
56 #else
57 const static uint32_t g_default_packet_timeout_sec = 0; // not specified
58 #endif
59 
60 // GDBRemoteCommunicationServerCommon constructor
61 GDBRemoteCommunicationServerCommon::GDBRemoteCommunicationServerCommon()
62     : GDBRemoteCommunicationServer(), m_process_launch_info(),
63       m_process_launch_error(), m_proc_infos(), m_proc_infos_index(0) {
64   RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_A,
65                                 &GDBRemoteCommunicationServerCommon::Handle_A);
66   RegisterMemberFunctionHandler(
67       StringExtractorGDBRemote::eServerPacketType_QEnvironment,
68       &GDBRemoteCommunicationServerCommon::Handle_QEnvironment);
69   RegisterMemberFunctionHandler(
70       StringExtractorGDBRemote::eServerPacketType_QEnvironmentHexEncoded,
71       &GDBRemoteCommunicationServerCommon::Handle_QEnvironmentHexEncoded);
72   RegisterMemberFunctionHandler(
73       StringExtractorGDBRemote::eServerPacketType_qfProcessInfo,
74       &GDBRemoteCommunicationServerCommon::Handle_qfProcessInfo);
75   RegisterMemberFunctionHandler(
76       StringExtractorGDBRemote::eServerPacketType_qGroupName,
77       &GDBRemoteCommunicationServerCommon::Handle_qGroupName);
78   RegisterMemberFunctionHandler(
79       StringExtractorGDBRemote::eServerPacketType_qHostInfo,
80       &GDBRemoteCommunicationServerCommon::Handle_qHostInfo);
81   RegisterMemberFunctionHandler(
82       StringExtractorGDBRemote::eServerPacketType_QLaunchArch,
83       &GDBRemoteCommunicationServerCommon::Handle_QLaunchArch);
84   RegisterMemberFunctionHandler(
85       StringExtractorGDBRemote::eServerPacketType_qLaunchSuccess,
86       &GDBRemoteCommunicationServerCommon::Handle_qLaunchSuccess);
87   RegisterMemberFunctionHandler(
88       StringExtractorGDBRemote::eServerPacketType_qEcho,
89       &GDBRemoteCommunicationServerCommon::Handle_qEcho);
90   RegisterMemberFunctionHandler(
91       StringExtractorGDBRemote::eServerPacketType_qModuleInfo,
92       &GDBRemoteCommunicationServerCommon::Handle_qModuleInfo);
93   RegisterMemberFunctionHandler(
94       StringExtractorGDBRemote::eServerPacketType_jModulesInfo,
95       &GDBRemoteCommunicationServerCommon::Handle_jModulesInfo);
96   RegisterMemberFunctionHandler(
97       StringExtractorGDBRemote::eServerPacketType_qPlatform_chmod,
98       &GDBRemoteCommunicationServerCommon::Handle_qPlatform_chmod);
99   RegisterMemberFunctionHandler(
100       StringExtractorGDBRemote::eServerPacketType_qPlatform_mkdir,
101       &GDBRemoteCommunicationServerCommon::Handle_qPlatform_mkdir);
102   RegisterMemberFunctionHandler(
103       StringExtractorGDBRemote::eServerPacketType_qPlatform_shell,
104       &GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell);
105   RegisterMemberFunctionHandler(
106       StringExtractorGDBRemote::eServerPacketType_qProcessInfoPID,
107       &GDBRemoteCommunicationServerCommon::Handle_qProcessInfoPID);
108   RegisterMemberFunctionHandler(
109       StringExtractorGDBRemote::eServerPacketType_QSetDetachOnError,
110       &GDBRemoteCommunicationServerCommon::Handle_QSetDetachOnError);
111   RegisterMemberFunctionHandler(
112       StringExtractorGDBRemote::eServerPacketType_QSetSTDERR,
113       &GDBRemoteCommunicationServerCommon::Handle_QSetSTDERR);
114   RegisterMemberFunctionHandler(
115       StringExtractorGDBRemote::eServerPacketType_QSetSTDIN,
116       &GDBRemoteCommunicationServerCommon::Handle_QSetSTDIN);
117   RegisterMemberFunctionHandler(
118       StringExtractorGDBRemote::eServerPacketType_QSetSTDOUT,
119       &GDBRemoteCommunicationServerCommon::Handle_QSetSTDOUT);
120   RegisterMemberFunctionHandler(
121       StringExtractorGDBRemote::eServerPacketType_qSpeedTest,
122       &GDBRemoteCommunicationServerCommon::Handle_qSpeedTest);
123   RegisterMemberFunctionHandler(
124       StringExtractorGDBRemote::eServerPacketType_qsProcessInfo,
125       &GDBRemoteCommunicationServerCommon::Handle_qsProcessInfo);
126   RegisterMemberFunctionHandler(
127       StringExtractorGDBRemote::eServerPacketType_QStartNoAckMode,
128       &GDBRemoteCommunicationServerCommon::Handle_QStartNoAckMode);
129   RegisterMemberFunctionHandler(
130       StringExtractorGDBRemote::eServerPacketType_qSupported,
131       &GDBRemoteCommunicationServerCommon::Handle_qSupported);
132   RegisterMemberFunctionHandler(
133       StringExtractorGDBRemote::eServerPacketType_qUserName,
134       &GDBRemoteCommunicationServerCommon::Handle_qUserName);
135   RegisterMemberFunctionHandler(
136       StringExtractorGDBRemote::eServerPacketType_vFile_close,
137       &GDBRemoteCommunicationServerCommon::Handle_vFile_Close);
138   RegisterMemberFunctionHandler(
139       StringExtractorGDBRemote::eServerPacketType_vFile_exists,
140       &GDBRemoteCommunicationServerCommon::Handle_vFile_Exists);
141   RegisterMemberFunctionHandler(
142       StringExtractorGDBRemote::eServerPacketType_vFile_md5,
143       &GDBRemoteCommunicationServerCommon::Handle_vFile_MD5);
144   RegisterMemberFunctionHandler(
145       StringExtractorGDBRemote::eServerPacketType_vFile_mode,
146       &GDBRemoteCommunicationServerCommon::Handle_vFile_Mode);
147   RegisterMemberFunctionHandler(
148       StringExtractorGDBRemote::eServerPacketType_vFile_open,
149       &GDBRemoteCommunicationServerCommon::Handle_vFile_Open);
150   RegisterMemberFunctionHandler(
151       StringExtractorGDBRemote::eServerPacketType_vFile_pread,
152       &GDBRemoteCommunicationServerCommon::Handle_vFile_pRead);
153   RegisterMemberFunctionHandler(
154       StringExtractorGDBRemote::eServerPacketType_vFile_pwrite,
155       &GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite);
156   RegisterMemberFunctionHandler(
157       StringExtractorGDBRemote::eServerPacketType_vFile_size,
158       &GDBRemoteCommunicationServerCommon::Handle_vFile_Size);
159   RegisterMemberFunctionHandler(
160       StringExtractorGDBRemote::eServerPacketType_vFile_fstat,
161       &GDBRemoteCommunicationServerCommon::Handle_vFile_FStat);
162   RegisterMemberFunctionHandler(
163       StringExtractorGDBRemote::eServerPacketType_vFile_stat,
164       &GDBRemoteCommunicationServerCommon::Handle_vFile_Stat);
165   RegisterMemberFunctionHandler(
166       StringExtractorGDBRemote::eServerPacketType_vFile_symlink,
167       &GDBRemoteCommunicationServerCommon::Handle_vFile_symlink);
168   RegisterMemberFunctionHandler(
169       StringExtractorGDBRemote::eServerPacketType_vFile_unlink,
170       &GDBRemoteCommunicationServerCommon::Handle_vFile_unlink);
171 }
172 
173 // Destructor
174 GDBRemoteCommunicationServerCommon::~GDBRemoteCommunicationServerCommon() =
175     default;
176 
177 GDBRemoteCommunication::PacketResult
178 GDBRemoteCommunicationServerCommon::Handle_qHostInfo(
179     StringExtractorGDBRemote &packet) {
180   StreamString response;
181 
182   // $cputype:16777223;cpusubtype:3;ostype:Darwin;vendor:apple;endian:little;ptrsize:8;#00
183 
184   ArchSpec host_arch(HostInfo::GetArchitecture());
185   const llvm::Triple &host_triple = host_arch.GetTriple();
186   response.PutCString("triple:");
187   response.PutStringAsRawHex8(host_triple.getTriple());
188   response.Printf(";ptrsize:%u;", host_arch.GetAddressByteSize());
189 
190   llvm::StringRef distribution_id = HostInfo::GetDistributionId();
191   if (!distribution_id.empty()) {
192     response.PutCString("distribution_id:");
193     response.PutStringAsRawHex8(distribution_id);
194     response.PutCString(";");
195   }
196 
197 #if defined(__APPLE__)
198   // For parity with debugserver, we'll include the vendor key.
199   response.PutCString("vendor:apple;");
200 
201   // Send out MachO info.
202   uint32_t cpu = host_arch.GetMachOCPUType();
203   uint32_t sub = host_arch.GetMachOCPUSubType();
204   if (cpu != LLDB_INVALID_CPUTYPE)
205     response.Printf("cputype:%u;", cpu);
206   if (sub != LLDB_INVALID_CPUTYPE)
207     response.Printf("cpusubtype:%u;", sub);
208 
209   if (cpu == llvm::MachO::CPU_TYPE_ARM || cpu == llvm::MachO::CPU_TYPE_ARM64) {
210 // Indicate the OS type.
211 #if defined(TARGET_OS_TV) && TARGET_OS_TV == 1
212     response.PutCString("ostype:tvos;");
213 #elif defined(TARGET_OS_WATCH) && TARGET_OS_WATCH == 1
214     response.PutCString("ostype:watchos;");
215 #elif defined(TARGET_OS_BRIDGE) && TARGET_OS_BRIDGE == 1
216     response.PutCString("ostype:bridgeos;");
217 #else
218     response.PutCString("ostype:ios;");
219 #endif
220 
221     // On arm, we use "synchronous" watchpoints which means the exception is
222     // delivered before the instruction executes.
223     response.PutCString("watchpoint_exceptions_received:before;");
224   } else {
225     response.PutCString("ostype:macosx;");
226     response.Printf("watchpoint_exceptions_received:after;");
227   }
228 
229 #else
230   if (host_arch.GetMachine() == llvm::Triple::aarch64 ||
231       host_arch.GetMachine() == llvm::Triple::aarch64_32 ||
232       host_arch.GetMachine() == llvm::Triple::aarch64_be ||
233       host_arch.GetMachine() == llvm::Triple::arm ||
234       host_arch.GetMachine() == llvm::Triple::armeb || host_arch.IsMIPS() ||
235       host_arch.GetTriple().isLoongArch())
236     response.Printf("watchpoint_exceptions_received:before;");
237   else
238     response.Printf("watchpoint_exceptions_received:after;");
239 #endif
240 
241   switch (endian::InlHostByteOrder()) {
242   case eByteOrderBig:
243     response.PutCString("endian:big;");
244     break;
245   case eByteOrderLittle:
246     response.PutCString("endian:little;");
247     break;
248   case eByteOrderPDP:
249     response.PutCString("endian:pdp;");
250     break;
251   default:
252     response.PutCString("endian:unknown;");
253     break;
254   }
255 
256   llvm::VersionTuple version = HostInfo::GetOSVersion();
257   if (!version.empty()) {
258     response.Format("os_version:{0}", version.getAsString());
259     response.PutChar(';');
260   }
261 
262 #if defined(__APPLE__)
263   llvm::VersionTuple maccatalyst_version = HostInfo::GetMacCatalystVersion();
264   if (!maccatalyst_version.empty()) {
265     response.Format("maccatalyst_version:{0}",
266                     maccatalyst_version.getAsString());
267     response.PutChar(';');
268   }
269 #endif
270 
271   if (std::optional<std::string> s = HostInfo::GetOSBuildString()) {
272     response.PutCString("os_build:");
273     response.PutStringAsRawHex8(*s);
274     response.PutChar(';');
275   }
276   if (std::optional<std::string> s = HostInfo::GetOSKernelDescription()) {
277     response.PutCString("os_kernel:");
278     response.PutStringAsRawHex8(*s);
279     response.PutChar(';');
280   }
281 
282   std::string s;
283 #if defined(__APPLE__)
284 
285 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
286   // For iOS devices, we are connected through a USB Mux so we never pretend to
287   // actually have a hostname as far as the remote lldb that is connecting to
288   // this lldb-platform is concerned
289   response.PutCString("hostname:");
290   response.PutStringAsRawHex8("127.0.0.1");
291   response.PutChar(';');
292 #else  // #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
293   if (HostInfo::GetHostname(s)) {
294     response.PutCString("hostname:");
295     response.PutStringAsRawHex8(s);
296     response.PutChar(';');
297   }
298 #endif // #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__)
299 
300 #else  // #if defined(__APPLE__)
301   if (HostInfo::GetHostname(s)) {
302     response.PutCString("hostname:");
303     response.PutStringAsRawHex8(s);
304     response.PutChar(';');
305   }
306 #endif // #if defined(__APPLE__)
307   // coverity[unsigned_compare]
308   if (g_default_packet_timeout_sec > 0)
309     response.Printf("default_packet_timeout:%u;", g_default_packet_timeout_sec);
310 
311   return SendPacketNoLock(response.GetString());
312 }
313 
314 GDBRemoteCommunication::PacketResult
315 GDBRemoteCommunicationServerCommon::Handle_qProcessInfoPID(
316     StringExtractorGDBRemote &packet) {
317   // Packet format: "qProcessInfoPID:%i" where %i is the pid
318   packet.SetFilePos(::strlen("qProcessInfoPID:"));
319   lldb::pid_t pid = packet.GetU32(LLDB_INVALID_PROCESS_ID);
320   if (pid != LLDB_INVALID_PROCESS_ID) {
321     ProcessInstanceInfo proc_info;
322     if (Host::GetProcessInfo(pid, proc_info)) {
323       StreamString response;
324       CreateProcessInfoResponse(proc_info, response);
325       return SendPacketNoLock(response.GetString());
326     }
327   }
328   return SendErrorResponse(1);
329 }
330 
331 GDBRemoteCommunication::PacketResult
332 GDBRemoteCommunicationServerCommon::Handle_qfProcessInfo(
333     StringExtractorGDBRemote &packet) {
334   m_proc_infos_index = 0;
335   m_proc_infos.clear();
336 
337   ProcessInstanceInfoMatch match_info;
338   packet.SetFilePos(::strlen("qfProcessInfo"));
339   if (packet.GetChar() == ':') {
340     llvm::StringRef key;
341     llvm::StringRef value;
342     while (packet.GetNameColonValue(key, value)) {
343       bool success = true;
344       if (key == "name") {
345         StringExtractor extractor(value);
346         std::string file;
347         extractor.GetHexByteString(file);
348         match_info.GetProcessInfo().GetExecutableFile().SetFile(
349             file, FileSpec::Style::native);
350       } else if (key == "name_match") {
351         NameMatch name_match = llvm::StringSwitch<NameMatch>(value)
352                                    .Case("equals", NameMatch::Equals)
353                                    .Case("starts_with", NameMatch::StartsWith)
354                                    .Case("ends_with", NameMatch::EndsWith)
355                                    .Case("contains", NameMatch::Contains)
356                                    .Case("regex", NameMatch::RegularExpression)
357                                    .Default(NameMatch::Ignore);
358         match_info.SetNameMatchType(name_match);
359         if (name_match == NameMatch::Ignore)
360           return SendErrorResponse(2);
361       } else if (key == "pid") {
362         lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
363         if (value.getAsInteger(0, pid))
364           return SendErrorResponse(2);
365         match_info.GetProcessInfo().SetProcessID(pid);
366       } else if (key == "parent_pid") {
367         lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
368         if (value.getAsInteger(0, pid))
369           return SendErrorResponse(2);
370         match_info.GetProcessInfo().SetParentProcessID(pid);
371       } else if (key == "uid") {
372         uint32_t uid = UINT32_MAX;
373         if (value.getAsInteger(0, uid))
374           return SendErrorResponse(2);
375         match_info.GetProcessInfo().SetUserID(uid);
376       } else if (key == "gid") {
377         uint32_t gid = UINT32_MAX;
378         if (value.getAsInteger(0, gid))
379           return SendErrorResponse(2);
380         match_info.GetProcessInfo().SetGroupID(gid);
381       } else if (key == "euid") {
382         uint32_t uid = UINT32_MAX;
383         if (value.getAsInteger(0, uid))
384           return SendErrorResponse(2);
385         match_info.GetProcessInfo().SetEffectiveUserID(uid);
386       } else if (key == "egid") {
387         uint32_t gid = UINT32_MAX;
388         if (value.getAsInteger(0, gid))
389           return SendErrorResponse(2);
390         match_info.GetProcessInfo().SetEffectiveGroupID(gid);
391       } else if (key == "all_users") {
392         match_info.SetMatchAllUsers(
393             OptionArgParser::ToBoolean(value, false, &success));
394       } else if (key == "triple") {
395         match_info.GetProcessInfo().GetArchitecture() =
396             HostInfo::GetAugmentedArchSpec(value);
397       } else {
398         success = false;
399       }
400 
401       if (!success)
402         return SendErrorResponse(2);
403     }
404   }
405 
406   if (Host::FindProcesses(match_info, m_proc_infos)) {
407     // We found something, return the first item by calling the get subsequent
408     // process info packet handler...
409     return Handle_qsProcessInfo(packet);
410   }
411   return SendErrorResponse(3);
412 }
413 
414 GDBRemoteCommunication::PacketResult
415 GDBRemoteCommunicationServerCommon::Handle_qsProcessInfo(
416     StringExtractorGDBRemote &packet) {
417   if (m_proc_infos_index < m_proc_infos.size()) {
418     StreamString response;
419     CreateProcessInfoResponse(m_proc_infos[m_proc_infos_index], response);
420     ++m_proc_infos_index;
421     return SendPacketNoLock(response.GetString());
422   }
423   return SendErrorResponse(4);
424 }
425 
426 GDBRemoteCommunication::PacketResult
427 GDBRemoteCommunicationServerCommon::Handle_qUserName(
428     StringExtractorGDBRemote &packet) {
429 #if LLDB_ENABLE_POSIX
430   Log *log = GetLog(LLDBLog::Process);
431   LLDB_LOGF(log, "GDBRemoteCommunicationServerCommon::%s begin", __FUNCTION__);
432 
433   // Packet format: "qUserName:%i" where %i is the uid
434   packet.SetFilePos(::strlen("qUserName:"));
435   uint32_t uid = packet.GetU32(UINT32_MAX);
436   if (uid != UINT32_MAX) {
437     if (std::optional<llvm::StringRef> name =
438             HostInfo::GetUserIDResolver().GetUserName(uid)) {
439       StreamString response;
440       response.PutStringAsRawHex8(*name);
441       return SendPacketNoLock(response.GetString());
442     }
443   }
444   LLDB_LOGF(log, "GDBRemoteCommunicationServerCommon::%s end", __FUNCTION__);
445 #endif
446   return SendErrorResponse(5);
447 }
448 
449 GDBRemoteCommunication::PacketResult
450 GDBRemoteCommunicationServerCommon::Handle_qGroupName(
451     StringExtractorGDBRemote &packet) {
452 #if LLDB_ENABLE_POSIX
453   // Packet format: "qGroupName:%i" where %i is the gid
454   packet.SetFilePos(::strlen("qGroupName:"));
455   uint32_t gid = packet.GetU32(UINT32_MAX);
456   if (gid != UINT32_MAX) {
457     if (std::optional<llvm::StringRef> name =
458             HostInfo::GetUserIDResolver().GetGroupName(gid)) {
459       StreamString response;
460       response.PutStringAsRawHex8(*name);
461       return SendPacketNoLock(response.GetString());
462     }
463   }
464 #endif
465   return SendErrorResponse(6);
466 }
467 
468 GDBRemoteCommunication::PacketResult
469 GDBRemoteCommunicationServerCommon::Handle_qSpeedTest(
470     StringExtractorGDBRemote &packet) {
471   packet.SetFilePos(::strlen("qSpeedTest:"));
472 
473   llvm::StringRef key;
474   llvm::StringRef value;
475   bool success = packet.GetNameColonValue(key, value);
476   if (success && key == "response_size") {
477     uint32_t response_size = 0;
478     if (!value.getAsInteger(0, response_size)) {
479       if (response_size == 0)
480         return SendOKResponse();
481       StreamString response;
482       uint32_t bytes_left = response_size;
483       response.PutCString("data:");
484       while (bytes_left > 0) {
485         if (bytes_left >= 26) {
486           response.PutCString("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
487           bytes_left -= 26;
488         } else {
489           response.Printf("%*.*s;", bytes_left, bytes_left,
490                           "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
491           bytes_left = 0;
492         }
493       }
494       return SendPacketNoLock(response.GetString());
495     }
496   }
497   return SendErrorResponse(7);
498 }
499 
500 static GDBErrno system_errno_to_gdb(int err) {
501   switch (err) {
502 #define HANDLE_ERRNO(name, value)                                              \
503   case name:                                                                   \
504     return GDB_##name;
505 #include "Plugins/Process/gdb-remote/GDBRemoteErrno.def"
506   default:
507     return GDB_EUNKNOWN;
508   }
509 }
510 
511 GDBRemoteCommunication::PacketResult
512 GDBRemoteCommunicationServerCommon::Handle_vFile_Open(
513     StringExtractorGDBRemote &packet) {
514   packet.SetFilePos(::strlen("vFile:open:"));
515   std::string path;
516   packet.GetHexByteStringTerminatedBy(path, ',');
517   if (!path.empty()) {
518     if (packet.GetChar() == ',') {
519       auto flags = File::OpenOptions(packet.GetHexMaxU32(false, 0));
520       if (packet.GetChar() == ',') {
521         mode_t mode = packet.GetHexMaxU32(false, 0600);
522         FileSpec path_spec(path);
523         FileSystem::Instance().Resolve(path_spec);
524         // Do not close fd.
525         auto file = FileSystem::Instance().Open(path_spec, flags, mode, false);
526 
527         StreamString response;
528         response.PutChar('F');
529 
530         int descriptor = File::kInvalidDescriptor;
531         if (file) {
532           descriptor = file.get()->GetDescriptor();
533           response.Printf("%x", descriptor);
534         } else {
535           response.PutCString("-1");
536           std::error_code code = errorToErrorCode(file.takeError());
537           response.Printf(",%x", system_errno_to_gdb(code.value()));
538         }
539 
540         return SendPacketNoLock(response.GetString());
541       }
542     }
543   }
544   return SendErrorResponse(18);
545 }
546 
547 GDBRemoteCommunication::PacketResult
548 GDBRemoteCommunicationServerCommon::Handle_vFile_Close(
549     StringExtractorGDBRemote &packet) {
550   packet.SetFilePos(::strlen("vFile:close:"));
551   int fd = packet.GetS32(-1, 16);
552   int err = -1;
553   int save_errno = 0;
554   if (fd >= 0) {
555     NativeFile file(fd, File::OpenOptions(0), true);
556     Status error = file.Close();
557     err = 0;
558     save_errno = error.GetError();
559   } else {
560     save_errno = EINVAL;
561   }
562   StreamString response;
563   response.PutChar('F');
564   response.Printf("%x", err);
565   if (save_errno)
566     response.Printf(",%x", system_errno_to_gdb(save_errno));
567   return SendPacketNoLock(response.GetString());
568 }
569 
570 GDBRemoteCommunication::PacketResult
571 GDBRemoteCommunicationServerCommon::Handle_vFile_pRead(
572     StringExtractorGDBRemote &packet) {
573   StreamGDBRemote response;
574   packet.SetFilePos(::strlen("vFile:pread:"));
575   int fd = packet.GetS32(-1, 16);
576   if (packet.GetChar() == ',') {
577     size_t count = packet.GetHexMaxU64(false, SIZE_MAX);
578     if (packet.GetChar() == ',') {
579       off_t offset = packet.GetHexMaxU32(false, UINT32_MAX);
580       if (count == SIZE_MAX) {
581         response.Printf("F-1:%x", EINVAL);
582         return SendPacketNoLock(response.GetString());
583       }
584 
585       std::string buffer(count, 0);
586       NativeFile file(fd, File::eOpenOptionReadOnly, false);
587       Status error = file.Read(static_cast<void *>(&buffer[0]), count, offset);
588       const int save_errno = error.GetError();
589       response.PutChar('F');
590       if (error.Success()) {
591         response.Printf("%zx", count);
592         response.PutChar(';');
593         response.PutEscapedBytes(&buffer[0], count);
594       } else {
595         response.PutCString("-1");
596         if (save_errno)
597           response.Printf(",%x", system_errno_to_gdb(save_errno));
598       }
599       return SendPacketNoLock(response.GetString());
600     }
601   }
602   return SendErrorResponse(21);
603 }
604 
605 GDBRemoteCommunication::PacketResult
606 GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite(
607     StringExtractorGDBRemote &packet) {
608   packet.SetFilePos(::strlen("vFile:pwrite:"));
609 
610   StreamGDBRemote response;
611   response.PutChar('F');
612 
613   int fd = packet.GetS32(-1, 16);
614   if (packet.GetChar() == ',') {
615     off_t offset = packet.GetHexMaxU32(false, UINT32_MAX);
616     if (packet.GetChar() == ',') {
617       std::string buffer;
618       if (packet.GetEscapedBinaryData(buffer)) {
619         NativeFile file(fd, File::eOpenOptionWriteOnly, false);
620         size_t count = buffer.size();
621         Status error =
622             file.Write(static_cast<const void *>(&buffer[0]), count, offset);
623         const int save_errno = error.GetError();
624         if (error.Success())
625           response.Printf("%zx", count);
626         else {
627           response.PutCString("-1");
628           if (save_errno)
629             response.Printf(",%x", system_errno_to_gdb(save_errno));
630         }
631       } else {
632         response.Printf("-1,%x", EINVAL);
633       }
634       return SendPacketNoLock(response.GetString());
635     }
636   }
637   return SendErrorResponse(27);
638 }
639 
640 GDBRemoteCommunication::PacketResult
641 GDBRemoteCommunicationServerCommon::Handle_vFile_Size(
642     StringExtractorGDBRemote &packet) {
643   packet.SetFilePos(::strlen("vFile:size:"));
644   std::string path;
645   packet.GetHexByteString(path);
646   if (!path.empty()) {
647     uint64_t Size;
648     if (llvm::sys::fs::file_size(path, Size))
649       return SendErrorResponse(5);
650     StreamString response;
651     response.PutChar('F');
652     response.PutHex64(Size);
653     if (Size == UINT64_MAX) {
654       response.PutChar(',');
655       response.PutHex64(Size); // TODO: replace with Host::GetSyswideErrorCode()
656     }
657     return SendPacketNoLock(response.GetString());
658   }
659   return SendErrorResponse(22);
660 }
661 
662 GDBRemoteCommunication::PacketResult
663 GDBRemoteCommunicationServerCommon::Handle_vFile_Mode(
664     StringExtractorGDBRemote &packet) {
665   packet.SetFilePos(::strlen("vFile:mode:"));
666   std::string path;
667   packet.GetHexByteString(path);
668   if (!path.empty()) {
669     FileSpec file_spec(path);
670     FileSystem::Instance().Resolve(file_spec);
671     std::error_code ec;
672     const uint32_t mode = FileSystem::Instance().GetPermissions(file_spec, ec);
673     StreamString response;
674     if (mode != llvm::sys::fs::perms_not_known)
675       response.Printf("F%x", mode);
676     else
677       response.Printf("F-1,%x", (int)Status(ec).GetError());
678     return SendPacketNoLock(response.GetString());
679   }
680   return SendErrorResponse(23);
681 }
682 
683 GDBRemoteCommunication::PacketResult
684 GDBRemoteCommunicationServerCommon::Handle_vFile_Exists(
685     StringExtractorGDBRemote &packet) {
686   packet.SetFilePos(::strlen("vFile:exists:"));
687   std::string path;
688   packet.GetHexByteString(path);
689   if (!path.empty()) {
690     bool retcode = llvm::sys::fs::exists(path);
691     StreamString response;
692     response.PutChar('F');
693     response.PutChar(',');
694     if (retcode)
695       response.PutChar('1');
696     else
697       response.PutChar('0');
698     return SendPacketNoLock(response.GetString());
699   }
700   return SendErrorResponse(24);
701 }
702 
703 GDBRemoteCommunication::PacketResult
704 GDBRemoteCommunicationServerCommon::Handle_vFile_symlink(
705     StringExtractorGDBRemote &packet) {
706   packet.SetFilePos(::strlen("vFile:symlink:"));
707   std::string dst, src;
708   packet.GetHexByteStringTerminatedBy(dst, ',');
709   packet.GetChar(); // Skip ',' char
710   packet.GetHexByteString(src);
711 
712   FileSpec src_spec(src);
713   FileSystem::Instance().Resolve(src_spec);
714   Status error = FileSystem::Instance().Symlink(src_spec, FileSpec(dst));
715 
716   StreamString response;
717   response.Printf("F%x,%x", error.GetError(), error.GetError());
718   return SendPacketNoLock(response.GetString());
719 }
720 
721 GDBRemoteCommunication::PacketResult
722 GDBRemoteCommunicationServerCommon::Handle_vFile_unlink(
723     StringExtractorGDBRemote &packet) {
724   packet.SetFilePos(::strlen("vFile:unlink:"));
725   std::string path;
726   packet.GetHexByteString(path);
727   Status error(llvm::sys::fs::remove(path));
728   StreamString response;
729   response.Printf("F%x,%x", error.GetError(),
730                   system_errno_to_gdb(error.GetError()));
731   return SendPacketNoLock(response.GetString());
732 }
733 
734 GDBRemoteCommunication::PacketResult
735 GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell(
736     StringExtractorGDBRemote &packet) {
737   packet.SetFilePos(::strlen("qPlatform_shell:"));
738   std::string path;
739   std::string working_dir;
740   packet.GetHexByteStringTerminatedBy(path, ',');
741   if (!path.empty()) {
742     if (packet.GetChar() == ',') {
743       // FIXME: add timeout to qPlatform_shell packet
744       // uint32_t timeout = packet.GetHexMaxU32(false, 32);
745       if (packet.GetChar() == ',')
746         packet.GetHexByteString(working_dir);
747       int status, signo;
748       std::string output;
749       FileSpec working_spec(working_dir);
750       FileSystem::Instance().Resolve(working_spec);
751       Status err =
752           Host::RunShellCommand(path.c_str(), working_spec, &status, &signo,
753                                 &output, std::chrono::seconds(10));
754       StreamGDBRemote response;
755       if (err.Fail()) {
756         response.PutCString("F,");
757         response.PutHex32(UINT32_MAX);
758       } else {
759         response.PutCString("F,");
760         response.PutHex32(status);
761         response.PutChar(',');
762         response.PutHex32(signo);
763         response.PutChar(',');
764         response.PutEscapedBytes(output.c_str(), output.size());
765       }
766       return SendPacketNoLock(response.GetString());
767     }
768   }
769   return SendErrorResponse(24);
770 }
771 
772 template <typename T, typename U>
773 static void fill_clamp(T &dest, U src, typename T::value_type fallback) {
774   static_assert(std::is_unsigned<typename T::value_type>::value,
775                 "Destination type must be unsigned.");
776   using UU = std::make_unsigned_t<U>;
777   constexpr auto T_max = std::numeric_limits<typename T::value_type>::max();
778   dest = src >= 0 && static_cast<UU>(src) <= T_max ? src : fallback;
779 }
780 
781 GDBRemoteCommunication::PacketResult
782 GDBRemoteCommunicationServerCommon::Handle_vFile_FStat(
783     StringExtractorGDBRemote &packet) {
784   StreamGDBRemote response;
785   packet.SetFilePos(::strlen("vFile:fstat:"));
786   int fd = packet.GetS32(-1, 16);
787 
788   struct stat file_stats;
789   if (::fstat(fd, &file_stats) == -1) {
790     const int save_errno = errno;
791     response.Printf("F-1,%x", system_errno_to_gdb(save_errno));
792     return SendPacketNoLock(response.GetString());
793   }
794 
795   GDBRemoteFStatData data;
796   fill_clamp(data.gdb_st_dev, file_stats.st_dev, 0);
797   fill_clamp(data.gdb_st_ino, file_stats.st_ino, 0);
798   data.gdb_st_mode = file_stats.st_mode;
799   fill_clamp(data.gdb_st_nlink, file_stats.st_nlink, UINT32_MAX);
800   fill_clamp(data.gdb_st_uid, file_stats.st_uid, 0);
801   fill_clamp(data.gdb_st_gid, file_stats.st_gid, 0);
802   fill_clamp(data.gdb_st_rdev, file_stats.st_rdev, 0);
803   data.gdb_st_size = file_stats.st_size;
804 #if !defined(_WIN32)
805   data.gdb_st_blksize = file_stats.st_blksize;
806   data.gdb_st_blocks = file_stats.st_blocks;
807 #else
808   data.gdb_st_blksize = 0;
809   data.gdb_st_blocks = 0;
810 #endif
811   fill_clamp(data.gdb_st_atime, file_stats.st_atime, 0);
812   fill_clamp(data.gdb_st_mtime, file_stats.st_mtime, 0);
813   fill_clamp(data.gdb_st_ctime, file_stats.st_ctime, 0);
814 
815   response.Printf("F%zx;", sizeof(data));
816   response.PutEscapedBytes(&data, sizeof(data));
817   return SendPacketNoLock(response.GetString());
818 }
819 
820 GDBRemoteCommunication::PacketResult
821 GDBRemoteCommunicationServerCommon::Handle_vFile_Stat(
822     StringExtractorGDBRemote &packet) {
823   return SendUnimplementedResponse(
824       "GDBRemoteCommunicationServerCommon::Handle_vFile_Stat() unimplemented");
825 }
826 
827 GDBRemoteCommunication::PacketResult
828 GDBRemoteCommunicationServerCommon::Handle_vFile_MD5(
829     StringExtractorGDBRemote &packet) {
830   packet.SetFilePos(::strlen("vFile:MD5:"));
831   std::string path;
832   packet.GetHexByteString(path);
833   if (!path.empty()) {
834     StreamGDBRemote response;
835     auto Result = llvm::sys::fs::md5_contents(path);
836     if (!Result) {
837       response.PutCString("F,");
838       response.PutCString("x");
839     } else {
840       response.PutCString("F,");
841       response.PutHex64(Result->low());
842       response.PutHex64(Result->high());
843     }
844     return SendPacketNoLock(response.GetString());
845   }
846   return SendErrorResponse(25);
847 }
848 
849 GDBRemoteCommunication::PacketResult
850 GDBRemoteCommunicationServerCommon::Handle_qPlatform_mkdir(
851     StringExtractorGDBRemote &packet) {
852   packet.SetFilePos(::strlen("qPlatform_mkdir:"));
853   mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX);
854   if (packet.GetChar() == ',') {
855     std::string path;
856     packet.GetHexByteString(path);
857     Status error(llvm::sys::fs::create_directory(path, mode));
858 
859     StreamGDBRemote response;
860     response.Printf("F%x", error.GetError());
861 
862     return SendPacketNoLock(response.GetString());
863   }
864   return SendErrorResponse(20);
865 }
866 
867 GDBRemoteCommunication::PacketResult
868 GDBRemoteCommunicationServerCommon::Handle_qPlatform_chmod(
869     StringExtractorGDBRemote &packet) {
870   packet.SetFilePos(::strlen("qPlatform_chmod:"));
871 
872   auto perms =
873       static_cast<llvm::sys::fs::perms>(packet.GetHexMaxU32(false, UINT32_MAX));
874   if (packet.GetChar() == ',') {
875     std::string path;
876     packet.GetHexByteString(path);
877     Status error(llvm::sys::fs::setPermissions(path, perms));
878 
879     StreamGDBRemote response;
880     response.Printf("F%x", error.GetError());
881 
882     return SendPacketNoLock(response.GetString());
883   }
884   return SendErrorResponse(19);
885 }
886 
887 GDBRemoteCommunication::PacketResult
888 GDBRemoteCommunicationServerCommon::Handle_qSupported(
889     StringExtractorGDBRemote &packet) {
890   // Parse client-indicated features.
891   llvm::SmallVector<llvm::StringRef, 4> client_features;
892   packet.GetStringRef().split(client_features, ';');
893   return SendPacketNoLock(llvm::join(HandleFeatures(client_features), ";"));
894 }
895 
896 GDBRemoteCommunication::PacketResult
897 GDBRemoteCommunicationServerCommon::Handle_QSetDetachOnError(
898     StringExtractorGDBRemote &packet) {
899   packet.SetFilePos(::strlen("QSetDetachOnError:"));
900   if (packet.GetU32(0))
901     m_process_launch_info.GetFlags().Set(eLaunchFlagDetachOnError);
902   else
903     m_process_launch_info.GetFlags().Clear(eLaunchFlagDetachOnError);
904   return SendOKResponse();
905 }
906 
907 GDBRemoteCommunication::PacketResult
908 GDBRemoteCommunicationServerCommon::Handle_QStartNoAckMode(
909     StringExtractorGDBRemote &packet) {
910   // Send response first before changing m_send_acks to we ack this packet
911   PacketResult packet_result = SendOKResponse();
912   m_send_acks = false;
913   return packet_result;
914 }
915 
916 GDBRemoteCommunication::PacketResult
917 GDBRemoteCommunicationServerCommon::Handle_QSetSTDIN(
918     StringExtractorGDBRemote &packet) {
919   packet.SetFilePos(::strlen("QSetSTDIN:"));
920   FileAction file_action;
921   std::string path;
922   packet.GetHexByteString(path);
923   const bool read = true;
924   const bool write = false;
925   if (file_action.Open(STDIN_FILENO, FileSpec(path), read, write)) {
926     m_process_launch_info.AppendFileAction(file_action);
927     return SendOKResponse();
928   }
929   return SendErrorResponse(15);
930 }
931 
932 GDBRemoteCommunication::PacketResult
933 GDBRemoteCommunicationServerCommon::Handle_QSetSTDOUT(
934     StringExtractorGDBRemote &packet) {
935   packet.SetFilePos(::strlen("QSetSTDOUT:"));
936   FileAction file_action;
937   std::string path;
938   packet.GetHexByteString(path);
939   const bool read = false;
940   const bool write = true;
941   if (file_action.Open(STDOUT_FILENO, FileSpec(path), read, write)) {
942     m_process_launch_info.AppendFileAction(file_action);
943     return SendOKResponse();
944   }
945   return SendErrorResponse(16);
946 }
947 
948 GDBRemoteCommunication::PacketResult
949 GDBRemoteCommunicationServerCommon::Handle_QSetSTDERR(
950     StringExtractorGDBRemote &packet) {
951   packet.SetFilePos(::strlen("QSetSTDERR:"));
952   FileAction file_action;
953   std::string path;
954   packet.GetHexByteString(path);
955   const bool read = false;
956   const bool write = true;
957   if (file_action.Open(STDERR_FILENO, FileSpec(path), read, write)) {
958     m_process_launch_info.AppendFileAction(file_action);
959     return SendOKResponse();
960   }
961   return SendErrorResponse(17);
962 }
963 
964 GDBRemoteCommunication::PacketResult
965 GDBRemoteCommunicationServerCommon::Handle_qLaunchSuccess(
966     StringExtractorGDBRemote &packet) {
967   if (m_process_launch_error.Success())
968     return SendOKResponse();
969   StreamString response;
970   response.PutChar('E');
971   response.PutCString(m_process_launch_error.AsCString("<unknown error>"));
972   return SendPacketNoLock(response.GetString());
973 }
974 
975 GDBRemoteCommunication::PacketResult
976 GDBRemoteCommunicationServerCommon::Handle_QEnvironment(
977     StringExtractorGDBRemote &packet) {
978   packet.SetFilePos(::strlen("QEnvironment:"));
979   const uint32_t bytes_left = packet.GetBytesLeft();
980   if (bytes_left > 0) {
981     m_process_launch_info.GetEnvironment().insert(packet.Peek());
982     return SendOKResponse();
983   }
984   return SendErrorResponse(12);
985 }
986 
987 GDBRemoteCommunication::PacketResult
988 GDBRemoteCommunicationServerCommon::Handle_QEnvironmentHexEncoded(
989     StringExtractorGDBRemote &packet) {
990   packet.SetFilePos(::strlen("QEnvironmentHexEncoded:"));
991   const uint32_t bytes_left = packet.GetBytesLeft();
992   if (bytes_left > 0) {
993     std::string str;
994     packet.GetHexByteString(str);
995     m_process_launch_info.GetEnvironment().insert(str);
996     return SendOKResponse();
997   }
998   return SendErrorResponse(12);
999 }
1000 
1001 GDBRemoteCommunication::PacketResult
1002 GDBRemoteCommunicationServerCommon::Handle_QLaunchArch(
1003     StringExtractorGDBRemote &packet) {
1004   packet.SetFilePos(::strlen("QLaunchArch:"));
1005   const uint32_t bytes_left = packet.GetBytesLeft();
1006   if (bytes_left > 0) {
1007     const char *arch_triple = packet.Peek();
1008     m_process_launch_info.SetArchitecture(
1009         HostInfo::GetAugmentedArchSpec(arch_triple));
1010     return SendOKResponse();
1011   }
1012   return SendErrorResponse(13);
1013 }
1014 
1015 GDBRemoteCommunication::PacketResult
1016 GDBRemoteCommunicationServerCommon::Handle_A(StringExtractorGDBRemote &packet) {
1017   // The 'A' packet is the most over designed packet ever here with redundant
1018   // argument indexes, redundant argument lengths and needed hex encoded
1019   // argument string values. Really all that is needed is a comma separated hex
1020   // encoded argument value list, but we will stay true to the documented
1021   // version of the 'A' packet here...
1022 
1023   Log *log = GetLog(LLDBLog::Process);
1024   int actual_arg_index = 0;
1025 
1026   packet.SetFilePos(1); // Skip the 'A'
1027   bool success = true;
1028   while (success && packet.GetBytesLeft() > 0) {
1029     // Decode the decimal argument string length. This length is the number of
1030     // hex nibbles in the argument string value.
1031     const uint32_t arg_len = packet.GetU32(UINT32_MAX);
1032     if (arg_len == UINT32_MAX)
1033       success = false;
1034     else {
1035       // Make sure the argument hex string length is followed by a comma
1036       if (packet.GetChar() != ',')
1037         success = false;
1038       else {
1039         // Decode the argument index. We ignore this really because who would
1040         // really send down the arguments in a random order???
1041         const uint32_t arg_idx = packet.GetU32(UINT32_MAX);
1042         if (arg_idx == UINT32_MAX)
1043           success = false;
1044         else {
1045           // Make sure the argument index is followed by a comma
1046           if (packet.GetChar() != ',')
1047             success = false;
1048           else {
1049             // Decode the argument string value from hex bytes back into a UTF8
1050             // string and make sure the length matches the one supplied in the
1051             // packet
1052             std::string arg;
1053             if (packet.GetHexByteStringFixedLength(arg, arg_len) !=
1054                 (arg_len / 2))
1055               success = false;
1056             else {
1057               // If there are any bytes left
1058               if (packet.GetBytesLeft()) {
1059                 if (packet.GetChar() != ',')
1060                   success = false;
1061               }
1062 
1063               if (success) {
1064                 if (arg_idx == 0)
1065                   m_process_launch_info.GetExecutableFile().SetFile(
1066                       arg, FileSpec::Style::native);
1067                 m_process_launch_info.GetArguments().AppendArgument(arg);
1068                 LLDB_LOGF(log, "LLGSPacketHandler::%s added arg %d: \"%s\"",
1069                           __FUNCTION__, actual_arg_index, arg.c_str());
1070                 ++actual_arg_index;
1071               }
1072             }
1073           }
1074         }
1075       }
1076     }
1077   }
1078 
1079   if (success) {
1080     m_process_launch_error = LaunchProcess();
1081     if (m_process_launch_error.Success())
1082       return SendOKResponse();
1083     LLDB_LOG(log, "failed to launch exe: {0}", m_process_launch_error);
1084   }
1085   return SendErrorResponse(8);
1086 }
1087 
1088 GDBRemoteCommunication::PacketResult
1089 GDBRemoteCommunicationServerCommon::Handle_qEcho(
1090     StringExtractorGDBRemote &packet) {
1091   // Just echo back the exact same packet for qEcho...
1092   return SendPacketNoLock(packet.GetStringRef());
1093 }
1094 
1095 GDBRemoteCommunication::PacketResult
1096 GDBRemoteCommunicationServerCommon::Handle_qModuleInfo(
1097     StringExtractorGDBRemote &packet) {
1098   packet.SetFilePos(::strlen("qModuleInfo:"));
1099 
1100   std::string module_path;
1101   packet.GetHexByteStringTerminatedBy(module_path, ';');
1102   if (module_path.empty())
1103     return SendErrorResponse(1);
1104 
1105   if (packet.GetChar() != ';')
1106     return SendErrorResponse(2);
1107 
1108   std::string triple;
1109   packet.GetHexByteString(triple);
1110 
1111   ModuleSpec matched_module_spec = GetModuleInfo(module_path, triple);
1112   if (!matched_module_spec.GetFileSpec())
1113     return SendErrorResponse(3);
1114 
1115   const auto file_offset = matched_module_spec.GetObjectOffset();
1116   const auto file_size = matched_module_spec.GetObjectSize();
1117   const auto uuid_str = matched_module_spec.GetUUID().GetAsString("");
1118 
1119   StreamGDBRemote response;
1120 
1121   if (uuid_str.empty()) {
1122     auto Result = llvm::sys::fs::md5_contents(
1123         matched_module_spec.GetFileSpec().GetPath());
1124     if (!Result)
1125       return SendErrorResponse(5);
1126     response.PutCString("md5:");
1127     response.PutStringAsRawHex8(Result->digest());
1128   } else {
1129     response.PutCString("uuid:");
1130     response.PutStringAsRawHex8(uuid_str);
1131   }
1132   response.PutChar(';');
1133 
1134   const auto &module_arch = matched_module_spec.GetArchitecture();
1135   response.PutCString("triple:");
1136   response.PutStringAsRawHex8(module_arch.GetTriple().getTriple());
1137   response.PutChar(';');
1138 
1139   response.PutCString("file_path:");
1140   response.PutStringAsRawHex8(
1141       matched_module_spec.GetFileSpec().GetPath().c_str());
1142   response.PutChar(';');
1143   response.PutCString("file_offset:");
1144   response.PutHex64(file_offset);
1145   response.PutChar(';');
1146   response.PutCString("file_size:");
1147   response.PutHex64(file_size);
1148   response.PutChar(';');
1149 
1150   return SendPacketNoLock(response.GetString());
1151 }
1152 
1153 GDBRemoteCommunication::PacketResult
1154 GDBRemoteCommunicationServerCommon::Handle_jModulesInfo(
1155     StringExtractorGDBRemote &packet) {
1156   namespace json = llvm::json;
1157 
1158   packet.SetFilePos(::strlen("jModulesInfo:"));
1159 
1160   StructuredData::ObjectSP object_sp = StructuredData::ParseJSON(packet.Peek());
1161   if (!object_sp)
1162     return SendErrorResponse(1);
1163 
1164   StructuredData::Array *packet_array = object_sp->GetAsArray();
1165   if (!packet_array)
1166     return SendErrorResponse(2);
1167 
1168   json::Array response_array;
1169   for (size_t i = 0; i < packet_array->GetSize(); ++i) {
1170     StructuredData::Dictionary *query =
1171         packet_array->GetItemAtIndex(i)->GetAsDictionary();
1172     if (!query)
1173       continue;
1174     llvm::StringRef file, triple;
1175     if (!query->GetValueForKeyAsString("file", file) ||
1176         !query->GetValueForKeyAsString("triple", triple))
1177       continue;
1178 
1179     ModuleSpec matched_module_spec = GetModuleInfo(file, triple);
1180     if (!matched_module_spec.GetFileSpec())
1181       continue;
1182 
1183     const auto file_offset = matched_module_spec.GetObjectOffset();
1184     const auto file_size = matched_module_spec.GetObjectSize();
1185     const auto uuid_str = matched_module_spec.GetUUID().GetAsString("");
1186     if (uuid_str.empty())
1187       continue;
1188     const auto triple_str =
1189         matched_module_spec.GetArchitecture().GetTriple().getTriple();
1190     const auto file_path = matched_module_spec.GetFileSpec().GetPath();
1191 
1192     json::Object response{{"uuid", uuid_str},
1193                           {"triple", triple_str},
1194                           {"file_path", file_path},
1195                           {"file_offset", static_cast<int64_t>(file_offset)},
1196                           {"file_size", static_cast<int64_t>(file_size)}};
1197     response_array.push_back(std::move(response));
1198   }
1199 
1200   StreamString response;
1201   response.AsRawOstream() << std::move(response_array);
1202   StreamGDBRemote escaped_response;
1203   escaped_response.PutEscapedBytes(response.GetString().data(),
1204                                    response.GetSize());
1205   return SendPacketNoLock(escaped_response.GetString());
1206 }
1207 
1208 void GDBRemoteCommunicationServerCommon::CreateProcessInfoResponse(
1209     const ProcessInstanceInfo &proc_info, StreamString &response) {
1210   response.Printf(
1211       "pid:%" PRIu64 ";ppid:%" PRIu64 ";uid:%i;gid:%i;euid:%i;egid:%i;",
1212       proc_info.GetProcessID(), proc_info.GetParentProcessID(),
1213       proc_info.GetUserID(), proc_info.GetGroupID(),
1214       proc_info.GetEffectiveUserID(), proc_info.GetEffectiveGroupID());
1215   response.PutCString("name:");
1216   response.PutStringAsRawHex8(proc_info.GetExecutableFile().GetPath().c_str());
1217 
1218   response.PutChar(';');
1219   response.PutCString("args:");
1220   response.PutStringAsRawHex8(proc_info.GetArg0());
1221   for (auto &arg : proc_info.GetArguments()) {
1222     response.PutChar('-');
1223     response.PutStringAsRawHex8(arg.ref());
1224   }
1225 
1226   response.PutChar(';');
1227   const ArchSpec &proc_arch = proc_info.GetArchitecture();
1228   if (proc_arch.IsValid()) {
1229     const llvm::Triple &proc_triple = proc_arch.GetTriple();
1230     response.PutCString("triple:");
1231     response.PutStringAsRawHex8(proc_triple.getTriple());
1232     response.PutChar(';');
1233   }
1234 }
1235 
1236 void GDBRemoteCommunicationServerCommon::
1237     CreateProcessInfoResponse_DebugServerStyle(
1238         const ProcessInstanceInfo &proc_info, StreamString &response) {
1239   response.Printf("pid:%" PRIx64 ";parent-pid:%" PRIx64
1240                   ";real-uid:%x;real-gid:%x;effective-uid:%x;effective-gid:%x;",
1241                   proc_info.GetProcessID(), proc_info.GetParentProcessID(),
1242                   proc_info.GetUserID(), proc_info.GetGroupID(),
1243                   proc_info.GetEffectiveUserID(),
1244                   proc_info.GetEffectiveGroupID());
1245 
1246   const ArchSpec &proc_arch = proc_info.GetArchitecture();
1247   if (proc_arch.IsValid()) {
1248     const llvm::Triple &proc_triple = proc_arch.GetTriple();
1249 #if defined(__APPLE__)
1250     // We'll send cputype/cpusubtype.
1251     const uint32_t cpu_type = proc_arch.GetMachOCPUType();
1252     if (cpu_type != 0)
1253       response.Printf("cputype:%" PRIx32 ";", cpu_type);
1254 
1255     const uint32_t cpu_subtype = proc_arch.GetMachOCPUSubType();
1256     if (cpu_subtype != 0)
1257       response.Printf("cpusubtype:%" PRIx32 ";", cpu_subtype);
1258 
1259     const std::string vendor = proc_triple.getVendorName().str();
1260     if (!vendor.empty())
1261       response.Printf("vendor:%s;", vendor.c_str());
1262 #else
1263     // We'll send the triple.
1264     response.PutCString("triple:");
1265     response.PutStringAsRawHex8(proc_triple.getTriple());
1266     response.PutChar(';');
1267 #endif
1268     std::string ostype = std::string(proc_triple.getOSName());
1269     // Adjust so ostype reports ios for Apple/ARM and Apple/ARM64.
1270     if (proc_triple.getVendor() == llvm::Triple::Apple) {
1271       switch (proc_triple.getArch()) {
1272       case llvm::Triple::arm:
1273       case llvm::Triple::thumb:
1274       case llvm::Triple::aarch64:
1275       case llvm::Triple::aarch64_32:
1276         ostype = "ios";
1277         break;
1278       default:
1279         // No change.
1280         break;
1281       }
1282     }
1283     response.Printf("ostype:%s;", ostype.c_str());
1284 
1285     switch (proc_arch.GetByteOrder()) {
1286     case lldb::eByteOrderLittle:
1287       response.PutCString("endian:little;");
1288       break;
1289     case lldb::eByteOrderBig:
1290       response.PutCString("endian:big;");
1291       break;
1292     case lldb::eByteOrderPDP:
1293       response.PutCString("endian:pdp;");
1294       break;
1295     default:
1296       // Nothing.
1297       break;
1298     }
1299     // In case of MIPS64, pointer size is depend on ELF ABI For N32 the pointer
1300     // size is 4 and for N64 it is 8
1301     std::string abi = proc_arch.GetTargetABI();
1302     if (!abi.empty())
1303       response.Printf("elf_abi:%s;", abi.c_str());
1304     response.Printf("ptrsize:%d;", proc_arch.GetAddressByteSize());
1305   }
1306 }
1307 
1308 FileSpec GDBRemoteCommunicationServerCommon::FindModuleFile(
1309     const std::string &module_path, const ArchSpec &arch) {
1310 #ifdef __ANDROID__
1311   return HostInfoAndroid::ResolveLibraryPath(module_path, arch);
1312 #else
1313   FileSpec file_spec(module_path);
1314   FileSystem::Instance().Resolve(file_spec);
1315   return file_spec;
1316 #endif
1317 }
1318 
1319 ModuleSpec
1320 GDBRemoteCommunicationServerCommon::GetModuleInfo(llvm::StringRef module_path,
1321                                                   llvm::StringRef triple) {
1322   ArchSpec arch(triple);
1323 
1324   FileSpec req_module_path_spec(module_path);
1325   FileSystem::Instance().Resolve(req_module_path_spec);
1326 
1327   const FileSpec module_path_spec =
1328       FindModuleFile(req_module_path_spec.GetPath(), arch);
1329 
1330   lldb::offset_t file_offset = 0;
1331   lldb::offset_t file_size = 0;
1332 #ifdef __ANDROID__
1333   // In Android API level 23 and above, dynamic loader is able to load .so file
1334   // directly from zip file. In that case, module_path will be
1335   // "zip_path!/so_path". Resolve the zip file path, .so file offset and size.
1336   ZipFileResolver::FileKind file_kind = ZipFileResolver::eFileKindInvalid;
1337   std::string file_path;
1338   if (!ZipFileResolver::ResolveSharedLibraryPath(
1339           module_path_spec, file_kind, file_path, file_offset, file_size)) {
1340     return ModuleSpec();
1341   }
1342   lldbassert(file_kind != ZipFileResolver::eFileKindInvalid);
1343   // For zip .so file, this file_path will contain only the actual zip file
1344   // path for the object file processing. Otherwise it is the same as
1345   // module_path.
1346   const FileSpec actual_module_path_spec(file_path);
1347 #else
1348   // It is just module_path_spec reference for other platforms.
1349   const FileSpec &actual_module_path_spec = module_path_spec;
1350 #endif
1351 
1352   const ModuleSpec module_spec(actual_module_path_spec, arch);
1353 
1354   ModuleSpecList module_specs;
1355   if (!ObjectFile::GetModuleSpecifications(actual_module_path_spec, file_offset,
1356                                            file_size, module_specs))
1357     return ModuleSpec();
1358 
1359   ModuleSpec matched_module_spec;
1360   if (!module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec))
1361     return ModuleSpec();
1362 
1363 #ifdef __ANDROID__
1364   if (file_kind == ZipFileResolver::eFileKindZip) {
1365     // For zip .so file, matched_module_spec contains only the actual zip file
1366     // path for the object file processing. Overwrite the matched_module_spec
1367     // file spec with the original module_path_spec to pass "zip_path!/so_path"
1368     // through to PlatformAndroid::DownloadModuleSlice.
1369     *matched_module_spec.GetFileSpecPtr() = module_path_spec;
1370   }
1371 #endif
1372 
1373   return matched_module_spec;
1374 }
1375 
1376 std::vector<std::string> GDBRemoteCommunicationServerCommon::HandleFeatures(
1377     const llvm::ArrayRef<llvm::StringRef> client_features) {
1378   // 128KBytes is a reasonable max packet size--debugger can always use less.
1379   constexpr uint32_t max_packet_size = 128 * 1024;
1380 
1381   // Features common to platform server and llgs.
1382   return {
1383       llvm::formatv("PacketSize={0}", max_packet_size),
1384       "QStartNoAckMode+",
1385       "qEcho+",
1386       "native-signals+",
1387   };
1388 }
1389