1 //===-- GDBRemoteCommunicationClient.cpp ------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 
11 #include "GDBRemoteCommunicationClient.h"
12 
13 // C Includes
14 #include <sys/stat.h>
15 
16 // C++ Includes
17 #include <sstream>
18 
19 // Other libraries and framework includes
20 #include "llvm/ADT/Triple.h"
21 #include "lldb/Interpreter/Args.h"
22 #include "lldb/Core/ConnectionFileDescriptor.h"
23 #include "lldb/Core/Log.h"
24 #include "lldb/Core/State.h"
25 #include "lldb/Core/StreamGDBRemote.h"
26 #include "lldb/Core/StreamString.h"
27 #include "lldb/Host/Endian.h"
28 #include "lldb/Host/Host.h"
29 #include "lldb/Host/TimeValue.h"
30 
31 // Project includes
32 #include "Utility/StringExtractorGDBRemote.h"
33 #include "ProcessGDBRemote.h"
34 #include "ProcessGDBRemoteLog.h"
35 #include "lldb/Host/Config.h"
36 
37 using namespace lldb;
38 using namespace lldb_private;
39 
40 #ifdef LLDB_DISABLE_POSIX
41 #define SIGSTOP 17
42 #endif
43 
44 //----------------------------------------------------------------------
45 // GDBRemoteCommunicationClient constructor
46 //----------------------------------------------------------------------
47 GDBRemoteCommunicationClient::GDBRemoteCommunicationClient(bool is_platform) :
48     GDBRemoteCommunication("gdb-remote.client", "gdb-remote.client.rx_packet", is_platform),
49     m_supports_not_sending_acks (eLazyBoolCalculate),
50     m_supports_thread_suffix (eLazyBoolCalculate),
51     m_supports_threads_in_stop_reply (eLazyBoolCalculate),
52     m_supports_vCont_all (eLazyBoolCalculate),
53     m_supports_vCont_any (eLazyBoolCalculate),
54     m_supports_vCont_c (eLazyBoolCalculate),
55     m_supports_vCont_C (eLazyBoolCalculate),
56     m_supports_vCont_s (eLazyBoolCalculate),
57     m_supports_vCont_S (eLazyBoolCalculate),
58     m_qHostInfo_is_valid (eLazyBoolCalculate),
59     m_qProcessInfo_is_valid (eLazyBoolCalculate),
60     m_supports_alloc_dealloc_memory (eLazyBoolCalculate),
61     m_supports_memory_region_info  (eLazyBoolCalculate),
62     m_supports_watchpoint_support_info  (eLazyBoolCalculate),
63     m_supports_detach_stay_stopped (eLazyBoolCalculate),
64     m_watchpoints_trigger_after_instruction(eLazyBoolCalculate),
65     m_attach_or_wait_reply(eLazyBoolCalculate),
66     m_prepare_for_reg_writing_reply (eLazyBoolCalculate),
67     m_supports_p (eLazyBoolCalculate),
68     m_supports_qProcessInfoPID (true),
69     m_supports_qfProcessInfo (true),
70     m_supports_qUserName (true),
71     m_supports_qGroupName (true),
72     m_supports_qThreadStopInfo (true),
73     m_supports_z0 (true),
74     m_supports_z1 (true),
75     m_supports_z2 (true),
76     m_supports_z3 (true),
77     m_supports_z4 (true),
78     m_curr_tid (LLDB_INVALID_THREAD_ID),
79     m_curr_tid_run (LLDB_INVALID_THREAD_ID),
80     m_num_supported_hardware_watchpoints (0),
81     m_async_mutex (Mutex::eMutexTypeRecursive),
82     m_async_packet_predicate (false),
83     m_async_packet (),
84     m_async_response (),
85     m_async_signal (-1),
86     m_thread_id_to_used_usec_map (),
87     m_host_arch(),
88     m_process_arch(),
89     m_os_version_major (UINT32_MAX),
90     m_os_version_minor (UINT32_MAX),
91     m_os_version_update (UINT32_MAX)
92 {
93 }
94 
95 //----------------------------------------------------------------------
96 // Destructor
97 //----------------------------------------------------------------------
98 GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient()
99 {
100     if (IsConnected())
101         Disconnect();
102 }
103 
104 bool
105 GDBRemoteCommunicationClient::HandshakeWithServer (Error *error_ptr)
106 {
107     // Start the read thread after we send the handshake ack since if we
108     // fail to send the handshake ack, there is no reason to continue...
109     if (SendAck())
110         return true;
111 
112     if (error_ptr)
113         error_ptr->SetErrorString("failed to send the handshake ack");
114     return false;
115 }
116 
117 void
118 GDBRemoteCommunicationClient::QueryNoAckModeSupported ()
119 {
120     if (m_supports_not_sending_acks == eLazyBoolCalculate)
121     {
122         m_send_acks = true;
123         m_supports_not_sending_acks = eLazyBoolNo;
124 
125         StringExtractorGDBRemote response;
126         if (SendPacketAndWaitForResponse("QStartNoAckMode", response, false))
127         {
128             if (response.IsOKResponse())
129             {
130                 m_send_acks = false;
131                 m_supports_not_sending_acks = eLazyBoolYes;
132             }
133         }
134     }
135 }
136 
137 void
138 GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported ()
139 {
140     if (m_supports_threads_in_stop_reply == eLazyBoolCalculate)
141     {
142         m_supports_threads_in_stop_reply = eLazyBoolNo;
143 
144         StringExtractorGDBRemote response;
145         if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response, false))
146         {
147             if (response.IsOKResponse())
148                 m_supports_threads_in_stop_reply = eLazyBoolYes;
149         }
150     }
151 }
152 
153 bool
154 GDBRemoteCommunicationClient::GetVAttachOrWaitSupported ()
155 {
156     if (m_attach_or_wait_reply == eLazyBoolCalculate)
157     {
158         m_attach_or_wait_reply = eLazyBoolNo;
159 
160         StringExtractorGDBRemote response;
161         if (SendPacketAndWaitForResponse("qVAttachOrWaitSupported", response, false))
162         {
163             if (response.IsOKResponse())
164                 m_attach_or_wait_reply = eLazyBoolYes;
165         }
166     }
167     if (m_attach_or_wait_reply == eLazyBoolYes)
168         return true;
169     else
170         return false;
171 }
172 
173 bool
174 GDBRemoteCommunicationClient::GetSyncThreadStateSupported ()
175 {
176     if (m_prepare_for_reg_writing_reply == eLazyBoolCalculate)
177     {
178         m_prepare_for_reg_writing_reply = eLazyBoolNo;
179 
180         StringExtractorGDBRemote response;
181         if (SendPacketAndWaitForResponse("qSyncThreadStateSupported", response, false))
182         {
183             if (response.IsOKResponse())
184                 m_prepare_for_reg_writing_reply = eLazyBoolYes;
185         }
186     }
187     if (m_prepare_for_reg_writing_reply == eLazyBoolYes)
188         return true;
189     else
190         return false;
191 }
192 
193 
194 void
195 GDBRemoteCommunicationClient::ResetDiscoverableSettings()
196 {
197     m_supports_not_sending_acks = eLazyBoolCalculate;
198     m_supports_thread_suffix = eLazyBoolCalculate;
199     m_supports_threads_in_stop_reply = eLazyBoolCalculate;
200     m_supports_vCont_c = eLazyBoolCalculate;
201     m_supports_vCont_C = eLazyBoolCalculate;
202     m_supports_vCont_s = eLazyBoolCalculate;
203     m_supports_vCont_S = eLazyBoolCalculate;
204     m_supports_p = eLazyBoolCalculate;
205     m_qHostInfo_is_valid = eLazyBoolCalculate;
206     m_qProcessInfo_is_valid = eLazyBoolCalculate;
207     m_supports_alloc_dealloc_memory = eLazyBoolCalculate;
208     m_supports_memory_region_info = eLazyBoolCalculate;
209     m_prepare_for_reg_writing_reply = eLazyBoolCalculate;
210     m_attach_or_wait_reply = eLazyBoolCalculate;
211 
212     m_supports_qProcessInfoPID = true;
213     m_supports_qfProcessInfo = true;
214     m_supports_qUserName = true;
215     m_supports_qGroupName = true;
216     m_supports_qThreadStopInfo = true;
217     m_supports_z0 = true;
218     m_supports_z1 = true;
219     m_supports_z2 = true;
220     m_supports_z3 = true;
221     m_supports_z4 = true;
222     m_host_arch.Clear();
223     m_process_arch.Clear();
224 }
225 
226 
227 bool
228 GDBRemoteCommunicationClient::GetThreadSuffixSupported ()
229 {
230     if (m_supports_thread_suffix == eLazyBoolCalculate)
231     {
232         StringExtractorGDBRemote response;
233         m_supports_thread_suffix = eLazyBoolNo;
234         if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response, false))
235         {
236             if (response.IsOKResponse())
237                 m_supports_thread_suffix = eLazyBoolYes;
238         }
239     }
240     return m_supports_thread_suffix;
241 }
242 bool
243 GDBRemoteCommunicationClient::GetVContSupported (char flavor)
244 {
245     if (m_supports_vCont_c == eLazyBoolCalculate)
246     {
247         StringExtractorGDBRemote response;
248         m_supports_vCont_any = eLazyBoolNo;
249         m_supports_vCont_all = eLazyBoolNo;
250         m_supports_vCont_c = eLazyBoolNo;
251         m_supports_vCont_C = eLazyBoolNo;
252         m_supports_vCont_s = eLazyBoolNo;
253         m_supports_vCont_S = eLazyBoolNo;
254         if (SendPacketAndWaitForResponse("vCont?", response, false))
255         {
256             const char *response_cstr = response.GetStringRef().c_str();
257             if (::strstr (response_cstr, ";c"))
258                 m_supports_vCont_c = eLazyBoolYes;
259 
260             if (::strstr (response_cstr, ";C"))
261                 m_supports_vCont_C = eLazyBoolYes;
262 
263             if (::strstr (response_cstr, ";s"))
264                 m_supports_vCont_s = eLazyBoolYes;
265 
266             if (::strstr (response_cstr, ";S"))
267                 m_supports_vCont_S = eLazyBoolYes;
268 
269             if (m_supports_vCont_c == eLazyBoolYes &&
270                 m_supports_vCont_C == eLazyBoolYes &&
271                 m_supports_vCont_s == eLazyBoolYes &&
272                 m_supports_vCont_S == eLazyBoolYes)
273             {
274                 m_supports_vCont_all = eLazyBoolYes;
275             }
276 
277             if (m_supports_vCont_c == eLazyBoolYes ||
278                 m_supports_vCont_C == eLazyBoolYes ||
279                 m_supports_vCont_s == eLazyBoolYes ||
280                 m_supports_vCont_S == eLazyBoolYes)
281             {
282                 m_supports_vCont_any = eLazyBoolYes;
283             }
284         }
285     }
286 
287     switch (flavor)
288     {
289     case 'a': return m_supports_vCont_any;
290     case 'A': return m_supports_vCont_all;
291     case 'c': return m_supports_vCont_c;
292     case 'C': return m_supports_vCont_C;
293     case 's': return m_supports_vCont_s;
294     case 'S': return m_supports_vCont_S;
295     default: break;
296     }
297     return false;
298 }
299 
300 // Check if the target supports 'p' packet. It sends out a 'p'
301 // packet and checks the response. A normal packet will tell us
302 // that support is available.
303 //
304 // Takes a valid thread ID because p needs to apply to a thread.
305 bool
306 GDBRemoteCommunicationClient::GetpPacketSupported (lldb::tid_t tid)
307 {
308     if (m_supports_p == eLazyBoolCalculate)
309     {
310         StringExtractorGDBRemote response;
311         m_supports_p = eLazyBoolNo;
312         char packet[256];
313         if (GetThreadSuffixSupported())
314             snprintf(packet, sizeof(packet), "p0;thread:%" PRIx64 ";", tid);
315         else
316             snprintf(packet, sizeof(packet), "p0");
317 
318         if (SendPacketAndWaitForResponse(packet, response, false))
319         {
320             if (response.IsNormalResponse())
321                 m_supports_p = eLazyBoolYes;
322         }
323     }
324     return m_supports_p;
325 }
326 
327 size_t
328 GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
329 (
330     const char *payload,
331     StringExtractorGDBRemote &response,
332     bool send_async
333 )
334 {
335     return SendPacketAndWaitForResponse (payload,
336                                          ::strlen (payload),
337                                          response,
338                                          send_async);
339 }
340 
341 size_t
342 GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
343 (
344     const char *payload,
345     size_t payload_length,
346     StringExtractorGDBRemote &response,
347     bool send_async
348 )
349 {
350     Mutex::Locker locker;
351     Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
352     size_t response_len = 0;
353     if (GetSequenceMutex (locker))
354     {
355         if (SendPacketNoLock (payload, payload_length))
356            response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
357         else
358         {
359             if (log)
360                 log->Printf("error: failed to send '%*s'", (int) payload_length, payload);
361         }
362     }
363     else
364     {
365         if (send_async)
366         {
367             if (IsRunning())
368             {
369                 Mutex::Locker async_locker (m_async_mutex);
370                 m_async_packet.assign(payload, payload_length);
371                 m_async_packet_predicate.SetValue (true, eBroadcastNever);
372 
373                 if (log)
374                     log->Printf ("async: async packet = %s", m_async_packet.c_str());
375 
376                 bool timed_out = false;
377                 if (SendInterrupt(locker, 2, timed_out))
378                 {
379                     if (m_interrupt_sent)
380                     {
381                         m_interrupt_sent = false;
382                         TimeValue timeout_time;
383                         timeout_time = TimeValue::Now();
384                         timeout_time.OffsetWithSeconds (m_packet_timeout);
385 
386                         if (log)
387                             log->Printf ("async: sent interrupt");
388 
389                         if (m_async_packet_predicate.WaitForValueEqualTo (false, &timeout_time, &timed_out))
390                         {
391                             if (log)
392                                 log->Printf ("async: got response");
393 
394                             // Swap the response buffer to avoid malloc and string copy
395                             response.GetStringRef().swap (m_async_response.GetStringRef());
396                             response_len = response.GetStringRef().size();
397                         }
398                         else
399                         {
400                             if (log)
401                                 log->Printf ("async: timed out waiting for response");
402                         }
403 
404                         // Make sure we wait until the continue packet has been sent again...
405                         if (m_private_is_running.WaitForValueEqualTo (true, &timeout_time, &timed_out))
406                         {
407                             if (log)
408                             {
409                                 if (timed_out)
410                                     log->Printf ("async: timed out waiting for process to resume, but process was resumed");
411                                 else
412                                     log->Printf ("async: async packet sent");
413                             }
414                         }
415                         else
416                         {
417                             if (log)
418                                 log->Printf ("async: timed out waiting for process to resume");
419                         }
420                     }
421                     else
422                     {
423                         // We had a racy condition where we went to send the interrupt
424                         // yet we were able to get the lock, so the process must have
425                         // just stopped?
426                         if (log)
427                             log->Printf ("async: got lock without sending interrupt");
428                         // Send the packet normally since we got the lock
429                         if (SendPacketNoLock (payload, payload_length))
430                             response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
431                         else
432                         {
433                             if (log)
434                                 log->Printf("error: failed to send '%*s'", (int) payload_length, payload);
435                         }
436                     }
437                 }
438                 else
439                 {
440                     if (log)
441                         log->Printf ("async: failed to interrupt");
442                 }
443             }
444             else
445             {
446                 if (log)
447                     log->Printf ("async: not running, async is ignored");
448             }
449         }
450         else
451         {
452             if (log)
453                 log->Printf("error: failed to get packet sequence mutex, not sending packet '%*s'", (int) payload_length, payload);
454         }
455     }
456     if (response_len == 0)
457     {
458         if (log)
459             log->Printf("error: failed to get response for '%*s'", (int) payload_length, payload);
460     }
461     return response_len;
462 }
463 
464 static const char *end_delimiter = "--end--;";
465 static const int end_delimiter_len = 8;
466 
467 std::string
468 GDBRemoteCommunicationClient::HarmonizeThreadIdsForProfileData
469 (   ProcessGDBRemote *process,
470     StringExtractorGDBRemote& profileDataExtractor
471 )
472 {
473     std::map<uint64_t, uint32_t> new_thread_id_to_used_usec_map;
474     std::stringstream final_output;
475     std::string name, value;
476 
477     // Going to assuming thread_used_usec comes first, else bail out.
478     while (profileDataExtractor.GetNameColonValue(name, value))
479     {
480         if (name.compare("thread_used_id") == 0)
481         {
482             StringExtractor threadIDHexExtractor(value.c_str());
483             uint64_t thread_id = threadIDHexExtractor.GetHexMaxU64(false, 0);
484 
485             bool has_used_usec = false;
486             uint32_t curr_used_usec = 0;
487             std::string usec_name, usec_value;
488             uint32_t input_file_pos = profileDataExtractor.GetFilePos();
489             if (profileDataExtractor.GetNameColonValue(usec_name, usec_value))
490             {
491                 if (usec_name.compare("thread_used_usec") == 0)
492                 {
493                     has_used_usec = true;
494                     curr_used_usec = strtoull(usec_value.c_str(), NULL, 0);
495                 }
496                 else
497                 {
498                     // We didn't find what we want, it is probably
499                     // an older version. Bail out.
500                     profileDataExtractor.SetFilePos(input_file_pos);
501                 }
502             }
503 
504             if (has_used_usec)
505             {
506                 uint32_t prev_used_usec = 0;
507                 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_used_usec_map.find(thread_id);
508                 if (iterator != m_thread_id_to_used_usec_map.end())
509                 {
510                     prev_used_usec = m_thread_id_to_used_usec_map[thread_id];
511                 }
512 
513                 uint32_t real_used_usec = curr_used_usec - prev_used_usec;
514                 // A good first time record is one that runs for at least 0.25 sec
515                 bool good_first_time = (prev_used_usec == 0) && (real_used_usec > 250000);
516                 bool good_subsequent_time = (prev_used_usec > 0) &&
517                     ((real_used_usec > 0) || (process->HasAssignedIndexIDToThread(thread_id)));
518 
519                 if (good_first_time || good_subsequent_time)
520                 {
521                     // We try to avoid doing too many index id reservation,
522                     // resulting in fast increase of index ids.
523 
524                     final_output << name << ":";
525                     int32_t index_id = process->AssignIndexIDToThread(thread_id);
526                     final_output << index_id << ";";
527 
528                     final_output << usec_name << ":" << usec_value << ";";
529                 }
530                 else
531                 {
532                     // Skip past 'thread_used_name'.
533                     std::string local_name, local_value;
534                     profileDataExtractor.GetNameColonValue(local_name, local_value);
535                 }
536 
537                 // Store current time as previous time so that they can be compared later.
538                 new_thread_id_to_used_usec_map[thread_id] = curr_used_usec;
539             }
540             else
541             {
542                 // Bail out and use old string.
543                 final_output << name << ":" << value << ";";
544             }
545         }
546         else
547         {
548             final_output << name << ":" << value << ";";
549         }
550     }
551     final_output << end_delimiter;
552     m_thread_id_to_used_usec_map = new_thread_id_to_used_usec_map;
553 
554     return final_output.str();
555 }
556 
557 StateType
558 GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse
559 (
560     ProcessGDBRemote *process,
561     const char *payload,
562     size_t packet_length,
563     StringExtractorGDBRemote &response
564 )
565 {
566     m_curr_tid = LLDB_INVALID_THREAD_ID;
567     Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
568     if (log)
569         log->Printf ("GDBRemoteCommunicationClient::%s ()", __FUNCTION__);
570 
571     Mutex::Locker locker(m_sequence_mutex);
572     StateType state = eStateRunning;
573 
574     BroadcastEvent(eBroadcastBitRunPacketSent, NULL);
575     m_public_is_running.SetValue (true, eBroadcastNever);
576     // Set the starting continue packet into "continue_packet". This packet
577     // may change if we are interrupted and we continue after an async packet...
578     std::string continue_packet(payload, packet_length);
579 
580     bool got_async_packet = false;
581 
582     while (state == eStateRunning)
583     {
584         if (!got_async_packet)
585         {
586             if (log)
587                 log->Printf ("GDBRemoteCommunicationClient::%s () sending continue packet: %s", __FUNCTION__, continue_packet.c_str());
588             if (SendPacketNoLock(continue_packet.c_str(), continue_packet.size()) == 0)
589                 state = eStateInvalid;
590 
591             m_private_is_running.SetValue (true, eBroadcastAlways);
592         }
593 
594         got_async_packet = false;
595 
596         if (log)
597             log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(%s)", __FUNCTION__, continue_packet.c_str());
598 
599         if (WaitForPacketWithTimeoutMicroSecondsNoLock(response, UINT32_MAX))
600         {
601             if (response.Empty())
602                 state = eStateInvalid;
603             else
604             {
605                 const char stop_type = response.GetChar();
606                 if (log)
607                     log->Printf ("GDBRemoteCommunicationClient::%s () got packet: %s", __FUNCTION__, response.GetStringRef().c_str());
608                 switch (stop_type)
609                 {
610                 case 'T':
611                 case 'S':
612                     {
613                         if (process->GetStopID() == 0)
614                         {
615                             if (process->GetID() == LLDB_INVALID_PROCESS_ID)
616                             {
617                                 lldb::pid_t pid = GetCurrentProcessID ();
618                                 if (pid != LLDB_INVALID_PROCESS_ID)
619                                     process->SetID (pid);
620                             }
621                             process->BuildDynamicRegisterInfo (true);
622                         }
623 
624                         // Privately notify any internal threads that we have stopped
625                         // in case we wanted to interrupt our process, yet we might
626                         // send a packet and continue without returning control to the
627                         // user.
628                         m_private_is_running.SetValue (false, eBroadcastAlways);
629 
630                         const uint8_t signo = response.GetHexU8 (UINT8_MAX);
631 
632                         bool continue_after_async = m_async_signal != -1 || m_async_packet_predicate.GetValue();
633                         if (continue_after_async || m_interrupt_sent)
634                         {
635                             // We sent an interrupt packet to stop the inferior process
636                             // for an async signal or to send an async packet while running
637                             // but we might have been single stepping and received the
638                             // stop packet for the step instead of for the interrupt packet.
639                             // Typically when an interrupt is sent a SIGINT or SIGSTOP
640                             // is used, so if we get anything else, we need to try and
641                             // get another stop reply packet that may have been sent
642                             // due to sending the interrupt when the target is stopped
643                             // which will just re-send a copy of the last stop reply
644                             // packet. If we don't do this, then the reply for our
645                             // async packet will be the repeat stop reply packet and cause
646                             // a lot of trouble for us!
647                             if (signo != SIGINT && signo != SIGSTOP)
648                             {
649                                 continue_after_async = false;
650 
651                                 // We didn't get a a SIGINT or SIGSTOP, so try for a
652                                 // very brief time (1 ms) to get another stop reply
653                                 // packet to make sure it doesn't get in the way
654                                 StringExtractorGDBRemote extra_stop_reply_packet;
655                                 uint32_t timeout_usec = 1000;
656                                 if (WaitForPacketWithTimeoutMicroSecondsNoLock (extra_stop_reply_packet, timeout_usec))
657                                 {
658                                     switch (extra_stop_reply_packet.GetChar())
659                                     {
660                                     case 'T':
661                                     case 'S':
662                                         // We did get an extra stop reply, which means
663                                         // our interrupt didn't stop the target so we
664                                         // shouldn't continue after the async signal
665                                         // or packet is sent...
666                                         continue_after_async = false;
667                                         break;
668                                     }
669                                 }
670                             }
671                         }
672 
673                         if (m_async_signal != -1)
674                         {
675                             if (log)
676                                 log->Printf ("async: send signo = %s", Host::GetSignalAsCString (m_async_signal));
677 
678                             // Save off the async signal we are supposed to send
679                             const int async_signal = m_async_signal;
680                             // Clear the async signal member so we don't end up
681                             // sending the signal multiple times...
682                             m_async_signal = -1;
683                             // Check which signal we stopped with
684                             if (signo == async_signal)
685                             {
686                                 if (log)
687                                     log->Printf ("async: stopped with signal %s, we are done running", Host::GetSignalAsCString (signo));
688 
689                                 // We already stopped with a signal that we wanted
690                                 // to stop with, so we are done
691                             }
692                             else
693                             {
694                                 // We stopped with a different signal that the one
695                                 // we wanted to stop with, so now we must resume
696                                 // with the signal we want
697                                 char signal_packet[32];
698                                 int signal_packet_len = 0;
699                                 signal_packet_len = ::snprintf (signal_packet,
700                                                                 sizeof (signal_packet),
701                                                                 "C%2.2x",
702                                                                 async_signal);
703 
704                                 if (log)
705                                     log->Printf ("async: stopped with signal %s, resume with %s",
706                                                        Host::GetSignalAsCString (signo),
707                                                        Host::GetSignalAsCString (async_signal));
708 
709                                 // Set the continue packet to resume even if the
710                                 // interrupt didn't cause our stop (ignore continue_after_async)
711                                 continue_packet.assign(signal_packet, signal_packet_len);
712                                 continue;
713                             }
714                         }
715                         else if (m_async_packet_predicate.GetValue())
716                         {
717                             Log * packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
718 
719                             // We are supposed to send an asynchronous packet while
720                             // we are running.
721                             m_async_response.Clear();
722                             if (m_async_packet.empty())
723                             {
724                                 if (packet_log)
725                                     packet_log->Printf ("async: error: empty async packet");
726 
727                             }
728                             else
729                             {
730                                 if (packet_log)
731                                     packet_log->Printf ("async: sending packet");
732 
733                                 SendPacketAndWaitForResponse (&m_async_packet[0],
734                                                               m_async_packet.size(),
735                                                               m_async_response,
736                                                               false);
737                             }
738                             // Let the other thread that was trying to send the async
739                             // packet know that the packet has been sent and response is
740                             // ready...
741                             m_async_packet_predicate.SetValue(false, eBroadcastAlways);
742 
743                             if (packet_log)
744                                 packet_log->Printf ("async: sent packet, continue_after_async = %i", continue_after_async);
745 
746                             // Set the continue packet to resume if our interrupt
747                             // for the async packet did cause the stop
748                             if (continue_after_async)
749                             {
750                                 // Reverting this for now as it is causing deadlocks
751                                 // in programs (<rdar://problem/11529853>). In the future
752                                 // we should check our thread list and "do the right thing"
753                                 // for new threads that show up while we stop and run async
754                                 // packets. Setting the packet to 'c' to continue all threads
755                                 // is the right thing to do 99.99% of the time because if a
756                                 // thread was single stepping, and we sent an interrupt, we
757                                 // will notice above that we didn't stop due to an interrupt
758                                 // but stopped due to stepping and we would _not_ continue.
759                                 continue_packet.assign (1, 'c');
760                                 continue;
761                             }
762                         }
763                         // Stop with signal and thread info
764                         state = eStateStopped;
765                     }
766                     break;
767 
768                 case 'W':
769                 case 'X':
770                     // process exited
771                     state = eStateExited;
772                     break;
773 
774                 case 'O':
775                     // STDOUT
776                     {
777                         got_async_packet = true;
778                         std::string inferior_stdout;
779                         inferior_stdout.reserve(response.GetBytesLeft () / 2);
780                         char ch;
781                         while ((ch = response.GetHexU8()) != '\0')
782                             inferior_stdout.append(1, ch);
783                         process->AppendSTDOUT (inferior_stdout.c_str(), inferior_stdout.size());
784                     }
785                     break;
786 
787                 case 'A':
788                     // Async miscellaneous reply. Right now, only profile data is coming through this channel.
789                     {
790                         got_async_packet = true;
791                         std::string input = response.GetStringRef().substr(1); // '1' to move beyond 'A'
792                         if (m_partial_profile_data.length() > 0)
793                         {
794                             m_partial_profile_data.append(input);
795                             input = m_partial_profile_data;
796                             m_partial_profile_data.clear();
797                         }
798 
799                         size_t found, pos = 0, len = input.length();
800                         while ((found = input.find(end_delimiter, pos)) != std::string::npos)
801                         {
802                             StringExtractorGDBRemote profileDataExtractor(input.substr(pos, found).c_str());
803                             std::string profile_data = HarmonizeThreadIdsForProfileData(process, profileDataExtractor);
804                             process->BroadcastAsyncProfileData (profile_data);
805 
806                             pos = found + end_delimiter_len;
807                         }
808 
809                         if (pos < len)
810                         {
811                             // Last incomplete chunk.
812                             m_partial_profile_data = input.substr(pos);
813                         }
814                     }
815                     break;
816 
817                 case 'E':
818                     // ERROR
819                     state = eStateInvalid;
820                     break;
821 
822                 default:
823                     if (log)
824                         log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__);
825                     state = eStateInvalid;
826                     break;
827                 }
828             }
829         }
830         else
831         {
832             if (log)
833                 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__);
834             state = eStateInvalid;
835         }
836     }
837     if (log)
838         log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state));
839     response.SetFilePos(0);
840     m_private_is_running.SetValue (false, eBroadcastAlways);
841     m_public_is_running.SetValue (false, eBroadcastAlways);
842     return state;
843 }
844 
845 bool
846 GDBRemoteCommunicationClient::SendAsyncSignal (int signo)
847 {
848     Mutex::Locker async_locker (m_async_mutex);
849     m_async_signal = signo;
850     bool timed_out = false;
851     Mutex::Locker locker;
852     if (SendInterrupt (locker, 1, timed_out))
853         return true;
854     m_async_signal = -1;
855     return false;
856 }
857 
858 // This function takes a mutex locker as a parameter in case the GetSequenceMutex
859 // actually succeeds. If it doesn't succeed in acquiring the sequence mutex
860 // (the expected result), then it will send the halt packet. If it does succeed
861 // then the caller that requested the interrupt will want to keep the sequence
862 // locked down so that no one else can send packets while the caller has control.
863 // This function usually gets called when we are running and need to stop the
864 // target. It can also be used when we are running and and we need to do something
865 // else (like read/write memory), so we need to interrupt the running process
866 // (gdb remote protocol requires this), and do what we need to do, then resume.
867 
868 bool
869 GDBRemoteCommunicationClient::SendInterrupt
870 (
871     Mutex::Locker& locker,
872     uint32_t seconds_to_wait_for_stop,
873     bool &timed_out
874 )
875 {
876     timed_out = false;
877     Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
878 
879     if (IsRunning())
880     {
881         // Only send an interrupt if our debugserver is running...
882         if (GetSequenceMutex (locker))
883         {
884             if (log)
885                 log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt");
886         }
887         else
888         {
889             // Someone has the mutex locked waiting for a response or for the
890             // inferior to stop, so send the interrupt on the down low...
891             char ctrl_c = '\x03';
892             ConnectionStatus status = eConnectionStatusSuccess;
893             size_t bytes_written = Write (&ctrl_c, 1, status, NULL);
894             if (log)
895                 log->PutCString("send packet: \\x03");
896             if (bytes_written > 0)
897             {
898                 m_interrupt_sent = true;
899                 if (seconds_to_wait_for_stop)
900                 {
901                     TimeValue timeout;
902                     if (seconds_to_wait_for_stop)
903                     {
904                         timeout = TimeValue::Now();
905                         timeout.OffsetWithSeconds (seconds_to_wait_for_stop);
906                     }
907                     if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out))
908                     {
909                         if (log)
910                             log->PutCString ("SendInterrupt () - sent interrupt, private state stopped");
911                         return true;
912                     }
913                     else
914                     {
915                         if (log)
916                             log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume");
917                     }
918                 }
919                 else
920                 {
921                     if (log)
922                         log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop...");
923                     return true;
924                 }
925             }
926             else
927             {
928                 if (log)
929                     log->Printf ("SendInterrupt () - failed to write interrupt");
930             }
931             return false;
932         }
933     }
934     else
935     {
936         if (log)
937             log->Printf ("SendInterrupt () - not running");
938     }
939     return true;
940 }
941 
942 lldb::pid_t
943 GDBRemoteCommunicationClient::GetCurrentProcessID ()
944 {
945     StringExtractorGDBRemote response;
946     if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false))
947     {
948         if (response.GetChar() == 'Q')
949             if (response.GetChar() == 'C')
950                 return response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID);
951     }
952     return LLDB_INVALID_PROCESS_ID;
953 }
954 
955 bool
956 GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str)
957 {
958     error_str.clear();
959     StringExtractorGDBRemote response;
960     if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false))
961     {
962         if (response.IsOKResponse())
963             return true;
964         if (response.GetChar() == 'E')
965         {
966             // A string the describes what failed when launching...
967             error_str = response.GetStringRef().substr(1);
968         }
969         else
970         {
971             error_str.assign ("unknown error occurred launching process");
972         }
973     }
974     else
975     {
976         error_str.assign ("timed out waiting for app to launch");
977     }
978     return false;
979 }
980 
981 int
982 GDBRemoteCommunicationClient::SendArgumentsPacket (char const *argv[])
983 {
984     if (argv && argv[0])
985     {
986         StreamString packet;
987         packet.PutChar('A');
988         const char *arg;
989         for (uint32_t i = 0; (arg = argv[i]) != NULL; ++i)
990         {
991             const int arg_len = strlen(arg);
992             if (i > 0)
993                 packet.PutChar(',');
994             packet.Printf("%i,%i,", arg_len * 2, i);
995             packet.PutBytesAsRawHex8 (arg, arg_len);
996         }
997 
998         StringExtractorGDBRemote response;
999         if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1000         {
1001             if (response.IsOKResponse())
1002                 return 0;
1003             uint8_t error = response.GetError();
1004             if (error)
1005                 return error;
1006         }
1007     }
1008     return -1;
1009 }
1010 
1011 int
1012 GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value)
1013 {
1014     if (name_equal_value && name_equal_value[0])
1015     {
1016         StreamString packet;
1017         packet.Printf("QEnvironment:%s", name_equal_value);
1018         StringExtractorGDBRemote response;
1019         if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1020         {
1021             if (response.IsOKResponse())
1022                 return 0;
1023             uint8_t error = response.GetError();
1024             if (error)
1025                 return error;
1026         }
1027     }
1028     return -1;
1029 }
1030 
1031 int
1032 GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch)
1033 {
1034     if (arch && arch[0])
1035     {
1036         StreamString packet;
1037         packet.Printf("QLaunchArch:%s", arch);
1038         StringExtractorGDBRemote response;
1039         if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1040         {
1041             if (response.IsOKResponse())
1042                 return 0;
1043             uint8_t error = response.GetError();
1044             if (error)
1045                 return error;
1046         }
1047     }
1048     return -1;
1049 }
1050 
1051 bool
1052 GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major,
1053                                             uint32_t &minor,
1054                                             uint32_t &update)
1055 {
1056     if (GetHostInfo ())
1057     {
1058         if (m_os_version_major != UINT32_MAX)
1059         {
1060             major = m_os_version_major;
1061             minor = m_os_version_minor;
1062             update = m_os_version_update;
1063             return true;
1064         }
1065     }
1066     return false;
1067 }
1068 
1069 bool
1070 GDBRemoteCommunicationClient::GetOSBuildString (std::string &s)
1071 {
1072     if (GetHostInfo ())
1073     {
1074         if (!m_os_build.empty())
1075         {
1076             s = m_os_build;
1077             return true;
1078         }
1079     }
1080     s.clear();
1081     return false;
1082 }
1083 
1084 
1085 bool
1086 GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s)
1087 {
1088     if (GetHostInfo ())
1089     {
1090         if (!m_os_kernel.empty())
1091         {
1092             s = m_os_kernel;
1093             return true;
1094         }
1095     }
1096     s.clear();
1097     return false;
1098 }
1099 
1100 bool
1101 GDBRemoteCommunicationClient::GetHostname (std::string &s)
1102 {
1103     if (GetHostInfo ())
1104     {
1105         if (!m_hostname.empty())
1106         {
1107             s = m_hostname;
1108             return true;
1109         }
1110     }
1111     s.clear();
1112     return false;
1113 }
1114 
1115 ArchSpec
1116 GDBRemoteCommunicationClient::GetSystemArchitecture ()
1117 {
1118     if (GetHostInfo ())
1119         return m_host_arch;
1120     return ArchSpec();
1121 }
1122 
1123 const lldb_private::ArchSpec &
1124 GDBRemoteCommunicationClient::GetProcessArchitecture ()
1125 {
1126     if (m_qProcessInfo_is_valid == eLazyBoolCalculate)
1127         GetCurrentProcessInfo ();
1128     return m_process_arch;
1129 }
1130 
1131 
1132 bool
1133 GDBRemoteCommunicationClient::GetHostInfo (bool force)
1134 {
1135     if (force || m_qHostInfo_is_valid == eLazyBoolCalculate)
1136     {
1137         m_qHostInfo_is_valid = eLazyBoolNo;
1138         StringExtractorGDBRemote response;
1139         if (SendPacketAndWaitForResponse ("qHostInfo", response, false))
1140         {
1141             if (response.IsNormalResponse())
1142             {
1143                 std::string name;
1144                 std::string value;
1145                 uint32_t cpu = LLDB_INVALID_CPUTYPE;
1146                 uint32_t sub = 0;
1147                 std::string arch_name;
1148                 std::string os_name;
1149                 std::string vendor_name;
1150                 std::string triple;
1151                 uint32_t pointer_byte_size = 0;
1152                 StringExtractor extractor;
1153                 ByteOrder byte_order = eByteOrderInvalid;
1154                 uint32_t num_keys_decoded = 0;
1155                 while (response.GetNameColonValue(name, value))
1156                 {
1157                     if (name.compare("cputype") == 0)
1158                     {
1159                         // exception type in big endian hex
1160                         cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0);
1161                         if (cpu != LLDB_INVALID_CPUTYPE)
1162                             ++num_keys_decoded;
1163                     }
1164                     else if (name.compare("cpusubtype") == 0)
1165                     {
1166                         // exception count in big endian hex
1167                         sub = Args::StringToUInt32 (value.c_str(), 0, 0);
1168                         if (sub != 0)
1169                             ++num_keys_decoded;
1170                     }
1171                     else if (name.compare("arch") == 0)
1172                     {
1173                         arch_name.swap (value);
1174                         ++num_keys_decoded;
1175                     }
1176                     else if (name.compare("triple") == 0)
1177                     {
1178                         // The triple comes as ASCII hex bytes since it contains '-' chars
1179                         extractor.GetStringRef().swap(value);
1180                         extractor.SetFilePos(0);
1181                         extractor.GetHexByteString (triple);
1182                         ++num_keys_decoded;
1183                     }
1184                     else if (name.compare("os_build") == 0)
1185                     {
1186                         extractor.GetStringRef().swap(value);
1187                         extractor.SetFilePos(0);
1188                         extractor.GetHexByteString (m_os_build);
1189                         ++num_keys_decoded;
1190                     }
1191                     else if (name.compare("hostname") == 0)
1192                     {
1193                         extractor.GetStringRef().swap(value);
1194                         extractor.SetFilePos(0);
1195                         extractor.GetHexByteString (m_hostname);
1196                         ++num_keys_decoded;
1197                     }
1198                     else if (name.compare("os_kernel") == 0)
1199                     {
1200                         extractor.GetStringRef().swap(value);
1201                         extractor.SetFilePos(0);
1202                         extractor.GetHexByteString (m_os_kernel);
1203                         ++num_keys_decoded;
1204                     }
1205                     else if (name.compare("ostype") == 0)
1206                     {
1207                         os_name.swap (value);
1208                         ++num_keys_decoded;
1209                     }
1210                     else if (name.compare("vendor") == 0)
1211                     {
1212                         vendor_name.swap(value);
1213                         ++num_keys_decoded;
1214                     }
1215                     else if (name.compare("endian") == 0)
1216                     {
1217                         ++num_keys_decoded;
1218                         if (value.compare("little") == 0)
1219                             byte_order = eByteOrderLittle;
1220                         else if (value.compare("big") == 0)
1221                             byte_order = eByteOrderBig;
1222                         else if (value.compare("pdp") == 0)
1223                             byte_order = eByteOrderPDP;
1224                         else
1225                             --num_keys_decoded;
1226                     }
1227                     else if (name.compare("ptrsize") == 0)
1228                     {
1229                         pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 0);
1230                         if (pointer_byte_size != 0)
1231                             ++num_keys_decoded;
1232                     }
1233                     else if (name.compare("os_version") == 0)
1234                     {
1235                         Args::StringToVersion (value.c_str(),
1236                                                m_os_version_major,
1237                                                m_os_version_minor,
1238                                                m_os_version_update);
1239                         if (m_os_version_major != UINT32_MAX)
1240                             ++num_keys_decoded;
1241                     }
1242                     else if (name.compare("watchpoint_exceptions_received") == 0)
1243                     {
1244                         ++num_keys_decoded;
1245                         if (strcmp(value.c_str(),"before") == 0)
1246                             m_watchpoints_trigger_after_instruction = eLazyBoolNo;
1247                         else if (strcmp(value.c_str(),"after") == 0)
1248                             m_watchpoints_trigger_after_instruction = eLazyBoolYes;
1249                         else
1250                             --num_keys_decoded;
1251                     }
1252 
1253                 }
1254 
1255                 if (num_keys_decoded > 0)
1256                     m_qHostInfo_is_valid = eLazyBoolYes;
1257 
1258                 if (triple.empty())
1259                 {
1260                     if (arch_name.empty())
1261                     {
1262                         if (cpu != LLDB_INVALID_CPUTYPE)
1263                         {
1264                             m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1265                             if (pointer_byte_size)
1266                             {
1267                                 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1268                             }
1269                             if (byte_order != eByteOrderInvalid)
1270                             {
1271                                 assert (byte_order == m_host_arch.GetByteOrder());
1272                             }
1273 
1274                             if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0)
1275                             {
1276                                 switch (m_host_arch.GetMachine())
1277                                 {
1278                                 case llvm::Triple::arm:
1279                                 case llvm::Triple::thumb:
1280                                     os_name = "ios";
1281                                     break;
1282                                 default:
1283                                     os_name = "macosx";
1284                                     break;
1285                                 }
1286                             }
1287                             if (!vendor_name.empty())
1288                                 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1289                             if (!os_name.empty())
1290                                 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
1291 
1292                         }
1293                     }
1294                     else
1295                     {
1296                         std::string triple;
1297                         triple += arch_name;
1298                         if (!vendor_name.empty() || !os_name.empty())
1299                         {
1300                             triple += '-';
1301                             if (vendor_name.empty())
1302                                 triple += "unknown";
1303                             else
1304                                 triple += vendor_name;
1305                             triple += '-';
1306                             if (os_name.empty())
1307                                 triple += "unknown";
1308                             else
1309                                 triple += os_name;
1310                         }
1311                         m_host_arch.SetTriple (triple.c_str());
1312 
1313                         llvm::Triple &host_triple = m_host_arch.GetTriple();
1314                         if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin)
1315                         {
1316                             switch (m_host_arch.GetMachine())
1317                             {
1318                                 case llvm::Triple::arm:
1319                                 case llvm::Triple::thumb:
1320                                     host_triple.setOS(llvm::Triple::IOS);
1321                                     break;
1322                                 default:
1323                                     host_triple.setOS(llvm::Triple::MacOSX);
1324                                     break;
1325                             }
1326                         }
1327                         if (pointer_byte_size)
1328                         {
1329                             assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1330                         }
1331                         if (byte_order != eByteOrderInvalid)
1332                         {
1333                             assert (byte_order == m_host_arch.GetByteOrder());
1334                         }
1335 
1336                     }
1337                 }
1338                 else
1339                 {
1340                     m_host_arch.SetTriple (triple.c_str());
1341                     if (pointer_byte_size)
1342                     {
1343                         assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1344                     }
1345                     if (byte_order != eByteOrderInvalid)
1346                     {
1347                         assert (byte_order == m_host_arch.GetByteOrder());
1348                     }
1349                 }
1350             }
1351         }
1352     }
1353     return m_qHostInfo_is_valid == eLazyBoolYes;
1354 }
1355 
1356 int
1357 GDBRemoteCommunicationClient::SendAttach
1358 (
1359     lldb::pid_t pid,
1360     StringExtractorGDBRemote& response
1361 )
1362 {
1363     if (pid != LLDB_INVALID_PROCESS_ID)
1364     {
1365         char packet[64];
1366         const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, pid);
1367         assert (packet_len < (int)sizeof(packet));
1368         if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1369         {
1370             if (response.IsErrorResponse())
1371                 return response.GetError();
1372             return 0;
1373         }
1374     }
1375     return -1;
1376 }
1377 
1378 const lldb_private::ArchSpec &
1379 GDBRemoteCommunicationClient::GetHostArchitecture ()
1380 {
1381     if (m_qHostInfo_is_valid == eLazyBoolCalculate)
1382         GetHostInfo ();
1383     return m_host_arch;
1384 }
1385 
1386 addr_t
1387 GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions)
1388 {
1389     if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
1390     {
1391         m_supports_alloc_dealloc_memory = eLazyBoolYes;
1392         char packet[64];
1393         const int packet_len = ::snprintf (packet, sizeof(packet), "_M%" PRIx64 ",%s%s%s",
1394                                            (uint64_t)size,
1395                                            permissions & lldb::ePermissionsReadable ? "r" : "",
1396                                            permissions & lldb::ePermissionsWritable ? "w" : "",
1397                                            permissions & lldb::ePermissionsExecutable ? "x" : "");
1398         assert (packet_len < (int)sizeof(packet));
1399         StringExtractorGDBRemote response;
1400         if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1401         {
1402             if (!response.IsErrorResponse())
1403                 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1404         }
1405         else
1406         {
1407             m_supports_alloc_dealloc_memory = eLazyBoolNo;
1408         }
1409     }
1410     return LLDB_INVALID_ADDRESS;
1411 }
1412 
1413 bool
1414 GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr)
1415 {
1416     if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
1417     {
1418         m_supports_alloc_dealloc_memory = eLazyBoolYes;
1419         char packet[64];
1420         const int packet_len = ::snprintf(packet, sizeof(packet), "_m%" PRIx64, (uint64_t)addr);
1421         assert (packet_len < (int)sizeof(packet));
1422         StringExtractorGDBRemote response;
1423         if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1424         {
1425             if (response.IsOKResponse())
1426                 return true;
1427         }
1428         else
1429         {
1430             m_supports_alloc_dealloc_memory = eLazyBoolNo;
1431         }
1432     }
1433     return false;
1434 }
1435 
1436 Error
1437 GDBRemoteCommunicationClient::Detach (bool keep_stopped)
1438 {
1439     Error error;
1440 
1441     if (keep_stopped)
1442     {
1443         if (m_supports_detach_stay_stopped == eLazyBoolCalculate)
1444         {
1445             char packet[64];
1446             const int packet_len = ::snprintf(packet, sizeof(packet), "qSupportsDetachAndStayStopped:");
1447             assert (packet_len < (int)sizeof(packet));
1448             StringExtractorGDBRemote response;
1449             if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1450             {
1451                 m_supports_detach_stay_stopped = eLazyBoolYes;
1452             }
1453             else
1454             {
1455                 m_supports_detach_stay_stopped = eLazyBoolNo;
1456             }
1457         }
1458 
1459         if (m_supports_detach_stay_stopped == eLazyBoolNo)
1460         {
1461             error.SetErrorString("Stays stopped not supported by this target.");
1462             return error;
1463         }
1464         else
1465         {
1466             size_t num_sent = SendPacket ("D1", 2);
1467             if (num_sent == 0)
1468                 error.SetErrorString ("Sending extended disconnect packet failed.");
1469         }
1470     }
1471     else
1472     {
1473         size_t num_sent = SendPacket ("D", 1);
1474         if (num_sent == 0)
1475             error.SetErrorString ("Sending disconnect packet failed.");
1476     }
1477     return error;
1478 }
1479 
1480 Error
1481 GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr,
1482                                                   lldb_private::MemoryRegionInfo &region_info)
1483 {
1484     Error error;
1485     region_info.Clear();
1486 
1487     if (m_supports_memory_region_info != eLazyBoolNo)
1488     {
1489         m_supports_memory_region_info = eLazyBoolYes;
1490         char packet[64];
1491         const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%" PRIx64, (uint64_t)addr);
1492         assert (packet_len < (int)sizeof(packet));
1493         StringExtractorGDBRemote response;
1494         if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1495         {
1496             std::string name;
1497             std::string value;
1498             addr_t addr_value;
1499             bool success = true;
1500             bool saw_permissions = false;
1501             while (success && response.GetNameColonValue(name, value))
1502             {
1503                 if (name.compare ("start") == 0)
1504                 {
1505                     addr_value = Args::StringToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success);
1506                     if (success)
1507                         region_info.GetRange().SetRangeBase(addr_value);
1508                 }
1509                 else if (name.compare ("size") == 0)
1510                 {
1511                     addr_value = Args::StringToUInt64(value.c_str(), 0, 16, &success);
1512                     if (success)
1513                         region_info.GetRange().SetByteSize (addr_value);
1514                 }
1515                 else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid())
1516                 {
1517                     saw_permissions = true;
1518                     if (region_info.GetRange().Contains (addr))
1519                     {
1520                         if (value.find('r') != std::string::npos)
1521                             region_info.SetReadable (MemoryRegionInfo::eYes);
1522                         else
1523                             region_info.SetReadable (MemoryRegionInfo::eNo);
1524 
1525                         if (value.find('w') != std::string::npos)
1526                             region_info.SetWritable (MemoryRegionInfo::eYes);
1527                         else
1528                             region_info.SetWritable (MemoryRegionInfo::eNo);
1529 
1530                         if (value.find('x') != std::string::npos)
1531                             region_info.SetExecutable (MemoryRegionInfo::eYes);
1532                         else
1533                             region_info.SetExecutable (MemoryRegionInfo::eNo);
1534                     }
1535                     else
1536                     {
1537                         // The reported region does not contain this address -- we're looking at an unmapped page
1538                         region_info.SetReadable (MemoryRegionInfo::eNo);
1539                         region_info.SetWritable (MemoryRegionInfo::eNo);
1540                         region_info.SetExecutable (MemoryRegionInfo::eNo);
1541                     }
1542                 }
1543                 else if (name.compare ("error") == 0)
1544                 {
1545                     StringExtractorGDBRemote name_extractor;
1546                     // Swap "value" over into "name_extractor"
1547                     name_extractor.GetStringRef().swap(value);
1548                     // Now convert the HEX bytes into a string value
1549                     name_extractor.GetHexByteString (value);
1550                     error.SetErrorString(value.c_str());
1551                 }
1552             }
1553 
1554             // We got a valid address range back but no permissions -- which means this is an unmapped page
1555             if (region_info.GetRange().IsValid() && saw_permissions == false)
1556             {
1557                 region_info.SetReadable (MemoryRegionInfo::eNo);
1558                 region_info.SetWritable (MemoryRegionInfo::eNo);
1559                 region_info.SetExecutable (MemoryRegionInfo::eNo);
1560             }
1561         }
1562         else
1563         {
1564             m_supports_memory_region_info = eLazyBoolNo;
1565         }
1566     }
1567 
1568     if (m_supports_memory_region_info == eLazyBoolNo)
1569     {
1570         error.SetErrorString("qMemoryRegionInfo is not supported");
1571     }
1572     if (error.Fail())
1573         region_info.Clear();
1574     return error;
1575 
1576 }
1577 
1578 Error
1579 GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num)
1580 {
1581     Error error;
1582 
1583     if (m_supports_watchpoint_support_info == eLazyBoolYes)
1584     {
1585         num = m_num_supported_hardware_watchpoints;
1586         return error;
1587     }
1588 
1589     // Set num to 0 first.
1590     num = 0;
1591     if (m_supports_watchpoint_support_info != eLazyBoolNo)
1592     {
1593         char packet[64];
1594         const int packet_len = ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:");
1595         assert (packet_len < (int)sizeof(packet));
1596         StringExtractorGDBRemote response;
1597         if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1598         {
1599             m_supports_watchpoint_support_info = eLazyBoolYes;
1600             std::string name;
1601             std::string value;
1602             while (response.GetNameColonValue(name, value))
1603             {
1604                 if (name.compare ("num") == 0)
1605                 {
1606                     num = Args::StringToUInt32(value.c_str(), 0, 0);
1607                     m_num_supported_hardware_watchpoints = num;
1608                 }
1609             }
1610         }
1611         else
1612         {
1613             m_supports_watchpoint_support_info = eLazyBoolNo;
1614         }
1615     }
1616 
1617     if (m_supports_watchpoint_support_info == eLazyBoolNo)
1618     {
1619         error.SetErrorString("qWatchpointSupportInfo is not supported");
1620     }
1621     return error;
1622 
1623 }
1624 
1625 lldb_private::Error
1626 GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num, bool& after)
1627 {
1628     Error error(GetWatchpointSupportInfo(num));
1629     if (error.Success())
1630         error = GetWatchpointsTriggerAfterInstruction(after);
1631     return error;
1632 }
1633 
1634 lldb_private::Error
1635 GDBRemoteCommunicationClient::GetWatchpointsTriggerAfterInstruction (bool &after)
1636 {
1637     Error error;
1638 
1639     // we assume watchpoints will happen after running the relevant opcode
1640     // and we only want to override this behavior if we have explicitly
1641     // received a qHostInfo telling us otherwise
1642     if (m_qHostInfo_is_valid != eLazyBoolYes)
1643         after = true;
1644     else
1645         after = (m_watchpoints_trigger_after_instruction != eLazyBoolNo);
1646     return error;
1647 }
1648 
1649 int
1650 GDBRemoteCommunicationClient::SetSTDIN (char const *path)
1651 {
1652     if (path && path[0])
1653     {
1654         StreamString packet;
1655         packet.PutCString("QSetSTDIN:");
1656         packet.PutBytesAsRawHex8(path, strlen(path));
1657 
1658         StringExtractorGDBRemote response;
1659         if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1660         {
1661             if (response.IsOKResponse())
1662                 return 0;
1663             uint8_t error = response.GetError();
1664             if (error)
1665                 return error;
1666         }
1667     }
1668     return -1;
1669 }
1670 
1671 int
1672 GDBRemoteCommunicationClient::SetSTDOUT (char const *path)
1673 {
1674     if (path && path[0])
1675     {
1676         StreamString packet;
1677         packet.PutCString("QSetSTDOUT:");
1678         packet.PutBytesAsRawHex8(path, strlen(path));
1679 
1680         StringExtractorGDBRemote response;
1681         if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1682         {
1683             if (response.IsOKResponse())
1684                 return 0;
1685             uint8_t error = response.GetError();
1686             if (error)
1687                 return error;
1688         }
1689     }
1690     return -1;
1691 }
1692 
1693 int
1694 GDBRemoteCommunicationClient::SetSTDERR (char const *path)
1695 {
1696     if (path && path[0])
1697     {
1698         StreamString packet;
1699         packet.PutCString("QSetSTDERR:");
1700         packet.PutBytesAsRawHex8(path, strlen(path));
1701 
1702         StringExtractorGDBRemote response;
1703         if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1704         {
1705             if (response.IsOKResponse())
1706                 return 0;
1707             uint8_t error = response.GetError();
1708             if (error)
1709                 return error;
1710         }
1711     }
1712     return -1;
1713 }
1714 
1715 int
1716 GDBRemoteCommunicationClient::SetWorkingDir (char const *path)
1717 {
1718     if (path && path[0])
1719     {
1720         StreamString packet;
1721         packet.PutCString("QSetWorkingDir:");
1722         packet.PutBytesAsRawHex8(path, strlen(path));
1723 
1724         StringExtractorGDBRemote response;
1725         if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1726         {
1727             if (response.IsOKResponse())
1728                 return 0;
1729             uint8_t error = response.GetError();
1730             if (error)
1731                 return error;
1732         }
1733     }
1734     return -1;
1735 }
1736 
1737 int
1738 GDBRemoteCommunicationClient::SetDisableASLR (bool enable)
1739 {
1740     char packet[32];
1741     const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0);
1742     assert (packet_len < (int)sizeof(packet));
1743     StringExtractorGDBRemote response;
1744     if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1745     {
1746         if (response.IsOKResponse())
1747             return 0;
1748         uint8_t error = response.GetError();
1749         if (error)
1750             return error;
1751     }
1752     return -1;
1753 }
1754 
1755 bool
1756 GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info)
1757 {
1758     if (response.IsNormalResponse())
1759     {
1760         std::string name;
1761         std::string value;
1762         StringExtractor extractor;
1763 
1764         while (response.GetNameColonValue(name, value))
1765         {
1766             if (name.compare("pid") == 0)
1767             {
1768                 process_info.SetProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1769             }
1770             else if (name.compare("ppid") == 0)
1771             {
1772                 process_info.SetParentProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1773             }
1774             else if (name.compare("uid") == 0)
1775             {
1776                 process_info.SetUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1777             }
1778             else if (name.compare("euid") == 0)
1779             {
1780                 process_info.SetEffectiveUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1781             }
1782             else if (name.compare("gid") == 0)
1783             {
1784                 process_info.SetGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1785             }
1786             else if (name.compare("egid") == 0)
1787             {
1788                 process_info.SetEffectiveGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1789             }
1790             else if (name.compare("triple") == 0)
1791             {
1792                 // The triple comes as ASCII hex bytes since it contains '-' chars
1793                 extractor.GetStringRef().swap(value);
1794                 extractor.SetFilePos(0);
1795                 extractor.GetHexByteString (value);
1796                 process_info.GetArchitecture ().SetTriple (value.c_str());
1797             }
1798             else if (name.compare("name") == 0)
1799             {
1800                 StringExtractor extractor;
1801                 // The process name from ASCII hex bytes since we can't
1802                 // control the characters in a process name
1803                 extractor.GetStringRef().swap(value);
1804                 extractor.SetFilePos(0);
1805                 extractor.GetHexByteString (value);
1806                 process_info.GetExecutableFile().SetFile (value.c_str(), false);
1807             }
1808         }
1809 
1810         if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
1811             return true;
1812     }
1813     return false;
1814 }
1815 
1816 bool
1817 GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
1818 {
1819     process_info.Clear();
1820 
1821     if (m_supports_qProcessInfoPID)
1822     {
1823         char packet[32];
1824         const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%" PRIu64, pid);
1825         assert (packet_len < (int)sizeof(packet));
1826         StringExtractorGDBRemote response;
1827         if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1828         {
1829             return DecodeProcessInfoResponse (response, process_info);
1830         }
1831         else
1832         {
1833             m_supports_qProcessInfoPID = false;
1834             return false;
1835         }
1836     }
1837     return false;
1838 }
1839 
1840 bool
1841 GDBRemoteCommunicationClient::GetCurrentProcessInfo ()
1842 {
1843     if (m_qProcessInfo_is_valid == eLazyBoolYes)
1844         return true;
1845     if (m_qProcessInfo_is_valid == eLazyBoolNo)
1846         return false;
1847 
1848     GetHostInfo ();
1849 
1850     StringExtractorGDBRemote response;
1851     if (SendPacketAndWaitForResponse ("qProcessInfo", response, false))
1852     {
1853         if (response.IsNormalResponse())
1854         {
1855             std::string name;
1856             std::string value;
1857             uint32_t cpu = LLDB_INVALID_CPUTYPE;
1858             uint32_t sub = 0;
1859             std::string arch_name;
1860             std::string os_name;
1861             std::string vendor_name;
1862             std::string triple;
1863             uint32_t pointer_byte_size = 0;
1864             StringExtractor extractor;
1865             ByteOrder byte_order = eByteOrderInvalid;
1866             uint32_t num_keys_decoded = 0;
1867             while (response.GetNameColonValue(name, value))
1868             {
1869                 if (name.compare("cputype") == 0)
1870                 {
1871                     cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 16);
1872                     if (cpu != LLDB_INVALID_CPUTYPE)
1873                         ++num_keys_decoded;
1874                 }
1875                 else if (name.compare("cpusubtype") == 0)
1876                 {
1877                     sub = Args::StringToUInt32 (value.c_str(), 0, 16);
1878                     if (sub != 0)
1879                         ++num_keys_decoded;
1880                 }
1881                 else if (name.compare("ostype") == 0)
1882                 {
1883                     os_name.swap (value);
1884                     ++num_keys_decoded;
1885                 }
1886                 else if (name.compare("vendor") == 0)
1887                 {
1888                     vendor_name.swap(value);
1889                     ++num_keys_decoded;
1890                 }
1891                 else if (name.compare("endian") == 0)
1892                 {
1893                     ++num_keys_decoded;
1894                     if (value.compare("little") == 0)
1895                         byte_order = eByteOrderLittle;
1896                     else if (value.compare("big") == 0)
1897                         byte_order = eByteOrderBig;
1898                     else if (value.compare("pdp") == 0)
1899                         byte_order = eByteOrderPDP;
1900                     else
1901                         --num_keys_decoded;
1902                 }
1903                 else if (name.compare("ptrsize") == 0)
1904                 {
1905                     pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 16);
1906                     if (pointer_byte_size != 0)
1907                         ++num_keys_decoded;
1908                 }
1909             }
1910             if (num_keys_decoded > 0)
1911                 m_qProcessInfo_is_valid = eLazyBoolYes;
1912             if (cpu != LLDB_INVALID_CPUTYPE && !os_name.empty() && !vendor_name.empty())
1913             {
1914                 m_process_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1915                 if (pointer_byte_size)
1916                 {
1917                     assert (pointer_byte_size == m_process_arch.GetAddressByteSize());
1918                 }
1919                 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1920                 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
1921                 return true;
1922             }
1923         }
1924     }
1925     else
1926     {
1927         m_qProcessInfo_is_valid = eLazyBoolNo;
1928     }
1929 
1930     return false;
1931 }
1932 
1933 
1934 uint32_t
1935 GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1936                                              ProcessInstanceInfoList &process_infos)
1937 {
1938     process_infos.Clear();
1939 
1940     if (m_supports_qfProcessInfo)
1941     {
1942         StreamString packet;
1943         packet.PutCString ("qfProcessInfo");
1944         if (!match_info.MatchAllProcesses())
1945         {
1946             packet.PutChar (':');
1947             const char *name = match_info.GetProcessInfo().GetName();
1948             bool has_name_match = false;
1949             if (name && name[0])
1950             {
1951                 has_name_match = true;
1952                 NameMatchType name_match_type = match_info.GetNameMatchType();
1953                 switch (name_match_type)
1954                 {
1955                 case eNameMatchIgnore:
1956                     has_name_match = false;
1957                     break;
1958 
1959                 case eNameMatchEquals:
1960                     packet.PutCString ("name_match:equals;");
1961                     break;
1962 
1963                 case eNameMatchContains:
1964                     packet.PutCString ("name_match:contains;");
1965                     break;
1966 
1967                 case eNameMatchStartsWith:
1968                     packet.PutCString ("name_match:starts_with;");
1969                     break;
1970 
1971                 case eNameMatchEndsWith:
1972                     packet.PutCString ("name_match:ends_with;");
1973                     break;
1974 
1975                 case eNameMatchRegularExpression:
1976                     packet.PutCString ("name_match:regex;");
1977                     break;
1978                 }
1979                 if (has_name_match)
1980                 {
1981                     packet.PutCString ("name:");
1982                     packet.PutBytesAsRawHex8(name, ::strlen(name));
1983                     packet.PutChar (';');
1984                 }
1985             }
1986 
1987             if (match_info.GetProcessInfo().ProcessIDIsValid())
1988                 packet.Printf("pid:%" PRIu64 ";",match_info.GetProcessInfo().GetProcessID());
1989             if (match_info.GetProcessInfo().ParentProcessIDIsValid())
1990                 packet.Printf("parent_pid:%" PRIu64 ";",match_info.GetProcessInfo().GetParentProcessID());
1991             if (match_info.GetProcessInfo().UserIDIsValid())
1992                 packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID());
1993             if (match_info.GetProcessInfo().GroupIDIsValid())
1994                 packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID());
1995             if (match_info.GetProcessInfo().EffectiveUserIDIsValid())
1996                 packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID());
1997             if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1998                 packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID());
1999             if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
2000                 packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0);
2001             if (match_info.GetProcessInfo().GetArchitecture().IsValid())
2002             {
2003                 const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture();
2004                 const llvm::Triple &triple = match_arch.GetTriple();
2005                 packet.PutCString("triple:");
2006                 packet.PutCStringAsRawHex8(triple.getTriple().c_str());
2007                 packet.PutChar (';');
2008             }
2009         }
2010         StringExtractorGDBRemote response;
2011         if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
2012         {
2013             do
2014             {
2015                 ProcessInstanceInfo process_info;
2016                 if (!DecodeProcessInfoResponse (response, process_info))
2017                     break;
2018                 process_infos.Append(process_info);
2019                 response.GetStringRef().clear();
2020                 response.SetFilePos(0);
2021             } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false));
2022         }
2023         else
2024         {
2025             m_supports_qfProcessInfo = false;
2026             return 0;
2027         }
2028     }
2029     return process_infos.GetSize();
2030 
2031 }
2032 
2033 bool
2034 GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name)
2035 {
2036     if (m_supports_qUserName)
2037     {
2038         char packet[32];
2039         const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid);
2040         assert (packet_len < (int)sizeof(packet));
2041         StringExtractorGDBRemote response;
2042         if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
2043         {
2044             if (response.IsNormalResponse())
2045             {
2046                 // Make sure we parsed the right number of characters. The response is
2047                 // the hex encoded user name and should make up the entire packet.
2048                 // If there are any non-hex ASCII bytes, the length won't match below..
2049                 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
2050                     return true;
2051             }
2052         }
2053         else
2054         {
2055             m_supports_qUserName = false;
2056             return false;
2057         }
2058     }
2059     return false;
2060 
2061 }
2062 
2063 bool
2064 GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name)
2065 {
2066     if (m_supports_qGroupName)
2067     {
2068         char packet[32];
2069         const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid);
2070         assert (packet_len < (int)sizeof(packet));
2071         StringExtractorGDBRemote response;
2072         if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
2073         {
2074             if (response.IsNormalResponse())
2075             {
2076                 // Make sure we parsed the right number of characters. The response is
2077                 // the hex encoded group name and should make up the entire packet.
2078                 // If there are any non-hex ASCII bytes, the length won't match below..
2079                 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
2080                     return true;
2081             }
2082         }
2083         else
2084         {
2085             m_supports_qGroupName = false;
2086             return false;
2087         }
2088     }
2089     return false;
2090 }
2091 
2092 void
2093 GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets)
2094 {
2095     uint32_t i;
2096     TimeValue start_time, end_time;
2097     uint64_t total_time_nsec;
2098     float packets_per_second;
2099     if (SendSpeedTestPacket (0, 0))
2100     {
2101         for (uint32_t send_size = 0; send_size <= 1024; send_size *= 2)
2102         {
2103             for (uint32_t recv_size = 0; recv_size <= 1024; recv_size *= 2)
2104             {
2105                 start_time = TimeValue::Now();
2106                 for (i=0; i<num_packets; ++i)
2107                 {
2108                     SendSpeedTestPacket (send_size, recv_size);
2109                 }
2110                 end_time = TimeValue::Now();
2111                 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
2112                 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
2113                 printf ("%u qSpeedTest(send=%-5u, recv=%-5u) in %" PRIu64 ".%9.9" PRIu64 " sec for %f packets/sec.\n",
2114                         num_packets,
2115                         send_size,
2116                         recv_size,
2117                         total_time_nsec / TimeValue::NanoSecPerSec,
2118                         total_time_nsec % TimeValue::NanoSecPerSec,
2119                         packets_per_second);
2120                 if (recv_size == 0)
2121                     recv_size = 32;
2122             }
2123             if (send_size == 0)
2124                 send_size = 32;
2125         }
2126     }
2127     else
2128     {
2129         start_time = TimeValue::Now();
2130         for (i=0; i<num_packets; ++i)
2131         {
2132             GetCurrentProcessID ();
2133         }
2134         end_time = TimeValue::Now();
2135         total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
2136         packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
2137         printf ("%u 'qC' packets packets in 0x%" PRIu64 "%9.9" PRIu64 " sec for %f packets/sec.\n",
2138                 num_packets,
2139                 total_time_nsec / TimeValue::NanoSecPerSec,
2140                 total_time_nsec % TimeValue::NanoSecPerSec,
2141                 packets_per_second);
2142     }
2143 }
2144 
2145 bool
2146 GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size)
2147 {
2148     StreamString packet;
2149     packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size);
2150     uint32_t bytes_left = send_size;
2151     while (bytes_left > 0)
2152     {
2153         if (bytes_left >= 26)
2154         {
2155             packet.PutCString("abcdefghijklmnopqrstuvwxyz");
2156             bytes_left -= 26;
2157         }
2158         else
2159         {
2160             packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz");
2161             bytes_left = 0;
2162         }
2163     }
2164 
2165     StringExtractorGDBRemote response;
2166     return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) > 0;
2167     return false;
2168 }
2169 
2170 uint16_t
2171 GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort (lldb::pid_t &pid)
2172 {
2173     pid = LLDB_INVALID_PROCESS_ID;
2174     StringExtractorGDBRemote response;
2175     StreamString stream;
2176     stream.PutCString("qLaunchGDBServer:port:0;");
2177     std::string hostname;
2178     if (Host::GetHostname (hostname))
2179     {
2180         // Make the GDB server we launch only accept connections from this host
2181         stream.Printf("host:%s;", hostname.c_str());
2182     }
2183     else
2184     {
2185         // Make the GDB server we launch accept connections from any host since we can't figure out the hostname
2186         stream.Printf("host:*;");
2187     }
2188     const char *packet = stream.GetData();
2189     int packet_len = stream.GetSize();
2190 
2191     if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2192     {
2193         std::string name;
2194         std::string value;
2195         uint16_t port = 0;
2196         while (response.GetNameColonValue(name, value))
2197         {
2198             if (name.compare("port") == 0)
2199                 port = Args::StringToUInt32(value.c_str(), 0, 0);
2200             else if (name.compare("pid") == 0)
2201                 pid = Args::StringToUInt64(value.c_str(), LLDB_INVALID_PROCESS_ID, 0);
2202         }
2203         return port;
2204     }
2205     return 0;
2206 }
2207 
2208 bool
2209 GDBRemoteCommunicationClient::KillSpawnedProcess (lldb::pid_t pid)
2210 {
2211     StreamString stream;
2212     stream.Printf ("qKillSpawnedProcess:%" PRId64 , pid);
2213     const char *packet = stream.GetData();
2214     int packet_len = stream.GetSize();
2215     pid = LLDB_INVALID_PROCESS_ID;
2216     StringExtractorGDBRemote response;
2217     if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2218     {
2219         if (response.IsOKResponse())
2220             return true;
2221     }
2222     return false;
2223 }
2224 
2225 bool
2226 GDBRemoteCommunicationClient::SetCurrentThread (uint64_t tid)
2227 {
2228     if (m_curr_tid == tid)
2229         return true;
2230 
2231     char packet[32];
2232     int packet_len;
2233     if (tid == UINT64_MAX)
2234         packet_len = ::snprintf (packet, sizeof(packet), "Hg-1");
2235     else
2236         packet_len = ::snprintf (packet, sizeof(packet), "Hg%" PRIx64, tid);
2237     assert (packet_len + 1 < (int)sizeof(packet));
2238     StringExtractorGDBRemote response;
2239     if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2240     {
2241         if (response.IsOKResponse())
2242         {
2243             m_curr_tid = tid;
2244             return true;
2245         }
2246     }
2247     return false;
2248 }
2249 
2250 bool
2251 GDBRemoteCommunicationClient::SetCurrentThreadForRun (uint64_t tid)
2252 {
2253     if (m_curr_tid_run == tid)
2254         return true;
2255 
2256     char packet[32];
2257     int packet_len;
2258     if (tid == UINT64_MAX)
2259         packet_len = ::snprintf (packet, sizeof(packet), "Hc-1");
2260     else
2261         packet_len = ::snprintf (packet, sizeof(packet), "Hc%" PRIx64, tid);
2262 
2263     assert (packet_len + 1 < (int)sizeof(packet));
2264     StringExtractorGDBRemote response;
2265     if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2266     {
2267         if (response.IsOKResponse())
2268         {
2269             m_curr_tid_run = tid;
2270             return true;
2271         }
2272     }
2273     return false;
2274 }
2275 
2276 bool
2277 GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response)
2278 {
2279     if (SendPacketAndWaitForResponse("?", 1, response, false))
2280         return response.IsNormalResponse();
2281     return false;
2282 }
2283 
2284 bool
2285 GDBRemoteCommunicationClient::GetThreadStopInfo (lldb::tid_t tid, StringExtractorGDBRemote &response)
2286 {
2287     if (m_supports_qThreadStopInfo)
2288     {
2289         char packet[256];
2290         int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%" PRIx64, tid);
2291         assert (packet_len < (int)sizeof(packet));
2292         if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2293         {
2294             if (response.IsNormalResponse())
2295                 return true;
2296             else
2297                 return false;
2298         }
2299         else
2300         {
2301             m_supports_qThreadStopInfo = false;
2302         }
2303     }
2304 //    if (SetCurrentThread (tid))
2305 //        return GetStopReply (response);
2306     return false;
2307 }
2308 
2309 
2310 uint8_t
2311 GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert,  addr_t addr, uint32_t length)
2312 {
2313     switch (type)
2314     {
2315     case eBreakpointSoftware:   if (!m_supports_z0) return UINT8_MAX; break;
2316     case eBreakpointHardware:   if (!m_supports_z1) return UINT8_MAX; break;
2317     case eWatchpointWrite:      if (!m_supports_z2) return UINT8_MAX; break;
2318     case eWatchpointRead:       if (!m_supports_z3) return UINT8_MAX; break;
2319     case eWatchpointReadWrite:  if (!m_supports_z4) return UINT8_MAX; break;
2320     }
2321 
2322     char packet[64];
2323     const int packet_len = ::snprintf (packet,
2324                                        sizeof(packet),
2325                                        "%c%i,%" PRIx64 ",%x",
2326                                        insert ? 'Z' : 'z',
2327                                        type,
2328                                        addr,
2329                                        length);
2330 
2331     assert (packet_len + 1 < (int)sizeof(packet));
2332     StringExtractorGDBRemote response;
2333     if (SendPacketAndWaitForResponse(packet, packet_len, response, true))
2334     {
2335         if (response.IsOKResponse())
2336             return 0;
2337         else if (response.IsErrorResponse())
2338             return response.GetError();
2339     }
2340     else
2341     {
2342         switch (type)
2343         {
2344             case eBreakpointSoftware:   m_supports_z0 = false; break;
2345             case eBreakpointHardware:   m_supports_z1 = false; break;
2346             case eWatchpointWrite:      m_supports_z2 = false; break;
2347             case eWatchpointRead:       m_supports_z3 = false; break;
2348             case eWatchpointReadWrite:  m_supports_z4 = false; break;
2349         }
2350     }
2351 
2352     return UINT8_MAX;
2353 }
2354 
2355 size_t
2356 GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
2357                                                    bool &sequence_mutex_unavailable)
2358 {
2359     Mutex::Locker locker;
2360     thread_ids.clear();
2361 
2362     if (GetSequenceMutex (locker, "ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex"))
2363     {
2364         sequence_mutex_unavailable = false;
2365         StringExtractorGDBRemote response;
2366 
2367         for (SendPacketNoLock ("qfThreadInfo", strlen("qfThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
2368              response.IsNormalResponse();
2369              SendPacketNoLock ("qsThreadInfo", strlen("qsThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()))
2370         {
2371             char ch = response.GetChar();
2372             if (ch == 'l')
2373                 break;
2374             if (ch == 'm')
2375             {
2376                 do
2377                 {
2378                     tid_t tid = response.GetHexMaxU64(false, LLDB_INVALID_THREAD_ID);
2379 
2380                     if (tid != LLDB_INVALID_THREAD_ID)
2381                     {
2382                         thread_ids.push_back (tid);
2383                     }
2384                     ch = response.GetChar();    // Skip the command separator
2385                 } while (ch == ',');            // Make sure we got a comma separator
2386             }
2387         }
2388     }
2389     else
2390     {
2391 #if defined (LLDB_CONFIGURATION_DEBUG)
2392         // assert(!"ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex");
2393 #else
2394         Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
2395         if (log)
2396             log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'");
2397 #endif
2398         sequence_mutex_unavailable = true;
2399     }
2400     return thread_ids.size();
2401 }
2402 
2403 lldb::addr_t
2404 GDBRemoteCommunicationClient::GetShlibInfoAddr()
2405 {
2406     if (!IsRunning())
2407     {
2408         StringExtractorGDBRemote response;
2409         if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false))
2410         {
2411             if (response.IsNormalResponse())
2412                 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
2413         }
2414     }
2415     return LLDB_INVALID_ADDRESS;
2416 }
2417 
2418 lldb_private::Error
2419 GDBRemoteCommunicationClient::RunShellCommand (const char *command,           // Shouldn't be NULL
2420                                                const char *working_dir,       // Pass NULL to use the current working directory
2421                                                int *status_ptr,               // Pass NULL if you don't want the process exit status
2422                                                int *signo_ptr,                // Pass NULL if you don't want the signal that caused the process to exit
2423                                                std::string *command_output,   // Pass NULL if you don't want the command output
2424                                                uint32_t timeout_sec)          // Timeout in seconds to wait for shell program to finish
2425 {
2426     lldb_private::StreamString stream;
2427     stream.PutCString("qPlatform_RunCommand:");
2428     stream.PutBytesAsRawHex8(command, strlen(command));
2429     stream.PutChar(',');
2430     stream.PutHex32(timeout_sec);
2431     if (working_dir && *working_dir)
2432     {
2433         stream.PutChar(',');
2434         stream.PutBytesAsRawHex8(working_dir, strlen(working_dir));
2435     }
2436     const char *packet = stream.GetData();
2437     int packet_len = stream.GetSize();
2438     StringExtractorGDBRemote response;
2439     if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2440     {
2441         if (response.GetChar() != 'F')
2442             return Error("malformed reply");
2443         if (response.GetChar() != ',')
2444             return Error("malformed reply");
2445         uint32_t exitcode = response.GetHexMaxU32(false, UINT32_MAX);
2446         if (exitcode == UINT32_MAX)
2447             return Error("unable to run remote process");
2448         else if (status_ptr)
2449             *status_ptr = exitcode;
2450         if (response.GetChar() != ',')
2451             return Error("malformed reply");
2452         uint32_t signo = response.GetHexMaxU32(false, UINT32_MAX);
2453         if (signo_ptr)
2454             *signo_ptr = signo;
2455         if (response.GetChar() != ',')
2456             return Error("malformed reply");
2457         std::string output;
2458         response.GetEscapedBinaryData(output);
2459         if (command_output)
2460             command_output->assign(output);
2461         return Error();
2462     }
2463     return Error("unable to send packet");
2464 }
2465 
2466 uint32_t
2467 GDBRemoteCommunicationClient::MakeDirectory (const std::string &path,
2468                                              mode_t mode)
2469 {
2470     lldb_private::StreamString stream;
2471     stream.PutCString("qPlatform_IO_MkDir:");
2472     stream.PutHex32(mode);
2473     stream.PutChar(',');
2474     stream.PutBytesAsRawHex8(path.c_str(), path.size());
2475     const char *packet = stream.GetData();
2476     int packet_len = stream.GetSize();
2477     StringExtractorGDBRemote response;
2478     if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2479     {
2480         return response.GetHexMaxU32(false, UINT32_MAX);
2481     }
2482     return UINT32_MAX;
2483 
2484 }
2485 
2486 static uint64_t
2487 ParseHostIOPacketResponse (StringExtractorGDBRemote &response,
2488                            uint64_t fail_result,
2489                            Error &error)
2490 {
2491     response.SetFilePos(0);
2492     if (response.GetChar() != 'F')
2493         return fail_result;
2494     int32_t result = response.GetS32 (-2);
2495     if (result == -2)
2496         return fail_result;
2497     if (response.GetChar() == ',')
2498     {
2499         int result_errno = response.GetS32 (-2);
2500         if (result_errno != -2)
2501             error.SetError(result_errno, eErrorTypePOSIX);
2502         else
2503             error.SetError(-1, eErrorTypeGeneric);
2504     }
2505     else
2506         error.Clear();
2507     return  result;
2508 }
2509 lldb::user_id_t
2510 GDBRemoteCommunicationClient::OpenFile (const lldb_private::FileSpec& file_spec,
2511                                         uint32_t flags,
2512                                         mode_t mode,
2513                                         Error &error)
2514 {
2515     lldb_private::StreamString stream;
2516     stream.PutCString("vFile:open:");
2517     std::string path (file_spec.GetPath());
2518     if (path.empty())
2519         return UINT64_MAX;
2520     stream.PutCStringAsRawHex8(path.c_str());
2521     stream.PutChar(',');
2522     const uint32_t posix_open_flags = File::ConvertOpenOptionsForPOSIXOpen(flags);
2523     stream.PutHex32(posix_open_flags);
2524     stream.PutChar(',');
2525     stream.PutHex32(mode);
2526     const char* packet = stream.GetData();
2527     int packet_len = stream.GetSize();
2528     StringExtractorGDBRemote response;
2529     if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2530     {
2531         return ParseHostIOPacketResponse (response, UINT64_MAX, error);
2532     }
2533     return UINT64_MAX;
2534 }
2535 
2536 bool
2537 GDBRemoteCommunicationClient::CloseFile (lldb::user_id_t fd,
2538                                          Error &error)
2539 {
2540     lldb_private::StreamString stream;
2541     stream.Printf("vFile:close:%i", (int)fd);
2542     const char* packet = stream.GetData();
2543     int packet_len = stream.GetSize();
2544     StringExtractorGDBRemote response;
2545     if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2546     {
2547         return ParseHostIOPacketResponse (response, -1, error) == 0;
2548     }
2549     return UINT64_MAX;
2550 }
2551 
2552 // Extension of host I/O packets to get the file size.
2553 lldb::user_id_t
2554 GDBRemoteCommunicationClient::GetFileSize (const lldb_private::FileSpec& file_spec)
2555 {
2556     lldb_private::StreamString stream;
2557     stream.PutCString("vFile:size:");
2558     std::string path (file_spec.GetPath());
2559     stream.PutCStringAsRawHex8(path.c_str());
2560     const char* packet = stream.GetData();
2561     int packet_len = stream.GetSize();
2562     StringExtractorGDBRemote response;
2563     if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2564     {
2565         if (response.GetChar() != 'F')
2566             return UINT64_MAX;
2567         uint32_t retcode = response.GetHexMaxU64(false, UINT64_MAX);
2568         return retcode;
2569     }
2570     return UINT64_MAX;
2571 }
2572 
2573 uint32_t
2574 GDBRemoteCommunicationClient::GetFilePermissions(const lldb_private::FileSpec& file_spec, Error &error)
2575 {
2576     lldb_private::StreamString stream;
2577     stream.PutCString("vFile:mode:");
2578     std::string path (file_spec.GetPath());
2579     stream.PutCStringAsRawHex8(path.c_str());
2580     const char* packet = stream.GetData();
2581     int packet_len = stream.GetSize();
2582     StringExtractorGDBRemote response;
2583     if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2584     {
2585         if (response.GetChar() != 'F')
2586         {
2587             error.SetErrorStringWithFormat ("invalid response to '%s' packet", packet);
2588             return 0;
2589         }
2590         const uint32_t mode = response.GetS32(-1);
2591         if (mode == -1)
2592         {
2593             if (response.GetChar() == ',')
2594             {
2595                 int response_errno = response.GetS32(-1);
2596                 if (response_errno > 0)
2597                     error.SetError(response_errno, lldb::eErrorTypePOSIX);
2598                 else
2599                     error.SetErrorToGenericError();
2600             }
2601         }
2602         else
2603             error.Clear();
2604         return mode & (S_IRWXU|S_IRWXG|S_IRWXO);
2605     }
2606     else
2607     {
2608         error.SetErrorStringWithFormat ("failed to send '%s' packet", packet);
2609     }
2610     return 0;
2611 }
2612 
2613 uint64_t
2614 GDBRemoteCommunicationClient::ReadFile (lldb::user_id_t fd,
2615                                         uint64_t offset,
2616                                         void *dst,
2617                                         uint64_t dst_len,
2618                                         Error &error)
2619 {
2620     lldb_private::StreamString stream;
2621     stream.Printf("vFile:pread:%i,%" PRId64 ",%" PRId64, (int)fd, dst_len, offset);
2622     const char* packet = stream.GetData();
2623     int packet_len = stream.GetSize();
2624     StringExtractorGDBRemote response;
2625     if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2626     {
2627         if (response.GetChar() != 'F')
2628             return 0;
2629         uint32_t retcode = response.GetHexMaxU32(false, UINT32_MAX);
2630         if (retcode == UINT32_MAX)
2631             return retcode;
2632         const char next = (response.Peek() ? *response.Peek() : 0);
2633         if (next == ',')
2634             return 0;
2635         if (next == ';')
2636         {
2637             response.GetChar(); // skip the semicolon
2638             std::string buffer;
2639             if (response.GetEscapedBinaryData(buffer))
2640             {
2641                 const uint64_t data_to_write = std::min<uint64_t>(dst_len, buffer.size());
2642                 if (data_to_write > 0)
2643                     memcpy(dst, &buffer[0], data_to_write);
2644                 return data_to_write;
2645             }
2646         }
2647     }
2648     return 0;
2649 }
2650 
2651 uint64_t
2652 GDBRemoteCommunicationClient::WriteFile (lldb::user_id_t fd,
2653                                          uint64_t offset,
2654                                          const void* src,
2655                                          uint64_t src_len,
2656                                          Error &error)
2657 {
2658     lldb_private::StreamGDBRemote stream;
2659     stream.Printf("vFile:pwrite:%i,%" PRId64 ",", (int)fd, offset);
2660     stream.PutEscapedBytes(src, src_len);
2661     const char* packet = stream.GetData();
2662     int packet_len = stream.GetSize();
2663     StringExtractorGDBRemote response;
2664     if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2665     {
2666         if (response.GetChar() != 'F')
2667         {
2668             error.SetErrorStringWithFormat("write file failed");
2669             return 0;
2670         }
2671         uint64_t bytes_written = response.GetU64(UINT64_MAX);
2672         if (bytes_written == UINT64_MAX)
2673         {
2674             error.SetErrorToGenericError();
2675             if (response.GetChar() == ',')
2676             {
2677                 int response_errno = response.GetS32(-1);
2678                 if (response_errno > 0)
2679                     error.SetError(response_errno, lldb::eErrorTypePOSIX);
2680             }
2681             return 0;
2682         }
2683         return bytes_written;
2684     }
2685     else
2686     {
2687         error.SetErrorString ("failed to send vFile:pwrite packet");
2688     }
2689     return 0;
2690 }
2691 
2692 // Extension of host I/O packets to get whether a file exists.
2693 bool
2694 GDBRemoteCommunicationClient::GetFileExists (const lldb_private::FileSpec& file_spec)
2695 {
2696     lldb_private::StreamString stream;
2697     stream.PutCString("vFile:exists:");
2698     std::string path (file_spec.GetPath());
2699     stream.PutCStringAsRawHex8(path.c_str());
2700     const char* packet = stream.GetData();
2701     int packet_len = stream.GetSize();
2702     StringExtractorGDBRemote response;
2703     if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2704     {
2705         if (response.GetChar() != 'F')
2706             return false;
2707         if (response.GetChar() != ',')
2708             return false;
2709         bool retcode = (response.GetChar() != '0');
2710         return retcode;
2711     }
2712     return false;
2713 }
2714 
2715 bool
2716 GDBRemoteCommunicationClient::CalculateMD5 (const lldb_private::FileSpec& file_spec,
2717                                             uint64_t &high,
2718                                             uint64_t &low)
2719 {
2720     lldb_private::StreamString stream;
2721     stream.PutCString("vFile:MD5:");
2722     std::string path (file_spec.GetPath());
2723     stream.PutCStringAsRawHex8(path.c_str());
2724     const char* packet = stream.GetData();
2725     int packet_len = stream.GetSize();
2726     StringExtractorGDBRemote response;
2727     if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2728     {
2729         if (response.GetChar() != 'F')
2730             return false;
2731         if (response.GetChar() != ',')
2732             return false;
2733         if (response.Peek() && *response.Peek() == 'x')
2734             return false;
2735         low = response.GetHexMaxU64(false, UINT64_MAX);
2736         high = response.GetHexMaxU64(false, UINT64_MAX);
2737         return true;
2738     }
2739     return false;
2740 }
2741