blob: b4263d18ca637f2c682d04e43ba2f1c4b54eddf3 [file] [log] [blame]
Greg Clayton61d043b2011-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
14// C++ Includes
15// Other libraries and framework includes
16#include "llvm/ADT/Triple.h"
17#include "lldb/Interpreter/Args.h"
18#include "lldb/Core/ConnectionFileDescriptor.h"
19#include "lldb/Core/Log.h"
20#include "lldb/Core/State.h"
21#include "lldb/Core/StreamString.h"
22#include "lldb/Host/Endian.h"
23#include "lldb/Host/Host.h"
24#include "lldb/Host/TimeValue.h"
25
26// Project includes
27#include "Utility/StringExtractorGDBRemote.h"
28#include "ProcessGDBRemote.h"
29#include "ProcessGDBRemoteLog.h"
30
31using namespace lldb;
32using namespace lldb_private;
33
34//----------------------------------------------------------------------
35// GDBRemoteCommunicationClient constructor
36//----------------------------------------------------------------------
Greg Claytonb72d0f02011-04-12 05:54:46 +000037GDBRemoteCommunicationClient::GDBRemoteCommunicationClient(bool is_platform) :
38 GDBRemoteCommunication("gdb-remote.client", "gdb-remote.client.rx_packet", is_platform),
Greg Clayton61d043b2011-03-22 04:00:09 +000039 m_supports_not_sending_acks (eLazyBoolCalculate),
40 m_supports_thread_suffix (eLazyBoolCalculate),
Greg Claytona1f645e2012-04-10 03:22:03 +000041 m_supports_threads_in_stop_reply (eLazyBoolCalculate),
Greg Clayton61d043b2011-03-22 04:00:09 +000042 m_supports_vCont_all (eLazyBoolCalculate),
43 m_supports_vCont_any (eLazyBoolCalculate),
44 m_supports_vCont_c (eLazyBoolCalculate),
45 m_supports_vCont_C (eLazyBoolCalculate),
46 m_supports_vCont_s (eLazyBoolCalculate),
47 m_supports_vCont_S (eLazyBoolCalculate),
Greg Clayton24bc5d92011-03-30 18:16:51 +000048 m_qHostInfo_is_valid (eLazyBoolCalculate),
Greg Clayton2f085c62011-05-15 01:25:55 +000049 m_supports_alloc_dealloc_memory (eLazyBoolCalculate),
Greg Claytona9385532011-11-18 07:03:08 +000050 m_supports_memory_region_info (eLazyBoolCalculate),
Johnny Chen7cbdcfb2012-05-23 21:09:52 +000051 m_supports_watchpoint_support_info (eLazyBoolCalculate),
Enrico Granata7de2a3b2012-07-13 23:18:48 +000052 m_watchpoints_trigger_after_instruction(eLazyBoolCalculate),
Jim Ingham3a458eb2012-07-20 21:37:13 +000053 m_attach_or_wait_reply(eLazyBoolCalculate),
Greg Clayton24bc5d92011-03-30 18:16:51 +000054 m_supports_qProcessInfoPID (true),
55 m_supports_qfProcessInfo (true),
56 m_supports_qUserName (true),
57 m_supports_qGroupName (true),
Greg Claytonb72d0f02011-04-12 05:54:46 +000058 m_supports_qThreadStopInfo (true),
59 m_supports_z0 (true),
60 m_supports_z1 (true),
61 m_supports_z2 (true),
62 m_supports_z3 (true),
63 m_supports_z4 (true),
64 m_curr_tid (LLDB_INVALID_THREAD_ID),
65 m_curr_tid_run (LLDB_INVALID_THREAD_ID),
Johnny Chen7cbdcfb2012-05-23 21:09:52 +000066 m_num_supported_hardware_watchpoints (0),
Greg Clayton61d043b2011-03-22 04:00:09 +000067 m_async_mutex (Mutex::eMutexTypeRecursive),
68 m_async_packet_predicate (false),
69 m_async_packet (),
70 m_async_response (),
71 m_async_signal (-1),
Greg Clayton58e26e02011-03-24 04:28:38 +000072 m_host_arch(),
73 m_os_version_major (UINT32_MAX),
74 m_os_version_minor (UINT32_MAX),
75 m_os_version_update (UINT32_MAX)
Greg Clayton61d043b2011-03-22 04:00:09 +000076{
Greg Clayton61d043b2011-03-22 04:00:09 +000077}
78
79//----------------------------------------------------------------------
80// Destructor
81//----------------------------------------------------------------------
82GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient()
83{
Greg Clayton61d043b2011-03-22 04:00:09 +000084 if (IsConnected())
Greg Clayton61d043b2011-03-22 04:00:09 +000085 Disconnect();
Greg Clayton61d043b2011-03-22 04:00:09 +000086}
87
88bool
Greg Clayton58e26e02011-03-24 04:28:38 +000089GDBRemoteCommunicationClient::HandshakeWithServer (Error *error_ptr)
90{
91 // Start the read thread after we send the handshake ack since if we
92 // fail to send the handshake ack, there is no reason to continue...
93 if (SendAck())
Greg Clayton63afdb02011-06-17 01:22:15 +000094 return true;
Greg Clayton58e26e02011-03-24 04:28:38 +000095
96 if (error_ptr)
97 error_ptr->SetErrorString("failed to send the handshake ack");
98 return false;
99}
100
101void
102GDBRemoteCommunicationClient::QueryNoAckModeSupported ()
Greg Clayton61d043b2011-03-22 04:00:09 +0000103{
104 if (m_supports_not_sending_acks == eLazyBoolCalculate)
105 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000106 m_send_acks = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000107 m_supports_not_sending_acks = eLazyBoolNo;
Greg Clayton58e26e02011-03-24 04:28:38 +0000108
109 StringExtractorGDBRemote response;
Greg Clayton61d043b2011-03-22 04:00:09 +0000110 if (SendPacketAndWaitForResponse("QStartNoAckMode", response, false))
111 {
112 if (response.IsOKResponse())
Greg Clayton58e26e02011-03-24 04:28:38 +0000113 {
114 m_send_acks = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000115 m_supports_not_sending_acks = eLazyBoolYes;
Greg Clayton58e26e02011-03-24 04:28:38 +0000116 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000117 }
118 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000119}
120
121void
Greg Claytona1f645e2012-04-10 03:22:03 +0000122GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported ()
123{
124 if (m_supports_threads_in_stop_reply == eLazyBoolCalculate)
125 {
126 m_supports_threads_in_stop_reply = eLazyBoolNo;
127
128 StringExtractorGDBRemote response;
129 if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response, false))
130 {
131 if (response.IsOKResponse())
132 m_supports_threads_in_stop_reply = eLazyBoolYes;
133 }
134 }
135}
136
Jim Ingham3a458eb2012-07-20 21:37:13 +0000137bool
138GDBRemoteCommunicationClient::GetVAttachOrWaitSupported ()
139{
140 if (m_attach_or_wait_reply == eLazyBoolCalculate)
141 {
142 m_attach_or_wait_reply = eLazyBoolNo;
143
144 StringExtractorGDBRemote response;
145 if (SendPacketAndWaitForResponse("qVAttachOrWaitSupported", response, false))
146 {
147 if (response.IsOKResponse())
148 m_attach_or_wait_reply = eLazyBoolYes;
149 }
150 }
151 if (m_attach_or_wait_reply == eLazyBoolYes)
152 return true;
153 else
154 return false;
155}
156
Greg Claytona1f645e2012-04-10 03:22:03 +0000157
158void
Greg Clayton61d043b2011-03-22 04:00:09 +0000159GDBRemoteCommunicationClient::ResetDiscoverableSettings()
160{
161 m_supports_not_sending_acks = eLazyBoolCalculate;
162 m_supports_thread_suffix = eLazyBoolCalculate;
Greg Claytona1f645e2012-04-10 03:22:03 +0000163 m_supports_threads_in_stop_reply = eLazyBoolCalculate;
Greg Clayton61d043b2011-03-22 04:00:09 +0000164 m_supports_vCont_c = eLazyBoolCalculate;
165 m_supports_vCont_C = eLazyBoolCalculate;
166 m_supports_vCont_s = eLazyBoolCalculate;
167 m_supports_vCont_S = eLazyBoolCalculate;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000168 m_qHostInfo_is_valid = eLazyBoolCalculate;
Greg Clayton2f085c62011-05-15 01:25:55 +0000169 m_supports_alloc_dealloc_memory = eLazyBoolCalculate;
Greg Claytona9385532011-11-18 07:03:08 +0000170 m_supports_memory_region_info = eLazyBoolCalculate;
Greg Clayton989816b2011-05-14 01:50:35 +0000171
Greg Clayton24bc5d92011-03-30 18:16:51 +0000172 m_supports_qProcessInfoPID = true;
173 m_supports_qfProcessInfo = true;
174 m_supports_qUserName = true;
175 m_supports_qGroupName = true;
Greg Claytonb72d0f02011-04-12 05:54:46 +0000176 m_supports_qThreadStopInfo = true;
177 m_supports_z0 = true;
178 m_supports_z1 = true;
179 m_supports_z2 = true;
180 m_supports_z3 = true;
181 m_supports_z4 = true;
Greg Claytoncb8977d2011-03-23 00:09:55 +0000182 m_host_arch.Clear();
Greg Clayton61d043b2011-03-22 04:00:09 +0000183}
184
185
186bool
187GDBRemoteCommunicationClient::GetThreadSuffixSupported ()
188{
189 if (m_supports_thread_suffix == eLazyBoolCalculate)
190 {
191 StringExtractorGDBRemote response;
192 m_supports_thread_suffix = eLazyBoolNo;
193 if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response, false))
194 {
195 if (response.IsOKResponse())
196 m_supports_thread_suffix = eLazyBoolYes;
197 }
198 }
199 return m_supports_thread_suffix;
200}
201bool
202GDBRemoteCommunicationClient::GetVContSupported (char flavor)
203{
204 if (m_supports_vCont_c == eLazyBoolCalculate)
205 {
206 StringExtractorGDBRemote response;
207 m_supports_vCont_any = eLazyBoolNo;
208 m_supports_vCont_all = eLazyBoolNo;
209 m_supports_vCont_c = eLazyBoolNo;
210 m_supports_vCont_C = eLazyBoolNo;
211 m_supports_vCont_s = eLazyBoolNo;
212 m_supports_vCont_S = eLazyBoolNo;
213 if (SendPacketAndWaitForResponse("vCont?", response, false))
214 {
215 const char *response_cstr = response.GetStringRef().c_str();
216 if (::strstr (response_cstr, ";c"))
217 m_supports_vCont_c = eLazyBoolYes;
218
219 if (::strstr (response_cstr, ";C"))
220 m_supports_vCont_C = eLazyBoolYes;
221
222 if (::strstr (response_cstr, ";s"))
223 m_supports_vCont_s = eLazyBoolYes;
224
225 if (::strstr (response_cstr, ";S"))
226 m_supports_vCont_S = eLazyBoolYes;
227
228 if (m_supports_vCont_c == eLazyBoolYes &&
229 m_supports_vCont_C == eLazyBoolYes &&
230 m_supports_vCont_s == eLazyBoolYes &&
231 m_supports_vCont_S == eLazyBoolYes)
232 {
233 m_supports_vCont_all = eLazyBoolYes;
234 }
235
236 if (m_supports_vCont_c == eLazyBoolYes ||
237 m_supports_vCont_C == eLazyBoolYes ||
238 m_supports_vCont_s == eLazyBoolYes ||
239 m_supports_vCont_S == eLazyBoolYes)
240 {
241 m_supports_vCont_any = eLazyBoolYes;
242 }
243 }
244 }
245
246 switch (flavor)
247 {
248 case 'a': return m_supports_vCont_any;
249 case 'A': return m_supports_vCont_all;
250 case 'c': return m_supports_vCont_c;
251 case 'C': return m_supports_vCont_C;
252 case 's': return m_supports_vCont_s;
253 case 'S': return m_supports_vCont_S;
254 default: break;
255 }
256 return false;
257}
258
259
260size_t
261GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
262(
263 const char *payload,
264 StringExtractorGDBRemote &response,
265 bool send_async
266)
267{
268 return SendPacketAndWaitForResponse (payload,
269 ::strlen (payload),
270 response,
271 send_async);
272}
273
274size_t
275GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
276(
277 const char *payload,
278 size_t payload_length,
279 StringExtractorGDBRemote &response,
280 bool send_async
281)
282{
283 Mutex::Locker locker;
Greg Clayton61d043b2011-03-22 04:00:09 +0000284 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton801417e2011-07-07 01:59:51 +0000285 size_t response_len = 0;
Greg Claytonc8dd5702012-04-12 19:04:34 +0000286 if (GetSequenceMutex (locker))
Greg Clayton61d043b2011-03-22 04:00:09 +0000287 {
Greg Clayton139da722011-05-20 03:15:54 +0000288 if (SendPacketNoLock (payload, payload_length))
Greg Clayton801417e2011-07-07 01:59:51 +0000289 response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
290 else
291 {
292 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000293 log->Printf("error: failed to send '%*s'", (int) payload_length, payload);
Greg Clayton801417e2011-07-07 01:59:51 +0000294 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000295 }
296 else
297 {
298 if (send_async)
299 {
Greg Clayton70e167f2012-05-31 21:24:20 +0000300 if (IsRunning())
Greg Clayton61d043b2011-03-22 04:00:09 +0000301 {
Greg Clayton70e167f2012-05-31 21:24:20 +0000302 Mutex::Locker async_locker (m_async_mutex);
303 m_async_packet.assign(payload, payload_length);
304 m_async_packet_predicate.SetValue (true, eBroadcastNever);
305
306 if (log)
307 log->Printf ("async: async packet = %s", m_async_packet.c_str());
308
309 bool timed_out = false;
310 if (SendInterrupt(locker, 2, timed_out))
Greg Clayton61d043b2011-03-22 04:00:09 +0000311 {
Greg Clayton70e167f2012-05-31 21:24:20 +0000312 if (m_interrupt_sent)
Greg Clayton61d043b2011-03-22 04:00:09 +0000313 {
Jim Ingham8247e622012-06-06 00:32:39 +0000314 m_interrupt_sent = false;
Greg Clayton70e167f2012-05-31 21:24:20 +0000315 TimeValue timeout_time;
316 timeout_time = TimeValue::Now();
317 timeout_time.OffsetWithSeconds (m_packet_timeout);
318
Greg Clayton61d043b2011-03-22 04:00:09 +0000319 if (log)
Greg Clayton70e167f2012-05-31 21:24:20 +0000320 log->Printf ("async: sent interrupt");
Greg Clayton801417e2011-07-07 01:59:51 +0000321
Greg Clayton70e167f2012-05-31 21:24:20 +0000322 if (m_async_packet_predicate.WaitForValueEqualTo (false, &timeout_time, &timed_out))
Greg Claytonb7669b32011-10-27 22:04:16 +0000323 {
Greg Clayton70e167f2012-05-31 21:24:20 +0000324 if (log)
325 log->Printf ("async: got response");
326
327 // Swap the response buffer to avoid malloc and string copy
328 response.GetStringRef().swap (m_async_response.GetStringRef());
329 response_len = response.GetStringRef().size();
330 }
331 else
332 {
333 if (log)
334 log->Printf ("async: timed out waiting for response");
335 }
336
337 // Make sure we wait until the continue packet has been sent again...
338 if (m_private_is_running.WaitForValueEqualTo (true, &timeout_time, &timed_out))
339 {
340 if (log)
341 {
342 if (timed_out)
343 log->Printf ("async: timed out waiting for process to resume, but process was resumed");
344 else
345 log->Printf ("async: async packet sent");
346 }
347 }
348 else
349 {
350 if (log)
351 log->Printf ("async: timed out waiting for process to resume");
Greg Claytonb7669b32011-10-27 22:04:16 +0000352 }
353 }
354 else
355 {
Greg Clayton70e167f2012-05-31 21:24:20 +0000356 // We had a racy condition where we went to send the interrupt
357 // yet we were able to get the lock, so the process must have
358 // just stopped?
Greg Clayton61d043b2011-03-22 04:00:09 +0000359 if (log)
Greg Clayton70e167f2012-05-31 21:24:20 +0000360 log->Printf ("async: got lock without sending interrupt");
361 // Send the packet normally since we got the lock
362 if (SendPacketNoLock (payload, payload_length))
363 response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
364 else
365 {
366 if (log)
367 log->Printf("error: failed to send '%*s'", (int) payload_length, payload);
368 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000369 }
370 }
371 else
372 {
Greg Clayton801417e2011-07-07 01:59:51 +0000373 if (log)
Greg Clayton70e167f2012-05-31 21:24:20 +0000374 log->Printf ("async: failed to interrupt");
Greg Clayton61d043b2011-03-22 04:00:09 +0000375 }
376 }
377 else
378 {
379 if (log)
Greg Clayton70e167f2012-05-31 21:24:20 +0000380 log->Printf ("async: not running, async is ignored");
Greg Clayton61d043b2011-03-22 04:00:09 +0000381 }
382 }
383 else
384 {
385 if (log)
Greg Claytonc8dd5702012-04-12 19:04:34 +0000386 log->Printf("error: failed to get packet sequence mutex, not sending packet '%*s'", (int) payload_length, payload);
Greg Clayton61d043b2011-03-22 04:00:09 +0000387 }
388 }
Greg Clayton801417e2011-07-07 01:59:51 +0000389 if (response_len == 0)
390 {
391 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000392 log->Printf("error: failed to get response for '%*s'", (int) payload_length, payload);
Greg Clayton801417e2011-07-07 01:59:51 +0000393 }
394 return response_len;
Greg Clayton61d043b2011-03-22 04:00:09 +0000395}
396
Greg Clayton61d043b2011-03-22 04:00:09 +0000397StateType
398GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse
399(
400 ProcessGDBRemote *process,
401 const char *payload,
402 size_t packet_length,
403 StringExtractorGDBRemote &response
404)
405{
Greg Clayton0d2c5412012-07-02 22:05:25 +0000406 m_curr_tid = LLDB_INVALID_THREAD_ID;
Greg Clayton61d043b2011-03-22 04:00:09 +0000407 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
408 if (log)
409 log->Printf ("GDBRemoteCommunicationClient::%s ()", __FUNCTION__);
410
411 Mutex::Locker locker(m_sequence_mutex);
412 StateType state = eStateRunning;
413
414 BroadcastEvent(eBroadcastBitRunPacketSent, NULL);
415 m_public_is_running.SetValue (true, eBroadcastNever);
416 // Set the starting continue packet into "continue_packet". This packet
Jim Ingham8247e622012-06-06 00:32:39 +0000417 // may change if we are interrupted and we continue after an async packet...
Greg Clayton61d043b2011-03-22 04:00:09 +0000418 std::string continue_packet(payload, packet_length);
419
Greg Clayton628cead2011-05-19 03:54:16 +0000420 bool got_stdout = false;
421
Greg Clayton61d043b2011-03-22 04:00:09 +0000422 while (state == eStateRunning)
423 {
Greg Clayton628cead2011-05-19 03:54:16 +0000424 if (!got_stdout)
425 {
426 if (log)
427 log->Printf ("GDBRemoteCommunicationClient::%s () sending continue packet: %s", __FUNCTION__, continue_packet.c_str());
Greg Clayton516f0842012-04-11 00:24:49 +0000428 if (SendPacketNoLock(continue_packet.c_str(), continue_packet.size()) == 0)
Greg Clayton628cead2011-05-19 03:54:16 +0000429 state = eStateInvalid;
Greg Clayton61d043b2011-03-22 04:00:09 +0000430
Greg Claytonb7669b32011-10-27 22:04:16 +0000431 m_private_is_running.SetValue (true, eBroadcastAlways);
Greg Clayton628cead2011-05-19 03:54:16 +0000432 }
433
434 got_stdout = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000435
436 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000437 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(%s)", __FUNCTION__, continue_packet.c_str());
Greg Clayton61d043b2011-03-22 04:00:09 +0000438
Greg Clayton516f0842012-04-11 00:24:49 +0000439 if (WaitForPacketWithTimeoutMicroSecondsNoLock(response, UINT32_MAX))
Greg Clayton61d043b2011-03-22 04:00:09 +0000440 {
441 if (response.Empty())
442 state = eStateInvalid;
443 else
444 {
445 const char stop_type = response.GetChar();
446 if (log)
447 log->Printf ("GDBRemoteCommunicationClient::%s () got packet: %s", __FUNCTION__, response.GetStringRef().c_str());
448 switch (stop_type)
449 {
450 case 'T':
451 case 'S':
Greg Clayton61d043b2011-03-22 04:00:09 +0000452 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000453 if (process->GetStopID() == 0)
Greg Clayton61d043b2011-03-22 04:00:09 +0000454 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000455 if (process->GetID() == LLDB_INVALID_PROCESS_ID)
456 {
457 lldb::pid_t pid = GetCurrentProcessID ();
458 if (pid != LLDB_INVALID_PROCESS_ID)
459 process->SetID (pid);
460 }
461 process->BuildDynamicRegisterInfo (true);
Greg Clayton61d043b2011-03-22 04:00:09 +0000462 }
Greg Clayton05e4d972012-03-29 01:55:41 +0000463
464 // Privately notify any internal threads that we have stopped
465 // in case we wanted to interrupt our process, yet we might
466 // send a packet and continue without returning control to the
467 // user.
468 m_private_is_running.SetValue (false, eBroadcastAlways);
469
470 const uint8_t signo = response.GetHexU8 (UINT8_MAX);
471
Jim Ingham8247e622012-06-06 00:32:39 +0000472 bool continue_after_async = m_async_signal != -1 || m_async_packet_predicate.GetValue();
473 if (continue_after_async || m_interrupt_sent)
Greg Clayton05e4d972012-03-29 01:55:41 +0000474 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000475 // We sent an interrupt packet to stop the inferior process
476 // for an async signal or to send an async packet while running
477 // but we might have been single stepping and received the
478 // stop packet for the step instead of for the interrupt packet.
479 // Typically when an interrupt is sent a SIGINT or SIGSTOP
480 // is used, so if we get anything else, we need to try and
481 // get another stop reply packet that may have been sent
482 // due to sending the interrupt when the target is stopped
483 // which will just re-send a copy of the last stop reply
484 // packet. If we don't do this, then the reply for our
485 // async packet will be the repeat stop reply packet and cause
486 // a lot of trouble for us!
487 if (signo != SIGINT && signo != SIGSTOP)
488 {
Greg Claytonc2c822c2012-05-15 02:50:49 +0000489 continue_after_async = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000490
491 // We didn't get a a SIGINT or SIGSTOP, so try for a
492 // very brief time (1 ms) to get another stop reply
493 // packet to make sure it doesn't get in the way
494 StringExtractorGDBRemote extra_stop_reply_packet;
495 uint32_t timeout_usec = 1000;
496 if (WaitForPacketWithTimeoutMicroSecondsNoLock (extra_stop_reply_packet, timeout_usec))
497 {
498 switch (extra_stop_reply_packet.GetChar())
499 {
500 case 'T':
501 case 'S':
502 // We did get an extra stop reply, which means
503 // our interrupt didn't stop the target so we
504 // shouldn't continue after the async signal
505 // or packet is sent...
Greg Claytonc2c822c2012-05-15 02:50:49 +0000506 continue_after_async = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000507 break;
508 }
509 }
510 }
511 }
512
513 if (m_async_signal != -1)
514 {
515 if (log)
516 log->Printf ("async: send signo = %s", Host::GetSignalAsCString (m_async_signal));
517
518 // Save off the async signal we are supposed to send
519 const int async_signal = m_async_signal;
520 // Clear the async signal member so we don't end up
521 // sending the signal multiple times...
522 m_async_signal = -1;
523 // Check which signal we stopped with
524 if (signo == async_signal)
525 {
526 if (log)
527 log->Printf ("async: stopped with signal %s, we are done running", Host::GetSignalAsCString (signo));
528
529 // We already stopped with a signal that we wanted
530 // to stop with, so we are done
531 }
532 else
533 {
534 // We stopped with a different signal that the one
535 // we wanted to stop with, so now we must resume
536 // with the signal we want
537 char signal_packet[32];
538 int signal_packet_len = 0;
539 signal_packet_len = ::snprintf (signal_packet,
540 sizeof (signal_packet),
541 "C%2.2x",
542 async_signal);
543
544 if (log)
545 log->Printf ("async: stopped with signal %s, resume with %s",
546 Host::GetSignalAsCString (signo),
547 Host::GetSignalAsCString (async_signal));
548
549 // Set the continue packet to resume even if the
Greg Claytonc2c822c2012-05-15 02:50:49 +0000550 // interrupt didn't cause our stop (ignore continue_after_async)
Greg Clayton05e4d972012-03-29 01:55:41 +0000551 continue_packet.assign(signal_packet, signal_packet_len);
552 continue;
553 }
554 }
555 else if (m_async_packet_predicate.GetValue())
556 {
557 LogSP packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
558
559 // We are supposed to send an asynchronous packet while
560 // we are running.
561 m_async_response.Clear();
562 if (m_async_packet.empty())
563 {
564 if (packet_log)
565 packet_log->Printf ("async: error: empty async packet");
566
567 }
568 else
569 {
570 if (packet_log)
571 packet_log->Printf ("async: sending packet");
572
573 SendPacketAndWaitForResponse (&m_async_packet[0],
574 m_async_packet.size(),
575 m_async_response,
576 false);
577 }
578 // Let the other thread that was trying to send the async
579 // packet know that the packet has been sent and response is
580 // ready...
581 m_async_packet_predicate.SetValue(false, eBroadcastAlways);
582
583 if (packet_log)
Greg Claytonc2c822c2012-05-15 02:50:49 +0000584 packet_log->Printf ("async: sent packet, continue_after_async = %i", continue_after_async);
Greg Clayton05e4d972012-03-29 01:55:41 +0000585
586 // Set the continue packet to resume if our interrupt
587 // for the async packet did cause the stop
Greg Claytonc2c822c2012-05-15 02:50:49 +0000588 if (continue_after_async)
Greg Clayton05e4d972012-03-29 01:55:41 +0000589 {
Greg Clayton8ca4fa72012-05-24 23:42:14 +0000590 // Reverting this for now as it is causing deadlocks
591 // in programs (<rdar://problem/11529853>). In the future
592 // we should check our thread list and "do the right thing"
593 // for new threads that show up while we stop and run async
594 // packets. Setting the packet to 'c' to continue all threads
595 // is the right thing to do 99.99% of the time because if a
596 // thread was single stepping, and we sent an interrupt, we
597 // will notice above that we didn't stop due to an interrupt
598 // but stopped due to stepping and we would _not_ continue.
599 continue_packet.assign (1, 'c');
Greg Clayton05e4d972012-03-29 01:55:41 +0000600 continue;
601 }
602 }
603 // Stop with signal and thread info
604 state = eStateStopped;
Greg Clayton61d043b2011-03-22 04:00:09 +0000605 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000606 break;
607
608 case 'W':
609 case 'X':
610 // process exited
611 state = eStateExited;
612 break;
613
614 case 'O':
615 // STDOUT
616 {
Greg Clayton628cead2011-05-19 03:54:16 +0000617 got_stdout = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000618 std::string inferior_stdout;
619 inferior_stdout.reserve(response.GetBytesLeft () / 2);
620 char ch;
621 while ((ch = response.GetHexU8()) != '\0')
622 inferior_stdout.append(1, ch);
623 process->AppendSTDOUT (inferior_stdout.c_str(), inferior_stdout.size());
624 }
625 break;
626
627 case 'E':
628 // ERROR
629 state = eStateInvalid;
630 break;
631
632 default:
633 if (log)
634 log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__);
635 state = eStateInvalid;
636 break;
637 }
638 }
639 }
640 else
641 {
642 if (log)
643 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__);
644 state = eStateInvalid;
645 }
646 }
647 if (log)
648 log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state));
649 response.SetFilePos(0);
650 m_private_is_running.SetValue (false, eBroadcastAlways);
651 m_public_is_running.SetValue (false, eBroadcastAlways);
652 return state;
653}
654
655bool
656GDBRemoteCommunicationClient::SendAsyncSignal (int signo)
657{
Greg Clayton05e4d972012-03-29 01:55:41 +0000658 Mutex::Locker async_locker (m_async_mutex);
Greg Clayton61d043b2011-03-22 04:00:09 +0000659 m_async_signal = signo;
660 bool timed_out = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000661 Mutex::Locker locker;
Greg Clayton05e4d972012-03-29 01:55:41 +0000662 if (SendInterrupt (locker, 1, timed_out))
Greg Clayton61d043b2011-03-22 04:00:09 +0000663 return true;
664 m_async_signal = -1;
665 return false;
666}
667
Greg Clayton516f0842012-04-11 00:24:49 +0000668// This function takes a mutex locker as a parameter in case the GetSequenceMutex
Greg Clayton61d043b2011-03-22 04:00:09 +0000669// actually succeeds. If it doesn't succeed in acquiring the sequence mutex
670// (the expected result), then it will send the halt packet. If it does succeed
671// then the caller that requested the interrupt will want to keep the sequence
672// locked down so that no one else can send packets while the caller has control.
673// This function usually gets called when we are running and need to stop the
674// target. It can also be used when we are running and and we need to do something
675// else (like read/write memory), so we need to interrupt the running process
676// (gdb remote protocol requires this), and do what we need to do, then resume.
677
678bool
Greg Clayton05e4d972012-03-29 01:55:41 +0000679GDBRemoteCommunicationClient::SendInterrupt
Greg Clayton61d043b2011-03-22 04:00:09 +0000680(
681 Mutex::Locker& locker,
682 uint32_t seconds_to_wait_for_stop,
Greg Clayton61d043b2011-03-22 04:00:09 +0000683 bool &timed_out
684)
685{
Greg Clayton61d043b2011-03-22 04:00:09 +0000686 timed_out = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000687 LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Clayton61d043b2011-03-22 04:00:09 +0000688
689 if (IsRunning())
690 {
691 // Only send an interrupt if our debugserver is running...
Greg Claytonc8dd5702012-04-12 19:04:34 +0000692 if (GetSequenceMutex (locker))
Greg Clayton516f0842012-04-11 00:24:49 +0000693 {
694 if (log)
695 log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt");
696 }
697 else
Greg Clayton61d043b2011-03-22 04:00:09 +0000698 {
699 // Someone has the mutex locked waiting for a response or for the
700 // inferior to stop, so send the interrupt on the down low...
701 char ctrl_c = '\x03';
702 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton61d043b2011-03-22 04:00:09 +0000703 size_t bytes_written = Write (&ctrl_c, 1, status, NULL);
Greg Clayton05e4d972012-03-29 01:55:41 +0000704 if (log)
705 log->PutCString("send packet: \\x03");
Greg Clayton61d043b2011-03-22 04:00:09 +0000706 if (bytes_written > 0)
707 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000708 m_interrupt_sent = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000709 if (seconds_to_wait_for_stop)
710 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000711 TimeValue timeout;
712 if (seconds_to_wait_for_stop)
713 {
714 timeout = TimeValue::Now();
715 timeout.OffsetWithSeconds (seconds_to_wait_for_stop);
716 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000717 if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out))
718 {
719 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000720 log->PutCString ("SendInterrupt () - sent interrupt, private state stopped");
Greg Clayton61d043b2011-03-22 04:00:09 +0000721 return true;
722 }
723 else
724 {
725 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000726 log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume");
Greg Clayton61d043b2011-03-22 04:00:09 +0000727 }
728 }
729 else
730 {
731 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000732 log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop...");
Greg Clayton61d043b2011-03-22 04:00:09 +0000733 return true;
734 }
735 }
736 else
737 {
738 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000739 log->Printf ("SendInterrupt () - failed to write interrupt");
Greg Clayton61d043b2011-03-22 04:00:09 +0000740 }
741 return false;
742 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000743 }
Greg Clayton05e4d972012-03-29 01:55:41 +0000744 else
745 {
746 if (log)
747 log->Printf ("SendInterrupt () - not running");
748 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000749 return true;
750}
751
752lldb::pid_t
753GDBRemoteCommunicationClient::GetCurrentProcessID ()
754{
755 StringExtractorGDBRemote response;
756 if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false))
757 {
758 if (response.GetChar() == 'Q')
759 if (response.GetChar() == 'C')
760 return response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID);
761 }
762 return LLDB_INVALID_PROCESS_ID;
763}
764
765bool
766GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str)
767{
768 error_str.clear();
769 StringExtractorGDBRemote response;
770 if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false))
771 {
772 if (response.IsOKResponse())
773 return true;
774 if (response.GetChar() == 'E')
775 {
776 // A string the describes what failed when launching...
777 error_str = response.GetStringRef().substr(1);
778 }
779 else
780 {
781 error_str.assign ("unknown error occurred launching process");
782 }
783 }
784 else
785 {
Jim Ingham7b3a49b2012-06-28 20:30:23 +0000786 error_str.assign ("timed out waiting for app to launch");
Greg Clayton61d043b2011-03-22 04:00:09 +0000787 }
788 return false;
789}
790
791int
792GDBRemoteCommunicationClient::SendArgumentsPacket (char const *argv[])
793{
794 if (argv && argv[0])
795 {
796 StreamString packet;
797 packet.PutChar('A');
798 const char *arg;
799 for (uint32_t i = 0; (arg = argv[i]) != NULL; ++i)
800 {
801 const int arg_len = strlen(arg);
802 if (i > 0)
803 packet.PutChar(',');
804 packet.Printf("%i,%i,", arg_len * 2, i);
805 packet.PutBytesAsRawHex8 (arg, arg_len);
806 }
807
808 StringExtractorGDBRemote response;
809 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
810 {
811 if (response.IsOKResponse())
812 return 0;
813 uint8_t error = response.GetError();
814 if (error)
815 return error;
816 }
817 }
818 return -1;
819}
820
821int
822GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value)
823{
824 if (name_equal_value && name_equal_value[0])
825 {
826 StreamString packet;
827 packet.Printf("QEnvironment:%s", name_equal_value);
828 StringExtractorGDBRemote response;
829 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
830 {
831 if (response.IsOKResponse())
832 return 0;
833 uint8_t error = response.GetError();
834 if (error)
835 return error;
836 }
837 }
838 return -1;
839}
840
Greg Claytona4582402011-05-08 04:53:50 +0000841int
842GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch)
843{
844 if (arch && arch[0])
845 {
846 StreamString packet;
847 packet.Printf("QLaunchArch:%s", arch);
848 StringExtractorGDBRemote response;
849 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
850 {
851 if (response.IsOKResponse())
852 return 0;
853 uint8_t error = response.GetError();
854 if (error)
855 return error;
856 }
857 }
858 return -1;
859}
860
Greg Clayton61d043b2011-03-22 04:00:09 +0000861bool
Greg Clayton58e26e02011-03-24 04:28:38 +0000862GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major,
863 uint32_t &minor,
864 uint32_t &update)
865{
866 if (GetHostInfo ())
867 {
868 if (m_os_version_major != UINT32_MAX)
869 {
870 major = m_os_version_major;
871 minor = m_os_version_minor;
872 update = m_os_version_update;
873 return true;
874 }
875 }
876 return false;
877}
878
879bool
880GDBRemoteCommunicationClient::GetOSBuildString (std::string &s)
881{
882 if (GetHostInfo ())
883 {
884 if (!m_os_build.empty())
885 {
886 s = m_os_build;
887 return true;
888 }
889 }
890 s.clear();
891 return false;
892}
893
894
895bool
896GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s)
897{
898 if (GetHostInfo ())
899 {
900 if (!m_os_kernel.empty())
901 {
902 s = m_os_kernel;
903 return true;
904 }
905 }
906 s.clear();
907 return false;
908}
909
910bool
911GDBRemoteCommunicationClient::GetHostname (std::string &s)
912{
913 if (GetHostInfo ())
914 {
915 if (!m_hostname.empty())
916 {
917 s = m_hostname;
918 return true;
919 }
920 }
921 s.clear();
922 return false;
923}
924
925ArchSpec
926GDBRemoteCommunicationClient::GetSystemArchitecture ()
927{
928 if (GetHostInfo ())
929 return m_host_arch;
930 return ArchSpec();
931}
932
933
934bool
Greg Clayton06d7cc82011-04-04 18:18:57 +0000935GDBRemoteCommunicationClient::GetHostInfo (bool force)
Greg Clayton61d043b2011-03-22 04:00:09 +0000936{
Greg Clayton06d7cc82011-04-04 18:18:57 +0000937 if (force || m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton61d043b2011-03-22 04:00:09 +0000938 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000939 m_qHostInfo_is_valid = eLazyBoolNo;
Greg Clayton61d043b2011-03-22 04:00:09 +0000940 StringExtractorGDBRemote response;
941 if (SendPacketAndWaitForResponse ("qHostInfo", response, false))
942 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +0000943 if (response.IsNormalResponse())
Greg Claytoncb8977d2011-03-23 00:09:55 +0000944 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000945 std::string name;
946 std::string value;
947 uint32_t cpu = LLDB_INVALID_CPUTYPE;
948 uint32_t sub = 0;
949 std::string arch_name;
950 std::string os_name;
951 std::string vendor_name;
952 std::string triple;
953 uint32_t pointer_byte_size = 0;
954 StringExtractor extractor;
955 ByteOrder byte_order = eByteOrderInvalid;
956 uint32_t num_keys_decoded = 0;
957 while (response.GetNameColonValue(name, value))
Greg Claytoncb8977d2011-03-23 00:09:55 +0000958 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000959 if (name.compare("cputype") == 0)
Greg Clayton58e26e02011-03-24 04:28:38 +0000960 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000961 // exception type in big endian hex
962 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0);
963 if (cpu != LLDB_INVALID_CPUTYPE)
964 ++num_keys_decoded;
965 }
966 else if (name.compare("cpusubtype") == 0)
967 {
968 // exception count in big endian hex
969 sub = Args::StringToUInt32 (value.c_str(), 0, 0);
970 if (sub != 0)
971 ++num_keys_decoded;
972 }
973 else if (name.compare("arch") == 0)
974 {
975 arch_name.swap (value);
976 ++num_keys_decoded;
977 }
978 else if (name.compare("triple") == 0)
979 {
980 // The triple comes as ASCII hex bytes since it contains '-' chars
981 extractor.GetStringRef().swap(value);
982 extractor.SetFilePos(0);
983 extractor.GetHexByteString (triple);
984 ++num_keys_decoded;
985 }
986 else if (name.compare("os_build") == 0)
987 {
988 extractor.GetStringRef().swap(value);
989 extractor.SetFilePos(0);
990 extractor.GetHexByteString (m_os_build);
991 ++num_keys_decoded;
992 }
993 else if (name.compare("hostname") == 0)
994 {
995 extractor.GetStringRef().swap(value);
996 extractor.SetFilePos(0);
997 extractor.GetHexByteString (m_hostname);
998 ++num_keys_decoded;
999 }
1000 else if (name.compare("os_kernel") == 0)
1001 {
1002 extractor.GetStringRef().swap(value);
1003 extractor.SetFilePos(0);
1004 extractor.GetHexByteString (m_os_kernel);
1005 ++num_keys_decoded;
1006 }
1007 else if (name.compare("ostype") == 0)
1008 {
1009 os_name.swap (value);
1010 ++num_keys_decoded;
1011 }
1012 else if (name.compare("vendor") == 0)
1013 {
1014 vendor_name.swap(value);
1015 ++num_keys_decoded;
1016 }
1017 else if (name.compare("endian") == 0)
1018 {
1019 ++num_keys_decoded;
1020 if (value.compare("little") == 0)
1021 byte_order = eByteOrderLittle;
1022 else if (value.compare("big") == 0)
1023 byte_order = eByteOrderBig;
1024 else if (value.compare("pdp") == 0)
1025 byte_order = eByteOrderPDP;
1026 else
1027 --num_keys_decoded;
1028 }
1029 else if (name.compare("ptrsize") == 0)
1030 {
1031 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 0);
1032 if (pointer_byte_size != 0)
1033 ++num_keys_decoded;
1034 }
1035 else if (name.compare("os_version") == 0)
1036 {
1037 Args::StringToVersion (value.c_str(),
1038 m_os_version_major,
1039 m_os_version_minor,
1040 m_os_version_update);
1041 if (m_os_version_major != UINT32_MAX)
1042 ++num_keys_decoded;
1043 }
Enrico Granata7de2a3b2012-07-13 23:18:48 +00001044 else if (name.compare("watchpoint_exceptions_received") == 0)
1045 {
1046 ++num_keys_decoded;
1047 if (strcmp(value.c_str(),"before") == 0)
1048 m_watchpoints_trigger_after_instruction = eLazyBoolNo;
1049 else if (strcmp(value.c_str(),"after") == 0)
1050 m_watchpoints_trigger_after_instruction = eLazyBoolYes;
1051 else
1052 --num_keys_decoded;
1053 }
1054
Greg Clayton24bc5d92011-03-30 18:16:51 +00001055 }
1056
1057 if (num_keys_decoded > 0)
1058 m_qHostInfo_is_valid = eLazyBoolYes;
1059
1060 if (triple.empty())
1061 {
1062 if (arch_name.empty())
1063 {
1064 if (cpu != LLDB_INVALID_CPUTYPE)
1065 {
1066 m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1067 if (pointer_byte_size)
1068 {
1069 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1070 }
1071 if (byte_order != eByteOrderInvalid)
1072 {
1073 assert (byte_order == m_host_arch.GetByteOrder());
1074 }
Greg Claytonb170aee2012-05-08 01:45:38 +00001075
1076 if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0)
1077 {
1078 switch (m_host_arch.GetMachine())
1079 {
1080 case llvm::Triple::arm:
1081 case llvm::Triple::thumb:
1082 os_name = "ios";
1083 break;
1084 default:
1085 os_name = "macosx";
1086 break;
1087 }
1088 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001089 if (!vendor_name.empty())
1090 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1091 if (!os_name.empty())
Greg Clayton89798cc2011-09-15 00:21:03 +00001092 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001093
1094 }
1095 }
1096 else
1097 {
1098 std::string triple;
1099 triple += arch_name;
Greg Claytonb170aee2012-05-08 01:45:38 +00001100 if (!vendor_name.empty() || !os_name.empty())
1101 {
1102 triple += '-';
1103 if (vendor_name.empty())
1104 triple += "unknown";
1105 else
1106 triple += vendor_name;
1107 triple += '-';
1108 if (os_name.empty())
1109 triple += "unknown";
1110 else
1111 triple += os_name;
1112 }
1113 m_host_arch.SetTriple (triple.c_str());
1114
1115 llvm::Triple &host_triple = m_host_arch.GetTriple();
1116 if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin)
1117 {
1118 switch (m_host_arch.GetMachine())
1119 {
1120 case llvm::Triple::arm:
1121 case llvm::Triple::thumb:
1122 host_triple.setOS(llvm::Triple::IOS);
1123 break;
1124 default:
1125 host_triple.setOS(llvm::Triple::MacOSX);
1126 break;
1127 }
1128 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001129 if (pointer_byte_size)
1130 {
1131 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1132 }
1133 if (byte_order != eByteOrderInvalid)
1134 {
1135 assert (byte_order == m_host_arch.GetByteOrder());
1136 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001137
Greg Clayton58e26e02011-03-24 04:28:38 +00001138 }
1139 }
1140 else
1141 {
Greg Claytonb170aee2012-05-08 01:45:38 +00001142 m_host_arch.SetTriple (triple.c_str());
Greg Claytoncb8977d2011-03-23 00:09:55 +00001143 if (pointer_byte_size)
1144 {
1145 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1146 }
1147 if (byte_order != eByteOrderInvalid)
1148 {
1149 assert (byte_order == m_host_arch.GetByteOrder());
1150 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001151 }
Greg Claytoncb8977d2011-03-23 00:09:55 +00001152 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001153 }
1154 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001155 return m_qHostInfo_is_valid == eLazyBoolYes;
Greg Clayton61d043b2011-03-22 04:00:09 +00001156}
1157
1158int
1159GDBRemoteCommunicationClient::SendAttach
1160(
1161 lldb::pid_t pid,
1162 StringExtractorGDBRemote& response
1163)
1164{
1165 if (pid != LLDB_INVALID_PROCESS_ID)
1166 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001167 char packet[64];
Greg Claytond9919d32011-12-01 23:28:38 +00001168 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%llx", pid);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001169 assert (packet_len < sizeof(packet));
1170 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton61d043b2011-03-22 04:00:09 +00001171 {
1172 if (response.IsErrorResponse())
1173 return response.GetError();
1174 return 0;
1175 }
1176 }
1177 return -1;
1178}
1179
1180const lldb_private::ArchSpec &
1181GDBRemoteCommunicationClient::GetHostArchitecture ()
1182{
Greg Clayton24bc5d92011-03-30 18:16:51 +00001183 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton61d043b2011-03-22 04:00:09 +00001184 GetHostInfo ();
Greg Claytoncb8977d2011-03-23 00:09:55 +00001185 return m_host_arch;
Greg Clayton61d043b2011-03-22 04:00:09 +00001186}
1187
1188addr_t
1189GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions)
1190{
Greg Clayton2f085c62011-05-15 01:25:55 +00001191 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton61d043b2011-03-22 04:00:09 +00001192 {
Greg Clayton2f085c62011-05-15 01:25:55 +00001193 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton989816b2011-05-14 01:50:35 +00001194 char packet[64];
1195 const int packet_len = ::snprintf (packet, sizeof(packet), "_M%zx,%s%s%s", size,
1196 permissions & lldb::ePermissionsReadable ? "r" : "",
1197 permissions & lldb::ePermissionsWritable ? "w" : "",
1198 permissions & lldb::ePermissionsExecutable ? "x" : "");
1199 assert (packet_len < sizeof(packet));
1200 StringExtractorGDBRemote response;
1201 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1202 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001203 if (!response.IsErrorResponse())
Greg Clayton989816b2011-05-14 01:50:35 +00001204 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1205 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001206 else
1207 {
1208 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1209 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001210 }
1211 return LLDB_INVALID_ADDRESS;
1212}
1213
1214bool
1215GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr)
1216{
Greg Clayton2f085c62011-05-15 01:25:55 +00001217 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton61d043b2011-03-22 04:00:09 +00001218 {
Greg Clayton2f085c62011-05-15 01:25:55 +00001219 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton989816b2011-05-14 01:50:35 +00001220 char packet[64];
1221 const int packet_len = ::snprintf(packet, sizeof(packet), "_m%llx", (uint64_t)addr);
1222 assert (packet_len < sizeof(packet));
1223 StringExtractorGDBRemote response;
1224 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1225 {
1226 if (response.IsOKResponse())
1227 return true;
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001228 }
1229 else
1230 {
1231 m_supports_alloc_dealloc_memory = eLazyBoolNo;
Greg Clayton989816b2011-05-14 01:50:35 +00001232 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001233 }
1234 return false;
1235}
1236
Greg Clayton516f0842012-04-11 00:24:49 +00001237bool
1238GDBRemoteCommunicationClient::Detach ()
1239{
1240 return SendPacket ("D", 1) > 0;
1241}
1242
Greg Claytona9385532011-11-18 07:03:08 +00001243Error
1244GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr,
1245 lldb_private::MemoryRegionInfo &region_info)
1246{
1247 Error error;
1248 region_info.Clear();
1249
1250 if (m_supports_memory_region_info != eLazyBoolNo)
1251 {
1252 m_supports_memory_region_info = eLazyBoolYes;
1253 char packet[64];
1254 const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%llx", (uint64_t)addr);
1255 assert (packet_len < sizeof(packet));
1256 StringExtractorGDBRemote response;
1257 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1258 {
1259 std::string name;
1260 std::string value;
1261 addr_t addr_value;
1262 bool success = true;
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001263 bool saw_permissions = false;
Greg Claytona9385532011-11-18 07:03:08 +00001264 while (success && response.GetNameColonValue(name, value))
1265 {
1266 if (name.compare ("start") == 0)
1267 {
1268 addr_value = Args::StringToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success);
1269 if (success)
1270 region_info.GetRange().SetRangeBase(addr_value);
1271 }
1272 else if (name.compare ("size") == 0)
1273 {
1274 addr_value = Args::StringToUInt64(value.c_str(), 0, 16, &success);
1275 if (success)
1276 region_info.GetRange().SetByteSize (addr_value);
1277 }
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001278 else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid())
Greg Claytona9385532011-11-18 07:03:08 +00001279 {
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001280 saw_permissions = true;
1281 if (region_info.GetRange().Contains (addr))
1282 {
1283 if (value.find('r') != std::string::npos)
1284 region_info.SetReadable (MemoryRegionInfo::eYes);
1285 else
1286 region_info.SetReadable (MemoryRegionInfo::eNo);
1287
1288 if (value.find('w') != std::string::npos)
1289 region_info.SetWritable (MemoryRegionInfo::eYes);
1290 else
1291 region_info.SetWritable (MemoryRegionInfo::eNo);
1292
1293 if (value.find('x') != std::string::npos)
1294 region_info.SetExecutable (MemoryRegionInfo::eYes);
1295 else
1296 region_info.SetExecutable (MemoryRegionInfo::eNo);
1297 }
1298 else
1299 {
1300 // The reported region does not contain this address -- we're looking at an unmapped page
1301 region_info.SetReadable (MemoryRegionInfo::eNo);
1302 region_info.SetWritable (MemoryRegionInfo::eNo);
1303 region_info.SetExecutable (MemoryRegionInfo::eNo);
1304 }
Greg Claytona9385532011-11-18 07:03:08 +00001305 }
1306 else if (name.compare ("error") == 0)
1307 {
1308 StringExtractorGDBRemote name_extractor;
1309 // Swap "value" over into "name_extractor"
1310 name_extractor.GetStringRef().swap(value);
1311 // Now convert the HEX bytes into a string value
1312 name_extractor.GetHexByteString (value);
1313 error.SetErrorString(value.c_str());
1314 }
1315 }
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001316
1317 // We got a valid address range back but no permissions -- which means this is an unmapped page
1318 if (region_info.GetRange().IsValid() && saw_permissions == false)
1319 {
1320 region_info.SetReadable (MemoryRegionInfo::eNo);
1321 region_info.SetWritable (MemoryRegionInfo::eNo);
1322 region_info.SetExecutable (MemoryRegionInfo::eNo);
1323 }
Greg Claytona9385532011-11-18 07:03:08 +00001324 }
1325 else
1326 {
1327 m_supports_memory_region_info = eLazyBoolNo;
1328 }
1329 }
1330
1331 if (m_supports_memory_region_info == eLazyBoolNo)
1332 {
1333 error.SetErrorString("qMemoryRegionInfo is not supported");
1334 }
1335 if (error.Fail())
1336 region_info.Clear();
1337 return error;
1338
1339}
1340
Johnny Chen7cbdcfb2012-05-23 21:09:52 +00001341Error
1342GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num)
1343{
1344 Error error;
1345
1346 if (m_supports_watchpoint_support_info == eLazyBoolYes)
1347 {
1348 num = m_num_supported_hardware_watchpoints;
1349 return error;
1350 }
1351
1352 // Set num to 0 first.
1353 num = 0;
1354 if (m_supports_watchpoint_support_info != eLazyBoolNo)
1355 {
1356 char packet[64];
1357 const int packet_len = ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:");
1358 assert (packet_len < sizeof(packet));
1359 StringExtractorGDBRemote response;
1360 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1361 {
1362 m_supports_watchpoint_support_info = eLazyBoolYes;
1363 std::string name;
1364 std::string value;
1365 while (response.GetNameColonValue(name, value))
1366 {
1367 if (name.compare ("num") == 0)
1368 {
1369 num = Args::StringToUInt32(value.c_str(), 0, 0);
1370 m_num_supported_hardware_watchpoints = num;
1371 }
1372 }
1373 }
1374 else
1375 {
1376 m_supports_watchpoint_support_info = eLazyBoolNo;
1377 }
1378 }
1379
1380 if (m_supports_watchpoint_support_info == eLazyBoolNo)
1381 {
1382 error.SetErrorString("qWatchpointSupportInfo is not supported");
1383 }
1384 return error;
1385
1386}
Greg Claytona9385532011-11-18 07:03:08 +00001387
Enrico Granata7de2a3b2012-07-13 23:18:48 +00001388lldb_private::Error
1389GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num, bool& after)
1390{
1391 Error error(GetWatchpointSupportInfo(num));
1392 if (error.Success())
1393 error = GetWatchpointsTriggerAfterInstruction(after);
1394 return error;
1395}
1396
1397lldb_private::Error
1398GDBRemoteCommunicationClient::GetWatchpointsTriggerAfterInstruction (bool &after)
1399{
1400 Error error;
1401
1402 // we assume watchpoints will happen after running the relevant opcode
1403 // and we only want to override this behavior if we have explicitly
1404 // received a qHostInfo telling us otherwise
1405 if (m_qHostInfo_is_valid != eLazyBoolYes)
1406 after = true;
1407 else
1408 after = (m_watchpoints_trigger_after_instruction != eLazyBoolNo);
1409 return error;
1410}
1411
Greg Clayton61d043b2011-03-22 04:00:09 +00001412int
1413GDBRemoteCommunicationClient::SetSTDIN (char const *path)
1414{
1415 if (path && path[0])
1416 {
1417 StreamString packet;
1418 packet.PutCString("QSetSTDIN:");
1419 packet.PutBytesAsRawHex8(path, strlen(path));
1420
1421 StringExtractorGDBRemote response;
1422 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1423 {
1424 if (response.IsOKResponse())
1425 return 0;
1426 uint8_t error = response.GetError();
1427 if (error)
1428 return error;
1429 }
1430 }
1431 return -1;
1432}
1433
1434int
1435GDBRemoteCommunicationClient::SetSTDOUT (char const *path)
1436{
1437 if (path && path[0])
1438 {
1439 StreamString packet;
1440 packet.PutCString("QSetSTDOUT:");
1441 packet.PutBytesAsRawHex8(path, strlen(path));
1442
1443 StringExtractorGDBRemote response;
1444 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1445 {
1446 if (response.IsOKResponse())
1447 return 0;
1448 uint8_t error = response.GetError();
1449 if (error)
1450 return error;
1451 }
1452 }
1453 return -1;
1454}
1455
1456int
1457GDBRemoteCommunicationClient::SetSTDERR (char const *path)
1458{
1459 if (path && path[0])
1460 {
1461 StreamString packet;
1462 packet.PutCString("QSetSTDERR:");
1463 packet.PutBytesAsRawHex8(path, strlen(path));
1464
1465 StringExtractorGDBRemote response;
1466 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1467 {
1468 if (response.IsOKResponse())
1469 return 0;
1470 uint8_t error = response.GetError();
1471 if (error)
1472 return error;
1473 }
1474 }
1475 return -1;
1476}
1477
1478int
1479GDBRemoteCommunicationClient::SetWorkingDir (char const *path)
1480{
1481 if (path && path[0])
1482 {
1483 StreamString packet;
1484 packet.PutCString("QSetWorkingDir:");
1485 packet.PutBytesAsRawHex8(path, strlen(path));
1486
1487 StringExtractorGDBRemote response;
1488 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1489 {
1490 if (response.IsOKResponse())
1491 return 0;
1492 uint8_t error = response.GetError();
1493 if (error)
1494 return error;
1495 }
1496 }
1497 return -1;
1498}
1499
1500int
1501GDBRemoteCommunicationClient::SetDisableASLR (bool enable)
1502{
Greg Clayton24bc5d92011-03-30 18:16:51 +00001503 char packet[32];
1504 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0);
1505 assert (packet_len < sizeof(packet));
Greg Clayton61d043b2011-03-22 04:00:09 +00001506 StringExtractorGDBRemote response;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001507 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton61d043b2011-03-22 04:00:09 +00001508 {
1509 if (response.IsOKResponse())
1510 return 0;
1511 uint8_t error = response.GetError();
1512 if (error)
1513 return error;
1514 }
1515 return -1;
1516}
Greg Clayton24bc5d92011-03-30 18:16:51 +00001517
1518bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001519GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001520{
1521 if (response.IsNormalResponse())
1522 {
1523 std::string name;
1524 std::string value;
1525 StringExtractor extractor;
1526
1527 while (response.GetNameColonValue(name, value))
1528 {
1529 if (name.compare("pid") == 0)
1530 {
1531 process_info.SetProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1532 }
1533 else if (name.compare("ppid") == 0)
1534 {
1535 process_info.SetParentProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1536 }
1537 else if (name.compare("uid") == 0)
1538 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001539 process_info.SetUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001540 }
1541 else if (name.compare("euid") == 0)
1542 {
1543 process_info.SetEffectiveUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1544 }
1545 else if (name.compare("gid") == 0)
1546 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001547 process_info.SetGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001548 }
1549 else if (name.compare("egid") == 0)
1550 {
1551 process_info.SetEffectiveGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1552 }
1553 else if (name.compare("triple") == 0)
1554 {
1555 // The triple comes as ASCII hex bytes since it contains '-' chars
1556 extractor.GetStringRef().swap(value);
1557 extractor.SetFilePos(0);
1558 extractor.GetHexByteString (value);
Greg Claytonb170aee2012-05-08 01:45:38 +00001559 process_info.GetArchitecture ().SetTriple (value.c_str());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001560 }
1561 else if (name.compare("name") == 0)
1562 {
1563 StringExtractor extractor;
Filipe Cabecinhase61ec7f2012-05-07 09:30:51 +00001564 // The process name from ASCII hex bytes since we can't
Greg Clayton24bc5d92011-03-30 18:16:51 +00001565 // control the characters in a process name
1566 extractor.GetStringRef().swap(value);
1567 extractor.SetFilePos(0);
1568 extractor.GetHexByteString (value);
Greg Clayton527154d2011-11-15 03:53:30 +00001569 process_info.GetExecutableFile().SetFile (value.c_str(), false);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001570 }
1571 }
1572
1573 if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
1574 return true;
1575 }
1576 return false;
1577}
1578
1579bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001580GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001581{
1582 process_info.Clear();
1583
1584 if (m_supports_qProcessInfoPID)
1585 {
1586 char packet[32];
Greg Claytond9919d32011-12-01 23:28:38 +00001587 const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%llu", pid);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001588 assert (packet_len < sizeof(packet));
1589 StringExtractorGDBRemote response;
1590 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1591 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001592 return DecodeProcessInfoResponse (response, process_info);
1593 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001594 else
1595 {
1596 m_supports_qProcessInfoPID = false;
1597 return false;
1598 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001599 }
1600 return false;
1601}
1602
1603uint32_t
Greg Claytonb72d0f02011-04-12 05:54:46 +00001604GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1605 ProcessInstanceInfoList &process_infos)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001606{
1607 process_infos.Clear();
1608
1609 if (m_supports_qfProcessInfo)
1610 {
1611 StreamString packet;
1612 packet.PutCString ("qfProcessInfo");
1613 if (!match_info.MatchAllProcesses())
1614 {
1615 packet.PutChar (':');
1616 const char *name = match_info.GetProcessInfo().GetName();
1617 bool has_name_match = false;
1618 if (name && name[0])
1619 {
1620 has_name_match = true;
1621 NameMatchType name_match_type = match_info.GetNameMatchType();
1622 switch (name_match_type)
1623 {
1624 case eNameMatchIgnore:
1625 has_name_match = false;
1626 break;
1627
1628 case eNameMatchEquals:
1629 packet.PutCString ("name_match:equals;");
1630 break;
1631
1632 case eNameMatchContains:
1633 packet.PutCString ("name_match:contains;");
1634 break;
1635
1636 case eNameMatchStartsWith:
1637 packet.PutCString ("name_match:starts_with;");
1638 break;
1639
1640 case eNameMatchEndsWith:
1641 packet.PutCString ("name_match:ends_with;");
1642 break;
1643
1644 case eNameMatchRegularExpression:
1645 packet.PutCString ("name_match:regex;");
1646 break;
1647 }
1648 if (has_name_match)
1649 {
1650 packet.PutCString ("name:");
1651 packet.PutBytesAsRawHex8(name, ::strlen(name));
1652 packet.PutChar (';');
1653 }
1654 }
1655
1656 if (match_info.GetProcessInfo().ProcessIDIsValid())
Greg Claytond9919d32011-12-01 23:28:38 +00001657 packet.Printf("pid:%llu;",match_info.GetProcessInfo().GetProcessID());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001658 if (match_info.GetProcessInfo().ParentProcessIDIsValid())
Greg Claytond9919d32011-12-01 23:28:38 +00001659 packet.Printf("parent_pid:%llu;",match_info.GetProcessInfo().GetParentProcessID());
Greg Claytonb72d0f02011-04-12 05:54:46 +00001660 if (match_info.GetProcessInfo().UserIDIsValid())
1661 packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID());
1662 if (match_info.GetProcessInfo().GroupIDIsValid())
1663 packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001664 if (match_info.GetProcessInfo().EffectiveUserIDIsValid())
1665 packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID());
1666 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1667 packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID());
1668 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1669 packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0);
1670 if (match_info.GetProcessInfo().GetArchitecture().IsValid())
1671 {
1672 const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture();
1673 const llvm::Triple &triple = match_arch.GetTriple();
1674 packet.PutCString("triple:");
1675 packet.PutCStringAsRawHex8(triple.getTriple().c_str());
1676 packet.PutChar (';');
1677 }
1678 }
1679 StringExtractorGDBRemote response;
1680 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1681 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001682 do
1683 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001684 ProcessInstanceInfo process_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001685 if (!DecodeProcessInfoResponse (response, process_info))
1686 break;
1687 process_infos.Append(process_info);
1688 response.GetStringRef().clear();
1689 response.SetFilePos(0);
1690 } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false));
1691 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001692 else
1693 {
1694 m_supports_qfProcessInfo = false;
1695 return 0;
1696 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001697 }
1698 return process_infos.GetSize();
1699
1700}
1701
1702bool
1703GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name)
1704{
1705 if (m_supports_qUserName)
1706 {
1707 char packet[32];
1708 const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid);
1709 assert (packet_len < sizeof(packet));
1710 StringExtractorGDBRemote response;
1711 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1712 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001713 if (response.IsNormalResponse())
1714 {
1715 // Make sure we parsed the right number of characters. The response is
1716 // the hex encoded user name and should make up the entire packet.
1717 // If there are any non-hex ASCII bytes, the length won't match below..
1718 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
1719 return true;
1720 }
1721 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001722 else
1723 {
1724 m_supports_qUserName = false;
1725 return false;
1726 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001727 }
1728 return false;
1729
1730}
1731
1732bool
1733GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name)
1734{
1735 if (m_supports_qGroupName)
1736 {
1737 char packet[32];
1738 const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid);
1739 assert (packet_len < sizeof(packet));
1740 StringExtractorGDBRemote response;
1741 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1742 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001743 if (response.IsNormalResponse())
1744 {
1745 // Make sure we parsed the right number of characters. The response is
1746 // the hex encoded group name and should make up the entire packet.
1747 // If there are any non-hex ASCII bytes, the length won't match below..
1748 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
1749 return true;
1750 }
1751 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001752 else
1753 {
1754 m_supports_qGroupName = false;
1755 return false;
1756 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001757 }
1758 return false;
Greg Clayton06d7cc82011-04-04 18:18:57 +00001759}
Greg Clayton24bc5d92011-03-30 18:16:51 +00001760
Greg Clayton06d7cc82011-04-04 18:18:57 +00001761void
1762GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets)
1763{
1764 uint32_t i;
1765 TimeValue start_time, end_time;
1766 uint64_t total_time_nsec;
1767 float packets_per_second;
1768 if (SendSpeedTestPacket (0, 0))
1769 {
1770 for (uint32_t send_size = 0; send_size <= 1024; send_size *= 2)
1771 {
1772 for (uint32_t recv_size = 0; recv_size <= 1024; recv_size *= 2)
1773 {
1774 start_time = TimeValue::Now();
1775 for (i=0; i<num_packets; ++i)
1776 {
1777 SendSpeedTestPacket (send_size, recv_size);
1778 }
1779 end_time = TimeValue::Now();
1780 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001781 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Greg Clayton153ccd72011-08-10 02:10:13 +00001782 printf ("%u qSpeedTest(send=%-5u, recv=%-5u) in %llu.%9.9llu sec for %f packets/sec.\n",
Greg Clayton06d7cc82011-04-04 18:18:57 +00001783 num_packets,
1784 send_size,
1785 recv_size,
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001786 total_time_nsec / TimeValue::NanoSecPerSec,
1787 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton06d7cc82011-04-04 18:18:57 +00001788 packets_per_second);
1789 if (recv_size == 0)
1790 recv_size = 32;
1791 }
1792 if (send_size == 0)
1793 send_size = 32;
1794 }
1795 }
1796 else
1797 {
1798 start_time = TimeValue::Now();
1799 for (i=0; i<num_packets; ++i)
1800 {
1801 GetCurrentProcessID ();
1802 }
1803 end_time = TimeValue::Now();
1804 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001805 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Greg Clayton153ccd72011-08-10 02:10:13 +00001806 printf ("%u 'qC' packets packets in 0x%llu%9.9llu sec for %f packets/sec.\n",
Greg Clayton06d7cc82011-04-04 18:18:57 +00001807 num_packets,
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001808 total_time_nsec / TimeValue::NanoSecPerSec,
1809 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton06d7cc82011-04-04 18:18:57 +00001810 packets_per_second);
1811 }
1812}
1813
1814bool
1815GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size)
1816{
1817 StreamString packet;
1818 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size);
1819 uint32_t bytes_left = send_size;
1820 while (bytes_left > 0)
1821 {
1822 if (bytes_left >= 26)
1823 {
1824 packet.PutCString("abcdefghijklmnopqrstuvwxyz");
1825 bytes_left -= 26;
1826 }
1827 else
1828 {
1829 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz");
1830 bytes_left = 0;
1831 }
1832 }
1833
1834 StringExtractorGDBRemote response;
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001835 return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) > 0;
Greg Clayton06d7cc82011-04-04 18:18:57 +00001836 return false;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001837}
Greg Claytonb72d0f02011-04-12 05:54:46 +00001838
1839uint16_t
1840GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort ()
1841{
1842 StringExtractorGDBRemote response;
1843 if (SendPacketAndWaitForResponse("qLaunchGDBServer", strlen("qLaunchGDBServer"), response, false))
1844 {
1845 std::string name;
1846 std::string value;
1847 uint16_t port = 0;
Greg Clayton4a379b12012-07-17 03:23:13 +00001848 //lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
Greg Claytonb72d0f02011-04-12 05:54:46 +00001849 while (response.GetNameColonValue(name, value))
1850 {
1851 if (name.size() == 4 && name.compare("port") == 0)
1852 port = Args::StringToUInt32(value.c_str(), 0, 0);
Greg Clayton4a379b12012-07-17 03:23:13 +00001853// if (name.size() == 3 && name.compare("pid") == 0)
1854// pid = Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001855 }
1856 return port;
1857 }
1858 return 0;
1859}
1860
1861bool
1862GDBRemoteCommunicationClient::SetCurrentThread (int tid)
1863{
1864 if (m_curr_tid == tid)
1865 return true;
1866
1867 char packet[32];
1868 int packet_len;
1869 if (tid <= 0)
1870 packet_len = ::snprintf (packet, sizeof(packet), "Hg%i", tid);
1871 else
1872 packet_len = ::snprintf (packet, sizeof(packet), "Hg%x", tid);
1873 assert (packet_len + 1 < sizeof(packet));
1874 StringExtractorGDBRemote response;
1875 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
1876 {
1877 if (response.IsOKResponse())
1878 {
1879 m_curr_tid = tid;
1880 return true;
1881 }
1882 }
1883 return false;
1884}
1885
1886bool
1887GDBRemoteCommunicationClient::SetCurrentThreadForRun (int tid)
1888{
1889 if (m_curr_tid_run == tid)
1890 return true;
1891
1892 char packet[32];
1893 int packet_len;
1894 if (tid <= 0)
1895 packet_len = ::snprintf (packet, sizeof(packet), "Hc%i", tid);
1896 else
1897 packet_len = ::snprintf (packet, sizeof(packet), "Hc%x", tid);
1898
1899 assert (packet_len + 1 < sizeof(packet));
1900 StringExtractorGDBRemote response;
1901 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
1902 {
1903 if (response.IsOKResponse())
1904 {
1905 m_curr_tid_run = tid;
1906 return true;
1907 }
1908 }
1909 return false;
1910}
1911
1912bool
1913GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response)
1914{
1915 if (SendPacketAndWaitForResponse("?", 1, response, false))
1916 return response.IsNormalResponse();
1917 return false;
1918}
1919
1920bool
1921GDBRemoteCommunicationClient::GetThreadStopInfo (uint32_t tid, StringExtractorGDBRemote &response)
1922{
1923 if (m_supports_qThreadStopInfo)
1924 {
1925 char packet[256];
1926 int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%x", tid);
1927 assert (packet_len < sizeof(packet));
1928 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
1929 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001930 if (response.IsNormalResponse())
Greg Claytonb72d0f02011-04-12 05:54:46 +00001931 return true;
1932 else
1933 return false;
1934 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001935 else
1936 {
1937 m_supports_qThreadStopInfo = false;
1938 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001939 }
Greg Clayton139da722011-05-20 03:15:54 +00001940// if (SetCurrentThread (tid))
1941// return GetStopReply (response);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001942 return false;
1943}
1944
1945
1946uint8_t
1947GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length)
1948{
1949 switch (type)
1950 {
1951 case eBreakpointSoftware: if (!m_supports_z0) return UINT8_MAX; break;
1952 case eBreakpointHardware: if (!m_supports_z1) return UINT8_MAX; break;
1953 case eWatchpointWrite: if (!m_supports_z2) return UINT8_MAX; break;
1954 case eWatchpointRead: if (!m_supports_z3) return UINT8_MAX; break;
1955 case eWatchpointReadWrite: if (!m_supports_z4) return UINT8_MAX; break;
1956 default: return UINT8_MAX;
1957 }
1958
1959 char packet[64];
1960 const int packet_len = ::snprintf (packet,
1961 sizeof(packet),
1962 "%c%i,%llx,%x",
1963 insert ? 'Z' : 'z',
1964 type,
1965 addr,
1966 length);
1967
1968 assert (packet_len + 1 < sizeof(packet));
1969 StringExtractorGDBRemote response;
1970 if (SendPacketAndWaitForResponse(packet, packet_len, response, true))
1971 {
1972 if (response.IsOKResponse())
1973 return 0;
Greg Claytonb72d0f02011-04-12 05:54:46 +00001974 else if (response.IsErrorResponse())
1975 return response.GetError();
1976 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001977 else
1978 {
1979 switch (type)
1980 {
1981 case eBreakpointSoftware: m_supports_z0 = false; break;
1982 case eBreakpointHardware: m_supports_z1 = false; break;
1983 case eWatchpointWrite: m_supports_z2 = false; break;
1984 case eWatchpointRead: m_supports_z3 = false; break;
1985 case eWatchpointReadWrite: m_supports_z4 = false; break;
1986 default: break;
1987 }
1988 }
1989
Greg Claytonb72d0f02011-04-12 05:54:46 +00001990 return UINT8_MAX;
1991}
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001992
1993size_t
1994GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
1995 bool &sequence_mutex_unavailable)
1996{
1997 Mutex::Locker locker;
1998 thread_ids.clear();
1999
Jim Ingham088684d2012-06-08 22:50:40 +00002000 if (GetSequenceMutex (locker, "ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex"))
Greg Clayton4a60f9e2011-05-20 23:38:13 +00002001 {
2002 sequence_mutex_unavailable = false;
2003 StringExtractorGDBRemote response;
2004
Greg Clayton63afdb02011-06-17 01:22:15 +00002005 for (SendPacketNoLock ("qfThreadInfo", strlen("qfThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
Greg Clayton4a60f9e2011-05-20 23:38:13 +00002006 response.IsNormalResponse();
Greg Clayton63afdb02011-06-17 01:22:15 +00002007 SendPacketNoLock ("qsThreadInfo", strlen("qsThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()))
Greg Clayton4a60f9e2011-05-20 23:38:13 +00002008 {
2009 char ch = response.GetChar();
2010 if (ch == 'l')
2011 break;
2012 if (ch == 'm')
2013 {
2014 do
2015 {
2016 tid_t tid = response.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID);
2017
2018 if (tid != LLDB_INVALID_THREAD_ID)
2019 {
2020 thread_ids.push_back (tid);
2021 }
2022 ch = response.GetChar(); // Skip the command separator
2023 } while (ch == ','); // Make sure we got a comma separator
2024 }
2025 }
2026 }
2027 else
2028 {
Jim Ingham088684d2012-06-08 22:50:40 +00002029#if defined (LLDB_CONFIGURATION_DEBUG)
2030 // assert(!"ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex");
2031#else
Greg Claytonc8dd5702012-04-12 19:04:34 +00002032 LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
2033 if (log)
2034 log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'");
Jim Ingham088684d2012-06-08 22:50:40 +00002035#endif
Greg Clayton4a60f9e2011-05-20 23:38:13 +00002036 sequence_mutex_unavailable = true;
2037 }
2038 return thread_ids.size();
2039}
Greg Clayton516f0842012-04-11 00:24:49 +00002040
2041lldb::addr_t
2042GDBRemoteCommunicationClient::GetShlibInfoAddr()
2043{
2044 if (!IsRunning())
2045 {
2046 StringExtractorGDBRemote response;
2047 if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false))
2048 {
2049 if (response.IsNormalResponse())
2050 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
2051 }
2052 }
2053 return LLDB_INVALID_ADDRESS;
2054}
2055