blob: 6774b5f807d94f60b52a34f9de112b7f8d43db27 [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 Claytonc8dd5702012-04-12 19:04:34 +0000262 if (GetSequenceMutex (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)
Greg Claytonc8dd5702012-04-12 19:04:34 +0000353 log->Printf("error: failed to get packet sequence mutex, 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
Greg Clayton61d043b2011-03-22 04:00:09 +0000364StateType
365GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse
366(
367 ProcessGDBRemote *process,
368 const char *payload,
369 size_t packet_length,
370 StringExtractorGDBRemote &response
371)
372{
373 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
374 if (log)
375 log->Printf ("GDBRemoteCommunicationClient::%s ()", __FUNCTION__);
376
377 Mutex::Locker locker(m_sequence_mutex);
378 StateType state = eStateRunning;
379
380 BroadcastEvent(eBroadcastBitRunPacketSent, NULL);
381 m_public_is_running.SetValue (true, eBroadcastNever);
382 // Set the starting continue packet into "continue_packet". This packet
383 // make change if we are interrupted and we continue after an async packet...
384 std::string continue_packet(payload, packet_length);
385
Greg Clayton628cead2011-05-19 03:54:16 +0000386 bool got_stdout = false;
387
Greg Clayton61d043b2011-03-22 04:00:09 +0000388 while (state == eStateRunning)
389 {
Greg Clayton628cead2011-05-19 03:54:16 +0000390 if (!got_stdout)
391 {
392 if (log)
393 log->Printf ("GDBRemoteCommunicationClient::%s () sending continue packet: %s", __FUNCTION__, continue_packet.c_str());
Greg Clayton516f0842012-04-11 00:24:49 +0000394 if (SendPacketNoLock(continue_packet.c_str(), continue_packet.size()) == 0)
Greg Clayton628cead2011-05-19 03:54:16 +0000395 state = eStateInvalid;
Greg Clayton61d043b2011-03-22 04:00:09 +0000396
Greg Claytonb7669b32011-10-27 22:04:16 +0000397 m_private_is_running.SetValue (true, eBroadcastAlways);
Greg Clayton628cead2011-05-19 03:54:16 +0000398 }
399
400 got_stdout = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000401
402 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000403 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(%s)", __FUNCTION__, continue_packet.c_str());
Greg Clayton61d043b2011-03-22 04:00:09 +0000404
Greg Clayton516f0842012-04-11 00:24:49 +0000405 if (WaitForPacketWithTimeoutMicroSecondsNoLock(response, UINT32_MAX))
Greg Clayton61d043b2011-03-22 04:00:09 +0000406 {
407 if (response.Empty())
408 state = eStateInvalid;
409 else
410 {
411 const char stop_type = response.GetChar();
412 if (log)
413 log->Printf ("GDBRemoteCommunicationClient::%s () got packet: %s", __FUNCTION__, response.GetStringRef().c_str());
414 switch (stop_type)
415 {
416 case 'T':
417 case 'S':
Greg Clayton61d043b2011-03-22 04:00:09 +0000418 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000419 if (process->GetStopID() == 0)
Greg Clayton61d043b2011-03-22 04:00:09 +0000420 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000421 if (process->GetID() == LLDB_INVALID_PROCESS_ID)
422 {
423 lldb::pid_t pid = GetCurrentProcessID ();
424 if (pid != LLDB_INVALID_PROCESS_ID)
425 process->SetID (pid);
426 }
427 process->BuildDynamicRegisterInfo (true);
Greg Clayton61d043b2011-03-22 04:00:09 +0000428 }
Greg Clayton05e4d972012-03-29 01:55:41 +0000429
430 // Privately notify any internal threads that we have stopped
431 // in case we wanted to interrupt our process, yet we might
432 // send a packet and continue without returning control to the
433 // user.
434 m_private_is_running.SetValue (false, eBroadcastAlways);
435
436 const uint8_t signo = response.GetHexU8 (UINT8_MAX);
437
438 bool continue_after_aync = false;
439 if (m_async_signal != -1 || m_async_packet_predicate.GetValue())
440 {
441 continue_after_aync = true;
442 // We sent an interrupt packet to stop the inferior process
443 // for an async signal or to send an async packet while running
444 // but we might have been single stepping and received the
445 // stop packet for the step instead of for the interrupt packet.
446 // Typically when an interrupt is sent a SIGINT or SIGSTOP
447 // is used, so if we get anything else, we need to try and
448 // get another stop reply packet that may have been sent
449 // due to sending the interrupt when the target is stopped
450 // which will just re-send a copy of the last stop reply
451 // packet. If we don't do this, then the reply for our
452 // async packet will be the repeat stop reply packet and cause
453 // a lot of trouble for us!
454 if (signo != SIGINT && signo != SIGSTOP)
455 {
456 continue_after_aync = false;
457
458 // We didn't get a a SIGINT or SIGSTOP, so try for a
459 // very brief time (1 ms) to get another stop reply
460 // packet to make sure it doesn't get in the way
461 StringExtractorGDBRemote extra_stop_reply_packet;
462 uint32_t timeout_usec = 1000;
463 if (WaitForPacketWithTimeoutMicroSecondsNoLock (extra_stop_reply_packet, timeout_usec))
464 {
465 switch (extra_stop_reply_packet.GetChar())
466 {
467 case 'T':
468 case 'S':
469 // We did get an extra stop reply, which means
470 // our interrupt didn't stop the target so we
471 // shouldn't continue after the async signal
472 // or packet is sent...
473 continue_after_aync = false;
474 break;
475 }
476 }
477 }
478 }
479
480 if (m_async_signal != -1)
481 {
482 if (log)
483 log->Printf ("async: send signo = %s", Host::GetSignalAsCString (m_async_signal));
484
485 // Save off the async signal we are supposed to send
486 const int async_signal = m_async_signal;
487 // Clear the async signal member so we don't end up
488 // sending the signal multiple times...
489 m_async_signal = -1;
490 // Check which signal we stopped with
491 if (signo == async_signal)
492 {
493 if (log)
494 log->Printf ("async: stopped with signal %s, we are done running", Host::GetSignalAsCString (signo));
495
496 // We already stopped with a signal that we wanted
497 // to stop with, so we are done
498 }
499 else
500 {
501 // We stopped with a different signal that the one
502 // we wanted to stop with, so now we must resume
503 // with the signal we want
504 char signal_packet[32];
505 int signal_packet_len = 0;
506 signal_packet_len = ::snprintf (signal_packet,
507 sizeof (signal_packet),
508 "C%2.2x",
509 async_signal);
510
511 if (log)
512 log->Printf ("async: stopped with signal %s, resume with %s",
513 Host::GetSignalAsCString (signo),
514 Host::GetSignalAsCString (async_signal));
515
516 // Set the continue packet to resume even if the
517 // interrupt didn't cause our stop (ignore continue_after_aync)
518 continue_packet.assign(signal_packet, signal_packet_len);
519 continue;
520 }
521 }
522 else if (m_async_packet_predicate.GetValue())
523 {
524 LogSP packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
525
526 // We are supposed to send an asynchronous packet while
527 // we are running.
528 m_async_response.Clear();
529 if (m_async_packet.empty())
530 {
531 if (packet_log)
532 packet_log->Printf ("async: error: empty async packet");
533
534 }
535 else
536 {
537 if (packet_log)
538 packet_log->Printf ("async: sending packet");
539
540 SendPacketAndWaitForResponse (&m_async_packet[0],
541 m_async_packet.size(),
542 m_async_response,
543 false);
544 }
545 // Let the other thread that was trying to send the async
546 // packet know that the packet has been sent and response is
547 // ready...
548 m_async_packet_predicate.SetValue(false, eBroadcastAlways);
549
550 if (packet_log)
551 packet_log->Printf ("async: sent packet, continue_after_aync = %i", continue_after_aync);
552
553 // Set the continue packet to resume if our interrupt
554 // for the async packet did cause the stop
555 if (continue_after_aync)
556 {
557 continue_packet.assign (1, 'c');
558 continue;
559 }
560 }
561 // Stop with signal and thread info
562 state = eStateStopped;
Greg Clayton61d043b2011-03-22 04:00:09 +0000563 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000564 break;
565
566 case 'W':
567 case 'X':
568 // process exited
569 state = eStateExited;
570 break;
571
572 case 'O':
573 // STDOUT
574 {
Greg Clayton628cead2011-05-19 03:54:16 +0000575 got_stdout = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000576 std::string inferior_stdout;
577 inferior_stdout.reserve(response.GetBytesLeft () / 2);
578 char ch;
579 while ((ch = response.GetHexU8()) != '\0')
580 inferior_stdout.append(1, ch);
581 process->AppendSTDOUT (inferior_stdout.c_str(), inferior_stdout.size());
582 }
583 break;
584
585 case 'E':
586 // ERROR
587 state = eStateInvalid;
588 break;
589
590 default:
591 if (log)
592 log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__);
593 state = eStateInvalid;
594 break;
595 }
596 }
597 }
598 else
599 {
600 if (log)
601 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__);
602 state = eStateInvalid;
603 }
604 }
605 if (log)
606 log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state));
607 response.SetFilePos(0);
608 m_private_is_running.SetValue (false, eBroadcastAlways);
609 m_public_is_running.SetValue (false, eBroadcastAlways);
610 return state;
611}
612
613bool
614GDBRemoteCommunicationClient::SendAsyncSignal (int signo)
615{
Greg Clayton05e4d972012-03-29 01:55:41 +0000616 Mutex::Locker async_locker (m_async_mutex);
Greg Clayton61d043b2011-03-22 04:00:09 +0000617 m_async_signal = signo;
618 bool timed_out = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000619 Mutex::Locker locker;
Greg Clayton05e4d972012-03-29 01:55:41 +0000620 if (SendInterrupt (locker, 1, timed_out))
Greg Clayton61d043b2011-03-22 04:00:09 +0000621 return true;
622 m_async_signal = -1;
623 return false;
624}
625
Greg Clayton516f0842012-04-11 00:24:49 +0000626// This function takes a mutex locker as a parameter in case the GetSequenceMutex
Greg Clayton61d043b2011-03-22 04:00:09 +0000627// actually succeeds. If it doesn't succeed in acquiring the sequence mutex
628// (the expected result), then it will send the halt packet. If it does succeed
629// then the caller that requested the interrupt will want to keep the sequence
630// locked down so that no one else can send packets while the caller has control.
631// This function usually gets called when we are running and need to stop the
632// target. It can also be used when we are running and and we need to do something
633// else (like read/write memory), so we need to interrupt the running process
634// (gdb remote protocol requires this), and do what we need to do, then resume.
635
636bool
Greg Clayton05e4d972012-03-29 01:55:41 +0000637GDBRemoteCommunicationClient::SendInterrupt
Greg Clayton61d043b2011-03-22 04:00:09 +0000638(
639 Mutex::Locker& locker,
640 uint32_t seconds_to_wait_for_stop,
Greg Clayton61d043b2011-03-22 04:00:09 +0000641 bool &timed_out
642)
643{
Greg Clayton05e4d972012-03-29 01:55:41 +0000644 m_interrupt_sent = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000645 timed_out = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000646 LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Clayton61d043b2011-03-22 04:00:09 +0000647
648 if (IsRunning())
649 {
650 // Only send an interrupt if our debugserver is running...
Greg Claytonc8dd5702012-04-12 19:04:34 +0000651 if (GetSequenceMutex (locker))
Greg Clayton516f0842012-04-11 00:24:49 +0000652 {
653 if (log)
654 log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt");
655 }
656 else
Greg Clayton61d043b2011-03-22 04:00:09 +0000657 {
658 // Someone has the mutex locked waiting for a response or for the
659 // inferior to stop, so send the interrupt on the down low...
660 char ctrl_c = '\x03';
661 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton61d043b2011-03-22 04:00:09 +0000662 size_t bytes_written = Write (&ctrl_c, 1, status, NULL);
Greg Clayton05e4d972012-03-29 01:55:41 +0000663 if (log)
664 log->PutCString("send packet: \\x03");
Greg Clayton61d043b2011-03-22 04:00:09 +0000665 if (bytes_written > 0)
666 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000667 m_interrupt_sent = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000668 if (seconds_to_wait_for_stop)
669 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000670 TimeValue timeout;
671 if (seconds_to_wait_for_stop)
672 {
673 timeout = TimeValue::Now();
674 timeout.OffsetWithSeconds (seconds_to_wait_for_stop);
675 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000676 if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out))
677 {
678 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000679 log->PutCString ("SendInterrupt () - sent interrupt, private state stopped");
Greg Clayton61d043b2011-03-22 04:00:09 +0000680 return true;
681 }
682 else
683 {
684 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000685 log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume");
Greg Clayton61d043b2011-03-22 04:00:09 +0000686 }
687 }
688 else
689 {
690 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000691 log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop...");
Greg Clayton61d043b2011-03-22 04:00:09 +0000692 return true;
693 }
694 }
695 else
696 {
697 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000698 log->Printf ("SendInterrupt () - failed to write interrupt");
Greg Clayton61d043b2011-03-22 04:00:09 +0000699 }
700 return false;
701 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000702 }
Greg Clayton05e4d972012-03-29 01:55:41 +0000703 else
704 {
705 if (log)
706 log->Printf ("SendInterrupt () - not running");
707 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000708 return true;
709}
710
711lldb::pid_t
712GDBRemoteCommunicationClient::GetCurrentProcessID ()
713{
714 StringExtractorGDBRemote response;
715 if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false))
716 {
717 if (response.GetChar() == 'Q')
718 if (response.GetChar() == 'C')
719 return response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID);
720 }
721 return LLDB_INVALID_PROCESS_ID;
722}
723
724bool
725GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str)
726{
727 error_str.clear();
728 StringExtractorGDBRemote response;
729 if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false))
730 {
731 if (response.IsOKResponse())
732 return true;
733 if (response.GetChar() == 'E')
734 {
735 // A string the describes what failed when launching...
736 error_str = response.GetStringRef().substr(1);
737 }
738 else
739 {
740 error_str.assign ("unknown error occurred launching process");
741 }
742 }
743 else
744 {
745 error_str.assign ("failed to send the qLaunchSuccess packet");
746 }
747 return false;
748}
749
750int
751GDBRemoteCommunicationClient::SendArgumentsPacket (char const *argv[])
752{
753 if (argv && argv[0])
754 {
755 StreamString packet;
756 packet.PutChar('A');
757 const char *arg;
758 for (uint32_t i = 0; (arg = argv[i]) != NULL; ++i)
759 {
760 const int arg_len = strlen(arg);
761 if (i > 0)
762 packet.PutChar(',');
763 packet.Printf("%i,%i,", arg_len * 2, i);
764 packet.PutBytesAsRawHex8 (arg, arg_len);
765 }
766
767 StringExtractorGDBRemote response;
768 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
769 {
770 if (response.IsOKResponse())
771 return 0;
772 uint8_t error = response.GetError();
773 if (error)
774 return error;
775 }
776 }
777 return -1;
778}
779
780int
781GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value)
782{
783 if (name_equal_value && name_equal_value[0])
784 {
785 StreamString packet;
786 packet.Printf("QEnvironment:%s", name_equal_value);
787 StringExtractorGDBRemote response;
788 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
789 {
790 if (response.IsOKResponse())
791 return 0;
792 uint8_t error = response.GetError();
793 if (error)
794 return error;
795 }
796 }
797 return -1;
798}
799
Greg Claytona4582402011-05-08 04:53:50 +0000800int
801GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch)
802{
803 if (arch && arch[0])
804 {
805 StreamString packet;
806 packet.Printf("QLaunchArch:%s", arch);
807 StringExtractorGDBRemote response;
808 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
809 {
810 if (response.IsOKResponse())
811 return 0;
812 uint8_t error = response.GetError();
813 if (error)
814 return error;
815 }
816 }
817 return -1;
818}
819
Greg Clayton61d043b2011-03-22 04:00:09 +0000820bool
Greg Clayton58e26e02011-03-24 04:28:38 +0000821GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major,
822 uint32_t &minor,
823 uint32_t &update)
824{
825 if (GetHostInfo ())
826 {
827 if (m_os_version_major != UINT32_MAX)
828 {
829 major = m_os_version_major;
830 minor = m_os_version_minor;
831 update = m_os_version_update;
832 return true;
833 }
834 }
835 return false;
836}
837
838bool
839GDBRemoteCommunicationClient::GetOSBuildString (std::string &s)
840{
841 if (GetHostInfo ())
842 {
843 if (!m_os_build.empty())
844 {
845 s = m_os_build;
846 return true;
847 }
848 }
849 s.clear();
850 return false;
851}
852
853
854bool
855GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s)
856{
857 if (GetHostInfo ())
858 {
859 if (!m_os_kernel.empty())
860 {
861 s = m_os_kernel;
862 return true;
863 }
864 }
865 s.clear();
866 return false;
867}
868
869bool
870GDBRemoteCommunicationClient::GetHostname (std::string &s)
871{
872 if (GetHostInfo ())
873 {
874 if (!m_hostname.empty())
875 {
876 s = m_hostname;
877 return true;
878 }
879 }
880 s.clear();
881 return false;
882}
883
884ArchSpec
885GDBRemoteCommunicationClient::GetSystemArchitecture ()
886{
887 if (GetHostInfo ())
888 return m_host_arch;
889 return ArchSpec();
890}
891
892
893bool
Greg Clayton06d7cc82011-04-04 18:18:57 +0000894GDBRemoteCommunicationClient::GetHostInfo (bool force)
Greg Clayton61d043b2011-03-22 04:00:09 +0000895{
Greg Clayton06d7cc82011-04-04 18:18:57 +0000896 if (force || m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton61d043b2011-03-22 04:00:09 +0000897 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000898 m_qHostInfo_is_valid = eLazyBoolNo;
Greg Clayton61d043b2011-03-22 04:00:09 +0000899 StringExtractorGDBRemote response;
900 if (SendPacketAndWaitForResponse ("qHostInfo", response, false))
901 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +0000902 if (response.IsNormalResponse())
Greg Claytoncb8977d2011-03-23 00:09:55 +0000903 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000904 std::string name;
905 std::string value;
906 uint32_t cpu = LLDB_INVALID_CPUTYPE;
907 uint32_t sub = 0;
908 std::string arch_name;
909 std::string os_name;
910 std::string vendor_name;
911 std::string triple;
912 uint32_t pointer_byte_size = 0;
913 StringExtractor extractor;
914 ByteOrder byte_order = eByteOrderInvalid;
915 uint32_t num_keys_decoded = 0;
916 while (response.GetNameColonValue(name, value))
Greg Claytoncb8977d2011-03-23 00:09:55 +0000917 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000918 if (name.compare("cputype") == 0)
Greg Clayton58e26e02011-03-24 04:28:38 +0000919 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000920 // exception type in big endian hex
921 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0);
922 if (cpu != LLDB_INVALID_CPUTYPE)
923 ++num_keys_decoded;
924 }
925 else if (name.compare("cpusubtype") == 0)
926 {
927 // exception count in big endian hex
928 sub = Args::StringToUInt32 (value.c_str(), 0, 0);
929 if (sub != 0)
930 ++num_keys_decoded;
931 }
932 else if (name.compare("arch") == 0)
933 {
934 arch_name.swap (value);
935 ++num_keys_decoded;
936 }
937 else if (name.compare("triple") == 0)
938 {
939 // The triple comes as ASCII hex bytes since it contains '-' chars
940 extractor.GetStringRef().swap(value);
941 extractor.SetFilePos(0);
942 extractor.GetHexByteString (triple);
943 ++num_keys_decoded;
944 }
945 else if (name.compare("os_build") == 0)
946 {
947 extractor.GetStringRef().swap(value);
948 extractor.SetFilePos(0);
949 extractor.GetHexByteString (m_os_build);
950 ++num_keys_decoded;
951 }
952 else if (name.compare("hostname") == 0)
953 {
954 extractor.GetStringRef().swap(value);
955 extractor.SetFilePos(0);
956 extractor.GetHexByteString (m_hostname);
957 ++num_keys_decoded;
958 }
959 else if (name.compare("os_kernel") == 0)
960 {
961 extractor.GetStringRef().swap(value);
962 extractor.SetFilePos(0);
963 extractor.GetHexByteString (m_os_kernel);
964 ++num_keys_decoded;
965 }
966 else if (name.compare("ostype") == 0)
967 {
968 os_name.swap (value);
969 ++num_keys_decoded;
970 }
971 else if (name.compare("vendor") == 0)
972 {
973 vendor_name.swap(value);
974 ++num_keys_decoded;
975 }
976 else if (name.compare("endian") == 0)
977 {
978 ++num_keys_decoded;
979 if (value.compare("little") == 0)
980 byte_order = eByteOrderLittle;
981 else if (value.compare("big") == 0)
982 byte_order = eByteOrderBig;
983 else if (value.compare("pdp") == 0)
984 byte_order = eByteOrderPDP;
985 else
986 --num_keys_decoded;
987 }
988 else if (name.compare("ptrsize") == 0)
989 {
990 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 0);
991 if (pointer_byte_size != 0)
992 ++num_keys_decoded;
993 }
994 else if (name.compare("os_version") == 0)
995 {
996 Args::StringToVersion (value.c_str(),
997 m_os_version_major,
998 m_os_version_minor,
999 m_os_version_update);
1000 if (m_os_version_major != UINT32_MAX)
1001 ++num_keys_decoded;
1002 }
1003 }
1004
1005 if (num_keys_decoded > 0)
1006 m_qHostInfo_is_valid = eLazyBoolYes;
1007
1008 if (triple.empty())
1009 {
1010 if (arch_name.empty())
1011 {
1012 if (cpu != LLDB_INVALID_CPUTYPE)
1013 {
1014 m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1015 if (pointer_byte_size)
1016 {
1017 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1018 }
1019 if (byte_order != eByteOrderInvalid)
1020 {
1021 assert (byte_order == m_host_arch.GetByteOrder());
1022 }
Greg Claytonb170aee2012-05-08 01:45:38 +00001023
1024 if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0)
1025 {
1026 switch (m_host_arch.GetMachine())
1027 {
1028 case llvm::Triple::arm:
1029 case llvm::Triple::thumb:
1030 os_name = "ios";
1031 break;
1032 default:
1033 os_name = "macosx";
1034 break;
1035 }
1036 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001037 if (!vendor_name.empty())
1038 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1039 if (!os_name.empty())
Greg Clayton89798cc2011-09-15 00:21:03 +00001040 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001041
1042 }
1043 }
1044 else
1045 {
1046 std::string triple;
1047 triple += arch_name;
Greg Claytonb170aee2012-05-08 01:45:38 +00001048 if (!vendor_name.empty() || !os_name.empty())
1049 {
1050 triple += '-';
1051 if (vendor_name.empty())
1052 triple += "unknown";
1053 else
1054 triple += vendor_name;
1055 triple += '-';
1056 if (os_name.empty())
1057 triple += "unknown";
1058 else
1059 triple += os_name;
1060 }
1061 m_host_arch.SetTriple (triple.c_str());
1062
1063 llvm::Triple &host_triple = m_host_arch.GetTriple();
1064 if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin)
1065 {
1066 switch (m_host_arch.GetMachine())
1067 {
1068 case llvm::Triple::arm:
1069 case llvm::Triple::thumb:
1070 host_triple.setOS(llvm::Triple::IOS);
1071 break;
1072 default:
1073 host_triple.setOS(llvm::Triple::MacOSX);
1074 break;
1075 }
1076 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001077 if (pointer_byte_size)
1078 {
1079 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1080 }
1081 if (byte_order != eByteOrderInvalid)
1082 {
1083 assert (byte_order == m_host_arch.GetByteOrder());
1084 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001085
Greg Clayton58e26e02011-03-24 04:28:38 +00001086 }
1087 }
1088 else
1089 {
Greg Claytonb170aee2012-05-08 01:45:38 +00001090 m_host_arch.SetTriple (triple.c_str());
Greg Claytoncb8977d2011-03-23 00:09:55 +00001091 if (pointer_byte_size)
1092 {
1093 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1094 }
1095 if (byte_order != eByteOrderInvalid)
1096 {
1097 assert (byte_order == m_host_arch.GetByteOrder());
1098 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001099 }
Greg Claytoncb8977d2011-03-23 00:09:55 +00001100 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001101 }
1102 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001103 return m_qHostInfo_is_valid == eLazyBoolYes;
Greg Clayton61d043b2011-03-22 04:00:09 +00001104}
1105
1106int
1107GDBRemoteCommunicationClient::SendAttach
1108(
1109 lldb::pid_t pid,
1110 StringExtractorGDBRemote& response
1111)
1112{
1113 if (pid != LLDB_INVALID_PROCESS_ID)
1114 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001115 char packet[64];
Greg Claytond9919d32011-12-01 23:28:38 +00001116 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%llx", pid);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001117 assert (packet_len < sizeof(packet));
1118 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton61d043b2011-03-22 04:00:09 +00001119 {
1120 if (response.IsErrorResponse())
1121 return response.GetError();
1122 return 0;
1123 }
1124 }
1125 return -1;
1126}
1127
1128const lldb_private::ArchSpec &
1129GDBRemoteCommunicationClient::GetHostArchitecture ()
1130{
Greg Clayton24bc5d92011-03-30 18:16:51 +00001131 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton61d043b2011-03-22 04:00:09 +00001132 GetHostInfo ();
Greg Claytoncb8977d2011-03-23 00:09:55 +00001133 return m_host_arch;
Greg Clayton61d043b2011-03-22 04:00:09 +00001134}
1135
1136addr_t
1137GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions)
1138{
Greg Clayton2f085c62011-05-15 01:25:55 +00001139 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton61d043b2011-03-22 04:00:09 +00001140 {
Greg Clayton2f085c62011-05-15 01:25:55 +00001141 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton989816b2011-05-14 01:50:35 +00001142 char packet[64];
1143 const int packet_len = ::snprintf (packet, sizeof(packet), "_M%zx,%s%s%s", size,
1144 permissions & lldb::ePermissionsReadable ? "r" : "",
1145 permissions & lldb::ePermissionsWritable ? "w" : "",
1146 permissions & lldb::ePermissionsExecutable ? "x" : "");
1147 assert (packet_len < sizeof(packet));
1148 StringExtractorGDBRemote response;
1149 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1150 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001151 if (!response.IsErrorResponse())
Greg Clayton989816b2011-05-14 01:50:35 +00001152 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1153 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001154 else
1155 {
1156 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1157 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001158 }
1159 return LLDB_INVALID_ADDRESS;
1160}
1161
1162bool
1163GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr)
1164{
Greg Clayton2f085c62011-05-15 01:25:55 +00001165 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton61d043b2011-03-22 04:00:09 +00001166 {
Greg Clayton2f085c62011-05-15 01:25:55 +00001167 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton989816b2011-05-14 01:50:35 +00001168 char packet[64];
1169 const int packet_len = ::snprintf(packet, sizeof(packet), "_m%llx", (uint64_t)addr);
1170 assert (packet_len < sizeof(packet));
1171 StringExtractorGDBRemote response;
1172 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1173 {
1174 if (response.IsOKResponse())
1175 return true;
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001176 }
1177 else
1178 {
1179 m_supports_alloc_dealloc_memory = eLazyBoolNo;
Greg Clayton989816b2011-05-14 01:50:35 +00001180 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001181 }
1182 return false;
1183}
1184
Greg Clayton516f0842012-04-11 00:24:49 +00001185bool
1186GDBRemoteCommunicationClient::Detach ()
1187{
1188 return SendPacket ("D", 1) > 0;
1189}
1190
Greg Claytona9385532011-11-18 07:03:08 +00001191Error
1192GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr,
1193 lldb_private::MemoryRegionInfo &region_info)
1194{
1195 Error error;
1196 region_info.Clear();
1197
1198 if (m_supports_memory_region_info != eLazyBoolNo)
1199 {
1200 m_supports_memory_region_info = eLazyBoolYes;
1201 char packet[64];
1202 const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%llx", (uint64_t)addr);
1203 assert (packet_len < sizeof(packet));
1204 StringExtractorGDBRemote response;
1205 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1206 {
1207 std::string name;
1208 std::string value;
1209 addr_t addr_value;
1210 bool success = true;
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001211 bool saw_permissions = false;
Greg Claytona9385532011-11-18 07:03:08 +00001212 while (success && response.GetNameColonValue(name, value))
1213 {
1214 if (name.compare ("start") == 0)
1215 {
1216 addr_value = Args::StringToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success);
1217 if (success)
1218 region_info.GetRange().SetRangeBase(addr_value);
1219 }
1220 else if (name.compare ("size") == 0)
1221 {
1222 addr_value = Args::StringToUInt64(value.c_str(), 0, 16, &success);
1223 if (success)
1224 region_info.GetRange().SetByteSize (addr_value);
1225 }
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001226 else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid())
Greg Claytona9385532011-11-18 07:03:08 +00001227 {
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001228 saw_permissions = true;
1229 if (region_info.GetRange().Contains (addr))
1230 {
1231 if (value.find('r') != std::string::npos)
1232 region_info.SetReadable (MemoryRegionInfo::eYes);
1233 else
1234 region_info.SetReadable (MemoryRegionInfo::eNo);
1235
1236 if (value.find('w') != std::string::npos)
1237 region_info.SetWritable (MemoryRegionInfo::eYes);
1238 else
1239 region_info.SetWritable (MemoryRegionInfo::eNo);
1240
1241 if (value.find('x') != std::string::npos)
1242 region_info.SetExecutable (MemoryRegionInfo::eYes);
1243 else
1244 region_info.SetExecutable (MemoryRegionInfo::eNo);
1245 }
1246 else
1247 {
1248 // The reported region does not contain this address -- we're looking at an unmapped page
1249 region_info.SetReadable (MemoryRegionInfo::eNo);
1250 region_info.SetWritable (MemoryRegionInfo::eNo);
1251 region_info.SetExecutable (MemoryRegionInfo::eNo);
1252 }
Greg Claytona9385532011-11-18 07:03:08 +00001253 }
1254 else if (name.compare ("error") == 0)
1255 {
1256 StringExtractorGDBRemote name_extractor;
1257 // Swap "value" over into "name_extractor"
1258 name_extractor.GetStringRef().swap(value);
1259 // Now convert the HEX bytes into a string value
1260 name_extractor.GetHexByteString (value);
1261 error.SetErrorString(value.c_str());
1262 }
1263 }
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001264
1265 // We got a valid address range back but no permissions -- which means this is an unmapped page
1266 if (region_info.GetRange().IsValid() && saw_permissions == false)
1267 {
1268 region_info.SetReadable (MemoryRegionInfo::eNo);
1269 region_info.SetWritable (MemoryRegionInfo::eNo);
1270 region_info.SetExecutable (MemoryRegionInfo::eNo);
1271 }
Greg Claytona9385532011-11-18 07:03:08 +00001272 }
1273 else
1274 {
1275 m_supports_memory_region_info = eLazyBoolNo;
1276 }
1277 }
1278
1279 if (m_supports_memory_region_info == eLazyBoolNo)
1280 {
1281 error.SetErrorString("qMemoryRegionInfo is not supported");
1282 }
1283 if (error.Fail())
1284 region_info.Clear();
1285 return error;
1286
1287}
1288
1289
Greg Clayton61d043b2011-03-22 04:00:09 +00001290int
1291GDBRemoteCommunicationClient::SetSTDIN (char const *path)
1292{
1293 if (path && path[0])
1294 {
1295 StreamString packet;
1296 packet.PutCString("QSetSTDIN:");
1297 packet.PutBytesAsRawHex8(path, strlen(path));
1298
1299 StringExtractorGDBRemote response;
1300 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1301 {
1302 if (response.IsOKResponse())
1303 return 0;
1304 uint8_t error = response.GetError();
1305 if (error)
1306 return error;
1307 }
1308 }
1309 return -1;
1310}
1311
1312int
1313GDBRemoteCommunicationClient::SetSTDOUT (char const *path)
1314{
1315 if (path && path[0])
1316 {
1317 StreamString packet;
1318 packet.PutCString("QSetSTDOUT:");
1319 packet.PutBytesAsRawHex8(path, strlen(path));
1320
1321 StringExtractorGDBRemote response;
1322 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1323 {
1324 if (response.IsOKResponse())
1325 return 0;
1326 uint8_t error = response.GetError();
1327 if (error)
1328 return error;
1329 }
1330 }
1331 return -1;
1332}
1333
1334int
1335GDBRemoteCommunicationClient::SetSTDERR (char const *path)
1336{
1337 if (path && path[0])
1338 {
1339 StreamString packet;
1340 packet.PutCString("QSetSTDERR:");
1341 packet.PutBytesAsRawHex8(path, strlen(path));
1342
1343 StringExtractorGDBRemote response;
1344 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1345 {
1346 if (response.IsOKResponse())
1347 return 0;
1348 uint8_t error = response.GetError();
1349 if (error)
1350 return error;
1351 }
1352 }
1353 return -1;
1354}
1355
1356int
1357GDBRemoteCommunicationClient::SetWorkingDir (char const *path)
1358{
1359 if (path && path[0])
1360 {
1361 StreamString packet;
1362 packet.PutCString("QSetWorkingDir:");
1363 packet.PutBytesAsRawHex8(path, strlen(path));
1364
1365 StringExtractorGDBRemote response;
1366 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1367 {
1368 if (response.IsOKResponse())
1369 return 0;
1370 uint8_t error = response.GetError();
1371 if (error)
1372 return error;
1373 }
1374 }
1375 return -1;
1376}
1377
1378int
1379GDBRemoteCommunicationClient::SetDisableASLR (bool enable)
1380{
Greg Clayton24bc5d92011-03-30 18:16:51 +00001381 char packet[32];
1382 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0);
1383 assert (packet_len < sizeof(packet));
Greg Clayton61d043b2011-03-22 04:00:09 +00001384 StringExtractorGDBRemote response;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001385 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton61d043b2011-03-22 04:00:09 +00001386 {
1387 if (response.IsOKResponse())
1388 return 0;
1389 uint8_t error = response.GetError();
1390 if (error)
1391 return error;
1392 }
1393 return -1;
1394}
Greg Clayton24bc5d92011-03-30 18:16:51 +00001395
1396bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001397GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001398{
1399 if (response.IsNormalResponse())
1400 {
1401 std::string name;
1402 std::string value;
1403 StringExtractor extractor;
1404
1405 while (response.GetNameColonValue(name, value))
1406 {
1407 if (name.compare("pid") == 0)
1408 {
1409 process_info.SetProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1410 }
1411 else if (name.compare("ppid") == 0)
1412 {
1413 process_info.SetParentProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1414 }
1415 else if (name.compare("uid") == 0)
1416 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001417 process_info.SetUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001418 }
1419 else if (name.compare("euid") == 0)
1420 {
1421 process_info.SetEffectiveUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1422 }
1423 else if (name.compare("gid") == 0)
1424 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001425 process_info.SetGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001426 }
1427 else if (name.compare("egid") == 0)
1428 {
1429 process_info.SetEffectiveGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1430 }
1431 else if (name.compare("triple") == 0)
1432 {
1433 // The triple comes as ASCII hex bytes since it contains '-' chars
1434 extractor.GetStringRef().swap(value);
1435 extractor.SetFilePos(0);
1436 extractor.GetHexByteString (value);
Greg Claytonb170aee2012-05-08 01:45:38 +00001437 process_info.GetArchitecture ().SetTriple (value.c_str());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001438 }
1439 else if (name.compare("name") == 0)
1440 {
1441 StringExtractor extractor;
Filipe Cabecinhase61ec7f2012-05-07 09:30:51 +00001442 // The process name from ASCII hex bytes since we can't
Greg Clayton24bc5d92011-03-30 18:16:51 +00001443 // control the characters in a process name
1444 extractor.GetStringRef().swap(value);
1445 extractor.SetFilePos(0);
1446 extractor.GetHexByteString (value);
Greg Clayton527154d2011-11-15 03:53:30 +00001447 process_info.GetExecutableFile().SetFile (value.c_str(), false);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001448 }
1449 }
1450
1451 if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
1452 return true;
1453 }
1454 return false;
1455}
1456
1457bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001458GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001459{
1460 process_info.Clear();
1461
1462 if (m_supports_qProcessInfoPID)
1463 {
1464 char packet[32];
Greg Claytond9919d32011-12-01 23:28:38 +00001465 const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%llu", pid);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001466 assert (packet_len < sizeof(packet));
1467 StringExtractorGDBRemote response;
1468 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1469 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001470 return DecodeProcessInfoResponse (response, process_info);
1471 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001472 else
1473 {
1474 m_supports_qProcessInfoPID = false;
1475 return false;
1476 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001477 }
1478 return false;
1479}
1480
1481uint32_t
Greg Claytonb72d0f02011-04-12 05:54:46 +00001482GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1483 ProcessInstanceInfoList &process_infos)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001484{
1485 process_infos.Clear();
1486
1487 if (m_supports_qfProcessInfo)
1488 {
1489 StreamString packet;
1490 packet.PutCString ("qfProcessInfo");
1491 if (!match_info.MatchAllProcesses())
1492 {
1493 packet.PutChar (':');
1494 const char *name = match_info.GetProcessInfo().GetName();
1495 bool has_name_match = false;
1496 if (name && name[0])
1497 {
1498 has_name_match = true;
1499 NameMatchType name_match_type = match_info.GetNameMatchType();
1500 switch (name_match_type)
1501 {
1502 case eNameMatchIgnore:
1503 has_name_match = false;
1504 break;
1505
1506 case eNameMatchEquals:
1507 packet.PutCString ("name_match:equals;");
1508 break;
1509
1510 case eNameMatchContains:
1511 packet.PutCString ("name_match:contains;");
1512 break;
1513
1514 case eNameMatchStartsWith:
1515 packet.PutCString ("name_match:starts_with;");
1516 break;
1517
1518 case eNameMatchEndsWith:
1519 packet.PutCString ("name_match:ends_with;");
1520 break;
1521
1522 case eNameMatchRegularExpression:
1523 packet.PutCString ("name_match:regex;");
1524 break;
1525 }
1526 if (has_name_match)
1527 {
1528 packet.PutCString ("name:");
1529 packet.PutBytesAsRawHex8(name, ::strlen(name));
1530 packet.PutChar (';');
1531 }
1532 }
1533
1534 if (match_info.GetProcessInfo().ProcessIDIsValid())
Greg Claytond9919d32011-12-01 23:28:38 +00001535 packet.Printf("pid:%llu;",match_info.GetProcessInfo().GetProcessID());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001536 if (match_info.GetProcessInfo().ParentProcessIDIsValid())
Greg Claytond9919d32011-12-01 23:28:38 +00001537 packet.Printf("parent_pid:%llu;",match_info.GetProcessInfo().GetParentProcessID());
Greg Claytonb72d0f02011-04-12 05:54:46 +00001538 if (match_info.GetProcessInfo().UserIDIsValid())
1539 packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID());
1540 if (match_info.GetProcessInfo().GroupIDIsValid())
1541 packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001542 if (match_info.GetProcessInfo().EffectiveUserIDIsValid())
1543 packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID());
1544 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1545 packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID());
1546 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1547 packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0);
1548 if (match_info.GetProcessInfo().GetArchitecture().IsValid())
1549 {
1550 const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture();
1551 const llvm::Triple &triple = match_arch.GetTriple();
1552 packet.PutCString("triple:");
1553 packet.PutCStringAsRawHex8(triple.getTriple().c_str());
1554 packet.PutChar (';');
1555 }
1556 }
1557 StringExtractorGDBRemote response;
1558 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1559 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001560 do
1561 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001562 ProcessInstanceInfo process_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001563 if (!DecodeProcessInfoResponse (response, process_info))
1564 break;
1565 process_infos.Append(process_info);
1566 response.GetStringRef().clear();
1567 response.SetFilePos(0);
1568 } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false));
1569 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001570 else
1571 {
1572 m_supports_qfProcessInfo = false;
1573 return 0;
1574 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001575 }
1576 return process_infos.GetSize();
1577
1578}
1579
1580bool
1581GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name)
1582{
1583 if (m_supports_qUserName)
1584 {
1585 char packet[32];
1586 const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid);
1587 assert (packet_len < sizeof(packet));
1588 StringExtractorGDBRemote response;
1589 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1590 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001591 if (response.IsNormalResponse())
1592 {
1593 // Make sure we parsed the right number of characters. The response is
1594 // the hex encoded user name and should make up the entire packet.
1595 // If there are any non-hex ASCII bytes, the length won't match below..
1596 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
1597 return true;
1598 }
1599 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001600 else
1601 {
1602 m_supports_qUserName = false;
1603 return false;
1604 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001605 }
1606 return false;
1607
1608}
1609
1610bool
1611GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name)
1612{
1613 if (m_supports_qGroupName)
1614 {
1615 char packet[32];
1616 const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid);
1617 assert (packet_len < sizeof(packet));
1618 StringExtractorGDBRemote response;
1619 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1620 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001621 if (response.IsNormalResponse())
1622 {
1623 // Make sure we parsed the right number of characters. The response is
1624 // the hex encoded group name and should make up the entire packet.
1625 // If there are any non-hex ASCII bytes, the length won't match below..
1626 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
1627 return true;
1628 }
1629 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001630 else
1631 {
1632 m_supports_qGroupName = false;
1633 return false;
1634 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001635 }
1636 return false;
Greg Clayton06d7cc82011-04-04 18:18:57 +00001637}
Greg Clayton24bc5d92011-03-30 18:16:51 +00001638
Greg Clayton06d7cc82011-04-04 18:18:57 +00001639void
1640GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets)
1641{
1642 uint32_t i;
1643 TimeValue start_time, end_time;
1644 uint64_t total_time_nsec;
1645 float packets_per_second;
1646 if (SendSpeedTestPacket (0, 0))
1647 {
1648 for (uint32_t send_size = 0; send_size <= 1024; send_size *= 2)
1649 {
1650 for (uint32_t recv_size = 0; recv_size <= 1024; recv_size *= 2)
1651 {
1652 start_time = TimeValue::Now();
1653 for (i=0; i<num_packets; ++i)
1654 {
1655 SendSpeedTestPacket (send_size, recv_size);
1656 }
1657 end_time = TimeValue::Now();
1658 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001659 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Greg Clayton153ccd72011-08-10 02:10:13 +00001660 printf ("%u qSpeedTest(send=%-5u, recv=%-5u) in %llu.%9.9llu sec for %f packets/sec.\n",
Greg Clayton06d7cc82011-04-04 18:18:57 +00001661 num_packets,
1662 send_size,
1663 recv_size,
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001664 total_time_nsec / TimeValue::NanoSecPerSec,
1665 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton06d7cc82011-04-04 18:18:57 +00001666 packets_per_second);
1667 if (recv_size == 0)
1668 recv_size = 32;
1669 }
1670 if (send_size == 0)
1671 send_size = 32;
1672 }
1673 }
1674 else
1675 {
1676 start_time = TimeValue::Now();
1677 for (i=0; i<num_packets; ++i)
1678 {
1679 GetCurrentProcessID ();
1680 }
1681 end_time = TimeValue::Now();
1682 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001683 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Greg Clayton153ccd72011-08-10 02:10:13 +00001684 printf ("%u 'qC' packets packets in 0x%llu%9.9llu sec for %f packets/sec.\n",
Greg Clayton06d7cc82011-04-04 18:18:57 +00001685 num_packets,
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001686 total_time_nsec / TimeValue::NanoSecPerSec,
1687 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton06d7cc82011-04-04 18:18:57 +00001688 packets_per_second);
1689 }
1690}
1691
1692bool
1693GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size)
1694{
1695 StreamString packet;
1696 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size);
1697 uint32_t bytes_left = send_size;
1698 while (bytes_left > 0)
1699 {
1700 if (bytes_left >= 26)
1701 {
1702 packet.PutCString("abcdefghijklmnopqrstuvwxyz");
1703 bytes_left -= 26;
1704 }
1705 else
1706 {
1707 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz");
1708 bytes_left = 0;
1709 }
1710 }
1711
1712 StringExtractorGDBRemote response;
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001713 return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) > 0;
Greg Clayton06d7cc82011-04-04 18:18:57 +00001714 return false;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001715}
Greg Claytonb72d0f02011-04-12 05:54:46 +00001716
1717uint16_t
1718GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort ()
1719{
1720 StringExtractorGDBRemote response;
1721 if (SendPacketAndWaitForResponse("qLaunchGDBServer", strlen("qLaunchGDBServer"), response, false))
1722 {
1723 std::string name;
1724 std::string value;
1725 uint16_t port = 0;
1726 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
1727 while (response.GetNameColonValue(name, value))
1728 {
1729 if (name.size() == 4 && name.compare("port") == 0)
1730 port = Args::StringToUInt32(value.c_str(), 0, 0);
1731 if (name.size() == 3 && name.compare("pid") == 0)
1732 pid = Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0);
1733 }
1734 return port;
1735 }
1736 return 0;
1737}
1738
1739bool
1740GDBRemoteCommunicationClient::SetCurrentThread (int tid)
1741{
1742 if (m_curr_tid == tid)
1743 return true;
1744
1745 char packet[32];
1746 int packet_len;
1747 if (tid <= 0)
1748 packet_len = ::snprintf (packet, sizeof(packet), "Hg%i", tid);
1749 else
1750 packet_len = ::snprintf (packet, sizeof(packet), "Hg%x", tid);
1751 assert (packet_len + 1 < sizeof(packet));
1752 StringExtractorGDBRemote response;
1753 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
1754 {
1755 if (response.IsOKResponse())
1756 {
1757 m_curr_tid = tid;
1758 return true;
1759 }
1760 }
1761 return false;
1762}
1763
1764bool
1765GDBRemoteCommunicationClient::SetCurrentThreadForRun (int tid)
1766{
1767 if (m_curr_tid_run == tid)
1768 return true;
1769
1770 char packet[32];
1771 int packet_len;
1772 if (tid <= 0)
1773 packet_len = ::snprintf (packet, sizeof(packet), "Hc%i", tid);
1774 else
1775 packet_len = ::snprintf (packet, sizeof(packet), "Hc%x", tid);
1776
1777 assert (packet_len + 1 < sizeof(packet));
1778 StringExtractorGDBRemote response;
1779 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
1780 {
1781 if (response.IsOKResponse())
1782 {
1783 m_curr_tid_run = tid;
1784 return true;
1785 }
1786 }
1787 return false;
1788}
1789
1790bool
1791GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response)
1792{
1793 if (SendPacketAndWaitForResponse("?", 1, response, false))
1794 return response.IsNormalResponse();
1795 return false;
1796}
1797
1798bool
1799GDBRemoteCommunicationClient::GetThreadStopInfo (uint32_t tid, StringExtractorGDBRemote &response)
1800{
1801 if (m_supports_qThreadStopInfo)
1802 {
1803 char packet[256];
1804 int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%x", tid);
1805 assert (packet_len < sizeof(packet));
1806 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
1807 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001808 if (response.IsNormalResponse())
Greg Claytonb72d0f02011-04-12 05:54:46 +00001809 return true;
1810 else
1811 return false;
1812 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001813 else
1814 {
1815 m_supports_qThreadStopInfo = false;
1816 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001817 }
Greg Clayton139da722011-05-20 03:15:54 +00001818// if (SetCurrentThread (tid))
1819// return GetStopReply (response);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001820 return false;
1821}
1822
1823
1824uint8_t
1825GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length)
1826{
1827 switch (type)
1828 {
1829 case eBreakpointSoftware: if (!m_supports_z0) return UINT8_MAX; break;
1830 case eBreakpointHardware: if (!m_supports_z1) return UINT8_MAX; break;
1831 case eWatchpointWrite: if (!m_supports_z2) return UINT8_MAX; break;
1832 case eWatchpointRead: if (!m_supports_z3) return UINT8_MAX; break;
1833 case eWatchpointReadWrite: if (!m_supports_z4) return UINT8_MAX; break;
1834 default: return UINT8_MAX;
1835 }
1836
1837 char packet[64];
1838 const int packet_len = ::snprintf (packet,
1839 sizeof(packet),
1840 "%c%i,%llx,%x",
1841 insert ? 'Z' : 'z',
1842 type,
1843 addr,
1844 length);
1845
1846 assert (packet_len + 1 < sizeof(packet));
1847 StringExtractorGDBRemote response;
1848 if (SendPacketAndWaitForResponse(packet, packet_len, response, true))
1849 {
1850 if (response.IsOKResponse())
1851 return 0;
Greg Claytonb72d0f02011-04-12 05:54:46 +00001852 else if (response.IsErrorResponse())
1853 return response.GetError();
1854 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001855 else
1856 {
1857 switch (type)
1858 {
1859 case eBreakpointSoftware: m_supports_z0 = false; break;
1860 case eBreakpointHardware: m_supports_z1 = false; break;
1861 case eWatchpointWrite: m_supports_z2 = false; break;
1862 case eWatchpointRead: m_supports_z3 = false; break;
1863 case eWatchpointReadWrite: m_supports_z4 = false; break;
1864 default: break;
1865 }
1866 }
1867
Greg Claytonb72d0f02011-04-12 05:54:46 +00001868 return UINT8_MAX;
1869}
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001870
1871size_t
1872GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
1873 bool &sequence_mutex_unavailable)
1874{
1875 Mutex::Locker locker;
1876 thread_ids.clear();
1877
Greg Claytonc8dd5702012-04-12 19:04:34 +00001878 if (GetSequenceMutex (locker))
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001879 {
1880 sequence_mutex_unavailable = false;
1881 StringExtractorGDBRemote response;
1882
Greg Clayton63afdb02011-06-17 01:22:15 +00001883 for (SendPacketNoLock ("qfThreadInfo", strlen("qfThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001884 response.IsNormalResponse();
Greg Clayton63afdb02011-06-17 01:22:15 +00001885 SendPacketNoLock ("qsThreadInfo", strlen("qsThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()))
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001886 {
1887 char ch = response.GetChar();
1888 if (ch == 'l')
1889 break;
1890 if (ch == 'm')
1891 {
1892 do
1893 {
1894 tid_t tid = response.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID);
1895
1896 if (tid != LLDB_INVALID_THREAD_ID)
1897 {
1898 thread_ids.push_back (tid);
1899 }
1900 ch = response.GetChar(); // Skip the command separator
1901 } while (ch == ','); // Make sure we got a comma separator
1902 }
1903 }
1904 }
1905 else
1906 {
Greg Claytonc8dd5702012-04-12 19:04:34 +00001907 LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
1908 if (log)
1909 log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'");
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001910 sequence_mutex_unavailable = true;
1911 }
1912 return thread_ids.size();
1913}
Greg Clayton516f0842012-04-11 00:24:49 +00001914
1915lldb::addr_t
1916GDBRemoteCommunicationClient::GetShlibInfoAddr()
1917{
1918 if (!IsRunning())
1919 {
1920 StringExtractorGDBRemote response;
1921 if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false))
1922 {
1923 if (response.IsNormalResponse())
1924 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1925 }
1926 }
1927 return LLDB_INVALID_ADDRESS;
1928}
1929