blob: 16a339c86c22d82ee9feb8ccb5d1c111e5944d68 [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),
Greg Clayton24bc5d92011-03-30 18:16:51 +000051 m_supports_qProcessInfoPID (true),
52 m_supports_qfProcessInfo (true),
53 m_supports_qUserName (true),
54 m_supports_qGroupName (true),
Greg Claytonb72d0f02011-04-12 05:54:46 +000055 m_supports_qThreadStopInfo (true),
56 m_supports_z0 (true),
57 m_supports_z1 (true),
58 m_supports_z2 (true),
59 m_supports_z3 (true),
60 m_supports_z4 (true),
61 m_curr_tid (LLDB_INVALID_THREAD_ID),
62 m_curr_tid_run (LLDB_INVALID_THREAD_ID),
Greg Clayton61d043b2011-03-22 04:00:09 +000063 m_async_mutex (Mutex::eMutexTypeRecursive),
64 m_async_packet_predicate (false),
65 m_async_packet (),
66 m_async_response (),
67 m_async_signal (-1),
Greg Clayton58e26e02011-03-24 04:28:38 +000068 m_host_arch(),
69 m_os_version_major (UINT32_MAX),
70 m_os_version_minor (UINT32_MAX),
71 m_os_version_update (UINT32_MAX)
Greg Clayton61d043b2011-03-22 04:00:09 +000072{
Greg Clayton61d043b2011-03-22 04:00:09 +000073}
74
75//----------------------------------------------------------------------
76// Destructor
77//----------------------------------------------------------------------
78GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient()
79{
Greg Clayton61d043b2011-03-22 04:00:09 +000080 if (IsConnected())
Greg Clayton61d043b2011-03-22 04:00:09 +000081 Disconnect();
Greg Clayton61d043b2011-03-22 04:00:09 +000082}
83
84bool
Greg Clayton58e26e02011-03-24 04:28:38 +000085GDBRemoteCommunicationClient::HandshakeWithServer (Error *error_ptr)
86{
87 // Start the read thread after we send the handshake ack since if we
88 // fail to send the handshake ack, there is no reason to continue...
89 if (SendAck())
Greg Clayton63afdb02011-06-17 01:22:15 +000090 return true;
Greg Clayton58e26e02011-03-24 04:28:38 +000091
92 if (error_ptr)
93 error_ptr->SetErrorString("failed to send the handshake ack");
94 return false;
95}
96
97void
98GDBRemoteCommunicationClient::QueryNoAckModeSupported ()
Greg Clayton61d043b2011-03-22 04:00:09 +000099{
100 if (m_supports_not_sending_acks == eLazyBoolCalculate)
101 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000102 m_send_acks = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000103 m_supports_not_sending_acks = eLazyBoolNo;
Greg Clayton58e26e02011-03-24 04:28:38 +0000104
105 StringExtractorGDBRemote response;
Greg Clayton61d043b2011-03-22 04:00:09 +0000106 if (SendPacketAndWaitForResponse("QStartNoAckMode", response, false))
107 {
108 if (response.IsOKResponse())
Greg Clayton58e26e02011-03-24 04:28:38 +0000109 {
110 m_send_acks = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000111 m_supports_not_sending_acks = eLazyBoolYes;
Greg Clayton58e26e02011-03-24 04:28:38 +0000112 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000113 }
114 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000115}
116
117void
Greg Claytona1f645e2012-04-10 03:22:03 +0000118GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported ()
119{
120 if (m_supports_threads_in_stop_reply == eLazyBoolCalculate)
121 {
122 m_supports_threads_in_stop_reply = eLazyBoolNo;
123
124 StringExtractorGDBRemote response;
125 if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response, false))
126 {
127 if (response.IsOKResponse())
128 m_supports_threads_in_stop_reply = eLazyBoolYes;
129 }
130 }
131}
132
133
134void
Greg Clayton61d043b2011-03-22 04:00:09 +0000135GDBRemoteCommunicationClient::ResetDiscoverableSettings()
136{
137 m_supports_not_sending_acks = eLazyBoolCalculate;
138 m_supports_thread_suffix = eLazyBoolCalculate;
Greg Claytona1f645e2012-04-10 03:22:03 +0000139 m_supports_threads_in_stop_reply = eLazyBoolCalculate;
Greg Clayton61d043b2011-03-22 04:00:09 +0000140 m_supports_vCont_c = eLazyBoolCalculate;
141 m_supports_vCont_C = eLazyBoolCalculate;
142 m_supports_vCont_s = eLazyBoolCalculate;
143 m_supports_vCont_S = eLazyBoolCalculate;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000144 m_qHostInfo_is_valid = eLazyBoolCalculate;
Greg Clayton2f085c62011-05-15 01:25:55 +0000145 m_supports_alloc_dealloc_memory = eLazyBoolCalculate;
Greg Claytona9385532011-11-18 07:03:08 +0000146 m_supports_memory_region_info = eLazyBoolCalculate;
Greg Clayton989816b2011-05-14 01:50:35 +0000147
Greg Clayton24bc5d92011-03-30 18:16:51 +0000148 m_supports_qProcessInfoPID = true;
149 m_supports_qfProcessInfo = true;
150 m_supports_qUserName = true;
151 m_supports_qGroupName = true;
Greg Claytonb72d0f02011-04-12 05:54:46 +0000152 m_supports_qThreadStopInfo = true;
153 m_supports_z0 = true;
154 m_supports_z1 = true;
155 m_supports_z2 = true;
156 m_supports_z3 = true;
157 m_supports_z4 = true;
Greg Claytoncb8977d2011-03-23 00:09:55 +0000158 m_host_arch.Clear();
Greg Clayton61d043b2011-03-22 04:00:09 +0000159}
160
161
162bool
163GDBRemoteCommunicationClient::GetThreadSuffixSupported ()
164{
165 if (m_supports_thread_suffix == eLazyBoolCalculate)
166 {
167 StringExtractorGDBRemote response;
168 m_supports_thread_suffix = eLazyBoolNo;
169 if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response, false))
170 {
171 if (response.IsOKResponse())
172 m_supports_thread_suffix = eLazyBoolYes;
173 }
174 }
175 return m_supports_thread_suffix;
176}
177bool
178GDBRemoteCommunicationClient::GetVContSupported (char flavor)
179{
180 if (m_supports_vCont_c == eLazyBoolCalculate)
181 {
182 StringExtractorGDBRemote response;
183 m_supports_vCont_any = eLazyBoolNo;
184 m_supports_vCont_all = eLazyBoolNo;
185 m_supports_vCont_c = eLazyBoolNo;
186 m_supports_vCont_C = eLazyBoolNo;
187 m_supports_vCont_s = eLazyBoolNo;
188 m_supports_vCont_S = eLazyBoolNo;
189 if (SendPacketAndWaitForResponse("vCont?", response, false))
190 {
191 const char *response_cstr = response.GetStringRef().c_str();
192 if (::strstr (response_cstr, ";c"))
193 m_supports_vCont_c = eLazyBoolYes;
194
195 if (::strstr (response_cstr, ";C"))
196 m_supports_vCont_C = eLazyBoolYes;
197
198 if (::strstr (response_cstr, ";s"))
199 m_supports_vCont_s = eLazyBoolYes;
200
201 if (::strstr (response_cstr, ";S"))
202 m_supports_vCont_S = eLazyBoolYes;
203
204 if (m_supports_vCont_c == eLazyBoolYes &&
205 m_supports_vCont_C == eLazyBoolYes &&
206 m_supports_vCont_s == eLazyBoolYes &&
207 m_supports_vCont_S == eLazyBoolYes)
208 {
209 m_supports_vCont_all = eLazyBoolYes;
210 }
211
212 if (m_supports_vCont_c == eLazyBoolYes ||
213 m_supports_vCont_C == eLazyBoolYes ||
214 m_supports_vCont_s == eLazyBoolYes ||
215 m_supports_vCont_S == eLazyBoolYes)
216 {
217 m_supports_vCont_any = eLazyBoolYes;
218 }
219 }
220 }
221
222 switch (flavor)
223 {
224 case 'a': return m_supports_vCont_any;
225 case 'A': return m_supports_vCont_all;
226 case 'c': return m_supports_vCont_c;
227 case 'C': return m_supports_vCont_C;
228 case 's': return m_supports_vCont_s;
229 case 'S': return m_supports_vCont_S;
230 default: break;
231 }
232 return false;
233}
234
235
236size_t
237GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
238(
239 const char *payload,
240 StringExtractorGDBRemote &response,
241 bool send_async
242)
243{
244 return SendPacketAndWaitForResponse (payload,
245 ::strlen (payload),
246 response,
247 send_async);
248}
249
250size_t
251GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
252(
253 const char *payload,
254 size_t payload_length,
255 StringExtractorGDBRemote &response,
256 bool send_async
257)
258{
259 Mutex::Locker locker;
Greg Clayton61d043b2011-03-22 04:00:09 +0000260 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton801417e2011-07-07 01:59:51 +0000261 size_t response_len = 0;
Greg Claytonae932352012-04-10 00:18:59 +0000262 if (TryLockSequenceMutex (locker))
Greg Clayton61d043b2011-03-22 04:00:09 +0000263 {
Greg Clayton139da722011-05-20 03:15:54 +0000264 if (SendPacketNoLock (payload, payload_length))
Greg Clayton801417e2011-07-07 01:59:51 +0000265 response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
266 else
267 {
268 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000269 log->Printf("error: failed to send '%*s'", (int) payload_length, payload);
Greg Clayton801417e2011-07-07 01:59:51 +0000270 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000271 }
272 else
273 {
274 if (send_async)
275 {
276 Mutex::Locker async_locker (m_async_mutex);
277 m_async_packet.assign(payload, payload_length);
278 m_async_packet_predicate.SetValue (true, eBroadcastNever);
279
280 if (log)
281 log->Printf ("async: async packet = %s", m_async_packet.c_str());
282
283 bool timed_out = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000284 if (SendInterrupt(locker, 2, timed_out))
Greg Clayton61d043b2011-03-22 04:00:09 +0000285 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000286 if (m_interrupt_sent)
Greg Clayton61d043b2011-03-22 04:00:09 +0000287 {
Greg Clayton63afdb02011-06-17 01:22:15 +0000288 TimeValue timeout_time;
289 timeout_time = TimeValue::Now();
290 timeout_time.OffsetWithSeconds (m_packet_timeout);
291
Greg Clayton61d043b2011-03-22 04:00:09 +0000292 if (log)
293 log->Printf ("async: sent interrupt");
Greg Clayton801417e2011-07-07 01:59:51 +0000294
Greg Clayton61d043b2011-03-22 04:00:09 +0000295 if (m_async_packet_predicate.WaitForValueEqualTo (false, &timeout_time, &timed_out))
296 {
297 if (log)
298 log->Printf ("async: got response");
Greg Clayton801417e2011-07-07 01:59:51 +0000299
300 // Swap the response buffer to avoid malloc and string copy
301 response.GetStringRef().swap (m_async_response.GetStringRef());
302 response_len = response.GetStringRef().size();
Greg Clayton61d043b2011-03-22 04:00:09 +0000303 }
304 else
305 {
306 if (log)
307 log->Printf ("async: timed out waiting for response");
308 }
309
310 // Make sure we wait until the continue packet has been sent again...
311 if (m_private_is_running.WaitForValueEqualTo (true, &timeout_time, &timed_out))
312 {
Greg Claytonb7669b32011-10-27 22:04:16 +0000313 if (log)
314 {
315 if (timed_out)
316 log->Printf ("async: timed out waiting for process to resume, but process was resumed");
317 else
318 log->Printf ("async: async packet sent");
319 }
320 }
321 else
322 {
Greg Clayton61d043b2011-03-22 04:00:09 +0000323 if (log)
324 log->Printf ("async: timed out waiting for process to resume");
325 }
326 }
327 else
328 {
329 // We had a racy condition where we went to send the interrupt
Greg Clayton05e4d972012-03-29 01:55:41 +0000330 // yet we were able to get the lock, so the process must have
331 // just stopped?
Greg Clayton801417e2011-07-07 01:59:51 +0000332 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000333 log->Printf ("async: got lock without sending interrupt");
334 // Send the packet normally since we got the lock
335 if (SendPacketNoLock (payload, payload_length))
336 response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
337 else
338 {
339 if (log)
340 log->Printf("error: failed to send '%*s'", (int) payload_length, payload);
341 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000342 }
343 }
344 else
345 {
346 if (log)
347 log->Printf ("async: failed to interrupt");
348 }
349 }
350 else
351 {
352 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000353 log->Printf("error: packet mutex taken and send_async == false, not sending packet '%*s'", (int) payload_length, payload);
Greg Clayton61d043b2011-03-22 04:00:09 +0000354 }
355 }
Greg Clayton801417e2011-07-07 01:59:51 +0000356 if (response_len == 0)
357 {
358 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000359 log->Printf("error: failed to get response for '%*s'", (int) payload_length, payload);
Greg Clayton801417e2011-07-07 01:59:51 +0000360 }
361 return response_len;
Greg Clayton61d043b2011-03-22 04:00:09 +0000362}
363
364//template<typename _Tp>
365//class ScopedValueChanger
366//{
367//public:
368// // Take a value reference and the value to assign it to when this class
369// // instance goes out of scope.
370// ScopedValueChanger (_Tp &value_ref, _Tp value) :
371// m_value_ref (value_ref),
372// m_value (value)
373// {
374// }
375//
376// // This object is going out of scope, change the value pointed to by
377// // m_value_ref to the value we got during construction which was stored in
378// // m_value;
379// ~ScopedValueChanger ()
380// {
381// m_value_ref = m_value;
382// }
383//protected:
384// _Tp &m_value_ref; // A reference to the value we will change when this object destructs
385// _Tp m_value; // The value to assign to m_value_ref when this goes out of scope.
386//};
387
388StateType
389GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse
390(
391 ProcessGDBRemote *process,
392 const char *payload,
393 size_t packet_length,
394 StringExtractorGDBRemote &response
395)
396{
397 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
398 if (log)
399 log->Printf ("GDBRemoteCommunicationClient::%s ()", __FUNCTION__);
400
401 Mutex::Locker locker(m_sequence_mutex);
402 StateType state = eStateRunning;
403
404 BroadcastEvent(eBroadcastBitRunPacketSent, NULL);
405 m_public_is_running.SetValue (true, eBroadcastNever);
406 // Set the starting continue packet into "continue_packet". This packet
407 // make change if we are interrupted and we continue after an async packet...
408 std::string continue_packet(payload, packet_length);
409
Greg Clayton628cead2011-05-19 03:54:16 +0000410 bool got_stdout = false;
411
Greg Clayton61d043b2011-03-22 04:00:09 +0000412 while (state == eStateRunning)
413 {
Greg Clayton628cead2011-05-19 03:54:16 +0000414 if (!got_stdout)
415 {
416 if (log)
417 log->Printf ("GDBRemoteCommunicationClient::%s () sending continue packet: %s", __FUNCTION__, continue_packet.c_str());
418 if (SendPacket(continue_packet.c_str(), continue_packet.size()) == 0)
419 state = eStateInvalid;
Greg Clayton61d043b2011-03-22 04:00:09 +0000420
Greg Claytonb7669b32011-10-27 22:04:16 +0000421 m_private_is_running.SetValue (true, eBroadcastAlways);
Greg Clayton628cead2011-05-19 03:54:16 +0000422 }
423
424 got_stdout = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000425
426 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000427 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(%s)", __FUNCTION__, continue_packet.c_str());
Greg Clayton61d043b2011-03-22 04:00:09 +0000428
Greg Clayton63afdb02011-06-17 01:22:15 +0000429 if (WaitForPacketWithTimeoutMicroSeconds (response, UINT32_MAX))
Greg Clayton61d043b2011-03-22 04:00:09 +0000430 {
431 if (response.Empty())
432 state = eStateInvalid;
433 else
434 {
435 const char stop_type = response.GetChar();
436 if (log)
437 log->Printf ("GDBRemoteCommunicationClient::%s () got packet: %s", __FUNCTION__, response.GetStringRef().c_str());
438 switch (stop_type)
439 {
440 case 'T':
441 case 'S':
Greg Clayton61d043b2011-03-22 04:00:09 +0000442 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000443 if (process->GetStopID() == 0)
Greg Clayton61d043b2011-03-22 04:00:09 +0000444 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000445 if (process->GetID() == LLDB_INVALID_PROCESS_ID)
446 {
447 lldb::pid_t pid = GetCurrentProcessID ();
448 if (pid != LLDB_INVALID_PROCESS_ID)
449 process->SetID (pid);
450 }
451 process->BuildDynamicRegisterInfo (true);
Greg Clayton61d043b2011-03-22 04:00:09 +0000452 }
Greg Clayton05e4d972012-03-29 01:55:41 +0000453
454 // Privately notify any internal threads that we have stopped
455 // in case we wanted to interrupt our process, yet we might
456 // send a packet and continue without returning control to the
457 // user.
458 m_private_is_running.SetValue (false, eBroadcastAlways);
459
460 const uint8_t signo = response.GetHexU8 (UINT8_MAX);
461
462 bool continue_after_aync = false;
463 if (m_async_signal != -1 || m_async_packet_predicate.GetValue())
464 {
465 continue_after_aync = true;
466 // We sent an interrupt packet to stop the inferior process
467 // for an async signal or to send an async packet while running
468 // but we might have been single stepping and received the
469 // stop packet for the step instead of for the interrupt packet.
470 // Typically when an interrupt is sent a SIGINT or SIGSTOP
471 // is used, so if we get anything else, we need to try and
472 // get another stop reply packet that may have been sent
473 // due to sending the interrupt when the target is stopped
474 // which will just re-send a copy of the last stop reply
475 // packet. If we don't do this, then the reply for our
476 // async packet will be the repeat stop reply packet and cause
477 // a lot of trouble for us!
478 if (signo != SIGINT && signo != SIGSTOP)
479 {
480 continue_after_aync = false;
481
482 // We didn't get a a SIGINT or SIGSTOP, so try for a
483 // very brief time (1 ms) to get another stop reply
484 // packet to make sure it doesn't get in the way
485 StringExtractorGDBRemote extra_stop_reply_packet;
486 uint32_t timeout_usec = 1000;
487 if (WaitForPacketWithTimeoutMicroSecondsNoLock (extra_stop_reply_packet, timeout_usec))
488 {
489 switch (extra_stop_reply_packet.GetChar())
490 {
491 case 'T':
492 case 'S':
493 // We did get an extra stop reply, which means
494 // our interrupt didn't stop the target so we
495 // shouldn't continue after the async signal
496 // or packet is sent...
497 continue_after_aync = false;
498 break;
499 }
500 }
501 }
502 }
503
504 if (m_async_signal != -1)
505 {
506 if (log)
507 log->Printf ("async: send signo = %s", Host::GetSignalAsCString (m_async_signal));
508
509 // Save off the async signal we are supposed to send
510 const int async_signal = m_async_signal;
511 // Clear the async signal member so we don't end up
512 // sending the signal multiple times...
513 m_async_signal = -1;
514 // Check which signal we stopped with
515 if (signo == async_signal)
516 {
517 if (log)
518 log->Printf ("async: stopped with signal %s, we are done running", Host::GetSignalAsCString (signo));
519
520 // We already stopped with a signal that we wanted
521 // to stop with, so we are done
522 }
523 else
524 {
525 // We stopped with a different signal that the one
526 // we wanted to stop with, so now we must resume
527 // with the signal we want
528 char signal_packet[32];
529 int signal_packet_len = 0;
530 signal_packet_len = ::snprintf (signal_packet,
531 sizeof (signal_packet),
532 "C%2.2x",
533 async_signal);
534
535 if (log)
536 log->Printf ("async: stopped with signal %s, resume with %s",
537 Host::GetSignalAsCString (signo),
538 Host::GetSignalAsCString (async_signal));
539
540 // Set the continue packet to resume even if the
541 // interrupt didn't cause our stop (ignore continue_after_aync)
542 continue_packet.assign(signal_packet, signal_packet_len);
543 continue;
544 }
545 }
546 else if (m_async_packet_predicate.GetValue())
547 {
548 LogSP packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
549
550 // We are supposed to send an asynchronous packet while
551 // we are running.
552 m_async_response.Clear();
553 if (m_async_packet.empty())
554 {
555 if (packet_log)
556 packet_log->Printf ("async: error: empty async packet");
557
558 }
559 else
560 {
561 if (packet_log)
562 packet_log->Printf ("async: sending packet");
563
564 SendPacketAndWaitForResponse (&m_async_packet[0],
565 m_async_packet.size(),
566 m_async_response,
567 false);
568 }
569 // Let the other thread that was trying to send the async
570 // packet know that the packet has been sent and response is
571 // ready...
572 m_async_packet_predicate.SetValue(false, eBroadcastAlways);
573
574 if (packet_log)
575 packet_log->Printf ("async: sent packet, continue_after_aync = %i", continue_after_aync);
576
577 // Set the continue packet to resume if our interrupt
578 // for the async packet did cause the stop
579 if (continue_after_aync)
580 {
581 continue_packet.assign (1, 'c');
582 continue;
583 }
584 }
585 // Stop with signal and thread info
586 state = eStateStopped;
Greg Clayton61d043b2011-03-22 04:00:09 +0000587 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000588 break;
589
590 case 'W':
591 case 'X':
592 // process exited
593 state = eStateExited;
594 break;
595
596 case 'O':
597 // STDOUT
598 {
Greg Clayton628cead2011-05-19 03:54:16 +0000599 got_stdout = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000600 std::string inferior_stdout;
601 inferior_stdout.reserve(response.GetBytesLeft () / 2);
602 char ch;
603 while ((ch = response.GetHexU8()) != '\0')
604 inferior_stdout.append(1, ch);
605 process->AppendSTDOUT (inferior_stdout.c_str(), inferior_stdout.size());
606 }
607 break;
608
609 case 'E':
610 // ERROR
611 state = eStateInvalid;
612 break;
613
614 default:
615 if (log)
616 log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__);
617 state = eStateInvalid;
618 break;
619 }
620 }
621 }
622 else
623 {
624 if (log)
625 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__);
626 state = eStateInvalid;
627 }
628 }
629 if (log)
630 log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state));
631 response.SetFilePos(0);
632 m_private_is_running.SetValue (false, eBroadcastAlways);
633 m_public_is_running.SetValue (false, eBroadcastAlways);
634 return state;
635}
636
637bool
638GDBRemoteCommunicationClient::SendAsyncSignal (int signo)
639{
Greg Clayton05e4d972012-03-29 01:55:41 +0000640 Mutex::Locker async_locker (m_async_mutex);
Greg Clayton61d043b2011-03-22 04:00:09 +0000641 m_async_signal = signo;
642 bool timed_out = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000643 Mutex::Locker locker;
Greg Clayton05e4d972012-03-29 01:55:41 +0000644 if (SendInterrupt (locker, 1, timed_out))
Greg Clayton61d043b2011-03-22 04:00:09 +0000645 return true;
646 m_async_signal = -1;
647 return false;
648}
649
Greg Claytonae932352012-04-10 00:18:59 +0000650// This function takes a mutex locker as a parameter in case the TryLockSequenceMutex
Greg Clayton61d043b2011-03-22 04:00:09 +0000651// actually succeeds. If it doesn't succeed in acquiring the sequence mutex
652// (the expected result), then it will send the halt packet. If it does succeed
653// then the caller that requested the interrupt will want to keep the sequence
654// locked down so that no one else can send packets while the caller has control.
655// This function usually gets called when we are running and need to stop the
656// target. It can also be used when we are running and and we need to do something
657// else (like read/write memory), so we need to interrupt the running process
658// (gdb remote protocol requires this), and do what we need to do, then resume.
659
660bool
Greg Clayton05e4d972012-03-29 01:55:41 +0000661GDBRemoteCommunicationClient::SendInterrupt
Greg Clayton61d043b2011-03-22 04:00:09 +0000662(
663 Mutex::Locker& locker,
664 uint32_t seconds_to_wait_for_stop,
Greg Clayton61d043b2011-03-22 04:00:09 +0000665 bool &timed_out
666)
667{
Greg Clayton05e4d972012-03-29 01:55:41 +0000668 m_interrupt_sent = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000669 timed_out = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000670 LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Clayton61d043b2011-03-22 04:00:09 +0000671
672 if (IsRunning())
673 {
674 // Only send an interrupt if our debugserver is running...
Greg Claytonae932352012-04-10 00:18:59 +0000675 if (TryLockSequenceMutex (locker) == false)
Greg Clayton61d043b2011-03-22 04:00:09 +0000676 {
677 // Someone has the mutex locked waiting for a response or for the
678 // inferior to stop, so send the interrupt on the down low...
679 char ctrl_c = '\x03';
680 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton61d043b2011-03-22 04:00:09 +0000681 size_t bytes_written = Write (&ctrl_c, 1, status, NULL);
Greg Clayton05e4d972012-03-29 01:55:41 +0000682 if (log)
683 log->PutCString("send packet: \\x03");
Greg Clayton61d043b2011-03-22 04:00:09 +0000684 if (bytes_written > 0)
685 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000686 m_interrupt_sent = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000687 if (seconds_to_wait_for_stop)
688 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000689 TimeValue timeout;
690 if (seconds_to_wait_for_stop)
691 {
692 timeout = TimeValue::Now();
693 timeout.OffsetWithSeconds (seconds_to_wait_for_stop);
694 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000695 if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out))
696 {
697 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000698 log->PutCString ("SendInterrupt () - sent interrupt, private state stopped");
Greg Clayton61d043b2011-03-22 04:00:09 +0000699 return true;
700 }
701 else
702 {
703 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000704 log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume");
Greg Clayton61d043b2011-03-22 04:00:09 +0000705 }
706 }
707 else
708 {
709 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000710 log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop...");
Greg Clayton61d043b2011-03-22 04:00:09 +0000711 return true;
712 }
713 }
714 else
715 {
716 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000717 log->Printf ("SendInterrupt () - failed to write interrupt");
Greg Clayton61d043b2011-03-22 04:00:09 +0000718 }
719 return false;
720 }
721 else
722 {
723 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000724 log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt");
Greg Clayton61d043b2011-03-22 04:00:09 +0000725 }
726 }
Greg Clayton05e4d972012-03-29 01:55:41 +0000727 else
728 {
729 if (log)
730 log->Printf ("SendInterrupt () - not running");
731 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000732 return true;
733}
734
735lldb::pid_t
736GDBRemoteCommunicationClient::GetCurrentProcessID ()
737{
738 StringExtractorGDBRemote response;
739 if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false))
740 {
741 if (response.GetChar() == 'Q')
742 if (response.GetChar() == 'C')
743 return response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID);
744 }
745 return LLDB_INVALID_PROCESS_ID;
746}
747
748bool
749GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str)
750{
751 error_str.clear();
752 StringExtractorGDBRemote response;
753 if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false))
754 {
755 if (response.IsOKResponse())
756 return true;
757 if (response.GetChar() == 'E')
758 {
759 // A string the describes what failed when launching...
760 error_str = response.GetStringRef().substr(1);
761 }
762 else
763 {
764 error_str.assign ("unknown error occurred launching process");
765 }
766 }
767 else
768 {
769 error_str.assign ("failed to send the qLaunchSuccess packet");
770 }
771 return false;
772}
773
774int
775GDBRemoteCommunicationClient::SendArgumentsPacket (char const *argv[])
776{
777 if (argv && argv[0])
778 {
779 StreamString packet;
780 packet.PutChar('A');
781 const char *arg;
782 for (uint32_t i = 0; (arg = argv[i]) != NULL; ++i)
783 {
784 const int arg_len = strlen(arg);
785 if (i > 0)
786 packet.PutChar(',');
787 packet.Printf("%i,%i,", arg_len * 2, i);
788 packet.PutBytesAsRawHex8 (arg, arg_len);
789 }
790
791 StringExtractorGDBRemote response;
792 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
793 {
794 if (response.IsOKResponse())
795 return 0;
796 uint8_t error = response.GetError();
797 if (error)
798 return error;
799 }
800 }
801 return -1;
802}
803
804int
805GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value)
806{
807 if (name_equal_value && name_equal_value[0])
808 {
809 StreamString packet;
810 packet.Printf("QEnvironment:%s", name_equal_value);
811 StringExtractorGDBRemote response;
812 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
813 {
814 if (response.IsOKResponse())
815 return 0;
816 uint8_t error = response.GetError();
817 if (error)
818 return error;
819 }
820 }
821 return -1;
822}
823
Greg Claytona4582402011-05-08 04:53:50 +0000824int
825GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch)
826{
827 if (arch && arch[0])
828 {
829 StreamString packet;
830 packet.Printf("QLaunchArch:%s", arch);
831 StringExtractorGDBRemote response;
832 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
833 {
834 if (response.IsOKResponse())
835 return 0;
836 uint8_t error = response.GetError();
837 if (error)
838 return error;
839 }
840 }
841 return -1;
842}
843
Greg Clayton61d043b2011-03-22 04:00:09 +0000844bool
Greg Clayton58e26e02011-03-24 04:28:38 +0000845GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major,
846 uint32_t &minor,
847 uint32_t &update)
848{
849 if (GetHostInfo ())
850 {
851 if (m_os_version_major != UINT32_MAX)
852 {
853 major = m_os_version_major;
854 minor = m_os_version_minor;
855 update = m_os_version_update;
856 return true;
857 }
858 }
859 return false;
860}
861
862bool
863GDBRemoteCommunicationClient::GetOSBuildString (std::string &s)
864{
865 if (GetHostInfo ())
866 {
867 if (!m_os_build.empty())
868 {
869 s = m_os_build;
870 return true;
871 }
872 }
873 s.clear();
874 return false;
875}
876
877
878bool
879GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s)
880{
881 if (GetHostInfo ())
882 {
883 if (!m_os_kernel.empty())
884 {
885 s = m_os_kernel;
886 return true;
887 }
888 }
889 s.clear();
890 return false;
891}
892
893bool
894GDBRemoteCommunicationClient::GetHostname (std::string &s)
895{
896 if (GetHostInfo ())
897 {
898 if (!m_hostname.empty())
899 {
900 s = m_hostname;
901 return true;
902 }
903 }
904 s.clear();
905 return false;
906}
907
908ArchSpec
909GDBRemoteCommunicationClient::GetSystemArchitecture ()
910{
911 if (GetHostInfo ())
912 return m_host_arch;
913 return ArchSpec();
914}
915
916
917bool
Greg Clayton06d7cc82011-04-04 18:18:57 +0000918GDBRemoteCommunicationClient::GetHostInfo (bool force)
Greg Clayton61d043b2011-03-22 04:00:09 +0000919{
Greg Clayton06d7cc82011-04-04 18:18:57 +0000920 if (force || m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton61d043b2011-03-22 04:00:09 +0000921 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000922 m_qHostInfo_is_valid = eLazyBoolNo;
Greg Clayton61d043b2011-03-22 04:00:09 +0000923 StringExtractorGDBRemote response;
924 if (SendPacketAndWaitForResponse ("qHostInfo", response, false))
925 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +0000926 if (response.IsNormalResponse())
Greg Claytoncb8977d2011-03-23 00:09:55 +0000927 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000928 std::string name;
929 std::string value;
930 uint32_t cpu = LLDB_INVALID_CPUTYPE;
931 uint32_t sub = 0;
932 std::string arch_name;
933 std::string os_name;
934 std::string vendor_name;
935 std::string triple;
936 uint32_t pointer_byte_size = 0;
937 StringExtractor extractor;
938 ByteOrder byte_order = eByteOrderInvalid;
939 uint32_t num_keys_decoded = 0;
940 while (response.GetNameColonValue(name, value))
Greg Claytoncb8977d2011-03-23 00:09:55 +0000941 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000942 if (name.compare("cputype") == 0)
Greg Clayton58e26e02011-03-24 04:28:38 +0000943 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000944 // exception type in big endian hex
945 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0);
946 if (cpu != LLDB_INVALID_CPUTYPE)
947 ++num_keys_decoded;
948 }
949 else if (name.compare("cpusubtype") == 0)
950 {
951 // exception count in big endian hex
952 sub = Args::StringToUInt32 (value.c_str(), 0, 0);
953 if (sub != 0)
954 ++num_keys_decoded;
955 }
956 else if (name.compare("arch") == 0)
957 {
958 arch_name.swap (value);
959 ++num_keys_decoded;
960 }
961 else if (name.compare("triple") == 0)
962 {
963 // The triple comes as ASCII hex bytes since it contains '-' chars
964 extractor.GetStringRef().swap(value);
965 extractor.SetFilePos(0);
966 extractor.GetHexByteString (triple);
967 ++num_keys_decoded;
968 }
969 else if (name.compare("os_build") == 0)
970 {
971 extractor.GetStringRef().swap(value);
972 extractor.SetFilePos(0);
973 extractor.GetHexByteString (m_os_build);
974 ++num_keys_decoded;
975 }
976 else if (name.compare("hostname") == 0)
977 {
978 extractor.GetStringRef().swap(value);
979 extractor.SetFilePos(0);
980 extractor.GetHexByteString (m_hostname);
981 ++num_keys_decoded;
982 }
983 else if (name.compare("os_kernel") == 0)
984 {
985 extractor.GetStringRef().swap(value);
986 extractor.SetFilePos(0);
987 extractor.GetHexByteString (m_os_kernel);
988 ++num_keys_decoded;
989 }
990 else if (name.compare("ostype") == 0)
991 {
992 os_name.swap (value);
993 ++num_keys_decoded;
994 }
995 else if (name.compare("vendor") == 0)
996 {
997 vendor_name.swap(value);
998 ++num_keys_decoded;
999 }
1000 else if (name.compare("endian") == 0)
1001 {
1002 ++num_keys_decoded;
1003 if (value.compare("little") == 0)
1004 byte_order = eByteOrderLittle;
1005 else if (value.compare("big") == 0)
1006 byte_order = eByteOrderBig;
1007 else if (value.compare("pdp") == 0)
1008 byte_order = eByteOrderPDP;
1009 else
1010 --num_keys_decoded;
1011 }
1012 else if (name.compare("ptrsize") == 0)
1013 {
1014 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 0);
1015 if (pointer_byte_size != 0)
1016 ++num_keys_decoded;
1017 }
1018 else if (name.compare("os_version") == 0)
1019 {
1020 Args::StringToVersion (value.c_str(),
1021 m_os_version_major,
1022 m_os_version_minor,
1023 m_os_version_update);
1024 if (m_os_version_major != UINT32_MAX)
1025 ++num_keys_decoded;
1026 }
1027 }
1028
1029 if (num_keys_decoded > 0)
1030 m_qHostInfo_is_valid = eLazyBoolYes;
1031
1032 if (triple.empty())
1033 {
1034 if (arch_name.empty())
1035 {
1036 if (cpu != LLDB_INVALID_CPUTYPE)
1037 {
1038 m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1039 if (pointer_byte_size)
1040 {
1041 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1042 }
1043 if (byte_order != eByteOrderInvalid)
1044 {
1045 assert (byte_order == m_host_arch.GetByteOrder());
1046 }
1047 if (!vendor_name.empty())
1048 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1049 if (!os_name.empty())
Greg Clayton89798cc2011-09-15 00:21:03 +00001050 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001051
1052 }
1053 }
1054 else
1055 {
1056 std::string triple;
1057 triple += arch_name;
1058 triple += '-';
1059 if (vendor_name.empty())
1060 triple += "unknown";
1061 else
1062 triple += vendor_name;
1063 triple += '-';
1064 if (os_name.empty())
1065 triple += "unknown";
1066 else
1067 triple += os_name;
Greg Claytonf15996e2011-04-07 22:46:35 +00001068 m_host_arch.SetTriple (triple.c_str(), NULL);
Greg Clayton58e26e02011-03-24 04:28:38 +00001069 if (pointer_byte_size)
1070 {
1071 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1072 }
1073 if (byte_order != eByteOrderInvalid)
1074 {
1075 assert (byte_order == m_host_arch.GetByteOrder());
1076 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001077
Greg Clayton58e26e02011-03-24 04:28:38 +00001078 }
1079 }
1080 else
1081 {
Greg Claytonf15996e2011-04-07 22:46:35 +00001082 m_host_arch.SetTriple (triple.c_str(), NULL);
Greg Claytoncb8977d2011-03-23 00:09:55 +00001083 if (pointer_byte_size)
1084 {
1085 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1086 }
1087 if (byte_order != eByteOrderInvalid)
1088 {
1089 assert (byte_order == m_host_arch.GetByteOrder());
1090 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001091 }
Greg Claytoncb8977d2011-03-23 00:09:55 +00001092 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001093 }
1094 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001095 return m_qHostInfo_is_valid == eLazyBoolYes;
Greg Clayton61d043b2011-03-22 04:00:09 +00001096}
1097
1098int
1099GDBRemoteCommunicationClient::SendAttach
1100(
1101 lldb::pid_t pid,
1102 StringExtractorGDBRemote& response
1103)
1104{
1105 if (pid != LLDB_INVALID_PROCESS_ID)
1106 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001107 char packet[64];
Greg Claytond9919d32011-12-01 23:28:38 +00001108 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%llx", pid);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001109 assert (packet_len < sizeof(packet));
1110 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton61d043b2011-03-22 04:00:09 +00001111 {
1112 if (response.IsErrorResponse())
1113 return response.GetError();
1114 return 0;
1115 }
1116 }
1117 return -1;
1118}
1119
1120const lldb_private::ArchSpec &
1121GDBRemoteCommunicationClient::GetHostArchitecture ()
1122{
Greg Clayton24bc5d92011-03-30 18:16:51 +00001123 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton61d043b2011-03-22 04:00:09 +00001124 GetHostInfo ();
Greg Claytoncb8977d2011-03-23 00:09:55 +00001125 return m_host_arch;
Greg Clayton61d043b2011-03-22 04:00:09 +00001126}
1127
1128addr_t
1129GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions)
1130{
Greg Clayton2f085c62011-05-15 01:25:55 +00001131 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton61d043b2011-03-22 04:00:09 +00001132 {
Greg Clayton2f085c62011-05-15 01:25:55 +00001133 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton989816b2011-05-14 01:50:35 +00001134 char packet[64];
1135 const int packet_len = ::snprintf (packet, sizeof(packet), "_M%zx,%s%s%s", size,
1136 permissions & lldb::ePermissionsReadable ? "r" : "",
1137 permissions & lldb::ePermissionsWritable ? "w" : "",
1138 permissions & lldb::ePermissionsExecutable ? "x" : "");
1139 assert (packet_len < sizeof(packet));
1140 StringExtractorGDBRemote response;
1141 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1142 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001143 if (!response.IsErrorResponse())
Greg Clayton989816b2011-05-14 01:50:35 +00001144 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1145 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001146 else
1147 {
1148 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1149 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001150 }
1151 return LLDB_INVALID_ADDRESS;
1152}
1153
1154bool
1155GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr)
1156{
Greg Clayton2f085c62011-05-15 01:25:55 +00001157 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton61d043b2011-03-22 04:00:09 +00001158 {
Greg Clayton2f085c62011-05-15 01:25:55 +00001159 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton989816b2011-05-14 01:50:35 +00001160 char packet[64];
1161 const int packet_len = ::snprintf(packet, sizeof(packet), "_m%llx", (uint64_t)addr);
1162 assert (packet_len < sizeof(packet));
1163 StringExtractorGDBRemote response;
1164 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1165 {
1166 if (response.IsOKResponse())
1167 return true;
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001168 }
1169 else
1170 {
1171 m_supports_alloc_dealloc_memory = eLazyBoolNo;
Greg Clayton989816b2011-05-14 01:50:35 +00001172 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001173 }
1174 return false;
1175}
1176
Greg Claytona9385532011-11-18 07:03:08 +00001177Error
1178GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr,
1179 lldb_private::MemoryRegionInfo &region_info)
1180{
1181 Error error;
1182 region_info.Clear();
1183
1184 if (m_supports_memory_region_info != eLazyBoolNo)
1185 {
1186 m_supports_memory_region_info = eLazyBoolYes;
1187 char packet[64];
1188 const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%llx", (uint64_t)addr);
1189 assert (packet_len < sizeof(packet));
1190 StringExtractorGDBRemote response;
1191 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1192 {
1193 std::string name;
1194 std::string value;
1195 addr_t addr_value;
1196 bool success = true;
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001197 bool saw_permissions = false;
Greg Claytona9385532011-11-18 07:03:08 +00001198 while (success && response.GetNameColonValue(name, value))
1199 {
1200 if (name.compare ("start") == 0)
1201 {
1202 addr_value = Args::StringToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success);
1203 if (success)
1204 region_info.GetRange().SetRangeBase(addr_value);
1205 }
1206 else if (name.compare ("size") == 0)
1207 {
1208 addr_value = Args::StringToUInt64(value.c_str(), 0, 16, &success);
1209 if (success)
1210 region_info.GetRange().SetByteSize (addr_value);
1211 }
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001212 else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid())
Greg Claytona9385532011-11-18 07:03:08 +00001213 {
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001214 saw_permissions = true;
1215 if (region_info.GetRange().Contains (addr))
1216 {
1217 if (value.find('r') != std::string::npos)
1218 region_info.SetReadable (MemoryRegionInfo::eYes);
1219 else
1220 region_info.SetReadable (MemoryRegionInfo::eNo);
1221
1222 if (value.find('w') != std::string::npos)
1223 region_info.SetWritable (MemoryRegionInfo::eYes);
1224 else
1225 region_info.SetWritable (MemoryRegionInfo::eNo);
1226
1227 if (value.find('x') != std::string::npos)
1228 region_info.SetExecutable (MemoryRegionInfo::eYes);
1229 else
1230 region_info.SetExecutable (MemoryRegionInfo::eNo);
1231 }
1232 else
1233 {
1234 // The reported region does not contain this address -- we're looking at an unmapped page
1235 region_info.SetReadable (MemoryRegionInfo::eNo);
1236 region_info.SetWritable (MemoryRegionInfo::eNo);
1237 region_info.SetExecutable (MemoryRegionInfo::eNo);
1238 }
Greg Claytona9385532011-11-18 07:03:08 +00001239 }
1240 else if (name.compare ("error") == 0)
1241 {
1242 StringExtractorGDBRemote name_extractor;
1243 // Swap "value" over into "name_extractor"
1244 name_extractor.GetStringRef().swap(value);
1245 // Now convert the HEX bytes into a string value
1246 name_extractor.GetHexByteString (value);
1247 error.SetErrorString(value.c_str());
1248 }
1249 }
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001250
1251 // We got a valid address range back but no permissions -- which means this is an unmapped page
1252 if (region_info.GetRange().IsValid() && saw_permissions == false)
1253 {
1254 region_info.SetReadable (MemoryRegionInfo::eNo);
1255 region_info.SetWritable (MemoryRegionInfo::eNo);
1256 region_info.SetExecutable (MemoryRegionInfo::eNo);
1257 }
Greg Claytona9385532011-11-18 07:03:08 +00001258 }
1259 else
1260 {
1261 m_supports_memory_region_info = eLazyBoolNo;
1262 }
1263 }
1264
1265 if (m_supports_memory_region_info == eLazyBoolNo)
1266 {
1267 error.SetErrorString("qMemoryRegionInfo is not supported");
1268 }
1269 if (error.Fail())
1270 region_info.Clear();
1271 return error;
1272
1273}
1274
1275
Greg Clayton61d043b2011-03-22 04:00:09 +00001276int
1277GDBRemoteCommunicationClient::SetSTDIN (char const *path)
1278{
1279 if (path && path[0])
1280 {
1281 StreamString packet;
1282 packet.PutCString("QSetSTDIN:");
1283 packet.PutBytesAsRawHex8(path, strlen(path));
1284
1285 StringExtractorGDBRemote response;
1286 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1287 {
1288 if (response.IsOKResponse())
1289 return 0;
1290 uint8_t error = response.GetError();
1291 if (error)
1292 return error;
1293 }
1294 }
1295 return -1;
1296}
1297
1298int
1299GDBRemoteCommunicationClient::SetSTDOUT (char const *path)
1300{
1301 if (path && path[0])
1302 {
1303 StreamString packet;
1304 packet.PutCString("QSetSTDOUT:");
1305 packet.PutBytesAsRawHex8(path, strlen(path));
1306
1307 StringExtractorGDBRemote response;
1308 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1309 {
1310 if (response.IsOKResponse())
1311 return 0;
1312 uint8_t error = response.GetError();
1313 if (error)
1314 return error;
1315 }
1316 }
1317 return -1;
1318}
1319
1320int
1321GDBRemoteCommunicationClient::SetSTDERR (char const *path)
1322{
1323 if (path && path[0])
1324 {
1325 StreamString packet;
1326 packet.PutCString("QSetSTDERR:");
1327 packet.PutBytesAsRawHex8(path, strlen(path));
1328
1329 StringExtractorGDBRemote response;
1330 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1331 {
1332 if (response.IsOKResponse())
1333 return 0;
1334 uint8_t error = response.GetError();
1335 if (error)
1336 return error;
1337 }
1338 }
1339 return -1;
1340}
1341
1342int
1343GDBRemoteCommunicationClient::SetWorkingDir (char const *path)
1344{
1345 if (path && path[0])
1346 {
1347 StreamString packet;
1348 packet.PutCString("QSetWorkingDir:");
1349 packet.PutBytesAsRawHex8(path, strlen(path));
1350
1351 StringExtractorGDBRemote response;
1352 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1353 {
1354 if (response.IsOKResponse())
1355 return 0;
1356 uint8_t error = response.GetError();
1357 if (error)
1358 return error;
1359 }
1360 }
1361 return -1;
1362}
1363
1364int
1365GDBRemoteCommunicationClient::SetDisableASLR (bool enable)
1366{
Greg Clayton24bc5d92011-03-30 18:16:51 +00001367 char packet[32];
1368 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0);
1369 assert (packet_len < sizeof(packet));
Greg Clayton61d043b2011-03-22 04:00:09 +00001370 StringExtractorGDBRemote response;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001371 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton61d043b2011-03-22 04:00:09 +00001372 {
1373 if (response.IsOKResponse())
1374 return 0;
1375 uint8_t error = response.GetError();
1376 if (error)
1377 return error;
1378 }
1379 return -1;
1380}
Greg Clayton24bc5d92011-03-30 18:16:51 +00001381
1382bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001383GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001384{
1385 if (response.IsNormalResponse())
1386 {
1387 std::string name;
1388 std::string value;
1389 StringExtractor extractor;
1390
1391 while (response.GetNameColonValue(name, value))
1392 {
1393 if (name.compare("pid") == 0)
1394 {
1395 process_info.SetProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1396 }
1397 else if (name.compare("ppid") == 0)
1398 {
1399 process_info.SetParentProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1400 }
1401 else if (name.compare("uid") == 0)
1402 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001403 process_info.SetUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001404 }
1405 else if (name.compare("euid") == 0)
1406 {
1407 process_info.SetEffectiveUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1408 }
1409 else if (name.compare("gid") == 0)
1410 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001411 process_info.SetGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001412 }
1413 else if (name.compare("egid") == 0)
1414 {
1415 process_info.SetEffectiveGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1416 }
1417 else if (name.compare("triple") == 0)
1418 {
1419 // The triple comes as ASCII hex bytes since it contains '-' chars
1420 extractor.GetStringRef().swap(value);
1421 extractor.SetFilePos(0);
1422 extractor.GetHexByteString (value);
Greg Claytonf15996e2011-04-07 22:46:35 +00001423 process_info.GetArchitecture ().SetTriple (value.c_str(), NULL);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001424 }
1425 else if (name.compare("name") == 0)
1426 {
1427 StringExtractor extractor;
1428 // The the process name from ASCII hex bytes since we can't
1429 // control the characters in a process name
1430 extractor.GetStringRef().swap(value);
1431 extractor.SetFilePos(0);
1432 extractor.GetHexByteString (value);
Greg Clayton527154d2011-11-15 03:53:30 +00001433 process_info.GetExecutableFile().SetFile (value.c_str(), false);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001434 }
1435 }
1436
1437 if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
1438 return true;
1439 }
1440 return false;
1441}
1442
1443bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001444GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001445{
1446 process_info.Clear();
1447
1448 if (m_supports_qProcessInfoPID)
1449 {
1450 char packet[32];
Greg Claytond9919d32011-12-01 23:28:38 +00001451 const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%llu", pid);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001452 assert (packet_len < sizeof(packet));
1453 StringExtractorGDBRemote response;
1454 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1455 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001456 return DecodeProcessInfoResponse (response, process_info);
1457 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001458 else
1459 {
1460 m_supports_qProcessInfoPID = false;
1461 return false;
1462 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001463 }
1464 return false;
1465}
1466
1467uint32_t
Greg Claytonb72d0f02011-04-12 05:54:46 +00001468GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1469 ProcessInstanceInfoList &process_infos)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001470{
1471 process_infos.Clear();
1472
1473 if (m_supports_qfProcessInfo)
1474 {
1475 StreamString packet;
1476 packet.PutCString ("qfProcessInfo");
1477 if (!match_info.MatchAllProcesses())
1478 {
1479 packet.PutChar (':');
1480 const char *name = match_info.GetProcessInfo().GetName();
1481 bool has_name_match = false;
1482 if (name && name[0])
1483 {
1484 has_name_match = true;
1485 NameMatchType name_match_type = match_info.GetNameMatchType();
1486 switch (name_match_type)
1487 {
1488 case eNameMatchIgnore:
1489 has_name_match = false;
1490 break;
1491
1492 case eNameMatchEquals:
1493 packet.PutCString ("name_match:equals;");
1494 break;
1495
1496 case eNameMatchContains:
1497 packet.PutCString ("name_match:contains;");
1498 break;
1499
1500 case eNameMatchStartsWith:
1501 packet.PutCString ("name_match:starts_with;");
1502 break;
1503
1504 case eNameMatchEndsWith:
1505 packet.PutCString ("name_match:ends_with;");
1506 break;
1507
1508 case eNameMatchRegularExpression:
1509 packet.PutCString ("name_match:regex;");
1510 break;
1511 }
1512 if (has_name_match)
1513 {
1514 packet.PutCString ("name:");
1515 packet.PutBytesAsRawHex8(name, ::strlen(name));
1516 packet.PutChar (';');
1517 }
1518 }
1519
1520 if (match_info.GetProcessInfo().ProcessIDIsValid())
Greg Claytond9919d32011-12-01 23:28:38 +00001521 packet.Printf("pid:%llu;",match_info.GetProcessInfo().GetProcessID());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001522 if (match_info.GetProcessInfo().ParentProcessIDIsValid())
Greg Claytond9919d32011-12-01 23:28:38 +00001523 packet.Printf("parent_pid:%llu;",match_info.GetProcessInfo().GetParentProcessID());
Greg Claytonb72d0f02011-04-12 05:54:46 +00001524 if (match_info.GetProcessInfo().UserIDIsValid())
1525 packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID());
1526 if (match_info.GetProcessInfo().GroupIDIsValid())
1527 packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001528 if (match_info.GetProcessInfo().EffectiveUserIDIsValid())
1529 packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID());
1530 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1531 packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID());
1532 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1533 packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0);
1534 if (match_info.GetProcessInfo().GetArchitecture().IsValid())
1535 {
1536 const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture();
1537 const llvm::Triple &triple = match_arch.GetTriple();
1538 packet.PutCString("triple:");
1539 packet.PutCStringAsRawHex8(triple.getTriple().c_str());
1540 packet.PutChar (';');
1541 }
1542 }
1543 StringExtractorGDBRemote response;
1544 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1545 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001546 do
1547 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001548 ProcessInstanceInfo process_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001549 if (!DecodeProcessInfoResponse (response, process_info))
1550 break;
1551 process_infos.Append(process_info);
1552 response.GetStringRef().clear();
1553 response.SetFilePos(0);
1554 } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false));
1555 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001556 else
1557 {
1558 m_supports_qfProcessInfo = false;
1559 return 0;
1560 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001561 }
1562 return process_infos.GetSize();
1563
1564}
1565
1566bool
1567GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name)
1568{
1569 if (m_supports_qUserName)
1570 {
1571 char packet[32];
1572 const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid);
1573 assert (packet_len < sizeof(packet));
1574 StringExtractorGDBRemote response;
1575 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1576 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001577 if (response.IsNormalResponse())
1578 {
1579 // Make sure we parsed the right number of characters. The response is
1580 // the hex encoded user name and should make up the entire packet.
1581 // If there are any non-hex ASCII bytes, the length won't match below..
1582 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
1583 return true;
1584 }
1585 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001586 else
1587 {
1588 m_supports_qUserName = false;
1589 return false;
1590 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001591 }
1592 return false;
1593
1594}
1595
1596bool
1597GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name)
1598{
1599 if (m_supports_qGroupName)
1600 {
1601 char packet[32];
1602 const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid);
1603 assert (packet_len < sizeof(packet));
1604 StringExtractorGDBRemote response;
1605 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1606 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001607 if (response.IsNormalResponse())
1608 {
1609 // Make sure we parsed the right number of characters. The response is
1610 // the hex encoded group name and should make up the entire packet.
1611 // If there are any non-hex ASCII bytes, the length won't match below..
1612 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
1613 return true;
1614 }
1615 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001616 else
1617 {
1618 m_supports_qGroupName = false;
1619 return false;
1620 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001621 }
1622 return false;
Greg Clayton06d7cc82011-04-04 18:18:57 +00001623}
Greg Clayton24bc5d92011-03-30 18:16:51 +00001624
Greg Clayton06d7cc82011-04-04 18:18:57 +00001625void
1626GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets)
1627{
1628 uint32_t i;
1629 TimeValue start_time, end_time;
1630 uint64_t total_time_nsec;
1631 float packets_per_second;
1632 if (SendSpeedTestPacket (0, 0))
1633 {
1634 for (uint32_t send_size = 0; send_size <= 1024; send_size *= 2)
1635 {
1636 for (uint32_t recv_size = 0; recv_size <= 1024; recv_size *= 2)
1637 {
1638 start_time = TimeValue::Now();
1639 for (i=0; i<num_packets; ++i)
1640 {
1641 SendSpeedTestPacket (send_size, recv_size);
1642 }
1643 end_time = TimeValue::Now();
1644 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001645 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Greg Clayton153ccd72011-08-10 02:10:13 +00001646 printf ("%u qSpeedTest(send=%-5u, recv=%-5u) in %llu.%9.9llu sec for %f packets/sec.\n",
Greg Clayton06d7cc82011-04-04 18:18:57 +00001647 num_packets,
1648 send_size,
1649 recv_size,
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001650 total_time_nsec / TimeValue::NanoSecPerSec,
1651 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton06d7cc82011-04-04 18:18:57 +00001652 packets_per_second);
1653 if (recv_size == 0)
1654 recv_size = 32;
1655 }
1656 if (send_size == 0)
1657 send_size = 32;
1658 }
1659 }
1660 else
1661 {
1662 start_time = TimeValue::Now();
1663 for (i=0; i<num_packets; ++i)
1664 {
1665 GetCurrentProcessID ();
1666 }
1667 end_time = TimeValue::Now();
1668 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001669 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Greg Clayton153ccd72011-08-10 02:10:13 +00001670 printf ("%u 'qC' packets packets in 0x%llu%9.9llu sec for %f packets/sec.\n",
Greg Clayton06d7cc82011-04-04 18:18:57 +00001671 num_packets,
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001672 total_time_nsec / TimeValue::NanoSecPerSec,
1673 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton06d7cc82011-04-04 18:18:57 +00001674 packets_per_second);
1675 }
1676}
1677
1678bool
1679GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size)
1680{
1681 StreamString packet;
1682 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size);
1683 uint32_t bytes_left = send_size;
1684 while (bytes_left > 0)
1685 {
1686 if (bytes_left >= 26)
1687 {
1688 packet.PutCString("abcdefghijklmnopqrstuvwxyz");
1689 bytes_left -= 26;
1690 }
1691 else
1692 {
1693 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz");
1694 bytes_left = 0;
1695 }
1696 }
1697
1698 StringExtractorGDBRemote response;
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001699 return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) > 0;
Greg Clayton06d7cc82011-04-04 18:18:57 +00001700 return false;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001701}
Greg Claytonb72d0f02011-04-12 05:54:46 +00001702
1703uint16_t
1704GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort ()
1705{
1706 StringExtractorGDBRemote response;
1707 if (SendPacketAndWaitForResponse("qLaunchGDBServer", strlen("qLaunchGDBServer"), response, false))
1708 {
1709 std::string name;
1710 std::string value;
1711 uint16_t port = 0;
1712 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
1713 while (response.GetNameColonValue(name, value))
1714 {
1715 if (name.size() == 4 && name.compare("port") == 0)
1716 port = Args::StringToUInt32(value.c_str(), 0, 0);
1717 if (name.size() == 3 && name.compare("pid") == 0)
1718 pid = Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0);
1719 }
1720 return port;
1721 }
1722 return 0;
1723}
1724
1725bool
1726GDBRemoteCommunicationClient::SetCurrentThread (int tid)
1727{
1728 if (m_curr_tid == tid)
1729 return true;
1730
1731 char packet[32];
1732 int packet_len;
1733 if (tid <= 0)
1734 packet_len = ::snprintf (packet, sizeof(packet), "Hg%i", tid);
1735 else
1736 packet_len = ::snprintf (packet, sizeof(packet), "Hg%x", tid);
1737 assert (packet_len + 1 < sizeof(packet));
1738 StringExtractorGDBRemote response;
1739 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
1740 {
1741 if (response.IsOKResponse())
1742 {
1743 m_curr_tid = tid;
1744 return true;
1745 }
1746 }
1747 return false;
1748}
1749
1750bool
1751GDBRemoteCommunicationClient::SetCurrentThreadForRun (int tid)
1752{
1753 if (m_curr_tid_run == tid)
1754 return true;
1755
1756 char packet[32];
1757 int packet_len;
1758 if (tid <= 0)
1759 packet_len = ::snprintf (packet, sizeof(packet), "Hc%i", tid);
1760 else
1761 packet_len = ::snprintf (packet, sizeof(packet), "Hc%x", tid);
1762
1763 assert (packet_len + 1 < sizeof(packet));
1764 StringExtractorGDBRemote response;
1765 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
1766 {
1767 if (response.IsOKResponse())
1768 {
1769 m_curr_tid_run = tid;
1770 return true;
1771 }
1772 }
1773 return false;
1774}
1775
1776bool
1777GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response)
1778{
1779 if (SendPacketAndWaitForResponse("?", 1, response, false))
1780 return response.IsNormalResponse();
1781 return false;
1782}
1783
1784bool
1785GDBRemoteCommunicationClient::GetThreadStopInfo (uint32_t tid, StringExtractorGDBRemote &response)
1786{
1787 if (m_supports_qThreadStopInfo)
1788 {
1789 char packet[256];
1790 int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%x", tid);
1791 assert (packet_len < sizeof(packet));
1792 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
1793 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001794 if (response.IsNormalResponse())
Greg Claytonb72d0f02011-04-12 05:54:46 +00001795 return true;
1796 else
1797 return false;
1798 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001799 else
1800 {
1801 m_supports_qThreadStopInfo = false;
1802 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001803 }
Greg Clayton139da722011-05-20 03:15:54 +00001804// if (SetCurrentThread (tid))
1805// return GetStopReply (response);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001806 return false;
1807}
1808
1809
1810uint8_t
1811GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length)
1812{
1813 switch (type)
1814 {
1815 case eBreakpointSoftware: if (!m_supports_z0) return UINT8_MAX; break;
1816 case eBreakpointHardware: if (!m_supports_z1) return UINT8_MAX; break;
1817 case eWatchpointWrite: if (!m_supports_z2) return UINT8_MAX; break;
1818 case eWatchpointRead: if (!m_supports_z3) return UINT8_MAX; break;
1819 case eWatchpointReadWrite: if (!m_supports_z4) return UINT8_MAX; break;
1820 default: return UINT8_MAX;
1821 }
1822
1823 char packet[64];
1824 const int packet_len = ::snprintf (packet,
1825 sizeof(packet),
1826 "%c%i,%llx,%x",
1827 insert ? 'Z' : 'z',
1828 type,
1829 addr,
1830 length);
1831
1832 assert (packet_len + 1 < sizeof(packet));
1833 StringExtractorGDBRemote response;
1834 if (SendPacketAndWaitForResponse(packet, packet_len, response, true))
1835 {
1836 if (response.IsOKResponse())
1837 return 0;
Greg Claytonb72d0f02011-04-12 05:54:46 +00001838 else if (response.IsErrorResponse())
1839 return response.GetError();
1840 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001841 else
1842 {
1843 switch (type)
1844 {
1845 case eBreakpointSoftware: m_supports_z0 = false; break;
1846 case eBreakpointHardware: m_supports_z1 = false; break;
1847 case eWatchpointWrite: m_supports_z2 = false; break;
1848 case eWatchpointRead: m_supports_z3 = false; break;
1849 case eWatchpointReadWrite: m_supports_z4 = false; break;
1850 default: break;
1851 }
1852 }
1853
Greg Claytonb72d0f02011-04-12 05:54:46 +00001854 return UINT8_MAX;
1855}
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001856
1857size_t
1858GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
1859 bool &sequence_mutex_unavailable)
1860{
1861 Mutex::Locker locker;
1862 thread_ids.clear();
1863
Greg Claytonae932352012-04-10 00:18:59 +00001864 if (TryLockSequenceMutex (locker))
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001865 {
1866 sequence_mutex_unavailable = false;
1867 StringExtractorGDBRemote response;
1868
Greg Clayton63afdb02011-06-17 01:22:15 +00001869 for (SendPacketNoLock ("qfThreadInfo", strlen("qfThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001870 response.IsNormalResponse();
Greg Clayton63afdb02011-06-17 01:22:15 +00001871 SendPacketNoLock ("qsThreadInfo", strlen("qsThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()))
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001872 {
1873 char ch = response.GetChar();
1874 if (ch == 'l')
1875 break;
1876 if (ch == 'm')
1877 {
1878 do
1879 {
1880 tid_t tid = response.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID);
1881
1882 if (tid != LLDB_INVALID_THREAD_ID)
1883 {
1884 thread_ids.push_back (tid);
1885 }
1886 ch = response.GetChar(); // Skip the command separator
1887 } while (ch == ','); // Make sure we got a comma separator
1888 }
1889 }
1890 }
1891 else
1892 {
1893 sequence_mutex_unavailable = true;
1894 }
1895 return thread_ids.size();
1896}