blob: 8c1fdf8cefc0c3846c27823889df4167c1549fd9 [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 Claytonf74cf862013-11-13 23:28:31 +000068 m_supports_QSaveRegisterState (eLazyBoolCalculate),
Greg Clayton32e0a752011-03-30 18:16:51 +000069 m_supports_qProcessInfoPID (true),
70 m_supports_qfProcessInfo (true),
71 m_supports_qUserName (true),
72 m_supports_qGroupName (true),
Greg Clayton8b82f082011-04-12 05:54:46 +000073 m_supports_qThreadStopInfo (true),
74 m_supports_z0 (true),
75 m_supports_z1 (true),
76 m_supports_z2 (true),
77 m_supports_z3 (true),
78 m_supports_z4 (true),
Greg Clayton89600582013-10-10 17:53:50 +000079 m_supports_QEnvironment (true),
80 m_supports_QEnvironmentHexEncoded (true),
Greg Clayton8b82f082011-04-12 05:54:46 +000081 m_curr_tid (LLDB_INVALID_THREAD_ID),
82 m_curr_tid_run (LLDB_INVALID_THREAD_ID),
Johnny Chen64637202012-05-23 21:09:52 +000083 m_num_supported_hardware_watchpoints (0),
Greg Clayton576d8832011-03-22 04:00:09 +000084 m_async_mutex (Mutex::eMutexTypeRecursive),
85 m_async_packet_predicate (false),
86 m_async_packet (),
Jim Inghama6195b72013-12-18 01:24:33 +000087 m_async_result (PacketResult::Success),
Greg Clayton576d8832011-03-22 04:00:09 +000088 m_async_response (),
89 m_async_signal (-1),
Han Ming Ong4b6459f2013-01-18 23:11:53 +000090 m_thread_id_to_used_usec_map (),
Greg Clayton1cb64962011-03-24 04:28:38 +000091 m_host_arch(),
Jason Molendaf17b5ac2012-12-19 02:54:03 +000092 m_process_arch(),
Greg Clayton1cb64962011-03-24 04:28:38 +000093 m_os_version_major (UINT32_MAX),
94 m_os_version_minor (UINT32_MAX),
Greg Clayton9ac6d2d2013-10-25 18:13:17 +000095 m_os_version_update (UINT32_MAX),
96 m_os_build (),
97 m_os_kernel (),
98 m_hostname (),
99 m_default_packet_timeout (0)
Greg Clayton576d8832011-03-22 04:00:09 +0000100{
Greg Clayton576d8832011-03-22 04:00:09 +0000101}
102
103//----------------------------------------------------------------------
104// Destructor
105//----------------------------------------------------------------------
106GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient()
107{
Greg Clayton576d8832011-03-22 04:00:09 +0000108 if (IsConnected())
Greg Clayton576d8832011-03-22 04:00:09 +0000109 Disconnect();
Greg Clayton576d8832011-03-22 04:00:09 +0000110}
111
112bool
Greg Clayton1cb64962011-03-24 04:28:38 +0000113GDBRemoteCommunicationClient::HandshakeWithServer (Error *error_ptr)
114{
Greg Claytonfb909312013-11-23 01:58:15 +0000115 ResetDiscoverableSettings();
116
Greg Clayton1cb64962011-03-24 04:28:38 +0000117 // Start the read thread after we send the handshake ack since if we
118 // fail to send the handshake ack, there is no reason to continue...
119 if (SendAck())
Greg Claytonfb909312013-11-23 01:58:15 +0000120 {
121 // The return value from QueryNoAckModeSupported() is true if the packet
122 // was sent and _any_ response (including UNIMPLEMENTED) was received),
123 // or false if no response was received. This quickly tells us if we have
124 // a live connection to a remote GDB server...
125 if (QueryNoAckModeSupported())
126 {
127 return true;
128 }
129 else
130 {
131 if (error_ptr)
132 error_ptr->SetErrorString("failed to get reply to handshake packet");
133 }
134 }
135 else
136 {
137 if (error_ptr)
138 error_ptr->SetErrorString("failed to send the handshake ack");
139 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000140 return false;
141}
142
Greg Claytonfb909312013-11-23 01:58:15 +0000143bool
Greg Clayton1cb64962011-03-24 04:28:38 +0000144GDBRemoteCommunicationClient::QueryNoAckModeSupported ()
Greg Clayton576d8832011-03-22 04:00:09 +0000145{
146 if (m_supports_not_sending_acks == eLazyBoolCalculate)
147 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000148 m_send_acks = true;
Greg Clayton576d8832011-03-22 04:00:09 +0000149 m_supports_not_sending_acks = eLazyBoolNo;
Greg Clayton1cb64962011-03-24 04:28:38 +0000150
151 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000152 if (SendPacketAndWaitForResponse("QStartNoAckMode", response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000153 {
154 if (response.IsOKResponse())
Greg Clayton1cb64962011-03-24 04:28:38 +0000155 {
156 m_send_acks = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000157 m_supports_not_sending_acks = eLazyBoolYes;
Greg Clayton1cb64962011-03-24 04:28:38 +0000158 }
Greg Claytonfb909312013-11-23 01:58:15 +0000159 return true;
Greg Clayton576d8832011-03-22 04:00:09 +0000160 }
161 }
Greg Claytonfb909312013-11-23 01:58:15 +0000162 return false;
Greg Clayton576d8832011-03-22 04:00:09 +0000163}
164
165void
Greg Clayton44633992012-04-10 03:22:03 +0000166GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported ()
167{
168 if (m_supports_threads_in_stop_reply == eLazyBoolCalculate)
169 {
170 m_supports_threads_in_stop_reply = eLazyBoolNo;
171
172 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000173 if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response, false) == PacketResult::Success)
Greg Clayton44633992012-04-10 03:22:03 +0000174 {
175 if (response.IsOKResponse())
176 m_supports_threads_in_stop_reply = eLazyBoolYes;
177 }
178 }
179}
180
Jim Inghamcd16df92012-07-20 21:37:13 +0000181bool
182GDBRemoteCommunicationClient::GetVAttachOrWaitSupported ()
183{
184 if (m_attach_or_wait_reply == eLazyBoolCalculate)
185 {
186 m_attach_or_wait_reply = eLazyBoolNo;
187
188 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000189 if (SendPacketAndWaitForResponse("qVAttachOrWaitSupported", response, false) == PacketResult::Success)
Jim Inghamcd16df92012-07-20 21:37:13 +0000190 {
191 if (response.IsOKResponse())
192 m_attach_or_wait_reply = eLazyBoolYes;
193 }
194 }
195 if (m_attach_or_wait_reply == eLazyBoolYes)
196 return true;
197 else
198 return false;
199}
200
Jim Ingham279ceec2012-07-25 21:12:43 +0000201bool
202GDBRemoteCommunicationClient::GetSyncThreadStateSupported ()
203{
204 if (m_prepare_for_reg_writing_reply == eLazyBoolCalculate)
205 {
206 m_prepare_for_reg_writing_reply = eLazyBoolNo;
207
208 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000209 if (SendPacketAndWaitForResponse("qSyncThreadStateSupported", response, false) == PacketResult::Success)
Jim Ingham279ceec2012-07-25 21:12:43 +0000210 {
211 if (response.IsOKResponse())
212 m_prepare_for_reg_writing_reply = eLazyBoolYes;
213 }
214 }
215 if (m_prepare_for_reg_writing_reply == eLazyBoolYes)
216 return true;
217 else
218 return false;
219}
220
Greg Clayton44633992012-04-10 03:22:03 +0000221
222void
Greg Clayton576d8832011-03-22 04:00:09 +0000223GDBRemoteCommunicationClient::ResetDiscoverableSettings()
224{
225 m_supports_not_sending_acks = eLazyBoolCalculate;
226 m_supports_thread_suffix = eLazyBoolCalculate;
Greg Clayton44633992012-04-10 03:22:03 +0000227 m_supports_threads_in_stop_reply = eLazyBoolCalculate;
Greg Clayton576d8832011-03-22 04:00:09 +0000228 m_supports_vCont_c = eLazyBoolCalculate;
229 m_supports_vCont_C = eLazyBoolCalculate;
230 m_supports_vCont_s = eLazyBoolCalculate;
231 m_supports_vCont_S = eLazyBoolCalculate;
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000232 m_supports_p = eLazyBoolCalculate;
Greg Claytonf74cf862013-11-13 23:28:31 +0000233 m_supports_QSaveRegisterState = eLazyBoolCalculate;
Greg Clayton32e0a752011-03-30 18:16:51 +0000234 m_qHostInfo_is_valid = eLazyBoolCalculate;
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000235 m_qProcessInfo_is_valid = eLazyBoolCalculate;
Greg Clayton70b57652011-05-15 01:25:55 +0000236 m_supports_alloc_dealloc_memory = eLazyBoolCalculate;
Greg Clayton46fb5582011-11-18 07:03:08 +0000237 m_supports_memory_region_info = eLazyBoolCalculate;
Jim Ingham279ceec2012-07-25 21:12:43 +0000238 m_prepare_for_reg_writing_reply = eLazyBoolCalculate;
239 m_attach_or_wait_reply = eLazyBoolCalculate;
Greg Clayton2a48f522011-05-14 01:50:35 +0000240
Greg Clayton32e0a752011-03-30 18:16:51 +0000241 m_supports_qProcessInfoPID = true;
242 m_supports_qfProcessInfo = true;
243 m_supports_qUserName = true;
244 m_supports_qGroupName = true;
Greg Clayton8b82f082011-04-12 05:54:46 +0000245 m_supports_qThreadStopInfo = true;
246 m_supports_z0 = true;
247 m_supports_z1 = true;
248 m_supports_z2 = true;
249 m_supports_z3 = true;
250 m_supports_z4 = true;
Greg Clayton89600582013-10-10 17:53:50 +0000251 m_supports_QEnvironment = true;
252 m_supports_QEnvironmentHexEncoded = true;
Greg Claytond314e812011-03-23 00:09:55 +0000253 m_host_arch.Clear();
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000254 m_process_arch.Clear();
Greg Clayton576d8832011-03-22 04:00:09 +0000255}
256
257
258bool
259GDBRemoteCommunicationClient::GetThreadSuffixSupported ()
260{
261 if (m_supports_thread_suffix == eLazyBoolCalculate)
262 {
263 StringExtractorGDBRemote response;
264 m_supports_thread_suffix = eLazyBoolNo;
Greg Clayton3dedae12013-12-06 21:45:27 +0000265 if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000266 {
267 if (response.IsOKResponse())
268 m_supports_thread_suffix = eLazyBoolYes;
269 }
270 }
271 return m_supports_thread_suffix;
272}
273bool
274GDBRemoteCommunicationClient::GetVContSupported (char flavor)
275{
276 if (m_supports_vCont_c == eLazyBoolCalculate)
277 {
278 StringExtractorGDBRemote response;
279 m_supports_vCont_any = eLazyBoolNo;
280 m_supports_vCont_all = eLazyBoolNo;
281 m_supports_vCont_c = eLazyBoolNo;
282 m_supports_vCont_C = eLazyBoolNo;
283 m_supports_vCont_s = eLazyBoolNo;
284 m_supports_vCont_S = eLazyBoolNo;
Greg Clayton3dedae12013-12-06 21:45:27 +0000285 if (SendPacketAndWaitForResponse("vCont?", response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000286 {
287 const char *response_cstr = response.GetStringRef().c_str();
288 if (::strstr (response_cstr, ";c"))
289 m_supports_vCont_c = eLazyBoolYes;
290
291 if (::strstr (response_cstr, ";C"))
292 m_supports_vCont_C = eLazyBoolYes;
293
294 if (::strstr (response_cstr, ";s"))
295 m_supports_vCont_s = eLazyBoolYes;
296
297 if (::strstr (response_cstr, ";S"))
298 m_supports_vCont_S = eLazyBoolYes;
299
300 if (m_supports_vCont_c == eLazyBoolYes &&
301 m_supports_vCont_C == eLazyBoolYes &&
302 m_supports_vCont_s == eLazyBoolYes &&
303 m_supports_vCont_S == eLazyBoolYes)
304 {
305 m_supports_vCont_all = eLazyBoolYes;
306 }
307
308 if (m_supports_vCont_c == eLazyBoolYes ||
309 m_supports_vCont_C == eLazyBoolYes ||
310 m_supports_vCont_s == eLazyBoolYes ||
311 m_supports_vCont_S == eLazyBoolYes)
312 {
313 m_supports_vCont_any = eLazyBoolYes;
314 }
315 }
316 }
317
318 switch (flavor)
319 {
320 case 'a': return m_supports_vCont_any;
321 case 'A': return m_supports_vCont_all;
322 case 'c': return m_supports_vCont_c;
323 case 'C': return m_supports_vCont_C;
324 case 's': return m_supports_vCont_s;
325 case 'S': return m_supports_vCont_S;
326 default: break;
327 }
328 return false;
329}
330
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000331// Check if the target supports 'p' packet. It sends out a 'p'
332// packet and checks the response. A normal packet will tell us
333// that support is available.
Sean Callananb1de1142013-09-04 23:24:15 +0000334//
335// Takes a valid thread ID because p needs to apply to a thread.
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000336bool
Sean Callananb1de1142013-09-04 23:24:15 +0000337GDBRemoteCommunicationClient::GetpPacketSupported (lldb::tid_t tid)
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000338{
339 if (m_supports_p == eLazyBoolCalculate)
340 {
341 StringExtractorGDBRemote response;
342 m_supports_p = eLazyBoolNo;
Sean Callananb1de1142013-09-04 23:24:15 +0000343 char packet[256];
344 if (GetThreadSuffixSupported())
345 snprintf(packet, sizeof(packet), "p0;thread:%" PRIx64 ";", tid);
346 else
347 snprintf(packet, sizeof(packet), "p0");
348
Greg Clayton3dedae12013-12-06 21:45:27 +0000349 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000350 {
351 if (response.IsNormalResponse())
352 m_supports_p = eLazyBoolYes;
353 }
354 }
355 return m_supports_p;
356}
Greg Clayton576d8832011-03-22 04:00:09 +0000357
Greg Clayton3dedae12013-12-06 21:45:27 +0000358GDBRemoteCommunicationClient::PacketResult
Greg Clayton576d8832011-03-22 04:00:09 +0000359GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
360(
361 const char *payload,
362 StringExtractorGDBRemote &response,
363 bool send_async
364)
365{
366 return SendPacketAndWaitForResponse (payload,
367 ::strlen (payload),
368 response,
369 send_async);
370}
371
Greg Clayton3dedae12013-12-06 21:45:27 +0000372GDBRemoteCommunicationClient::PacketResult
373GDBRemoteCommunicationClient::SendPacketAndWaitForResponseNoLock (const char *payload,
374 size_t payload_length,
375 StringExtractorGDBRemote &response)
376{
377 PacketResult packet_result = SendPacketNoLock (payload, payload_length);
378 if (packet_result == PacketResult::Success)
379 packet_result = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
380 return packet_result;
381}
382
383GDBRemoteCommunicationClient::PacketResult
Greg Clayton576d8832011-03-22 04:00:09 +0000384GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
385(
386 const char *payload,
387 size_t payload_length,
388 StringExtractorGDBRemote &response,
389 bool send_async
390)
391{
Greg Clayton3dedae12013-12-06 21:45:27 +0000392 PacketResult packet_result = PacketResult::ErrorSendFailed;
Greg Clayton576d8832011-03-22 04:00:09 +0000393 Mutex::Locker locker;
Greg Clayton5160ce52013-03-27 23:08:40 +0000394 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton644247c2011-07-07 01:59:51 +0000395 size_t response_len = 0;
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000396 if (GetSequenceMutex (locker))
Greg Clayton576d8832011-03-22 04:00:09 +0000397 {
Greg Clayton3dedae12013-12-06 21:45:27 +0000398 packet_result = SendPacketAndWaitForResponseNoLock (payload, payload_length, response);
Greg Clayton576d8832011-03-22 04:00:09 +0000399 }
400 else
401 {
402 if (send_async)
403 {
Greg Claytond3544052012-05-31 21:24:20 +0000404 if (IsRunning())
Greg Clayton576d8832011-03-22 04:00:09 +0000405 {
Greg Claytond3544052012-05-31 21:24:20 +0000406 Mutex::Locker async_locker (m_async_mutex);
407 m_async_packet.assign(payload, payload_length);
408 m_async_packet_predicate.SetValue (true, eBroadcastNever);
409
410 if (log)
411 log->Printf ("async: async packet = %s", m_async_packet.c_str());
412
413 bool timed_out = false;
414 if (SendInterrupt(locker, 2, timed_out))
Greg Clayton576d8832011-03-22 04:00:09 +0000415 {
Greg Claytond3544052012-05-31 21:24:20 +0000416 if (m_interrupt_sent)
Greg Clayton576d8832011-03-22 04:00:09 +0000417 {
Jim Inghambabfc382012-06-06 00:32:39 +0000418 m_interrupt_sent = false;
Greg Claytond3544052012-05-31 21:24:20 +0000419 TimeValue timeout_time;
420 timeout_time = TimeValue::Now();
421 timeout_time.OffsetWithSeconds (m_packet_timeout);
422
Greg Clayton576d8832011-03-22 04:00:09 +0000423 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000424 log->Printf ("async: sent interrupt");
Greg Clayton644247c2011-07-07 01:59:51 +0000425
Greg Claytond3544052012-05-31 21:24:20 +0000426 if (m_async_packet_predicate.WaitForValueEqualTo (false, &timeout_time, &timed_out))
Greg Claytone889ad62011-10-27 22:04:16 +0000427 {
Greg Claytond3544052012-05-31 21:24:20 +0000428 if (log)
429 log->Printf ("async: got response");
430
431 // Swap the response buffer to avoid malloc and string copy
432 response.GetStringRef().swap (m_async_response.GetStringRef());
433 response_len = response.GetStringRef().size();
Jim Inghama6195b72013-12-18 01:24:33 +0000434 packet_result = m_async_result;
Greg Claytond3544052012-05-31 21:24:20 +0000435 }
436 else
437 {
438 if (log)
439 log->Printf ("async: timed out waiting for response");
440 }
441
442 // Make sure we wait until the continue packet has been sent again...
443 if (m_private_is_running.WaitForValueEqualTo (true, &timeout_time, &timed_out))
444 {
445 if (log)
446 {
447 if (timed_out)
448 log->Printf ("async: timed out waiting for process to resume, but process was resumed");
449 else
450 log->Printf ("async: async packet sent");
451 }
452 }
453 else
454 {
455 if (log)
456 log->Printf ("async: timed out waiting for process to resume");
Greg Claytone889ad62011-10-27 22:04:16 +0000457 }
458 }
459 else
460 {
Greg Claytond3544052012-05-31 21:24:20 +0000461 // We had a racy condition where we went to send the interrupt
462 // yet we were able to get the lock, so the process must have
463 // just stopped?
Greg Clayton576d8832011-03-22 04:00:09 +0000464 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000465 log->Printf ("async: got lock without sending interrupt");
466 // Send the packet normally since we got the lock
Greg Clayton3dedae12013-12-06 21:45:27 +0000467 packet_result = SendPacketAndWaitForResponseNoLock (payload, payload_length, response);
Greg Clayton576d8832011-03-22 04:00:09 +0000468 }
469 }
470 else
471 {
Greg Clayton644247c2011-07-07 01:59:51 +0000472 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000473 log->Printf ("async: failed to interrupt");
Greg Clayton576d8832011-03-22 04:00:09 +0000474 }
475 }
476 else
477 {
478 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000479 log->Printf ("async: not running, async is ignored");
Greg Clayton576d8832011-03-22 04:00:09 +0000480 }
481 }
482 else
483 {
484 if (log)
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000485 log->Printf("error: failed to get packet sequence mutex, not sending packet '%*s'", (int) payload_length, payload);
Greg Clayton576d8832011-03-22 04:00:09 +0000486 }
487 }
Greg Clayton3dedae12013-12-06 21:45:27 +0000488 return packet_result;
Greg Clayton576d8832011-03-22 04:00:09 +0000489}
490
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000491static const char *end_delimiter = "--end--;";
492static const int end_delimiter_len = 8;
493
494std::string
495GDBRemoteCommunicationClient::HarmonizeThreadIdsForProfileData
496( ProcessGDBRemote *process,
497 StringExtractorGDBRemote& profileDataExtractor
498)
499{
500 std::map<uint64_t, uint32_t> new_thread_id_to_used_usec_map;
501 std::stringstream final_output;
502 std::string name, value;
503
504 // Going to assuming thread_used_usec comes first, else bail out.
505 while (profileDataExtractor.GetNameColonValue(name, value))
506 {
507 if (name.compare("thread_used_id") == 0)
508 {
509 StringExtractor threadIDHexExtractor(value.c_str());
510 uint64_t thread_id = threadIDHexExtractor.GetHexMaxU64(false, 0);
511
512 bool has_used_usec = false;
513 uint32_t curr_used_usec = 0;
514 std::string usec_name, usec_value;
515 uint32_t input_file_pos = profileDataExtractor.GetFilePos();
516 if (profileDataExtractor.GetNameColonValue(usec_name, usec_value))
517 {
518 if (usec_name.compare("thread_used_usec") == 0)
519 {
520 has_used_usec = true;
521 curr_used_usec = strtoull(usec_value.c_str(), NULL, 0);
522 }
523 else
524 {
525 // We didn't find what we want, it is probably
526 // an older version. Bail out.
527 profileDataExtractor.SetFilePos(input_file_pos);
528 }
529 }
530
531 if (has_used_usec)
532 {
533 uint32_t prev_used_usec = 0;
534 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_used_usec_map.find(thread_id);
535 if (iterator != m_thread_id_to_used_usec_map.end())
536 {
537 prev_used_usec = m_thread_id_to_used_usec_map[thread_id];
538 }
539
540 uint32_t real_used_usec = curr_used_usec - prev_used_usec;
541 // A good first time record is one that runs for at least 0.25 sec
542 bool good_first_time = (prev_used_usec == 0) && (real_used_usec > 250000);
543 bool good_subsequent_time = (prev_used_usec > 0) &&
544 ((real_used_usec > 0) || (process->HasAssignedIndexIDToThread(thread_id)));
545
546 if (good_first_time || good_subsequent_time)
547 {
548 // We try to avoid doing too many index id reservation,
549 // resulting in fast increase of index ids.
550
551 final_output << name << ":";
552 int32_t index_id = process->AssignIndexIDToThread(thread_id);
553 final_output << index_id << ";";
554
555 final_output << usec_name << ":" << usec_value << ";";
556 }
557 else
558 {
559 // Skip past 'thread_used_name'.
560 std::string local_name, local_value;
561 profileDataExtractor.GetNameColonValue(local_name, local_value);
562 }
563
564 // Store current time as previous time so that they can be compared later.
565 new_thread_id_to_used_usec_map[thread_id] = curr_used_usec;
566 }
567 else
568 {
569 // Bail out and use old string.
570 final_output << name << ":" << value << ";";
571 }
572 }
573 else
574 {
575 final_output << name << ":" << value << ";";
576 }
577 }
578 final_output << end_delimiter;
579 m_thread_id_to_used_usec_map = new_thread_id_to_used_usec_map;
580
581 return final_output.str();
582}
583
Greg Clayton576d8832011-03-22 04:00:09 +0000584StateType
585GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse
586(
587 ProcessGDBRemote *process,
588 const char *payload,
589 size_t packet_length,
590 StringExtractorGDBRemote &response
591)
592{
Greg Clayton1f5181a2012-07-02 22:05:25 +0000593 m_curr_tid = LLDB_INVALID_THREAD_ID;
Greg Clayton5160ce52013-03-27 23:08:40 +0000594 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton576d8832011-03-22 04:00:09 +0000595 if (log)
596 log->Printf ("GDBRemoteCommunicationClient::%s ()", __FUNCTION__);
597
598 Mutex::Locker locker(m_sequence_mutex);
599 StateType state = eStateRunning;
600
601 BroadcastEvent(eBroadcastBitRunPacketSent, NULL);
602 m_public_is_running.SetValue (true, eBroadcastNever);
603 // Set the starting continue packet into "continue_packet". This packet
Jim Inghambabfc382012-06-06 00:32:39 +0000604 // may change if we are interrupted and we continue after an async packet...
Greg Clayton576d8832011-03-22 04:00:09 +0000605 std::string continue_packet(payload, packet_length);
606
Greg Clayton3f875c52013-02-22 22:23:55 +0000607 bool got_async_packet = false;
Greg Claytonaf247d72011-05-19 03:54:16 +0000608
Greg Clayton576d8832011-03-22 04:00:09 +0000609 while (state == eStateRunning)
610 {
Greg Clayton3f875c52013-02-22 22:23:55 +0000611 if (!got_async_packet)
Greg Claytonaf247d72011-05-19 03:54:16 +0000612 {
613 if (log)
614 log->Printf ("GDBRemoteCommunicationClient::%s () sending continue packet: %s", __FUNCTION__, continue_packet.c_str());
Greg Clayton3dedae12013-12-06 21:45:27 +0000615 if (SendPacketNoLock(continue_packet.c_str(), continue_packet.size()) != PacketResult::Success)
Greg Claytonaf247d72011-05-19 03:54:16 +0000616 state = eStateInvalid;
Greg Clayton576d8832011-03-22 04:00:09 +0000617
Greg Claytone889ad62011-10-27 22:04:16 +0000618 m_private_is_running.SetValue (true, eBroadcastAlways);
Greg Claytonaf247d72011-05-19 03:54:16 +0000619 }
620
Greg Clayton3f875c52013-02-22 22:23:55 +0000621 got_async_packet = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000622
623 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000624 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(%s)", __FUNCTION__, continue_packet.c_str());
Greg Clayton576d8832011-03-22 04:00:09 +0000625
Greg Clayton3dedae12013-12-06 21:45:27 +0000626 if (WaitForPacketWithTimeoutMicroSecondsNoLock(response, UINT32_MAX) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000627 {
628 if (response.Empty())
629 state = eStateInvalid;
630 else
631 {
632 const char stop_type = response.GetChar();
633 if (log)
634 log->Printf ("GDBRemoteCommunicationClient::%s () got packet: %s", __FUNCTION__, response.GetStringRef().c_str());
635 switch (stop_type)
636 {
637 case 'T':
638 case 'S':
Greg Clayton576d8832011-03-22 04:00:09 +0000639 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000640 if (process->GetStopID() == 0)
Greg Clayton576d8832011-03-22 04:00:09 +0000641 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000642 if (process->GetID() == LLDB_INVALID_PROCESS_ID)
643 {
644 lldb::pid_t pid = GetCurrentProcessID ();
645 if (pid != LLDB_INVALID_PROCESS_ID)
646 process->SetID (pid);
647 }
648 process->BuildDynamicRegisterInfo (true);
Greg Clayton576d8832011-03-22 04:00:09 +0000649 }
Greg Clayton2687cd12012-03-29 01:55:41 +0000650
651 // Privately notify any internal threads that we have stopped
652 // in case we wanted to interrupt our process, yet we might
653 // send a packet and continue without returning control to the
654 // user.
655 m_private_is_running.SetValue (false, eBroadcastAlways);
656
657 const uint8_t signo = response.GetHexU8 (UINT8_MAX);
658
Jim Inghambabfc382012-06-06 00:32:39 +0000659 bool continue_after_async = m_async_signal != -1 || m_async_packet_predicate.GetValue();
660 if (continue_after_async || m_interrupt_sent)
Greg Clayton2687cd12012-03-29 01:55:41 +0000661 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000662 // We sent an interrupt packet to stop the inferior process
663 // for an async signal or to send an async packet while running
664 // but we might have been single stepping and received the
665 // stop packet for the step instead of for the interrupt packet.
666 // Typically when an interrupt is sent a SIGINT or SIGSTOP
667 // is used, so if we get anything else, we need to try and
668 // get another stop reply packet that may have been sent
669 // due to sending the interrupt when the target is stopped
670 // which will just re-send a copy of the last stop reply
671 // packet. If we don't do this, then the reply for our
672 // async packet will be the repeat stop reply packet and cause
673 // a lot of trouble for us!
674 if (signo != SIGINT && signo != SIGSTOP)
675 {
Greg Claytonfb72fde2012-05-15 02:50:49 +0000676 continue_after_async = false;
Greg Clayton2687cd12012-03-29 01:55:41 +0000677
678 // We didn't get a a SIGINT or SIGSTOP, so try for a
679 // very brief time (1 ms) to get another stop reply
680 // packet to make sure it doesn't get in the way
681 StringExtractorGDBRemote extra_stop_reply_packet;
682 uint32_t timeout_usec = 1000;
Greg Clayton3dedae12013-12-06 21:45:27 +0000683 if (WaitForPacketWithTimeoutMicroSecondsNoLock (extra_stop_reply_packet, timeout_usec) == PacketResult::Success)
Greg Clayton2687cd12012-03-29 01:55:41 +0000684 {
685 switch (extra_stop_reply_packet.GetChar())
686 {
687 case 'T':
688 case 'S':
689 // We did get an extra stop reply, which means
690 // our interrupt didn't stop the target so we
691 // shouldn't continue after the async signal
692 // or packet is sent...
Greg Claytonfb72fde2012-05-15 02:50:49 +0000693 continue_after_async = false;
Greg Clayton2687cd12012-03-29 01:55:41 +0000694 break;
695 }
696 }
697 }
698 }
699
700 if (m_async_signal != -1)
701 {
702 if (log)
703 log->Printf ("async: send signo = %s", Host::GetSignalAsCString (m_async_signal));
704
705 // Save off the async signal we are supposed to send
706 const int async_signal = m_async_signal;
707 // Clear the async signal member so we don't end up
708 // sending the signal multiple times...
709 m_async_signal = -1;
710 // Check which signal we stopped with
711 if (signo == async_signal)
712 {
713 if (log)
714 log->Printf ("async: stopped with signal %s, we are done running", Host::GetSignalAsCString (signo));
715
716 // We already stopped with a signal that we wanted
717 // to stop with, so we are done
718 }
719 else
720 {
721 // We stopped with a different signal that the one
722 // we wanted to stop with, so now we must resume
723 // with the signal we want
724 char signal_packet[32];
725 int signal_packet_len = 0;
726 signal_packet_len = ::snprintf (signal_packet,
727 sizeof (signal_packet),
728 "C%2.2x",
729 async_signal);
730
731 if (log)
732 log->Printf ("async: stopped with signal %s, resume with %s",
733 Host::GetSignalAsCString (signo),
734 Host::GetSignalAsCString (async_signal));
735
736 // Set the continue packet to resume even if the
Greg Claytonfb72fde2012-05-15 02:50:49 +0000737 // interrupt didn't cause our stop (ignore continue_after_async)
Greg Clayton2687cd12012-03-29 01:55:41 +0000738 continue_packet.assign(signal_packet, signal_packet_len);
739 continue;
740 }
741 }
742 else if (m_async_packet_predicate.GetValue())
743 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000744 Log * packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Greg Clayton2687cd12012-03-29 01:55:41 +0000745
746 // We are supposed to send an asynchronous packet while
Jim Inghama6195b72013-12-18 01:24:33 +0000747 // we are running.
Greg Clayton2687cd12012-03-29 01:55:41 +0000748 m_async_response.Clear();
749 if (m_async_packet.empty())
750 {
Jim Inghama6195b72013-12-18 01:24:33 +0000751 m_async_result = PacketResult::ErrorSendFailed;
752 if (packet_log)
Greg Clayton2687cd12012-03-29 01:55:41 +0000753 packet_log->Printf ("async: error: empty async packet");
754
755 }
756 else
757 {
758 if (packet_log)
759 packet_log->Printf ("async: sending packet");
760
Jim Inghama6195b72013-12-18 01:24:33 +0000761 m_async_result = SendPacketAndWaitForResponse (&m_async_packet[0],
762 m_async_packet.size(),
763 m_async_response,
764 false);
Greg Clayton2687cd12012-03-29 01:55:41 +0000765 }
766 // Let the other thread that was trying to send the async
767 // packet know that the packet has been sent and response is
768 // ready...
769 m_async_packet_predicate.SetValue(false, eBroadcastAlways);
770
771 if (packet_log)
Greg Claytonfb72fde2012-05-15 02:50:49 +0000772 packet_log->Printf ("async: sent packet, continue_after_async = %i", continue_after_async);
Greg Clayton2687cd12012-03-29 01:55:41 +0000773
774 // Set the continue packet to resume if our interrupt
775 // for the async packet did cause the stop
Greg Claytonfb72fde2012-05-15 02:50:49 +0000776 if (continue_after_async)
Greg Clayton2687cd12012-03-29 01:55:41 +0000777 {
Greg Claytonf1186de2012-05-24 23:42:14 +0000778 // Reverting this for now as it is causing deadlocks
779 // in programs (<rdar://problem/11529853>). In the future
780 // we should check our thread list and "do the right thing"
781 // for new threads that show up while we stop and run async
782 // packets. Setting the packet to 'c' to continue all threads
783 // is the right thing to do 99.99% of the time because if a
784 // thread was single stepping, and we sent an interrupt, we
785 // will notice above that we didn't stop due to an interrupt
786 // but stopped due to stepping and we would _not_ continue.
787 continue_packet.assign (1, 'c');
Greg Clayton2687cd12012-03-29 01:55:41 +0000788 continue;
789 }
790 }
791 // Stop with signal and thread info
792 state = eStateStopped;
Greg Clayton576d8832011-03-22 04:00:09 +0000793 }
Greg Clayton576d8832011-03-22 04:00:09 +0000794 break;
795
796 case 'W':
797 case 'X':
798 // process exited
799 state = eStateExited;
800 break;
801
802 case 'O':
803 // STDOUT
804 {
Greg Clayton3f875c52013-02-22 22:23:55 +0000805 got_async_packet = true;
Greg Clayton576d8832011-03-22 04:00:09 +0000806 std::string inferior_stdout;
807 inferior_stdout.reserve(response.GetBytesLeft () / 2);
808 char ch;
809 while ((ch = response.GetHexU8()) != '\0')
810 inferior_stdout.append(1, ch);
811 process->AppendSTDOUT (inferior_stdout.c_str(), inferior_stdout.size());
812 }
813 break;
814
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000815 case 'A':
816 // Async miscellaneous reply. Right now, only profile data is coming through this channel.
817 {
Greg Clayton3f875c52013-02-22 22:23:55 +0000818 got_async_packet = true;
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000819 std::string input = response.GetStringRef().substr(1); // '1' to move beyond 'A'
820 if (m_partial_profile_data.length() > 0)
821 {
822 m_partial_profile_data.append(input);
823 input = m_partial_profile_data;
824 m_partial_profile_data.clear();
825 }
826
827 size_t found, pos = 0, len = input.length();
828 while ((found = input.find(end_delimiter, pos)) != std::string::npos)
829 {
830 StringExtractorGDBRemote profileDataExtractor(input.substr(pos, found).c_str());
Han Ming Ong91ed6b82013-06-24 18:15:05 +0000831 std::string profile_data = HarmonizeThreadIdsForProfileData(process, profileDataExtractor);
832 process->BroadcastAsyncProfileData (profile_data);
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000833
834 pos = found + end_delimiter_len;
835 }
836
837 if (pos < len)
838 {
839 // Last incomplete chunk.
840 m_partial_profile_data = input.substr(pos);
841 }
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000842 }
843 break;
844
Greg Clayton576d8832011-03-22 04:00:09 +0000845 case 'E':
846 // ERROR
847 state = eStateInvalid;
848 break;
849
850 default:
851 if (log)
852 log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__);
853 state = eStateInvalid;
854 break;
855 }
856 }
857 }
858 else
859 {
860 if (log)
861 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__);
862 state = eStateInvalid;
863 }
864 }
865 if (log)
866 log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state));
867 response.SetFilePos(0);
868 m_private_is_running.SetValue (false, eBroadcastAlways);
869 m_public_is_running.SetValue (false, eBroadcastAlways);
870 return state;
871}
872
873bool
874GDBRemoteCommunicationClient::SendAsyncSignal (int signo)
875{
Greg Clayton2687cd12012-03-29 01:55:41 +0000876 Mutex::Locker async_locker (m_async_mutex);
Greg Clayton576d8832011-03-22 04:00:09 +0000877 m_async_signal = signo;
878 bool timed_out = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000879 Mutex::Locker locker;
Greg Clayton2687cd12012-03-29 01:55:41 +0000880 if (SendInterrupt (locker, 1, timed_out))
Greg Clayton576d8832011-03-22 04:00:09 +0000881 return true;
882 m_async_signal = -1;
883 return false;
884}
885
Greg Clayton37a0a242012-04-11 00:24:49 +0000886// This function takes a mutex locker as a parameter in case the GetSequenceMutex
Greg Clayton576d8832011-03-22 04:00:09 +0000887// actually succeeds. If it doesn't succeed in acquiring the sequence mutex
888// (the expected result), then it will send the halt packet. If it does succeed
889// then the caller that requested the interrupt will want to keep the sequence
890// locked down so that no one else can send packets while the caller has control.
891// This function usually gets called when we are running and need to stop the
892// target. It can also be used when we are running and and we need to do something
893// else (like read/write memory), so we need to interrupt the running process
894// (gdb remote protocol requires this), and do what we need to do, then resume.
895
896bool
Greg Clayton2687cd12012-03-29 01:55:41 +0000897GDBRemoteCommunicationClient::SendInterrupt
Greg Clayton576d8832011-03-22 04:00:09 +0000898(
899 Mutex::Locker& locker,
900 uint32_t seconds_to_wait_for_stop,
Greg Clayton576d8832011-03-22 04:00:09 +0000901 bool &timed_out
902)
903{
Greg Clayton576d8832011-03-22 04:00:09 +0000904 timed_out = false;
Greg Clayton5160ce52013-03-27 23:08:40 +0000905 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Clayton576d8832011-03-22 04:00:09 +0000906
907 if (IsRunning())
908 {
909 // Only send an interrupt if our debugserver is running...
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000910 if (GetSequenceMutex (locker))
Greg Clayton37a0a242012-04-11 00:24:49 +0000911 {
912 if (log)
913 log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt");
914 }
915 else
Greg Clayton576d8832011-03-22 04:00:09 +0000916 {
917 // Someone has the mutex locked waiting for a response or for the
918 // inferior to stop, so send the interrupt on the down low...
919 char ctrl_c = '\x03';
920 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton576d8832011-03-22 04:00:09 +0000921 size_t bytes_written = Write (&ctrl_c, 1, status, NULL);
Greg Clayton2687cd12012-03-29 01:55:41 +0000922 if (log)
923 log->PutCString("send packet: \\x03");
Greg Clayton576d8832011-03-22 04:00:09 +0000924 if (bytes_written > 0)
925 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000926 m_interrupt_sent = true;
Greg Clayton576d8832011-03-22 04:00:09 +0000927 if (seconds_to_wait_for_stop)
928 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000929 TimeValue timeout;
930 if (seconds_to_wait_for_stop)
931 {
932 timeout = TimeValue::Now();
933 timeout.OffsetWithSeconds (seconds_to_wait_for_stop);
934 }
Greg Clayton576d8832011-03-22 04:00:09 +0000935 if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out))
936 {
937 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +0000938 log->PutCString ("SendInterrupt () - sent interrupt, private state stopped");
Greg Clayton576d8832011-03-22 04:00:09 +0000939 return true;
940 }
941 else
942 {
943 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +0000944 log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume");
Greg Clayton576d8832011-03-22 04:00:09 +0000945 }
946 }
947 else
948 {
949 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +0000950 log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop...");
Greg Clayton576d8832011-03-22 04:00:09 +0000951 return true;
952 }
953 }
954 else
955 {
956 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +0000957 log->Printf ("SendInterrupt () - failed to write interrupt");
Greg Clayton576d8832011-03-22 04:00:09 +0000958 }
959 return false;
960 }
Greg Clayton576d8832011-03-22 04:00:09 +0000961 }
Greg Clayton2687cd12012-03-29 01:55:41 +0000962 else
963 {
964 if (log)
965 log->Printf ("SendInterrupt () - not running");
966 }
Greg Clayton576d8832011-03-22 04:00:09 +0000967 return true;
968}
969
970lldb::pid_t
971GDBRemoteCommunicationClient::GetCurrentProcessID ()
972{
973 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000974 if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000975 {
976 if (response.GetChar() == 'Q')
977 if (response.GetChar() == 'C')
978 return response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID);
979 }
980 return LLDB_INVALID_PROCESS_ID;
981}
982
983bool
984GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str)
985{
986 error_str.clear();
987 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000988 if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000989 {
990 if (response.IsOKResponse())
991 return true;
992 if (response.GetChar() == 'E')
993 {
994 // A string the describes what failed when launching...
995 error_str = response.GetStringRef().substr(1);
996 }
997 else
998 {
999 error_str.assign ("unknown error occurred launching process");
1000 }
1001 }
1002 else
1003 {
Jim Ingham98d6da52012-06-28 20:30:23 +00001004 error_str.assign ("timed out waiting for app to launch");
Greg Clayton576d8832011-03-22 04:00:09 +00001005 }
1006 return false;
1007}
1008
1009int
Greg Claytonfbb76342013-11-20 21:07:01 +00001010GDBRemoteCommunicationClient::SendArgumentsPacket (const ProcessLaunchInfo &launch_info)
Greg Clayton576d8832011-03-22 04:00:09 +00001011{
Greg Claytonfbb76342013-11-20 21:07:01 +00001012 // Since we don't get the send argv0 separate from the executable path, we need to
1013 // make sure to use the actual exectuable path found in the launch_info...
1014 std::vector<const char *> argv;
1015 FileSpec exe_file = launch_info.GetExecutableFile();
1016 std::string exe_path;
1017 const char *arg = NULL;
1018 const Args &launch_args = launch_info.GetArguments();
1019 if (exe_file)
1020 exe_path = exe_file.GetPath();
1021 else
1022 {
1023 arg = launch_args.GetArgumentAtIndex(0);
1024 if (arg)
1025 exe_path = arg;
1026 }
1027 if (!exe_path.empty())
1028 {
1029 argv.push_back(exe_path.c_str());
1030 for (uint32_t i=1; (arg = launch_args.GetArgumentAtIndex(i)) != NULL; ++i)
1031 {
1032 if (arg)
1033 argv.push_back(arg);
1034 }
1035 }
1036 if (!argv.empty())
Greg Clayton576d8832011-03-22 04:00:09 +00001037 {
1038 StreamString packet;
1039 packet.PutChar('A');
Greg Claytonfbb76342013-11-20 21:07:01 +00001040 for (size_t i = 0, n = argv.size(); i < n; ++i)
Greg Clayton576d8832011-03-22 04:00:09 +00001041 {
Greg Claytonfbb76342013-11-20 21:07:01 +00001042 arg = argv[i];
Greg Clayton576d8832011-03-22 04:00:09 +00001043 const int arg_len = strlen(arg);
1044 if (i > 0)
1045 packet.PutChar(',');
Greg Claytonfbb76342013-11-20 21:07:01 +00001046 packet.Printf("%i,%i,", arg_len * 2, (int)i);
Greg Clayton576d8832011-03-22 04:00:09 +00001047 packet.PutBytesAsRawHex8 (arg, arg_len);
1048 }
1049
1050 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001051 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001052 {
1053 if (response.IsOKResponse())
1054 return 0;
1055 uint8_t error = response.GetError();
1056 if (error)
1057 return error;
1058 }
1059 }
1060 return -1;
1061}
1062
1063int
1064GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value)
1065{
1066 if (name_equal_value && name_equal_value[0])
1067 {
1068 StreamString packet;
Greg Clayton89600582013-10-10 17:53:50 +00001069 bool send_hex_encoding = false;
1070 for (const char *p = name_equal_value; *p != '\0' && send_hex_encoding == false; ++p)
Greg Clayton576d8832011-03-22 04:00:09 +00001071 {
Greg Clayton89600582013-10-10 17:53:50 +00001072 if (isprint(*p))
1073 {
1074 switch (*p)
1075 {
1076 case '$':
1077 case '#':
1078 send_hex_encoding = true;
1079 break;
1080 default:
1081 break;
1082 }
1083 }
1084 else
1085 {
1086 // We have non printable characters, lets hex encode this...
1087 send_hex_encoding = true;
1088 }
1089 }
1090
1091 StringExtractorGDBRemote response;
1092 if (send_hex_encoding)
1093 {
1094 if (m_supports_QEnvironmentHexEncoded)
1095 {
1096 packet.PutCString("QEnvironmentHexEncoded:");
1097 packet.PutBytesAsRawHex8 (name_equal_value, strlen(name_equal_value));
Greg Clayton3dedae12013-12-06 21:45:27 +00001098 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton89600582013-10-10 17:53:50 +00001099 {
1100 if (response.IsOKResponse())
1101 return 0;
1102 uint8_t error = response.GetError();
1103 if (error)
1104 return error;
1105 if (response.IsUnsupportedResponse())
1106 m_supports_QEnvironmentHexEncoded = false;
1107 }
1108 }
1109
1110 }
1111 else if (m_supports_QEnvironment)
1112 {
1113 packet.Printf("QEnvironment:%s", name_equal_value);
Greg Clayton3dedae12013-12-06 21:45:27 +00001114 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton89600582013-10-10 17:53:50 +00001115 {
1116 if (response.IsOKResponse())
1117 return 0;
1118 uint8_t error = response.GetError();
1119 if (error)
1120 return error;
1121 if (response.IsUnsupportedResponse())
1122 m_supports_QEnvironment = false;
1123 }
Greg Clayton576d8832011-03-22 04:00:09 +00001124 }
1125 }
1126 return -1;
1127}
1128
Greg Claytonc4103b32011-05-08 04:53:50 +00001129int
1130GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch)
1131{
1132 if (arch && arch[0])
1133 {
1134 StreamString packet;
1135 packet.Printf("QLaunchArch:%s", arch);
1136 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001137 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Claytonc4103b32011-05-08 04:53:50 +00001138 {
1139 if (response.IsOKResponse())
1140 return 0;
1141 uint8_t error = response.GetError();
1142 if (error)
1143 return error;
1144 }
1145 }
1146 return -1;
1147}
1148
Greg Clayton576d8832011-03-22 04:00:09 +00001149bool
Greg Clayton1cb64962011-03-24 04:28:38 +00001150GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major,
1151 uint32_t &minor,
1152 uint32_t &update)
1153{
1154 if (GetHostInfo ())
1155 {
1156 if (m_os_version_major != UINT32_MAX)
1157 {
1158 major = m_os_version_major;
1159 minor = m_os_version_minor;
1160 update = m_os_version_update;
1161 return true;
1162 }
1163 }
1164 return false;
1165}
1166
1167bool
1168GDBRemoteCommunicationClient::GetOSBuildString (std::string &s)
1169{
1170 if (GetHostInfo ())
1171 {
1172 if (!m_os_build.empty())
1173 {
1174 s = m_os_build;
1175 return true;
1176 }
1177 }
1178 s.clear();
1179 return false;
1180}
1181
1182
1183bool
1184GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s)
1185{
1186 if (GetHostInfo ())
1187 {
1188 if (!m_os_kernel.empty())
1189 {
1190 s = m_os_kernel;
1191 return true;
1192 }
1193 }
1194 s.clear();
1195 return false;
1196}
1197
1198bool
1199GDBRemoteCommunicationClient::GetHostname (std::string &s)
1200{
1201 if (GetHostInfo ())
1202 {
1203 if (!m_hostname.empty())
1204 {
1205 s = m_hostname;
1206 return true;
1207 }
1208 }
1209 s.clear();
1210 return false;
1211}
1212
1213ArchSpec
1214GDBRemoteCommunicationClient::GetSystemArchitecture ()
1215{
1216 if (GetHostInfo ())
1217 return m_host_arch;
1218 return ArchSpec();
1219}
1220
Jason Molendaf17b5ac2012-12-19 02:54:03 +00001221const lldb_private::ArchSpec &
1222GDBRemoteCommunicationClient::GetProcessArchitecture ()
1223{
1224 if (m_qProcessInfo_is_valid == eLazyBoolCalculate)
1225 GetCurrentProcessInfo ();
1226 return m_process_arch;
1227}
1228
Greg Clayton1cb64962011-03-24 04:28:38 +00001229
1230bool
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00001231GDBRemoteCommunicationClient::GetHostInfo (bool force)
Greg Clayton576d8832011-03-22 04:00:09 +00001232{
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00001233 if (force || m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton576d8832011-03-22 04:00:09 +00001234 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001235 m_qHostInfo_is_valid = eLazyBoolNo;
Greg Clayton576d8832011-03-22 04:00:09 +00001236 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001237 if (SendPacketAndWaitForResponse ("qHostInfo", response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001238 {
Greg Clayton17a0cb62011-05-15 23:46:54 +00001239 if (response.IsNormalResponse())
Greg Claytond314e812011-03-23 00:09:55 +00001240 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001241 std::string name;
1242 std::string value;
1243 uint32_t cpu = LLDB_INVALID_CPUTYPE;
1244 uint32_t sub = 0;
1245 std::string arch_name;
1246 std::string os_name;
1247 std::string vendor_name;
1248 std::string triple;
1249 uint32_t pointer_byte_size = 0;
1250 StringExtractor extractor;
1251 ByteOrder byte_order = eByteOrderInvalid;
1252 uint32_t num_keys_decoded = 0;
1253 while (response.GetNameColonValue(name, value))
Greg Claytond314e812011-03-23 00:09:55 +00001254 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001255 if (name.compare("cputype") == 0)
Greg Clayton1cb64962011-03-24 04:28:38 +00001256 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001257 // exception type in big endian hex
1258 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0);
1259 if (cpu != LLDB_INVALID_CPUTYPE)
1260 ++num_keys_decoded;
1261 }
1262 else if (name.compare("cpusubtype") == 0)
1263 {
1264 // exception count in big endian hex
1265 sub = Args::StringToUInt32 (value.c_str(), 0, 0);
1266 if (sub != 0)
1267 ++num_keys_decoded;
1268 }
1269 else if (name.compare("arch") == 0)
1270 {
1271 arch_name.swap (value);
1272 ++num_keys_decoded;
1273 }
1274 else if (name.compare("triple") == 0)
1275 {
1276 // The triple comes as ASCII hex bytes since it contains '-' chars
1277 extractor.GetStringRef().swap(value);
1278 extractor.SetFilePos(0);
1279 extractor.GetHexByteString (triple);
1280 ++num_keys_decoded;
1281 }
1282 else if (name.compare("os_build") == 0)
1283 {
1284 extractor.GetStringRef().swap(value);
1285 extractor.SetFilePos(0);
1286 extractor.GetHexByteString (m_os_build);
1287 ++num_keys_decoded;
1288 }
1289 else if (name.compare("hostname") == 0)
1290 {
1291 extractor.GetStringRef().swap(value);
1292 extractor.SetFilePos(0);
1293 extractor.GetHexByteString (m_hostname);
1294 ++num_keys_decoded;
1295 }
1296 else if (name.compare("os_kernel") == 0)
1297 {
1298 extractor.GetStringRef().swap(value);
1299 extractor.SetFilePos(0);
1300 extractor.GetHexByteString (m_os_kernel);
1301 ++num_keys_decoded;
1302 }
1303 else if (name.compare("ostype") == 0)
1304 {
1305 os_name.swap (value);
1306 ++num_keys_decoded;
1307 }
1308 else if (name.compare("vendor") == 0)
1309 {
1310 vendor_name.swap(value);
1311 ++num_keys_decoded;
1312 }
1313 else if (name.compare("endian") == 0)
1314 {
1315 ++num_keys_decoded;
1316 if (value.compare("little") == 0)
1317 byte_order = eByteOrderLittle;
1318 else if (value.compare("big") == 0)
1319 byte_order = eByteOrderBig;
1320 else if (value.compare("pdp") == 0)
1321 byte_order = eByteOrderPDP;
1322 else
1323 --num_keys_decoded;
1324 }
1325 else if (name.compare("ptrsize") == 0)
1326 {
1327 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 0);
1328 if (pointer_byte_size != 0)
1329 ++num_keys_decoded;
1330 }
1331 else if (name.compare("os_version") == 0)
1332 {
1333 Args::StringToVersion (value.c_str(),
1334 m_os_version_major,
1335 m_os_version_minor,
1336 m_os_version_update);
1337 if (m_os_version_major != UINT32_MAX)
1338 ++num_keys_decoded;
1339 }
Enrico Granataf04a2192012-07-13 23:18:48 +00001340 else if (name.compare("watchpoint_exceptions_received") == 0)
1341 {
1342 ++num_keys_decoded;
1343 if (strcmp(value.c_str(),"before") == 0)
1344 m_watchpoints_trigger_after_instruction = eLazyBoolNo;
1345 else if (strcmp(value.c_str(),"after") == 0)
1346 m_watchpoints_trigger_after_instruction = eLazyBoolYes;
1347 else
1348 --num_keys_decoded;
1349 }
Greg Clayton9ac6d2d2013-10-25 18:13:17 +00001350 else if (name.compare("default_packet_timeout") == 0)
1351 {
1352 m_default_packet_timeout = Args::StringToUInt32(value.c_str(), 0);
1353 if (m_default_packet_timeout > 0)
1354 {
1355 SetPacketTimeout(m_default_packet_timeout);
1356 ++num_keys_decoded;
1357 }
1358 }
Enrico Granataf04a2192012-07-13 23:18:48 +00001359
Greg Clayton32e0a752011-03-30 18:16:51 +00001360 }
1361
1362 if (num_keys_decoded > 0)
1363 m_qHostInfo_is_valid = eLazyBoolYes;
1364
1365 if (triple.empty())
1366 {
1367 if (arch_name.empty())
1368 {
1369 if (cpu != LLDB_INVALID_CPUTYPE)
1370 {
1371 m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1372 if (pointer_byte_size)
1373 {
1374 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1375 }
1376 if (byte_order != eByteOrderInvalid)
1377 {
1378 assert (byte_order == m_host_arch.GetByteOrder());
1379 }
Greg Clayton70512312012-05-08 01:45:38 +00001380
1381 if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0)
1382 {
1383 switch (m_host_arch.GetMachine())
1384 {
1385 case llvm::Triple::arm:
1386 case llvm::Triple::thumb:
1387 os_name = "ios";
1388 break;
1389 default:
1390 os_name = "macosx";
1391 break;
1392 }
1393 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001394 if (!vendor_name.empty())
1395 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1396 if (!os_name.empty())
Greg Claytone1dadb82011-09-15 00:21:03 +00001397 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Greg Clayton32e0a752011-03-30 18:16:51 +00001398
1399 }
1400 }
1401 else
1402 {
1403 std::string triple;
1404 triple += arch_name;
Greg Clayton70512312012-05-08 01:45:38 +00001405 if (!vendor_name.empty() || !os_name.empty())
1406 {
1407 triple += '-';
1408 if (vendor_name.empty())
1409 triple += "unknown";
1410 else
1411 triple += vendor_name;
1412 triple += '-';
1413 if (os_name.empty())
1414 triple += "unknown";
1415 else
1416 triple += os_name;
1417 }
1418 m_host_arch.SetTriple (triple.c_str());
1419
1420 llvm::Triple &host_triple = m_host_arch.GetTriple();
1421 if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin)
1422 {
1423 switch (m_host_arch.GetMachine())
1424 {
1425 case llvm::Triple::arm:
1426 case llvm::Triple::thumb:
1427 host_triple.setOS(llvm::Triple::IOS);
1428 break;
1429 default:
1430 host_triple.setOS(llvm::Triple::MacOSX);
1431 break;
1432 }
1433 }
Greg Clayton1cb64962011-03-24 04:28:38 +00001434 if (pointer_byte_size)
1435 {
1436 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1437 }
1438 if (byte_order != eByteOrderInvalid)
1439 {
1440 assert (byte_order == m_host_arch.GetByteOrder());
1441 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001442
Greg Clayton1cb64962011-03-24 04:28:38 +00001443 }
1444 }
1445 else
1446 {
Greg Clayton70512312012-05-08 01:45:38 +00001447 m_host_arch.SetTriple (triple.c_str());
Greg Claytond314e812011-03-23 00:09:55 +00001448 if (pointer_byte_size)
1449 {
1450 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1451 }
1452 if (byte_order != eByteOrderInvalid)
1453 {
1454 assert (byte_order == m_host_arch.GetByteOrder());
1455 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001456 }
Greg Claytond314e812011-03-23 00:09:55 +00001457 }
Greg Clayton576d8832011-03-22 04:00:09 +00001458 }
1459 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001460 return m_qHostInfo_is_valid == eLazyBoolYes;
Greg Clayton576d8832011-03-22 04:00:09 +00001461}
1462
1463int
1464GDBRemoteCommunicationClient::SendAttach
1465(
1466 lldb::pid_t pid,
1467 StringExtractorGDBRemote& response
1468)
1469{
1470 if (pid != LLDB_INVALID_PROCESS_ID)
1471 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001472 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001473 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, pid);
Andy Gibbsa297a972013-06-19 19:04:53 +00001474 assert (packet_len < (int)sizeof(packet));
Greg Clayton3dedae12013-12-06 21:45:27 +00001475 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001476 {
1477 if (response.IsErrorResponse())
1478 return response.GetError();
1479 return 0;
1480 }
1481 }
1482 return -1;
1483}
1484
1485const lldb_private::ArchSpec &
1486GDBRemoteCommunicationClient::GetHostArchitecture ()
1487{
Greg Clayton32e0a752011-03-30 18:16:51 +00001488 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton576d8832011-03-22 04:00:09 +00001489 GetHostInfo ();
Greg Claytond314e812011-03-23 00:09:55 +00001490 return m_host_arch;
Greg Clayton576d8832011-03-22 04:00:09 +00001491}
1492
Greg Clayton9ac6d2d2013-10-25 18:13:17 +00001493uint32_t
1494GDBRemoteCommunicationClient::GetHostDefaultPacketTimeout ()
1495{
1496 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
1497 GetHostInfo ();
1498 return m_default_packet_timeout;
1499}
1500
Greg Clayton576d8832011-03-22 04:00:09 +00001501addr_t
1502GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions)
1503{
Greg Clayton70b57652011-05-15 01:25:55 +00001504 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton576d8832011-03-22 04:00:09 +00001505 {
Greg Clayton70b57652011-05-15 01:25:55 +00001506 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton2a48f522011-05-14 01:50:35 +00001507 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001508 const int packet_len = ::snprintf (packet, sizeof(packet), "_M%" PRIx64 ",%s%s%s",
Greg Clayton43e0af02012-09-18 18:04:04 +00001509 (uint64_t)size,
Greg Clayton2a48f522011-05-14 01:50:35 +00001510 permissions & lldb::ePermissionsReadable ? "r" : "",
1511 permissions & lldb::ePermissionsWritable ? "w" : "",
1512 permissions & lldb::ePermissionsExecutable ? "x" : "");
Andy Gibbsa297a972013-06-19 19:04:53 +00001513 assert (packet_len < (int)sizeof(packet));
Greg Clayton2a48f522011-05-14 01:50:35 +00001514 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001515 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton2a48f522011-05-14 01:50:35 +00001516 {
Greg Clayton17a0cb62011-05-15 23:46:54 +00001517 if (!response.IsErrorResponse())
Greg Clayton2a48f522011-05-14 01:50:35 +00001518 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1519 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00001520 else
1521 {
1522 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1523 }
Greg Clayton576d8832011-03-22 04:00:09 +00001524 }
1525 return LLDB_INVALID_ADDRESS;
1526}
1527
1528bool
1529GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr)
1530{
Greg Clayton70b57652011-05-15 01:25:55 +00001531 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton576d8832011-03-22 04:00:09 +00001532 {
Greg Clayton70b57652011-05-15 01:25:55 +00001533 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton2a48f522011-05-14 01:50:35 +00001534 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001535 const int packet_len = ::snprintf(packet, sizeof(packet), "_m%" PRIx64, (uint64_t)addr);
Andy Gibbsa297a972013-06-19 19:04:53 +00001536 assert (packet_len < (int)sizeof(packet));
Greg Clayton2a48f522011-05-14 01:50:35 +00001537 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001538 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton2a48f522011-05-14 01:50:35 +00001539 {
1540 if (response.IsOKResponse())
1541 return true;
Greg Clayton17a0cb62011-05-15 23:46:54 +00001542 }
1543 else
1544 {
1545 m_supports_alloc_dealloc_memory = eLazyBoolNo;
Greg Clayton2a48f522011-05-14 01:50:35 +00001546 }
Greg Clayton576d8832011-03-22 04:00:09 +00001547 }
1548 return false;
1549}
1550
Jim Inghamacff8952013-05-02 00:27:30 +00001551Error
1552GDBRemoteCommunicationClient::Detach (bool keep_stopped)
Greg Clayton37a0a242012-04-11 00:24:49 +00001553{
Jim Inghamacff8952013-05-02 00:27:30 +00001554 Error error;
1555
1556 if (keep_stopped)
1557 {
1558 if (m_supports_detach_stay_stopped == eLazyBoolCalculate)
1559 {
1560 char packet[64];
1561 const int packet_len = ::snprintf(packet, sizeof(packet), "qSupportsDetachAndStayStopped:");
Andy Gibbsa297a972013-06-19 19:04:53 +00001562 assert (packet_len < (int)sizeof(packet));
Jim Inghamacff8952013-05-02 00:27:30 +00001563 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001564 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Jim Inghamacff8952013-05-02 00:27:30 +00001565 {
1566 m_supports_detach_stay_stopped = eLazyBoolYes;
1567 }
1568 else
1569 {
1570 m_supports_detach_stay_stopped = eLazyBoolNo;
1571 }
1572 }
1573
1574 if (m_supports_detach_stay_stopped == eLazyBoolNo)
1575 {
1576 error.SetErrorString("Stays stopped not supported by this target.");
1577 return error;
1578 }
1579 else
1580 {
Greg Clayton3dedae12013-12-06 21:45:27 +00001581 PacketResult packet_result = SendPacket ("D1", 2);
1582 if (packet_result != PacketResult::Success)
Jim Inghamacff8952013-05-02 00:27:30 +00001583 error.SetErrorString ("Sending extended disconnect packet failed.");
1584 }
1585 }
1586 else
1587 {
Greg Clayton3dedae12013-12-06 21:45:27 +00001588 PacketResult packet_result = SendPacket ("D", 1);
1589 if (packet_result != PacketResult::Success)
Jim Inghamacff8952013-05-02 00:27:30 +00001590 error.SetErrorString ("Sending disconnect packet failed.");
1591 }
1592 return error;
Greg Clayton37a0a242012-04-11 00:24:49 +00001593}
1594
Greg Clayton46fb5582011-11-18 07:03:08 +00001595Error
1596GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr,
1597 lldb_private::MemoryRegionInfo &region_info)
1598{
1599 Error error;
1600 region_info.Clear();
1601
1602 if (m_supports_memory_region_info != eLazyBoolNo)
1603 {
1604 m_supports_memory_region_info = eLazyBoolYes;
1605 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001606 const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%" PRIx64, (uint64_t)addr);
Andy Gibbsa297a972013-06-19 19:04:53 +00001607 assert (packet_len < (int)sizeof(packet));
Greg Clayton46fb5582011-11-18 07:03:08 +00001608 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001609 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton46fb5582011-11-18 07:03:08 +00001610 {
1611 std::string name;
1612 std::string value;
1613 addr_t addr_value;
1614 bool success = true;
Jason Molendacb349ee2011-12-13 05:39:38 +00001615 bool saw_permissions = false;
Greg Clayton46fb5582011-11-18 07:03:08 +00001616 while (success && response.GetNameColonValue(name, value))
1617 {
1618 if (name.compare ("start") == 0)
1619 {
1620 addr_value = Args::StringToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success);
1621 if (success)
1622 region_info.GetRange().SetRangeBase(addr_value);
1623 }
1624 else if (name.compare ("size") == 0)
1625 {
1626 addr_value = Args::StringToUInt64(value.c_str(), 0, 16, &success);
1627 if (success)
1628 region_info.GetRange().SetByteSize (addr_value);
1629 }
Jason Molendacb349ee2011-12-13 05:39:38 +00001630 else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid())
Greg Clayton46fb5582011-11-18 07:03:08 +00001631 {
Jason Molendacb349ee2011-12-13 05:39:38 +00001632 saw_permissions = true;
1633 if (region_info.GetRange().Contains (addr))
1634 {
1635 if (value.find('r') != std::string::npos)
1636 region_info.SetReadable (MemoryRegionInfo::eYes);
1637 else
1638 region_info.SetReadable (MemoryRegionInfo::eNo);
1639
1640 if (value.find('w') != std::string::npos)
1641 region_info.SetWritable (MemoryRegionInfo::eYes);
1642 else
1643 region_info.SetWritable (MemoryRegionInfo::eNo);
1644
1645 if (value.find('x') != std::string::npos)
1646 region_info.SetExecutable (MemoryRegionInfo::eYes);
1647 else
1648 region_info.SetExecutable (MemoryRegionInfo::eNo);
1649 }
1650 else
1651 {
1652 // The reported region does not contain this address -- we're looking at an unmapped page
1653 region_info.SetReadable (MemoryRegionInfo::eNo);
1654 region_info.SetWritable (MemoryRegionInfo::eNo);
1655 region_info.SetExecutable (MemoryRegionInfo::eNo);
1656 }
Greg Clayton46fb5582011-11-18 07:03:08 +00001657 }
1658 else if (name.compare ("error") == 0)
1659 {
1660 StringExtractorGDBRemote name_extractor;
1661 // Swap "value" over into "name_extractor"
1662 name_extractor.GetStringRef().swap(value);
1663 // Now convert the HEX bytes into a string value
1664 name_extractor.GetHexByteString (value);
1665 error.SetErrorString(value.c_str());
1666 }
1667 }
Jason Molendacb349ee2011-12-13 05:39:38 +00001668
1669 // We got a valid address range back but no permissions -- which means this is an unmapped page
1670 if (region_info.GetRange().IsValid() && saw_permissions == false)
1671 {
1672 region_info.SetReadable (MemoryRegionInfo::eNo);
1673 region_info.SetWritable (MemoryRegionInfo::eNo);
1674 region_info.SetExecutable (MemoryRegionInfo::eNo);
1675 }
Greg Clayton46fb5582011-11-18 07:03:08 +00001676 }
1677 else
1678 {
1679 m_supports_memory_region_info = eLazyBoolNo;
1680 }
1681 }
1682
1683 if (m_supports_memory_region_info == eLazyBoolNo)
1684 {
1685 error.SetErrorString("qMemoryRegionInfo is not supported");
1686 }
1687 if (error.Fail())
1688 region_info.Clear();
1689 return error;
1690
1691}
1692
Johnny Chen64637202012-05-23 21:09:52 +00001693Error
1694GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num)
1695{
1696 Error error;
1697
1698 if (m_supports_watchpoint_support_info == eLazyBoolYes)
1699 {
1700 num = m_num_supported_hardware_watchpoints;
1701 return error;
1702 }
1703
1704 // Set num to 0 first.
1705 num = 0;
1706 if (m_supports_watchpoint_support_info != eLazyBoolNo)
1707 {
1708 char packet[64];
1709 const int packet_len = ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:");
Andy Gibbsa297a972013-06-19 19:04:53 +00001710 assert (packet_len < (int)sizeof(packet));
Johnny Chen64637202012-05-23 21:09:52 +00001711 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001712 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Johnny Chen64637202012-05-23 21:09:52 +00001713 {
1714 m_supports_watchpoint_support_info = eLazyBoolYes;
1715 std::string name;
1716 std::string value;
1717 while (response.GetNameColonValue(name, value))
1718 {
1719 if (name.compare ("num") == 0)
1720 {
1721 num = Args::StringToUInt32(value.c_str(), 0, 0);
1722 m_num_supported_hardware_watchpoints = num;
1723 }
1724 }
1725 }
1726 else
1727 {
1728 m_supports_watchpoint_support_info = eLazyBoolNo;
1729 }
1730 }
1731
1732 if (m_supports_watchpoint_support_info == eLazyBoolNo)
1733 {
1734 error.SetErrorString("qWatchpointSupportInfo is not supported");
1735 }
1736 return error;
1737
1738}
Greg Clayton46fb5582011-11-18 07:03:08 +00001739
Enrico Granataf04a2192012-07-13 23:18:48 +00001740lldb_private::Error
1741GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num, bool& after)
1742{
1743 Error error(GetWatchpointSupportInfo(num));
1744 if (error.Success())
1745 error = GetWatchpointsTriggerAfterInstruction(after);
1746 return error;
1747}
1748
1749lldb_private::Error
1750GDBRemoteCommunicationClient::GetWatchpointsTriggerAfterInstruction (bool &after)
1751{
1752 Error error;
1753
1754 // we assume watchpoints will happen after running the relevant opcode
1755 // and we only want to override this behavior if we have explicitly
1756 // received a qHostInfo telling us otherwise
1757 if (m_qHostInfo_is_valid != eLazyBoolYes)
1758 after = true;
1759 else
1760 after = (m_watchpoints_trigger_after_instruction != eLazyBoolNo);
1761 return error;
1762}
1763
Greg Clayton576d8832011-03-22 04:00:09 +00001764int
1765GDBRemoteCommunicationClient::SetSTDIN (char const *path)
1766{
1767 if (path && path[0])
1768 {
1769 StreamString packet;
1770 packet.PutCString("QSetSTDIN:");
1771 packet.PutBytesAsRawHex8(path, strlen(path));
1772
1773 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001774 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001775 {
1776 if (response.IsOKResponse())
1777 return 0;
1778 uint8_t error = response.GetError();
1779 if (error)
1780 return error;
1781 }
1782 }
1783 return -1;
1784}
1785
1786int
1787GDBRemoteCommunicationClient::SetSTDOUT (char const *path)
1788{
1789 if (path && path[0])
1790 {
1791 StreamString packet;
1792 packet.PutCString("QSetSTDOUT:");
1793 packet.PutBytesAsRawHex8(path, strlen(path));
1794
1795 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001796 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001797 {
1798 if (response.IsOKResponse())
1799 return 0;
1800 uint8_t error = response.GetError();
1801 if (error)
1802 return error;
1803 }
1804 }
1805 return -1;
1806}
1807
1808int
1809GDBRemoteCommunicationClient::SetSTDERR (char const *path)
1810{
1811 if (path && path[0])
1812 {
1813 StreamString packet;
1814 packet.PutCString("QSetSTDERR:");
1815 packet.PutBytesAsRawHex8(path, strlen(path));
1816
1817 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001818 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001819 {
1820 if (response.IsOKResponse())
1821 return 0;
1822 uint8_t error = response.GetError();
1823 if (error)
1824 return error;
1825 }
1826 }
1827 return -1;
1828}
1829
Greg Claytonfbb76342013-11-20 21:07:01 +00001830bool
1831GDBRemoteCommunicationClient::GetWorkingDir (std::string &cwd)
1832{
1833 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001834 if (SendPacketAndWaitForResponse ("qGetWorkingDir", response, false) == PacketResult::Success)
Greg Claytonfbb76342013-11-20 21:07:01 +00001835 {
1836 if (response.IsUnsupportedResponse())
1837 return false;
1838 if (response.IsErrorResponse())
1839 return false;
1840 response.GetHexByteString (cwd);
1841 return !cwd.empty();
1842 }
1843 return false;
1844}
1845
Greg Clayton576d8832011-03-22 04:00:09 +00001846int
1847GDBRemoteCommunicationClient::SetWorkingDir (char const *path)
1848{
1849 if (path && path[0])
1850 {
1851 StreamString packet;
1852 packet.PutCString("QSetWorkingDir:");
1853 packet.PutBytesAsRawHex8(path, strlen(path));
1854
1855 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001856 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001857 {
1858 if (response.IsOKResponse())
1859 return 0;
1860 uint8_t error = response.GetError();
1861 if (error)
1862 return error;
1863 }
1864 }
1865 return -1;
1866}
1867
1868int
1869GDBRemoteCommunicationClient::SetDisableASLR (bool enable)
1870{
Greg Clayton32e0a752011-03-30 18:16:51 +00001871 char packet[32];
1872 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0);
Andy Gibbsa297a972013-06-19 19:04:53 +00001873 assert (packet_len < (int)sizeof(packet));
Greg Clayton576d8832011-03-22 04:00:09 +00001874 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001875 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001876 {
1877 if (response.IsOKResponse())
1878 return 0;
1879 uint8_t error = response.GetError();
1880 if (error)
1881 return error;
1882 }
1883 return -1;
1884}
Greg Clayton32e0a752011-03-30 18:16:51 +00001885
1886bool
Greg Clayton8b82f082011-04-12 05:54:46 +00001887GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00001888{
1889 if (response.IsNormalResponse())
1890 {
1891 std::string name;
1892 std::string value;
1893 StringExtractor extractor;
1894
1895 while (response.GetNameColonValue(name, value))
1896 {
1897 if (name.compare("pid") == 0)
1898 {
1899 process_info.SetProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1900 }
1901 else if (name.compare("ppid") == 0)
1902 {
1903 process_info.SetParentProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1904 }
1905 else if (name.compare("uid") == 0)
1906 {
Greg Clayton8b82f082011-04-12 05:54:46 +00001907 process_info.SetUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00001908 }
1909 else if (name.compare("euid") == 0)
1910 {
1911 process_info.SetEffectiveUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1912 }
1913 else if (name.compare("gid") == 0)
1914 {
Greg Clayton8b82f082011-04-12 05:54:46 +00001915 process_info.SetGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00001916 }
1917 else if (name.compare("egid") == 0)
1918 {
1919 process_info.SetEffectiveGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1920 }
1921 else if (name.compare("triple") == 0)
1922 {
1923 // The triple comes as ASCII hex bytes since it contains '-' chars
1924 extractor.GetStringRef().swap(value);
1925 extractor.SetFilePos(0);
1926 extractor.GetHexByteString (value);
Greg Clayton70512312012-05-08 01:45:38 +00001927 process_info.GetArchitecture ().SetTriple (value.c_str());
Greg Clayton32e0a752011-03-30 18:16:51 +00001928 }
1929 else if (name.compare("name") == 0)
1930 {
1931 StringExtractor extractor;
Filipe Cabecinhasf86cf782012-05-07 09:30:51 +00001932 // The process name from ASCII hex bytes since we can't
Greg Clayton32e0a752011-03-30 18:16:51 +00001933 // control the characters in a process name
1934 extractor.GetStringRef().swap(value);
1935 extractor.SetFilePos(0);
1936 extractor.GetHexByteString (value);
Greg Clayton144f3a92011-11-15 03:53:30 +00001937 process_info.GetExecutableFile().SetFile (value.c_str(), false);
Greg Clayton32e0a752011-03-30 18:16:51 +00001938 }
1939 }
1940
1941 if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
1942 return true;
1943 }
1944 return false;
1945}
1946
1947bool
Greg Clayton8b82f082011-04-12 05:54:46 +00001948GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00001949{
1950 process_info.Clear();
1951
1952 if (m_supports_qProcessInfoPID)
1953 {
1954 char packet[32];
Daniel Malead01b2952012-11-29 21:49:15 +00001955 const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%" PRIu64, pid);
Andy Gibbsa297a972013-06-19 19:04:53 +00001956 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00001957 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001958 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00001959 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001960 return DecodeProcessInfoResponse (response, process_info);
1961 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00001962 else
1963 {
1964 m_supports_qProcessInfoPID = false;
1965 return false;
1966 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001967 }
1968 return false;
1969}
1970
Jason Molendaf17b5ac2012-12-19 02:54:03 +00001971bool
1972GDBRemoteCommunicationClient::GetCurrentProcessInfo ()
1973{
1974 if (m_qProcessInfo_is_valid == eLazyBoolYes)
1975 return true;
1976 if (m_qProcessInfo_is_valid == eLazyBoolNo)
1977 return false;
1978
1979 GetHostInfo ();
1980
1981 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001982 if (SendPacketAndWaitForResponse ("qProcessInfo", response, false) == PacketResult::Success)
Jason Molendaf17b5ac2012-12-19 02:54:03 +00001983 {
1984 if (response.IsNormalResponse())
1985 {
1986 std::string name;
1987 std::string value;
1988 uint32_t cpu = LLDB_INVALID_CPUTYPE;
1989 uint32_t sub = 0;
1990 std::string arch_name;
1991 std::string os_name;
1992 std::string vendor_name;
1993 std::string triple;
1994 uint32_t pointer_byte_size = 0;
1995 StringExtractor extractor;
1996 ByteOrder byte_order = eByteOrderInvalid;
1997 uint32_t num_keys_decoded = 0;
1998 while (response.GetNameColonValue(name, value))
1999 {
2000 if (name.compare("cputype") == 0)
2001 {
2002 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 16);
2003 if (cpu != LLDB_INVALID_CPUTYPE)
2004 ++num_keys_decoded;
2005 }
2006 else if (name.compare("cpusubtype") == 0)
2007 {
2008 sub = Args::StringToUInt32 (value.c_str(), 0, 16);
2009 if (sub != 0)
2010 ++num_keys_decoded;
2011 }
2012 else if (name.compare("ostype") == 0)
2013 {
2014 os_name.swap (value);
2015 ++num_keys_decoded;
2016 }
2017 else if (name.compare("vendor") == 0)
2018 {
2019 vendor_name.swap(value);
2020 ++num_keys_decoded;
2021 }
2022 else if (name.compare("endian") == 0)
2023 {
2024 ++num_keys_decoded;
2025 if (value.compare("little") == 0)
2026 byte_order = eByteOrderLittle;
2027 else if (value.compare("big") == 0)
2028 byte_order = eByteOrderBig;
2029 else if (value.compare("pdp") == 0)
2030 byte_order = eByteOrderPDP;
2031 else
2032 --num_keys_decoded;
2033 }
2034 else if (name.compare("ptrsize") == 0)
2035 {
2036 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 16);
2037 if (pointer_byte_size != 0)
2038 ++num_keys_decoded;
2039 }
2040 }
2041 if (num_keys_decoded > 0)
2042 m_qProcessInfo_is_valid = eLazyBoolYes;
2043 if (cpu != LLDB_INVALID_CPUTYPE && !os_name.empty() && !vendor_name.empty())
2044 {
2045 m_process_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
2046 if (pointer_byte_size)
2047 {
2048 assert (pointer_byte_size == m_process_arch.GetAddressByteSize());
2049 }
2050 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
2051 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
2052 return true;
2053 }
2054 }
2055 }
2056 else
2057 {
2058 m_qProcessInfo_is_valid = eLazyBoolNo;
2059 }
2060
2061 return false;
2062}
2063
2064
Greg Clayton32e0a752011-03-30 18:16:51 +00002065uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +00002066GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info,
2067 ProcessInstanceInfoList &process_infos)
Greg Clayton32e0a752011-03-30 18:16:51 +00002068{
2069 process_infos.Clear();
2070
2071 if (m_supports_qfProcessInfo)
2072 {
2073 StreamString packet;
2074 packet.PutCString ("qfProcessInfo");
2075 if (!match_info.MatchAllProcesses())
2076 {
2077 packet.PutChar (':');
2078 const char *name = match_info.GetProcessInfo().GetName();
2079 bool has_name_match = false;
2080 if (name && name[0])
2081 {
2082 has_name_match = true;
2083 NameMatchType name_match_type = match_info.GetNameMatchType();
2084 switch (name_match_type)
2085 {
2086 case eNameMatchIgnore:
2087 has_name_match = false;
2088 break;
2089
2090 case eNameMatchEquals:
2091 packet.PutCString ("name_match:equals;");
2092 break;
2093
2094 case eNameMatchContains:
2095 packet.PutCString ("name_match:contains;");
2096 break;
2097
2098 case eNameMatchStartsWith:
2099 packet.PutCString ("name_match:starts_with;");
2100 break;
2101
2102 case eNameMatchEndsWith:
2103 packet.PutCString ("name_match:ends_with;");
2104 break;
2105
2106 case eNameMatchRegularExpression:
2107 packet.PutCString ("name_match:regex;");
2108 break;
2109 }
2110 if (has_name_match)
2111 {
2112 packet.PutCString ("name:");
2113 packet.PutBytesAsRawHex8(name, ::strlen(name));
2114 packet.PutChar (';');
2115 }
2116 }
2117
2118 if (match_info.GetProcessInfo().ProcessIDIsValid())
Daniel Malead01b2952012-11-29 21:49:15 +00002119 packet.Printf("pid:%" PRIu64 ";",match_info.GetProcessInfo().GetProcessID());
Greg Clayton32e0a752011-03-30 18:16:51 +00002120 if (match_info.GetProcessInfo().ParentProcessIDIsValid())
Daniel Malead01b2952012-11-29 21:49:15 +00002121 packet.Printf("parent_pid:%" PRIu64 ";",match_info.GetProcessInfo().GetParentProcessID());
Greg Clayton8b82f082011-04-12 05:54:46 +00002122 if (match_info.GetProcessInfo().UserIDIsValid())
2123 packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID());
2124 if (match_info.GetProcessInfo().GroupIDIsValid())
2125 packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID());
Greg Clayton32e0a752011-03-30 18:16:51 +00002126 if (match_info.GetProcessInfo().EffectiveUserIDIsValid())
2127 packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID());
2128 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
2129 packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID());
2130 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
2131 packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0);
2132 if (match_info.GetProcessInfo().GetArchitecture().IsValid())
2133 {
2134 const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture();
2135 const llvm::Triple &triple = match_arch.GetTriple();
2136 packet.PutCString("triple:");
2137 packet.PutCStringAsRawHex8(triple.getTriple().c_str());
2138 packet.PutChar (';');
2139 }
2140 }
2141 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002142 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002143 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002144 do
2145 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002146 ProcessInstanceInfo process_info;
Greg Clayton32e0a752011-03-30 18:16:51 +00002147 if (!DecodeProcessInfoResponse (response, process_info))
2148 break;
2149 process_infos.Append(process_info);
2150 response.GetStringRef().clear();
2151 response.SetFilePos(0);
Greg Clayton3dedae12013-12-06 21:45:27 +00002152 } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false) == PacketResult::Success);
Greg Clayton32e0a752011-03-30 18:16:51 +00002153 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002154 else
2155 {
2156 m_supports_qfProcessInfo = false;
2157 return 0;
2158 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002159 }
2160 return process_infos.GetSize();
2161
2162}
2163
2164bool
2165GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name)
2166{
2167 if (m_supports_qUserName)
2168 {
2169 char packet[32];
2170 const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002171 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00002172 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002173 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002174 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002175 if (response.IsNormalResponse())
2176 {
2177 // Make sure we parsed the right number of characters. The response is
2178 // the hex encoded user name and should make up the entire packet.
2179 // If there are any non-hex ASCII bytes, the length won't match below..
2180 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
2181 return true;
2182 }
2183 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002184 else
2185 {
2186 m_supports_qUserName = false;
2187 return false;
2188 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002189 }
2190 return false;
2191
2192}
2193
2194bool
2195GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name)
2196{
2197 if (m_supports_qGroupName)
2198 {
2199 char packet[32];
2200 const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002201 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00002202 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002203 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002204 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002205 if (response.IsNormalResponse())
2206 {
2207 // Make sure we parsed the right number of characters. The response is
2208 // the hex encoded group name and should make up the entire packet.
2209 // If there are any non-hex ASCII bytes, the length won't match below..
2210 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
2211 return true;
2212 }
2213 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002214 else
2215 {
2216 m_supports_qGroupName = false;
2217 return false;
2218 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002219 }
2220 return false;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002221}
Greg Clayton32e0a752011-03-30 18:16:51 +00002222
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002223void
2224GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets)
2225{
2226 uint32_t i;
2227 TimeValue start_time, end_time;
2228 uint64_t total_time_nsec;
2229 float packets_per_second;
2230 if (SendSpeedTestPacket (0, 0))
2231 {
2232 for (uint32_t send_size = 0; send_size <= 1024; send_size *= 2)
2233 {
2234 for (uint32_t recv_size = 0; recv_size <= 1024; recv_size *= 2)
2235 {
2236 start_time = TimeValue::Now();
2237 for (i=0; i<num_packets; ++i)
2238 {
2239 SendSpeedTestPacket (send_size, recv_size);
2240 }
2241 end_time = TimeValue::Now();
2242 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourneba23ca02011-06-18 23:52:14 +00002243 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Daniel Malead01b2952012-11-29 21:49:15 +00002244 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 +00002245 num_packets,
2246 send_size,
2247 recv_size,
Peter Collingbourneba23ca02011-06-18 23:52:14 +00002248 total_time_nsec / TimeValue::NanoSecPerSec,
2249 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002250 packets_per_second);
2251 if (recv_size == 0)
2252 recv_size = 32;
2253 }
2254 if (send_size == 0)
2255 send_size = 32;
2256 }
2257 }
2258 else
2259 {
2260 start_time = TimeValue::Now();
2261 for (i=0; i<num_packets; ++i)
2262 {
2263 GetCurrentProcessID ();
2264 }
2265 end_time = TimeValue::Now();
2266 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourneba23ca02011-06-18 23:52:14 +00002267 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Daniel Malead01b2952012-11-29 21:49:15 +00002268 printf ("%u 'qC' packets packets in 0x%" PRIu64 "%9.9" PRIu64 " sec for %f packets/sec.\n",
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002269 num_packets,
Peter Collingbourneba23ca02011-06-18 23:52:14 +00002270 total_time_nsec / TimeValue::NanoSecPerSec,
2271 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002272 packets_per_second);
2273 }
2274}
2275
2276bool
2277GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size)
2278{
2279 StreamString packet;
2280 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size);
2281 uint32_t bytes_left = send_size;
2282 while (bytes_left > 0)
2283 {
2284 if (bytes_left >= 26)
2285 {
2286 packet.PutCString("abcdefghijklmnopqrstuvwxyz");
2287 bytes_left -= 26;
2288 }
2289 else
2290 {
2291 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz");
2292 bytes_left = 0;
2293 }
2294 }
2295
2296 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002297 return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success;
Greg Clayton32e0a752011-03-30 18:16:51 +00002298}
Greg Clayton8b82f082011-04-12 05:54:46 +00002299
2300uint16_t
Greg Claytondbf04572013-12-04 19:40:33 +00002301GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort (lldb::pid_t &pid, const char *remote_accept_hostname)
Greg Clayton8b82f082011-04-12 05:54:46 +00002302{
Daniel Maleae0f8f572013-08-26 23:57:52 +00002303 pid = LLDB_INVALID_PROCESS_ID;
Greg Clayton8b82f082011-04-12 05:54:46 +00002304 StringExtractorGDBRemote response;
Daniel Maleae0f8f572013-08-26 23:57:52 +00002305 StreamString stream;
Greg Clayton29b8fc42013-11-21 01:44:58 +00002306 stream.PutCString("qLaunchGDBServer;");
Daniel Maleae0f8f572013-08-26 23:57:52 +00002307 std::string hostname;
Greg Claytondbf04572013-12-04 19:40:33 +00002308 if (remote_accept_hostname && remote_accept_hostname[0])
2309 hostname = remote_accept_hostname;
Daniel Maleae0f8f572013-08-26 23:57:52 +00002310 else
2311 {
Greg Claytondbf04572013-12-04 19:40:33 +00002312 if (Host::GetHostname (hostname))
2313 {
2314 // Make the GDB server we launch only accept connections from this host
2315 stream.Printf("host:%s;", hostname.c_str());
2316 }
2317 else
2318 {
2319 // Make the GDB server we launch accept connections from any host since we can't figure out the hostname
2320 stream.Printf("host:*;");
2321 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00002322 }
2323 const char *packet = stream.GetData();
2324 int packet_len = stream.GetSize();
2325
Greg Clayton3dedae12013-12-06 21:45:27 +00002326 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002327 {
2328 std::string name;
2329 std::string value;
2330 uint16_t port = 0;
Greg Clayton8b82f082011-04-12 05:54:46 +00002331 while (response.GetNameColonValue(name, value))
2332 {
Daniel Maleae0f8f572013-08-26 23:57:52 +00002333 if (name.compare("port") == 0)
Greg Clayton8b82f082011-04-12 05:54:46 +00002334 port = Args::StringToUInt32(value.c_str(), 0, 0);
Daniel Maleae0f8f572013-08-26 23:57:52 +00002335 else if (name.compare("pid") == 0)
2336 pid = Args::StringToUInt64(value.c_str(), LLDB_INVALID_PROCESS_ID, 0);
Greg Clayton8b82f082011-04-12 05:54:46 +00002337 }
2338 return port;
2339 }
2340 return 0;
2341}
2342
2343bool
Daniel Maleae0f8f572013-08-26 23:57:52 +00002344GDBRemoteCommunicationClient::KillSpawnedProcess (lldb::pid_t pid)
2345{
2346 StreamString stream;
2347 stream.Printf ("qKillSpawnedProcess:%" PRId64 , pid);
2348 const char *packet = stream.GetData();
2349 int packet_len = stream.GetSize();
Sylvestre Ledrufd654c42013-10-06 09:51:02 +00002350
Daniel Maleae0f8f572013-08-26 23:57:52 +00002351 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002352 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00002353 {
2354 if (response.IsOKResponse())
2355 return true;
2356 }
2357 return false;
2358}
2359
2360bool
Jason Molendae9ca4af2013-02-23 02:04:45 +00002361GDBRemoteCommunicationClient::SetCurrentThread (uint64_t tid)
Greg Clayton8b82f082011-04-12 05:54:46 +00002362{
2363 if (m_curr_tid == tid)
2364 return true;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002365
Greg Clayton8b82f082011-04-12 05:54:46 +00002366 char packet[32];
2367 int packet_len;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002368 if (tid == UINT64_MAX)
2369 packet_len = ::snprintf (packet, sizeof(packet), "Hg-1");
Greg Clayton8b82f082011-04-12 05:54:46 +00002370 else
Jason Molendae9ca4af2013-02-23 02:04:45 +00002371 packet_len = ::snprintf (packet, sizeof(packet), "Hg%" PRIx64, tid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002372 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002373 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002374 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002375 {
2376 if (response.IsOKResponse())
2377 {
2378 m_curr_tid = tid;
2379 return true;
2380 }
2381 }
2382 return false;
2383}
2384
2385bool
Jason Molendae9ca4af2013-02-23 02:04:45 +00002386GDBRemoteCommunicationClient::SetCurrentThreadForRun (uint64_t tid)
Greg Clayton8b82f082011-04-12 05:54:46 +00002387{
2388 if (m_curr_tid_run == tid)
2389 return true;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002390
Greg Clayton8b82f082011-04-12 05:54:46 +00002391 char packet[32];
2392 int packet_len;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002393 if (tid == UINT64_MAX)
2394 packet_len = ::snprintf (packet, sizeof(packet), "Hc-1");
Greg Clayton8b82f082011-04-12 05:54:46 +00002395 else
Jason Molendae9ca4af2013-02-23 02:04:45 +00002396 packet_len = ::snprintf (packet, sizeof(packet), "Hc%" PRIx64, tid);
2397
Andy Gibbsa297a972013-06-19 19:04:53 +00002398 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002399 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002400 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002401 {
2402 if (response.IsOKResponse())
2403 {
2404 m_curr_tid_run = tid;
2405 return true;
2406 }
2407 }
2408 return false;
2409}
2410
2411bool
2412GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response)
2413{
Greg Clayton3dedae12013-12-06 21:45:27 +00002414 if (SendPacketAndWaitForResponse("?", 1, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002415 return response.IsNormalResponse();
2416 return false;
2417}
2418
2419bool
Greg Claytonf402f782012-10-13 02:11:55 +00002420GDBRemoteCommunicationClient::GetThreadStopInfo (lldb::tid_t tid, StringExtractorGDBRemote &response)
Greg Clayton8b82f082011-04-12 05:54:46 +00002421{
2422 if (m_supports_qThreadStopInfo)
2423 {
2424 char packet[256];
Daniel Malead01b2952012-11-29 21:49:15 +00002425 int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%" PRIx64, tid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002426 assert (packet_len < (int)sizeof(packet));
Greg Clayton3dedae12013-12-06 21:45:27 +00002427 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002428 {
Greg Claytonef8180a2013-10-15 00:14:28 +00002429 if (response.IsUnsupportedResponse())
2430 m_supports_qThreadStopInfo = false;
2431 else if (response.IsNormalResponse())
Greg Clayton8b82f082011-04-12 05:54:46 +00002432 return true;
2433 else
2434 return false;
2435 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002436 else
2437 {
2438 m_supports_qThreadStopInfo = false;
2439 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002440 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002441 return false;
2442}
2443
2444
2445uint8_t
2446GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length)
2447{
2448 switch (type)
2449 {
2450 case eBreakpointSoftware: if (!m_supports_z0) return UINT8_MAX; break;
2451 case eBreakpointHardware: if (!m_supports_z1) return UINT8_MAX; break;
2452 case eWatchpointWrite: if (!m_supports_z2) return UINT8_MAX; break;
2453 case eWatchpointRead: if (!m_supports_z3) return UINT8_MAX; break;
2454 case eWatchpointReadWrite: if (!m_supports_z4) return UINT8_MAX; break;
Greg Clayton8b82f082011-04-12 05:54:46 +00002455 }
2456
2457 char packet[64];
2458 const int packet_len = ::snprintf (packet,
2459 sizeof(packet),
Daniel Malead01b2952012-11-29 21:49:15 +00002460 "%c%i,%" PRIx64 ",%x",
Greg Clayton8b82f082011-04-12 05:54:46 +00002461 insert ? 'Z' : 'z',
2462 type,
2463 addr,
2464 length);
2465
Andy Gibbsa297a972013-06-19 19:04:53 +00002466 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002467 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002468 if (SendPacketAndWaitForResponse(packet, packet_len, response, true) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002469 {
2470 if (response.IsOKResponse())
2471 return 0;
Greg Clayton8b82f082011-04-12 05:54:46 +00002472 else if (response.IsErrorResponse())
2473 return response.GetError();
2474 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002475 else
2476 {
2477 switch (type)
2478 {
2479 case eBreakpointSoftware: m_supports_z0 = false; break;
2480 case eBreakpointHardware: m_supports_z1 = false; break;
2481 case eWatchpointWrite: m_supports_z2 = false; break;
2482 case eWatchpointRead: m_supports_z3 = false; break;
2483 case eWatchpointReadWrite: m_supports_z4 = false; break;
Greg Clayton17a0cb62011-05-15 23:46:54 +00002484 }
2485 }
2486
Greg Clayton8b82f082011-04-12 05:54:46 +00002487 return UINT8_MAX;
2488}
Greg Claytonadc00cb2011-05-20 23:38:13 +00002489
2490size_t
2491GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
2492 bool &sequence_mutex_unavailable)
2493{
2494 Mutex::Locker locker;
2495 thread_ids.clear();
2496
Jim Ingham4ceb9282012-06-08 22:50:40 +00002497 if (GetSequenceMutex (locker, "ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex"))
Greg Claytonadc00cb2011-05-20 23:38:13 +00002498 {
2499 sequence_mutex_unavailable = false;
2500 StringExtractorGDBRemote response;
2501
Greg Clayton3dedae12013-12-06 21:45:27 +00002502 PacketResult packet_result;
2503 for (packet_result = SendPacketAndWaitForResponseNoLock ("qfThreadInfo", strlen("qfThreadInfo"), response);
2504 packet_result == PacketResult::Success && response.IsNormalResponse();
2505 packet_result = SendPacketAndWaitForResponseNoLock ("qsThreadInfo", strlen("qsThreadInfo"), response))
Greg Claytonadc00cb2011-05-20 23:38:13 +00002506 {
2507 char ch = response.GetChar();
2508 if (ch == 'l')
2509 break;
2510 if (ch == 'm')
2511 {
2512 do
2513 {
Jason Molendae9ca4af2013-02-23 02:04:45 +00002514 tid_t tid = response.GetHexMaxU64(false, LLDB_INVALID_THREAD_ID);
Greg Claytonadc00cb2011-05-20 23:38:13 +00002515
2516 if (tid != LLDB_INVALID_THREAD_ID)
2517 {
2518 thread_ids.push_back (tid);
2519 }
2520 ch = response.GetChar(); // Skip the command separator
2521 } while (ch == ','); // Make sure we got a comma separator
2522 }
2523 }
2524 }
2525 else
2526 {
Jim Ingham4ceb9282012-06-08 22:50:40 +00002527#if defined (LLDB_CONFIGURATION_DEBUG)
2528 // assert(!"ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex");
2529#else
Greg Clayton5160ce52013-03-27 23:08:40 +00002530 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Claytonc3c0b0e2012-04-12 19:04:34 +00002531 if (log)
2532 log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'");
Jim Ingham4ceb9282012-06-08 22:50:40 +00002533#endif
Greg Claytonadc00cb2011-05-20 23:38:13 +00002534 sequence_mutex_unavailable = true;
2535 }
2536 return thread_ids.size();
2537}
Greg Clayton37a0a242012-04-11 00:24:49 +00002538
2539lldb::addr_t
2540GDBRemoteCommunicationClient::GetShlibInfoAddr()
2541{
2542 if (!IsRunning())
2543 {
2544 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002545 if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false) == PacketResult::Success)
Greg Clayton37a0a242012-04-11 00:24:49 +00002546 {
2547 if (response.IsNormalResponse())
2548 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
2549 }
2550 }
2551 return LLDB_INVALID_ADDRESS;
2552}
2553
Daniel Maleae0f8f572013-08-26 23:57:52 +00002554lldb_private::Error
2555GDBRemoteCommunicationClient::RunShellCommand (const char *command, // Shouldn't be NULL
2556 const char *working_dir, // Pass NULL to use the current working directory
2557 int *status_ptr, // Pass NULL if you don't want the process exit status
2558 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
2559 std::string *command_output, // Pass NULL if you don't want the command output
2560 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish
2561{
2562 lldb_private::StreamString stream;
Greg Claytonfbb76342013-11-20 21:07:01 +00002563 stream.PutCString("qPlatform_shell:");
Daniel Maleae0f8f572013-08-26 23:57:52 +00002564 stream.PutBytesAsRawHex8(command, strlen(command));
2565 stream.PutChar(',');
2566 stream.PutHex32(timeout_sec);
2567 if (working_dir && *working_dir)
2568 {
2569 stream.PutChar(',');
2570 stream.PutBytesAsRawHex8(working_dir, strlen(working_dir));
2571 }
2572 const char *packet = stream.GetData();
2573 int packet_len = stream.GetSize();
2574 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002575 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00002576 {
2577 if (response.GetChar() != 'F')
2578 return Error("malformed reply");
2579 if (response.GetChar() != ',')
2580 return Error("malformed reply");
2581 uint32_t exitcode = response.GetHexMaxU32(false, UINT32_MAX);
2582 if (exitcode == UINT32_MAX)
2583 return Error("unable to run remote process");
2584 else if (status_ptr)
2585 *status_ptr = exitcode;
2586 if (response.GetChar() != ',')
2587 return Error("malformed reply");
2588 uint32_t signo = response.GetHexMaxU32(false, UINT32_MAX);
2589 if (signo_ptr)
2590 *signo_ptr = signo;
2591 if (response.GetChar() != ',')
2592 return Error("malformed reply");
2593 std::string output;
2594 response.GetEscapedBinaryData(output);
2595 if (command_output)
2596 command_output->assign(output);
2597 return Error();
2598 }
2599 return Error("unable to send packet");
2600}
2601
Greg Claytonfbb76342013-11-20 21:07:01 +00002602Error
2603GDBRemoteCommunicationClient::MakeDirectory (const char *path,
2604 uint32_t file_permissions)
Daniel Maleae0f8f572013-08-26 23:57:52 +00002605{
2606 lldb_private::StreamString stream;
Greg Claytonfbb76342013-11-20 21:07:01 +00002607 stream.PutCString("qPlatform_mkdir:");
2608 stream.PutHex32(file_permissions);
Daniel Maleae0f8f572013-08-26 23:57:52 +00002609 stream.PutChar(',');
Greg Claytonfbb76342013-11-20 21:07:01 +00002610 stream.PutBytesAsRawHex8(path, strlen(path));
Daniel Maleae0f8f572013-08-26 23:57:52 +00002611 const char *packet = stream.GetData();
2612 int packet_len = stream.GetSize();
2613 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002614 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00002615 {
Greg Claytonfbb76342013-11-20 21:07:01 +00002616 return Error(response.GetHexMaxU32(false, UINT32_MAX), eErrorTypePOSIX);
Daniel Maleae0f8f572013-08-26 23:57:52 +00002617 }
Greg Claytonfbb76342013-11-20 21:07:01 +00002618 return Error();
Daniel Maleae0f8f572013-08-26 23:57:52 +00002619
2620}
2621
Greg Claytonfbb76342013-11-20 21:07:01 +00002622Error
2623GDBRemoteCommunicationClient::SetFilePermissions (const char *path,
2624 uint32_t file_permissions)
2625{
2626 lldb_private::StreamString stream;
2627 stream.PutCString("qPlatform_chmod:");
2628 stream.PutHex32(file_permissions);
2629 stream.PutChar(',');
2630 stream.PutBytesAsRawHex8(path, strlen(path));
2631 const char *packet = stream.GetData();
2632 int packet_len = stream.GetSize();
2633 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002634 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Claytonfbb76342013-11-20 21:07:01 +00002635 {
2636 return Error(response.GetHexMaxU32(false, UINT32_MAX), eErrorTypePOSIX);
2637 }
2638 return Error();
2639
2640}
2641
Daniel Maleae0f8f572013-08-26 23:57:52 +00002642static uint64_t
2643ParseHostIOPacketResponse (StringExtractorGDBRemote &response,
2644 uint64_t fail_result,
2645 Error &error)
2646{
2647 response.SetFilePos(0);
2648 if (response.GetChar() != 'F')
2649 return fail_result;
2650 int32_t result = response.GetS32 (-2);
2651 if (result == -2)
2652 return fail_result;
2653 if (response.GetChar() == ',')
2654 {
2655 int result_errno = response.GetS32 (-2);
2656 if (result_errno != -2)
2657 error.SetError(result_errno, eErrorTypePOSIX);
2658 else
2659 error.SetError(-1, eErrorTypeGeneric);
2660 }
2661 else
2662 error.Clear();
2663 return result;
2664}
2665lldb::user_id_t
2666GDBRemoteCommunicationClient::OpenFile (const lldb_private::FileSpec& file_spec,
2667 uint32_t flags,
2668 mode_t mode,
2669 Error &error)
2670{
2671 lldb_private::StreamString stream;
2672 stream.PutCString("vFile:open:");
2673 std::string path (file_spec.GetPath());
2674 if (path.empty())
2675 return UINT64_MAX;
2676 stream.PutCStringAsRawHex8(path.c_str());
2677 stream.PutChar(',');
2678 const uint32_t posix_open_flags = File::ConvertOpenOptionsForPOSIXOpen(flags);
2679 stream.PutHex32(posix_open_flags);
2680 stream.PutChar(',');
2681 stream.PutHex32(mode);
2682 const char* packet = stream.GetData();
2683 int packet_len = stream.GetSize();
2684 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002685 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00002686 {
2687 return ParseHostIOPacketResponse (response, UINT64_MAX, error);
2688 }
2689 return UINT64_MAX;
2690}
2691
2692bool
2693GDBRemoteCommunicationClient::CloseFile (lldb::user_id_t fd,
2694 Error &error)
2695{
2696 lldb_private::StreamString stream;
2697 stream.Printf("vFile:close:%i", (int)fd);
2698 const char* packet = stream.GetData();
2699 int packet_len = stream.GetSize();
2700 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002701 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00002702 {
2703 return ParseHostIOPacketResponse (response, -1, error) == 0;
2704 }
Deepak Panickald66b50c2013-10-22 12:27:43 +00002705 return false;
Daniel Maleae0f8f572013-08-26 23:57:52 +00002706}
2707
2708// Extension of host I/O packets to get the file size.
2709lldb::user_id_t
2710GDBRemoteCommunicationClient::GetFileSize (const lldb_private::FileSpec& file_spec)
2711{
2712 lldb_private::StreamString stream;
2713 stream.PutCString("vFile:size:");
2714 std::string path (file_spec.GetPath());
2715 stream.PutCStringAsRawHex8(path.c_str());
2716 const char* packet = stream.GetData();
2717 int packet_len = stream.GetSize();
2718 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002719 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00002720 {
2721 if (response.GetChar() != 'F')
2722 return UINT64_MAX;
2723 uint32_t retcode = response.GetHexMaxU64(false, UINT64_MAX);
2724 return retcode;
2725 }
2726 return UINT64_MAX;
2727}
2728
Greg Claytonfbb76342013-11-20 21:07:01 +00002729Error
2730GDBRemoteCommunicationClient::GetFilePermissions(const char *path, uint32_t &file_permissions)
Daniel Maleae0f8f572013-08-26 23:57:52 +00002731{
Greg Claytonfbb76342013-11-20 21:07:01 +00002732 Error error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00002733 lldb_private::StreamString stream;
2734 stream.PutCString("vFile:mode:");
Greg Claytonfbb76342013-11-20 21:07:01 +00002735 stream.PutCStringAsRawHex8(path);
Daniel Maleae0f8f572013-08-26 23:57:52 +00002736 const char* packet = stream.GetData();
2737 int packet_len = stream.GetSize();
2738 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002739 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00002740 {
2741 if (response.GetChar() != 'F')
2742 {
2743 error.SetErrorStringWithFormat ("invalid response to '%s' packet", packet);
Daniel Maleae0f8f572013-08-26 23:57:52 +00002744 }
Greg Claytonfbb76342013-11-20 21:07:01 +00002745 else
Daniel Maleae0f8f572013-08-26 23:57:52 +00002746 {
Greg Claytonfbb76342013-11-20 21:07:01 +00002747 const uint32_t mode = response.GetS32(-1);
2748 if (mode == -1)
Daniel Maleae0f8f572013-08-26 23:57:52 +00002749 {
Greg Claytonfbb76342013-11-20 21:07:01 +00002750 if (response.GetChar() == ',')
2751 {
2752 int response_errno = response.GetS32(-1);
2753 if (response_errno > 0)
2754 error.SetError(response_errno, lldb::eErrorTypePOSIX);
2755 else
2756 error.SetErrorToGenericError();
2757 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00002758 else
2759 error.SetErrorToGenericError();
2760 }
Greg Claytonfbb76342013-11-20 21:07:01 +00002761 else
2762 {
2763 file_permissions = mode & (S_IRWXU|S_IRWXG|S_IRWXO);
2764 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00002765 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00002766 }
2767 else
2768 {
2769 error.SetErrorStringWithFormat ("failed to send '%s' packet", packet);
2770 }
Greg Claytonfbb76342013-11-20 21:07:01 +00002771 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00002772}
2773
2774uint64_t
2775GDBRemoteCommunicationClient::ReadFile (lldb::user_id_t fd,
2776 uint64_t offset,
2777 void *dst,
2778 uint64_t dst_len,
2779 Error &error)
2780{
2781 lldb_private::StreamString stream;
2782 stream.Printf("vFile:pread:%i,%" PRId64 ",%" PRId64, (int)fd, dst_len, offset);
2783 const char* packet = stream.GetData();
2784 int packet_len = stream.GetSize();
2785 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002786 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00002787 {
2788 if (response.GetChar() != 'F')
2789 return 0;
2790 uint32_t retcode = response.GetHexMaxU32(false, UINT32_MAX);
2791 if (retcode == UINT32_MAX)
2792 return retcode;
2793 const char next = (response.Peek() ? *response.Peek() : 0);
2794 if (next == ',')
2795 return 0;
2796 if (next == ';')
2797 {
2798 response.GetChar(); // skip the semicolon
2799 std::string buffer;
2800 if (response.GetEscapedBinaryData(buffer))
2801 {
2802 const uint64_t data_to_write = std::min<uint64_t>(dst_len, buffer.size());
2803 if (data_to_write > 0)
2804 memcpy(dst, &buffer[0], data_to_write);
2805 return data_to_write;
2806 }
2807 }
2808 }
2809 return 0;
2810}
2811
2812uint64_t
2813GDBRemoteCommunicationClient::WriteFile (lldb::user_id_t fd,
2814 uint64_t offset,
2815 const void* src,
2816 uint64_t src_len,
2817 Error &error)
2818{
2819 lldb_private::StreamGDBRemote stream;
2820 stream.Printf("vFile:pwrite:%i,%" PRId64 ",", (int)fd, offset);
2821 stream.PutEscapedBytes(src, src_len);
2822 const char* packet = stream.GetData();
2823 int packet_len = stream.GetSize();
2824 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002825 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00002826 {
2827 if (response.GetChar() != 'F')
2828 {
2829 error.SetErrorStringWithFormat("write file failed");
2830 return 0;
2831 }
2832 uint64_t bytes_written = response.GetU64(UINT64_MAX);
2833 if (bytes_written == UINT64_MAX)
2834 {
2835 error.SetErrorToGenericError();
2836 if (response.GetChar() == ',')
2837 {
2838 int response_errno = response.GetS32(-1);
2839 if (response_errno > 0)
2840 error.SetError(response_errno, lldb::eErrorTypePOSIX);
2841 }
2842 return 0;
2843 }
2844 return bytes_written;
2845 }
2846 else
2847 {
2848 error.SetErrorString ("failed to send vFile:pwrite packet");
2849 }
2850 return 0;
2851}
2852
Greg Claytonfbb76342013-11-20 21:07:01 +00002853Error
2854GDBRemoteCommunicationClient::CreateSymlink (const char *src, const char *dst)
2855{
2856 Error error;
2857 lldb_private::StreamGDBRemote stream;
2858 stream.PutCString("vFile:symlink:");
2859 // the unix symlink() command reverses its parameters where the dst if first,
2860 // so we follow suit here
2861 stream.PutCStringAsRawHex8(dst);
2862 stream.PutChar(',');
2863 stream.PutCStringAsRawHex8(src);
2864 const char* packet = stream.GetData();
2865 int packet_len = stream.GetSize();
2866 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002867 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Claytonfbb76342013-11-20 21:07:01 +00002868 {
2869 if (response.GetChar() == 'F')
2870 {
2871 uint32_t result = response.GetU32(UINT32_MAX);
2872 if (result != 0)
2873 {
2874 error.SetErrorToGenericError();
2875 if (response.GetChar() == ',')
2876 {
2877 int response_errno = response.GetS32(-1);
2878 if (response_errno > 0)
2879 error.SetError(response_errno, lldb::eErrorTypePOSIX);
2880 }
2881 }
2882 }
2883 else
2884 {
2885 // Should have returned with 'F<result>[,<errno>]'
2886 error.SetErrorStringWithFormat("symlink failed");
2887 }
2888 }
2889 else
2890 {
2891 error.SetErrorString ("failed to send vFile:symlink packet");
2892 }
2893 return error;
2894}
2895
2896Error
2897GDBRemoteCommunicationClient::Unlink (const char *path)
2898{
2899 Error error;
2900 lldb_private::StreamGDBRemote stream;
2901 stream.PutCString("vFile:unlink:");
2902 // the unix symlink() command reverses its parameters where the dst if first,
2903 // so we follow suit here
2904 stream.PutCStringAsRawHex8(path);
2905 const char* packet = stream.GetData();
2906 int packet_len = stream.GetSize();
2907 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002908 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Claytonfbb76342013-11-20 21:07:01 +00002909 {
2910 if (response.GetChar() == 'F')
2911 {
2912 uint32_t result = response.GetU32(UINT32_MAX);
2913 if (result != 0)
2914 {
2915 error.SetErrorToGenericError();
2916 if (response.GetChar() == ',')
2917 {
2918 int response_errno = response.GetS32(-1);
2919 if (response_errno > 0)
2920 error.SetError(response_errno, lldb::eErrorTypePOSIX);
2921 }
2922 }
2923 }
2924 else
2925 {
2926 // Should have returned with 'F<result>[,<errno>]'
2927 error.SetErrorStringWithFormat("unlink failed");
2928 }
2929 }
2930 else
2931 {
2932 error.SetErrorString ("failed to send vFile:unlink packet");
2933 }
2934 return error;
2935}
2936
Daniel Maleae0f8f572013-08-26 23:57:52 +00002937// Extension of host I/O packets to get whether a file exists.
2938bool
2939GDBRemoteCommunicationClient::GetFileExists (const lldb_private::FileSpec& file_spec)
2940{
2941 lldb_private::StreamString stream;
2942 stream.PutCString("vFile:exists:");
2943 std::string path (file_spec.GetPath());
2944 stream.PutCStringAsRawHex8(path.c_str());
2945 const char* packet = stream.GetData();
2946 int packet_len = stream.GetSize();
2947 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002948 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00002949 {
2950 if (response.GetChar() != 'F')
2951 return false;
2952 if (response.GetChar() != ',')
2953 return false;
2954 bool retcode = (response.GetChar() != '0');
2955 return retcode;
2956 }
2957 return false;
2958}
2959
2960bool
2961GDBRemoteCommunicationClient::CalculateMD5 (const lldb_private::FileSpec& file_spec,
2962 uint64_t &high,
2963 uint64_t &low)
2964{
2965 lldb_private::StreamString stream;
2966 stream.PutCString("vFile:MD5:");
2967 std::string path (file_spec.GetPath());
2968 stream.PutCStringAsRawHex8(path.c_str());
2969 const char* packet = stream.GetData();
2970 int packet_len = stream.GetSize();
2971 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002972 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00002973 {
2974 if (response.GetChar() != 'F')
2975 return false;
2976 if (response.GetChar() != ',')
2977 return false;
2978 if (response.Peek() && *response.Peek() == 'x')
2979 return false;
2980 low = response.GetHexMaxU64(false, UINT64_MAX);
2981 high = response.GetHexMaxU64(false, UINT64_MAX);
2982 return true;
2983 }
2984 return false;
2985}
Greg Claytonf74cf862013-11-13 23:28:31 +00002986
2987bool
2988GDBRemoteCommunicationClient::ReadRegister(lldb::tid_t tid, uint32_t reg, StringExtractorGDBRemote &response)
2989{
2990 Mutex::Locker locker;
2991 if (GetSequenceMutex (locker, "Didn't get sequence mutex for p packet."))
2992 {
2993 const bool thread_suffix_supported = GetThreadSuffixSupported();
2994
2995 if (thread_suffix_supported || SetCurrentThread(tid))
2996 {
2997 char packet[64];
2998 int packet_len = 0;
2999 if (thread_suffix_supported)
3000 packet_len = ::snprintf (packet, sizeof(packet), "p%x;thread:%4.4" PRIx64 ";", reg, tid);
3001 else
3002 packet_len = ::snprintf (packet, sizeof(packet), "p%x", reg);
3003 assert (packet_len < ((int)sizeof(packet) - 1));
Greg Clayton3dedae12013-12-06 21:45:27 +00003004 return SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success;
Greg Claytonf74cf862013-11-13 23:28:31 +00003005 }
3006 }
3007 return false;
3008
3009}
3010
3011
3012bool
3013GDBRemoteCommunicationClient::ReadAllRegisters (lldb::tid_t tid, StringExtractorGDBRemote &response)
3014{
3015 Mutex::Locker locker;
3016 if (GetSequenceMutex (locker, "Didn't get sequence mutex for g packet."))
3017 {
3018 const bool thread_suffix_supported = GetThreadSuffixSupported();
3019
3020 if (thread_suffix_supported || SetCurrentThread(tid))
3021 {
3022 char packet[64];
3023 int packet_len = 0;
3024 // Get all registers in one packet
3025 if (thread_suffix_supported)
3026 packet_len = ::snprintf (packet, sizeof(packet), "g;thread:%4.4" PRIx64 ";", tid);
3027 else
3028 packet_len = ::snprintf (packet, sizeof(packet), "g");
3029 assert (packet_len < ((int)sizeof(packet) - 1));
Greg Clayton3dedae12013-12-06 21:45:27 +00003030 return SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success;
Greg Claytonf74cf862013-11-13 23:28:31 +00003031 }
3032 }
3033 return false;
3034}
3035bool
3036GDBRemoteCommunicationClient::SaveRegisterState (lldb::tid_t tid, uint32_t &save_id)
3037{
3038 save_id = 0; // Set to invalid save ID
3039 if (m_supports_QSaveRegisterState == eLazyBoolNo)
3040 return false;
3041
3042 m_supports_QSaveRegisterState = eLazyBoolYes;
3043 Mutex::Locker locker;
3044 if (GetSequenceMutex (locker, "Didn't get sequence mutex for QSaveRegisterState."))
3045 {
3046 const bool thread_suffix_supported = GetThreadSuffixSupported();
3047 if (thread_suffix_supported || SetCurrentThread(tid))
3048 {
3049 char packet[256];
3050 if (thread_suffix_supported)
3051 ::snprintf (packet, sizeof(packet), "QSaveRegisterState;thread:%4.4" PRIx64 ";", tid);
3052 else
3053 ::strncpy (packet, "QSaveRegisterState", sizeof(packet));
3054
3055 StringExtractorGDBRemote response;
3056
Greg Clayton3dedae12013-12-06 21:45:27 +00003057 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
Greg Claytonf74cf862013-11-13 23:28:31 +00003058 {
3059 if (response.IsUnsupportedResponse())
3060 {
3061 // This packet isn't supported, don't try calling it again
3062 m_supports_QSaveRegisterState = eLazyBoolNo;
3063 }
3064
3065 const uint32_t response_save_id = response.GetU32(0);
3066 if (response_save_id != 0)
3067 {
3068 save_id = response_save_id;
3069 return true;
3070 }
3071 }
3072 }
3073 }
3074 return false;
3075}
3076
3077bool
3078GDBRemoteCommunicationClient::RestoreRegisterState (lldb::tid_t tid, uint32_t save_id)
3079{
3080 // We use the "m_supports_QSaveRegisterState" variable here becuase the
3081 // QSaveRegisterState and QRestoreRegisterState packets must both be supported in
3082 // order to be useful
3083 if (m_supports_QSaveRegisterState == eLazyBoolNo)
3084 return false;
3085
3086 Mutex::Locker locker;
3087 if (GetSequenceMutex (locker, "Didn't get sequence mutex for QRestoreRegisterState."))
3088 {
3089 const bool thread_suffix_supported = GetThreadSuffixSupported();
3090 if (thread_suffix_supported || SetCurrentThread(tid))
3091 {
3092 char packet[256];
3093 if (thread_suffix_supported)
3094 ::snprintf (packet, sizeof(packet), "QRestoreRegisterState:%u;thread:%4.4" PRIx64 ";", save_id, tid);
3095 else
3096 ::snprintf (packet, sizeof(packet), "QRestoreRegisterState:%u" PRIx64 ";", save_id);
3097
3098 StringExtractorGDBRemote response;
3099
Greg Clayton3dedae12013-12-06 21:45:27 +00003100 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
Greg Claytonf74cf862013-11-13 23:28:31 +00003101 {
3102 if (response.IsOKResponse())
3103 {
3104 return true;
3105 }
3106 else if (response.IsUnsupportedResponse())
3107 {
3108 // This packet isn't supported, don't try calling this packet or
3109 // QSaveRegisterState again...
3110 m_supports_QSaveRegisterState = eLazyBoolNo;
3111 }
3112 }
3113 }
3114 }
3115 return false;
3116}