blob: 6a53a11152d546262f8e96e19d2caea96196f667 [file] [log] [blame]
Greg Clayton576d8832011-03-22 04:00:09 +00001//===-- 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
Daniel Maleab89d0492013-08-28 16:06:16 +000014#include <sys/stat.h>
15
Greg Clayton576d8832011-03-22 04:00:09 +000016// C++ Includes
Han Ming Ong4b6459f2013-01-18 23:11:53 +000017#include <sstream>
18
Greg Clayton576d8832011-03-22 04:00:09 +000019// 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"
Daniel Maleae0f8f572013-08-26 23:57:52 +000025#include "lldb/Core/StreamGDBRemote.h"
Greg Clayton576d8832011-03-22 04:00:09 +000026#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"
Virgile Bellob2f1fb22013-08-23 12:44:05 +000035#include "lldb/Host/Config.h"
Greg Clayton576d8832011-03-22 04:00:09 +000036
37using namespace lldb;
38using namespace lldb_private;
39
Virgile Bellob2f1fb22013-08-23 12:44:05 +000040#ifdef LLDB_DISABLE_POSIX
41#define SIGSTOP 17
42#endif
43
Greg Clayton576d8832011-03-22 04:00:09 +000044//----------------------------------------------------------------------
45// GDBRemoteCommunicationClient constructor
46//----------------------------------------------------------------------
Greg Clayton8b82f082011-04-12 05:54:46 +000047GDBRemoteCommunicationClient::GDBRemoteCommunicationClient(bool is_platform) :
48 GDBRemoteCommunication("gdb-remote.client", "gdb-remote.client.rx_packet", is_platform),
Greg Clayton576d8832011-03-22 04:00:09 +000049 m_supports_not_sending_acks (eLazyBoolCalculate),
50 m_supports_thread_suffix (eLazyBoolCalculate),
Greg Clayton44633992012-04-10 03:22:03 +000051 m_supports_threads_in_stop_reply (eLazyBoolCalculate),
Greg Clayton576d8832011-03-22 04:00:09 +000052 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),
Greg Clayton32e0a752011-03-30 18:16:51 +000058 m_qHostInfo_is_valid (eLazyBoolCalculate),
Jason Molendaf17b5ac2012-12-19 02:54:03 +000059 m_qProcessInfo_is_valid (eLazyBoolCalculate),
Greg Clayton70b57652011-05-15 01:25:55 +000060 m_supports_alloc_dealloc_memory (eLazyBoolCalculate),
Greg Clayton46fb5582011-11-18 07:03:08 +000061 m_supports_memory_region_info (eLazyBoolCalculate),
Johnny Chen64637202012-05-23 21:09:52 +000062 m_supports_watchpoint_support_info (eLazyBoolCalculate),
Jim Inghamacff8952013-05-02 00:27:30 +000063 m_supports_detach_stay_stopped (eLazyBoolCalculate),
Enrico Granataf04a2192012-07-13 23:18:48 +000064 m_watchpoints_trigger_after_instruction(eLazyBoolCalculate),
Jim Inghamcd16df92012-07-20 21:37:13 +000065 m_attach_or_wait_reply(eLazyBoolCalculate),
Jim Ingham279ceec2012-07-25 21:12:43 +000066 m_prepare_for_reg_writing_reply (eLazyBoolCalculate),
Eric Christopher2490f5c2013-08-30 17:50:57 +000067 m_supports_p (eLazyBoolCalculate),
Greg Clayton32e0a752011-03-30 18:16:51 +000068 m_supports_qProcessInfoPID (true),
69 m_supports_qfProcessInfo (true),
70 m_supports_qUserName (true),
71 m_supports_qGroupName (true),
Greg Clayton8b82f082011-04-12 05:54:46 +000072 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),
Johnny Chen64637202012-05-23 21:09:52 +000080 m_num_supported_hardware_watchpoints (0),
Greg Clayton576d8832011-03-22 04:00:09 +000081 m_async_mutex (Mutex::eMutexTypeRecursive),
82 m_async_packet_predicate (false),
83 m_async_packet (),
84 m_async_response (),
85 m_async_signal (-1),
Han Ming Ong4b6459f2013-01-18 23:11:53 +000086 m_thread_id_to_used_usec_map (),
Greg Clayton1cb64962011-03-24 04:28:38 +000087 m_host_arch(),
Jason Molendaf17b5ac2012-12-19 02:54:03 +000088 m_process_arch(),
Greg Clayton1cb64962011-03-24 04:28:38 +000089 m_os_version_major (UINT32_MAX),
90 m_os_version_minor (UINT32_MAX),
91 m_os_version_update (UINT32_MAX)
Greg Clayton576d8832011-03-22 04:00:09 +000092{
Greg Clayton576d8832011-03-22 04:00:09 +000093}
94
95//----------------------------------------------------------------------
96// Destructor
97//----------------------------------------------------------------------
98GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient()
99{
Greg Clayton576d8832011-03-22 04:00:09 +0000100 if (IsConnected())
Greg Clayton576d8832011-03-22 04:00:09 +0000101 Disconnect();
Greg Clayton576d8832011-03-22 04:00:09 +0000102}
103
104bool
Greg Clayton1cb64962011-03-24 04:28:38 +0000105GDBRemoteCommunicationClient::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())
Greg Clayton73bf5db2011-06-17 01:22:15 +0000110 return true;
Greg Clayton1cb64962011-03-24 04:28:38 +0000111
112 if (error_ptr)
113 error_ptr->SetErrorString("failed to send the handshake ack");
114 return false;
115}
116
117void
118GDBRemoteCommunicationClient::QueryNoAckModeSupported ()
Greg Clayton576d8832011-03-22 04:00:09 +0000119{
120 if (m_supports_not_sending_acks == eLazyBoolCalculate)
121 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000122 m_send_acks = true;
Greg Clayton576d8832011-03-22 04:00:09 +0000123 m_supports_not_sending_acks = eLazyBoolNo;
Greg Clayton1cb64962011-03-24 04:28:38 +0000124
125 StringExtractorGDBRemote response;
Greg Clayton576d8832011-03-22 04:00:09 +0000126 if (SendPacketAndWaitForResponse("QStartNoAckMode", response, false))
127 {
128 if (response.IsOKResponse())
Greg Clayton1cb64962011-03-24 04:28:38 +0000129 {
130 m_send_acks = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000131 m_supports_not_sending_acks = eLazyBoolYes;
Greg Clayton1cb64962011-03-24 04:28:38 +0000132 }
Greg Clayton576d8832011-03-22 04:00:09 +0000133 }
134 }
Greg Clayton576d8832011-03-22 04:00:09 +0000135}
136
137void
Greg Clayton44633992012-04-10 03:22:03 +0000138GDBRemoteCommunicationClient::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
Jim Inghamcd16df92012-07-20 21:37:13 +0000153bool
154GDBRemoteCommunicationClient::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
Jim Ingham279ceec2012-07-25 21:12:43 +0000173bool
174GDBRemoteCommunicationClient::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
Greg Clayton44633992012-04-10 03:22:03 +0000193
194void
Greg Clayton576d8832011-03-22 04:00:09 +0000195GDBRemoteCommunicationClient::ResetDiscoverableSettings()
196{
197 m_supports_not_sending_acks = eLazyBoolCalculate;
198 m_supports_thread_suffix = eLazyBoolCalculate;
Greg Clayton44633992012-04-10 03:22:03 +0000199 m_supports_threads_in_stop_reply = eLazyBoolCalculate;
Greg Clayton576d8832011-03-22 04:00:09 +0000200 m_supports_vCont_c = eLazyBoolCalculate;
201 m_supports_vCont_C = eLazyBoolCalculate;
202 m_supports_vCont_s = eLazyBoolCalculate;
203 m_supports_vCont_S = eLazyBoolCalculate;
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000204 m_supports_p = eLazyBoolCalculate;
Greg Clayton32e0a752011-03-30 18:16:51 +0000205 m_qHostInfo_is_valid = eLazyBoolCalculate;
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000206 m_qProcessInfo_is_valid = eLazyBoolCalculate;
Greg Clayton70b57652011-05-15 01:25:55 +0000207 m_supports_alloc_dealloc_memory = eLazyBoolCalculate;
Greg Clayton46fb5582011-11-18 07:03:08 +0000208 m_supports_memory_region_info = eLazyBoolCalculate;
Jim Ingham279ceec2012-07-25 21:12:43 +0000209 m_prepare_for_reg_writing_reply = eLazyBoolCalculate;
210 m_attach_or_wait_reply = eLazyBoolCalculate;
Greg Clayton2a48f522011-05-14 01:50:35 +0000211
Greg Clayton32e0a752011-03-30 18:16:51 +0000212 m_supports_qProcessInfoPID = true;
213 m_supports_qfProcessInfo = true;
214 m_supports_qUserName = true;
215 m_supports_qGroupName = true;
Greg Clayton8b82f082011-04-12 05:54:46 +0000216 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;
Greg Claytond314e812011-03-23 00:09:55 +0000222 m_host_arch.Clear();
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000223 m_process_arch.Clear();
Greg Clayton576d8832011-03-22 04:00:09 +0000224}
225
226
227bool
228GDBRemoteCommunicationClient::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}
242bool
243GDBRemoteCommunicationClient::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
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000300// 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.
Sean Callananb1de1142013-09-04 23:24:15 +0000303//
304// Takes a valid thread ID because p needs to apply to a thread.
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000305bool
Sean Callananb1de1142013-09-04 23:24:15 +0000306GDBRemoteCommunicationClient::GetpPacketSupported (lldb::tid_t tid)
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000307{
308 if (m_supports_p == eLazyBoolCalculate)
309 {
310 StringExtractorGDBRemote response;
311 m_supports_p = eLazyBoolNo;
Sean Callananb1de1142013-09-04 23:24:15 +0000312 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))
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000319 {
320 if (response.IsNormalResponse())
321 m_supports_p = eLazyBoolYes;
322 }
323 }
324 return m_supports_p;
325}
Greg Clayton576d8832011-03-22 04:00:09 +0000326
327size_t
328GDBRemoteCommunicationClient::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
341size_t
342GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
343(
344 const char *payload,
345 size_t payload_length,
346 StringExtractorGDBRemote &response,
347 bool send_async
348)
349{
350 Mutex::Locker locker;
Greg Clayton5160ce52013-03-27 23:08:40 +0000351 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton644247c2011-07-07 01:59:51 +0000352 size_t response_len = 0;
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000353 if (GetSequenceMutex (locker))
Greg Clayton576d8832011-03-22 04:00:09 +0000354 {
Greg Clayton5fe15d22011-05-20 03:15:54 +0000355 if (SendPacketNoLock (payload, payload_length))
Greg Clayton644247c2011-07-07 01:59:51 +0000356 response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
357 else
358 {
359 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000360 log->Printf("error: failed to send '%*s'", (int) payload_length, payload);
Greg Clayton644247c2011-07-07 01:59:51 +0000361 }
Greg Clayton576d8832011-03-22 04:00:09 +0000362 }
363 else
364 {
365 if (send_async)
366 {
Greg Claytond3544052012-05-31 21:24:20 +0000367 if (IsRunning())
Greg Clayton576d8832011-03-22 04:00:09 +0000368 {
Greg Claytond3544052012-05-31 21:24:20 +0000369 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))
Greg Clayton576d8832011-03-22 04:00:09 +0000378 {
Greg Claytond3544052012-05-31 21:24:20 +0000379 if (m_interrupt_sent)
Greg Clayton576d8832011-03-22 04:00:09 +0000380 {
Jim Inghambabfc382012-06-06 00:32:39 +0000381 m_interrupt_sent = false;
Greg Claytond3544052012-05-31 21:24:20 +0000382 TimeValue timeout_time;
383 timeout_time = TimeValue::Now();
384 timeout_time.OffsetWithSeconds (m_packet_timeout);
385
Greg Clayton576d8832011-03-22 04:00:09 +0000386 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000387 log->Printf ("async: sent interrupt");
Greg Clayton644247c2011-07-07 01:59:51 +0000388
Greg Claytond3544052012-05-31 21:24:20 +0000389 if (m_async_packet_predicate.WaitForValueEqualTo (false, &timeout_time, &timed_out))
Greg Claytone889ad62011-10-27 22:04:16 +0000390 {
Greg Claytond3544052012-05-31 21:24:20 +0000391 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");
Greg Claytone889ad62011-10-27 22:04:16 +0000419 }
420 }
421 else
422 {
Greg Claytond3544052012-05-31 21:24:20 +0000423 // 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?
Greg Clayton576d8832011-03-22 04:00:09 +0000426 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000427 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 }
Greg Clayton576d8832011-03-22 04:00:09 +0000436 }
437 }
438 else
439 {
Greg Clayton644247c2011-07-07 01:59:51 +0000440 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000441 log->Printf ("async: failed to interrupt");
Greg Clayton576d8832011-03-22 04:00:09 +0000442 }
443 }
444 else
445 {
446 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000447 log->Printf ("async: not running, async is ignored");
Greg Clayton576d8832011-03-22 04:00:09 +0000448 }
449 }
450 else
451 {
452 if (log)
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000453 log->Printf("error: failed to get packet sequence mutex, not sending packet '%*s'", (int) payload_length, payload);
Greg Clayton576d8832011-03-22 04:00:09 +0000454 }
455 }
Greg Clayton644247c2011-07-07 01:59:51 +0000456 if (response_len == 0)
457 {
458 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000459 log->Printf("error: failed to get response for '%*s'", (int) payload_length, payload);
Greg Clayton644247c2011-07-07 01:59:51 +0000460 }
461 return response_len;
Greg Clayton576d8832011-03-22 04:00:09 +0000462}
463
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000464static const char *end_delimiter = "--end--;";
465static const int end_delimiter_len = 8;
466
467std::string
468GDBRemoteCommunicationClient::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
Greg Clayton576d8832011-03-22 04:00:09 +0000557StateType
558GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse
559(
560 ProcessGDBRemote *process,
561 const char *payload,
562 size_t packet_length,
563 StringExtractorGDBRemote &response
564)
565{
Greg Clayton1f5181a2012-07-02 22:05:25 +0000566 m_curr_tid = LLDB_INVALID_THREAD_ID;
Greg Clayton5160ce52013-03-27 23:08:40 +0000567 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton576d8832011-03-22 04:00:09 +0000568 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
Jim Inghambabfc382012-06-06 00:32:39 +0000577 // may change if we are interrupted and we continue after an async packet...
Greg Clayton576d8832011-03-22 04:00:09 +0000578 std::string continue_packet(payload, packet_length);
579
Greg Clayton3f875c52013-02-22 22:23:55 +0000580 bool got_async_packet = false;
Greg Claytonaf247d72011-05-19 03:54:16 +0000581
Greg Clayton576d8832011-03-22 04:00:09 +0000582 while (state == eStateRunning)
583 {
Greg Clayton3f875c52013-02-22 22:23:55 +0000584 if (!got_async_packet)
Greg Claytonaf247d72011-05-19 03:54:16 +0000585 {
586 if (log)
587 log->Printf ("GDBRemoteCommunicationClient::%s () sending continue packet: %s", __FUNCTION__, continue_packet.c_str());
Greg Clayton37a0a242012-04-11 00:24:49 +0000588 if (SendPacketNoLock(continue_packet.c_str(), continue_packet.size()) == 0)
Greg Claytonaf247d72011-05-19 03:54:16 +0000589 state = eStateInvalid;
Greg Clayton576d8832011-03-22 04:00:09 +0000590
Greg Claytone889ad62011-10-27 22:04:16 +0000591 m_private_is_running.SetValue (true, eBroadcastAlways);
Greg Claytonaf247d72011-05-19 03:54:16 +0000592 }
593
Greg Clayton3f875c52013-02-22 22:23:55 +0000594 got_async_packet = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000595
596 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000597 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(%s)", __FUNCTION__, continue_packet.c_str());
Greg Clayton576d8832011-03-22 04:00:09 +0000598
Greg Clayton37a0a242012-04-11 00:24:49 +0000599 if (WaitForPacketWithTimeoutMicroSecondsNoLock(response, UINT32_MAX))
Greg Clayton576d8832011-03-22 04:00:09 +0000600 {
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':
Greg Clayton576d8832011-03-22 04:00:09 +0000612 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000613 if (process->GetStopID() == 0)
Greg Clayton576d8832011-03-22 04:00:09 +0000614 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000615 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);
Greg Clayton576d8832011-03-22 04:00:09 +0000622 }
Greg Clayton2687cd12012-03-29 01:55:41 +0000623
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
Jim Inghambabfc382012-06-06 00:32:39 +0000632 bool continue_after_async = m_async_signal != -1 || m_async_packet_predicate.GetValue();
633 if (continue_after_async || m_interrupt_sent)
Greg Clayton2687cd12012-03-29 01:55:41 +0000634 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000635 // 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 {
Greg Claytonfb72fde2012-05-15 02:50:49 +0000649 continue_after_async = false;
Greg Clayton2687cd12012-03-29 01:55:41 +0000650
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...
Greg Claytonfb72fde2012-05-15 02:50:49 +0000666 continue_after_async = false;
Greg Clayton2687cd12012-03-29 01:55:41 +0000667 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
Greg Claytonfb72fde2012-05-15 02:50:49 +0000710 // interrupt didn't cause our stop (ignore continue_after_async)
Greg Clayton2687cd12012-03-29 01:55:41 +0000711 continue_packet.assign(signal_packet, signal_packet_len);
712 continue;
713 }
714 }
715 else if (m_async_packet_predicate.GetValue())
716 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000717 Log * packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Greg Clayton2687cd12012-03-29 01:55:41 +0000718
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)
Greg Claytonfb72fde2012-05-15 02:50:49 +0000744 packet_log->Printf ("async: sent packet, continue_after_async = %i", continue_after_async);
Greg Clayton2687cd12012-03-29 01:55:41 +0000745
746 // Set the continue packet to resume if our interrupt
747 // for the async packet did cause the stop
Greg Claytonfb72fde2012-05-15 02:50:49 +0000748 if (continue_after_async)
Greg Clayton2687cd12012-03-29 01:55:41 +0000749 {
Greg Claytonf1186de2012-05-24 23:42:14 +0000750 // 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');
Greg Clayton2687cd12012-03-29 01:55:41 +0000760 continue;
761 }
762 }
763 // Stop with signal and thread info
764 state = eStateStopped;
Greg Clayton576d8832011-03-22 04:00:09 +0000765 }
Greg Clayton576d8832011-03-22 04:00:09 +0000766 break;
767
768 case 'W':
769 case 'X':
770 // process exited
771 state = eStateExited;
772 break;
773
774 case 'O':
775 // STDOUT
776 {
Greg Clayton3f875c52013-02-22 22:23:55 +0000777 got_async_packet = true;
Greg Clayton576d8832011-03-22 04:00:09 +0000778 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
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000787 case 'A':
788 // Async miscellaneous reply. Right now, only profile data is coming through this channel.
789 {
Greg Clayton3f875c52013-02-22 22:23:55 +0000790 got_async_packet = true;
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000791 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());
Han Ming Ong91ed6b82013-06-24 18:15:05 +0000803 std::string profile_data = HarmonizeThreadIdsForProfileData(process, profileDataExtractor);
804 process->BroadcastAsyncProfileData (profile_data);
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000805
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 }
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000814 }
815 break;
816
Greg Clayton576d8832011-03-22 04:00:09 +0000817 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
845bool
846GDBRemoteCommunicationClient::SendAsyncSignal (int signo)
847{
Greg Clayton2687cd12012-03-29 01:55:41 +0000848 Mutex::Locker async_locker (m_async_mutex);
Greg Clayton576d8832011-03-22 04:00:09 +0000849 m_async_signal = signo;
850 bool timed_out = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000851 Mutex::Locker locker;
Greg Clayton2687cd12012-03-29 01:55:41 +0000852 if (SendInterrupt (locker, 1, timed_out))
Greg Clayton576d8832011-03-22 04:00:09 +0000853 return true;
854 m_async_signal = -1;
855 return false;
856}
857
Greg Clayton37a0a242012-04-11 00:24:49 +0000858// This function takes a mutex locker as a parameter in case the GetSequenceMutex
Greg Clayton576d8832011-03-22 04:00:09 +0000859// 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
868bool
Greg Clayton2687cd12012-03-29 01:55:41 +0000869GDBRemoteCommunicationClient::SendInterrupt
Greg Clayton576d8832011-03-22 04:00:09 +0000870(
871 Mutex::Locker& locker,
872 uint32_t seconds_to_wait_for_stop,
Greg Clayton576d8832011-03-22 04:00:09 +0000873 bool &timed_out
874)
875{
Greg Clayton576d8832011-03-22 04:00:09 +0000876 timed_out = false;
Greg Clayton5160ce52013-03-27 23:08:40 +0000877 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Clayton576d8832011-03-22 04:00:09 +0000878
879 if (IsRunning())
880 {
881 // Only send an interrupt if our debugserver is running...
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000882 if (GetSequenceMutex (locker))
Greg Clayton37a0a242012-04-11 00:24:49 +0000883 {
884 if (log)
885 log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt");
886 }
887 else
Greg Clayton576d8832011-03-22 04:00:09 +0000888 {
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;
Greg Clayton576d8832011-03-22 04:00:09 +0000893 size_t bytes_written = Write (&ctrl_c, 1, status, NULL);
Greg Clayton2687cd12012-03-29 01:55:41 +0000894 if (log)
895 log->PutCString("send packet: \\x03");
Greg Clayton576d8832011-03-22 04:00:09 +0000896 if (bytes_written > 0)
897 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000898 m_interrupt_sent = true;
Greg Clayton576d8832011-03-22 04:00:09 +0000899 if (seconds_to_wait_for_stop)
900 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000901 TimeValue timeout;
902 if (seconds_to_wait_for_stop)
903 {
904 timeout = TimeValue::Now();
905 timeout.OffsetWithSeconds (seconds_to_wait_for_stop);
906 }
Greg Clayton576d8832011-03-22 04:00:09 +0000907 if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out))
908 {
909 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +0000910 log->PutCString ("SendInterrupt () - sent interrupt, private state stopped");
Greg Clayton576d8832011-03-22 04:00:09 +0000911 return true;
912 }
913 else
914 {
915 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +0000916 log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume");
Greg Clayton576d8832011-03-22 04:00:09 +0000917 }
918 }
919 else
920 {
921 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +0000922 log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop...");
Greg Clayton576d8832011-03-22 04:00:09 +0000923 return true;
924 }
925 }
926 else
927 {
928 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +0000929 log->Printf ("SendInterrupt () - failed to write interrupt");
Greg Clayton576d8832011-03-22 04:00:09 +0000930 }
931 return false;
932 }
Greg Clayton576d8832011-03-22 04:00:09 +0000933 }
Greg Clayton2687cd12012-03-29 01:55:41 +0000934 else
935 {
936 if (log)
937 log->Printf ("SendInterrupt () - not running");
938 }
Greg Clayton576d8832011-03-22 04:00:09 +0000939 return true;
940}
941
942lldb::pid_t
943GDBRemoteCommunicationClient::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
955bool
956GDBRemoteCommunicationClient::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 {
Jim Ingham98d6da52012-06-28 20:30:23 +0000976 error_str.assign ("timed out waiting for app to launch");
Greg Clayton576d8832011-03-22 04:00:09 +0000977 }
978 return false;
979}
980
981int
982GDBRemoteCommunicationClient::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
1011int
1012GDBRemoteCommunicationClient::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
Greg Claytonc4103b32011-05-08 04:53:50 +00001031int
1032GDBRemoteCommunicationClient::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
Greg Clayton576d8832011-03-22 04:00:09 +00001051bool
Greg Clayton1cb64962011-03-24 04:28:38 +00001052GDBRemoteCommunicationClient::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
1069bool
1070GDBRemoteCommunicationClient::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
1085bool
1086GDBRemoteCommunicationClient::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
1100bool
1101GDBRemoteCommunicationClient::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
1115ArchSpec
1116GDBRemoteCommunicationClient::GetSystemArchitecture ()
1117{
1118 if (GetHostInfo ())
1119 return m_host_arch;
1120 return ArchSpec();
1121}
1122
Jason Molendaf17b5ac2012-12-19 02:54:03 +00001123const lldb_private::ArchSpec &
1124GDBRemoteCommunicationClient::GetProcessArchitecture ()
1125{
1126 if (m_qProcessInfo_is_valid == eLazyBoolCalculate)
1127 GetCurrentProcessInfo ();
1128 return m_process_arch;
1129}
1130
Greg Clayton1cb64962011-03-24 04:28:38 +00001131
1132bool
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00001133GDBRemoteCommunicationClient::GetHostInfo (bool force)
Greg Clayton576d8832011-03-22 04:00:09 +00001134{
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00001135 if (force || m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton576d8832011-03-22 04:00:09 +00001136 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001137 m_qHostInfo_is_valid = eLazyBoolNo;
Greg Clayton576d8832011-03-22 04:00:09 +00001138 StringExtractorGDBRemote response;
1139 if (SendPacketAndWaitForResponse ("qHostInfo", response, false))
1140 {
Greg Clayton17a0cb62011-05-15 23:46:54 +00001141 if (response.IsNormalResponse())
Greg Claytond314e812011-03-23 00:09:55 +00001142 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001143 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))
Greg Claytond314e812011-03-23 00:09:55 +00001156 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001157 if (name.compare("cputype") == 0)
Greg Clayton1cb64962011-03-24 04:28:38 +00001158 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001159 // 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 }
Enrico Granataf04a2192012-07-13 23:18:48 +00001242 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
Greg Clayton32e0a752011-03-30 18:16:51 +00001253 }
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 }
Greg Clayton70512312012-05-08 01:45:38 +00001273
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 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001287 if (!vendor_name.empty())
1288 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1289 if (!os_name.empty())
Greg Claytone1dadb82011-09-15 00:21:03 +00001290 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Greg Clayton32e0a752011-03-30 18:16:51 +00001291
1292 }
1293 }
1294 else
1295 {
1296 std::string triple;
1297 triple += arch_name;
Greg Clayton70512312012-05-08 01:45:38 +00001298 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 }
Greg Clayton1cb64962011-03-24 04:28:38 +00001327 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 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001335
Greg Clayton1cb64962011-03-24 04:28:38 +00001336 }
1337 }
1338 else
1339 {
Greg Clayton70512312012-05-08 01:45:38 +00001340 m_host_arch.SetTriple (triple.c_str());
Greg Claytond314e812011-03-23 00:09:55 +00001341 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 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001349 }
Greg Claytond314e812011-03-23 00:09:55 +00001350 }
Greg Clayton576d8832011-03-22 04:00:09 +00001351 }
1352 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001353 return m_qHostInfo_is_valid == eLazyBoolYes;
Greg Clayton576d8832011-03-22 04:00:09 +00001354}
1355
1356int
1357GDBRemoteCommunicationClient::SendAttach
1358(
1359 lldb::pid_t pid,
1360 StringExtractorGDBRemote& response
1361)
1362{
1363 if (pid != LLDB_INVALID_PROCESS_ID)
1364 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001365 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001366 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, pid);
Andy Gibbsa297a972013-06-19 19:04:53 +00001367 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00001368 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton576d8832011-03-22 04:00:09 +00001369 {
1370 if (response.IsErrorResponse())
1371 return response.GetError();
1372 return 0;
1373 }
1374 }
1375 return -1;
1376}
1377
1378const lldb_private::ArchSpec &
1379GDBRemoteCommunicationClient::GetHostArchitecture ()
1380{
Greg Clayton32e0a752011-03-30 18:16:51 +00001381 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton576d8832011-03-22 04:00:09 +00001382 GetHostInfo ();
Greg Claytond314e812011-03-23 00:09:55 +00001383 return m_host_arch;
Greg Clayton576d8832011-03-22 04:00:09 +00001384}
1385
1386addr_t
1387GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions)
1388{
Greg Clayton70b57652011-05-15 01:25:55 +00001389 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton576d8832011-03-22 04:00:09 +00001390 {
Greg Clayton70b57652011-05-15 01:25:55 +00001391 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton2a48f522011-05-14 01:50:35 +00001392 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001393 const int packet_len = ::snprintf (packet, sizeof(packet), "_M%" PRIx64 ",%s%s%s",
Greg Clayton43e0af02012-09-18 18:04:04 +00001394 (uint64_t)size,
Greg Clayton2a48f522011-05-14 01:50:35 +00001395 permissions & lldb::ePermissionsReadable ? "r" : "",
1396 permissions & lldb::ePermissionsWritable ? "w" : "",
1397 permissions & lldb::ePermissionsExecutable ? "x" : "");
Andy Gibbsa297a972013-06-19 19:04:53 +00001398 assert (packet_len < (int)sizeof(packet));
Greg Clayton2a48f522011-05-14 01:50:35 +00001399 StringExtractorGDBRemote response;
1400 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1401 {
Greg Clayton17a0cb62011-05-15 23:46:54 +00001402 if (!response.IsErrorResponse())
Greg Clayton2a48f522011-05-14 01:50:35 +00001403 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1404 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00001405 else
1406 {
1407 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1408 }
Greg Clayton576d8832011-03-22 04:00:09 +00001409 }
1410 return LLDB_INVALID_ADDRESS;
1411}
1412
1413bool
1414GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr)
1415{
Greg Clayton70b57652011-05-15 01:25:55 +00001416 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton576d8832011-03-22 04:00:09 +00001417 {
Greg Clayton70b57652011-05-15 01:25:55 +00001418 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton2a48f522011-05-14 01:50:35 +00001419 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001420 const int packet_len = ::snprintf(packet, sizeof(packet), "_m%" PRIx64, (uint64_t)addr);
Andy Gibbsa297a972013-06-19 19:04:53 +00001421 assert (packet_len < (int)sizeof(packet));
Greg Clayton2a48f522011-05-14 01:50:35 +00001422 StringExtractorGDBRemote response;
1423 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1424 {
1425 if (response.IsOKResponse())
1426 return true;
Greg Clayton17a0cb62011-05-15 23:46:54 +00001427 }
1428 else
1429 {
1430 m_supports_alloc_dealloc_memory = eLazyBoolNo;
Greg Clayton2a48f522011-05-14 01:50:35 +00001431 }
Greg Clayton576d8832011-03-22 04:00:09 +00001432 }
1433 return false;
1434}
1435
Jim Inghamacff8952013-05-02 00:27:30 +00001436Error
1437GDBRemoteCommunicationClient::Detach (bool keep_stopped)
Greg Clayton37a0a242012-04-11 00:24:49 +00001438{
Jim Inghamacff8952013-05-02 00:27:30 +00001439 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:");
Andy Gibbsa297a972013-06-19 19:04:53 +00001447 assert (packet_len < (int)sizeof(packet));
Jim Inghamacff8952013-05-02 00:27:30 +00001448 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;
Greg Clayton37a0a242012-04-11 00:24:49 +00001478}
1479
Greg Clayton46fb5582011-11-18 07:03:08 +00001480Error
1481GDBRemoteCommunicationClient::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];
Daniel Malead01b2952012-11-29 21:49:15 +00001491 const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%" PRIx64, (uint64_t)addr);
Andy Gibbsa297a972013-06-19 19:04:53 +00001492 assert (packet_len < (int)sizeof(packet));
Greg Clayton46fb5582011-11-18 07:03:08 +00001493 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;
Jason Molendacb349ee2011-12-13 05:39:38 +00001500 bool saw_permissions = false;
Greg Clayton46fb5582011-11-18 07:03:08 +00001501 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 }
Jason Molendacb349ee2011-12-13 05:39:38 +00001515 else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid())
Greg Clayton46fb5582011-11-18 07:03:08 +00001516 {
Jason Molendacb349ee2011-12-13 05:39:38 +00001517 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 }
Greg Clayton46fb5582011-11-18 07:03:08 +00001542 }
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 }
Jason Molendacb349ee2011-12-13 05:39:38 +00001553
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 }
Greg Clayton46fb5582011-11-18 07:03:08 +00001561 }
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
Johnny Chen64637202012-05-23 21:09:52 +00001578Error
1579GDBRemoteCommunicationClient::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:");
Andy Gibbsa297a972013-06-19 19:04:53 +00001595 assert (packet_len < (int)sizeof(packet));
Johnny Chen64637202012-05-23 21:09:52 +00001596 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}
Greg Clayton46fb5582011-11-18 07:03:08 +00001624
Enrico Granataf04a2192012-07-13 23:18:48 +00001625lldb_private::Error
1626GDBRemoteCommunicationClient::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
1634lldb_private::Error
1635GDBRemoteCommunicationClient::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
Greg Clayton576d8832011-03-22 04:00:09 +00001649int
1650GDBRemoteCommunicationClient::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
1671int
1672GDBRemoteCommunicationClient::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
1693int
1694GDBRemoteCommunicationClient::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
1715int
1716GDBRemoteCommunicationClient::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
1737int
1738GDBRemoteCommunicationClient::SetDisableASLR (bool enable)
1739{
Greg Clayton32e0a752011-03-30 18:16:51 +00001740 char packet[32];
1741 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0);
Andy Gibbsa297a972013-06-19 19:04:53 +00001742 assert (packet_len < (int)sizeof(packet));
Greg Clayton576d8832011-03-22 04:00:09 +00001743 StringExtractorGDBRemote response;
Greg Clayton32e0a752011-03-30 18:16:51 +00001744 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton576d8832011-03-22 04:00:09 +00001745 {
1746 if (response.IsOKResponse())
1747 return 0;
1748 uint8_t error = response.GetError();
1749 if (error)
1750 return error;
1751 }
1752 return -1;
1753}
Greg Clayton32e0a752011-03-30 18:16:51 +00001754
1755bool
Greg Clayton8b82f082011-04-12 05:54:46 +00001756GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00001757{
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 {
Greg Clayton8b82f082011-04-12 05:54:46 +00001776 process_info.SetUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00001777 }
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 {
Greg Clayton8b82f082011-04-12 05:54:46 +00001784 process_info.SetGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00001785 }
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);
Greg Clayton70512312012-05-08 01:45:38 +00001796 process_info.GetArchitecture ().SetTriple (value.c_str());
Greg Clayton32e0a752011-03-30 18:16:51 +00001797 }
1798 else if (name.compare("name") == 0)
1799 {
1800 StringExtractor extractor;
Filipe Cabecinhasf86cf782012-05-07 09:30:51 +00001801 // The process name from ASCII hex bytes since we can't
Greg Clayton32e0a752011-03-30 18:16:51 +00001802 // control the characters in a process name
1803 extractor.GetStringRef().swap(value);
1804 extractor.SetFilePos(0);
1805 extractor.GetHexByteString (value);
Greg Clayton144f3a92011-11-15 03:53:30 +00001806 process_info.GetExecutableFile().SetFile (value.c_str(), false);
Greg Clayton32e0a752011-03-30 18:16:51 +00001807 }
1808 }
1809
1810 if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
1811 return true;
1812 }
1813 return false;
1814}
1815
1816bool
Greg Clayton8b82f082011-04-12 05:54:46 +00001817GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00001818{
1819 process_info.Clear();
1820
1821 if (m_supports_qProcessInfoPID)
1822 {
1823 char packet[32];
Daniel Malead01b2952012-11-29 21:49:15 +00001824 const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%" PRIu64, pid);
Andy Gibbsa297a972013-06-19 19:04:53 +00001825 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00001826 StringExtractorGDBRemote response;
1827 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1828 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001829 return DecodeProcessInfoResponse (response, process_info);
1830 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00001831 else
1832 {
1833 m_supports_qProcessInfoPID = false;
1834 return false;
1835 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001836 }
1837 return false;
1838}
1839
Jason Molendaf17b5ac2012-12-19 02:54:03 +00001840bool
1841GDBRemoteCommunicationClient::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
Greg Clayton32e0a752011-03-30 18:16:51 +00001934uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +00001935GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1936 ProcessInstanceInfoList &process_infos)
Greg Clayton32e0a752011-03-30 18:16:51 +00001937{
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())
Daniel Malead01b2952012-11-29 21:49:15 +00001988 packet.Printf("pid:%" PRIu64 ";",match_info.GetProcessInfo().GetProcessID());
Greg Clayton32e0a752011-03-30 18:16:51 +00001989 if (match_info.GetProcessInfo().ParentProcessIDIsValid())
Daniel Malead01b2952012-11-29 21:49:15 +00001990 packet.Printf("parent_pid:%" PRIu64 ";",match_info.GetProcessInfo().GetParentProcessID());
Greg Clayton8b82f082011-04-12 05:54:46 +00001991 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());
Greg Clayton32e0a752011-03-30 18:16:51 +00001995 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 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002013 do
2014 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002015 ProcessInstanceInfo process_info;
Greg Clayton32e0a752011-03-30 18:16:51 +00002016 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 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002023 else
2024 {
2025 m_supports_qfProcessInfo = false;
2026 return 0;
2027 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002028 }
2029 return process_infos.GetSize();
2030
2031}
2032
2033bool
2034GDBRemoteCommunicationClient::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);
Andy Gibbsa297a972013-06-19 19:04:53 +00002040 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00002041 StringExtractorGDBRemote response;
2042 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
2043 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002044 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 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002053 else
2054 {
2055 m_supports_qUserName = false;
2056 return false;
2057 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002058 }
2059 return false;
2060
2061}
2062
2063bool
2064GDBRemoteCommunicationClient::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);
Andy Gibbsa297a972013-06-19 19:04:53 +00002070 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00002071 StringExtractorGDBRemote response;
2072 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
2073 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002074 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 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002083 else
2084 {
2085 m_supports_qGroupName = false;
2086 return false;
2087 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002088 }
2089 return false;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002090}
Greg Clayton32e0a752011-03-30 18:16:51 +00002091
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002092void
2093GDBRemoteCommunicationClient::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();
Peter Collingbourneba23ca02011-06-18 23:52:14 +00002112 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Daniel Malead01b2952012-11-29 21:49:15 +00002113 printf ("%u qSpeedTest(send=%-5u, recv=%-5u) in %" PRIu64 ".%9.9" PRIu64 " sec for %f packets/sec.\n",
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002114 num_packets,
2115 send_size,
2116 recv_size,
Peter Collingbourneba23ca02011-06-18 23:52:14 +00002117 total_time_nsec / TimeValue::NanoSecPerSec,
2118 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002119 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();
Peter Collingbourneba23ca02011-06-18 23:52:14 +00002136 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Daniel Malead01b2952012-11-29 21:49:15 +00002137 printf ("%u 'qC' packets packets in 0x%" PRIu64 "%9.9" PRIu64 " sec for %f packets/sec.\n",
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002138 num_packets,
Peter Collingbourneba23ca02011-06-18 23:52:14 +00002139 total_time_nsec / TimeValue::NanoSecPerSec,
2140 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002141 packets_per_second);
2142 }
2143}
2144
2145bool
2146GDBRemoteCommunicationClient::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;
Greg Clayton17a0cb62011-05-15 23:46:54 +00002166 return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) > 0;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002167 return false;
Greg Clayton32e0a752011-03-30 18:16:51 +00002168}
Greg Clayton8b82f082011-04-12 05:54:46 +00002169
2170uint16_t
Daniel Maleae0f8f572013-08-26 23:57:52 +00002171GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort (lldb::pid_t &pid)
Greg Clayton8b82f082011-04-12 05:54:46 +00002172{
Daniel Maleae0f8f572013-08-26 23:57:52 +00002173 pid = LLDB_INVALID_PROCESS_ID;
Greg Clayton8b82f082011-04-12 05:54:46 +00002174 StringExtractorGDBRemote response;
Daniel Maleae0f8f572013-08-26 23:57:52 +00002175 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))
Greg Clayton8b82f082011-04-12 05:54:46 +00002192 {
2193 std::string name;
2194 std::string value;
2195 uint16_t port = 0;
Greg Clayton8b82f082011-04-12 05:54:46 +00002196 while (response.GetNameColonValue(name, value))
2197 {
Daniel Maleae0f8f572013-08-26 23:57:52 +00002198 if (name.compare("port") == 0)
Greg Clayton8b82f082011-04-12 05:54:46 +00002199 port = Args::StringToUInt32(value.c_str(), 0, 0);
Daniel Maleae0f8f572013-08-26 23:57:52 +00002200 else if (name.compare("pid") == 0)
2201 pid = Args::StringToUInt64(value.c_str(), LLDB_INVALID_PROCESS_ID, 0);
Greg Clayton8b82f082011-04-12 05:54:46 +00002202 }
2203 return port;
2204 }
2205 return 0;
2206}
2207
2208bool
Daniel Maleae0f8f572013-08-26 23:57:52 +00002209GDBRemoteCommunicationClient::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
2225bool
Jason Molendae9ca4af2013-02-23 02:04:45 +00002226GDBRemoteCommunicationClient::SetCurrentThread (uint64_t tid)
Greg Clayton8b82f082011-04-12 05:54:46 +00002227{
2228 if (m_curr_tid == tid)
2229 return true;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002230
Greg Clayton8b82f082011-04-12 05:54:46 +00002231 char packet[32];
2232 int packet_len;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002233 if (tid == UINT64_MAX)
2234 packet_len = ::snprintf (packet, sizeof(packet), "Hg-1");
Greg Clayton8b82f082011-04-12 05:54:46 +00002235 else
Jason Molendae9ca4af2013-02-23 02:04:45 +00002236 packet_len = ::snprintf (packet, sizeof(packet), "Hg%" PRIx64, tid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002237 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002238 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
2250bool
Jason Molendae9ca4af2013-02-23 02:04:45 +00002251GDBRemoteCommunicationClient::SetCurrentThreadForRun (uint64_t tid)
Greg Clayton8b82f082011-04-12 05:54:46 +00002252{
2253 if (m_curr_tid_run == tid)
2254 return true;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002255
Greg Clayton8b82f082011-04-12 05:54:46 +00002256 char packet[32];
2257 int packet_len;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002258 if (tid == UINT64_MAX)
2259 packet_len = ::snprintf (packet, sizeof(packet), "Hc-1");
Greg Clayton8b82f082011-04-12 05:54:46 +00002260 else
Jason Molendae9ca4af2013-02-23 02:04:45 +00002261 packet_len = ::snprintf (packet, sizeof(packet), "Hc%" PRIx64, tid);
2262
Andy Gibbsa297a972013-06-19 19:04:53 +00002263 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002264 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
2276bool
2277GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response)
2278{
2279 if (SendPacketAndWaitForResponse("?", 1, response, false))
2280 return response.IsNormalResponse();
2281 return false;
2282}
2283
2284bool
Greg Claytonf402f782012-10-13 02:11:55 +00002285GDBRemoteCommunicationClient::GetThreadStopInfo (lldb::tid_t tid, StringExtractorGDBRemote &response)
Greg Clayton8b82f082011-04-12 05:54:46 +00002286{
2287 if (m_supports_qThreadStopInfo)
2288 {
2289 char packet[256];
Daniel Malead01b2952012-11-29 21:49:15 +00002290 int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%" PRIx64, tid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002291 assert (packet_len < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002292 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2293 {
Greg Clayton17a0cb62011-05-15 23:46:54 +00002294 if (response.IsNormalResponse())
Greg Clayton8b82f082011-04-12 05:54:46 +00002295 return true;
2296 else
2297 return false;
2298 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002299 else
2300 {
2301 m_supports_qThreadStopInfo = false;
2302 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002303 }
Greg Clayton5fe15d22011-05-20 03:15:54 +00002304// if (SetCurrentThread (tid))
2305// return GetStopReply (response);
Greg Clayton8b82f082011-04-12 05:54:46 +00002306 return false;
2307}
2308
2309
2310uint8_t
2311GDBRemoteCommunicationClient::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;
Greg Clayton8b82f082011-04-12 05:54:46 +00002320 }
2321
2322 char packet[64];
2323 const int packet_len = ::snprintf (packet,
2324 sizeof(packet),
Daniel Malead01b2952012-11-29 21:49:15 +00002325 "%c%i,%" PRIx64 ",%x",
Greg Clayton8b82f082011-04-12 05:54:46 +00002326 insert ? 'Z' : 'z',
2327 type,
2328 addr,
2329 length);
2330
Andy Gibbsa297a972013-06-19 19:04:53 +00002331 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002332 StringExtractorGDBRemote response;
2333 if (SendPacketAndWaitForResponse(packet, packet_len, response, true))
2334 {
2335 if (response.IsOKResponse())
2336 return 0;
Greg Clayton8b82f082011-04-12 05:54:46 +00002337 else if (response.IsErrorResponse())
2338 return response.GetError();
2339 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002340 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;
Greg Clayton17a0cb62011-05-15 23:46:54 +00002349 }
2350 }
2351
Greg Clayton8b82f082011-04-12 05:54:46 +00002352 return UINT8_MAX;
2353}
Greg Claytonadc00cb2011-05-20 23:38:13 +00002354
2355size_t
2356GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
2357 bool &sequence_mutex_unavailable)
2358{
2359 Mutex::Locker locker;
2360 thread_ids.clear();
2361
Jim Ingham4ceb9282012-06-08 22:50:40 +00002362 if (GetSequenceMutex (locker, "ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex"))
Greg Claytonadc00cb2011-05-20 23:38:13 +00002363 {
2364 sequence_mutex_unavailable = false;
2365 StringExtractorGDBRemote response;
2366
Greg Clayton73bf5db2011-06-17 01:22:15 +00002367 for (SendPacketNoLock ("qfThreadInfo", strlen("qfThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
Greg Claytonadc00cb2011-05-20 23:38:13 +00002368 response.IsNormalResponse();
Greg Clayton73bf5db2011-06-17 01:22:15 +00002369 SendPacketNoLock ("qsThreadInfo", strlen("qsThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()))
Greg Claytonadc00cb2011-05-20 23:38:13 +00002370 {
2371 char ch = response.GetChar();
2372 if (ch == 'l')
2373 break;
2374 if (ch == 'm')
2375 {
2376 do
2377 {
Jason Molendae9ca4af2013-02-23 02:04:45 +00002378 tid_t tid = response.GetHexMaxU64(false, LLDB_INVALID_THREAD_ID);
Greg Claytonadc00cb2011-05-20 23:38:13 +00002379
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 {
Jim Ingham4ceb9282012-06-08 22:50:40 +00002391#if defined (LLDB_CONFIGURATION_DEBUG)
2392 // assert(!"ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex");
2393#else
Greg Clayton5160ce52013-03-27 23:08:40 +00002394 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Claytonc3c0b0e2012-04-12 19:04:34 +00002395 if (log)
2396 log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'");
Jim Ingham4ceb9282012-06-08 22:50:40 +00002397#endif
Greg Claytonadc00cb2011-05-20 23:38:13 +00002398 sequence_mutex_unavailable = true;
2399 }
2400 return thread_ids.size();
2401}
Greg Clayton37a0a242012-04-11 00:24:49 +00002402
2403lldb::addr_t
2404GDBRemoteCommunicationClient::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
Daniel Maleae0f8f572013-08-26 23:57:52 +00002418lldb_private::Error
2419GDBRemoteCommunicationClient::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
2466uint32_t
2467GDBRemoteCommunicationClient::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
2486static uint64_t
2487ParseHostIOPacketResponse (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}
2509lldb::user_id_t
2510GDBRemoteCommunicationClient::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
2536bool
2537GDBRemoteCommunicationClient::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.
2553lldb::user_id_t
2554GDBRemoteCommunicationClient::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
2573uint32_t
2574GDBRemoteCommunicationClient::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
2613uint64_t
2614GDBRemoteCommunicationClient::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
2651uint64_t
2652GDBRemoteCommunicationClient::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.
2693bool
2694GDBRemoteCommunicationClient::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
2715bool
2716GDBRemoteCommunicationClient::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}