1 //===-- GDBRemoteCommunicationClient.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 "GDBRemoteCommunicationClient.h"
10 
11 #include <cmath>
12 #include <sys/stat.h>
13 
14 #include <numeric>
15 #include <sstream>
16 
17 #include "lldb/Core/ModuleSpec.h"
18 #include "lldb/Host/HostInfo.h"
19 #include "lldb/Host/XML.h"
20 #include "lldb/Symbol/Symbol.h"
21 #include "lldb/Target/MemoryRegionInfo.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Target/UnixSignals.h"
24 #include "lldb/Utility/Args.h"
25 #include "lldb/Utility/DataBufferHeap.h"
26 #include "lldb/Utility/LLDBAssert.h"
27 #include "lldb/Utility/LLDBLog.h"
28 #include "lldb/Utility/Log.h"
29 #include "lldb/Utility/State.h"
30 #include "lldb/Utility/StreamString.h"
31 
32 #include "ProcessGDBRemote.h"
33 #include "ProcessGDBRemoteLog.h"
34 #include "lldb/Host/Config.h"
35 #include "lldb/Utility/StringExtractorGDBRemote.h"
36 
37 #include "llvm/ADT/StringSwitch.h"
38 #include "llvm/Support/JSON.h"
39 
40 #if defined(HAVE_LIBCOMPRESSION)
41 #include <compression.h>
42 #endif
43 
44 using namespace lldb;
45 using namespace lldb_private::process_gdb_remote;
46 using namespace lldb_private;
47 using namespace std::chrono;
48 
49 llvm::raw_ostream &process_gdb_remote::operator<<(llvm::raw_ostream &os,
50                                                   const QOffsets &offsets) {
51   return os << llvm::formatv(
52              "QOffsets({0}, [{1:@[x]}])", offsets.segments,
53              llvm::make_range(offsets.offsets.begin(), offsets.offsets.end()));
54 }
55 
56 // GDBRemoteCommunicationClient constructor
57 GDBRemoteCommunicationClient::GDBRemoteCommunicationClient()
58     : GDBRemoteClientBase("gdb-remote.client", "gdb-remote.client.rx_packet"),
59 
60       m_supports_qProcessInfoPID(true), m_supports_qfProcessInfo(true),
61       m_supports_qUserName(true), m_supports_qGroupName(true),
62       m_supports_qThreadStopInfo(true), m_supports_z0(true),
63       m_supports_z1(true), m_supports_z2(true), m_supports_z3(true),
64       m_supports_z4(true), m_supports_QEnvironment(true),
65       m_supports_QEnvironmentHexEncoded(true), m_supports_qSymbol(true),
66       m_qSymbol_requests_done(false), m_supports_qModuleInfo(true),
67       m_supports_jThreadsInfo(true), m_supports_jModulesInfo(true),
68       m_supports_vFileSize(true), m_supports_vFileMode(true),
69       m_supports_vFileExists(true), m_supports_vRun(true),
70 
71       m_host_arch(), m_process_arch(), m_os_build(), m_os_kernel(),
72       m_hostname(), m_gdb_server_name(), m_default_packet_timeout(0),
73       m_qSupported_response(), m_supported_async_json_packets_sp(),
74       m_qXfer_memory_map() {}
75 
76 // Destructor
77 GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient() {
78   if (IsConnected())
79     Disconnect();
80 }
81 
82 bool GDBRemoteCommunicationClient::HandshakeWithServer(Status *error_ptr) {
83   ResetDiscoverableSettings(false);
84 
85   // Start the read thread after we send the handshake ack since if we fail to
86   // send the handshake ack, there is no reason to continue...
87   std::chrono::steady_clock::time_point start_of_handshake =
88       std::chrono::steady_clock::now();
89   if (SendAck()) {
90     // The return value from QueryNoAckModeSupported() is true if the packet
91     // was sent and _any_ response (including UNIMPLEMENTED) was received), or
92     // false if no response was received. This quickly tells us if we have a
93     // live connection to a remote GDB server...
94     if (QueryNoAckModeSupported()) {
95       return true;
96     } else {
97       std::chrono::steady_clock::time_point end_of_handshake =
98           std::chrono::steady_clock::now();
99       auto handshake_timeout =
100           std::chrono::duration<double>(end_of_handshake - start_of_handshake)
101               .count();
102       if (error_ptr) {
103         if (!IsConnected())
104           error_ptr->SetErrorString("Connection shut down by remote side "
105                                     "while waiting for reply to initial "
106                                     "handshake packet");
107         else
108           error_ptr->SetErrorStringWithFormat(
109               "failed to get reply to handshake packet within timeout of "
110               "%.1f seconds",
111               handshake_timeout);
112       }
113     }
114   } else {
115     if (error_ptr)
116       error_ptr->SetErrorString("failed to send the handshake ack");
117   }
118   return false;
119 }
120 
121 bool GDBRemoteCommunicationClient::GetEchoSupported() {
122   if (m_supports_qEcho == eLazyBoolCalculate) {
123     GetRemoteQSupported();
124   }
125   return m_supports_qEcho == eLazyBoolYes;
126 }
127 
128 bool GDBRemoteCommunicationClient::GetQPassSignalsSupported() {
129   if (m_supports_QPassSignals == eLazyBoolCalculate) {
130     GetRemoteQSupported();
131   }
132   return m_supports_QPassSignals == eLazyBoolYes;
133 }
134 
135 bool GDBRemoteCommunicationClient::GetAugmentedLibrariesSVR4ReadSupported() {
136   if (m_supports_augmented_libraries_svr4_read == eLazyBoolCalculate) {
137     GetRemoteQSupported();
138   }
139   return m_supports_augmented_libraries_svr4_read == eLazyBoolYes;
140 }
141 
142 bool GDBRemoteCommunicationClient::GetQXferLibrariesSVR4ReadSupported() {
143   if (m_supports_qXfer_libraries_svr4_read == eLazyBoolCalculate) {
144     GetRemoteQSupported();
145   }
146   return m_supports_qXfer_libraries_svr4_read == eLazyBoolYes;
147 }
148 
149 bool GDBRemoteCommunicationClient::GetQXferLibrariesReadSupported() {
150   if (m_supports_qXfer_libraries_read == eLazyBoolCalculate) {
151     GetRemoteQSupported();
152   }
153   return m_supports_qXfer_libraries_read == eLazyBoolYes;
154 }
155 
156 bool GDBRemoteCommunicationClient::GetQXferAuxvReadSupported() {
157   if (m_supports_qXfer_auxv_read == eLazyBoolCalculate) {
158     GetRemoteQSupported();
159   }
160   return m_supports_qXfer_auxv_read == eLazyBoolYes;
161 }
162 
163 bool GDBRemoteCommunicationClient::GetQXferFeaturesReadSupported() {
164   if (m_supports_qXfer_features_read == eLazyBoolCalculate) {
165     GetRemoteQSupported();
166   }
167   return m_supports_qXfer_features_read == eLazyBoolYes;
168 }
169 
170 bool GDBRemoteCommunicationClient::GetQXferMemoryMapReadSupported() {
171   if (m_supports_qXfer_memory_map_read == eLazyBoolCalculate) {
172     GetRemoteQSupported();
173   }
174   return m_supports_qXfer_memory_map_read == eLazyBoolYes;
175 }
176 
177 bool GDBRemoteCommunicationClient::GetQXferSigInfoReadSupported() {
178   if (m_supports_qXfer_siginfo_read == eLazyBoolCalculate) {
179     GetRemoteQSupported();
180   }
181   return m_supports_qXfer_siginfo_read == eLazyBoolYes;
182 }
183 
184 uint64_t GDBRemoteCommunicationClient::GetRemoteMaxPacketSize() {
185   if (m_max_packet_size == 0) {
186     GetRemoteQSupported();
187   }
188   return m_max_packet_size;
189 }
190 
191 bool GDBRemoteCommunicationClient::QueryNoAckModeSupported() {
192   if (m_supports_not_sending_acks == eLazyBoolCalculate) {
193     m_send_acks = true;
194     m_supports_not_sending_acks = eLazyBoolNo;
195 
196     // This is the first real packet that we'll send in a debug session and it
197     // may take a little longer than normal to receive a reply.  Wait at least
198     // 6 seconds for a reply to this packet.
199 
200     ScopedTimeout timeout(*this, std::max(GetPacketTimeout(), seconds(6)));
201 
202     StringExtractorGDBRemote response;
203     if (SendPacketAndWaitForResponse("QStartNoAckMode", response) ==
204         PacketResult::Success) {
205       if (response.IsOKResponse()) {
206         m_send_acks = false;
207         m_supports_not_sending_acks = eLazyBoolYes;
208       }
209       return true;
210     }
211   }
212   return false;
213 }
214 
215 void GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported() {
216   if (m_supports_threads_in_stop_reply == eLazyBoolCalculate) {
217     m_supports_threads_in_stop_reply = eLazyBoolNo;
218 
219     StringExtractorGDBRemote response;
220     if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response) ==
221         PacketResult::Success) {
222       if (response.IsOKResponse())
223         m_supports_threads_in_stop_reply = eLazyBoolYes;
224     }
225   }
226 }
227 
228 bool GDBRemoteCommunicationClient::GetVAttachOrWaitSupported() {
229   if (m_attach_or_wait_reply == eLazyBoolCalculate) {
230     m_attach_or_wait_reply = eLazyBoolNo;
231 
232     StringExtractorGDBRemote response;
233     if (SendPacketAndWaitForResponse("qVAttachOrWaitSupported", response) ==
234         PacketResult::Success) {
235       if (response.IsOKResponse())
236         m_attach_or_wait_reply = eLazyBoolYes;
237     }
238   }
239   return m_attach_or_wait_reply == eLazyBoolYes;
240 }
241 
242 bool GDBRemoteCommunicationClient::GetSyncThreadStateSupported() {
243   if (m_prepare_for_reg_writing_reply == eLazyBoolCalculate) {
244     m_prepare_for_reg_writing_reply = eLazyBoolNo;
245 
246     StringExtractorGDBRemote response;
247     if (SendPacketAndWaitForResponse("qSyncThreadStateSupported", response) ==
248         PacketResult::Success) {
249       if (response.IsOKResponse())
250         m_prepare_for_reg_writing_reply = eLazyBoolYes;
251     }
252   }
253   return m_prepare_for_reg_writing_reply == eLazyBoolYes;
254 }
255 
256 void GDBRemoteCommunicationClient::ResetDiscoverableSettings(bool did_exec) {
257   if (!did_exec) {
258     // Hard reset everything, this is when we first connect to a GDB server
259     m_supports_not_sending_acks = eLazyBoolCalculate;
260     m_supports_thread_suffix = eLazyBoolCalculate;
261     m_supports_threads_in_stop_reply = eLazyBoolCalculate;
262     m_supports_vCont_c = eLazyBoolCalculate;
263     m_supports_vCont_C = eLazyBoolCalculate;
264     m_supports_vCont_s = eLazyBoolCalculate;
265     m_supports_vCont_S = eLazyBoolCalculate;
266     m_supports_p = eLazyBoolCalculate;
267     m_supports_x = eLazyBoolCalculate;
268     m_supports_QSaveRegisterState = eLazyBoolCalculate;
269     m_qHostInfo_is_valid = eLazyBoolCalculate;
270     m_curr_pid_is_valid = eLazyBoolCalculate;
271     m_qGDBServerVersion_is_valid = eLazyBoolCalculate;
272     m_supports_alloc_dealloc_memory = eLazyBoolCalculate;
273     m_supports_memory_region_info = eLazyBoolCalculate;
274     m_prepare_for_reg_writing_reply = eLazyBoolCalculate;
275     m_attach_or_wait_reply = eLazyBoolCalculate;
276     m_avoid_g_packets = eLazyBoolCalculate;
277     m_supports_multiprocess = eLazyBoolCalculate;
278     m_supports_qSaveCore = eLazyBoolCalculate;
279     m_supports_qXfer_auxv_read = eLazyBoolCalculate;
280     m_supports_qXfer_libraries_read = eLazyBoolCalculate;
281     m_supports_qXfer_libraries_svr4_read = eLazyBoolCalculate;
282     m_supports_qXfer_features_read = eLazyBoolCalculate;
283     m_supports_qXfer_memory_map_read = eLazyBoolCalculate;
284     m_supports_qXfer_siginfo_read = eLazyBoolCalculate;
285     m_supports_augmented_libraries_svr4_read = eLazyBoolCalculate;
286     m_uses_native_signals = eLazyBoolCalculate;
287     m_supports_qProcessInfoPID = true;
288     m_supports_qfProcessInfo = true;
289     m_supports_qUserName = true;
290     m_supports_qGroupName = true;
291     m_supports_qThreadStopInfo = true;
292     m_supports_z0 = true;
293     m_supports_z1 = true;
294     m_supports_z2 = true;
295     m_supports_z3 = true;
296     m_supports_z4 = true;
297     m_supports_QEnvironment = true;
298     m_supports_QEnvironmentHexEncoded = true;
299     m_supports_qSymbol = true;
300     m_qSymbol_requests_done = false;
301     m_supports_qModuleInfo = true;
302     m_host_arch.Clear();
303     m_os_version = llvm::VersionTuple();
304     m_os_build.clear();
305     m_os_kernel.clear();
306     m_hostname.clear();
307     m_gdb_server_name.clear();
308     m_gdb_server_version = UINT32_MAX;
309     m_default_packet_timeout = seconds(0);
310     m_target_vm_page_size = 0;
311     m_max_packet_size = 0;
312     m_qSupported_response.clear();
313     m_supported_async_json_packets_is_valid = false;
314     m_supported_async_json_packets_sp.reset();
315     m_supports_jModulesInfo = true;
316   }
317 
318   // These flags should be reset when we first connect to a GDB server and when
319   // our inferior process execs
320   m_qProcessInfo_is_valid = eLazyBoolCalculate;
321   m_process_arch.Clear();
322 }
323 
324 void GDBRemoteCommunicationClient::GetRemoteQSupported() {
325   // Clear out any capabilities we expect to see in the qSupported response
326   m_supports_qXfer_auxv_read = eLazyBoolNo;
327   m_supports_qXfer_libraries_read = eLazyBoolNo;
328   m_supports_qXfer_libraries_svr4_read = eLazyBoolNo;
329   m_supports_augmented_libraries_svr4_read = eLazyBoolNo;
330   m_supports_qXfer_features_read = eLazyBoolNo;
331   m_supports_qXfer_memory_map_read = eLazyBoolNo;
332   m_supports_qXfer_siginfo_read = eLazyBoolNo;
333   m_supports_multiprocess = eLazyBoolNo;
334   m_supports_qEcho = eLazyBoolNo;
335   m_supports_QPassSignals = eLazyBoolNo;
336   m_supports_memory_tagging = eLazyBoolNo;
337   m_supports_qSaveCore = eLazyBoolNo;
338   m_uses_native_signals = eLazyBoolNo;
339 
340   m_max_packet_size = UINT64_MAX; // It's supposed to always be there, but if
341                                   // not, we assume no limit
342 
343   // build the qSupported packet
344   std::vector<std::string> features = {"xmlRegisters=i386,arm,mips,arc",
345                                        "multiprocess+", "fork-events+",
346                                        "vfork-events+"};
347   StreamString packet;
348   packet.PutCString("qSupported");
349   for (uint32_t i = 0; i < features.size(); ++i) {
350     packet.PutCString(i == 0 ? ":" : ";");
351     packet.PutCString(features[i]);
352   }
353 
354   StringExtractorGDBRemote response;
355   if (SendPacketAndWaitForResponse(packet.GetString(), response) ==
356       PacketResult::Success) {
357     // Hang on to the qSupported packet, so that platforms can do custom
358     // configuration of the transport before attaching/launching the process.
359     m_qSupported_response = response.GetStringRef().str();
360 
361     for (llvm::StringRef x : llvm::split(response.GetStringRef(), ';')) {
362       if (x == "qXfer:auxv:read+")
363         m_supports_qXfer_auxv_read = eLazyBoolYes;
364       else if (x == "qXfer:libraries-svr4:read+")
365         m_supports_qXfer_libraries_svr4_read = eLazyBoolYes;
366       else if (x == "augmented-libraries-svr4-read") {
367         m_supports_qXfer_libraries_svr4_read = eLazyBoolYes; // implied
368         m_supports_augmented_libraries_svr4_read = eLazyBoolYes;
369       } else if (x == "qXfer:libraries:read+")
370         m_supports_qXfer_libraries_read = eLazyBoolYes;
371       else if (x == "qXfer:features:read+")
372         m_supports_qXfer_features_read = eLazyBoolYes;
373       else if (x == "qXfer:memory-map:read+")
374         m_supports_qXfer_memory_map_read = eLazyBoolYes;
375       else if (x == "qXfer:siginfo:read+")
376         m_supports_qXfer_siginfo_read = eLazyBoolYes;
377       else if (x == "qEcho")
378         m_supports_qEcho = eLazyBoolYes;
379       else if (x == "QPassSignals+")
380         m_supports_QPassSignals = eLazyBoolYes;
381       else if (x == "multiprocess+")
382         m_supports_multiprocess = eLazyBoolYes;
383       else if (x == "memory-tagging+")
384         m_supports_memory_tagging = eLazyBoolYes;
385       else if (x == "qSaveCore+")
386         m_supports_qSaveCore = eLazyBoolYes;
387       else if (x == "native-signals+")
388         m_uses_native_signals = eLazyBoolYes;
389       // Look for a list of compressions in the features list e.g.
390       // qXfer:features:read+;PacketSize=20000;qEcho+;SupportedCompressions=zlib-
391       // deflate,lzma
392       else if (x.consume_front("SupportedCompressions=")) {
393         llvm::SmallVector<llvm::StringRef, 4> compressions;
394         x.split(compressions, ',');
395         if (!compressions.empty())
396           MaybeEnableCompression(compressions);
397       } else if (x.consume_front("PacketSize=")) {
398         StringExtractorGDBRemote packet_response(x);
399         m_max_packet_size =
400             packet_response.GetHexMaxU64(/*little_endian=*/false, UINT64_MAX);
401         if (m_max_packet_size == 0) {
402           m_max_packet_size = UINT64_MAX; // Must have been a garbled response
403           Log *log(GetLog(GDBRLog::Process));
404           LLDB_LOGF(log, "Garbled PacketSize spec in qSupported response");
405         }
406       }
407     }
408   }
409 }
410 
411 bool GDBRemoteCommunicationClient::GetThreadSuffixSupported() {
412   if (m_supports_thread_suffix == eLazyBoolCalculate) {
413     StringExtractorGDBRemote response;
414     m_supports_thread_suffix = eLazyBoolNo;
415     if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response) ==
416         PacketResult::Success) {
417       if (response.IsOKResponse())
418         m_supports_thread_suffix = eLazyBoolYes;
419     }
420   }
421   return m_supports_thread_suffix;
422 }
423 bool GDBRemoteCommunicationClient::GetVContSupported(char flavor) {
424   if (m_supports_vCont_c == eLazyBoolCalculate) {
425     StringExtractorGDBRemote response;
426     m_supports_vCont_any = eLazyBoolNo;
427     m_supports_vCont_all = eLazyBoolNo;
428     m_supports_vCont_c = eLazyBoolNo;
429     m_supports_vCont_C = eLazyBoolNo;
430     m_supports_vCont_s = eLazyBoolNo;
431     m_supports_vCont_S = eLazyBoolNo;
432     if (SendPacketAndWaitForResponse("vCont?", response) ==
433         PacketResult::Success) {
434       const char *response_cstr = response.GetStringRef().data();
435       if (::strstr(response_cstr, ";c"))
436         m_supports_vCont_c = eLazyBoolYes;
437 
438       if (::strstr(response_cstr, ";C"))
439         m_supports_vCont_C = eLazyBoolYes;
440 
441       if (::strstr(response_cstr, ";s"))
442         m_supports_vCont_s = eLazyBoolYes;
443 
444       if (::strstr(response_cstr, ";S"))
445         m_supports_vCont_S = eLazyBoolYes;
446 
447       if (m_supports_vCont_c == eLazyBoolYes &&
448           m_supports_vCont_C == eLazyBoolYes &&
449           m_supports_vCont_s == eLazyBoolYes &&
450           m_supports_vCont_S == eLazyBoolYes) {
451         m_supports_vCont_all = eLazyBoolYes;
452       }
453 
454       if (m_supports_vCont_c == eLazyBoolYes ||
455           m_supports_vCont_C == eLazyBoolYes ||
456           m_supports_vCont_s == eLazyBoolYes ||
457           m_supports_vCont_S == eLazyBoolYes) {
458         m_supports_vCont_any = eLazyBoolYes;
459       }
460     }
461   }
462 
463   switch (flavor) {
464   case 'a':
465     return m_supports_vCont_any;
466   case 'A':
467     return m_supports_vCont_all;
468   case 'c':
469     return m_supports_vCont_c;
470   case 'C':
471     return m_supports_vCont_C;
472   case 's':
473     return m_supports_vCont_s;
474   case 'S':
475     return m_supports_vCont_S;
476   default:
477     break;
478   }
479   return false;
480 }
481 
482 GDBRemoteCommunication::PacketResult
483 GDBRemoteCommunicationClient::SendThreadSpecificPacketAndWaitForResponse(
484     lldb::tid_t tid, StreamString &&payload,
485     StringExtractorGDBRemote &response) {
486   Lock lock(*this);
487   if (!lock) {
488     if (Log *log = GetLog(GDBRLog::Process | GDBRLog::Packets))
489       LLDB_LOGF(log,
490                 "GDBRemoteCommunicationClient::%s: Didn't get sequence mutex "
491                 "for %s packet.",
492                 __FUNCTION__, payload.GetData());
493     return PacketResult::ErrorNoSequenceLock;
494   }
495 
496   if (GetThreadSuffixSupported())
497     payload.Printf(";thread:%4.4" PRIx64 ";", tid);
498   else {
499     if (!SetCurrentThread(tid))
500       return PacketResult::ErrorSendFailed;
501   }
502 
503   return SendPacketAndWaitForResponseNoLock(payload.GetString(), response);
504 }
505 
506 // Check if the target supports 'p' packet. It sends out a 'p' packet and
507 // checks the response. A normal packet will tell us that support is available.
508 //
509 // Takes a valid thread ID because p needs to apply to a thread.
510 bool GDBRemoteCommunicationClient::GetpPacketSupported(lldb::tid_t tid) {
511   if (m_supports_p == eLazyBoolCalculate)
512     m_supports_p = GetThreadPacketSupported(tid, "p0");
513   return m_supports_p;
514 }
515 
516 LazyBool GDBRemoteCommunicationClient::GetThreadPacketSupported(
517     lldb::tid_t tid, llvm::StringRef packetStr) {
518   StreamString payload;
519   payload.PutCString(packetStr);
520   StringExtractorGDBRemote response;
521   if (SendThreadSpecificPacketAndWaitForResponse(
522           tid, std::move(payload), response) == PacketResult::Success &&
523       response.IsNormalResponse()) {
524     return eLazyBoolYes;
525   }
526   return eLazyBoolNo;
527 }
528 
529 bool GDBRemoteCommunicationClient::GetSaveCoreSupported() const {
530   return m_supports_qSaveCore == eLazyBoolYes;
531 }
532 
533 StructuredData::ObjectSP GDBRemoteCommunicationClient::GetThreadsInfo() {
534   // Get information on all threads at one using the "jThreadsInfo" packet
535   StructuredData::ObjectSP object_sp;
536 
537   if (m_supports_jThreadsInfo) {
538     StringExtractorGDBRemote response;
539     response.SetResponseValidatorToJSON();
540     if (SendPacketAndWaitForResponse("jThreadsInfo", response) ==
541         PacketResult::Success) {
542       if (response.IsUnsupportedResponse()) {
543         m_supports_jThreadsInfo = false;
544       } else if (!response.Empty()) {
545         object_sp =
546             StructuredData::ParseJSON(std::string(response.GetStringRef()));
547       }
548     }
549   }
550   return object_sp;
551 }
552 
553 bool GDBRemoteCommunicationClient::GetThreadExtendedInfoSupported() {
554   if (m_supports_jThreadExtendedInfo == eLazyBoolCalculate) {
555     StringExtractorGDBRemote response;
556     m_supports_jThreadExtendedInfo = eLazyBoolNo;
557     if (SendPacketAndWaitForResponse("jThreadExtendedInfo:", response) ==
558         PacketResult::Success) {
559       if (response.IsOKResponse()) {
560         m_supports_jThreadExtendedInfo = eLazyBoolYes;
561       }
562     }
563   }
564   return m_supports_jThreadExtendedInfo;
565 }
566 
567 void GDBRemoteCommunicationClient::EnableErrorStringInPacket() {
568   if (m_supports_error_string_reply == eLazyBoolCalculate) {
569     StringExtractorGDBRemote response;
570     // We try to enable error strings in remote packets but if we fail, we just
571     // work in the older way.
572     m_supports_error_string_reply = eLazyBoolNo;
573     if (SendPacketAndWaitForResponse("QEnableErrorStrings", response) ==
574         PacketResult::Success) {
575       if (response.IsOKResponse()) {
576         m_supports_error_string_reply = eLazyBoolYes;
577       }
578     }
579   }
580 }
581 
582 bool GDBRemoteCommunicationClient::GetLoadedDynamicLibrariesInfosSupported() {
583   if (m_supports_jLoadedDynamicLibrariesInfos == eLazyBoolCalculate) {
584     StringExtractorGDBRemote response;
585     m_supports_jLoadedDynamicLibrariesInfos = eLazyBoolNo;
586     if (SendPacketAndWaitForResponse("jGetLoadedDynamicLibrariesInfos:",
587                                      response) == PacketResult::Success) {
588       if (response.IsOKResponse()) {
589         m_supports_jLoadedDynamicLibrariesInfos = eLazyBoolYes;
590       }
591     }
592   }
593   return m_supports_jLoadedDynamicLibrariesInfos;
594 }
595 
596 bool GDBRemoteCommunicationClient::GetSharedCacheInfoSupported() {
597   if (m_supports_jGetSharedCacheInfo == eLazyBoolCalculate) {
598     StringExtractorGDBRemote response;
599     m_supports_jGetSharedCacheInfo = eLazyBoolNo;
600     if (SendPacketAndWaitForResponse("jGetSharedCacheInfo:", response) ==
601         PacketResult::Success) {
602       if (response.IsOKResponse()) {
603         m_supports_jGetSharedCacheInfo = eLazyBoolYes;
604       }
605     }
606   }
607   return m_supports_jGetSharedCacheInfo;
608 }
609 
610 bool GDBRemoteCommunicationClient::GetMemoryTaggingSupported() {
611   if (m_supports_memory_tagging == eLazyBoolCalculate) {
612     GetRemoteQSupported();
613   }
614   return m_supports_memory_tagging == eLazyBoolYes;
615 }
616 
617 DataBufferSP GDBRemoteCommunicationClient::ReadMemoryTags(lldb::addr_t addr,
618                                                           size_t len,
619                                                           int32_t type) {
620   StreamString packet;
621   packet.Printf("qMemTags:%" PRIx64 ",%zx:%" PRIx32, addr, len, type);
622   StringExtractorGDBRemote response;
623 
624   Log *log = GetLog(GDBRLog::Memory);
625 
626   if (SendPacketAndWaitForResponse(packet.GetString(), response) !=
627           PacketResult::Success ||
628       !response.IsNormalResponse()) {
629     LLDB_LOGF(log, "GDBRemoteCommunicationClient::%s: qMemTags packet failed",
630               __FUNCTION__);
631     return nullptr;
632   }
633 
634   // We are expecting
635   // m<hex encoded bytes>
636 
637   if (response.GetChar() != 'm') {
638     LLDB_LOGF(log,
639               "GDBRemoteCommunicationClient::%s: qMemTags response did not "
640               "begin with \"m\"",
641               __FUNCTION__);
642     return nullptr;
643   }
644 
645   size_t expected_bytes = response.GetBytesLeft() / 2;
646   DataBufferSP buffer_sp(new DataBufferHeap(expected_bytes, 0));
647   size_t got_bytes = response.GetHexBytesAvail(buffer_sp->GetData());
648   // Check both because in some situations chars are consumed even
649   // if the decoding fails.
650   if (response.GetBytesLeft() || (expected_bytes != got_bytes)) {
651     LLDB_LOGF(
652         log,
653         "GDBRemoteCommunicationClient::%s: Invalid data in qMemTags response",
654         __FUNCTION__);
655     return nullptr;
656   }
657 
658   return buffer_sp;
659 }
660 
661 Status GDBRemoteCommunicationClient::WriteMemoryTags(
662     lldb::addr_t addr, size_t len, int32_t type,
663     const std::vector<uint8_t> &tags) {
664   // Format QMemTags:address,length:type:tags
665   StreamString packet;
666   packet.Printf("QMemTags:%" PRIx64 ",%zx:%" PRIx32 ":", addr, len, type);
667   packet.PutBytesAsRawHex8(tags.data(), tags.size());
668 
669   Status status;
670   StringExtractorGDBRemote response;
671   if (SendPacketAndWaitForResponse(packet.GetString(), response) !=
672           PacketResult::Success ||
673       !response.IsOKResponse()) {
674     status.SetErrorString("QMemTags packet failed");
675   }
676   return status;
677 }
678 
679 bool GDBRemoteCommunicationClient::GetxPacketSupported() {
680   if (m_supports_x == eLazyBoolCalculate) {
681     StringExtractorGDBRemote response;
682     m_supports_x = eLazyBoolNo;
683     char packet[256];
684     snprintf(packet, sizeof(packet), "x0,0");
685     if (SendPacketAndWaitForResponse(packet, response) ==
686         PacketResult::Success) {
687       if (response.IsOKResponse())
688         m_supports_x = eLazyBoolYes;
689     }
690   }
691   return m_supports_x;
692 }
693 
694 lldb::pid_t GDBRemoteCommunicationClient::GetCurrentProcessID(bool allow_lazy) {
695   if (allow_lazy && m_curr_pid_is_valid == eLazyBoolYes)
696     return m_curr_pid;
697 
698   // First try to retrieve the pid via the qProcessInfo request.
699   GetCurrentProcessInfo(allow_lazy);
700   if (m_curr_pid_is_valid == eLazyBoolYes) {
701     // We really got it.
702     return m_curr_pid;
703   } else {
704     // If we don't get a response for qProcessInfo, check if $qC gives us a
705     // result. $qC only returns a real process id on older debugserver and
706     // lldb-platform stubs. The gdb remote protocol documents $qC as returning
707     // the thread id, which newer debugserver and lldb-gdbserver stubs return
708     // correctly.
709     StringExtractorGDBRemote response;
710     if (SendPacketAndWaitForResponse("qC", response) == PacketResult::Success) {
711       if (response.GetChar() == 'Q') {
712         if (response.GetChar() == 'C') {
713           m_curr_pid_run = m_curr_pid =
714               response.GetHexMaxU64(false, LLDB_INVALID_PROCESS_ID);
715           if (m_curr_pid != LLDB_INVALID_PROCESS_ID) {
716             m_curr_pid_is_valid = eLazyBoolYes;
717             return m_curr_pid;
718           }
719         }
720       }
721     }
722 
723     // If we don't get a response for $qC, check if $qfThreadID gives us a
724     // result.
725     if (m_curr_pid == LLDB_INVALID_PROCESS_ID) {
726       bool sequence_mutex_unavailable;
727       auto ids = GetCurrentProcessAndThreadIDs(sequence_mutex_unavailable);
728       if (!ids.empty() && !sequence_mutex_unavailable) {
729         // If server returned an explicit PID, use that.
730         m_curr_pid_run = m_curr_pid = ids.front().first;
731         // Otherwise, use the TID of the first thread (Linux hack).
732         if (m_curr_pid == LLDB_INVALID_PROCESS_ID)
733           m_curr_pid_run = m_curr_pid = ids.front().second;
734         m_curr_pid_is_valid = eLazyBoolYes;
735         return m_curr_pid;
736       }
737     }
738   }
739 
740   return LLDB_INVALID_PROCESS_ID;
741 }
742 
743 bool GDBRemoteCommunicationClient::GetLaunchSuccess(std::string &error_str) {
744   error_str.clear();
745   StringExtractorGDBRemote response;
746   if (SendPacketAndWaitForResponse("qLaunchSuccess", response) ==
747       PacketResult::Success) {
748     if (response.IsOKResponse())
749       return true;
750     // GDB does not implement qLaunchSuccess -- but if we used vRun,
751     // then we already received a successful launch indication via stop
752     // reason.
753     if (response.IsUnsupportedResponse() && m_supports_vRun)
754       return true;
755     if (response.GetChar() == 'E') {
756       // A string the describes what failed when launching...
757       error_str = std::string(response.GetStringRef().substr(1));
758     } else {
759       error_str.assign("unknown error occurred launching process");
760     }
761   } else {
762     error_str.assign("timed out waiting for app to launch");
763   }
764   return false;
765 }
766 
767 int GDBRemoteCommunicationClient::SendArgumentsPacket(
768     const ProcessLaunchInfo &launch_info) {
769   // Since we don't get the send argv0 separate from the executable path, we
770   // need to make sure to use the actual executable path found in the
771   // launch_info...
772   std::vector<const char *> argv;
773   FileSpec exe_file = launch_info.GetExecutableFile();
774   std::string exe_path;
775   const char *arg = nullptr;
776   const Args &launch_args = launch_info.GetArguments();
777   if (exe_file)
778     exe_path = exe_file.GetPath(false);
779   else {
780     arg = launch_args.GetArgumentAtIndex(0);
781     if (arg)
782       exe_path = arg;
783   }
784   if (!exe_path.empty()) {
785     argv.push_back(exe_path.c_str());
786     for (uint32_t i = 1; (arg = launch_args.GetArgumentAtIndex(i)) != nullptr;
787          ++i) {
788       if (arg)
789         argv.push_back(arg);
790     }
791   }
792   if (!argv.empty()) {
793     // try vRun first
794     if (m_supports_vRun) {
795       StreamString packet;
796       packet.PutCString("vRun");
797       for (const char *arg : argv) {
798         packet.PutChar(';');
799         packet.PutBytesAsRawHex8(arg, strlen(arg));
800       }
801 
802       StringExtractorGDBRemote response;
803       if (SendPacketAndWaitForResponse(packet.GetString(), response) !=
804           PacketResult::Success)
805         return -1;
806 
807       if (response.IsErrorResponse()) {
808         uint8_t error = response.GetError();
809         if (error)
810           return error;
811         return -1;
812       }
813       // vRun replies with a stop reason packet
814       // FIXME: right now we just discard the packet and LLDB queries
815       // for stop reason again
816       if (!response.IsUnsupportedResponse())
817         return 0;
818 
819       m_supports_vRun = false;
820     }
821 
822     // fallback to A
823     StreamString packet;
824     packet.PutChar('A');
825     for (size_t i = 0, n = argv.size(); i < n; ++i) {
826       arg = argv[i];
827       const int arg_len = strlen(arg);
828       if (i > 0)
829         packet.PutChar(',');
830       packet.Printf("%i,%i,", arg_len * 2, (int)i);
831       packet.PutBytesAsRawHex8(arg, arg_len);
832     }
833 
834     StringExtractorGDBRemote response;
835     if (SendPacketAndWaitForResponse(packet.GetString(), response) ==
836         PacketResult::Success) {
837       if (response.IsOKResponse())
838         return 0;
839       uint8_t error = response.GetError();
840       if (error)
841         return error;
842     }
843   }
844   return -1;
845 }
846 
847 int GDBRemoteCommunicationClient::SendEnvironment(const Environment &env) {
848   for (const auto &KV : env) {
849     int r = SendEnvironmentPacket(Environment::compose(KV).c_str());
850     if (r != 0)
851       return r;
852   }
853   return 0;
854 }
855 
856 int GDBRemoteCommunicationClient::SendEnvironmentPacket(
857     char const *name_equal_value) {
858   if (name_equal_value && name_equal_value[0]) {
859     bool send_hex_encoding = false;
860     for (const char *p = name_equal_value; *p != '\0' && !send_hex_encoding;
861          ++p) {
862       if (llvm::isPrint(*p)) {
863         switch (*p) {
864         case '$':
865         case '#':
866         case '*':
867         case '}':
868           send_hex_encoding = true;
869           break;
870         default:
871           break;
872         }
873       } else {
874         // We have non printable characters, lets hex encode this...
875         send_hex_encoding = true;
876       }
877     }
878 
879     StringExtractorGDBRemote response;
880     // Prefer sending unencoded, if possible and the server supports it.
881     if (!send_hex_encoding && m_supports_QEnvironment) {
882       StreamString packet;
883       packet.Printf("QEnvironment:%s", name_equal_value);
884       if (SendPacketAndWaitForResponse(packet.GetString(), response) !=
885           PacketResult::Success)
886         return -1;
887 
888       if (response.IsOKResponse())
889         return 0;
890       if (response.IsUnsupportedResponse())
891         m_supports_QEnvironment = false;
892       else {
893         uint8_t error = response.GetError();
894         if (error)
895           return error;
896         return -1;
897       }
898     }
899 
900     if (m_supports_QEnvironmentHexEncoded) {
901       StreamString packet;
902       packet.PutCString("QEnvironmentHexEncoded:");
903       packet.PutBytesAsRawHex8(name_equal_value, strlen(name_equal_value));
904       if (SendPacketAndWaitForResponse(packet.GetString(), response) !=
905           PacketResult::Success)
906         return -1;
907 
908       if (response.IsOKResponse())
909         return 0;
910       if (response.IsUnsupportedResponse())
911         m_supports_QEnvironmentHexEncoded = false;
912       else {
913         uint8_t error = response.GetError();
914         if (error)
915           return error;
916         return -1;
917       }
918     }
919   }
920   return -1;
921 }
922 
923 int GDBRemoteCommunicationClient::SendLaunchArchPacket(char const *arch) {
924   if (arch && arch[0]) {
925     StreamString packet;
926     packet.Printf("QLaunchArch:%s", arch);
927     StringExtractorGDBRemote response;
928     if (SendPacketAndWaitForResponse(packet.GetString(), response) ==
929         PacketResult::Success) {
930       if (response.IsOKResponse())
931         return 0;
932       uint8_t error = response.GetError();
933       if (error)
934         return error;
935     }
936   }
937   return -1;
938 }
939 
940 int GDBRemoteCommunicationClient::SendLaunchEventDataPacket(
941     char const *data, bool *was_supported) {
942   if (data && *data != '\0') {
943     StreamString packet;
944     packet.Printf("QSetProcessEvent:%s", data);
945     StringExtractorGDBRemote response;
946     if (SendPacketAndWaitForResponse(packet.GetString(), response) ==
947         PacketResult::Success) {
948       if (response.IsOKResponse()) {
949         if (was_supported)
950           *was_supported = true;
951         return 0;
952       } else if (response.IsUnsupportedResponse()) {
953         if (was_supported)
954           *was_supported = false;
955         return -1;
956       } else {
957         uint8_t error = response.GetError();
958         if (was_supported)
959           *was_supported = true;
960         if (error)
961           return error;
962       }
963     }
964   }
965   return -1;
966 }
967 
968 llvm::VersionTuple GDBRemoteCommunicationClient::GetOSVersion() {
969   GetHostInfo();
970   return m_os_version;
971 }
972 
973 llvm::VersionTuple GDBRemoteCommunicationClient::GetMacCatalystVersion() {
974   GetHostInfo();
975   return m_maccatalyst_version;
976 }
977 
978 llvm::Optional<std::string> GDBRemoteCommunicationClient::GetOSBuildString() {
979   if (GetHostInfo()) {
980     if (!m_os_build.empty())
981       return m_os_build;
982   }
983   return llvm::None;
984 }
985 
986 llvm::Optional<std::string>
987 GDBRemoteCommunicationClient::GetOSKernelDescription() {
988   if (GetHostInfo()) {
989     if (!m_os_kernel.empty())
990       return m_os_kernel;
991   }
992   return llvm::None;
993 }
994 
995 bool GDBRemoteCommunicationClient::GetHostname(std::string &s) {
996   if (GetHostInfo()) {
997     if (!m_hostname.empty()) {
998       s = m_hostname;
999       return true;
1000     }
1001   }
1002   s.clear();
1003   return false;
1004 }
1005 
1006 ArchSpec GDBRemoteCommunicationClient::GetSystemArchitecture() {
1007   if (GetHostInfo())
1008     return m_host_arch;
1009   return ArchSpec();
1010 }
1011 
1012 const lldb_private::ArchSpec &
1013 GDBRemoteCommunicationClient::GetProcessArchitecture() {
1014   if (m_qProcessInfo_is_valid == eLazyBoolCalculate)
1015     GetCurrentProcessInfo();
1016   return m_process_arch;
1017 }
1018 
1019 bool GDBRemoteCommunicationClient::GetProcessStandaloneBinary(
1020     UUID &uuid, addr_t &value, bool &value_is_offset) {
1021   if (m_qProcessInfo_is_valid == eLazyBoolCalculate)
1022     GetCurrentProcessInfo();
1023 
1024   // Return true if we have a UUID or an address/offset of the
1025   // main standalone / firmware binary being used.
1026   if (!m_process_standalone_uuid.IsValid() &&
1027       m_process_standalone_value == LLDB_INVALID_ADDRESS)
1028     return false;
1029 
1030   uuid = m_process_standalone_uuid;
1031   value = m_process_standalone_value;
1032   value_is_offset = m_process_standalone_value_is_offset;
1033   return true;
1034 }
1035 
1036 bool GDBRemoteCommunicationClient::GetGDBServerVersion() {
1037   if (m_qGDBServerVersion_is_valid == eLazyBoolCalculate) {
1038     m_gdb_server_name.clear();
1039     m_gdb_server_version = 0;
1040     m_qGDBServerVersion_is_valid = eLazyBoolNo;
1041 
1042     StringExtractorGDBRemote response;
1043     if (SendPacketAndWaitForResponse("qGDBServerVersion", response) ==
1044         PacketResult::Success) {
1045       if (response.IsNormalResponse()) {
1046         llvm::StringRef name, value;
1047         bool success = false;
1048         while (response.GetNameColonValue(name, value)) {
1049           if (name.equals("name")) {
1050             success = true;
1051             m_gdb_server_name = std::string(value);
1052           } else if (name.equals("version")) {
1053             llvm::StringRef major, minor;
1054             std::tie(major, minor) = value.split('.');
1055             if (!major.getAsInteger(0, m_gdb_server_version))
1056               success = true;
1057           }
1058         }
1059         if (success)
1060           m_qGDBServerVersion_is_valid = eLazyBoolYes;
1061       }
1062     }
1063   }
1064   return m_qGDBServerVersion_is_valid == eLazyBoolYes;
1065 }
1066 
1067 void GDBRemoteCommunicationClient::MaybeEnableCompression(
1068     llvm::ArrayRef<llvm::StringRef> supported_compressions) {
1069   CompressionType avail_type = CompressionType::None;
1070   llvm::StringRef avail_name;
1071 
1072 #if defined(HAVE_LIBCOMPRESSION)
1073   if (avail_type == CompressionType::None) {
1074     for (auto compression : supported_compressions) {
1075       if (compression == "lzfse") {
1076         avail_type = CompressionType::LZFSE;
1077         avail_name = compression;
1078         break;
1079       }
1080     }
1081   }
1082 #endif
1083 
1084 #if defined(HAVE_LIBCOMPRESSION)
1085   if (avail_type == CompressionType::None) {
1086     for (auto compression : supported_compressions) {
1087       if (compression == "zlib-deflate") {
1088         avail_type = CompressionType::ZlibDeflate;
1089         avail_name = compression;
1090         break;
1091       }
1092     }
1093   }
1094 #endif
1095 
1096 #if LLVM_ENABLE_ZLIB
1097   if (avail_type == CompressionType::None) {
1098     for (auto compression : supported_compressions) {
1099       if (compression == "zlib-deflate") {
1100         avail_type = CompressionType::ZlibDeflate;
1101         avail_name = compression;
1102         break;
1103       }
1104     }
1105   }
1106 #endif
1107 
1108 #if defined(HAVE_LIBCOMPRESSION)
1109   if (avail_type == CompressionType::None) {
1110     for (auto compression : supported_compressions) {
1111       if (compression == "lz4") {
1112         avail_type = CompressionType::LZ4;
1113         avail_name = compression;
1114         break;
1115       }
1116     }
1117   }
1118 #endif
1119 
1120 #if defined(HAVE_LIBCOMPRESSION)
1121   if (avail_type == CompressionType::None) {
1122     for (auto compression : supported_compressions) {
1123       if (compression == "lzma") {
1124         avail_type = CompressionType::LZMA;
1125         avail_name = compression;
1126         break;
1127       }
1128     }
1129   }
1130 #endif
1131 
1132   if (avail_type != CompressionType::None) {
1133     StringExtractorGDBRemote response;
1134     std::string packet = "QEnableCompression:type:" + avail_name.str() + ";";
1135     if (SendPacketAndWaitForResponse(packet, response) != PacketResult::Success)
1136       return;
1137 
1138     if (response.IsOKResponse()) {
1139       m_compression_type = avail_type;
1140     }
1141   }
1142 }
1143 
1144 const char *GDBRemoteCommunicationClient::GetGDBServerProgramName() {
1145   if (GetGDBServerVersion()) {
1146     if (!m_gdb_server_name.empty())
1147       return m_gdb_server_name.c_str();
1148   }
1149   return nullptr;
1150 }
1151 
1152 uint32_t GDBRemoteCommunicationClient::GetGDBServerProgramVersion() {
1153   if (GetGDBServerVersion())
1154     return m_gdb_server_version;
1155   return 0;
1156 }
1157 
1158 bool GDBRemoteCommunicationClient::GetDefaultThreadId(lldb::tid_t &tid) {
1159   StringExtractorGDBRemote response;
1160   if (SendPacketAndWaitForResponse("qC", response) != PacketResult::Success)
1161     return false;
1162 
1163   if (!response.IsNormalResponse())
1164     return false;
1165 
1166   if (response.GetChar() == 'Q' && response.GetChar() == 'C') {
1167     auto pid_tid = response.GetPidTid(0);
1168     if (!pid_tid)
1169       return false;
1170 
1171     lldb::pid_t pid = pid_tid->first;
1172     // invalid
1173     if (pid == StringExtractorGDBRemote::AllProcesses)
1174       return false;
1175 
1176     // if we get pid as well, update m_curr_pid
1177     if (pid != 0) {
1178       m_curr_pid_run = m_curr_pid = pid;
1179       m_curr_pid_is_valid = eLazyBoolYes;
1180     }
1181     tid = pid_tid->second;
1182   }
1183 
1184   return true;
1185 }
1186 
1187 static void ParseOSType(llvm::StringRef value, std::string &os_name,
1188                         std::string &environment) {
1189   if (value.equals("iossimulator") || value.equals("tvossimulator") ||
1190       value.equals("watchossimulator")) {
1191     environment = "simulator";
1192     os_name = value.drop_back(environment.size()).str();
1193   } else if (value.equals("maccatalyst")) {
1194     os_name = "ios";
1195     environment = "macabi";
1196   } else {
1197     os_name = value.str();
1198   }
1199 }
1200 
1201 bool GDBRemoteCommunicationClient::GetHostInfo(bool force) {
1202   Log *log = GetLog(GDBRLog::Process);
1203 
1204   if (force || m_qHostInfo_is_valid == eLazyBoolCalculate) {
1205     // host info computation can require DNS traffic and shelling out to external processes.
1206     // Increase the timeout to account for that.
1207     ScopedTimeout timeout(*this, seconds(10));
1208     m_qHostInfo_is_valid = eLazyBoolNo;
1209     StringExtractorGDBRemote response;
1210     if (SendPacketAndWaitForResponse("qHostInfo", response) ==
1211         PacketResult::Success) {
1212       if (response.IsNormalResponse()) {
1213         llvm::StringRef name;
1214         llvm::StringRef value;
1215         uint32_t cpu = LLDB_INVALID_CPUTYPE;
1216         uint32_t sub = 0;
1217         std::string arch_name;
1218         std::string os_name;
1219         std::string environment;
1220         std::string vendor_name;
1221         std::string triple;
1222         std::string distribution_id;
1223         uint32_t pointer_byte_size = 0;
1224         ByteOrder byte_order = eByteOrderInvalid;
1225         uint32_t num_keys_decoded = 0;
1226         while (response.GetNameColonValue(name, value)) {
1227           if (name.equals("cputype")) {
1228             // exception type in big endian hex
1229             if (!value.getAsInteger(0, cpu))
1230               ++num_keys_decoded;
1231           } else if (name.equals("cpusubtype")) {
1232             // exception count in big endian hex
1233             if (!value.getAsInteger(0, sub))
1234               ++num_keys_decoded;
1235           } else if (name.equals("arch")) {
1236             arch_name = std::string(value);
1237             ++num_keys_decoded;
1238           } else if (name.equals("triple")) {
1239             StringExtractor extractor(value);
1240             extractor.GetHexByteString(triple);
1241             ++num_keys_decoded;
1242           } else if (name.equals("distribution_id")) {
1243             StringExtractor extractor(value);
1244             extractor.GetHexByteString(distribution_id);
1245             ++num_keys_decoded;
1246           } else if (name.equals("os_build")) {
1247             StringExtractor extractor(value);
1248             extractor.GetHexByteString(m_os_build);
1249             ++num_keys_decoded;
1250           } else if (name.equals("hostname")) {
1251             StringExtractor extractor(value);
1252             extractor.GetHexByteString(m_hostname);
1253             ++num_keys_decoded;
1254           } else if (name.equals("os_kernel")) {
1255             StringExtractor extractor(value);
1256             extractor.GetHexByteString(m_os_kernel);
1257             ++num_keys_decoded;
1258           } else if (name.equals("ostype")) {
1259             ParseOSType(value, os_name, environment);
1260             ++num_keys_decoded;
1261           } else if (name.equals("vendor")) {
1262             vendor_name = std::string(value);
1263             ++num_keys_decoded;
1264           } else if (name.equals("endian")) {
1265             byte_order = llvm::StringSwitch<lldb::ByteOrder>(value)
1266                              .Case("little", eByteOrderLittle)
1267                              .Case("big", eByteOrderBig)
1268                              .Case("pdp", eByteOrderPDP)
1269                              .Default(eByteOrderInvalid);
1270             if (byte_order != eByteOrderInvalid)
1271               ++num_keys_decoded;
1272           } else if (name.equals("ptrsize")) {
1273             if (!value.getAsInteger(0, pointer_byte_size))
1274               ++num_keys_decoded;
1275           } else if (name.equals("addressing_bits")) {
1276             if (!value.getAsInteger(0, m_addressing_bits))
1277               ++num_keys_decoded;
1278           } else if (name.equals("os_version") ||
1279                      name.equals("version")) // Older debugserver binaries used
1280                                              // the "version" key instead of
1281                                              // "os_version"...
1282           {
1283             if (!m_os_version.tryParse(value))
1284               ++num_keys_decoded;
1285           } else if (name.equals("maccatalyst_version")) {
1286             if (!m_maccatalyst_version.tryParse(value))
1287               ++num_keys_decoded;
1288           } else if (name.equals("watchpoint_exceptions_received")) {
1289             m_watchpoints_trigger_after_instruction =
1290                 llvm::StringSwitch<LazyBool>(value)
1291                     .Case("before", eLazyBoolNo)
1292                     .Case("after", eLazyBoolYes)
1293                     .Default(eLazyBoolCalculate);
1294             if (m_watchpoints_trigger_after_instruction != eLazyBoolCalculate)
1295               ++num_keys_decoded;
1296           } else if (name.equals("default_packet_timeout")) {
1297             uint32_t timeout_seconds;
1298             if (!value.getAsInteger(0, timeout_seconds)) {
1299               m_default_packet_timeout = seconds(timeout_seconds);
1300               SetPacketTimeout(m_default_packet_timeout);
1301               ++num_keys_decoded;
1302             }
1303           } else if (name.equals("vm-page-size")) {
1304             int page_size;
1305             if (!value.getAsInteger(0, page_size)) {
1306               m_target_vm_page_size = page_size;
1307               ++num_keys_decoded;
1308             }
1309           }
1310         }
1311 
1312         if (num_keys_decoded > 0)
1313           m_qHostInfo_is_valid = eLazyBoolYes;
1314 
1315         if (triple.empty()) {
1316           if (arch_name.empty()) {
1317             if (cpu != LLDB_INVALID_CPUTYPE) {
1318               m_host_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
1319               if (pointer_byte_size) {
1320                 assert(pointer_byte_size == m_host_arch.GetAddressByteSize());
1321               }
1322               if (byte_order != eByteOrderInvalid) {
1323                 assert(byte_order == m_host_arch.GetByteOrder());
1324               }
1325 
1326               if (!vendor_name.empty())
1327                 m_host_arch.GetTriple().setVendorName(
1328                     llvm::StringRef(vendor_name));
1329               if (!os_name.empty())
1330                 m_host_arch.GetTriple().setOSName(llvm::StringRef(os_name));
1331               if (!environment.empty())
1332                 m_host_arch.GetTriple().setEnvironmentName(environment);
1333             }
1334           } else {
1335             std::string triple;
1336             triple += arch_name;
1337             if (!vendor_name.empty() || !os_name.empty()) {
1338               triple += '-';
1339               if (vendor_name.empty())
1340                 triple += "unknown";
1341               else
1342                 triple += vendor_name;
1343               triple += '-';
1344               if (os_name.empty())
1345                 triple += "unknown";
1346               else
1347                 triple += os_name;
1348             }
1349             m_host_arch.SetTriple(triple.c_str());
1350 
1351             llvm::Triple &host_triple = m_host_arch.GetTriple();
1352             if (host_triple.getVendor() == llvm::Triple::Apple &&
1353                 host_triple.getOS() == llvm::Triple::Darwin) {
1354               switch (m_host_arch.GetMachine()) {
1355               case llvm::Triple::aarch64:
1356               case llvm::Triple::aarch64_32:
1357               case llvm::Triple::arm:
1358               case llvm::Triple::thumb:
1359                 host_triple.setOS(llvm::Triple::IOS);
1360                 break;
1361               default:
1362                 host_triple.setOS(llvm::Triple::MacOSX);
1363                 break;
1364               }
1365             }
1366             if (pointer_byte_size) {
1367               assert(pointer_byte_size == m_host_arch.GetAddressByteSize());
1368             }
1369             if (byte_order != eByteOrderInvalid) {
1370               assert(byte_order == m_host_arch.GetByteOrder());
1371             }
1372           }
1373         } else {
1374           m_host_arch.SetTriple(triple.c_str());
1375           if (pointer_byte_size) {
1376             assert(pointer_byte_size == m_host_arch.GetAddressByteSize());
1377           }
1378           if (byte_order != eByteOrderInvalid) {
1379             assert(byte_order == m_host_arch.GetByteOrder());
1380           }
1381 
1382           LLDB_LOGF(log,
1383                     "GDBRemoteCommunicationClient::%s parsed host "
1384                     "architecture as %s, triple as %s from triple text %s",
1385                     __FUNCTION__,
1386                     m_host_arch.GetArchitectureName()
1387                         ? m_host_arch.GetArchitectureName()
1388                         : "<null-arch-name>",
1389                     m_host_arch.GetTriple().getTriple().c_str(),
1390                     triple.c_str());
1391         }
1392         if (!distribution_id.empty())
1393           m_host_arch.SetDistributionId(distribution_id.c_str());
1394       }
1395     }
1396   }
1397   return m_qHostInfo_is_valid == eLazyBoolYes;
1398 }
1399 
1400 int GDBRemoteCommunicationClient::SendStdinNotification(const char *data,
1401                                                         size_t data_len) {
1402   StreamString packet;
1403   packet.PutCString("I");
1404   packet.PutBytesAsRawHex8(data, data_len);
1405   StringExtractorGDBRemote response;
1406   if (SendPacketAndWaitForResponse(packet.GetString(), response) ==
1407       PacketResult::Success) {
1408     return 0;
1409   }
1410   return response.GetError();
1411 }
1412 
1413 const lldb_private::ArchSpec &
1414 GDBRemoteCommunicationClient::GetHostArchitecture() {
1415   if (m_qHostInfo_is_valid == eLazyBoolCalculate)
1416     GetHostInfo();
1417   return m_host_arch;
1418 }
1419 
1420 uint32_t GDBRemoteCommunicationClient::GetAddressingBits() {
1421   if (m_qHostInfo_is_valid == eLazyBoolCalculate)
1422     GetHostInfo();
1423   return m_addressing_bits;
1424 }
1425 seconds GDBRemoteCommunicationClient::GetHostDefaultPacketTimeout() {
1426   if (m_qHostInfo_is_valid == eLazyBoolCalculate)
1427     GetHostInfo();
1428   return m_default_packet_timeout;
1429 }
1430 
1431 addr_t GDBRemoteCommunicationClient::AllocateMemory(size_t size,
1432                                                     uint32_t permissions) {
1433   if (m_supports_alloc_dealloc_memory != eLazyBoolNo) {
1434     m_supports_alloc_dealloc_memory = eLazyBoolYes;
1435     char packet[64];
1436     const int packet_len = ::snprintf(
1437         packet, sizeof(packet), "_M%" PRIx64 ",%s%s%s", (uint64_t)size,
1438         permissions & lldb::ePermissionsReadable ? "r" : "",
1439         permissions & lldb::ePermissionsWritable ? "w" : "",
1440         permissions & lldb::ePermissionsExecutable ? "x" : "");
1441     assert(packet_len < (int)sizeof(packet));
1442     UNUSED_IF_ASSERT_DISABLED(packet_len);
1443     StringExtractorGDBRemote response;
1444     if (SendPacketAndWaitForResponse(packet, response) ==
1445         PacketResult::Success) {
1446       if (response.IsUnsupportedResponse())
1447         m_supports_alloc_dealloc_memory = eLazyBoolNo;
1448       else if (!response.IsErrorResponse())
1449         return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1450     } else {
1451       m_supports_alloc_dealloc_memory = eLazyBoolNo;
1452     }
1453   }
1454   return LLDB_INVALID_ADDRESS;
1455 }
1456 
1457 bool GDBRemoteCommunicationClient::DeallocateMemory(addr_t addr) {
1458   if (m_supports_alloc_dealloc_memory != eLazyBoolNo) {
1459     m_supports_alloc_dealloc_memory = eLazyBoolYes;
1460     char packet[64];
1461     const int packet_len =
1462         ::snprintf(packet, sizeof(packet), "_m%" PRIx64, (uint64_t)addr);
1463     assert(packet_len < (int)sizeof(packet));
1464     UNUSED_IF_ASSERT_DISABLED(packet_len);
1465     StringExtractorGDBRemote response;
1466     if (SendPacketAndWaitForResponse(packet, response) ==
1467         PacketResult::Success) {
1468       if (response.IsUnsupportedResponse())
1469         m_supports_alloc_dealloc_memory = eLazyBoolNo;
1470       else if (response.IsOKResponse())
1471         return true;
1472     } else {
1473       m_supports_alloc_dealloc_memory = eLazyBoolNo;
1474     }
1475   }
1476   return false;
1477 }
1478 
1479 Status GDBRemoteCommunicationClient::Detach(bool keep_stopped,
1480                                             lldb::pid_t pid) {
1481   Status error;
1482   lldb_private::StreamString packet;
1483 
1484   packet.PutChar('D');
1485   if (keep_stopped) {
1486     if (m_supports_detach_stay_stopped == eLazyBoolCalculate) {
1487       char packet[64];
1488       const int packet_len =
1489           ::snprintf(packet, sizeof(packet), "qSupportsDetachAndStayStopped:");
1490       assert(packet_len < (int)sizeof(packet));
1491       UNUSED_IF_ASSERT_DISABLED(packet_len);
1492       StringExtractorGDBRemote response;
1493       if (SendPacketAndWaitForResponse(packet, response) ==
1494               PacketResult::Success &&
1495           response.IsOKResponse()) {
1496         m_supports_detach_stay_stopped = eLazyBoolYes;
1497       } else {
1498         m_supports_detach_stay_stopped = eLazyBoolNo;
1499       }
1500     }
1501 
1502     if (m_supports_detach_stay_stopped == eLazyBoolNo) {
1503       error.SetErrorString("Stays stopped not supported by this target.");
1504       return error;
1505     } else {
1506       packet.PutChar('1');
1507     }
1508   }
1509 
1510   if (m_supports_multiprocess) {
1511     // Some servers (e.g. qemu) require specifying the PID even if only a single
1512     // process is running.
1513     if (pid == LLDB_INVALID_PROCESS_ID)
1514       pid = GetCurrentProcessID();
1515     packet.PutChar(';');
1516     packet.PutHex64(pid);
1517   } else if (pid != LLDB_INVALID_PROCESS_ID) {
1518     error.SetErrorString("Multiprocess extension not supported by the server.");
1519     return error;
1520   }
1521 
1522   StringExtractorGDBRemote response;
1523   PacketResult packet_result =
1524       SendPacketAndWaitForResponse(packet.GetString(), response);
1525   if (packet_result != PacketResult::Success)
1526     error.SetErrorString("Sending isconnect packet failed.");
1527   return error;
1528 }
1529 
1530 Status GDBRemoteCommunicationClient::GetMemoryRegionInfo(
1531     lldb::addr_t addr, lldb_private::MemoryRegionInfo &region_info) {
1532   Status error;
1533   region_info.Clear();
1534 
1535   if (m_supports_memory_region_info != eLazyBoolNo) {
1536     m_supports_memory_region_info = eLazyBoolYes;
1537     char packet[64];
1538     const int packet_len = ::snprintf(
1539         packet, sizeof(packet), "qMemoryRegionInfo:%" PRIx64, (uint64_t)addr);
1540     assert(packet_len < (int)sizeof(packet));
1541     UNUSED_IF_ASSERT_DISABLED(packet_len);
1542     StringExtractorGDBRemote response;
1543     if (SendPacketAndWaitForResponse(packet, response) ==
1544             PacketResult::Success &&
1545         response.GetResponseType() == StringExtractorGDBRemote::eResponse) {
1546       llvm::StringRef name;
1547       llvm::StringRef value;
1548       addr_t addr_value = LLDB_INVALID_ADDRESS;
1549       bool success = true;
1550       bool saw_permissions = false;
1551       while (success && response.GetNameColonValue(name, value)) {
1552         if (name.equals("start")) {
1553           if (!value.getAsInteger(16, addr_value))
1554             region_info.GetRange().SetRangeBase(addr_value);
1555         } else if (name.equals("size")) {
1556           if (!value.getAsInteger(16, addr_value))
1557             region_info.GetRange().SetByteSize(addr_value);
1558         } else if (name.equals("permissions") &&
1559                    region_info.GetRange().IsValid()) {
1560           saw_permissions = true;
1561           if (region_info.GetRange().Contains(addr)) {
1562             if (value.contains('r'))
1563               region_info.SetReadable(MemoryRegionInfo::eYes);
1564             else
1565               region_info.SetReadable(MemoryRegionInfo::eNo);
1566 
1567             if (value.contains('w'))
1568               region_info.SetWritable(MemoryRegionInfo::eYes);
1569             else
1570               region_info.SetWritable(MemoryRegionInfo::eNo);
1571 
1572             if (value.contains('x'))
1573               region_info.SetExecutable(MemoryRegionInfo::eYes);
1574             else
1575               region_info.SetExecutable(MemoryRegionInfo::eNo);
1576 
1577             region_info.SetMapped(MemoryRegionInfo::eYes);
1578           } else {
1579             // The reported region does not contain this address -- we're
1580             // looking at an unmapped page
1581             region_info.SetReadable(MemoryRegionInfo::eNo);
1582             region_info.SetWritable(MemoryRegionInfo::eNo);
1583             region_info.SetExecutable(MemoryRegionInfo::eNo);
1584             region_info.SetMapped(MemoryRegionInfo::eNo);
1585           }
1586         } else if (name.equals("name")) {
1587           StringExtractorGDBRemote name_extractor(value);
1588           std::string name;
1589           name_extractor.GetHexByteString(name);
1590           region_info.SetName(name.c_str());
1591         } else if (name.equals("flags")) {
1592           region_info.SetMemoryTagged(MemoryRegionInfo::eNo);
1593 
1594           llvm::StringRef flags = value;
1595           llvm::StringRef flag;
1596           while (flags.size()) {
1597             flags = flags.ltrim();
1598             std::tie(flag, flags) = flags.split(' ');
1599             // To account for trailing whitespace
1600             if (flag.size()) {
1601               if (flag == "mt") {
1602                 region_info.SetMemoryTagged(MemoryRegionInfo::eYes);
1603                 break;
1604               }
1605             }
1606           }
1607         } else if (name.equals("type")) {
1608           std::string comma_sep_str = value.str();
1609           size_t comma_pos;
1610           while ((comma_pos = comma_sep_str.find(',')) != std::string::npos) {
1611             comma_sep_str[comma_pos] = '\0';
1612             if (comma_sep_str == "stack") {
1613               region_info.SetIsStackMemory(MemoryRegionInfo::eYes);
1614             }
1615           }
1616           // handle final (or only) type of "stack"
1617           if (comma_sep_str == "stack") {
1618             region_info.SetIsStackMemory(MemoryRegionInfo::eYes);
1619           }
1620         } else if (name.equals("error")) {
1621           StringExtractorGDBRemote error_extractor(value);
1622           std::string error_string;
1623           // Now convert the HEX bytes into a string value
1624           error_extractor.GetHexByteString(error_string);
1625           error.SetErrorString(error_string.c_str());
1626         } else if (name.equals("dirty-pages")) {
1627           std::vector<addr_t> dirty_page_list;
1628           for (llvm::StringRef x : llvm::split(value, ',')) {
1629             addr_t page;
1630             x.consume_front("0x");
1631             if (llvm::to_integer(x, page, 16))
1632               dirty_page_list.push_back(page);
1633           }
1634           region_info.SetDirtyPageList(dirty_page_list);
1635         }
1636       }
1637 
1638       if (m_target_vm_page_size != 0)
1639         region_info.SetPageSize(m_target_vm_page_size);
1640 
1641       if (region_info.GetRange().IsValid()) {
1642         // We got a valid address range back but no permissions -- which means
1643         // this is an unmapped page
1644         if (!saw_permissions) {
1645           region_info.SetReadable(MemoryRegionInfo::eNo);
1646           region_info.SetWritable(MemoryRegionInfo::eNo);
1647           region_info.SetExecutable(MemoryRegionInfo::eNo);
1648           region_info.SetMapped(MemoryRegionInfo::eNo);
1649         }
1650       } else {
1651         // We got an invalid address range back
1652         error.SetErrorString("Server returned invalid range");
1653       }
1654     } else {
1655       m_supports_memory_region_info = eLazyBoolNo;
1656     }
1657   }
1658 
1659   if (m_supports_memory_region_info == eLazyBoolNo) {
1660     error.SetErrorString("qMemoryRegionInfo is not supported");
1661   }
1662 
1663   // Try qXfer:memory-map:read to get region information not included in
1664   // qMemoryRegionInfo
1665   MemoryRegionInfo qXfer_region_info;
1666   Status qXfer_error = GetQXferMemoryMapRegionInfo(addr, qXfer_region_info);
1667 
1668   if (error.Fail()) {
1669     // If qMemoryRegionInfo failed, but qXfer:memory-map:read succeeded, use
1670     // the qXfer result as a fallback
1671     if (qXfer_error.Success()) {
1672       region_info = qXfer_region_info;
1673       error.Clear();
1674     } else {
1675       region_info.Clear();
1676     }
1677   } else if (qXfer_error.Success()) {
1678     // If both qMemoryRegionInfo and qXfer:memory-map:read succeeded, and if
1679     // both regions are the same range, update the result to include the flash-
1680     // memory information that is specific to the qXfer result.
1681     if (region_info.GetRange() == qXfer_region_info.GetRange()) {
1682       region_info.SetFlash(qXfer_region_info.GetFlash());
1683       region_info.SetBlocksize(qXfer_region_info.GetBlocksize());
1684     }
1685   }
1686   return error;
1687 }
1688 
1689 Status GDBRemoteCommunicationClient::GetQXferMemoryMapRegionInfo(
1690     lldb::addr_t addr, MemoryRegionInfo &region) {
1691   Status error = LoadQXferMemoryMap();
1692   if (!error.Success())
1693     return error;
1694   for (const auto &map_region : m_qXfer_memory_map) {
1695     if (map_region.GetRange().Contains(addr)) {
1696       region = map_region;
1697       return error;
1698     }
1699   }
1700   error.SetErrorString("Region not found");
1701   return error;
1702 }
1703 
1704 Status GDBRemoteCommunicationClient::LoadQXferMemoryMap() {
1705 
1706   Status error;
1707 
1708   if (m_qXfer_memory_map_loaded)
1709     // Already loaded, return success
1710     return error;
1711 
1712   if (!XMLDocument::XMLEnabled()) {
1713     error.SetErrorString("XML is not supported");
1714     return error;
1715   }
1716 
1717   if (!GetQXferMemoryMapReadSupported()) {
1718     error.SetErrorString("Memory map is not supported");
1719     return error;
1720   }
1721 
1722   llvm::Expected<std::string> xml = ReadExtFeature("memory-map", "");
1723   if (!xml)
1724     return Status(xml.takeError());
1725 
1726   XMLDocument xml_document;
1727 
1728   if (!xml_document.ParseMemory(xml->c_str(), xml->size())) {
1729     error.SetErrorString("Failed to parse memory map xml");
1730     return error;
1731   }
1732 
1733   XMLNode map_node = xml_document.GetRootElement("memory-map");
1734   if (!map_node) {
1735     error.SetErrorString("Invalid root node in memory map xml");
1736     return error;
1737   }
1738 
1739   m_qXfer_memory_map.clear();
1740 
1741   map_node.ForEachChildElement([this](const XMLNode &memory_node) -> bool {
1742     if (!memory_node.IsElement())
1743       return true;
1744     if (memory_node.GetName() != "memory")
1745       return true;
1746     auto type = memory_node.GetAttributeValue("type", "");
1747     uint64_t start;
1748     uint64_t length;
1749     if (!memory_node.GetAttributeValueAsUnsigned("start", start))
1750       return true;
1751     if (!memory_node.GetAttributeValueAsUnsigned("length", length))
1752       return true;
1753     MemoryRegionInfo region;
1754     region.GetRange().SetRangeBase(start);
1755     region.GetRange().SetByteSize(length);
1756     if (type == "rom") {
1757       region.SetReadable(MemoryRegionInfo::eYes);
1758       this->m_qXfer_memory_map.push_back(region);
1759     } else if (type == "ram") {
1760       region.SetReadable(MemoryRegionInfo::eYes);
1761       region.SetWritable(MemoryRegionInfo::eYes);
1762       this->m_qXfer_memory_map.push_back(region);
1763     } else if (type == "flash") {
1764       region.SetFlash(MemoryRegionInfo::eYes);
1765       memory_node.ForEachChildElement(
1766           [&region](const XMLNode &prop_node) -> bool {
1767             if (!prop_node.IsElement())
1768               return true;
1769             if (prop_node.GetName() != "property")
1770               return true;
1771             auto propname = prop_node.GetAttributeValue("name", "");
1772             if (propname == "blocksize") {
1773               uint64_t blocksize;
1774               if (prop_node.GetElementTextAsUnsigned(blocksize))
1775                 region.SetBlocksize(blocksize);
1776             }
1777             return true;
1778           });
1779       this->m_qXfer_memory_map.push_back(region);
1780     }
1781     return true;
1782   });
1783 
1784   m_qXfer_memory_map_loaded = true;
1785 
1786   return error;
1787 }
1788 
1789 Status GDBRemoteCommunicationClient::GetWatchpointSupportInfo(uint32_t &num) {
1790   Status error;
1791 
1792   if (m_supports_watchpoint_support_info == eLazyBoolYes) {
1793     num = m_num_supported_hardware_watchpoints;
1794     return error;
1795   }
1796 
1797   // Set num to 0 first.
1798   num = 0;
1799   if (m_supports_watchpoint_support_info != eLazyBoolNo) {
1800     StringExtractorGDBRemote response;
1801     if (SendPacketAndWaitForResponse("qWatchpointSupportInfo:", response) ==
1802         PacketResult::Success) {
1803       m_supports_watchpoint_support_info = eLazyBoolYes;
1804       llvm::StringRef name;
1805       llvm::StringRef value;
1806       bool found_num_field = false;
1807       while (response.GetNameColonValue(name, value)) {
1808         if (name.equals("num")) {
1809           value.getAsInteger(0, m_num_supported_hardware_watchpoints);
1810           num = m_num_supported_hardware_watchpoints;
1811           found_num_field = true;
1812         }
1813       }
1814       if (!found_num_field) {
1815         m_supports_watchpoint_support_info = eLazyBoolNo;
1816       }
1817     } else {
1818       m_supports_watchpoint_support_info = eLazyBoolNo;
1819     }
1820   }
1821 
1822   if (m_supports_watchpoint_support_info == eLazyBoolNo) {
1823     error.SetErrorString("qWatchpointSupportInfo is not supported");
1824   }
1825   return error;
1826 }
1827 
1828 lldb_private::Status GDBRemoteCommunicationClient::GetWatchpointSupportInfo(
1829     uint32_t &num, bool &after, const ArchSpec &arch) {
1830   Status error(GetWatchpointSupportInfo(num));
1831   if (error.Success())
1832     error = GetWatchpointsTriggerAfterInstruction(after, arch);
1833   return error;
1834 }
1835 
1836 lldb_private::Status
1837 GDBRemoteCommunicationClient::GetWatchpointsTriggerAfterInstruction(
1838     bool &after, const ArchSpec &arch) {
1839   Status error;
1840   llvm::Triple triple = arch.GetTriple();
1841 
1842   // we assume watchpoints will happen after running the relevant opcode and we
1843   // only want to override this behavior if we have explicitly received a
1844   // qHostInfo telling us otherwise
1845   if (m_qHostInfo_is_valid != eLazyBoolYes) {
1846     // On targets like MIPS and ppc64, watchpoint exceptions are always
1847     // generated before the instruction is executed. The connected target may
1848     // not support qHostInfo or qWatchpointSupportInfo packets.
1849     after = !(triple.isMIPS() || triple.isPPC64());
1850   } else {
1851     // For MIPS and ppc64, set m_watchpoints_trigger_after_instruction to
1852     // eLazyBoolNo if it is not calculated before.
1853     if (m_watchpoints_trigger_after_instruction == eLazyBoolCalculate &&
1854         (triple.isMIPS() || triple.isPPC64()))
1855       m_watchpoints_trigger_after_instruction = eLazyBoolNo;
1856 
1857     after = (m_watchpoints_trigger_after_instruction != eLazyBoolNo);
1858   }
1859   return error;
1860 }
1861 
1862 int GDBRemoteCommunicationClient::SetSTDIN(const FileSpec &file_spec) {
1863   if (file_spec) {
1864     std::string path{file_spec.GetPath(false)};
1865     StreamString packet;
1866     packet.PutCString("QSetSTDIN:");
1867     packet.PutStringAsRawHex8(path);
1868 
1869     StringExtractorGDBRemote response;
1870     if (SendPacketAndWaitForResponse(packet.GetString(), response) ==
1871         PacketResult::Success) {
1872       if (response.IsOKResponse())
1873         return 0;
1874       uint8_t error = response.GetError();
1875       if (error)
1876         return error;
1877     }
1878   }
1879   return -1;
1880 }
1881 
1882 int GDBRemoteCommunicationClient::SetSTDOUT(const FileSpec &file_spec) {
1883   if (file_spec) {
1884     std::string path{file_spec.GetPath(false)};
1885     StreamString packet;
1886     packet.PutCString("QSetSTDOUT:");
1887     packet.PutStringAsRawHex8(path);
1888 
1889     StringExtractorGDBRemote response;
1890     if (SendPacketAndWaitForResponse(packet.GetString(), response) ==
1891         PacketResult::Success) {
1892       if (response.IsOKResponse())
1893         return 0;
1894       uint8_t error = response.GetError();
1895       if (error)
1896         return error;
1897     }
1898   }
1899   return -1;
1900 }
1901 
1902 int GDBRemoteCommunicationClient::SetSTDERR(const FileSpec &file_spec) {
1903   if (file_spec) {
1904     std::string path{file_spec.GetPath(false)};
1905     StreamString packet;
1906     packet.PutCString("QSetSTDERR:");
1907     packet.PutStringAsRawHex8(path);
1908 
1909     StringExtractorGDBRemote response;
1910     if (SendPacketAndWaitForResponse(packet.GetString(), response) ==
1911         PacketResult::Success) {
1912       if (response.IsOKResponse())
1913         return 0;
1914       uint8_t error = response.GetError();
1915       if (error)
1916         return error;
1917     }
1918   }
1919   return -1;
1920 }
1921 
1922 bool GDBRemoteCommunicationClient::GetWorkingDir(FileSpec &working_dir) {
1923   StringExtractorGDBRemote response;
1924   if (SendPacketAndWaitForResponse("qGetWorkingDir", response) ==
1925       PacketResult::Success) {
1926     if (response.IsUnsupportedResponse())
1927       return false;
1928     if (response.IsErrorResponse())
1929       return false;
1930     std::string cwd;
1931     response.GetHexByteString(cwd);
1932     working_dir.SetFile(cwd, GetHostArchitecture().GetTriple());
1933     return !cwd.empty();
1934   }
1935   return false;
1936 }
1937 
1938 int GDBRemoteCommunicationClient::SetWorkingDir(const FileSpec &working_dir) {
1939   if (working_dir) {
1940     std::string path{working_dir.GetPath(false)};
1941     StreamString packet;
1942     packet.PutCString("QSetWorkingDir:");
1943     packet.PutStringAsRawHex8(path);
1944 
1945     StringExtractorGDBRemote response;
1946     if (SendPacketAndWaitForResponse(packet.GetString(), response) ==
1947         PacketResult::Success) {
1948       if (response.IsOKResponse())
1949         return 0;
1950       uint8_t error = response.GetError();
1951       if (error)
1952         return error;
1953     }
1954   }
1955   return -1;
1956 }
1957 
1958 int GDBRemoteCommunicationClient::SetDisableASLR(bool enable) {
1959   char packet[32];
1960   const int packet_len =
1961       ::snprintf(packet, sizeof(packet), "QSetDisableASLR:%i", enable ? 1 : 0);
1962   assert(packet_len < (int)sizeof(packet));
1963   UNUSED_IF_ASSERT_DISABLED(packet_len);
1964   StringExtractorGDBRemote response;
1965   if (SendPacketAndWaitForResponse(packet, response) == PacketResult::Success) {
1966     if (response.IsOKResponse())
1967       return 0;
1968     uint8_t error = response.GetError();
1969     if (error)
1970       return error;
1971   }
1972   return -1;
1973 }
1974 
1975 int GDBRemoteCommunicationClient::SetDetachOnError(bool enable) {
1976   char packet[32];
1977   const int packet_len = ::snprintf(packet, sizeof(packet),
1978                                     "QSetDetachOnError:%i", enable ? 1 : 0);
1979   assert(packet_len < (int)sizeof(packet));
1980   UNUSED_IF_ASSERT_DISABLED(packet_len);
1981   StringExtractorGDBRemote response;
1982   if (SendPacketAndWaitForResponse(packet, response) == PacketResult::Success) {
1983     if (response.IsOKResponse())
1984       return 0;
1985     uint8_t error = response.GetError();
1986     if (error)
1987       return error;
1988   }
1989   return -1;
1990 }
1991 
1992 bool GDBRemoteCommunicationClient::DecodeProcessInfoResponse(
1993     StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info) {
1994   if (response.IsNormalResponse()) {
1995     llvm::StringRef name;
1996     llvm::StringRef value;
1997     StringExtractor extractor;
1998 
1999     uint32_t cpu = LLDB_INVALID_CPUTYPE;
2000     uint32_t sub = 0;
2001     std::string vendor;
2002     std::string os_type;
2003 
2004     while (response.GetNameColonValue(name, value)) {
2005       if (name.equals("pid")) {
2006         lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
2007         value.getAsInteger(0, pid);
2008         process_info.SetProcessID(pid);
2009       } else if (name.equals("ppid")) {
2010         lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
2011         value.getAsInteger(0, pid);
2012         process_info.SetParentProcessID(pid);
2013       } else if (name.equals("uid")) {
2014         uint32_t uid = UINT32_MAX;
2015         value.getAsInteger(0, uid);
2016         process_info.SetUserID(uid);
2017       } else if (name.equals("euid")) {
2018         uint32_t uid = UINT32_MAX;
2019         value.getAsInteger(0, uid);
2020         process_info.SetEffectiveUserID(uid);
2021       } else if (name.equals("gid")) {
2022         uint32_t gid = UINT32_MAX;
2023         value.getAsInteger(0, gid);
2024         process_info.SetGroupID(gid);
2025       } else if (name.equals("egid")) {
2026         uint32_t gid = UINT32_MAX;
2027         value.getAsInteger(0, gid);
2028         process_info.SetEffectiveGroupID(gid);
2029       } else if (name.equals("triple")) {
2030         StringExtractor extractor(value);
2031         std::string triple;
2032         extractor.GetHexByteString(triple);
2033         process_info.GetArchitecture().SetTriple(triple.c_str());
2034       } else if (name.equals("name")) {
2035         StringExtractor extractor(value);
2036         // The process name from ASCII hex bytes since we can't control the
2037         // characters in a process name
2038         std::string name;
2039         extractor.GetHexByteString(name);
2040         process_info.GetExecutableFile().SetFile(name, FileSpec::Style::native);
2041       } else if (name.equals("args")) {
2042         llvm::StringRef encoded_args(value), hex_arg;
2043 
2044         bool is_arg0 = true;
2045         while (!encoded_args.empty()) {
2046           std::tie(hex_arg, encoded_args) = encoded_args.split('-');
2047           std::string arg;
2048           StringExtractor extractor(hex_arg);
2049           if (extractor.GetHexByteString(arg) * 2 != hex_arg.size()) {
2050             // In case of wrong encoding, we discard all the arguments
2051             process_info.GetArguments().Clear();
2052             process_info.SetArg0("");
2053             break;
2054           }
2055           if (is_arg0)
2056             process_info.SetArg0(arg);
2057           else
2058             process_info.GetArguments().AppendArgument(arg);
2059           is_arg0 = false;
2060         }
2061       } else if (name.equals("cputype")) {
2062         value.getAsInteger(0, cpu);
2063       } else if (name.equals("cpusubtype")) {
2064         value.getAsInteger(0, sub);
2065       } else if (name.equals("vendor")) {
2066         vendor = std::string(value);
2067       } else if (name.equals("ostype")) {
2068         os_type = std::string(value);
2069       }
2070     }
2071 
2072     if (cpu != LLDB_INVALID_CPUTYPE && !vendor.empty() && !os_type.empty()) {
2073       if (vendor == "apple") {
2074         process_info.GetArchitecture().SetArchitecture(eArchTypeMachO, cpu,
2075                                                        sub);
2076         process_info.GetArchitecture().GetTriple().setVendorName(
2077             llvm::StringRef(vendor));
2078         process_info.GetArchitecture().GetTriple().setOSName(
2079             llvm::StringRef(os_type));
2080       }
2081     }
2082 
2083     if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
2084       return true;
2085   }
2086   return false;
2087 }
2088 
2089 bool GDBRemoteCommunicationClient::GetProcessInfo(
2090     lldb::pid_t pid, ProcessInstanceInfo &process_info) {
2091   process_info.Clear();
2092 
2093   if (m_supports_qProcessInfoPID) {
2094     char packet[32];
2095     const int packet_len =
2096         ::snprintf(packet, sizeof(packet), "qProcessInfoPID:%" PRIu64, pid);
2097     assert(packet_len < (int)sizeof(packet));
2098     UNUSED_IF_ASSERT_DISABLED(packet_len);
2099     StringExtractorGDBRemote response;
2100     if (SendPacketAndWaitForResponse(packet, response) ==
2101         PacketResult::Success) {
2102       return DecodeProcessInfoResponse(response, process_info);
2103     } else {
2104       m_supports_qProcessInfoPID = false;
2105       return false;
2106     }
2107   }
2108   return false;
2109 }
2110 
2111 bool GDBRemoteCommunicationClient::GetCurrentProcessInfo(bool allow_lazy) {
2112   Log *log(GetLog(GDBRLog::Process | GDBRLog::Packets));
2113 
2114   if (allow_lazy) {
2115     if (m_qProcessInfo_is_valid == eLazyBoolYes)
2116       return true;
2117     if (m_qProcessInfo_is_valid == eLazyBoolNo)
2118       return false;
2119   }
2120 
2121   GetHostInfo();
2122 
2123   StringExtractorGDBRemote response;
2124   if (SendPacketAndWaitForResponse("qProcessInfo", response) ==
2125       PacketResult::Success) {
2126     if (response.IsNormalResponse()) {
2127       llvm::StringRef name;
2128       llvm::StringRef value;
2129       uint32_t cpu = LLDB_INVALID_CPUTYPE;
2130       uint32_t sub = 0;
2131       std::string arch_name;
2132       std::string os_name;
2133       std::string environment;
2134       std::string vendor_name;
2135       std::string triple;
2136       std::string elf_abi;
2137       uint32_t pointer_byte_size = 0;
2138       StringExtractor extractor;
2139       ByteOrder byte_order = eByteOrderInvalid;
2140       uint32_t num_keys_decoded = 0;
2141       lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
2142       while (response.GetNameColonValue(name, value)) {
2143         if (name.equals("cputype")) {
2144           if (!value.getAsInteger(16, cpu))
2145             ++num_keys_decoded;
2146         } else if (name.equals("cpusubtype")) {
2147           if (!value.getAsInteger(16, sub))
2148             ++num_keys_decoded;
2149         } else if (name.equals("triple")) {
2150           StringExtractor extractor(value);
2151           extractor.GetHexByteString(triple);
2152           ++num_keys_decoded;
2153         } else if (name.equals("ostype")) {
2154           ParseOSType(value, os_name, environment);
2155           ++num_keys_decoded;
2156         } else if (name.equals("vendor")) {
2157           vendor_name = std::string(value);
2158           ++num_keys_decoded;
2159         } else if (name.equals("endian")) {
2160           byte_order = llvm::StringSwitch<lldb::ByteOrder>(value)
2161                            .Case("little", eByteOrderLittle)
2162                            .Case("big", eByteOrderBig)
2163                            .Case("pdp", eByteOrderPDP)
2164                            .Default(eByteOrderInvalid);
2165           if (byte_order != eByteOrderInvalid)
2166             ++num_keys_decoded;
2167         } else if (name.equals("ptrsize")) {
2168           if (!value.getAsInteger(16, pointer_byte_size))
2169             ++num_keys_decoded;
2170         } else if (name.equals("pid")) {
2171           if (!value.getAsInteger(16, pid))
2172             ++num_keys_decoded;
2173         } else if (name.equals("elf_abi")) {
2174           elf_abi = std::string(value);
2175           ++num_keys_decoded;
2176         } else if (name.equals("main-binary-uuid")) {
2177           m_process_standalone_uuid.SetFromStringRef(value);
2178           ++num_keys_decoded;
2179         } else if (name.equals("main-binary-slide")) {
2180           StringExtractor extractor(value);
2181           m_process_standalone_value =
2182               extractor.GetU64(LLDB_INVALID_ADDRESS, 16);
2183           if (m_process_standalone_value != LLDB_INVALID_ADDRESS) {
2184             m_process_standalone_value_is_offset = true;
2185             ++num_keys_decoded;
2186           }
2187         } else if (name.equals("main-binary-address")) {
2188           StringExtractor extractor(value);
2189           m_process_standalone_value =
2190               extractor.GetU64(LLDB_INVALID_ADDRESS, 16);
2191           if (m_process_standalone_value != LLDB_INVALID_ADDRESS) {
2192             m_process_standalone_value_is_offset = false;
2193             ++num_keys_decoded;
2194           }
2195         }
2196       }
2197       if (num_keys_decoded > 0)
2198         m_qProcessInfo_is_valid = eLazyBoolYes;
2199       if (pid != LLDB_INVALID_PROCESS_ID) {
2200         m_curr_pid_is_valid = eLazyBoolYes;
2201         m_curr_pid_run = m_curr_pid = pid;
2202       }
2203 
2204       // Set the ArchSpec from the triple if we have it.
2205       if (!triple.empty()) {
2206         m_process_arch.SetTriple(triple.c_str());
2207         m_process_arch.SetFlags(elf_abi);
2208         if (pointer_byte_size) {
2209           assert(pointer_byte_size == m_process_arch.GetAddressByteSize());
2210         }
2211       } else if (cpu != LLDB_INVALID_CPUTYPE && !os_name.empty() &&
2212                  !vendor_name.empty()) {
2213         llvm::Triple triple(llvm::Twine("-") + vendor_name + "-" + os_name);
2214         if (!environment.empty())
2215             triple.setEnvironmentName(environment);
2216 
2217         assert(triple.getObjectFormat() != llvm::Triple::UnknownObjectFormat);
2218         assert(triple.getObjectFormat() != llvm::Triple::Wasm);
2219         assert(triple.getObjectFormat() != llvm::Triple::XCOFF);
2220         switch (triple.getObjectFormat()) {
2221         case llvm::Triple::MachO:
2222           m_process_arch.SetArchitecture(eArchTypeMachO, cpu, sub);
2223           break;
2224         case llvm::Triple::ELF:
2225           m_process_arch.SetArchitecture(eArchTypeELF, cpu, sub);
2226           break;
2227         case llvm::Triple::COFF:
2228           m_process_arch.SetArchitecture(eArchTypeCOFF, cpu, sub);
2229           break;
2230         case llvm::Triple::GOFF:
2231         case llvm::Triple::Wasm:
2232         case llvm::Triple::XCOFF:
2233           LLDB_LOGF(log, "error: not supported target architecture");
2234           return false;
2235         case llvm::Triple::UnknownObjectFormat:
2236           LLDB_LOGF(log, "error: failed to determine target architecture");
2237           return false;
2238         }
2239 
2240         if (pointer_byte_size) {
2241           assert(pointer_byte_size == m_process_arch.GetAddressByteSize());
2242         }
2243         if (byte_order != eByteOrderInvalid) {
2244           assert(byte_order == m_process_arch.GetByteOrder());
2245         }
2246         m_process_arch.GetTriple().setVendorName(llvm::StringRef(vendor_name));
2247         m_process_arch.GetTriple().setOSName(llvm::StringRef(os_name));
2248         m_process_arch.GetTriple().setEnvironmentName(llvm::StringRef(environment));
2249         m_host_arch.GetTriple().setVendorName(llvm::StringRef(vendor_name));
2250         m_host_arch.GetTriple().setOSName(llvm::StringRef(os_name));
2251         m_host_arch.GetTriple().setEnvironmentName(llvm::StringRef(environment));
2252       }
2253       return true;
2254     }
2255   } else {
2256     m_qProcessInfo_is_valid = eLazyBoolNo;
2257   }
2258 
2259   return false;
2260 }
2261 
2262 uint32_t GDBRemoteCommunicationClient::FindProcesses(
2263     const ProcessInstanceInfoMatch &match_info,
2264     ProcessInstanceInfoList &process_infos) {
2265   process_infos.clear();
2266 
2267   if (m_supports_qfProcessInfo) {
2268     StreamString packet;
2269     packet.PutCString("qfProcessInfo");
2270     if (!match_info.MatchAllProcesses()) {
2271       packet.PutChar(':');
2272       const char *name = match_info.GetProcessInfo().GetName();
2273       bool has_name_match = false;
2274       if (name && name[0]) {
2275         has_name_match = true;
2276         NameMatch name_match_type = match_info.GetNameMatchType();
2277         switch (name_match_type) {
2278         case NameMatch::Ignore:
2279           has_name_match = false;
2280           break;
2281 
2282         case NameMatch::Equals:
2283           packet.PutCString("name_match:equals;");
2284           break;
2285 
2286         case NameMatch::Contains:
2287           packet.PutCString("name_match:contains;");
2288           break;
2289 
2290         case NameMatch::StartsWith:
2291           packet.PutCString("name_match:starts_with;");
2292           break;
2293 
2294         case NameMatch::EndsWith:
2295           packet.PutCString("name_match:ends_with;");
2296           break;
2297 
2298         case NameMatch::RegularExpression:
2299           packet.PutCString("name_match:regex;");
2300           break;
2301         }
2302         if (has_name_match) {
2303           packet.PutCString("name:");
2304           packet.PutBytesAsRawHex8(name, ::strlen(name));
2305           packet.PutChar(';');
2306         }
2307       }
2308 
2309       if (match_info.GetProcessInfo().ProcessIDIsValid())
2310         packet.Printf("pid:%" PRIu64 ";",
2311                       match_info.GetProcessInfo().GetProcessID());
2312       if (match_info.GetProcessInfo().ParentProcessIDIsValid())
2313         packet.Printf("parent_pid:%" PRIu64 ";",
2314                       match_info.GetProcessInfo().GetParentProcessID());
2315       if (match_info.GetProcessInfo().UserIDIsValid())
2316         packet.Printf("uid:%u;", match_info.GetProcessInfo().GetUserID());
2317       if (match_info.GetProcessInfo().GroupIDIsValid())
2318         packet.Printf("gid:%u;", match_info.GetProcessInfo().GetGroupID());
2319       if (match_info.GetProcessInfo().EffectiveUserIDIsValid())
2320         packet.Printf("euid:%u;",
2321                       match_info.GetProcessInfo().GetEffectiveUserID());
2322       if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
2323         packet.Printf("egid:%u;",
2324                       match_info.GetProcessInfo().GetEffectiveGroupID());
2325       packet.Printf("all_users:%u;", match_info.GetMatchAllUsers() ? 1 : 0);
2326       if (match_info.GetProcessInfo().GetArchitecture().IsValid()) {
2327         const ArchSpec &match_arch =
2328             match_info.GetProcessInfo().GetArchitecture();
2329         const llvm::Triple &triple = match_arch.GetTriple();
2330         packet.PutCString("triple:");
2331         packet.PutCString(triple.getTriple());
2332         packet.PutChar(';');
2333       }
2334     }
2335     StringExtractorGDBRemote response;
2336     // Increase timeout as the first qfProcessInfo packet takes a long time on
2337     // Android. The value of 1min was arrived at empirically.
2338     ScopedTimeout timeout(*this, minutes(1));
2339     if (SendPacketAndWaitForResponse(packet.GetString(), response) ==
2340         PacketResult::Success) {
2341       do {
2342         ProcessInstanceInfo process_info;
2343         if (!DecodeProcessInfoResponse(response, process_info))
2344           break;
2345         process_infos.push_back(process_info);
2346         response = StringExtractorGDBRemote();
2347       } while (SendPacketAndWaitForResponse("qsProcessInfo", response) ==
2348                PacketResult::Success);
2349     } else {
2350       m_supports_qfProcessInfo = false;
2351       return 0;
2352     }
2353   }
2354   return process_infos.size();
2355 }
2356 
2357 bool GDBRemoteCommunicationClient::GetUserName(uint32_t uid,
2358                                                std::string &name) {
2359   if (m_supports_qUserName) {
2360     char packet[32];
2361     const int packet_len =
2362         ::snprintf(packet, sizeof(packet), "qUserName:%i", uid);
2363     assert(packet_len < (int)sizeof(packet));
2364     UNUSED_IF_ASSERT_DISABLED(packet_len);
2365     StringExtractorGDBRemote response;
2366     if (SendPacketAndWaitForResponse(packet, response) ==
2367         PacketResult::Success) {
2368       if (response.IsNormalResponse()) {
2369         // Make sure we parsed the right number of characters. The response is
2370         // the hex encoded user name and should make up the entire packet. If
2371         // there are any non-hex ASCII bytes, the length won't match below..
2372         if (response.GetHexByteString(name) * 2 ==
2373             response.GetStringRef().size())
2374           return true;
2375       }
2376     } else {
2377       m_supports_qUserName = false;
2378       return false;
2379     }
2380   }
2381   return false;
2382 }
2383 
2384 bool GDBRemoteCommunicationClient::GetGroupName(uint32_t gid,
2385                                                 std::string &name) {
2386   if (m_supports_qGroupName) {
2387     char packet[32];
2388     const int packet_len =
2389         ::snprintf(packet, sizeof(packet), "qGroupName:%i", gid);
2390     assert(packet_len < (int)sizeof(packet));
2391     UNUSED_IF_ASSERT_DISABLED(packet_len);
2392     StringExtractorGDBRemote response;
2393     if (SendPacketAndWaitForResponse(packet, response) ==
2394         PacketResult::Success) {
2395       if (response.IsNormalResponse()) {
2396         // Make sure we parsed the right number of characters. The response is
2397         // the hex encoded group name and should make up the entire packet. If
2398         // there are any non-hex ASCII bytes, the length won't match below..
2399         if (response.GetHexByteString(name) * 2 ==
2400             response.GetStringRef().size())
2401           return true;
2402       }
2403     } else {
2404       m_supports_qGroupName = false;
2405       return false;
2406     }
2407   }
2408   return false;
2409 }
2410 
2411 static void MakeSpeedTestPacket(StreamString &packet, uint32_t send_size,
2412                                 uint32_t recv_size) {
2413   packet.Clear();
2414   packet.Printf("qSpeedTest:response_size:%i;data:", recv_size);
2415   uint32_t bytes_left = send_size;
2416   while (bytes_left > 0) {
2417     if (bytes_left >= 26) {
2418       packet.PutCString("abcdefghijklmnopqrstuvwxyz");
2419       bytes_left -= 26;
2420     } else {
2421       packet.Printf("%*.*s;", bytes_left, bytes_left,
2422                     "abcdefghijklmnopqrstuvwxyz");
2423       bytes_left = 0;
2424     }
2425   }
2426 }
2427 
2428 duration<float>
2429 calculate_standard_deviation(const std::vector<duration<float>> &v) {
2430   using Dur = duration<float>;
2431   Dur sum = std::accumulate(std::begin(v), std::end(v), Dur());
2432   Dur mean = sum / v.size();
2433   float accum = 0;
2434   for (auto d : v) {
2435     float delta = (d - mean).count();
2436     accum += delta * delta;
2437   };
2438 
2439   return Dur(sqrtf(accum / (v.size() - 1)));
2440 }
2441 
2442 void GDBRemoteCommunicationClient::TestPacketSpeed(const uint32_t num_packets,
2443                                                    uint32_t max_send,
2444                                                    uint32_t max_recv,
2445                                                    uint64_t recv_amount,
2446                                                    bool json, Stream &strm) {
2447   uint32_t i;
2448   if (SendSpeedTestPacket(0, 0)) {
2449     StreamString packet;
2450     if (json)
2451       strm.Printf("{ \"packet_speeds\" : {\n    \"num_packets\" : %u,\n    "
2452                   "\"results\" : [",
2453                   num_packets);
2454     else
2455       strm.Printf("Testing sending %u packets of various sizes:\n",
2456                   num_packets);
2457     strm.Flush();
2458 
2459     uint32_t result_idx = 0;
2460     uint32_t send_size;
2461     std::vector<duration<float>> packet_times;
2462 
2463     for (send_size = 0; send_size <= max_send;
2464          send_size ? send_size *= 2 : send_size = 4) {
2465       for (uint32_t recv_size = 0; recv_size <= max_recv;
2466            recv_size ? recv_size *= 2 : recv_size = 4) {
2467         MakeSpeedTestPacket(packet, send_size, recv_size);
2468 
2469         packet_times.clear();
2470         // Test how long it takes to send 'num_packets' packets
2471         const auto start_time = steady_clock::now();
2472         for (i = 0; i < num_packets; ++i) {
2473           const auto packet_start_time = steady_clock::now();
2474           StringExtractorGDBRemote response;
2475           SendPacketAndWaitForResponse(packet.GetString(), response);
2476           const auto packet_end_time = steady_clock::now();
2477           packet_times.push_back(packet_end_time - packet_start_time);
2478         }
2479         const auto end_time = steady_clock::now();
2480         const auto total_time = end_time - start_time;
2481 
2482         float packets_per_second =
2483             ((float)num_packets) / duration<float>(total_time).count();
2484         auto average_per_packet = total_time / num_packets;
2485         const duration<float> standard_deviation =
2486             calculate_standard_deviation(packet_times);
2487         if (json) {
2488           strm.Format("{0}\n     {{\"send_size\" : {1,6}, \"recv_size\" : "
2489                       "{2,6}, \"total_time_nsec\" : {3,12:ns-}, "
2490                       "\"standard_deviation_nsec\" : {4,9:ns-f0}}",
2491                       result_idx > 0 ? "," : "", send_size, recv_size,
2492                       total_time, standard_deviation);
2493           ++result_idx;
2494         } else {
2495           strm.Format("qSpeedTest(send={0,7}, recv={1,7}) in {2:s+f9} for "
2496                       "{3,9:f2} packets/s ({4,10:ms+f6} per packet) with "
2497                       "standard deviation of {5,10:ms+f6}\n",
2498                       send_size, recv_size, duration<float>(total_time),
2499                       packets_per_second, duration<float>(average_per_packet),
2500                       standard_deviation);
2501         }
2502         strm.Flush();
2503       }
2504     }
2505 
2506     const float k_recv_amount_mb = (float)recv_amount / (1024.0f * 1024.0f);
2507     if (json)
2508       strm.Printf("\n    ]\n  },\n  \"download_speed\" : {\n    \"byte_size\" "
2509                   ": %" PRIu64 ",\n    \"results\" : [",
2510                   recv_amount);
2511     else
2512       strm.Printf("Testing receiving %2.1fMB of data using varying receive "
2513                   "packet sizes:\n",
2514                   k_recv_amount_mb);
2515     strm.Flush();
2516     send_size = 0;
2517     result_idx = 0;
2518     for (uint32_t recv_size = 32; recv_size <= max_recv; recv_size *= 2) {
2519       MakeSpeedTestPacket(packet, send_size, recv_size);
2520 
2521       // If we have a receive size, test how long it takes to receive 4MB of
2522       // data
2523       if (recv_size > 0) {
2524         const auto start_time = steady_clock::now();
2525         uint32_t bytes_read = 0;
2526         uint32_t packet_count = 0;
2527         while (bytes_read < recv_amount) {
2528           StringExtractorGDBRemote response;
2529           SendPacketAndWaitForResponse(packet.GetString(), response);
2530           bytes_read += recv_size;
2531           ++packet_count;
2532         }
2533         const auto end_time = steady_clock::now();
2534         const auto total_time = end_time - start_time;
2535         float mb_second = ((float)recv_amount) /
2536                           duration<float>(total_time).count() /
2537                           (1024.0 * 1024.0);
2538         float packets_per_second =
2539             ((float)packet_count) / duration<float>(total_time).count();
2540         const auto average_per_packet = total_time / packet_count;
2541 
2542         if (json) {
2543           strm.Format("{0}\n     {{\"send_size\" : {1,6}, \"recv_size\" : "
2544                       "{2,6}, \"total_time_nsec\" : {3,12:ns-}}",
2545                       result_idx > 0 ? "," : "", send_size, recv_size,
2546                       total_time);
2547           ++result_idx;
2548         } else {
2549           strm.Format("qSpeedTest(send={0,7}, recv={1,7}) {2,6} packets needed "
2550                       "to receive {3:f1}MB in {4:s+f9} for {5} MB/sec for "
2551                       "{6,9:f2} packets/sec ({7,10:ms+f6} per packet)\n",
2552                       send_size, recv_size, packet_count, k_recv_amount_mb,
2553                       duration<float>(total_time), mb_second,
2554                       packets_per_second, duration<float>(average_per_packet));
2555         }
2556         strm.Flush();
2557       }
2558     }
2559     if (json)
2560       strm.Printf("\n    ]\n  }\n}\n");
2561     else
2562       strm.EOL();
2563   }
2564 }
2565 
2566 bool GDBRemoteCommunicationClient::SendSpeedTestPacket(uint32_t send_size,
2567                                                        uint32_t recv_size) {
2568   StreamString packet;
2569   packet.Printf("qSpeedTest:response_size:%i;data:", recv_size);
2570   uint32_t bytes_left = send_size;
2571   while (bytes_left > 0) {
2572     if (bytes_left >= 26) {
2573       packet.PutCString("abcdefghijklmnopqrstuvwxyz");
2574       bytes_left -= 26;
2575     } else {
2576       packet.Printf("%*.*s;", bytes_left, bytes_left,
2577                     "abcdefghijklmnopqrstuvwxyz");
2578       bytes_left = 0;
2579     }
2580   }
2581 
2582   StringExtractorGDBRemote response;
2583   return SendPacketAndWaitForResponse(packet.GetString(), response) ==
2584          PacketResult::Success;
2585 }
2586 
2587 bool GDBRemoteCommunicationClient::LaunchGDBServer(
2588     const char *remote_accept_hostname, lldb::pid_t &pid, uint16_t &port,
2589     std::string &socket_name) {
2590   pid = LLDB_INVALID_PROCESS_ID;
2591   port = 0;
2592   socket_name.clear();
2593 
2594   StringExtractorGDBRemote response;
2595   StreamString stream;
2596   stream.PutCString("qLaunchGDBServer;");
2597   std::string hostname;
2598   if (remote_accept_hostname && remote_accept_hostname[0])
2599     hostname = remote_accept_hostname;
2600   else {
2601     if (HostInfo::GetHostname(hostname)) {
2602       // Make the GDB server we launch only accept connections from this host
2603       stream.Printf("host:%s;", hostname.c_str());
2604     } else {
2605       // Make the GDB server we launch accept connections from any host since
2606       // we can't figure out the hostname
2607       stream.Printf("host:*;");
2608     }
2609   }
2610   // give the process a few seconds to startup
2611   ScopedTimeout timeout(*this, seconds(10));
2612 
2613   if (SendPacketAndWaitForResponse(stream.GetString(), response) ==
2614       PacketResult::Success) {
2615     llvm::StringRef name;
2616     llvm::StringRef value;
2617     while (response.GetNameColonValue(name, value)) {
2618       if (name.equals("port"))
2619         value.getAsInteger(0, port);
2620       else if (name.equals("pid"))
2621         value.getAsInteger(0, pid);
2622       else if (name.compare("socket_name") == 0) {
2623         StringExtractor extractor(value);
2624         extractor.GetHexByteString(socket_name);
2625       }
2626     }
2627     return true;
2628   }
2629   return false;
2630 }
2631 
2632 size_t GDBRemoteCommunicationClient::QueryGDBServer(
2633     std::vector<std::pair<uint16_t, std::string>> &connection_urls) {
2634   connection_urls.clear();
2635 
2636   StringExtractorGDBRemote response;
2637   if (SendPacketAndWaitForResponse("qQueryGDBServer", response) !=
2638       PacketResult::Success)
2639     return 0;
2640 
2641   StructuredData::ObjectSP data =
2642       StructuredData::ParseJSON(std::string(response.GetStringRef()));
2643   if (!data)
2644     return 0;
2645 
2646   StructuredData::Array *array = data->GetAsArray();
2647   if (!array)
2648     return 0;
2649 
2650   for (size_t i = 0, count = array->GetSize(); i < count; ++i) {
2651     StructuredData::Dictionary *element = nullptr;
2652     if (!array->GetItemAtIndexAsDictionary(i, element))
2653       continue;
2654 
2655     uint16_t port = 0;
2656     if (StructuredData::ObjectSP port_osp =
2657             element->GetValueForKey(llvm::StringRef("port")))
2658       port = port_osp->GetIntegerValue(0);
2659 
2660     std::string socket_name;
2661     if (StructuredData::ObjectSP socket_name_osp =
2662             element->GetValueForKey(llvm::StringRef("socket_name")))
2663       socket_name = std::string(socket_name_osp->GetStringValue());
2664 
2665     if (port != 0 || !socket_name.empty())
2666       connection_urls.emplace_back(port, socket_name);
2667   }
2668   return connection_urls.size();
2669 }
2670 
2671 bool GDBRemoteCommunicationClient::KillSpawnedProcess(lldb::pid_t pid) {
2672   StreamString stream;
2673   stream.Printf("qKillSpawnedProcess:%" PRId64, pid);
2674 
2675   StringExtractorGDBRemote response;
2676   if (SendPacketAndWaitForResponse(stream.GetString(), response) ==
2677       PacketResult::Success) {
2678     if (response.IsOKResponse())
2679       return true;
2680   }
2681   return false;
2682 }
2683 
2684 llvm::Optional<PidTid>
2685 GDBRemoteCommunicationClient::SendSetCurrentThreadPacket(uint64_t tid,
2686                                                          uint64_t pid,
2687                                                          char op) {
2688   lldb_private::StreamString packet;
2689   packet.PutChar('H');
2690   packet.PutChar(op);
2691 
2692   if (pid != LLDB_INVALID_PROCESS_ID)
2693     packet.Printf("p%" PRIx64 ".", pid);
2694 
2695   if (tid == UINT64_MAX)
2696     packet.PutCString("-1");
2697   else
2698     packet.Printf("%" PRIx64, tid);
2699 
2700   StringExtractorGDBRemote response;
2701   if (SendPacketAndWaitForResponse(packet.GetString(), response)
2702       == PacketResult::Success) {
2703     if (response.IsOKResponse())
2704       return {{pid, tid}};
2705 
2706     /*
2707      * Connected bare-iron target (like YAMON gdb-stub) may not have support for
2708      * Hg packet.
2709      * The reply from '?' packet could be as simple as 'S05'. There is no packet
2710      * which can
2711      * give us pid and/or tid. Assume pid=tid=1 in such cases.
2712      */
2713     if (response.IsUnsupportedResponse() && IsConnected())
2714       return {{1, 1}};
2715   }
2716   return llvm::None;
2717 }
2718 
2719 bool GDBRemoteCommunicationClient::SetCurrentThread(uint64_t tid,
2720                                                     uint64_t pid) {
2721   if (m_curr_tid == tid &&
2722       (m_curr_pid == pid || LLDB_INVALID_PROCESS_ID == pid))
2723     return true;
2724 
2725   llvm::Optional<PidTid> ret = SendSetCurrentThreadPacket(tid, pid, 'g');
2726   if (ret.hasValue()) {
2727     if (ret->pid != LLDB_INVALID_PROCESS_ID)
2728       m_curr_pid = ret->pid;
2729     m_curr_tid = ret->tid;
2730   }
2731   return ret.hasValue();
2732 }
2733 
2734 bool GDBRemoteCommunicationClient::SetCurrentThreadForRun(uint64_t tid,
2735                                                           uint64_t pid) {
2736   if (m_curr_tid_run == tid &&
2737       (m_curr_pid_run == pid || LLDB_INVALID_PROCESS_ID == pid))
2738     return true;
2739 
2740   llvm::Optional<PidTid> ret = SendSetCurrentThreadPacket(tid, pid, 'c');
2741   if (ret.hasValue()) {
2742     if (ret->pid != LLDB_INVALID_PROCESS_ID)
2743       m_curr_pid_run = ret->pid;
2744     m_curr_tid_run = ret->tid;
2745   }
2746   return ret.hasValue();
2747 }
2748 
2749 bool GDBRemoteCommunicationClient::GetStopReply(
2750     StringExtractorGDBRemote &response) {
2751   if (SendPacketAndWaitForResponse("?", response) == PacketResult::Success)
2752     return response.IsNormalResponse();
2753   return false;
2754 }
2755 
2756 bool GDBRemoteCommunicationClient::GetThreadStopInfo(
2757     lldb::tid_t tid, StringExtractorGDBRemote &response) {
2758   if (m_supports_qThreadStopInfo) {
2759     char packet[256];
2760     int packet_len =
2761         ::snprintf(packet, sizeof(packet), "qThreadStopInfo%" PRIx64, tid);
2762     assert(packet_len < (int)sizeof(packet));
2763     UNUSED_IF_ASSERT_DISABLED(packet_len);
2764     if (SendPacketAndWaitForResponse(packet, response) ==
2765         PacketResult::Success) {
2766       if (response.IsUnsupportedResponse())
2767         m_supports_qThreadStopInfo = false;
2768       else if (response.IsNormalResponse())
2769         return true;
2770       else
2771         return false;
2772     } else {
2773       m_supports_qThreadStopInfo = false;
2774     }
2775   }
2776   return false;
2777 }
2778 
2779 uint8_t GDBRemoteCommunicationClient::SendGDBStoppointTypePacket(
2780     GDBStoppointType type, bool insert, addr_t addr, uint32_t length,
2781     std::chrono::seconds timeout) {
2782   Log *log = GetLog(LLDBLog::Breakpoints);
2783   LLDB_LOGF(log, "GDBRemoteCommunicationClient::%s() %s at addr = 0x%" PRIx64,
2784             __FUNCTION__, insert ? "add" : "remove", addr);
2785 
2786   // Check if the stub is known not to support this breakpoint type
2787   if (!SupportsGDBStoppointPacket(type))
2788     return UINT8_MAX;
2789   // Construct the breakpoint packet
2790   char packet[64];
2791   const int packet_len =
2792       ::snprintf(packet, sizeof(packet), "%c%i,%" PRIx64 ",%x",
2793                  insert ? 'Z' : 'z', type, addr, length);
2794   // Check we haven't overwritten the end of the packet buffer
2795   assert(packet_len + 1 < (int)sizeof(packet));
2796   UNUSED_IF_ASSERT_DISABLED(packet_len);
2797   StringExtractorGDBRemote response;
2798   // Make sure the response is either "OK", "EXX" where XX are two hex digits,
2799   // or "" (unsupported)
2800   response.SetResponseValidatorToOKErrorNotSupported();
2801   // Try to send the breakpoint packet, and check that it was correctly sent
2802   if (SendPacketAndWaitForResponse(packet, response, timeout) ==
2803       PacketResult::Success) {
2804     // Receive and OK packet when the breakpoint successfully placed
2805     if (response.IsOKResponse())
2806       return 0;
2807 
2808     // Status while setting breakpoint, send back specific error
2809     if (response.IsErrorResponse())
2810       return response.GetError();
2811 
2812     // Empty packet informs us that breakpoint is not supported
2813     if (response.IsUnsupportedResponse()) {
2814       // Disable this breakpoint type since it is unsupported
2815       switch (type) {
2816       case eBreakpointSoftware:
2817         m_supports_z0 = false;
2818         break;
2819       case eBreakpointHardware:
2820         m_supports_z1 = false;
2821         break;
2822       case eWatchpointWrite:
2823         m_supports_z2 = false;
2824         break;
2825       case eWatchpointRead:
2826         m_supports_z3 = false;
2827         break;
2828       case eWatchpointReadWrite:
2829         m_supports_z4 = false;
2830         break;
2831       case eStoppointInvalid:
2832         return UINT8_MAX;
2833       }
2834     }
2835   }
2836   // Signal generic failure
2837   return UINT8_MAX;
2838 }
2839 
2840 std::vector<std::pair<lldb::pid_t, lldb::tid_t>>
2841 GDBRemoteCommunicationClient::GetCurrentProcessAndThreadIDs(
2842     bool &sequence_mutex_unavailable) {
2843   std::vector<std::pair<lldb::pid_t, lldb::tid_t>> ids;
2844 
2845   Lock lock(*this);
2846   if (lock) {
2847     sequence_mutex_unavailable = false;
2848     StringExtractorGDBRemote response;
2849 
2850     PacketResult packet_result;
2851     for (packet_result =
2852              SendPacketAndWaitForResponseNoLock("qfThreadInfo", response);
2853          packet_result == PacketResult::Success && response.IsNormalResponse();
2854          packet_result =
2855              SendPacketAndWaitForResponseNoLock("qsThreadInfo", response)) {
2856       char ch = response.GetChar();
2857       if (ch == 'l')
2858         break;
2859       if (ch == 'm') {
2860         do {
2861           auto pid_tid = response.GetPidTid(LLDB_INVALID_PROCESS_ID);
2862           // If we get an invalid response, break out of the loop.
2863           // If there are valid tids, they have been added to ids.
2864           // If there are no valid tids, we'll fall through to the
2865           // bare-iron target handling below.
2866           if (!pid_tid)
2867             break;
2868 
2869           ids.push_back(pid_tid.getValue());
2870           ch = response.GetChar(); // Skip the command separator
2871         } while (ch == ',');       // Make sure we got a comma separator
2872       }
2873     }
2874 
2875     /*
2876      * Connected bare-iron target (like YAMON gdb-stub) may not have support for
2877      * qProcessInfo, qC and qfThreadInfo packets. The reply from '?' packet
2878      * could
2879      * be as simple as 'S05'. There is no packet which can give us pid and/or
2880      * tid.
2881      * Assume pid=tid=1 in such cases.
2882      */
2883     if ((response.IsUnsupportedResponse() || response.IsNormalResponse()) &&
2884         ids.size() == 0 && IsConnected()) {
2885       ids.emplace_back(1, 1);
2886     }
2887   } else {
2888     Log *log(GetLog(GDBRLog::Process | GDBRLog::Packets));
2889     LLDB_LOG(log, "error: failed to get packet sequence mutex, not sending "
2890                   "packet 'qfThreadInfo'");
2891     sequence_mutex_unavailable = true;
2892   }
2893 
2894   return ids;
2895 }
2896 
2897 size_t GDBRemoteCommunicationClient::GetCurrentThreadIDs(
2898     std::vector<lldb::tid_t> &thread_ids, bool &sequence_mutex_unavailable) {
2899   lldb::pid_t pid = GetCurrentProcessID();
2900   thread_ids.clear();
2901 
2902   auto ids = GetCurrentProcessAndThreadIDs(sequence_mutex_unavailable);
2903   if (ids.empty() || sequence_mutex_unavailable)
2904     return 0;
2905 
2906   for (auto id : ids) {
2907     // skip threads that do not belong to the current process
2908     if (id.first != LLDB_INVALID_PROCESS_ID && id.first != pid)
2909       continue;
2910     if (id.second != LLDB_INVALID_THREAD_ID &&
2911         id.second != StringExtractorGDBRemote::AllThreads)
2912       thread_ids.push_back(id.second);
2913   }
2914 
2915   return thread_ids.size();
2916 }
2917 
2918 lldb::addr_t GDBRemoteCommunicationClient::GetShlibInfoAddr() {
2919   StringExtractorGDBRemote response;
2920   if (SendPacketAndWaitForResponse("qShlibInfoAddr", response) !=
2921           PacketResult::Success ||
2922       !response.IsNormalResponse())
2923     return LLDB_INVALID_ADDRESS;
2924   return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
2925 }
2926 
2927 lldb_private::Status GDBRemoteCommunicationClient::RunShellCommand(
2928     llvm::StringRef command,
2929     const FileSpec &
2930         working_dir, // Pass empty FileSpec to use the current working directory
2931     int *status_ptr, // Pass NULL if you don't want the process exit status
2932     int *signo_ptr,  // Pass NULL if you don't want the signal that caused the
2933                      // process to exit
2934     std::string
2935         *command_output, // Pass NULL if you don't want the command output
2936     const Timeout<std::micro> &timeout) {
2937   lldb_private::StreamString stream;
2938   stream.PutCString("qPlatform_shell:");
2939   stream.PutBytesAsRawHex8(command.data(), command.size());
2940   stream.PutChar(',');
2941   uint32_t timeout_sec = UINT32_MAX;
2942   if (timeout) {
2943     // TODO: Use chrono version of std::ceil once c++17 is available.
2944     timeout_sec = std::ceil(std::chrono::duration<double>(*timeout).count());
2945   }
2946   stream.PutHex32(timeout_sec);
2947   if (working_dir) {
2948     std::string path{working_dir.GetPath(false)};
2949     stream.PutChar(',');
2950     stream.PutStringAsRawHex8(path);
2951   }
2952   StringExtractorGDBRemote response;
2953   if (SendPacketAndWaitForResponse(stream.GetString(), response) ==
2954       PacketResult::Success) {
2955     if (response.GetChar() != 'F')
2956       return Status("malformed reply");
2957     if (response.GetChar() != ',')
2958       return Status("malformed reply");
2959     uint32_t exitcode = response.GetHexMaxU32(false, UINT32_MAX);
2960     if (exitcode == UINT32_MAX)
2961       return Status("unable to run remote process");
2962     else if (status_ptr)
2963       *status_ptr = exitcode;
2964     if (response.GetChar() != ',')
2965       return Status("malformed reply");
2966     uint32_t signo = response.GetHexMaxU32(false, UINT32_MAX);
2967     if (signo_ptr)
2968       *signo_ptr = signo;
2969     if (response.GetChar() != ',')
2970       return Status("malformed reply");
2971     std::string output;
2972     response.GetEscapedBinaryData(output);
2973     if (command_output)
2974       command_output->assign(output);
2975     return Status();
2976   }
2977   return Status("unable to send packet");
2978 }
2979 
2980 Status GDBRemoteCommunicationClient::MakeDirectory(const FileSpec &file_spec,
2981                                                    uint32_t file_permissions) {
2982   std::string path{file_spec.GetPath(false)};
2983   lldb_private::StreamString stream;
2984   stream.PutCString("qPlatform_mkdir:");
2985   stream.PutHex32(file_permissions);
2986   stream.PutChar(',');
2987   stream.PutStringAsRawHex8(path);
2988   llvm::StringRef packet = stream.GetString();
2989   StringExtractorGDBRemote response;
2990 
2991   if (SendPacketAndWaitForResponse(packet, response) != PacketResult::Success)
2992     return Status("failed to send '%s' packet", packet.str().c_str());
2993 
2994   if (response.GetChar() != 'F')
2995     return Status("invalid response to '%s' packet", packet.str().c_str());
2996 
2997   return Status(response.GetHexMaxU32(false, UINT32_MAX), eErrorTypePOSIX);
2998 }
2999 
3000 Status
3001 GDBRemoteCommunicationClient::SetFilePermissions(const FileSpec &file_spec,
3002                                                  uint32_t file_permissions) {
3003   std::string path{file_spec.GetPath(false)};
3004   lldb_private::StreamString stream;
3005   stream.PutCString("qPlatform_chmod:");
3006   stream.PutHex32(file_permissions);
3007   stream.PutChar(',');
3008   stream.PutStringAsRawHex8(path);
3009   llvm::StringRef packet = stream.GetString();
3010   StringExtractorGDBRemote response;
3011 
3012   if (SendPacketAndWaitForResponse(packet, response) != PacketResult::Success)
3013     return Status("failed to send '%s' packet", stream.GetData());
3014 
3015   if (response.GetChar() != 'F')
3016     return Status("invalid response to '%s' packet", stream.GetData());
3017 
3018   return Status(response.GetHexMaxU32(false, UINT32_MAX), eErrorTypePOSIX);
3019 }
3020 
3021 static int gdb_errno_to_system(int err) {
3022   switch (err) {
3023 #define HANDLE_ERRNO(name, value)                                              \
3024   case GDB_##name:                                                             \
3025     return name;
3026 #include "Plugins/Process/gdb-remote/GDBRemoteErrno.def"
3027   default:
3028     return -1;
3029   }
3030 }
3031 
3032 static uint64_t ParseHostIOPacketResponse(StringExtractorGDBRemote &response,
3033                                           uint64_t fail_result, Status &error) {
3034   response.SetFilePos(0);
3035   if (response.GetChar() != 'F')
3036     return fail_result;
3037   int32_t result = response.GetS32(-2, 16);
3038   if (result == -2)
3039     return fail_result;
3040   if (response.GetChar() == ',') {
3041     int result_errno = gdb_errno_to_system(response.GetS32(-1, 16));
3042     if (result_errno != -1)
3043       error.SetError(result_errno, eErrorTypePOSIX);
3044     else
3045       error.SetError(-1, eErrorTypeGeneric);
3046   } else
3047     error.Clear();
3048   return result;
3049 }
3050 lldb::user_id_t
3051 GDBRemoteCommunicationClient::OpenFile(const lldb_private::FileSpec &file_spec,
3052                                        File::OpenOptions flags, mode_t mode,
3053                                        Status &error) {
3054   std::string path(file_spec.GetPath(false));
3055   lldb_private::StreamString stream;
3056   stream.PutCString("vFile:open:");
3057   if (path.empty())
3058     return UINT64_MAX;
3059   stream.PutStringAsRawHex8(path);
3060   stream.PutChar(',');
3061   stream.PutHex32(flags);
3062   stream.PutChar(',');
3063   stream.PutHex32(mode);
3064   StringExtractorGDBRemote response;
3065   if (SendPacketAndWaitForResponse(stream.GetString(), response) ==
3066       PacketResult::Success) {
3067     return ParseHostIOPacketResponse(response, UINT64_MAX, error);
3068   }
3069   return UINT64_MAX;
3070 }
3071 
3072 bool GDBRemoteCommunicationClient::CloseFile(lldb::user_id_t fd,
3073                                              Status &error) {
3074   lldb_private::StreamString stream;
3075   stream.Printf("vFile:close:%x", (int)fd);
3076   StringExtractorGDBRemote response;
3077   if (SendPacketAndWaitForResponse(stream.GetString(), response) ==
3078       PacketResult::Success) {
3079     return ParseHostIOPacketResponse(response, -1, error) == 0;
3080   }
3081   return false;
3082 }
3083 
3084 llvm::Optional<GDBRemoteFStatData>
3085 GDBRemoteCommunicationClient::FStat(lldb::user_id_t fd) {
3086   lldb_private::StreamString stream;
3087   stream.Printf("vFile:fstat:%" PRIx64, fd);
3088   StringExtractorGDBRemote response;
3089   if (SendPacketAndWaitForResponse(stream.GetString(), response) ==
3090       PacketResult::Success) {
3091     if (response.GetChar() != 'F')
3092       return llvm::None;
3093     int64_t size = response.GetS64(-1, 16);
3094     if (size > 0 && response.GetChar() == ';') {
3095       std::string buffer;
3096       if (response.GetEscapedBinaryData(buffer)) {
3097         GDBRemoteFStatData out;
3098         if (buffer.size() != sizeof(out))
3099           return llvm::None;
3100         memcpy(&out, buffer.data(), sizeof(out));
3101         return out;
3102       }
3103     }
3104   }
3105   return llvm::None;
3106 }
3107 
3108 llvm::Optional<GDBRemoteFStatData>
3109 GDBRemoteCommunicationClient::Stat(const lldb_private::FileSpec &file_spec) {
3110   Status error;
3111   lldb::user_id_t fd = OpenFile(file_spec, File::eOpenOptionReadOnly, 0, error);
3112   if (fd == UINT64_MAX)
3113     return llvm::None;
3114   llvm::Optional<GDBRemoteFStatData> st = FStat(fd);
3115   CloseFile(fd, error);
3116   return st;
3117 }
3118 
3119 // Extension of host I/O packets to get the file size.
3120 lldb::user_id_t GDBRemoteCommunicationClient::GetFileSize(
3121     const lldb_private::FileSpec &file_spec) {
3122   if (m_supports_vFileSize) {
3123     std::string path(file_spec.GetPath(false));
3124     lldb_private::StreamString stream;
3125     stream.PutCString("vFile:size:");
3126     stream.PutStringAsRawHex8(path);
3127     StringExtractorGDBRemote response;
3128     if (SendPacketAndWaitForResponse(stream.GetString(), response) !=
3129         PacketResult::Success)
3130       return UINT64_MAX;
3131 
3132     if (!response.IsUnsupportedResponse()) {
3133       if (response.GetChar() != 'F')
3134         return UINT64_MAX;
3135       uint32_t retcode = response.GetHexMaxU64(false, UINT64_MAX);
3136       return retcode;
3137     }
3138     m_supports_vFileSize = false;
3139   }
3140 
3141   // Fallback to fstat.
3142   llvm::Optional<GDBRemoteFStatData> st = Stat(file_spec);
3143   return st ? st->gdb_st_size : UINT64_MAX;
3144 }
3145 
3146 void GDBRemoteCommunicationClient::AutoCompleteDiskFileOrDirectory(
3147     CompletionRequest &request, bool only_dir) {
3148   lldb_private::StreamString stream;
3149   stream.PutCString("qPathComplete:");
3150   stream.PutHex32(only_dir ? 1 : 0);
3151   stream.PutChar(',');
3152   stream.PutStringAsRawHex8(request.GetCursorArgumentPrefix());
3153   StringExtractorGDBRemote response;
3154   if (SendPacketAndWaitForResponse(stream.GetString(), response) ==
3155       PacketResult::Success) {
3156     StreamString strm;
3157     char ch = response.GetChar();
3158     if (ch != 'M')
3159       return;
3160     while (response.Peek()) {
3161       strm.Clear();
3162       while ((ch = response.GetHexU8(0, false)) != '\0')
3163         strm.PutChar(ch);
3164       request.AddCompletion(strm.GetString());
3165       if (response.GetChar() != ',')
3166         break;
3167     }
3168   }
3169 }
3170 
3171 Status
3172 GDBRemoteCommunicationClient::GetFilePermissions(const FileSpec &file_spec,
3173                                                  uint32_t &file_permissions) {
3174   if (m_supports_vFileMode) {
3175     std::string path{file_spec.GetPath(false)};
3176     Status error;
3177     lldb_private::StreamString stream;
3178     stream.PutCString("vFile:mode:");
3179     stream.PutStringAsRawHex8(path);
3180     StringExtractorGDBRemote response;
3181     if (SendPacketAndWaitForResponse(stream.GetString(), response) !=
3182         PacketResult::Success) {
3183       error.SetErrorStringWithFormat("failed to send '%s' packet",
3184                                      stream.GetData());
3185       return error;
3186     }
3187     if (!response.IsUnsupportedResponse()) {
3188       if (response.GetChar() != 'F') {
3189         error.SetErrorStringWithFormat("invalid response to '%s' packet",
3190                                        stream.GetData());
3191       } else {
3192         const uint32_t mode = response.GetS32(-1, 16);
3193         if (static_cast<int32_t>(mode) == -1) {
3194           if (response.GetChar() == ',') {
3195             int response_errno = gdb_errno_to_system(response.GetS32(-1, 16));
3196             if (response_errno > 0)
3197               error.SetError(response_errno, lldb::eErrorTypePOSIX);
3198             else
3199               error.SetErrorToGenericError();
3200           } else
3201             error.SetErrorToGenericError();
3202         } else {
3203           file_permissions = mode & (S_IRWXU | S_IRWXG | S_IRWXO);
3204         }
3205       }
3206       return error;
3207     } else { // response.IsUnsupportedResponse()
3208       m_supports_vFileMode = false;
3209     }
3210   }
3211 
3212   // Fallback to fstat.
3213   if (llvm::Optional<GDBRemoteFStatData> st = Stat(file_spec)) {
3214     file_permissions = st->gdb_st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
3215     return Status();
3216   }
3217   return Status("fstat failed");
3218 }
3219 
3220 uint64_t GDBRemoteCommunicationClient::ReadFile(lldb::user_id_t fd,
3221                                                 uint64_t offset, void *dst,
3222                                                 uint64_t dst_len,
3223                                                 Status &error) {
3224   lldb_private::StreamString stream;
3225   stream.Printf("vFile:pread:%x,%" PRIx64 ",%" PRIx64, (int)fd, dst_len,
3226                 offset);
3227   StringExtractorGDBRemote response;
3228   if (SendPacketAndWaitForResponse(stream.GetString(), response) ==
3229       PacketResult::Success) {
3230     if (response.GetChar() != 'F')
3231       return 0;
3232     int64_t retcode = response.GetS64(-1, 16);
3233     if (retcode == -1) {
3234       error.SetErrorToGenericError();
3235       if (response.GetChar() == ',') {
3236         int response_errno = gdb_errno_to_system(response.GetS32(-1, 16));
3237         if (response_errno > 0)
3238           error.SetError(response_errno, lldb::eErrorTypePOSIX);
3239       }
3240       return -1;
3241     }
3242     const char next = (response.Peek() ? *response.Peek() : 0);
3243     if (next == ',')
3244       return 0;
3245     if (next == ';') {
3246       response.GetChar(); // skip the semicolon
3247       std::string buffer;
3248       if (response.GetEscapedBinaryData(buffer)) {
3249         const uint64_t data_to_write =
3250             std::min<uint64_t>(dst_len, buffer.size());
3251         if (data_to_write > 0)
3252           memcpy(dst, &buffer[0], data_to_write);
3253         return data_to_write;
3254       }
3255     }
3256   }
3257   return 0;
3258 }
3259 
3260 uint64_t GDBRemoteCommunicationClient::WriteFile(lldb::user_id_t fd,
3261                                                  uint64_t offset,
3262                                                  const void *src,
3263                                                  uint64_t src_len,
3264                                                  Status &error) {
3265   lldb_private::StreamGDBRemote stream;
3266   stream.Printf("vFile:pwrite:%x,%" PRIx64 ",", (int)fd, offset);
3267   stream.PutEscapedBytes(src, src_len);
3268   StringExtractorGDBRemote response;
3269   if (SendPacketAndWaitForResponse(stream.GetString(), response) ==
3270       PacketResult::Success) {
3271     if (response.GetChar() != 'F') {
3272       error.SetErrorStringWithFormat("write file failed");
3273       return 0;
3274     }
3275     int64_t bytes_written = response.GetS64(-1, 16);
3276     if (bytes_written == -1) {
3277       error.SetErrorToGenericError();
3278       if (response.GetChar() == ',') {
3279         int response_errno = gdb_errno_to_system(response.GetS32(-1, 16));
3280         if (response_errno > 0)
3281           error.SetError(response_errno, lldb::eErrorTypePOSIX);
3282       }
3283       return -1;
3284     }
3285     return bytes_written;
3286   } else {
3287     error.SetErrorString("failed to send vFile:pwrite packet");
3288   }
3289   return 0;
3290 }
3291 
3292 Status GDBRemoteCommunicationClient::CreateSymlink(const FileSpec &src,
3293                                                    const FileSpec &dst) {
3294   std::string src_path{src.GetPath(false)}, dst_path{dst.GetPath(false)};
3295   Status error;
3296   lldb_private::StreamGDBRemote stream;
3297   stream.PutCString("vFile:symlink:");
3298   // the unix symlink() command reverses its parameters where the dst if first,
3299   // so we follow suit here
3300   stream.PutStringAsRawHex8(dst_path);
3301   stream.PutChar(',');
3302   stream.PutStringAsRawHex8(src_path);
3303   StringExtractorGDBRemote response;
3304   if (SendPacketAndWaitForResponse(stream.GetString(), response) ==
3305       PacketResult::Success) {
3306     if (response.GetChar() == 'F') {
3307       uint32_t result = response.GetHexMaxU32(false, UINT32_MAX);
3308       if (result != 0) {
3309         error.SetErrorToGenericError();
3310         if (response.GetChar() == ',') {
3311           int response_errno = gdb_errno_to_system(response.GetS32(-1, 16));
3312           if (response_errno > 0)
3313             error.SetError(response_errno, lldb::eErrorTypePOSIX);
3314         }
3315       }
3316     } else {
3317       // Should have returned with 'F<result>[,<errno>]'
3318       error.SetErrorStringWithFormat("symlink failed");
3319     }
3320   } else {
3321     error.SetErrorString("failed to send vFile:symlink packet");
3322   }
3323   return error;
3324 }
3325 
3326 Status GDBRemoteCommunicationClient::Unlink(const FileSpec &file_spec) {
3327   std::string path{file_spec.GetPath(false)};
3328   Status error;
3329   lldb_private::StreamGDBRemote stream;
3330   stream.PutCString("vFile:unlink:");
3331   // the unix symlink() command reverses its parameters where the dst if first,
3332   // so we follow suit here
3333   stream.PutStringAsRawHex8(path);
3334   StringExtractorGDBRemote response;
3335   if (SendPacketAndWaitForResponse(stream.GetString(), response) ==
3336       PacketResult::Success) {
3337     if (response.GetChar() == 'F') {
3338       uint32_t result = response.GetHexMaxU32(false, UINT32_MAX);
3339       if (result != 0) {
3340         error.SetErrorToGenericError();
3341         if (response.GetChar() == ',') {
3342           int response_errno = gdb_errno_to_system(response.GetS32(-1, 16));
3343           if (response_errno > 0)
3344             error.SetError(response_errno, lldb::eErrorTypePOSIX);
3345         }
3346       }
3347     } else {
3348       // Should have returned with 'F<result>[,<errno>]'
3349       error.SetErrorStringWithFormat("unlink failed");
3350     }
3351   } else {
3352     error.SetErrorString("failed to send vFile:unlink packet");
3353   }
3354   return error;
3355 }
3356 
3357 // Extension of host I/O packets to get whether a file exists.
3358 bool GDBRemoteCommunicationClient::GetFileExists(
3359     const lldb_private::FileSpec &file_spec) {
3360   if (m_supports_vFileExists) {
3361     std::string path(file_spec.GetPath(false));
3362     lldb_private::StreamString stream;
3363     stream.PutCString("vFile:exists:");
3364     stream.PutStringAsRawHex8(path);
3365     StringExtractorGDBRemote response;
3366     if (SendPacketAndWaitForResponse(stream.GetString(), response) !=
3367         PacketResult::Success)
3368       return false;
3369     if (!response.IsUnsupportedResponse()) {
3370       if (response.GetChar() != 'F')
3371         return false;
3372       if (response.GetChar() != ',')
3373         return false;
3374       bool retcode = (response.GetChar() != '0');
3375       return retcode;
3376     } else
3377       m_supports_vFileExists = false;
3378   }
3379 
3380   // Fallback to open.
3381   Status error;
3382   lldb::user_id_t fd = OpenFile(file_spec, File::eOpenOptionReadOnly, 0, error);
3383   if (fd == UINT64_MAX)
3384     return false;
3385   CloseFile(fd, error);
3386   return true;
3387 }
3388 
3389 bool GDBRemoteCommunicationClient::CalculateMD5(
3390     const lldb_private::FileSpec &file_spec, uint64_t &high, uint64_t &low) {
3391   std::string path(file_spec.GetPath(false));
3392   lldb_private::StreamString stream;
3393   stream.PutCString("vFile:MD5:");
3394   stream.PutStringAsRawHex8(path);
3395   StringExtractorGDBRemote response;
3396   if (SendPacketAndWaitForResponse(stream.GetString(), response) ==
3397       PacketResult::Success) {
3398     if (response.GetChar() != 'F')
3399       return false;
3400     if (response.GetChar() != ',')
3401       return false;
3402     if (response.Peek() && *response.Peek() == 'x')
3403       return false;
3404     low = response.GetHexMaxU64(false, UINT64_MAX);
3405     high = response.GetHexMaxU64(false, UINT64_MAX);
3406     return true;
3407   }
3408   return false;
3409 }
3410 
3411 bool GDBRemoteCommunicationClient::AvoidGPackets(ProcessGDBRemote *process) {
3412   // Some targets have issues with g/G packets and we need to avoid using them
3413   if (m_avoid_g_packets == eLazyBoolCalculate) {
3414     if (process) {
3415       m_avoid_g_packets = eLazyBoolNo;
3416       const ArchSpec &arch = process->GetTarget().GetArchitecture();
3417       if (arch.IsValid() &&
3418           arch.GetTriple().getVendor() == llvm::Triple::Apple &&
3419           arch.GetTriple().getOS() == llvm::Triple::IOS &&
3420           (arch.GetTriple().getArch() == llvm::Triple::aarch64 ||
3421            arch.GetTriple().getArch() == llvm::Triple::aarch64_32)) {
3422         m_avoid_g_packets = eLazyBoolYes;
3423         uint32_t gdb_server_version = GetGDBServerProgramVersion();
3424         if (gdb_server_version != 0) {
3425           const char *gdb_server_name = GetGDBServerProgramName();
3426           if (gdb_server_name && strcmp(gdb_server_name, "debugserver") == 0) {
3427             if (gdb_server_version >= 310)
3428               m_avoid_g_packets = eLazyBoolNo;
3429           }
3430         }
3431       }
3432     }
3433   }
3434   return m_avoid_g_packets == eLazyBoolYes;
3435 }
3436 
3437 DataBufferSP GDBRemoteCommunicationClient::ReadRegister(lldb::tid_t tid,
3438                                                         uint32_t reg) {
3439   StreamString payload;
3440   payload.Printf("p%x", reg);
3441   StringExtractorGDBRemote response;
3442   if (SendThreadSpecificPacketAndWaitForResponse(
3443           tid, std::move(payload), response) != PacketResult::Success ||
3444       !response.IsNormalResponse())
3445     return nullptr;
3446 
3447   DataBufferSP buffer_sp(
3448       new DataBufferHeap(response.GetStringRef().size() / 2, 0));
3449   response.GetHexBytes(buffer_sp->GetData(), '\xcc');
3450   return buffer_sp;
3451 }
3452 
3453 DataBufferSP GDBRemoteCommunicationClient::ReadAllRegisters(lldb::tid_t tid) {
3454   StreamString payload;
3455   payload.PutChar('g');
3456   StringExtractorGDBRemote response;
3457   if (SendThreadSpecificPacketAndWaitForResponse(
3458           tid, std::move(payload), response) != PacketResult::Success ||
3459       !response.IsNormalResponse())
3460     return nullptr;
3461 
3462   DataBufferSP buffer_sp(
3463       new DataBufferHeap(response.GetStringRef().size() / 2, 0));
3464   response.GetHexBytes(buffer_sp->GetData(), '\xcc');
3465   return buffer_sp;
3466 }
3467 
3468 bool GDBRemoteCommunicationClient::WriteRegister(lldb::tid_t tid,
3469                                                  uint32_t reg_num,
3470                                                  llvm::ArrayRef<uint8_t> data) {
3471   StreamString payload;
3472   payload.Printf("P%x=", reg_num);
3473   payload.PutBytesAsRawHex8(data.data(), data.size(),
3474                             endian::InlHostByteOrder(),
3475                             endian::InlHostByteOrder());
3476   StringExtractorGDBRemote response;
3477   return SendThreadSpecificPacketAndWaitForResponse(
3478              tid, std::move(payload), response) == PacketResult::Success &&
3479          response.IsOKResponse();
3480 }
3481 
3482 bool GDBRemoteCommunicationClient::WriteAllRegisters(
3483     lldb::tid_t tid, llvm::ArrayRef<uint8_t> data) {
3484   StreamString payload;
3485   payload.PutChar('G');
3486   payload.PutBytesAsRawHex8(data.data(), data.size(),
3487                             endian::InlHostByteOrder(),
3488                             endian::InlHostByteOrder());
3489   StringExtractorGDBRemote response;
3490   return SendThreadSpecificPacketAndWaitForResponse(
3491              tid, std::move(payload), response) == PacketResult::Success &&
3492          response.IsOKResponse();
3493 }
3494 
3495 bool GDBRemoteCommunicationClient::SaveRegisterState(lldb::tid_t tid,
3496                                                      uint32_t &save_id) {
3497   save_id = 0; // Set to invalid save ID
3498   if (m_supports_QSaveRegisterState == eLazyBoolNo)
3499     return false;
3500 
3501   m_supports_QSaveRegisterState = eLazyBoolYes;
3502   StreamString payload;
3503   payload.PutCString("QSaveRegisterState");
3504   StringExtractorGDBRemote response;
3505   if (SendThreadSpecificPacketAndWaitForResponse(
3506           tid, std::move(payload), response) != PacketResult::Success)
3507     return false;
3508 
3509   if (response.IsUnsupportedResponse())
3510     m_supports_QSaveRegisterState = eLazyBoolNo;
3511 
3512   const uint32_t response_save_id = response.GetU32(0);
3513   if (response_save_id == 0)
3514     return false;
3515 
3516   save_id = response_save_id;
3517   return true;
3518 }
3519 
3520 bool GDBRemoteCommunicationClient::RestoreRegisterState(lldb::tid_t tid,
3521                                                         uint32_t save_id) {
3522   // We use the "m_supports_QSaveRegisterState" variable here because the
3523   // QSaveRegisterState and QRestoreRegisterState packets must both be
3524   // supported in order to be useful
3525   if (m_supports_QSaveRegisterState == eLazyBoolNo)
3526     return false;
3527 
3528   StreamString payload;
3529   payload.Printf("QRestoreRegisterState:%u", save_id);
3530   StringExtractorGDBRemote response;
3531   if (SendThreadSpecificPacketAndWaitForResponse(
3532           tid, std::move(payload), response) != PacketResult::Success)
3533     return false;
3534 
3535   if (response.IsOKResponse())
3536     return true;
3537 
3538   if (response.IsUnsupportedResponse())
3539     m_supports_QSaveRegisterState = eLazyBoolNo;
3540   return false;
3541 }
3542 
3543 bool GDBRemoteCommunicationClient::SyncThreadState(lldb::tid_t tid) {
3544   if (!GetSyncThreadStateSupported())
3545     return false;
3546 
3547   StreamString packet;
3548   StringExtractorGDBRemote response;
3549   packet.Printf("QSyncThreadState:%4.4" PRIx64 ";", tid);
3550   return SendPacketAndWaitForResponse(packet.GetString(), response) ==
3551              GDBRemoteCommunication::PacketResult::Success &&
3552          response.IsOKResponse();
3553 }
3554 
3555 llvm::Expected<TraceSupportedResponse>
3556 GDBRemoteCommunicationClient::SendTraceSupported(std::chrono::seconds timeout) {
3557   Log *log = GetLog(GDBRLog::Process);
3558 
3559   StreamGDBRemote escaped_packet;
3560   escaped_packet.PutCString("jLLDBTraceSupported");
3561 
3562   StringExtractorGDBRemote response;
3563   if (SendPacketAndWaitForResponse(escaped_packet.GetString(), response,
3564                                    timeout) ==
3565       GDBRemoteCommunication::PacketResult::Success) {
3566     if (response.IsErrorResponse())
3567       return response.GetStatus().ToError();
3568     if (response.IsUnsupportedResponse())
3569       return llvm::createStringError(llvm::inconvertibleErrorCode(),
3570                                      "jLLDBTraceSupported is unsupported");
3571 
3572     return llvm::json::parse<TraceSupportedResponse>(response.Peek(),
3573                                                      "TraceSupportedResponse");
3574   }
3575   LLDB_LOG(log, "failed to send packet: jLLDBTraceSupported");
3576   return llvm::createStringError(llvm::inconvertibleErrorCode(),
3577                                  "failed to send packet: jLLDBTraceSupported");
3578 }
3579 
3580 llvm::Error
3581 GDBRemoteCommunicationClient::SendTraceStop(const TraceStopRequest &request,
3582                                             std::chrono::seconds timeout) {
3583   Log *log = GetLog(GDBRLog::Process);
3584 
3585   StreamGDBRemote escaped_packet;
3586   escaped_packet.PutCString("jLLDBTraceStop:");
3587 
3588   std::string json_string;
3589   llvm::raw_string_ostream os(json_string);
3590   os << toJSON(request);
3591   os.flush();
3592 
3593   escaped_packet.PutEscapedBytes(json_string.c_str(), json_string.size());
3594 
3595   StringExtractorGDBRemote response;
3596   if (SendPacketAndWaitForResponse(escaped_packet.GetString(), response,
3597                                    timeout) ==
3598       GDBRemoteCommunication::PacketResult::Success) {
3599     if (response.IsErrorResponse())
3600       return response.GetStatus().ToError();
3601     if (response.IsUnsupportedResponse())
3602       return llvm::createStringError(llvm::inconvertibleErrorCode(),
3603                                      "jLLDBTraceStop is unsupported");
3604     if (response.IsOKResponse())
3605       return llvm::Error::success();
3606     return llvm::createStringError(llvm::inconvertibleErrorCode(),
3607                                    "Invalid jLLDBTraceStart response");
3608   }
3609   LLDB_LOG(log, "failed to send packet: jLLDBTraceStop");
3610   return llvm::createStringError(llvm::inconvertibleErrorCode(),
3611                                  "failed to send packet: jLLDBTraceStop '%s'",
3612                                  escaped_packet.GetData());
3613 }
3614 
3615 llvm::Error
3616 GDBRemoteCommunicationClient::SendTraceStart(const llvm::json::Value &params,
3617                                              std::chrono::seconds timeout) {
3618   Log *log = GetLog(GDBRLog::Process);
3619 
3620   StreamGDBRemote escaped_packet;
3621   escaped_packet.PutCString("jLLDBTraceStart:");
3622 
3623   std::string json_string;
3624   llvm::raw_string_ostream os(json_string);
3625   os << params;
3626   os.flush();
3627 
3628   escaped_packet.PutEscapedBytes(json_string.c_str(), json_string.size());
3629 
3630   StringExtractorGDBRemote response;
3631   if (SendPacketAndWaitForResponse(escaped_packet.GetString(), response,
3632                                    timeout) ==
3633       GDBRemoteCommunication::PacketResult::Success) {
3634     if (response.IsErrorResponse())
3635       return response.GetStatus().ToError();
3636     if (response.IsUnsupportedResponse())
3637       return llvm::createStringError(llvm::inconvertibleErrorCode(),
3638                                      "jLLDBTraceStart is unsupported");
3639     if (response.IsOKResponse())
3640       return llvm::Error::success();
3641     return llvm::createStringError(llvm::inconvertibleErrorCode(),
3642                                    "Invalid jLLDBTraceStart response");
3643   }
3644   LLDB_LOG(log, "failed to send packet: jLLDBTraceStart");
3645   return llvm::createStringError(llvm::inconvertibleErrorCode(),
3646                                  "failed to send packet: jLLDBTraceStart '%s'",
3647                                  escaped_packet.GetData());
3648 }
3649 
3650 llvm::Expected<std::string>
3651 GDBRemoteCommunicationClient::SendTraceGetState(llvm::StringRef type,
3652                                                 std::chrono::seconds timeout) {
3653   Log *log = GetLog(GDBRLog::Process);
3654 
3655   StreamGDBRemote escaped_packet;
3656   escaped_packet.PutCString("jLLDBTraceGetState:");
3657 
3658   std::string json_string;
3659   llvm::raw_string_ostream os(json_string);
3660   os << toJSON(TraceGetStateRequest{type.str()});
3661   os.flush();
3662 
3663   escaped_packet.PutEscapedBytes(json_string.c_str(), json_string.size());
3664 
3665   StringExtractorGDBRemote response;
3666   if (SendPacketAndWaitForResponse(escaped_packet.GetString(), response,
3667                                    timeout) ==
3668       GDBRemoteCommunication::PacketResult::Success) {
3669     if (response.IsErrorResponse())
3670       return response.GetStatus().ToError();
3671     if (response.IsUnsupportedResponse())
3672       return llvm::createStringError(llvm::inconvertibleErrorCode(),
3673                                      "jLLDBTraceGetState is unsupported");
3674     return std::string(response.Peek());
3675   }
3676 
3677   LLDB_LOG(log, "failed to send packet: jLLDBTraceGetState");
3678   return llvm::createStringError(
3679       llvm::inconvertibleErrorCode(),
3680       "failed to send packet: jLLDBTraceGetState '%s'",
3681       escaped_packet.GetData());
3682 }
3683 
3684 llvm::Expected<std::vector<uint8_t>>
3685 GDBRemoteCommunicationClient::SendTraceGetBinaryData(
3686     const TraceGetBinaryDataRequest &request, std::chrono::seconds timeout) {
3687   Log *log = GetLog(GDBRLog::Process);
3688 
3689   StreamGDBRemote escaped_packet;
3690   escaped_packet.PutCString("jLLDBTraceGetBinaryData:");
3691 
3692   std::string json_string;
3693   llvm::raw_string_ostream os(json_string);
3694   os << toJSON(request);
3695   os.flush();
3696 
3697   escaped_packet.PutEscapedBytes(json_string.c_str(), json_string.size());
3698 
3699   StringExtractorGDBRemote response;
3700   if (SendPacketAndWaitForResponse(escaped_packet.GetString(), response,
3701                                    timeout) ==
3702       GDBRemoteCommunication::PacketResult::Success) {
3703     if (response.IsErrorResponse())
3704       return response.GetStatus().ToError();
3705     if (response.IsUnsupportedResponse())
3706       return llvm::createStringError(llvm::inconvertibleErrorCode(),
3707                                      "jLLDBTraceGetBinaryData is unsupported");
3708     std::string data;
3709     response.GetEscapedBinaryData(data);
3710     return std::vector<uint8_t>(data.begin(), data.end());
3711   }
3712   LLDB_LOG(log, "failed to send packet: jLLDBTraceGetBinaryData");
3713   return llvm::createStringError(
3714       llvm::inconvertibleErrorCode(),
3715       "failed to send packet: jLLDBTraceGetBinaryData '%s'",
3716       escaped_packet.GetData());
3717 }
3718 
3719 llvm::Optional<QOffsets> GDBRemoteCommunicationClient::GetQOffsets() {
3720   StringExtractorGDBRemote response;
3721   if (SendPacketAndWaitForResponse("qOffsets", response) !=
3722       PacketResult::Success)
3723     return llvm::None;
3724   if (!response.IsNormalResponse())
3725     return llvm::None;
3726 
3727   QOffsets result;
3728   llvm::StringRef ref = response.GetStringRef();
3729   const auto &GetOffset = [&] {
3730     addr_t offset;
3731     if (ref.consumeInteger(16, offset))
3732       return false;
3733     result.offsets.push_back(offset);
3734     return true;
3735   };
3736 
3737   if (ref.consume_front("Text=")) {
3738     result.segments = false;
3739     if (!GetOffset())
3740       return llvm::None;
3741     if (!ref.consume_front(";Data=") || !GetOffset())
3742       return llvm::None;
3743     if (ref.empty())
3744       return result;
3745     if (ref.consume_front(";Bss=") && GetOffset() && ref.empty())
3746       return result;
3747   } else if (ref.consume_front("TextSeg=")) {
3748     result.segments = true;
3749     if (!GetOffset())
3750       return llvm::None;
3751     if (ref.empty())
3752       return result;
3753     if (ref.consume_front(";DataSeg=") && GetOffset() && ref.empty())
3754       return result;
3755   }
3756   return llvm::None;
3757 }
3758 
3759 bool GDBRemoteCommunicationClient::GetModuleInfo(
3760     const FileSpec &module_file_spec, const lldb_private::ArchSpec &arch_spec,
3761     ModuleSpec &module_spec) {
3762   if (!m_supports_qModuleInfo)
3763     return false;
3764 
3765   std::string module_path = module_file_spec.GetPath(false);
3766   if (module_path.empty())
3767     return false;
3768 
3769   StreamString packet;
3770   packet.PutCString("qModuleInfo:");
3771   packet.PutStringAsRawHex8(module_path);
3772   packet.PutCString(";");
3773   const auto &triple = arch_spec.GetTriple().getTriple();
3774   packet.PutStringAsRawHex8(triple);
3775 
3776   StringExtractorGDBRemote response;
3777   if (SendPacketAndWaitForResponse(packet.GetString(), response) !=
3778       PacketResult::Success)
3779     return false;
3780 
3781   if (response.IsErrorResponse())
3782     return false;
3783 
3784   if (response.IsUnsupportedResponse()) {
3785     m_supports_qModuleInfo = false;
3786     return false;
3787   }
3788 
3789   llvm::StringRef name;
3790   llvm::StringRef value;
3791 
3792   module_spec.Clear();
3793   module_spec.GetFileSpec() = module_file_spec;
3794 
3795   while (response.GetNameColonValue(name, value)) {
3796     if (name == "uuid" || name == "md5") {
3797       StringExtractor extractor(value);
3798       std::string uuid;
3799       extractor.GetHexByteString(uuid);
3800       module_spec.GetUUID().SetFromStringRef(uuid);
3801     } else if (name == "triple") {
3802       StringExtractor extractor(value);
3803       std::string triple;
3804       extractor.GetHexByteString(triple);
3805       module_spec.GetArchitecture().SetTriple(triple.c_str());
3806     } else if (name == "file_offset") {
3807       uint64_t ival = 0;
3808       if (!value.getAsInteger(16, ival))
3809         module_spec.SetObjectOffset(ival);
3810     } else if (name == "file_size") {
3811       uint64_t ival = 0;
3812       if (!value.getAsInteger(16, ival))
3813         module_spec.SetObjectSize(ival);
3814     } else if (name == "file_path") {
3815       StringExtractor extractor(value);
3816       std::string path;
3817       extractor.GetHexByteString(path);
3818       module_spec.GetFileSpec() = FileSpec(path, arch_spec.GetTriple());
3819     }
3820   }
3821 
3822   return true;
3823 }
3824 
3825 static llvm::Optional<ModuleSpec>
3826 ParseModuleSpec(StructuredData::Dictionary *dict) {
3827   ModuleSpec result;
3828   if (!dict)
3829     return llvm::None;
3830 
3831   llvm::StringRef string;
3832   uint64_t integer;
3833 
3834   if (!dict->GetValueForKeyAsString("uuid", string))
3835     return llvm::None;
3836   if (!result.GetUUID().SetFromStringRef(string))
3837     return llvm::None;
3838 
3839   if (!dict->GetValueForKeyAsInteger("file_offset", integer))
3840     return llvm::None;
3841   result.SetObjectOffset(integer);
3842 
3843   if (!dict->GetValueForKeyAsInteger("file_size", integer))
3844     return llvm::None;
3845   result.SetObjectSize(integer);
3846 
3847   if (!dict->GetValueForKeyAsString("triple", string))
3848     return llvm::None;
3849   result.GetArchitecture().SetTriple(string);
3850 
3851   if (!dict->GetValueForKeyAsString("file_path", string))
3852     return llvm::None;
3853   result.GetFileSpec() = FileSpec(string, result.GetArchitecture().GetTriple());
3854 
3855   return result;
3856 }
3857 
3858 llvm::Optional<std::vector<ModuleSpec>>
3859 GDBRemoteCommunicationClient::GetModulesInfo(
3860     llvm::ArrayRef<FileSpec> module_file_specs, const llvm::Triple &triple) {
3861   namespace json = llvm::json;
3862 
3863   if (!m_supports_jModulesInfo)
3864     return llvm::None;
3865 
3866   json::Array module_array;
3867   for (const FileSpec &module_file_spec : module_file_specs) {
3868     module_array.push_back(
3869         json::Object{{"file", module_file_spec.GetPath(false)},
3870                      {"triple", triple.getTriple()}});
3871   }
3872   StreamString unescaped_payload;
3873   unescaped_payload.PutCString("jModulesInfo:");
3874   unescaped_payload.AsRawOstream() << std::move(module_array);
3875 
3876   StreamGDBRemote payload;
3877   payload.PutEscapedBytes(unescaped_payload.GetString().data(),
3878                           unescaped_payload.GetSize());
3879 
3880   // Increase the timeout for jModulesInfo since this packet can take longer.
3881   ScopedTimeout timeout(*this, std::chrono::seconds(10));
3882 
3883   StringExtractorGDBRemote response;
3884   if (SendPacketAndWaitForResponse(payload.GetString(), response) !=
3885           PacketResult::Success ||
3886       response.IsErrorResponse())
3887     return llvm::None;
3888 
3889   if (response.IsUnsupportedResponse()) {
3890     m_supports_jModulesInfo = false;
3891     return llvm::None;
3892   }
3893 
3894   StructuredData::ObjectSP response_object_sp =
3895       StructuredData::ParseJSON(std::string(response.GetStringRef()));
3896   if (!response_object_sp)
3897     return llvm::None;
3898 
3899   StructuredData::Array *response_array = response_object_sp->GetAsArray();
3900   if (!response_array)
3901     return llvm::None;
3902 
3903   std::vector<ModuleSpec> result;
3904   for (size_t i = 0; i < response_array->GetSize(); ++i) {
3905     if (llvm::Optional<ModuleSpec> module_spec = ParseModuleSpec(
3906             response_array->GetItemAtIndex(i)->GetAsDictionary()))
3907       result.push_back(*module_spec);
3908   }
3909 
3910   return result;
3911 }
3912 
3913 // query the target remote for extended information using the qXfer packet
3914 //
3915 // example: object='features', annex='target.xml'
3916 // return: <xml output> or error
3917 llvm::Expected<std::string>
3918 GDBRemoteCommunicationClient::ReadExtFeature(llvm::StringRef object,
3919                                              llvm::StringRef annex) {
3920 
3921   std::string output;
3922   llvm::raw_string_ostream output_stream(output);
3923   StringExtractorGDBRemote chunk;
3924 
3925   uint64_t size = GetRemoteMaxPacketSize();
3926   if (size == 0)
3927     size = 0x1000;
3928   size = size - 1; // Leave space for the 'm' or 'l' character in the response
3929   int offset = 0;
3930   bool active = true;
3931 
3932   // loop until all data has been read
3933   while (active) {
3934 
3935     // send query extended feature packet
3936     std::string packet =
3937         ("qXfer:" + object + ":read:" + annex + ":" +
3938          llvm::Twine::utohexstr(offset) + "," + llvm::Twine::utohexstr(size))
3939             .str();
3940 
3941     GDBRemoteCommunication::PacketResult res =
3942         SendPacketAndWaitForResponse(packet, chunk);
3943 
3944     if (res != GDBRemoteCommunication::PacketResult::Success ||
3945         chunk.GetStringRef().empty()) {
3946       return llvm::createStringError(llvm::inconvertibleErrorCode(),
3947                                      "Error sending $qXfer packet");
3948     }
3949 
3950     // check packet code
3951     switch (chunk.GetStringRef()[0]) {
3952     // last chunk
3953     case ('l'):
3954       active = false;
3955       LLVM_FALLTHROUGH;
3956 
3957     // more chunks
3958     case ('m'):
3959       output_stream << chunk.GetStringRef().drop_front();
3960       offset += chunk.GetStringRef().size() - 1;
3961       break;
3962 
3963     // unknown chunk
3964     default:
3965       return llvm::createStringError(
3966           llvm::inconvertibleErrorCode(),
3967           "Invalid continuation code from $qXfer packet");
3968     }
3969   }
3970 
3971   return output_stream.str();
3972 }
3973 
3974 // Notify the target that gdb is prepared to serve symbol lookup requests.
3975 //  packet: "qSymbol::"
3976 //  reply:
3977 //  OK                  The target does not need to look up any (more) symbols.
3978 //  qSymbol:<sym_name>  The target requests the value of symbol sym_name (hex
3979 //  encoded).
3980 //                      LLDB may provide the value by sending another qSymbol
3981 //                      packet
3982 //                      in the form of"qSymbol:<sym_value>:<sym_name>".
3983 //
3984 //  Three examples:
3985 //
3986 //  lldb sends:    qSymbol::
3987 //  lldb receives: OK
3988 //     Remote gdb stub does not need to know the addresses of any symbols, lldb
3989 //     does not
3990 //     need to ask again in this session.
3991 //
3992 //  lldb sends:    qSymbol::
3993 //  lldb receives: qSymbol:64697370617463685f71756575655f6f666673657473
3994 //  lldb sends:    qSymbol::64697370617463685f71756575655f6f666673657473
3995 //  lldb receives: OK
3996 //     Remote gdb stub asks for address of 'dispatch_queue_offsets'.  lldb does
3997 //     not know
3998 //     the address at this time.  lldb needs to send qSymbol:: again when it has
3999 //     more
4000 //     solibs loaded.
4001 //
4002 //  lldb sends:    qSymbol::
4003 //  lldb receives: qSymbol:64697370617463685f71756575655f6f666673657473
4004 //  lldb sends:    qSymbol:2bc97554:64697370617463685f71756575655f6f666673657473
4005 //  lldb receives: OK
4006 //     Remote gdb stub asks for address of 'dispatch_queue_offsets'.  lldb says
4007 //     that it
4008 //     is at address 0x2bc97554.  Remote gdb stub sends 'OK' indicating that it
4009 //     does not
4010 //     need any more symbols.  lldb does not need to ask again in this session.
4011 
4012 void GDBRemoteCommunicationClient::ServeSymbolLookups(
4013     lldb_private::Process *process) {
4014   // Set to true once we've resolved a symbol to an address for the remote
4015   // stub. If we get an 'OK' response after this, the remote stub doesn't need
4016   // any more symbols and we can stop asking.
4017   bool symbol_response_provided = false;
4018 
4019   // Is this the initial qSymbol:: packet?
4020   bool first_qsymbol_query = true;
4021 
4022   if (m_supports_qSymbol && !m_qSymbol_requests_done) {
4023     Lock lock(*this);
4024     if (lock) {
4025       StreamString packet;
4026       packet.PutCString("qSymbol::");
4027       StringExtractorGDBRemote response;
4028       while (SendPacketAndWaitForResponseNoLock(packet.GetString(), response) ==
4029              PacketResult::Success) {
4030         if (response.IsOKResponse()) {
4031           if (symbol_response_provided || first_qsymbol_query) {
4032             m_qSymbol_requests_done = true;
4033           }
4034 
4035           // We are done serving symbols requests
4036           return;
4037         }
4038         first_qsymbol_query = false;
4039 
4040         if (response.IsUnsupportedResponse()) {
4041           // qSymbol is not supported by the current GDB server we are
4042           // connected to
4043           m_supports_qSymbol = false;
4044           return;
4045         } else {
4046           llvm::StringRef response_str(response.GetStringRef());
4047           if (response_str.startswith("qSymbol:")) {
4048             response.SetFilePos(strlen("qSymbol:"));
4049             std::string symbol_name;
4050             if (response.GetHexByteString(symbol_name)) {
4051               if (symbol_name.empty())
4052                 return;
4053 
4054               addr_t symbol_load_addr = LLDB_INVALID_ADDRESS;
4055               lldb_private::SymbolContextList sc_list;
4056               process->GetTarget().GetImages().FindSymbolsWithNameAndType(
4057                   ConstString(symbol_name), eSymbolTypeAny, sc_list);
4058               if (!sc_list.IsEmpty()) {
4059                 const size_t num_scs = sc_list.GetSize();
4060                 for (size_t sc_idx = 0;
4061                      sc_idx < num_scs &&
4062                      symbol_load_addr == LLDB_INVALID_ADDRESS;
4063                      ++sc_idx) {
4064                   SymbolContext sc;
4065                   if (sc_list.GetContextAtIndex(sc_idx, sc)) {
4066                     if (sc.symbol) {
4067                       switch (sc.symbol->GetType()) {
4068                       case eSymbolTypeInvalid:
4069                       case eSymbolTypeAbsolute:
4070                       case eSymbolTypeUndefined:
4071                       case eSymbolTypeSourceFile:
4072                       case eSymbolTypeHeaderFile:
4073                       case eSymbolTypeObjectFile:
4074                       case eSymbolTypeCommonBlock:
4075                       case eSymbolTypeBlock:
4076                       case eSymbolTypeLocal:
4077                       case eSymbolTypeParam:
4078                       case eSymbolTypeVariable:
4079                       case eSymbolTypeVariableType:
4080                       case eSymbolTypeLineEntry:
4081                       case eSymbolTypeLineHeader:
4082                       case eSymbolTypeScopeBegin:
4083                       case eSymbolTypeScopeEnd:
4084                       case eSymbolTypeAdditional:
4085                       case eSymbolTypeCompiler:
4086                       case eSymbolTypeInstrumentation:
4087                       case eSymbolTypeTrampoline:
4088                         break;
4089 
4090                       case eSymbolTypeCode:
4091                       case eSymbolTypeResolver:
4092                       case eSymbolTypeData:
4093                       case eSymbolTypeRuntime:
4094                       case eSymbolTypeException:
4095                       case eSymbolTypeObjCClass:
4096                       case eSymbolTypeObjCMetaClass:
4097                       case eSymbolTypeObjCIVar:
4098                       case eSymbolTypeReExported:
4099                         symbol_load_addr =
4100                             sc.symbol->GetLoadAddress(&process->GetTarget());
4101                         break;
4102                       }
4103                     }
4104                   }
4105                 }
4106               }
4107               // This is the normal path where our symbol lookup was successful
4108               // and we want to send a packet with the new symbol value and see
4109               // if another lookup needs to be done.
4110 
4111               // Change "packet" to contain the requested symbol value and name
4112               packet.Clear();
4113               packet.PutCString("qSymbol:");
4114               if (symbol_load_addr != LLDB_INVALID_ADDRESS) {
4115                 packet.Printf("%" PRIx64, symbol_load_addr);
4116                 symbol_response_provided = true;
4117               } else {
4118                 symbol_response_provided = false;
4119               }
4120               packet.PutCString(":");
4121               packet.PutBytesAsRawHex8(symbol_name.data(), symbol_name.size());
4122               continue; // go back to the while loop and send "packet" and wait
4123                         // for another response
4124             }
4125           }
4126         }
4127       }
4128       // If we make it here, the symbol request packet response wasn't valid or
4129       // our symbol lookup failed so we must abort
4130       return;
4131 
4132     } else if (Log *log = GetLog(GDBRLog::Process | GDBRLog::Packets)) {
4133       LLDB_LOGF(log,
4134                 "GDBRemoteCommunicationClient::%s: Didn't get sequence mutex.",
4135                 __FUNCTION__);
4136     }
4137   }
4138 }
4139 
4140 StructuredData::Array *
4141 GDBRemoteCommunicationClient::GetSupportedStructuredDataPlugins() {
4142   if (!m_supported_async_json_packets_is_valid) {
4143     // Query the server for the array of supported asynchronous JSON packets.
4144     m_supported_async_json_packets_is_valid = true;
4145 
4146     Log *log = GetLog(GDBRLog::Process);
4147 
4148     // Poll it now.
4149     StringExtractorGDBRemote response;
4150     if (SendPacketAndWaitForResponse("qStructuredDataPlugins", response) ==
4151         PacketResult::Success) {
4152       m_supported_async_json_packets_sp =
4153           StructuredData::ParseJSON(std::string(response.GetStringRef()));
4154       if (m_supported_async_json_packets_sp &&
4155           !m_supported_async_json_packets_sp->GetAsArray()) {
4156         // We were returned something other than a JSON array.  This is
4157         // invalid.  Clear it out.
4158         LLDB_LOGF(log,
4159                   "GDBRemoteCommunicationClient::%s(): "
4160                   "QSupportedAsyncJSONPackets returned invalid "
4161                   "result: %s",
4162                   __FUNCTION__, response.GetStringRef().data());
4163         m_supported_async_json_packets_sp.reset();
4164       }
4165     } else {
4166       LLDB_LOGF(log,
4167                 "GDBRemoteCommunicationClient::%s(): "
4168                 "QSupportedAsyncJSONPackets unsupported",
4169                 __FUNCTION__);
4170     }
4171 
4172     if (log && m_supported_async_json_packets_sp) {
4173       StreamString stream;
4174       m_supported_async_json_packets_sp->Dump(stream);
4175       LLDB_LOGF(log,
4176                 "GDBRemoteCommunicationClient::%s(): supported async "
4177                 "JSON packets: %s",
4178                 __FUNCTION__, stream.GetData());
4179     }
4180   }
4181 
4182   return m_supported_async_json_packets_sp
4183              ? m_supported_async_json_packets_sp->GetAsArray()
4184              : nullptr;
4185 }
4186 
4187 Status GDBRemoteCommunicationClient::SendSignalsToIgnore(
4188     llvm::ArrayRef<int32_t> signals) {
4189   // Format packet:
4190   // QPassSignals:<hex_sig1>;<hex_sig2>...;<hex_sigN>
4191   auto range = llvm::make_range(signals.begin(), signals.end());
4192   std::string packet = formatv("QPassSignals:{0:$[;]@(x-2)}", range).str();
4193 
4194   StringExtractorGDBRemote response;
4195   auto send_status = SendPacketAndWaitForResponse(packet, response);
4196 
4197   if (send_status != GDBRemoteCommunication::PacketResult::Success)
4198     return Status("Sending QPassSignals packet failed");
4199 
4200   if (response.IsOKResponse()) {
4201     return Status();
4202   } else {
4203     return Status("Unknown error happened during sending QPassSignals packet.");
4204   }
4205 }
4206 
4207 Status GDBRemoteCommunicationClient::ConfigureRemoteStructuredData(
4208     ConstString type_name, const StructuredData::ObjectSP &config_sp) {
4209   Status error;
4210 
4211   if (type_name.GetLength() == 0) {
4212     error.SetErrorString("invalid type_name argument");
4213     return error;
4214   }
4215 
4216   // Build command: Configure{type_name}: serialized config data.
4217   StreamGDBRemote stream;
4218   stream.PutCString("QConfigure");
4219   stream.PutCString(type_name.GetStringRef());
4220   stream.PutChar(':');
4221   if (config_sp) {
4222     // Gather the plain-text version of the configuration data.
4223     StreamString unescaped_stream;
4224     config_sp->Dump(unescaped_stream);
4225     unescaped_stream.Flush();
4226 
4227     // Add it to the stream in escaped fashion.
4228     stream.PutEscapedBytes(unescaped_stream.GetString().data(),
4229                            unescaped_stream.GetSize());
4230   }
4231   stream.Flush();
4232 
4233   // Send the packet.
4234   StringExtractorGDBRemote response;
4235   auto result = SendPacketAndWaitForResponse(stream.GetString(), response);
4236   if (result == PacketResult::Success) {
4237     // We failed if the config result comes back other than OK.
4238     if (strcmp(response.GetStringRef().data(), "OK") == 0) {
4239       // Okay!
4240       error.Clear();
4241     } else {
4242       error.SetErrorStringWithFormat("configuring StructuredData feature "
4243                                      "%s failed with error %s",
4244                                      type_name.AsCString(),
4245                                      response.GetStringRef().data());
4246     }
4247   } else {
4248     // Can we get more data here on the failure?
4249     error.SetErrorStringWithFormat("configuring StructuredData feature %s "
4250                                    "failed when sending packet: "
4251                                    "PacketResult=%d",
4252                                    type_name.AsCString(), (int)result);
4253   }
4254   return error;
4255 }
4256 
4257 void GDBRemoteCommunicationClient::OnRunPacketSent(bool first) {
4258   GDBRemoteClientBase::OnRunPacketSent(first);
4259   m_curr_tid = LLDB_INVALID_THREAD_ID;
4260 }
4261 
4262 bool GDBRemoteCommunicationClient::UsesNativeSignals() {
4263   if (m_uses_native_signals == eLazyBoolCalculate)
4264     GetRemoteQSupported();
4265   if (m_uses_native_signals == eLazyBoolYes)
4266     return true;
4267 
4268   // If the remote didn't indicate native-signal support explicitly,
4269   // check whether it is an old version of lldb-server.
4270   return GetThreadSuffixSupported();
4271 }
4272