blob: 013519c8e7a43fab0e7638f4d76d55903dd6d0e0 [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),
Greg Clayton24bc5d92011-03-30 18:16:51 +000052 m_supports_qProcessInfoPID (true),
53 m_supports_qfProcessInfo (true),
54 m_supports_qUserName (true),
55 m_supports_qGroupName (true),
Greg Claytonb72d0f02011-04-12 05:54:46 +000056 m_supports_qThreadStopInfo (true),
57 m_supports_z0 (true),
58 m_supports_z1 (true),
59 m_supports_z2 (true),
60 m_supports_z3 (true),
61 m_supports_z4 (true),
62 m_curr_tid (LLDB_INVALID_THREAD_ID),
63 m_curr_tid_run (LLDB_INVALID_THREAD_ID),
Johnny Chen7cbdcfb2012-05-23 21:09:52 +000064 m_num_supported_hardware_watchpoints (0),
Greg Clayton61d043b2011-03-22 04:00:09 +000065 m_async_mutex (Mutex::eMutexTypeRecursive),
66 m_async_packet_predicate (false),
67 m_async_packet (),
68 m_async_response (),
69 m_async_signal (-1),
Greg Clayton58e26e02011-03-24 04:28:38 +000070 m_host_arch(),
71 m_os_version_major (UINT32_MAX),
72 m_os_version_minor (UINT32_MAX),
73 m_os_version_update (UINT32_MAX)
Greg Clayton61d043b2011-03-22 04:00:09 +000074{
Greg Clayton61d043b2011-03-22 04:00:09 +000075}
76
77//----------------------------------------------------------------------
78// Destructor
79//----------------------------------------------------------------------
80GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient()
81{
Greg Clayton61d043b2011-03-22 04:00:09 +000082 if (IsConnected())
Greg Clayton61d043b2011-03-22 04:00:09 +000083 Disconnect();
Greg Clayton61d043b2011-03-22 04:00:09 +000084}
85
86bool
Greg Clayton58e26e02011-03-24 04:28:38 +000087GDBRemoteCommunicationClient::HandshakeWithServer (Error *error_ptr)
88{
89 // Start the read thread after we send the handshake ack since if we
90 // fail to send the handshake ack, there is no reason to continue...
91 if (SendAck())
Greg Clayton63afdb02011-06-17 01:22:15 +000092 return true;
Greg Clayton58e26e02011-03-24 04:28:38 +000093
94 if (error_ptr)
95 error_ptr->SetErrorString("failed to send the handshake ack");
96 return false;
97}
98
99void
100GDBRemoteCommunicationClient::QueryNoAckModeSupported ()
Greg Clayton61d043b2011-03-22 04:00:09 +0000101{
102 if (m_supports_not_sending_acks == eLazyBoolCalculate)
103 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000104 m_send_acks = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000105 m_supports_not_sending_acks = eLazyBoolNo;
Greg Clayton58e26e02011-03-24 04:28:38 +0000106
107 StringExtractorGDBRemote response;
Greg Clayton61d043b2011-03-22 04:00:09 +0000108 if (SendPacketAndWaitForResponse("QStartNoAckMode", response, false))
109 {
110 if (response.IsOKResponse())
Greg Clayton58e26e02011-03-24 04:28:38 +0000111 {
112 m_send_acks = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000113 m_supports_not_sending_acks = eLazyBoolYes;
Greg Clayton58e26e02011-03-24 04:28:38 +0000114 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000115 }
116 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000117}
118
119void
Greg Claytona1f645e2012-04-10 03:22:03 +0000120GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported ()
121{
122 if (m_supports_threads_in_stop_reply == eLazyBoolCalculate)
123 {
124 m_supports_threads_in_stop_reply = eLazyBoolNo;
125
126 StringExtractorGDBRemote response;
127 if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response, false))
128 {
129 if (response.IsOKResponse())
130 m_supports_threads_in_stop_reply = eLazyBoolYes;
131 }
132 }
133}
134
135
136void
Greg Clayton61d043b2011-03-22 04:00:09 +0000137GDBRemoteCommunicationClient::ResetDiscoverableSettings()
138{
139 m_supports_not_sending_acks = eLazyBoolCalculate;
140 m_supports_thread_suffix = eLazyBoolCalculate;
Greg Claytona1f645e2012-04-10 03:22:03 +0000141 m_supports_threads_in_stop_reply = eLazyBoolCalculate;
Greg Clayton61d043b2011-03-22 04:00:09 +0000142 m_supports_vCont_c = eLazyBoolCalculate;
143 m_supports_vCont_C = eLazyBoolCalculate;
144 m_supports_vCont_s = eLazyBoolCalculate;
145 m_supports_vCont_S = eLazyBoolCalculate;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000146 m_qHostInfo_is_valid = eLazyBoolCalculate;
Greg Clayton2f085c62011-05-15 01:25:55 +0000147 m_supports_alloc_dealloc_memory = eLazyBoolCalculate;
Greg Claytona9385532011-11-18 07:03:08 +0000148 m_supports_memory_region_info = eLazyBoolCalculate;
Greg Clayton989816b2011-05-14 01:50:35 +0000149
Greg Clayton24bc5d92011-03-30 18:16:51 +0000150 m_supports_qProcessInfoPID = true;
151 m_supports_qfProcessInfo = true;
152 m_supports_qUserName = true;
153 m_supports_qGroupName = true;
Greg Claytonb72d0f02011-04-12 05:54:46 +0000154 m_supports_qThreadStopInfo = true;
155 m_supports_z0 = true;
156 m_supports_z1 = true;
157 m_supports_z2 = true;
158 m_supports_z3 = true;
159 m_supports_z4 = true;
Greg Claytoncb8977d2011-03-23 00:09:55 +0000160 m_host_arch.Clear();
Greg Clayton61d043b2011-03-22 04:00:09 +0000161}
162
163
164bool
165GDBRemoteCommunicationClient::GetThreadSuffixSupported ()
166{
167 if (m_supports_thread_suffix == eLazyBoolCalculate)
168 {
169 StringExtractorGDBRemote response;
170 m_supports_thread_suffix = eLazyBoolNo;
171 if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response, false))
172 {
173 if (response.IsOKResponse())
174 m_supports_thread_suffix = eLazyBoolYes;
175 }
176 }
177 return m_supports_thread_suffix;
178}
179bool
180GDBRemoteCommunicationClient::GetVContSupported (char flavor)
181{
182 if (m_supports_vCont_c == eLazyBoolCalculate)
183 {
184 StringExtractorGDBRemote response;
185 m_supports_vCont_any = eLazyBoolNo;
186 m_supports_vCont_all = eLazyBoolNo;
187 m_supports_vCont_c = eLazyBoolNo;
188 m_supports_vCont_C = eLazyBoolNo;
189 m_supports_vCont_s = eLazyBoolNo;
190 m_supports_vCont_S = eLazyBoolNo;
191 if (SendPacketAndWaitForResponse("vCont?", response, false))
192 {
193 const char *response_cstr = response.GetStringRef().c_str();
194 if (::strstr (response_cstr, ";c"))
195 m_supports_vCont_c = eLazyBoolYes;
196
197 if (::strstr (response_cstr, ";C"))
198 m_supports_vCont_C = eLazyBoolYes;
199
200 if (::strstr (response_cstr, ";s"))
201 m_supports_vCont_s = eLazyBoolYes;
202
203 if (::strstr (response_cstr, ";S"))
204 m_supports_vCont_S = eLazyBoolYes;
205
206 if (m_supports_vCont_c == eLazyBoolYes &&
207 m_supports_vCont_C == eLazyBoolYes &&
208 m_supports_vCont_s == eLazyBoolYes &&
209 m_supports_vCont_S == eLazyBoolYes)
210 {
211 m_supports_vCont_all = eLazyBoolYes;
212 }
213
214 if (m_supports_vCont_c == eLazyBoolYes ||
215 m_supports_vCont_C == eLazyBoolYes ||
216 m_supports_vCont_s == eLazyBoolYes ||
217 m_supports_vCont_S == eLazyBoolYes)
218 {
219 m_supports_vCont_any = eLazyBoolYes;
220 }
221 }
222 }
223
224 switch (flavor)
225 {
226 case 'a': return m_supports_vCont_any;
227 case 'A': return m_supports_vCont_all;
228 case 'c': return m_supports_vCont_c;
229 case 'C': return m_supports_vCont_C;
230 case 's': return m_supports_vCont_s;
231 case 'S': return m_supports_vCont_S;
232 default: break;
233 }
234 return false;
235}
236
237
238size_t
239GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
240(
241 const char *payload,
242 StringExtractorGDBRemote &response,
243 bool send_async
244)
245{
246 return SendPacketAndWaitForResponse (payload,
247 ::strlen (payload),
248 response,
249 send_async);
250}
251
252size_t
253GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
254(
255 const char *payload,
256 size_t payload_length,
257 StringExtractorGDBRemote &response,
258 bool send_async
259)
260{
261 Mutex::Locker locker;
Greg Clayton61d043b2011-03-22 04:00:09 +0000262 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton801417e2011-07-07 01:59:51 +0000263 size_t response_len = 0;
Greg Claytonc8dd5702012-04-12 19:04:34 +0000264 if (GetSequenceMutex (locker))
Greg Clayton61d043b2011-03-22 04:00:09 +0000265 {
Greg Clayton139da722011-05-20 03:15:54 +0000266 if (SendPacketNoLock (payload, payload_length))
Greg Clayton801417e2011-07-07 01:59:51 +0000267 response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
268 else
269 {
270 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000271 log->Printf("error: failed to send '%*s'", (int) payload_length, payload);
Greg Clayton801417e2011-07-07 01:59:51 +0000272 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000273 }
274 else
275 {
276 if (send_async)
277 {
278 Mutex::Locker async_locker (m_async_mutex);
279 m_async_packet.assign(payload, payload_length);
280 m_async_packet_predicate.SetValue (true, eBroadcastNever);
281
282 if (log)
283 log->Printf ("async: async packet = %s", m_async_packet.c_str());
284
285 bool timed_out = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000286 if (SendInterrupt(locker, 2, timed_out))
Greg Clayton61d043b2011-03-22 04:00:09 +0000287 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000288 if (m_interrupt_sent)
Greg Clayton61d043b2011-03-22 04:00:09 +0000289 {
Greg Clayton63afdb02011-06-17 01:22:15 +0000290 TimeValue timeout_time;
291 timeout_time = TimeValue::Now();
292 timeout_time.OffsetWithSeconds (m_packet_timeout);
293
Greg Clayton61d043b2011-03-22 04:00:09 +0000294 if (log)
295 log->Printf ("async: sent interrupt");
Greg Clayton801417e2011-07-07 01:59:51 +0000296
Greg Clayton61d043b2011-03-22 04:00:09 +0000297 if (m_async_packet_predicate.WaitForValueEqualTo (false, &timeout_time, &timed_out))
298 {
299 if (log)
300 log->Printf ("async: got response");
Greg Clayton801417e2011-07-07 01:59:51 +0000301
302 // Swap the response buffer to avoid malloc and string copy
303 response.GetStringRef().swap (m_async_response.GetStringRef());
304 response_len = response.GetStringRef().size();
Greg Clayton61d043b2011-03-22 04:00:09 +0000305 }
306 else
307 {
308 if (log)
309 log->Printf ("async: timed out waiting for response");
310 }
311
312 // Make sure we wait until the continue packet has been sent again...
313 if (m_private_is_running.WaitForValueEqualTo (true, &timeout_time, &timed_out))
314 {
Greg Claytonb7669b32011-10-27 22:04:16 +0000315 if (log)
316 {
317 if (timed_out)
318 log->Printf ("async: timed out waiting for process to resume, but process was resumed");
319 else
320 log->Printf ("async: async packet sent");
321 }
322 }
323 else
324 {
Greg Clayton61d043b2011-03-22 04:00:09 +0000325 if (log)
326 log->Printf ("async: timed out waiting for process to resume");
327 }
328 }
329 else
330 {
331 // We had a racy condition where we went to send the interrupt
Greg Clayton05e4d972012-03-29 01:55:41 +0000332 // yet we were able to get the lock, so the process must have
333 // just stopped?
Greg Clayton801417e2011-07-07 01:59:51 +0000334 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000335 log->Printf ("async: got lock without sending interrupt");
336 // Send the packet normally since we got the lock
337 if (SendPacketNoLock (payload, payload_length))
338 response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
339 else
340 {
341 if (log)
342 log->Printf("error: failed to send '%*s'", (int) payload_length, payload);
343 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000344 }
345 }
346 else
347 {
348 if (log)
349 log->Printf ("async: failed to interrupt");
350 }
351 }
352 else
353 {
354 if (log)
Greg Claytonc8dd5702012-04-12 19:04:34 +0000355 log->Printf("error: failed to get packet sequence mutex, not sending packet '%*s'", (int) payload_length, payload);
Greg Clayton61d043b2011-03-22 04:00:09 +0000356 }
357 }
Greg Clayton801417e2011-07-07 01:59:51 +0000358 if (response_len == 0)
359 {
360 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000361 log->Printf("error: failed to get response for '%*s'", (int) payload_length, payload);
Greg Clayton801417e2011-07-07 01:59:51 +0000362 }
363 return response_len;
Greg Clayton61d043b2011-03-22 04:00:09 +0000364}
365
Greg Clayton61d043b2011-03-22 04:00:09 +0000366StateType
367GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse
368(
369 ProcessGDBRemote *process,
370 const char *payload,
371 size_t packet_length,
372 StringExtractorGDBRemote &response
373)
374{
375 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
376 if (log)
377 log->Printf ("GDBRemoteCommunicationClient::%s ()", __FUNCTION__);
378
379 Mutex::Locker locker(m_sequence_mutex);
380 StateType state = eStateRunning;
381
382 BroadcastEvent(eBroadcastBitRunPacketSent, NULL);
383 m_public_is_running.SetValue (true, eBroadcastNever);
384 // Set the starting continue packet into "continue_packet". This packet
385 // make change if we are interrupted and we continue after an async packet...
386 std::string continue_packet(payload, packet_length);
387
Greg Clayton628cead2011-05-19 03:54:16 +0000388 bool got_stdout = false;
389
Greg Clayton61d043b2011-03-22 04:00:09 +0000390 while (state == eStateRunning)
391 {
Greg Clayton628cead2011-05-19 03:54:16 +0000392 if (!got_stdout)
393 {
394 if (log)
395 log->Printf ("GDBRemoteCommunicationClient::%s () sending continue packet: %s", __FUNCTION__, continue_packet.c_str());
Greg Clayton516f0842012-04-11 00:24:49 +0000396 if (SendPacketNoLock(continue_packet.c_str(), continue_packet.size()) == 0)
Greg Clayton628cead2011-05-19 03:54:16 +0000397 state = eStateInvalid;
Greg Clayton61d043b2011-03-22 04:00:09 +0000398
Greg Claytonb7669b32011-10-27 22:04:16 +0000399 m_private_is_running.SetValue (true, eBroadcastAlways);
Greg Clayton628cead2011-05-19 03:54:16 +0000400 }
401
402 got_stdout = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000403
404 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000405 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(%s)", __FUNCTION__, continue_packet.c_str());
Greg Clayton61d043b2011-03-22 04:00:09 +0000406
Greg Clayton516f0842012-04-11 00:24:49 +0000407 if (WaitForPacketWithTimeoutMicroSecondsNoLock(response, UINT32_MAX))
Greg Clayton61d043b2011-03-22 04:00:09 +0000408 {
409 if (response.Empty())
410 state = eStateInvalid;
411 else
412 {
413 const char stop_type = response.GetChar();
414 if (log)
415 log->Printf ("GDBRemoteCommunicationClient::%s () got packet: %s", __FUNCTION__, response.GetStringRef().c_str());
416 switch (stop_type)
417 {
418 case 'T':
419 case 'S':
Greg Clayton61d043b2011-03-22 04:00:09 +0000420 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000421 if (process->GetStopID() == 0)
Greg Clayton61d043b2011-03-22 04:00:09 +0000422 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000423 if (process->GetID() == LLDB_INVALID_PROCESS_ID)
424 {
425 lldb::pid_t pid = GetCurrentProcessID ();
426 if (pid != LLDB_INVALID_PROCESS_ID)
427 process->SetID (pid);
428 }
429 process->BuildDynamicRegisterInfo (true);
Greg Clayton61d043b2011-03-22 04:00:09 +0000430 }
Greg Clayton05e4d972012-03-29 01:55:41 +0000431
432 // Privately notify any internal threads that we have stopped
433 // in case we wanted to interrupt our process, yet we might
434 // send a packet and continue without returning control to the
435 // user.
436 m_private_is_running.SetValue (false, eBroadcastAlways);
437
438 const uint8_t signo = response.GetHexU8 (UINT8_MAX);
439
Greg Claytonc2c822c2012-05-15 02:50:49 +0000440 bool continue_after_async = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000441 if (m_async_signal != -1 || m_async_packet_predicate.GetValue())
442 {
Greg Claytonc2c822c2012-05-15 02:50:49 +0000443 continue_after_async = true;
Greg Clayton05e4d972012-03-29 01:55:41 +0000444 // We sent an interrupt packet to stop the inferior process
445 // for an async signal or to send an async packet while running
446 // but we might have been single stepping and received the
447 // stop packet for the step instead of for the interrupt packet.
448 // Typically when an interrupt is sent a SIGINT or SIGSTOP
449 // is used, so if we get anything else, we need to try and
450 // get another stop reply packet that may have been sent
451 // due to sending the interrupt when the target is stopped
452 // which will just re-send a copy of the last stop reply
453 // packet. If we don't do this, then the reply for our
454 // async packet will be the repeat stop reply packet and cause
455 // a lot of trouble for us!
456 if (signo != SIGINT && signo != SIGSTOP)
457 {
Greg Claytonc2c822c2012-05-15 02:50:49 +0000458 continue_after_async = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000459
460 // We didn't get a a SIGINT or SIGSTOP, so try for a
461 // very brief time (1 ms) to get another stop reply
462 // packet to make sure it doesn't get in the way
463 StringExtractorGDBRemote extra_stop_reply_packet;
464 uint32_t timeout_usec = 1000;
465 if (WaitForPacketWithTimeoutMicroSecondsNoLock (extra_stop_reply_packet, timeout_usec))
466 {
467 switch (extra_stop_reply_packet.GetChar())
468 {
469 case 'T':
470 case 'S':
471 // We did get an extra stop reply, which means
472 // our interrupt didn't stop the target so we
473 // shouldn't continue after the async signal
474 // or packet is sent...
Greg Claytonc2c822c2012-05-15 02:50:49 +0000475 continue_after_async = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000476 break;
477 }
478 }
479 }
480 }
481
482 if (m_async_signal != -1)
483 {
484 if (log)
485 log->Printf ("async: send signo = %s", Host::GetSignalAsCString (m_async_signal));
486
487 // Save off the async signal we are supposed to send
488 const int async_signal = m_async_signal;
489 // Clear the async signal member so we don't end up
490 // sending the signal multiple times...
491 m_async_signal = -1;
492 // Check which signal we stopped with
493 if (signo == async_signal)
494 {
495 if (log)
496 log->Printf ("async: stopped with signal %s, we are done running", Host::GetSignalAsCString (signo));
497
498 // We already stopped with a signal that we wanted
499 // to stop with, so we are done
500 }
501 else
502 {
503 // We stopped with a different signal that the one
504 // we wanted to stop with, so now we must resume
505 // with the signal we want
506 char signal_packet[32];
507 int signal_packet_len = 0;
508 signal_packet_len = ::snprintf (signal_packet,
509 sizeof (signal_packet),
510 "C%2.2x",
511 async_signal);
512
513 if (log)
514 log->Printf ("async: stopped with signal %s, resume with %s",
515 Host::GetSignalAsCString (signo),
516 Host::GetSignalAsCString (async_signal));
517
518 // Set the continue packet to resume even if the
Greg Claytonc2c822c2012-05-15 02:50:49 +0000519 // interrupt didn't cause our stop (ignore continue_after_async)
Greg Clayton05e4d972012-03-29 01:55:41 +0000520 continue_packet.assign(signal_packet, signal_packet_len);
521 continue;
522 }
523 }
524 else if (m_async_packet_predicate.GetValue())
525 {
526 LogSP packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
527
528 // We are supposed to send an asynchronous packet while
529 // we are running.
530 m_async_response.Clear();
531 if (m_async_packet.empty())
532 {
533 if (packet_log)
534 packet_log->Printf ("async: error: empty async packet");
535
536 }
537 else
538 {
539 if (packet_log)
540 packet_log->Printf ("async: sending packet");
541
542 SendPacketAndWaitForResponse (&m_async_packet[0],
543 m_async_packet.size(),
544 m_async_response,
545 false);
546 }
547 // Let the other thread that was trying to send the async
548 // packet know that the packet has been sent and response is
549 // ready...
550 m_async_packet_predicate.SetValue(false, eBroadcastAlways);
551
552 if (packet_log)
Greg Claytonc2c822c2012-05-15 02:50:49 +0000553 packet_log->Printf ("async: sent packet, continue_after_async = %i", continue_after_async);
Greg Clayton05e4d972012-03-29 01:55:41 +0000554
555 // Set the continue packet to resume if our interrupt
556 // for the async packet did cause the stop
Greg Claytonc2c822c2012-05-15 02:50:49 +0000557 if (continue_after_async)
Greg Clayton05e4d972012-03-29 01:55:41 +0000558 {
Greg Claytonc2c822c2012-05-15 02:50:49 +0000559 //continue_packet.assign (1, 'c');
Greg Clayton05e4d972012-03-29 01:55:41 +0000560 continue;
561 }
562 }
563 // Stop with signal and thread info
564 state = eStateStopped;
Greg Clayton61d043b2011-03-22 04:00:09 +0000565 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000566 break;
567
568 case 'W':
569 case 'X':
570 // process exited
571 state = eStateExited;
572 break;
573
574 case 'O':
575 // STDOUT
576 {
Greg Clayton628cead2011-05-19 03:54:16 +0000577 got_stdout = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000578 std::string inferior_stdout;
579 inferior_stdout.reserve(response.GetBytesLeft () / 2);
580 char ch;
581 while ((ch = response.GetHexU8()) != '\0')
582 inferior_stdout.append(1, ch);
583 process->AppendSTDOUT (inferior_stdout.c_str(), inferior_stdout.size());
584 }
585 break;
586
587 case 'E':
588 // ERROR
589 state = eStateInvalid;
590 break;
591
592 default:
593 if (log)
594 log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__);
595 state = eStateInvalid;
596 break;
597 }
598 }
599 }
600 else
601 {
602 if (log)
603 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__);
604 state = eStateInvalid;
605 }
606 }
607 if (log)
608 log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state));
609 response.SetFilePos(0);
610 m_private_is_running.SetValue (false, eBroadcastAlways);
611 m_public_is_running.SetValue (false, eBroadcastAlways);
612 return state;
613}
614
615bool
616GDBRemoteCommunicationClient::SendAsyncSignal (int signo)
617{
Greg Clayton05e4d972012-03-29 01:55:41 +0000618 Mutex::Locker async_locker (m_async_mutex);
Greg Clayton61d043b2011-03-22 04:00:09 +0000619 m_async_signal = signo;
620 bool timed_out = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000621 Mutex::Locker locker;
Greg Clayton05e4d972012-03-29 01:55:41 +0000622 if (SendInterrupt (locker, 1, timed_out))
Greg Clayton61d043b2011-03-22 04:00:09 +0000623 return true;
624 m_async_signal = -1;
625 return false;
626}
627
Greg Clayton516f0842012-04-11 00:24:49 +0000628// This function takes a mutex locker as a parameter in case the GetSequenceMutex
Greg Clayton61d043b2011-03-22 04:00:09 +0000629// actually succeeds. If it doesn't succeed in acquiring the sequence mutex
630// (the expected result), then it will send the halt packet. If it does succeed
631// then the caller that requested the interrupt will want to keep the sequence
632// locked down so that no one else can send packets while the caller has control.
633// This function usually gets called when we are running and need to stop the
634// target. It can also be used when we are running and and we need to do something
635// else (like read/write memory), so we need to interrupt the running process
636// (gdb remote protocol requires this), and do what we need to do, then resume.
637
638bool
Greg Clayton05e4d972012-03-29 01:55:41 +0000639GDBRemoteCommunicationClient::SendInterrupt
Greg Clayton61d043b2011-03-22 04:00:09 +0000640(
641 Mutex::Locker& locker,
642 uint32_t seconds_to_wait_for_stop,
Greg Clayton61d043b2011-03-22 04:00:09 +0000643 bool &timed_out
644)
645{
Greg Clayton05e4d972012-03-29 01:55:41 +0000646 m_interrupt_sent = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000647 timed_out = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000648 LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Clayton61d043b2011-03-22 04:00:09 +0000649
650 if (IsRunning())
651 {
652 // Only send an interrupt if our debugserver is running...
Greg Claytonc8dd5702012-04-12 19:04:34 +0000653 if (GetSequenceMutex (locker))
Greg Clayton516f0842012-04-11 00:24:49 +0000654 {
655 if (log)
656 log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt");
657 }
658 else
Greg Clayton61d043b2011-03-22 04:00:09 +0000659 {
660 // Someone has the mutex locked waiting for a response or for the
661 // inferior to stop, so send the interrupt on the down low...
662 char ctrl_c = '\x03';
663 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton61d043b2011-03-22 04:00:09 +0000664 size_t bytes_written = Write (&ctrl_c, 1, status, NULL);
Greg Clayton05e4d972012-03-29 01:55:41 +0000665 if (log)
666 log->PutCString("send packet: \\x03");
Greg Clayton61d043b2011-03-22 04:00:09 +0000667 if (bytes_written > 0)
668 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000669 m_interrupt_sent = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000670 if (seconds_to_wait_for_stop)
671 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000672 TimeValue timeout;
673 if (seconds_to_wait_for_stop)
674 {
675 timeout = TimeValue::Now();
676 timeout.OffsetWithSeconds (seconds_to_wait_for_stop);
677 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000678 if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out))
679 {
680 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000681 log->PutCString ("SendInterrupt () - sent interrupt, private state stopped");
Greg Clayton61d043b2011-03-22 04:00:09 +0000682 return true;
683 }
684 else
685 {
686 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000687 log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume");
Greg Clayton61d043b2011-03-22 04:00:09 +0000688 }
689 }
690 else
691 {
692 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000693 log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop...");
Greg Clayton61d043b2011-03-22 04:00:09 +0000694 return true;
695 }
696 }
697 else
698 {
699 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000700 log->Printf ("SendInterrupt () - failed to write interrupt");
Greg Clayton61d043b2011-03-22 04:00:09 +0000701 }
702 return false;
703 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000704 }
Greg Clayton05e4d972012-03-29 01:55:41 +0000705 else
706 {
707 if (log)
708 log->Printf ("SendInterrupt () - not running");
709 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000710 return true;
711}
712
713lldb::pid_t
714GDBRemoteCommunicationClient::GetCurrentProcessID ()
715{
716 StringExtractorGDBRemote response;
717 if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false))
718 {
719 if (response.GetChar() == 'Q')
720 if (response.GetChar() == 'C')
721 return response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID);
722 }
723 return LLDB_INVALID_PROCESS_ID;
724}
725
726bool
727GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str)
728{
729 error_str.clear();
730 StringExtractorGDBRemote response;
731 if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false))
732 {
733 if (response.IsOKResponse())
734 return true;
735 if (response.GetChar() == 'E')
736 {
737 // A string the describes what failed when launching...
738 error_str = response.GetStringRef().substr(1);
739 }
740 else
741 {
742 error_str.assign ("unknown error occurred launching process");
743 }
744 }
745 else
746 {
747 error_str.assign ("failed to send the qLaunchSuccess packet");
748 }
749 return false;
750}
751
752int
753GDBRemoteCommunicationClient::SendArgumentsPacket (char const *argv[])
754{
755 if (argv && argv[0])
756 {
757 StreamString packet;
758 packet.PutChar('A');
759 const char *arg;
760 for (uint32_t i = 0; (arg = argv[i]) != NULL; ++i)
761 {
762 const int arg_len = strlen(arg);
763 if (i > 0)
764 packet.PutChar(',');
765 packet.Printf("%i,%i,", arg_len * 2, i);
766 packet.PutBytesAsRawHex8 (arg, arg_len);
767 }
768
769 StringExtractorGDBRemote response;
770 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
771 {
772 if (response.IsOKResponse())
773 return 0;
774 uint8_t error = response.GetError();
775 if (error)
776 return error;
777 }
778 }
779 return -1;
780}
781
782int
783GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value)
784{
785 if (name_equal_value && name_equal_value[0])
786 {
787 StreamString packet;
788 packet.Printf("QEnvironment:%s", name_equal_value);
789 StringExtractorGDBRemote response;
790 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
791 {
792 if (response.IsOKResponse())
793 return 0;
794 uint8_t error = response.GetError();
795 if (error)
796 return error;
797 }
798 }
799 return -1;
800}
801
Greg Claytona4582402011-05-08 04:53:50 +0000802int
803GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch)
804{
805 if (arch && arch[0])
806 {
807 StreamString packet;
808 packet.Printf("QLaunchArch:%s", arch);
809 StringExtractorGDBRemote response;
810 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
811 {
812 if (response.IsOKResponse())
813 return 0;
814 uint8_t error = response.GetError();
815 if (error)
816 return error;
817 }
818 }
819 return -1;
820}
821
Greg Clayton61d043b2011-03-22 04:00:09 +0000822bool
Greg Clayton58e26e02011-03-24 04:28:38 +0000823GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major,
824 uint32_t &minor,
825 uint32_t &update)
826{
827 if (GetHostInfo ())
828 {
829 if (m_os_version_major != UINT32_MAX)
830 {
831 major = m_os_version_major;
832 minor = m_os_version_minor;
833 update = m_os_version_update;
834 return true;
835 }
836 }
837 return false;
838}
839
840bool
841GDBRemoteCommunicationClient::GetOSBuildString (std::string &s)
842{
843 if (GetHostInfo ())
844 {
845 if (!m_os_build.empty())
846 {
847 s = m_os_build;
848 return true;
849 }
850 }
851 s.clear();
852 return false;
853}
854
855
856bool
857GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s)
858{
859 if (GetHostInfo ())
860 {
861 if (!m_os_kernel.empty())
862 {
863 s = m_os_kernel;
864 return true;
865 }
866 }
867 s.clear();
868 return false;
869}
870
871bool
872GDBRemoteCommunicationClient::GetHostname (std::string &s)
873{
874 if (GetHostInfo ())
875 {
876 if (!m_hostname.empty())
877 {
878 s = m_hostname;
879 return true;
880 }
881 }
882 s.clear();
883 return false;
884}
885
886ArchSpec
887GDBRemoteCommunicationClient::GetSystemArchitecture ()
888{
889 if (GetHostInfo ())
890 return m_host_arch;
891 return ArchSpec();
892}
893
894
895bool
Greg Clayton06d7cc82011-04-04 18:18:57 +0000896GDBRemoteCommunicationClient::GetHostInfo (bool force)
Greg Clayton61d043b2011-03-22 04:00:09 +0000897{
Greg Clayton06d7cc82011-04-04 18:18:57 +0000898 if (force || m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton61d043b2011-03-22 04:00:09 +0000899 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000900 m_qHostInfo_is_valid = eLazyBoolNo;
Greg Clayton61d043b2011-03-22 04:00:09 +0000901 StringExtractorGDBRemote response;
902 if (SendPacketAndWaitForResponse ("qHostInfo", response, false))
903 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +0000904 if (response.IsNormalResponse())
Greg Claytoncb8977d2011-03-23 00:09:55 +0000905 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000906 std::string name;
907 std::string value;
908 uint32_t cpu = LLDB_INVALID_CPUTYPE;
909 uint32_t sub = 0;
910 std::string arch_name;
911 std::string os_name;
912 std::string vendor_name;
913 std::string triple;
914 uint32_t pointer_byte_size = 0;
915 StringExtractor extractor;
916 ByteOrder byte_order = eByteOrderInvalid;
917 uint32_t num_keys_decoded = 0;
918 while (response.GetNameColonValue(name, value))
Greg Claytoncb8977d2011-03-23 00:09:55 +0000919 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000920 if (name.compare("cputype") == 0)
Greg Clayton58e26e02011-03-24 04:28:38 +0000921 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000922 // exception type in big endian hex
923 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0);
924 if (cpu != LLDB_INVALID_CPUTYPE)
925 ++num_keys_decoded;
926 }
927 else if (name.compare("cpusubtype") == 0)
928 {
929 // exception count in big endian hex
930 sub = Args::StringToUInt32 (value.c_str(), 0, 0);
931 if (sub != 0)
932 ++num_keys_decoded;
933 }
934 else if (name.compare("arch") == 0)
935 {
936 arch_name.swap (value);
937 ++num_keys_decoded;
938 }
939 else if (name.compare("triple") == 0)
940 {
941 // The triple comes as ASCII hex bytes since it contains '-' chars
942 extractor.GetStringRef().swap(value);
943 extractor.SetFilePos(0);
944 extractor.GetHexByteString (triple);
945 ++num_keys_decoded;
946 }
947 else if (name.compare("os_build") == 0)
948 {
949 extractor.GetStringRef().swap(value);
950 extractor.SetFilePos(0);
951 extractor.GetHexByteString (m_os_build);
952 ++num_keys_decoded;
953 }
954 else if (name.compare("hostname") == 0)
955 {
956 extractor.GetStringRef().swap(value);
957 extractor.SetFilePos(0);
958 extractor.GetHexByteString (m_hostname);
959 ++num_keys_decoded;
960 }
961 else if (name.compare("os_kernel") == 0)
962 {
963 extractor.GetStringRef().swap(value);
964 extractor.SetFilePos(0);
965 extractor.GetHexByteString (m_os_kernel);
966 ++num_keys_decoded;
967 }
968 else if (name.compare("ostype") == 0)
969 {
970 os_name.swap (value);
971 ++num_keys_decoded;
972 }
973 else if (name.compare("vendor") == 0)
974 {
975 vendor_name.swap(value);
976 ++num_keys_decoded;
977 }
978 else if (name.compare("endian") == 0)
979 {
980 ++num_keys_decoded;
981 if (value.compare("little") == 0)
982 byte_order = eByteOrderLittle;
983 else if (value.compare("big") == 0)
984 byte_order = eByteOrderBig;
985 else if (value.compare("pdp") == 0)
986 byte_order = eByteOrderPDP;
987 else
988 --num_keys_decoded;
989 }
990 else if (name.compare("ptrsize") == 0)
991 {
992 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 0);
993 if (pointer_byte_size != 0)
994 ++num_keys_decoded;
995 }
996 else if (name.compare("os_version") == 0)
997 {
998 Args::StringToVersion (value.c_str(),
999 m_os_version_major,
1000 m_os_version_minor,
1001 m_os_version_update);
1002 if (m_os_version_major != UINT32_MAX)
1003 ++num_keys_decoded;
1004 }
1005 }
1006
1007 if (num_keys_decoded > 0)
1008 m_qHostInfo_is_valid = eLazyBoolYes;
1009
1010 if (triple.empty())
1011 {
1012 if (arch_name.empty())
1013 {
1014 if (cpu != LLDB_INVALID_CPUTYPE)
1015 {
1016 m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1017 if (pointer_byte_size)
1018 {
1019 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1020 }
1021 if (byte_order != eByteOrderInvalid)
1022 {
1023 assert (byte_order == m_host_arch.GetByteOrder());
1024 }
Greg Claytonb170aee2012-05-08 01:45:38 +00001025
1026 if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0)
1027 {
1028 switch (m_host_arch.GetMachine())
1029 {
1030 case llvm::Triple::arm:
1031 case llvm::Triple::thumb:
1032 os_name = "ios";
1033 break;
1034 default:
1035 os_name = "macosx";
1036 break;
1037 }
1038 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001039 if (!vendor_name.empty())
1040 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1041 if (!os_name.empty())
Greg Clayton89798cc2011-09-15 00:21:03 +00001042 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001043
1044 }
1045 }
1046 else
1047 {
1048 std::string triple;
1049 triple += arch_name;
Greg Claytonb170aee2012-05-08 01:45:38 +00001050 if (!vendor_name.empty() || !os_name.empty())
1051 {
1052 triple += '-';
1053 if (vendor_name.empty())
1054 triple += "unknown";
1055 else
1056 triple += vendor_name;
1057 triple += '-';
1058 if (os_name.empty())
1059 triple += "unknown";
1060 else
1061 triple += os_name;
1062 }
1063 m_host_arch.SetTriple (triple.c_str());
1064
1065 llvm::Triple &host_triple = m_host_arch.GetTriple();
1066 if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin)
1067 {
1068 switch (m_host_arch.GetMachine())
1069 {
1070 case llvm::Triple::arm:
1071 case llvm::Triple::thumb:
1072 host_triple.setOS(llvm::Triple::IOS);
1073 break;
1074 default:
1075 host_triple.setOS(llvm::Triple::MacOSX);
1076 break;
1077 }
1078 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001079 if (pointer_byte_size)
1080 {
1081 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1082 }
1083 if (byte_order != eByteOrderInvalid)
1084 {
1085 assert (byte_order == m_host_arch.GetByteOrder());
1086 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001087
Greg Clayton58e26e02011-03-24 04:28:38 +00001088 }
1089 }
1090 else
1091 {
Greg Claytonb170aee2012-05-08 01:45:38 +00001092 m_host_arch.SetTriple (triple.c_str());
Greg Claytoncb8977d2011-03-23 00:09:55 +00001093 if (pointer_byte_size)
1094 {
1095 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1096 }
1097 if (byte_order != eByteOrderInvalid)
1098 {
1099 assert (byte_order == m_host_arch.GetByteOrder());
1100 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001101 }
Greg Claytoncb8977d2011-03-23 00:09:55 +00001102 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001103 }
1104 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001105 return m_qHostInfo_is_valid == eLazyBoolYes;
Greg Clayton61d043b2011-03-22 04:00:09 +00001106}
1107
1108int
1109GDBRemoteCommunicationClient::SendAttach
1110(
1111 lldb::pid_t pid,
1112 StringExtractorGDBRemote& response
1113)
1114{
1115 if (pid != LLDB_INVALID_PROCESS_ID)
1116 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001117 char packet[64];
Greg Claytond9919d32011-12-01 23:28:38 +00001118 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%llx", pid);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001119 assert (packet_len < sizeof(packet));
1120 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton61d043b2011-03-22 04:00:09 +00001121 {
1122 if (response.IsErrorResponse())
1123 return response.GetError();
1124 return 0;
1125 }
1126 }
1127 return -1;
1128}
1129
1130const lldb_private::ArchSpec &
1131GDBRemoteCommunicationClient::GetHostArchitecture ()
1132{
Greg Clayton24bc5d92011-03-30 18:16:51 +00001133 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton61d043b2011-03-22 04:00:09 +00001134 GetHostInfo ();
Greg Claytoncb8977d2011-03-23 00:09:55 +00001135 return m_host_arch;
Greg Clayton61d043b2011-03-22 04:00:09 +00001136}
1137
1138addr_t
1139GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions)
1140{
Greg Clayton2f085c62011-05-15 01:25:55 +00001141 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton61d043b2011-03-22 04:00:09 +00001142 {
Greg Clayton2f085c62011-05-15 01:25:55 +00001143 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton989816b2011-05-14 01:50:35 +00001144 char packet[64];
1145 const int packet_len = ::snprintf (packet, sizeof(packet), "_M%zx,%s%s%s", size,
1146 permissions & lldb::ePermissionsReadable ? "r" : "",
1147 permissions & lldb::ePermissionsWritable ? "w" : "",
1148 permissions & lldb::ePermissionsExecutable ? "x" : "");
1149 assert (packet_len < sizeof(packet));
1150 StringExtractorGDBRemote response;
1151 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1152 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001153 if (!response.IsErrorResponse())
Greg Clayton989816b2011-05-14 01:50:35 +00001154 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1155 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001156 else
1157 {
1158 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1159 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001160 }
1161 return LLDB_INVALID_ADDRESS;
1162}
1163
1164bool
1165GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr)
1166{
Greg Clayton2f085c62011-05-15 01:25:55 +00001167 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton61d043b2011-03-22 04:00:09 +00001168 {
Greg Clayton2f085c62011-05-15 01:25:55 +00001169 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton989816b2011-05-14 01:50:35 +00001170 char packet[64];
1171 const int packet_len = ::snprintf(packet, sizeof(packet), "_m%llx", (uint64_t)addr);
1172 assert (packet_len < sizeof(packet));
1173 StringExtractorGDBRemote response;
1174 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1175 {
1176 if (response.IsOKResponse())
1177 return true;
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001178 }
1179 else
1180 {
1181 m_supports_alloc_dealloc_memory = eLazyBoolNo;
Greg Clayton989816b2011-05-14 01:50:35 +00001182 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001183 }
1184 return false;
1185}
1186
Greg Clayton516f0842012-04-11 00:24:49 +00001187bool
1188GDBRemoteCommunicationClient::Detach ()
1189{
1190 return SendPacket ("D", 1) > 0;
1191}
1192
Greg Claytona9385532011-11-18 07:03:08 +00001193Error
1194GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr,
1195 lldb_private::MemoryRegionInfo &region_info)
1196{
1197 Error error;
1198 region_info.Clear();
1199
1200 if (m_supports_memory_region_info != eLazyBoolNo)
1201 {
1202 m_supports_memory_region_info = eLazyBoolYes;
1203 char packet[64];
1204 const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%llx", (uint64_t)addr);
1205 assert (packet_len < sizeof(packet));
1206 StringExtractorGDBRemote response;
1207 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1208 {
1209 std::string name;
1210 std::string value;
1211 addr_t addr_value;
1212 bool success = true;
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001213 bool saw_permissions = false;
Greg Claytona9385532011-11-18 07:03:08 +00001214 while (success && response.GetNameColonValue(name, value))
1215 {
1216 if (name.compare ("start") == 0)
1217 {
1218 addr_value = Args::StringToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success);
1219 if (success)
1220 region_info.GetRange().SetRangeBase(addr_value);
1221 }
1222 else if (name.compare ("size") == 0)
1223 {
1224 addr_value = Args::StringToUInt64(value.c_str(), 0, 16, &success);
1225 if (success)
1226 region_info.GetRange().SetByteSize (addr_value);
1227 }
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001228 else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid())
Greg Claytona9385532011-11-18 07:03:08 +00001229 {
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001230 saw_permissions = true;
1231 if (region_info.GetRange().Contains (addr))
1232 {
1233 if (value.find('r') != std::string::npos)
1234 region_info.SetReadable (MemoryRegionInfo::eYes);
1235 else
1236 region_info.SetReadable (MemoryRegionInfo::eNo);
1237
1238 if (value.find('w') != std::string::npos)
1239 region_info.SetWritable (MemoryRegionInfo::eYes);
1240 else
1241 region_info.SetWritable (MemoryRegionInfo::eNo);
1242
1243 if (value.find('x') != std::string::npos)
1244 region_info.SetExecutable (MemoryRegionInfo::eYes);
1245 else
1246 region_info.SetExecutable (MemoryRegionInfo::eNo);
1247 }
1248 else
1249 {
1250 // The reported region does not contain this address -- we're looking at an unmapped page
1251 region_info.SetReadable (MemoryRegionInfo::eNo);
1252 region_info.SetWritable (MemoryRegionInfo::eNo);
1253 region_info.SetExecutable (MemoryRegionInfo::eNo);
1254 }
Greg Claytona9385532011-11-18 07:03:08 +00001255 }
1256 else if (name.compare ("error") == 0)
1257 {
1258 StringExtractorGDBRemote name_extractor;
1259 // Swap "value" over into "name_extractor"
1260 name_extractor.GetStringRef().swap(value);
1261 // Now convert the HEX bytes into a string value
1262 name_extractor.GetHexByteString (value);
1263 error.SetErrorString(value.c_str());
1264 }
1265 }
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001266
1267 // We got a valid address range back but no permissions -- which means this is an unmapped page
1268 if (region_info.GetRange().IsValid() && saw_permissions == false)
1269 {
1270 region_info.SetReadable (MemoryRegionInfo::eNo);
1271 region_info.SetWritable (MemoryRegionInfo::eNo);
1272 region_info.SetExecutable (MemoryRegionInfo::eNo);
1273 }
Greg Claytona9385532011-11-18 07:03:08 +00001274 }
1275 else
1276 {
1277 m_supports_memory_region_info = eLazyBoolNo;
1278 }
1279 }
1280
1281 if (m_supports_memory_region_info == eLazyBoolNo)
1282 {
1283 error.SetErrorString("qMemoryRegionInfo is not supported");
1284 }
1285 if (error.Fail())
1286 region_info.Clear();
1287 return error;
1288
1289}
1290
Johnny Chen7cbdcfb2012-05-23 21:09:52 +00001291Error
1292GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num)
1293{
1294 Error error;
1295
1296 if (m_supports_watchpoint_support_info == eLazyBoolYes)
1297 {
1298 num = m_num_supported_hardware_watchpoints;
1299 return error;
1300 }
1301
1302 // Set num to 0 first.
1303 num = 0;
1304 if (m_supports_watchpoint_support_info != eLazyBoolNo)
1305 {
1306 char packet[64];
1307 const int packet_len = ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:");
1308 assert (packet_len < sizeof(packet));
1309 StringExtractorGDBRemote response;
1310 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1311 {
1312 m_supports_watchpoint_support_info = eLazyBoolYes;
1313 std::string name;
1314 std::string value;
1315 while (response.GetNameColonValue(name, value))
1316 {
1317 if (name.compare ("num") == 0)
1318 {
1319 num = Args::StringToUInt32(value.c_str(), 0, 0);
1320 m_num_supported_hardware_watchpoints = num;
1321 }
1322 }
1323 }
1324 else
1325 {
1326 m_supports_watchpoint_support_info = eLazyBoolNo;
1327 }
1328 }
1329
1330 if (m_supports_watchpoint_support_info == eLazyBoolNo)
1331 {
1332 error.SetErrorString("qWatchpointSupportInfo is not supported");
1333 }
1334 return error;
1335
1336}
Greg Claytona9385532011-11-18 07:03:08 +00001337
Greg Clayton61d043b2011-03-22 04:00:09 +00001338int
1339GDBRemoteCommunicationClient::SetSTDIN (char const *path)
1340{
1341 if (path && path[0])
1342 {
1343 StreamString packet;
1344 packet.PutCString("QSetSTDIN:");
1345 packet.PutBytesAsRawHex8(path, strlen(path));
1346
1347 StringExtractorGDBRemote response;
1348 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1349 {
1350 if (response.IsOKResponse())
1351 return 0;
1352 uint8_t error = response.GetError();
1353 if (error)
1354 return error;
1355 }
1356 }
1357 return -1;
1358}
1359
1360int
1361GDBRemoteCommunicationClient::SetSTDOUT (char const *path)
1362{
1363 if (path && path[0])
1364 {
1365 StreamString packet;
1366 packet.PutCString("QSetSTDOUT:");
1367 packet.PutBytesAsRawHex8(path, strlen(path));
1368
1369 StringExtractorGDBRemote response;
1370 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1371 {
1372 if (response.IsOKResponse())
1373 return 0;
1374 uint8_t error = response.GetError();
1375 if (error)
1376 return error;
1377 }
1378 }
1379 return -1;
1380}
1381
1382int
1383GDBRemoteCommunicationClient::SetSTDERR (char const *path)
1384{
1385 if (path && path[0])
1386 {
1387 StreamString packet;
1388 packet.PutCString("QSetSTDERR:");
1389 packet.PutBytesAsRawHex8(path, strlen(path));
1390
1391 StringExtractorGDBRemote response;
1392 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1393 {
1394 if (response.IsOKResponse())
1395 return 0;
1396 uint8_t error = response.GetError();
1397 if (error)
1398 return error;
1399 }
1400 }
1401 return -1;
1402}
1403
1404int
1405GDBRemoteCommunicationClient::SetWorkingDir (char const *path)
1406{
1407 if (path && path[0])
1408 {
1409 StreamString packet;
1410 packet.PutCString("QSetWorkingDir:");
1411 packet.PutBytesAsRawHex8(path, strlen(path));
1412
1413 StringExtractorGDBRemote response;
1414 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1415 {
1416 if (response.IsOKResponse())
1417 return 0;
1418 uint8_t error = response.GetError();
1419 if (error)
1420 return error;
1421 }
1422 }
1423 return -1;
1424}
1425
1426int
1427GDBRemoteCommunicationClient::SetDisableASLR (bool enable)
1428{
Greg Clayton24bc5d92011-03-30 18:16:51 +00001429 char packet[32];
1430 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0);
1431 assert (packet_len < sizeof(packet));
Greg Clayton61d043b2011-03-22 04:00:09 +00001432 StringExtractorGDBRemote response;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001433 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton61d043b2011-03-22 04:00:09 +00001434 {
1435 if (response.IsOKResponse())
1436 return 0;
1437 uint8_t error = response.GetError();
1438 if (error)
1439 return error;
1440 }
1441 return -1;
1442}
Greg Clayton24bc5d92011-03-30 18:16:51 +00001443
1444bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001445GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001446{
1447 if (response.IsNormalResponse())
1448 {
1449 std::string name;
1450 std::string value;
1451 StringExtractor extractor;
1452
1453 while (response.GetNameColonValue(name, value))
1454 {
1455 if (name.compare("pid") == 0)
1456 {
1457 process_info.SetProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1458 }
1459 else if (name.compare("ppid") == 0)
1460 {
1461 process_info.SetParentProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1462 }
1463 else if (name.compare("uid") == 0)
1464 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001465 process_info.SetUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001466 }
1467 else if (name.compare("euid") == 0)
1468 {
1469 process_info.SetEffectiveUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1470 }
1471 else if (name.compare("gid") == 0)
1472 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001473 process_info.SetGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001474 }
1475 else if (name.compare("egid") == 0)
1476 {
1477 process_info.SetEffectiveGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1478 }
1479 else if (name.compare("triple") == 0)
1480 {
1481 // The triple comes as ASCII hex bytes since it contains '-' chars
1482 extractor.GetStringRef().swap(value);
1483 extractor.SetFilePos(0);
1484 extractor.GetHexByteString (value);
Greg Claytonb170aee2012-05-08 01:45:38 +00001485 process_info.GetArchitecture ().SetTriple (value.c_str());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001486 }
1487 else if (name.compare("name") == 0)
1488 {
1489 StringExtractor extractor;
Filipe Cabecinhase61ec7f2012-05-07 09:30:51 +00001490 // The process name from ASCII hex bytes since we can't
Greg Clayton24bc5d92011-03-30 18:16:51 +00001491 // control the characters in a process name
1492 extractor.GetStringRef().swap(value);
1493 extractor.SetFilePos(0);
1494 extractor.GetHexByteString (value);
Greg Clayton527154d2011-11-15 03:53:30 +00001495 process_info.GetExecutableFile().SetFile (value.c_str(), false);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001496 }
1497 }
1498
1499 if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
1500 return true;
1501 }
1502 return false;
1503}
1504
1505bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001506GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001507{
1508 process_info.Clear();
1509
1510 if (m_supports_qProcessInfoPID)
1511 {
1512 char packet[32];
Greg Claytond9919d32011-12-01 23:28:38 +00001513 const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%llu", pid);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001514 assert (packet_len < sizeof(packet));
1515 StringExtractorGDBRemote response;
1516 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1517 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001518 return DecodeProcessInfoResponse (response, process_info);
1519 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001520 else
1521 {
1522 m_supports_qProcessInfoPID = false;
1523 return false;
1524 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001525 }
1526 return false;
1527}
1528
1529uint32_t
Greg Claytonb72d0f02011-04-12 05:54:46 +00001530GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1531 ProcessInstanceInfoList &process_infos)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001532{
1533 process_infos.Clear();
1534
1535 if (m_supports_qfProcessInfo)
1536 {
1537 StreamString packet;
1538 packet.PutCString ("qfProcessInfo");
1539 if (!match_info.MatchAllProcesses())
1540 {
1541 packet.PutChar (':');
1542 const char *name = match_info.GetProcessInfo().GetName();
1543 bool has_name_match = false;
1544 if (name && name[0])
1545 {
1546 has_name_match = true;
1547 NameMatchType name_match_type = match_info.GetNameMatchType();
1548 switch (name_match_type)
1549 {
1550 case eNameMatchIgnore:
1551 has_name_match = false;
1552 break;
1553
1554 case eNameMatchEquals:
1555 packet.PutCString ("name_match:equals;");
1556 break;
1557
1558 case eNameMatchContains:
1559 packet.PutCString ("name_match:contains;");
1560 break;
1561
1562 case eNameMatchStartsWith:
1563 packet.PutCString ("name_match:starts_with;");
1564 break;
1565
1566 case eNameMatchEndsWith:
1567 packet.PutCString ("name_match:ends_with;");
1568 break;
1569
1570 case eNameMatchRegularExpression:
1571 packet.PutCString ("name_match:regex;");
1572 break;
1573 }
1574 if (has_name_match)
1575 {
1576 packet.PutCString ("name:");
1577 packet.PutBytesAsRawHex8(name, ::strlen(name));
1578 packet.PutChar (';');
1579 }
1580 }
1581
1582 if (match_info.GetProcessInfo().ProcessIDIsValid())
Greg Claytond9919d32011-12-01 23:28:38 +00001583 packet.Printf("pid:%llu;",match_info.GetProcessInfo().GetProcessID());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001584 if (match_info.GetProcessInfo().ParentProcessIDIsValid())
Greg Claytond9919d32011-12-01 23:28:38 +00001585 packet.Printf("parent_pid:%llu;",match_info.GetProcessInfo().GetParentProcessID());
Greg Claytonb72d0f02011-04-12 05:54:46 +00001586 if (match_info.GetProcessInfo().UserIDIsValid())
1587 packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID());
1588 if (match_info.GetProcessInfo().GroupIDIsValid())
1589 packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001590 if (match_info.GetProcessInfo().EffectiveUserIDIsValid())
1591 packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID());
1592 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1593 packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID());
1594 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1595 packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0);
1596 if (match_info.GetProcessInfo().GetArchitecture().IsValid())
1597 {
1598 const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture();
1599 const llvm::Triple &triple = match_arch.GetTriple();
1600 packet.PutCString("triple:");
1601 packet.PutCStringAsRawHex8(triple.getTriple().c_str());
1602 packet.PutChar (';');
1603 }
1604 }
1605 StringExtractorGDBRemote response;
1606 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1607 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001608 do
1609 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001610 ProcessInstanceInfo process_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001611 if (!DecodeProcessInfoResponse (response, process_info))
1612 break;
1613 process_infos.Append(process_info);
1614 response.GetStringRef().clear();
1615 response.SetFilePos(0);
1616 } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false));
1617 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001618 else
1619 {
1620 m_supports_qfProcessInfo = false;
1621 return 0;
1622 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001623 }
1624 return process_infos.GetSize();
1625
1626}
1627
1628bool
1629GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name)
1630{
1631 if (m_supports_qUserName)
1632 {
1633 char packet[32];
1634 const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid);
1635 assert (packet_len < sizeof(packet));
1636 StringExtractorGDBRemote response;
1637 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1638 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001639 if (response.IsNormalResponse())
1640 {
1641 // Make sure we parsed the right number of characters. The response is
1642 // the hex encoded user name and should make up the entire packet.
1643 // If there are any non-hex ASCII bytes, the length won't match below..
1644 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
1645 return true;
1646 }
1647 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001648 else
1649 {
1650 m_supports_qUserName = false;
1651 return false;
1652 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001653 }
1654 return false;
1655
1656}
1657
1658bool
1659GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name)
1660{
1661 if (m_supports_qGroupName)
1662 {
1663 char packet[32];
1664 const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid);
1665 assert (packet_len < sizeof(packet));
1666 StringExtractorGDBRemote response;
1667 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1668 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001669 if (response.IsNormalResponse())
1670 {
1671 // Make sure we parsed the right number of characters. The response is
1672 // the hex encoded group name and should make up the entire packet.
1673 // If there are any non-hex ASCII bytes, the length won't match below..
1674 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
1675 return true;
1676 }
1677 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001678 else
1679 {
1680 m_supports_qGroupName = false;
1681 return false;
1682 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001683 }
1684 return false;
Greg Clayton06d7cc82011-04-04 18:18:57 +00001685}
Greg Clayton24bc5d92011-03-30 18:16:51 +00001686
Greg Clayton06d7cc82011-04-04 18:18:57 +00001687void
1688GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets)
1689{
1690 uint32_t i;
1691 TimeValue start_time, end_time;
1692 uint64_t total_time_nsec;
1693 float packets_per_second;
1694 if (SendSpeedTestPacket (0, 0))
1695 {
1696 for (uint32_t send_size = 0; send_size <= 1024; send_size *= 2)
1697 {
1698 for (uint32_t recv_size = 0; recv_size <= 1024; recv_size *= 2)
1699 {
1700 start_time = TimeValue::Now();
1701 for (i=0; i<num_packets; ++i)
1702 {
1703 SendSpeedTestPacket (send_size, recv_size);
1704 }
1705 end_time = TimeValue::Now();
1706 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001707 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Greg Clayton153ccd72011-08-10 02:10:13 +00001708 printf ("%u qSpeedTest(send=%-5u, recv=%-5u) in %llu.%9.9llu sec for %f packets/sec.\n",
Greg Clayton06d7cc82011-04-04 18:18:57 +00001709 num_packets,
1710 send_size,
1711 recv_size,
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001712 total_time_nsec / TimeValue::NanoSecPerSec,
1713 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton06d7cc82011-04-04 18:18:57 +00001714 packets_per_second);
1715 if (recv_size == 0)
1716 recv_size = 32;
1717 }
1718 if (send_size == 0)
1719 send_size = 32;
1720 }
1721 }
1722 else
1723 {
1724 start_time = TimeValue::Now();
1725 for (i=0; i<num_packets; ++i)
1726 {
1727 GetCurrentProcessID ();
1728 }
1729 end_time = TimeValue::Now();
1730 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001731 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Greg Clayton153ccd72011-08-10 02:10:13 +00001732 printf ("%u 'qC' packets packets in 0x%llu%9.9llu sec for %f packets/sec.\n",
Greg Clayton06d7cc82011-04-04 18:18:57 +00001733 num_packets,
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001734 total_time_nsec / TimeValue::NanoSecPerSec,
1735 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton06d7cc82011-04-04 18:18:57 +00001736 packets_per_second);
1737 }
1738}
1739
1740bool
1741GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size)
1742{
1743 StreamString packet;
1744 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size);
1745 uint32_t bytes_left = send_size;
1746 while (bytes_left > 0)
1747 {
1748 if (bytes_left >= 26)
1749 {
1750 packet.PutCString("abcdefghijklmnopqrstuvwxyz");
1751 bytes_left -= 26;
1752 }
1753 else
1754 {
1755 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz");
1756 bytes_left = 0;
1757 }
1758 }
1759
1760 StringExtractorGDBRemote response;
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001761 return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) > 0;
Greg Clayton06d7cc82011-04-04 18:18:57 +00001762 return false;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001763}
Greg Claytonb72d0f02011-04-12 05:54:46 +00001764
1765uint16_t
1766GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort ()
1767{
1768 StringExtractorGDBRemote response;
1769 if (SendPacketAndWaitForResponse("qLaunchGDBServer", strlen("qLaunchGDBServer"), response, false))
1770 {
1771 std::string name;
1772 std::string value;
1773 uint16_t port = 0;
1774 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
1775 while (response.GetNameColonValue(name, value))
1776 {
1777 if (name.size() == 4 && name.compare("port") == 0)
1778 port = Args::StringToUInt32(value.c_str(), 0, 0);
1779 if (name.size() == 3 && name.compare("pid") == 0)
1780 pid = Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0);
1781 }
1782 return port;
1783 }
1784 return 0;
1785}
1786
1787bool
1788GDBRemoteCommunicationClient::SetCurrentThread (int tid)
1789{
1790 if (m_curr_tid == tid)
1791 return true;
1792
1793 char packet[32];
1794 int packet_len;
1795 if (tid <= 0)
1796 packet_len = ::snprintf (packet, sizeof(packet), "Hg%i", tid);
1797 else
1798 packet_len = ::snprintf (packet, sizeof(packet), "Hg%x", tid);
1799 assert (packet_len + 1 < sizeof(packet));
1800 StringExtractorGDBRemote response;
1801 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
1802 {
1803 if (response.IsOKResponse())
1804 {
1805 m_curr_tid = tid;
1806 return true;
1807 }
1808 }
1809 return false;
1810}
1811
1812bool
1813GDBRemoteCommunicationClient::SetCurrentThreadForRun (int tid)
1814{
1815 if (m_curr_tid_run == tid)
1816 return true;
1817
1818 char packet[32];
1819 int packet_len;
1820 if (tid <= 0)
1821 packet_len = ::snprintf (packet, sizeof(packet), "Hc%i", tid);
1822 else
1823 packet_len = ::snprintf (packet, sizeof(packet), "Hc%x", tid);
1824
1825 assert (packet_len + 1 < sizeof(packet));
1826 StringExtractorGDBRemote response;
1827 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
1828 {
1829 if (response.IsOKResponse())
1830 {
1831 m_curr_tid_run = tid;
1832 return true;
1833 }
1834 }
1835 return false;
1836}
1837
1838bool
1839GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response)
1840{
1841 if (SendPacketAndWaitForResponse("?", 1, response, false))
1842 return response.IsNormalResponse();
1843 return false;
1844}
1845
1846bool
1847GDBRemoteCommunicationClient::GetThreadStopInfo (uint32_t tid, StringExtractorGDBRemote &response)
1848{
1849 if (m_supports_qThreadStopInfo)
1850 {
1851 char packet[256];
1852 int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%x", tid);
1853 assert (packet_len < sizeof(packet));
1854 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
1855 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001856 if (response.IsNormalResponse())
Greg Claytonb72d0f02011-04-12 05:54:46 +00001857 return true;
1858 else
1859 return false;
1860 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001861 else
1862 {
1863 m_supports_qThreadStopInfo = false;
1864 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001865 }
Greg Clayton139da722011-05-20 03:15:54 +00001866// if (SetCurrentThread (tid))
1867// return GetStopReply (response);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001868 return false;
1869}
1870
1871
1872uint8_t
1873GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length)
1874{
1875 switch (type)
1876 {
1877 case eBreakpointSoftware: if (!m_supports_z0) return UINT8_MAX; break;
1878 case eBreakpointHardware: if (!m_supports_z1) return UINT8_MAX; break;
1879 case eWatchpointWrite: if (!m_supports_z2) return UINT8_MAX; break;
1880 case eWatchpointRead: if (!m_supports_z3) return UINT8_MAX; break;
1881 case eWatchpointReadWrite: if (!m_supports_z4) return UINT8_MAX; break;
1882 default: return UINT8_MAX;
1883 }
1884
1885 char packet[64];
1886 const int packet_len = ::snprintf (packet,
1887 sizeof(packet),
1888 "%c%i,%llx,%x",
1889 insert ? 'Z' : 'z',
1890 type,
1891 addr,
1892 length);
1893
1894 assert (packet_len + 1 < sizeof(packet));
1895 StringExtractorGDBRemote response;
1896 if (SendPacketAndWaitForResponse(packet, packet_len, response, true))
1897 {
1898 if (response.IsOKResponse())
1899 return 0;
Greg Claytonb72d0f02011-04-12 05:54:46 +00001900 else if (response.IsErrorResponse())
1901 return response.GetError();
1902 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001903 else
1904 {
1905 switch (type)
1906 {
1907 case eBreakpointSoftware: m_supports_z0 = false; break;
1908 case eBreakpointHardware: m_supports_z1 = false; break;
1909 case eWatchpointWrite: m_supports_z2 = false; break;
1910 case eWatchpointRead: m_supports_z3 = false; break;
1911 case eWatchpointReadWrite: m_supports_z4 = false; break;
1912 default: break;
1913 }
1914 }
1915
Greg Claytonb72d0f02011-04-12 05:54:46 +00001916 return UINT8_MAX;
1917}
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001918
1919size_t
1920GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
1921 bool &sequence_mutex_unavailable)
1922{
1923 Mutex::Locker locker;
1924 thread_ids.clear();
1925
Greg Claytonc8dd5702012-04-12 19:04:34 +00001926 if (GetSequenceMutex (locker))
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001927 {
1928 sequence_mutex_unavailable = false;
1929 StringExtractorGDBRemote response;
1930
Greg Clayton63afdb02011-06-17 01:22:15 +00001931 for (SendPacketNoLock ("qfThreadInfo", strlen("qfThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001932 response.IsNormalResponse();
Greg Clayton63afdb02011-06-17 01:22:15 +00001933 SendPacketNoLock ("qsThreadInfo", strlen("qsThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()))
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001934 {
1935 char ch = response.GetChar();
1936 if (ch == 'l')
1937 break;
1938 if (ch == 'm')
1939 {
1940 do
1941 {
1942 tid_t tid = response.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID);
1943
1944 if (tid != LLDB_INVALID_THREAD_ID)
1945 {
1946 thread_ids.push_back (tid);
1947 }
1948 ch = response.GetChar(); // Skip the command separator
1949 } while (ch == ','); // Make sure we got a comma separator
1950 }
1951 }
1952 }
1953 else
1954 {
Greg Claytonc8dd5702012-04-12 19:04:34 +00001955 LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
1956 if (log)
1957 log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'");
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001958 sequence_mutex_unavailable = true;
1959 }
1960 return thread_ids.size();
1961}
Greg Clayton516f0842012-04-11 00:24:49 +00001962
1963lldb::addr_t
1964GDBRemoteCommunicationClient::GetShlibInfoAddr()
1965{
1966 if (!IsRunning())
1967 {
1968 StringExtractorGDBRemote response;
1969 if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false))
1970 {
1971 if (response.IsNormalResponse())
1972 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1973 }
1974 }
1975 return LLDB_INVALID_ADDRESS;
1976}
1977