blob: 2a3ed596fb795fac424a3c11c3f24c6ee6906adb [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 Clayton8ca4fa72012-05-24 23:42:14 +0000559 // Reverting this for now as it is causing deadlocks
560 // in programs (<rdar://problem/11529853>). In the future
561 // we should check our thread list and "do the right thing"
562 // for new threads that show up while we stop and run async
563 // packets. Setting the packet to 'c' to continue all threads
564 // is the right thing to do 99.99% of the time because if a
565 // thread was single stepping, and we sent an interrupt, we
566 // will notice above that we didn't stop due to an interrupt
567 // but stopped due to stepping and we would _not_ continue.
568 continue_packet.assign (1, 'c');
Greg Clayton05e4d972012-03-29 01:55:41 +0000569 continue;
570 }
571 }
572 // Stop with signal and thread info
573 state = eStateStopped;
Greg Clayton61d043b2011-03-22 04:00:09 +0000574 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000575 break;
576
577 case 'W':
578 case 'X':
579 // process exited
580 state = eStateExited;
581 break;
582
583 case 'O':
584 // STDOUT
585 {
Greg Clayton628cead2011-05-19 03:54:16 +0000586 got_stdout = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000587 std::string inferior_stdout;
588 inferior_stdout.reserve(response.GetBytesLeft () / 2);
589 char ch;
590 while ((ch = response.GetHexU8()) != '\0')
591 inferior_stdout.append(1, ch);
592 process->AppendSTDOUT (inferior_stdout.c_str(), inferior_stdout.size());
593 }
594 break;
595
596 case 'E':
597 // ERROR
598 state = eStateInvalid;
599 break;
600
601 default:
602 if (log)
603 log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__);
604 state = eStateInvalid;
605 break;
606 }
607 }
608 }
609 else
610 {
611 if (log)
612 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__);
613 state = eStateInvalid;
614 }
615 }
616 if (log)
617 log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state));
618 response.SetFilePos(0);
619 m_private_is_running.SetValue (false, eBroadcastAlways);
620 m_public_is_running.SetValue (false, eBroadcastAlways);
621 return state;
622}
623
624bool
625GDBRemoteCommunicationClient::SendAsyncSignal (int signo)
626{
Greg Clayton05e4d972012-03-29 01:55:41 +0000627 Mutex::Locker async_locker (m_async_mutex);
Greg Clayton61d043b2011-03-22 04:00:09 +0000628 m_async_signal = signo;
629 bool timed_out = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000630 Mutex::Locker locker;
Greg Clayton05e4d972012-03-29 01:55:41 +0000631 if (SendInterrupt (locker, 1, timed_out))
Greg Clayton61d043b2011-03-22 04:00:09 +0000632 return true;
633 m_async_signal = -1;
634 return false;
635}
636
Greg Clayton516f0842012-04-11 00:24:49 +0000637// This function takes a mutex locker as a parameter in case the GetSequenceMutex
Greg Clayton61d043b2011-03-22 04:00:09 +0000638// actually succeeds. If it doesn't succeed in acquiring the sequence mutex
639// (the expected result), then it will send the halt packet. If it does succeed
640// then the caller that requested the interrupt will want to keep the sequence
641// locked down so that no one else can send packets while the caller has control.
642// This function usually gets called when we are running and need to stop the
643// target. It can also be used when we are running and and we need to do something
644// else (like read/write memory), so we need to interrupt the running process
645// (gdb remote protocol requires this), and do what we need to do, then resume.
646
647bool
Greg Clayton05e4d972012-03-29 01:55:41 +0000648GDBRemoteCommunicationClient::SendInterrupt
Greg Clayton61d043b2011-03-22 04:00:09 +0000649(
650 Mutex::Locker& locker,
651 uint32_t seconds_to_wait_for_stop,
Greg Clayton61d043b2011-03-22 04:00:09 +0000652 bool &timed_out
653)
654{
Greg Clayton05e4d972012-03-29 01:55:41 +0000655 m_interrupt_sent = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000656 timed_out = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000657 LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Clayton61d043b2011-03-22 04:00:09 +0000658
659 if (IsRunning())
660 {
661 // Only send an interrupt if our debugserver is running...
Greg Claytonc8dd5702012-04-12 19:04:34 +0000662 if (GetSequenceMutex (locker))
Greg Clayton516f0842012-04-11 00:24:49 +0000663 {
664 if (log)
665 log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt");
666 }
667 else
Greg Clayton61d043b2011-03-22 04:00:09 +0000668 {
669 // Someone has the mutex locked waiting for a response or for the
670 // inferior to stop, so send the interrupt on the down low...
671 char ctrl_c = '\x03';
672 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton61d043b2011-03-22 04:00:09 +0000673 size_t bytes_written = Write (&ctrl_c, 1, status, NULL);
Greg Clayton05e4d972012-03-29 01:55:41 +0000674 if (log)
675 log->PutCString("send packet: \\x03");
Greg Clayton61d043b2011-03-22 04:00:09 +0000676 if (bytes_written > 0)
677 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000678 m_interrupt_sent = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000679 if (seconds_to_wait_for_stop)
680 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000681 TimeValue timeout;
682 if (seconds_to_wait_for_stop)
683 {
684 timeout = TimeValue::Now();
685 timeout.OffsetWithSeconds (seconds_to_wait_for_stop);
686 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000687 if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out))
688 {
689 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000690 log->PutCString ("SendInterrupt () - sent interrupt, private state stopped");
Greg Clayton61d043b2011-03-22 04:00:09 +0000691 return true;
692 }
693 else
694 {
695 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000696 log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume");
Greg Clayton61d043b2011-03-22 04:00:09 +0000697 }
698 }
699 else
700 {
701 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000702 log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop...");
Greg Clayton61d043b2011-03-22 04:00:09 +0000703 return true;
704 }
705 }
706 else
707 {
708 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000709 log->Printf ("SendInterrupt () - failed to write interrupt");
Greg Clayton61d043b2011-03-22 04:00:09 +0000710 }
711 return false;
712 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000713 }
Greg Clayton05e4d972012-03-29 01:55:41 +0000714 else
715 {
716 if (log)
717 log->Printf ("SendInterrupt () - not running");
718 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000719 return true;
720}
721
722lldb::pid_t
723GDBRemoteCommunicationClient::GetCurrentProcessID ()
724{
725 StringExtractorGDBRemote response;
726 if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false))
727 {
728 if (response.GetChar() == 'Q')
729 if (response.GetChar() == 'C')
730 return response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID);
731 }
732 return LLDB_INVALID_PROCESS_ID;
733}
734
735bool
736GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str)
737{
738 error_str.clear();
739 StringExtractorGDBRemote response;
740 if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false))
741 {
742 if (response.IsOKResponse())
743 return true;
744 if (response.GetChar() == 'E')
745 {
746 // A string the describes what failed when launching...
747 error_str = response.GetStringRef().substr(1);
748 }
749 else
750 {
751 error_str.assign ("unknown error occurred launching process");
752 }
753 }
754 else
755 {
756 error_str.assign ("failed to send the qLaunchSuccess packet");
757 }
758 return false;
759}
760
761int
762GDBRemoteCommunicationClient::SendArgumentsPacket (char const *argv[])
763{
764 if (argv && argv[0])
765 {
766 StreamString packet;
767 packet.PutChar('A');
768 const char *arg;
769 for (uint32_t i = 0; (arg = argv[i]) != NULL; ++i)
770 {
771 const int arg_len = strlen(arg);
772 if (i > 0)
773 packet.PutChar(',');
774 packet.Printf("%i,%i,", arg_len * 2, i);
775 packet.PutBytesAsRawHex8 (arg, arg_len);
776 }
777
778 StringExtractorGDBRemote response;
779 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
780 {
781 if (response.IsOKResponse())
782 return 0;
783 uint8_t error = response.GetError();
784 if (error)
785 return error;
786 }
787 }
788 return -1;
789}
790
791int
792GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value)
793{
794 if (name_equal_value && name_equal_value[0])
795 {
796 StreamString packet;
797 packet.Printf("QEnvironment:%s", name_equal_value);
798 StringExtractorGDBRemote response;
799 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
800 {
801 if (response.IsOKResponse())
802 return 0;
803 uint8_t error = response.GetError();
804 if (error)
805 return error;
806 }
807 }
808 return -1;
809}
810
Greg Claytona4582402011-05-08 04:53:50 +0000811int
812GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch)
813{
814 if (arch && arch[0])
815 {
816 StreamString packet;
817 packet.Printf("QLaunchArch:%s", arch);
818 StringExtractorGDBRemote response;
819 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
820 {
821 if (response.IsOKResponse())
822 return 0;
823 uint8_t error = response.GetError();
824 if (error)
825 return error;
826 }
827 }
828 return -1;
829}
830
Greg Clayton61d043b2011-03-22 04:00:09 +0000831bool
Greg Clayton58e26e02011-03-24 04:28:38 +0000832GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major,
833 uint32_t &minor,
834 uint32_t &update)
835{
836 if (GetHostInfo ())
837 {
838 if (m_os_version_major != UINT32_MAX)
839 {
840 major = m_os_version_major;
841 minor = m_os_version_minor;
842 update = m_os_version_update;
843 return true;
844 }
845 }
846 return false;
847}
848
849bool
850GDBRemoteCommunicationClient::GetOSBuildString (std::string &s)
851{
852 if (GetHostInfo ())
853 {
854 if (!m_os_build.empty())
855 {
856 s = m_os_build;
857 return true;
858 }
859 }
860 s.clear();
861 return false;
862}
863
864
865bool
866GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s)
867{
868 if (GetHostInfo ())
869 {
870 if (!m_os_kernel.empty())
871 {
872 s = m_os_kernel;
873 return true;
874 }
875 }
876 s.clear();
877 return false;
878}
879
880bool
881GDBRemoteCommunicationClient::GetHostname (std::string &s)
882{
883 if (GetHostInfo ())
884 {
885 if (!m_hostname.empty())
886 {
887 s = m_hostname;
888 return true;
889 }
890 }
891 s.clear();
892 return false;
893}
894
895ArchSpec
896GDBRemoteCommunicationClient::GetSystemArchitecture ()
897{
898 if (GetHostInfo ())
899 return m_host_arch;
900 return ArchSpec();
901}
902
903
904bool
Greg Clayton06d7cc82011-04-04 18:18:57 +0000905GDBRemoteCommunicationClient::GetHostInfo (bool force)
Greg Clayton61d043b2011-03-22 04:00:09 +0000906{
Greg Clayton06d7cc82011-04-04 18:18:57 +0000907 if (force || m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton61d043b2011-03-22 04:00:09 +0000908 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000909 m_qHostInfo_is_valid = eLazyBoolNo;
Greg Clayton61d043b2011-03-22 04:00:09 +0000910 StringExtractorGDBRemote response;
911 if (SendPacketAndWaitForResponse ("qHostInfo", response, false))
912 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +0000913 if (response.IsNormalResponse())
Greg Claytoncb8977d2011-03-23 00:09:55 +0000914 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000915 std::string name;
916 std::string value;
917 uint32_t cpu = LLDB_INVALID_CPUTYPE;
918 uint32_t sub = 0;
919 std::string arch_name;
920 std::string os_name;
921 std::string vendor_name;
922 std::string triple;
923 uint32_t pointer_byte_size = 0;
924 StringExtractor extractor;
925 ByteOrder byte_order = eByteOrderInvalid;
926 uint32_t num_keys_decoded = 0;
927 while (response.GetNameColonValue(name, value))
Greg Claytoncb8977d2011-03-23 00:09:55 +0000928 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000929 if (name.compare("cputype") == 0)
Greg Clayton58e26e02011-03-24 04:28:38 +0000930 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000931 // exception type in big endian hex
932 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0);
933 if (cpu != LLDB_INVALID_CPUTYPE)
934 ++num_keys_decoded;
935 }
936 else if (name.compare("cpusubtype") == 0)
937 {
938 // exception count in big endian hex
939 sub = Args::StringToUInt32 (value.c_str(), 0, 0);
940 if (sub != 0)
941 ++num_keys_decoded;
942 }
943 else if (name.compare("arch") == 0)
944 {
945 arch_name.swap (value);
946 ++num_keys_decoded;
947 }
948 else if (name.compare("triple") == 0)
949 {
950 // The triple comes as ASCII hex bytes since it contains '-' chars
951 extractor.GetStringRef().swap(value);
952 extractor.SetFilePos(0);
953 extractor.GetHexByteString (triple);
954 ++num_keys_decoded;
955 }
956 else if (name.compare("os_build") == 0)
957 {
958 extractor.GetStringRef().swap(value);
959 extractor.SetFilePos(0);
960 extractor.GetHexByteString (m_os_build);
961 ++num_keys_decoded;
962 }
963 else if (name.compare("hostname") == 0)
964 {
965 extractor.GetStringRef().swap(value);
966 extractor.SetFilePos(0);
967 extractor.GetHexByteString (m_hostname);
968 ++num_keys_decoded;
969 }
970 else if (name.compare("os_kernel") == 0)
971 {
972 extractor.GetStringRef().swap(value);
973 extractor.SetFilePos(0);
974 extractor.GetHexByteString (m_os_kernel);
975 ++num_keys_decoded;
976 }
977 else if (name.compare("ostype") == 0)
978 {
979 os_name.swap (value);
980 ++num_keys_decoded;
981 }
982 else if (name.compare("vendor") == 0)
983 {
984 vendor_name.swap(value);
985 ++num_keys_decoded;
986 }
987 else if (name.compare("endian") == 0)
988 {
989 ++num_keys_decoded;
990 if (value.compare("little") == 0)
991 byte_order = eByteOrderLittle;
992 else if (value.compare("big") == 0)
993 byte_order = eByteOrderBig;
994 else if (value.compare("pdp") == 0)
995 byte_order = eByteOrderPDP;
996 else
997 --num_keys_decoded;
998 }
999 else if (name.compare("ptrsize") == 0)
1000 {
1001 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 0);
1002 if (pointer_byte_size != 0)
1003 ++num_keys_decoded;
1004 }
1005 else if (name.compare("os_version") == 0)
1006 {
1007 Args::StringToVersion (value.c_str(),
1008 m_os_version_major,
1009 m_os_version_minor,
1010 m_os_version_update);
1011 if (m_os_version_major != UINT32_MAX)
1012 ++num_keys_decoded;
1013 }
1014 }
1015
1016 if (num_keys_decoded > 0)
1017 m_qHostInfo_is_valid = eLazyBoolYes;
1018
1019 if (triple.empty())
1020 {
1021 if (arch_name.empty())
1022 {
1023 if (cpu != LLDB_INVALID_CPUTYPE)
1024 {
1025 m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1026 if (pointer_byte_size)
1027 {
1028 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1029 }
1030 if (byte_order != eByteOrderInvalid)
1031 {
1032 assert (byte_order == m_host_arch.GetByteOrder());
1033 }
Greg Claytonb170aee2012-05-08 01:45:38 +00001034
1035 if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0)
1036 {
1037 switch (m_host_arch.GetMachine())
1038 {
1039 case llvm::Triple::arm:
1040 case llvm::Triple::thumb:
1041 os_name = "ios";
1042 break;
1043 default:
1044 os_name = "macosx";
1045 break;
1046 }
1047 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001048 if (!vendor_name.empty())
1049 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1050 if (!os_name.empty())
Greg Clayton89798cc2011-09-15 00:21:03 +00001051 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001052
1053 }
1054 }
1055 else
1056 {
1057 std::string triple;
1058 triple += arch_name;
Greg Claytonb170aee2012-05-08 01:45:38 +00001059 if (!vendor_name.empty() || !os_name.empty())
1060 {
1061 triple += '-';
1062 if (vendor_name.empty())
1063 triple += "unknown";
1064 else
1065 triple += vendor_name;
1066 triple += '-';
1067 if (os_name.empty())
1068 triple += "unknown";
1069 else
1070 triple += os_name;
1071 }
1072 m_host_arch.SetTriple (triple.c_str());
1073
1074 llvm::Triple &host_triple = m_host_arch.GetTriple();
1075 if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin)
1076 {
1077 switch (m_host_arch.GetMachine())
1078 {
1079 case llvm::Triple::arm:
1080 case llvm::Triple::thumb:
1081 host_triple.setOS(llvm::Triple::IOS);
1082 break;
1083 default:
1084 host_triple.setOS(llvm::Triple::MacOSX);
1085 break;
1086 }
1087 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001088 if (pointer_byte_size)
1089 {
1090 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1091 }
1092 if (byte_order != eByteOrderInvalid)
1093 {
1094 assert (byte_order == m_host_arch.GetByteOrder());
1095 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001096
Greg Clayton58e26e02011-03-24 04:28:38 +00001097 }
1098 }
1099 else
1100 {
Greg Claytonb170aee2012-05-08 01:45:38 +00001101 m_host_arch.SetTriple (triple.c_str());
Greg Claytoncb8977d2011-03-23 00:09:55 +00001102 if (pointer_byte_size)
1103 {
1104 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1105 }
1106 if (byte_order != eByteOrderInvalid)
1107 {
1108 assert (byte_order == m_host_arch.GetByteOrder());
1109 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001110 }
Greg Claytoncb8977d2011-03-23 00:09:55 +00001111 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001112 }
1113 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001114 return m_qHostInfo_is_valid == eLazyBoolYes;
Greg Clayton61d043b2011-03-22 04:00:09 +00001115}
1116
1117int
1118GDBRemoteCommunicationClient::SendAttach
1119(
1120 lldb::pid_t pid,
1121 StringExtractorGDBRemote& response
1122)
1123{
1124 if (pid != LLDB_INVALID_PROCESS_ID)
1125 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001126 char packet[64];
Greg Claytond9919d32011-12-01 23:28:38 +00001127 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%llx", pid);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001128 assert (packet_len < sizeof(packet));
1129 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton61d043b2011-03-22 04:00:09 +00001130 {
1131 if (response.IsErrorResponse())
1132 return response.GetError();
1133 return 0;
1134 }
1135 }
1136 return -1;
1137}
1138
1139const lldb_private::ArchSpec &
1140GDBRemoteCommunicationClient::GetHostArchitecture ()
1141{
Greg Clayton24bc5d92011-03-30 18:16:51 +00001142 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton61d043b2011-03-22 04:00:09 +00001143 GetHostInfo ();
Greg Claytoncb8977d2011-03-23 00:09:55 +00001144 return m_host_arch;
Greg Clayton61d043b2011-03-22 04:00:09 +00001145}
1146
1147addr_t
1148GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions)
1149{
Greg Clayton2f085c62011-05-15 01:25:55 +00001150 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton61d043b2011-03-22 04:00:09 +00001151 {
Greg Clayton2f085c62011-05-15 01:25:55 +00001152 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton989816b2011-05-14 01:50:35 +00001153 char packet[64];
1154 const int packet_len = ::snprintf (packet, sizeof(packet), "_M%zx,%s%s%s", size,
1155 permissions & lldb::ePermissionsReadable ? "r" : "",
1156 permissions & lldb::ePermissionsWritable ? "w" : "",
1157 permissions & lldb::ePermissionsExecutable ? "x" : "");
1158 assert (packet_len < sizeof(packet));
1159 StringExtractorGDBRemote response;
1160 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1161 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001162 if (!response.IsErrorResponse())
Greg Clayton989816b2011-05-14 01:50:35 +00001163 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1164 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001165 else
1166 {
1167 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1168 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001169 }
1170 return LLDB_INVALID_ADDRESS;
1171}
1172
1173bool
1174GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr)
1175{
Greg Clayton2f085c62011-05-15 01:25:55 +00001176 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton61d043b2011-03-22 04:00:09 +00001177 {
Greg Clayton2f085c62011-05-15 01:25:55 +00001178 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton989816b2011-05-14 01:50:35 +00001179 char packet[64];
1180 const int packet_len = ::snprintf(packet, sizeof(packet), "_m%llx", (uint64_t)addr);
1181 assert (packet_len < sizeof(packet));
1182 StringExtractorGDBRemote response;
1183 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1184 {
1185 if (response.IsOKResponse())
1186 return true;
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001187 }
1188 else
1189 {
1190 m_supports_alloc_dealloc_memory = eLazyBoolNo;
Greg Clayton989816b2011-05-14 01:50:35 +00001191 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001192 }
1193 return false;
1194}
1195
Greg Clayton516f0842012-04-11 00:24:49 +00001196bool
1197GDBRemoteCommunicationClient::Detach ()
1198{
1199 return SendPacket ("D", 1) > 0;
1200}
1201
Greg Claytona9385532011-11-18 07:03:08 +00001202Error
1203GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr,
1204 lldb_private::MemoryRegionInfo &region_info)
1205{
1206 Error error;
1207 region_info.Clear();
1208
1209 if (m_supports_memory_region_info != eLazyBoolNo)
1210 {
1211 m_supports_memory_region_info = eLazyBoolYes;
1212 char packet[64];
1213 const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%llx", (uint64_t)addr);
1214 assert (packet_len < sizeof(packet));
1215 StringExtractorGDBRemote response;
1216 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1217 {
1218 std::string name;
1219 std::string value;
1220 addr_t addr_value;
1221 bool success = true;
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001222 bool saw_permissions = false;
Greg Claytona9385532011-11-18 07:03:08 +00001223 while (success && response.GetNameColonValue(name, value))
1224 {
1225 if (name.compare ("start") == 0)
1226 {
1227 addr_value = Args::StringToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success);
1228 if (success)
1229 region_info.GetRange().SetRangeBase(addr_value);
1230 }
1231 else if (name.compare ("size") == 0)
1232 {
1233 addr_value = Args::StringToUInt64(value.c_str(), 0, 16, &success);
1234 if (success)
1235 region_info.GetRange().SetByteSize (addr_value);
1236 }
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001237 else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid())
Greg Claytona9385532011-11-18 07:03:08 +00001238 {
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001239 saw_permissions = true;
1240 if (region_info.GetRange().Contains (addr))
1241 {
1242 if (value.find('r') != std::string::npos)
1243 region_info.SetReadable (MemoryRegionInfo::eYes);
1244 else
1245 region_info.SetReadable (MemoryRegionInfo::eNo);
1246
1247 if (value.find('w') != std::string::npos)
1248 region_info.SetWritable (MemoryRegionInfo::eYes);
1249 else
1250 region_info.SetWritable (MemoryRegionInfo::eNo);
1251
1252 if (value.find('x') != std::string::npos)
1253 region_info.SetExecutable (MemoryRegionInfo::eYes);
1254 else
1255 region_info.SetExecutable (MemoryRegionInfo::eNo);
1256 }
1257 else
1258 {
1259 // The reported region does not contain this address -- we're looking at an unmapped page
1260 region_info.SetReadable (MemoryRegionInfo::eNo);
1261 region_info.SetWritable (MemoryRegionInfo::eNo);
1262 region_info.SetExecutable (MemoryRegionInfo::eNo);
1263 }
Greg Claytona9385532011-11-18 07:03:08 +00001264 }
1265 else if (name.compare ("error") == 0)
1266 {
1267 StringExtractorGDBRemote name_extractor;
1268 // Swap "value" over into "name_extractor"
1269 name_extractor.GetStringRef().swap(value);
1270 // Now convert the HEX bytes into a string value
1271 name_extractor.GetHexByteString (value);
1272 error.SetErrorString(value.c_str());
1273 }
1274 }
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001275
1276 // We got a valid address range back but no permissions -- which means this is an unmapped page
1277 if (region_info.GetRange().IsValid() && saw_permissions == false)
1278 {
1279 region_info.SetReadable (MemoryRegionInfo::eNo);
1280 region_info.SetWritable (MemoryRegionInfo::eNo);
1281 region_info.SetExecutable (MemoryRegionInfo::eNo);
1282 }
Greg Claytona9385532011-11-18 07:03:08 +00001283 }
1284 else
1285 {
1286 m_supports_memory_region_info = eLazyBoolNo;
1287 }
1288 }
1289
1290 if (m_supports_memory_region_info == eLazyBoolNo)
1291 {
1292 error.SetErrorString("qMemoryRegionInfo is not supported");
1293 }
1294 if (error.Fail())
1295 region_info.Clear();
1296 return error;
1297
1298}
1299
Johnny Chen7cbdcfb2012-05-23 21:09:52 +00001300Error
1301GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num)
1302{
1303 Error error;
1304
1305 if (m_supports_watchpoint_support_info == eLazyBoolYes)
1306 {
1307 num = m_num_supported_hardware_watchpoints;
1308 return error;
1309 }
1310
1311 // Set num to 0 first.
1312 num = 0;
1313 if (m_supports_watchpoint_support_info != eLazyBoolNo)
1314 {
1315 char packet[64];
1316 const int packet_len = ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:");
1317 assert (packet_len < sizeof(packet));
1318 StringExtractorGDBRemote response;
1319 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1320 {
1321 m_supports_watchpoint_support_info = eLazyBoolYes;
1322 std::string name;
1323 std::string value;
1324 while (response.GetNameColonValue(name, value))
1325 {
1326 if (name.compare ("num") == 0)
1327 {
1328 num = Args::StringToUInt32(value.c_str(), 0, 0);
1329 m_num_supported_hardware_watchpoints = num;
1330 }
1331 }
1332 }
1333 else
1334 {
1335 m_supports_watchpoint_support_info = eLazyBoolNo;
1336 }
1337 }
1338
1339 if (m_supports_watchpoint_support_info == eLazyBoolNo)
1340 {
1341 error.SetErrorString("qWatchpointSupportInfo is not supported");
1342 }
1343 return error;
1344
1345}
Greg Claytona9385532011-11-18 07:03:08 +00001346
Greg Clayton61d043b2011-03-22 04:00:09 +00001347int
1348GDBRemoteCommunicationClient::SetSTDIN (char const *path)
1349{
1350 if (path && path[0])
1351 {
1352 StreamString packet;
1353 packet.PutCString("QSetSTDIN:");
1354 packet.PutBytesAsRawHex8(path, strlen(path));
1355
1356 StringExtractorGDBRemote response;
1357 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1358 {
1359 if (response.IsOKResponse())
1360 return 0;
1361 uint8_t error = response.GetError();
1362 if (error)
1363 return error;
1364 }
1365 }
1366 return -1;
1367}
1368
1369int
1370GDBRemoteCommunicationClient::SetSTDOUT (char const *path)
1371{
1372 if (path && path[0])
1373 {
1374 StreamString packet;
1375 packet.PutCString("QSetSTDOUT:");
1376 packet.PutBytesAsRawHex8(path, strlen(path));
1377
1378 StringExtractorGDBRemote response;
1379 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1380 {
1381 if (response.IsOKResponse())
1382 return 0;
1383 uint8_t error = response.GetError();
1384 if (error)
1385 return error;
1386 }
1387 }
1388 return -1;
1389}
1390
1391int
1392GDBRemoteCommunicationClient::SetSTDERR (char const *path)
1393{
1394 if (path && path[0])
1395 {
1396 StreamString packet;
1397 packet.PutCString("QSetSTDERR:");
1398 packet.PutBytesAsRawHex8(path, strlen(path));
1399
1400 StringExtractorGDBRemote response;
1401 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1402 {
1403 if (response.IsOKResponse())
1404 return 0;
1405 uint8_t error = response.GetError();
1406 if (error)
1407 return error;
1408 }
1409 }
1410 return -1;
1411}
1412
1413int
1414GDBRemoteCommunicationClient::SetWorkingDir (char const *path)
1415{
1416 if (path && path[0])
1417 {
1418 StreamString packet;
1419 packet.PutCString("QSetWorkingDir:");
1420 packet.PutBytesAsRawHex8(path, strlen(path));
1421
1422 StringExtractorGDBRemote response;
1423 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1424 {
1425 if (response.IsOKResponse())
1426 return 0;
1427 uint8_t error = response.GetError();
1428 if (error)
1429 return error;
1430 }
1431 }
1432 return -1;
1433}
1434
1435int
1436GDBRemoteCommunicationClient::SetDisableASLR (bool enable)
1437{
Greg Clayton24bc5d92011-03-30 18:16:51 +00001438 char packet[32];
1439 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0);
1440 assert (packet_len < sizeof(packet));
Greg Clayton61d043b2011-03-22 04:00:09 +00001441 StringExtractorGDBRemote response;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001442 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton61d043b2011-03-22 04:00:09 +00001443 {
1444 if (response.IsOKResponse())
1445 return 0;
1446 uint8_t error = response.GetError();
1447 if (error)
1448 return error;
1449 }
1450 return -1;
1451}
Greg Clayton24bc5d92011-03-30 18:16:51 +00001452
1453bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001454GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001455{
1456 if (response.IsNormalResponse())
1457 {
1458 std::string name;
1459 std::string value;
1460 StringExtractor extractor;
1461
1462 while (response.GetNameColonValue(name, value))
1463 {
1464 if (name.compare("pid") == 0)
1465 {
1466 process_info.SetProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1467 }
1468 else if (name.compare("ppid") == 0)
1469 {
1470 process_info.SetParentProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1471 }
1472 else if (name.compare("uid") == 0)
1473 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001474 process_info.SetUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001475 }
1476 else if (name.compare("euid") == 0)
1477 {
1478 process_info.SetEffectiveUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1479 }
1480 else if (name.compare("gid") == 0)
1481 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001482 process_info.SetGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001483 }
1484 else if (name.compare("egid") == 0)
1485 {
1486 process_info.SetEffectiveGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1487 }
1488 else if (name.compare("triple") == 0)
1489 {
1490 // The triple comes as ASCII hex bytes since it contains '-' chars
1491 extractor.GetStringRef().swap(value);
1492 extractor.SetFilePos(0);
1493 extractor.GetHexByteString (value);
Greg Claytonb170aee2012-05-08 01:45:38 +00001494 process_info.GetArchitecture ().SetTriple (value.c_str());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001495 }
1496 else if (name.compare("name") == 0)
1497 {
1498 StringExtractor extractor;
Filipe Cabecinhase61ec7f2012-05-07 09:30:51 +00001499 // The process name from ASCII hex bytes since we can't
Greg Clayton24bc5d92011-03-30 18:16:51 +00001500 // control the characters in a process name
1501 extractor.GetStringRef().swap(value);
1502 extractor.SetFilePos(0);
1503 extractor.GetHexByteString (value);
Greg Clayton527154d2011-11-15 03:53:30 +00001504 process_info.GetExecutableFile().SetFile (value.c_str(), false);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001505 }
1506 }
1507
1508 if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
1509 return true;
1510 }
1511 return false;
1512}
1513
1514bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001515GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001516{
1517 process_info.Clear();
1518
1519 if (m_supports_qProcessInfoPID)
1520 {
1521 char packet[32];
Greg Claytond9919d32011-12-01 23:28:38 +00001522 const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%llu", pid);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001523 assert (packet_len < sizeof(packet));
1524 StringExtractorGDBRemote response;
1525 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1526 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001527 return DecodeProcessInfoResponse (response, process_info);
1528 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001529 else
1530 {
1531 m_supports_qProcessInfoPID = false;
1532 return false;
1533 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001534 }
1535 return false;
1536}
1537
1538uint32_t
Greg Claytonb72d0f02011-04-12 05:54:46 +00001539GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1540 ProcessInstanceInfoList &process_infos)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001541{
1542 process_infos.Clear();
1543
1544 if (m_supports_qfProcessInfo)
1545 {
1546 StreamString packet;
1547 packet.PutCString ("qfProcessInfo");
1548 if (!match_info.MatchAllProcesses())
1549 {
1550 packet.PutChar (':');
1551 const char *name = match_info.GetProcessInfo().GetName();
1552 bool has_name_match = false;
1553 if (name && name[0])
1554 {
1555 has_name_match = true;
1556 NameMatchType name_match_type = match_info.GetNameMatchType();
1557 switch (name_match_type)
1558 {
1559 case eNameMatchIgnore:
1560 has_name_match = false;
1561 break;
1562
1563 case eNameMatchEquals:
1564 packet.PutCString ("name_match:equals;");
1565 break;
1566
1567 case eNameMatchContains:
1568 packet.PutCString ("name_match:contains;");
1569 break;
1570
1571 case eNameMatchStartsWith:
1572 packet.PutCString ("name_match:starts_with;");
1573 break;
1574
1575 case eNameMatchEndsWith:
1576 packet.PutCString ("name_match:ends_with;");
1577 break;
1578
1579 case eNameMatchRegularExpression:
1580 packet.PutCString ("name_match:regex;");
1581 break;
1582 }
1583 if (has_name_match)
1584 {
1585 packet.PutCString ("name:");
1586 packet.PutBytesAsRawHex8(name, ::strlen(name));
1587 packet.PutChar (';');
1588 }
1589 }
1590
1591 if (match_info.GetProcessInfo().ProcessIDIsValid())
Greg Claytond9919d32011-12-01 23:28:38 +00001592 packet.Printf("pid:%llu;",match_info.GetProcessInfo().GetProcessID());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001593 if (match_info.GetProcessInfo().ParentProcessIDIsValid())
Greg Claytond9919d32011-12-01 23:28:38 +00001594 packet.Printf("parent_pid:%llu;",match_info.GetProcessInfo().GetParentProcessID());
Greg Claytonb72d0f02011-04-12 05:54:46 +00001595 if (match_info.GetProcessInfo().UserIDIsValid())
1596 packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID());
1597 if (match_info.GetProcessInfo().GroupIDIsValid())
1598 packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001599 if (match_info.GetProcessInfo().EffectiveUserIDIsValid())
1600 packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID());
1601 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1602 packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID());
1603 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1604 packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0);
1605 if (match_info.GetProcessInfo().GetArchitecture().IsValid())
1606 {
1607 const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture();
1608 const llvm::Triple &triple = match_arch.GetTriple();
1609 packet.PutCString("triple:");
1610 packet.PutCStringAsRawHex8(triple.getTriple().c_str());
1611 packet.PutChar (';');
1612 }
1613 }
1614 StringExtractorGDBRemote response;
1615 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1616 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001617 do
1618 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001619 ProcessInstanceInfo process_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001620 if (!DecodeProcessInfoResponse (response, process_info))
1621 break;
1622 process_infos.Append(process_info);
1623 response.GetStringRef().clear();
1624 response.SetFilePos(0);
1625 } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false));
1626 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001627 else
1628 {
1629 m_supports_qfProcessInfo = false;
1630 return 0;
1631 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001632 }
1633 return process_infos.GetSize();
1634
1635}
1636
1637bool
1638GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name)
1639{
1640 if (m_supports_qUserName)
1641 {
1642 char packet[32];
1643 const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid);
1644 assert (packet_len < sizeof(packet));
1645 StringExtractorGDBRemote response;
1646 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1647 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001648 if (response.IsNormalResponse())
1649 {
1650 // Make sure we parsed the right number of characters. The response is
1651 // the hex encoded user name and should make up the entire packet.
1652 // If there are any non-hex ASCII bytes, the length won't match below..
1653 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
1654 return true;
1655 }
1656 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001657 else
1658 {
1659 m_supports_qUserName = false;
1660 return false;
1661 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001662 }
1663 return false;
1664
1665}
1666
1667bool
1668GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name)
1669{
1670 if (m_supports_qGroupName)
1671 {
1672 char packet[32];
1673 const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid);
1674 assert (packet_len < sizeof(packet));
1675 StringExtractorGDBRemote response;
1676 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1677 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001678 if (response.IsNormalResponse())
1679 {
1680 // Make sure we parsed the right number of characters. The response is
1681 // the hex encoded group name and should make up the entire packet.
1682 // If there are any non-hex ASCII bytes, the length won't match below..
1683 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
1684 return true;
1685 }
1686 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001687 else
1688 {
1689 m_supports_qGroupName = false;
1690 return false;
1691 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001692 }
1693 return false;
Greg Clayton06d7cc82011-04-04 18:18:57 +00001694}
Greg Clayton24bc5d92011-03-30 18:16:51 +00001695
Greg Clayton06d7cc82011-04-04 18:18:57 +00001696void
1697GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets)
1698{
1699 uint32_t i;
1700 TimeValue start_time, end_time;
1701 uint64_t total_time_nsec;
1702 float packets_per_second;
1703 if (SendSpeedTestPacket (0, 0))
1704 {
1705 for (uint32_t send_size = 0; send_size <= 1024; send_size *= 2)
1706 {
1707 for (uint32_t recv_size = 0; recv_size <= 1024; recv_size *= 2)
1708 {
1709 start_time = TimeValue::Now();
1710 for (i=0; i<num_packets; ++i)
1711 {
1712 SendSpeedTestPacket (send_size, recv_size);
1713 }
1714 end_time = TimeValue::Now();
1715 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001716 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Greg Clayton153ccd72011-08-10 02:10:13 +00001717 printf ("%u qSpeedTest(send=%-5u, recv=%-5u) in %llu.%9.9llu sec for %f packets/sec.\n",
Greg Clayton06d7cc82011-04-04 18:18:57 +00001718 num_packets,
1719 send_size,
1720 recv_size,
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001721 total_time_nsec / TimeValue::NanoSecPerSec,
1722 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton06d7cc82011-04-04 18:18:57 +00001723 packets_per_second);
1724 if (recv_size == 0)
1725 recv_size = 32;
1726 }
1727 if (send_size == 0)
1728 send_size = 32;
1729 }
1730 }
1731 else
1732 {
1733 start_time = TimeValue::Now();
1734 for (i=0; i<num_packets; ++i)
1735 {
1736 GetCurrentProcessID ();
1737 }
1738 end_time = TimeValue::Now();
1739 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001740 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Greg Clayton153ccd72011-08-10 02:10:13 +00001741 printf ("%u 'qC' packets packets in 0x%llu%9.9llu sec for %f packets/sec.\n",
Greg Clayton06d7cc82011-04-04 18:18:57 +00001742 num_packets,
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001743 total_time_nsec / TimeValue::NanoSecPerSec,
1744 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton06d7cc82011-04-04 18:18:57 +00001745 packets_per_second);
1746 }
1747}
1748
1749bool
1750GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size)
1751{
1752 StreamString packet;
1753 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size);
1754 uint32_t bytes_left = send_size;
1755 while (bytes_left > 0)
1756 {
1757 if (bytes_left >= 26)
1758 {
1759 packet.PutCString("abcdefghijklmnopqrstuvwxyz");
1760 bytes_left -= 26;
1761 }
1762 else
1763 {
1764 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz");
1765 bytes_left = 0;
1766 }
1767 }
1768
1769 StringExtractorGDBRemote response;
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001770 return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) > 0;
Greg Clayton06d7cc82011-04-04 18:18:57 +00001771 return false;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001772}
Greg Claytonb72d0f02011-04-12 05:54:46 +00001773
1774uint16_t
1775GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort ()
1776{
1777 StringExtractorGDBRemote response;
1778 if (SendPacketAndWaitForResponse("qLaunchGDBServer", strlen("qLaunchGDBServer"), response, false))
1779 {
1780 std::string name;
1781 std::string value;
1782 uint16_t port = 0;
1783 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
1784 while (response.GetNameColonValue(name, value))
1785 {
1786 if (name.size() == 4 && name.compare("port") == 0)
1787 port = Args::StringToUInt32(value.c_str(), 0, 0);
1788 if (name.size() == 3 && name.compare("pid") == 0)
1789 pid = Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0);
1790 }
1791 return port;
1792 }
1793 return 0;
1794}
1795
1796bool
1797GDBRemoteCommunicationClient::SetCurrentThread (int tid)
1798{
1799 if (m_curr_tid == tid)
1800 return true;
1801
1802 char packet[32];
1803 int packet_len;
1804 if (tid <= 0)
1805 packet_len = ::snprintf (packet, sizeof(packet), "Hg%i", tid);
1806 else
1807 packet_len = ::snprintf (packet, sizeof(packet), "Hg%x", tid);
1808 assert (packet_len + 1 < sizeof(packet));
1809 StringExtractorGDBRemote response;
1810 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
1811 {
1812 if (response.IsOKResponse())
1813 {
1814 m_curr_tid = tid;
1815 return true;
1816 }
1817 }
1818 return false;
1819}
1820
1821bool
1822GDBRemoteCommunicationClient::SetCurrentThreadForRun (int tid)
1823{
1824 if (m_curr_tid_run == tid)
1825 return true;
1826
1827 char packet[32];
1828 int packet_len;
1829 if (tid <= 0)
1830 packet_len = ::snprintf (packet, sizeof(packet), "Hc%i", tid);
1831 else
1832 packet_len = ::snprintf (packet, sizeof(packet), "Hc%x", tid);
1833
1834 assert (packet_len + 1 < sizeof(packet));
1835 StringExtractorGDBRemote response;
1836 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
1837 {
1838 if (response.IsOKResponse())
1839 {
1840 m_curr_tid_run = tid;
1841 return true;
1842 }
1843 }
1844 return false;
1845}
1846
1847bool
1848GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response)
1849{
1850 if (SendPacketAndWaitForResponse("?", 1, response, false))
1851 return response.IsNormalResponse();
1852 return false;
1853}
1854
1855bool
1856GDBRemoteCommunicationClient::GetThreadStopInfo (uint32_t tid, StringExtractorGDBRemote &response)
1857{
1858 if (m_supports_qThreadStopInfo)
1859 {
1860 char packet[256];
1861 int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%x", tid);
1862 assert (packet_len < sizeof(packet));
1863 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
1864 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001865 if (response.IsNormalResponse())
Greg Claytonb72d0f02011-04-12 05:54:46 +00001866 return true;
1867 else
1868 return false;
1869 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001870 else
1871 {
1872 m_supports_qThreadStopInfo = false;
1873 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001874 }
Greg Clayton139da722011-05-20 03:15:54 +00001875// if (SetCurrentThread (tid))
1876// return GetStopReply (response);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001877 return false;
1878}
1879
1880
1881uint8_t
1882GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length)
1883{
1884 switch (type)
1885 {
1886 case eBreakpointSoftware: if (!m_supports_z0) return UINT8_MAX; break;
1887 case eBreakpointHardware: if (!m_supports_z1) return UINT8_MAX; break;
1888 case eWatchpointWrite: if (!m_supports_z2) return UINT8_MAX; break;
1889 case eWatchpointRead: if (!m_supports_z3) return UINT8_MAX; break;
1890 case eWatchpointReadWrite: if (!m_supports_z4) return UINT8_MAX; break;
1891 default: return UINT8_MAX;
1892 }
1893
1894 char packet[64];
1895 const int packet_len = ::snprintf (packet,
1896 sizeof(packet),
1897 "%c%i,%llx,%x",
1898 insert ? 'Z' : 'z',
1899 type,
1900 addr,
1901 length);
1902
1903 assert (packet_len + 1 < sizeof(packet));
1904 StringExtractorGDBRemote response;
1905 if (SendPacketAndWaitForResponse(packet, packet_len, response, true))
1906 {
1907 if (response.IsOKResponse())
1908 return 0;
Greg Claytonb72d0f02011-04-12 05:54:46 +00001909 else if (response.IsErrorResponse())
1910 return response.GetError();
1911 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001912 else
1913 {
1914 switch (type)
1915 {
1916 case eBreakpointSoftware: m_supports_z0 = false; break;
1917 case eBreakpointHardware: m_supports_z1 = false; break;
1918 case eWatchpointWrite: m_supports_z2 = false; break;
1919 case eWatchpointRead: m_supports_z3 = false; break;
1920 case eWatchpointReadWrite: m_supports_z4 = false; break;
1921 default: break;
1922 }
1923 }
1924
Greg Claytonb72d0f02011-04-12 05:54:46 +00001925 return UINT8_MAX;
1926}
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001927
1928size_t
1929GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
1930 bool &sequence_mutex_unavailable)
1931{
1932 Mutex::Locker locker;
1933 thread_ids.clear();
1934
Greg Claytonc8dd5702012-04-12 19:04:34 +00001935 if (GetSequenceMutex (locker))
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001936 {
1937 sequence_mutex_unavailable = false;
1938 StringExtractorGDBRemote response;
1939
Greg Clayton63afdb02011-06-17 01:22:15 +00001940 for (SendPacketNoLock ("qfThreadInfo", strlen("qfThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001941 response.IsNormalResponse();
Greg Clayton63afdb02011-06-17 01:22:15 +00001942 SendPacketNoLock ("qsThreadInfo", strlen("qsThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()))
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001943 {
1944 char ch = response.GetChar();
1945 if (ch == 'l')
1946 break;
1947 if (ch == 'm')
1948 {
1949 do
1950 {
1951 tid_t tid = response.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID);
1952
1953 if (tid != LLDB_INVALID_THREAD_ID)
1954 {
1955 thread_ids.push_back (tid);
1956 }
1957 ch = response.GetChar(); // Skip the command separator
1958 } while (ch == ','); // Make sure we got a comma separator
1959 }
1960 }
1961 }
1962 else
1963 {
Greg Claytonc8dd5702012-04-12 19:04:34 +00001964 LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
1965 if (log)
1966 log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'");
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001967 sequence_mutex_unavailable = true;
1968 }
1969 return thread_ids.size();
1970}
Greg Clayton516f0842012-04-11 00:24:49 +00001971
1972lldb::addr_t
1973GDBRemoteCommunicationClient::GetShlibInfoAddr()
1974{
1975 if (!IsRunning())
1976 {
1977 StringExtractorGDBRemote response;
1978 if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false))
1979 {
1980 if (response.IsNormalResponse())
1981 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1982 }
1983 }
1984 return LLDB_INVALID_ADDRESS;
1985}
1986