blob: dc5e8d1afea50a81a4366bcc7aa9c53af7989cd5 [file] [log] [blame]
Greg Clayton61d043b2011-03-22 04:00:09 +00001//===-- GDBRemoteCommunicationClient.cpp ------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10
11#include "GDBRemoteCommunicationClient.h"
12
13// C Includes
14// C++ Includes
15// Other libraries and framework includes
16#include "llvm/ADT/Triple.h"
17#include "lldb/Interpreter/Args.h"
18#include "lldb/Core/ConnectionFileDescriptor.h"
19#include "lldb/Core/Log.h"
20#include "lldb/Core/State.h"
21#include "lldb/Core/StreamString.h"
22#include "lldb/Host/Endian.h"
23#include "lldb/Host/Host.h"
24#include "lldb/Host/TimeValue.h"
25
26// Project includes
27#include "Utility/StringExtractorGDBRemote.h"
28#include "ProcessGDBRemote.h"
29#include "ProcessGDBRemoteLog.h"
30
31using namespace lldb;
32using namespace lldb_private;
33
34//----------------------------------------------------------------------
35// GDBRemoteCommunicationClient constructor
36//----------------------------------------------------------------------
Greg Claytonb72d0f02011-04-12 05:54:46 +000037GDBRemoteCommunicationClient::GDBRemoteCommunicationClient(bool is_platform) :
38 GDBRemoteCommunication("gdb-remote.client", "gdb-remote.client.rx_packet", is_platform),
Greg Clayton61d043b2011-03-22 04:00:09 +000039 m_supports_not_sending_acks (eLazyBoolCalculate),
40 m_supports_thread_suffix (eLazyBoolCalculate),
Greg Claytona1f645e2012-04-10 03:22:03 +000041 m_supports_threads_in_stop_reply (eLazyBoolCalculate),
Greg Clayton61d043b2011-03-22 04:00:09 +000042 m_supports_vCont_all (eLazyBoolCalculate),
43 m_supports_vCont_any (eLazyBoolCalculate),
44 m_supports_vCont_c (eLazyBoolCalculate),
45 m_supports_vCont_C (eLazyBoolCalculate),
46 m_supports_vCont_s (eLazyBoolCalculate),
47 m_supports_vCont_S (eLazyBoolCalculate),
Greg Clayton24bc5d92011-03-30 18:16:51 +000048 m_qHostInfo_is_valid (eLazyBoolCalculate),
Greg Clayton2f085c62011-05-15 01:25:55 +000049 m_supports_alloc_dealloc_memory (eLazyBoolCalculate),
Greg Claytona9385532011-11-18 07:03:08 +000050 m_supports_memory_region_info (eLazyBoolCalculate),
Johnny Chen7cbdcfb2012-05-23 21:09:52 +000051 m_supports_watchpoint_support_info (eLazyBoolCalculate),
Greg Clayton24bc5d92011-03-30 18:16:51 +000052 m_supports_qProcessInfoPID (true),
53 m_supports_qfProcessInfo (true),
54 m_supports_qUserName (true),
55 m_supports_qGroupName (true),
Greg Claytonb72d0f02011-04-12 05:54:46 +000056 m_supports_qThreadStopInfo (true),
57 m_supports_z0 (true),
58 m_supports_z1 (true),
59 m_supports_z2 (true),
60 m_supports_z3 (true),
61 m_supports_z4 (true),
62 m_curr_tid (LLDB_INVALID_THREAD_ID),
63 m_curr_tid_run (LLDB_INVALID_THREAD_ID),
Johnny Chen7cbdcfb2012-05-23 21:09:52 +000064 m_num_supported_hardware_watchpoints (0),
Greg Clayton61d043b2011-03-22 04:00:09 +000065 m_async_mutex (Mutex::eMutexTypeRecursive),
66 m_async_packet_predicate (false),
67 m_async_packet (),
68 m_async_response (),
69 m_async_signal (-1),
Greg Clayton58e26e02011-03-24 04:28:38 +000070 m_host_arch(),
71 m_os_version_major (UINT32_MAX),
72 m_os_version_minor (UINT32_MAX),
73 m_os_version_update (UINT32_MAX)
Greg Clayton61d043b2011-03-22 04:00:09 +000074{
Greg Clayton61d043b2011-03-22 04:00:09 +000075}
76
77//----------------------------------------------------------------------
78// Destructor
79//----------------------------------------------------------------------
80GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient()
81{
Greg Clayton61d043b2011-03-22 04:00:09 +000082 if (IsConnected())
Greg Clayton61d043b2011-03-22 04:00:09 +000083 Disconnect();
Greg Clayton61d043b2011-03-22 04:00:09 +000084}
85
86bool
Greg Clayton58e26e02011-03-24 04:28:38 +000087GDBRemoteCommunicationClient::HandshakeWithServer (Error *error_ptr)
88{
89 // Start the read thread after we send the handshake ack since if we
90 // fail to send the handshake ack, there is no reason to continue...
91 if (SendAck())
Greg Clayton63afdb02011-06-17 01:22:15 +000092 return true;
Greg Clayton58e26e02011-03-24 04:28:38 +000093
94 if (error_ptr)
95 error_ptr->SetErrorString("failed to send the handshake ack");
96 return false;
97}
98
99void
100GDBRemoteCommunicationClient::QueryNoAckModeSupported ()
Greg Clayton61d043b2011-03-22 04:00:09 +0000101{
102 if (m_supports_not_sending_acks == eLazyBoolCalculate)
103 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000104 m_send_acks = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000105 m_supports_not_sending_acks = eLazyBoolNo;
Greg Clayton58e26e02011-03-24 04:28:38 +0000106
107 StringExtractorGDBRemote response;
Greg Clayton61d043b2011-03-22 04:00:09 +0000108 if (SendPacketAndWaitForResponse("QStartNoAckMode", response, false))
109 {
110 if (response.IsOKResponse())
Greg Clayton58e26e02011-03-24 04:28:38 +0000111 {
112 m_send_acks = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000113 m_supports_not_sending_acks = eLazyBoolYes;
Greg Clayton58e26e02011-03-24 04:28:38 +0000114 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000115 }
116 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000117}
118
119void
Greg Claytona1f645e2012-04-10 03:22:03 +0000120GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported ()
121{
122 if (m_supports_threads_in_stop_reply == eLazyBoolCalculate)
123 {
124 m_supports_threads_in_stop_reply = eLazyBoolNo;
125
126 StringExtractorGDBRemote response;
127 if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response, false))
128 {
129 if (response.IsOKResponse())
130 m_supports_threads_in_stop_reply = eLazyBoolYes;
131 }
132 }
133}
134
135
136void
Greg Clayton61d043b2011-03-22 04:00:09 +0000137GDBRemoteCommunicationClient::ResetDiscoverableSettings()
138{
139 m_supports_not_sending_acks = eLazyBoolCalculate;
140 m_supports_thread_suffix = eLazyBoolCalculate;
Greg Claytona1f645e2012-04-10 03:22:03 +0000141 m_supports_threads_in_stop_reply = eLazyBoolCalculate;
Greg Clayton61d043b2011-03-22 04:00:09 +0000142 m_supports_vCont_c = eLazyBoolCalculate;
143 m_supports_vCont_C = eLazyBoolCalculate;
144 m_supports_vCont_s = eLazyBoolCalculate;
145 m_supports_vCont_S = eLazyBoolCalculate;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000146 m_qHostInfo_is_valid = eLazyBoolCalculate;
Greg Clayton2f085c62011-05-15 01:25:55 +0000147 m_supports_alloc_dealloc_memory = eLazyBoolCalculate;
Greg Claytona9385532011-11-18 07:03:08 +0000148 m_supports_memory_region_info = eLazyBoolCalculate;
Greg Clayton989816b2011-05-14 01:50:35 +0000149
Greg Clayton24bc5d92011-03-30 18:16:51 +0000150 m_supports_qProcessInfoPID = true;
151 m_supports_qfProcessInfo = true;
152 m_supports_qUserName = true;
153 m_supports_qGroupName = true;
Greg Claytonb72d0f02011-04-12 05:54:46 +0000154 m_supports_qThreadStopInfo = true;
155 m_supports_z0 = true;
156 m_supports_z1 = true;
157 m_supports_z2 = true;
158 m_supports_z3 = true;
159 m_supports_z4 = true;
Greg Claytoncb8977d2011-03-23 00:09:55 +0000160 m_host_arch.Clear();
Greg Clayton61d043b2011-03-22 04:00:09 +0000161}
162
163
164bool
165GDBRemoteCommunicationClient::GetThreadSuffixSupported ()
166{
167 if (m_supports_thread_suffix == eLazyBoolCalculate)
168 {
169 StringExtractorGDBRemote response;
170 m_supports_thread_suffix = eLazyBoolNo;
171 if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response, false))
172 {
173 if (response.IsOKResponse())
174 m_supports_thread_suffix = eLazyBoolYes;
175 }
176 }
177 return m_supports_thread_suffix;
178}
179bool
180GDBRemoteCommunicationClient::GetVContSupported (char flavor)
181{
182 if (m_supports_vCont_c == eLazyBoolCalculate)
183 {
184 StringExtractorGDBRemote response;
185 m_supports_vCont_any = eLazyBoolNo;
186 m_supports_vCont_all = eLazyBoolNo;
187 m_supports_vCont_c = eLazyBoolNo;
188 m_supports_vCont_C = eLazyBoolNo;
189 m_supports_vCont_s = eLazyBoolNo;
190 m_supports_vCont_S = eLazyBoolNo;
191 if (SendPacketAndWaitForResponse("vCont?", response, false))
192 {
193 const char *response_cstr = response.GetStringRef().c_str();
194 if (::strstr (response_cstr, ";c"))
195 m_supports_vCont_c = eLazyBoolYes;
196
197 if (::strstr (response_cstr, ";C"))
198 m_supports_vCont_C = eLazyBoolYes;
199
200 if (::strstr (response_cstr, ";s"))
201 m_supports_vCont_s = eLazyBoolYes;
202
203 if (::strstr (response_cstr, ";S"))
204 m_supports_vCont_S = eLazyBoolYes;
205
206 if (m_supports_vCont_c == eLazyBoolYes &&
207 m_supports_vCont_C == eLazyBoolYes &&
208 m_supports_vCont_s == eLazyBoolYes &&
209 m_supports_vCont_S == eLazyBoolYes)
210 {
211 m_supports_vCont_all = eLazyBoolYes;
212 }
213
214 if (m_supports_vCont_c == eLazyBoolYes ||
215 m_supports_vCont_C == eLazyBoolYes ||
216 m_supports_vCont_s == eLazyBoolYes ||
217 m_supports_vCont_S == eLazyBoolYes)
218 {
219 m_supports_vCont_any = eLazyBoolYes;
220 }
221 }
222 }
223
224 switch (flavor)
225 {
226 case 'a': return m_supports_vCont_any;
227 case 'A': return m_supports_vCont_all;
228 case 'c': return m_supports_vCont_c;
229 case 'C': return m_supports_vCont_C;
230 case 's': return m_supports_vCont_s;
231 case 'S': return m_supports_vCont_S;
232 default: break;
233 }
234 return false;
235}
236
237
238size_t
239GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
240(
241 const char *payload,
242 StringExtractorGDBRemote &response,
243 bool send_async
244)
245{
246 return SendPacketAndWaitForResponse (payload,
247 ::strlen (payload),
248 response,
249 send_async);
250}
251
252size_t
253GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
254(
255 const char *payload,
256 size_t payload_length,
257 StringExtractorGDBRemote &response,
258 bool send_async
259)
260{
261 Mutex::Locker locker;
Greg Clayton61d043b2011-03-22 04:00:09 +0000262 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton801417e2011-07-07 01:59:51 +0000263 size_t response_len = 0;
Greg Claytonc8dd5702012-04-12 19:04:34 +0000264 if (GetSequenceMutex (locker))
Greg Clayton61d043b2011-03-22 04:00:09 +0000265 {
Greg Clayton139da722011-05-20 03:15:54 +0000266 if (SendPacketNoLock (payload, payload_length))
Greg Clayton801417e2011-07-07 01:59:51 +0000267 response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
268 else
269 {
270 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000271 log->Printf("error: failed to send '%*s'", (int) payload_length, payload);
Greg Clayton801417e2011-07-07 01:59:51 +0000272 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000273 }
274 else
275 {
276 if (send_async)
277 {
Greg Clayton70e167f2012-05-31 21:24:20 +0000278 if (IsRunning())
Greg Clayton61d043b2011-03-22 04:00:09 +0000279 {
Greg Clayton70e167f2012-05-31 21:24:20 +0000280 Mutex::Locker async_locker (m_async_mutex);
281 m_async_packet.assign(payload, payload_length);
282 m_async_packet_predicate.SetValue (true, eBroadcastNever);
283
284 if (log)
285 log->Printf ("async: async packet = %s", m_async_packet.c_str());
286
287 bool timed_out = false;
288 if (SendInterrupt(locker, 2, timed_out))
Greg Clayton61d043b2011-03-22 04:00:09 +0000289 {
Greg Clayton70e167f2012-05-31 21:24:20 +0000290 if (m_interrupt_sent)
Greg Clayton61d043b2011-03-22 04:00:09 +0000291 {
Greg Clayton70e167f2012-05-31 21:24:20 +0000292 TimeValue timeout_time;
293 timeout_time = TimeValue::Now();
294 timeout_time.OffsetWithSeconds (m_packet_timeout);
295
Greg Clayton61d043b2011-03-22 04:00:09 +0000296 if (log)
Greg Clayton70e167f2012-05-31 21:24:20 +0000297 log->Printf ("async: sent interrupt");
Greg Clayton801417e2011-07-07 01:59:51 +0000298
Greg Clayton70e167f2012-05-31 21:24:20 +0000299 if (m_async_packet_predicate.WaitForValueEqualTo (false, &timeout_time, &timed_out))
Greg Claytonb7669b32011-10-27 22:04:16 +0000300 {
Greg Clayton70e167f2012-05-31 21:24:20 +0000301 if (log)
302 log->Printf ("async: got response");
303
304 // Swap the response buffer to avoid malloc and string copy
305 response.GetStringRef().swap (m_async_response.GetStringRef());
306 response_len = response.GetStringRef().size();
307 }
308 else
309 {
310 if (log)
311 log->Printf ("async: timed out waiting for response");
312 }
313
314 // Make sure we wait until the continue packet has been sent again...
315 if (m_private_is_running.WaitForValueEqualTo (true, &timeout_time, &timed_out))
316 {
317 if (log)
318 {
319 if (timed_out)
320 log->Printf ("async: timed out waiting for process to resume, but process was resumed");
321 else
322 log->Printf ("async: async packet sent");
323 }
324 }
325 else
326 {
327 if (log)
328 log->Printf ("async: timed out waiting for process to resume");
Greg Claytonb7669b32011-10-27 22:04:16 +0000329 }
330 }
331 else
332 {
Greg Clayton70e167f2012-05-31 21:24:20 +0000333 // We had a racy condition where we went to send the interrupt
334 // yet we were able to get the lock, so the process must have
335 // just stopped?
Greg Clayton61d043b2011-03-22 04:00:09 +0000336 if (log)
Greg Clayton70e167f2012-05-31 21:24:20 +0000337 log->Printf ("async: got lock without sending interrupt");
338 // Send the packet normally since we got the lock
339 if (SendPacketNoLock (payload, payload_length))
340 response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
341 else
342 {
343 if (log)
344 log->Printf("error: failed to send '%*s'", (int) payload_length, payload);
345 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000346 }
347 }
348 else
349 {
Greg Clayton801417e2011-07-07 01:59:51 +0000350 if (log)
Greg Clayton70e167f2012-05-31 21:24:20 +0000351 log->Printf ("async: failed to interrupt");
Greg Clayton61d043b2011-03-22 04:00:09 +0000352 }
353 }
354 else
355 {
356 if (log)
Greg Clayton70e167f2012-05-31 21:24:20 +0000357 log->Printf ("async: not running, async is ignored");
Greg Clayton61d043b2011-03-22 04:00:09 +0000358 }
359 }
360 else
361 {
362 if (log)
Greg Claytonc8dd5702012-04-12 19:04:34 +0000363 log->Printf("error: failed to get packet sequence mutex, not sending packet '%*s'", (int) payload_length, payload);
Greg Clayton61d043b2011-03-22 04:00:09 +0000364 }
365 }
Greg Clayton801417e2011-07-07 01:59:51 +0000366 if (response_len == 0)
367 {
368 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000369 log->Printf("error: failed to get response for '%*s'", (int) payload_length, payload);
Greg Clayton801417e2011-07-07 01:59:51 +0000370 }
371 return response_len;
Greg Clayton61d043b2011-03-22 04:00:09 +0000372}
373
Greg Clayton61d043b2011-03-22 04:00:09 +0000374StateType
375GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse
376(
377 ProcessGDBRemote *process,
378 const char *payload,
379 size_t packet_length,
380 StringExtractorGDBRemote &response
381)
382{
383 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
384 if (log)
385 log->Printf ("GDBRemoteCommunicationClient::%s ()", __FUNCTION__);
386
387 Mutex::Locker locker(m_sequence_mutex);
388 StateType state = eStateRunning;
389
390 BroadcastEvent(eBroadcastBitRunPacketSent, NULL);
391 m_public_is_running.SetValue (true, eBroadcastNever);
392 // Set the starting continue packet into "continue_packet". This packet
393 // make change if we are interrupted and we continue after an async packet...
394 std::string continue_packet(payload, packet_length);
395
Greg Clayton628cead2011-05-19 03:54:16 +0000396 bool got_stdout = false;
397
Greg Clayton61d043b2011-03-22 04:00:09 +0000398 while (state == eStateRunning)
399 {
Greg Clayton628cead2011-05-19 03:54:16 +0000400 if (!got_stdout)
401 {
402 if (log)
403 log->Printf ("GDBRemoteCommunicationClient::%s () sending continue packet: %s", __FUNCTION__, continue_packet.c_str());
Greg Clayton516f0842012-04-11 00:24:49 +0000404 if (SendPacketNoLock(continue_packet.c_str(), continue_packet.size()) == 0)
Greg Clayton628cead2011-05-19 03:54:16 +0000405 state = eStateInvalid;
Greg Clayton61d043b2011-03-22 04:00:09 +0000406
Greg Claytonb7669b32011-10-27 22:04:16 +0000407 m_private_is_running.SetValue (true, eBroadcastAlways);
Greg Clayton628cead2011-05-19 03:54:16 +0000408 }
409
410 got_stdout = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000411
412 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000413 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(%s)", __FUNCTION__, continue_packet.c_str());
Greg Clayton61d043b2011-03-22 04:00:09 +0000414
Greg Clayton516f0842012-04-11 00:24:49 +0000415 if (WaitForPacketWithTimeoutMicroSecondsNoLock(response, UINT32_MAX))
Greg Clayton61d043b2011-03-22 04:00:09 +0000416 {
417 if (response.Empty())
418 state = eStateInvalid;
419 else
420 {
421 const char stop_type = response.GetChar();
422 if (log)
423 log->Printf ("GDBRemoteCommunicationClient::%s () got packet: %s", __FUNCTION__, response.GetStringRef().c_str());
424 switch (stop_type)
425 {
426 case 'T':
427 case 'S':
Greg Clayton61d043b2011-03-22 04:00:09 +0000428 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000429 if (process->GetStopID() == 0)
Greg Clayton61d043b2011-03-22 04:00:09 +0000430 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000431 if (process->GetID() == LLDB_INVALID_PROCESS_ID)
432 {
433 lldb::pid_t pid = GetCurrentProcessID ();
434 if (pid != LLDB_INVALID_PROCESS_ID)
435 process->SetID (pid);
436 }
437 process->BuildDynamicRegisterInfo (true);
Greg Clayton61d043b2011-03-22 04:00:09 +0000438 }
Greg Clayton05e4d972012-03-29 01:55:41 +0000439
440 // Privately notify any internal threads that we have stopped
441 // in case we wanted to interrupt our process, yet we might
442 // send a packet and continue without returning control to the
443 // user.
444 m_private_is_running.SetValue (false, eBroadcastAlways);
445
446 const uint8_t signo = response.GetHexU8 (UINT8_MAX);
447
Greg Claytonc2c822c2012-05-15 02:50:49 +0000448 bool continue_after_async = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000449 if (m_async_signal != -1 || m_async_packet_predicate.GetValue())
450 {
Greg Claytonc2c822c2012-05-15 02:50:49 +0000451 continue_after_async = true;
Greg Clayton05e4d972012-03-29 01:55:41 +0000452 // We sent an interrupt packet to stop the inferior process
453 // for an async signal or to send an async packet while running
454 // but we might have been single stepping and received the
455 // stop packet for the step instead of for the interrupt packet.
456 // Typically when an interrupt is sent a SIGINT or SIGSTOP
457 // is used, so if we get anything else, we need to try and
458 // get another stop reply packet that may have been sent
459 // due to sending the interrupt when the target is stopped
460 // which will just re-send a copy of the last stop reply
461 // packet. If we don't do this, then the reply for our
462 // async packet will be the repeat stop reply packet and cause
463 // a lot of trouble for us!
464 if (signo != SIGINT && signo != SIGSTOP)
465 {
Greg Claytonc2c822c2012-05-15 02:50:49 +0000466 continue_after_async = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000467
468 // We didn't get a a SIGINT or SIGSTOP, so try for a
469 // very brief time (1 ms) to get another stop reply
470 // packet to make sure it doesn't get in the way
471 StringExtractorGDBRemote extra_stop_reply_packet;
472 uint32_t timeout_usec = 1000;
473 if (WaitForPacketWithTimeoutMicroSecondsNoLock (extra_stop_reply_packet, timeout_usec))
474 {
475 switch (extra_stop_reply_packet.GetChar())
476 {
477 case 'T':
478 case 'S':
479 // We did get an extra stop reply, which means
480 // our interrupt didn't stop the target so we
481 // shouldn't continue after the async signal
482 // or packet is sent...
Greg Claytonc2c822c2012-05-15 02:50:49 +0000483 continue_after_async = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000484 break;
485 }
486 }
487 }
488 }
489
490 if (m_async_signal != -1)
491 {
492 if (log)
493 log->Printf ("async: send signo = %s", Host::GetSignalAsCString (m_async_signal));
494
495 // Save off the async signal we are supposed to send
496 const int async_signal = m_async_signal;
497 // Clear the async signal member so we don't end up
498 // sending the signal multiple times...
499 m_async_signal = -1;
500 // Check which signal we stopped with
501 if (signo == async_signal)
502 {
503 if (log)
504 log->Printf ("async: stopped with signal %s, we are done running", Host::GetSignalAsCString (signo));
505
506 // We already stopped with a signal that we wanted
507 // to stop with, so we are done
508 }
509 else
510 {
511 // We stopped with a different signal that the one
512 // we wanted to stop with, so now we must resume
513 // with the signal we want
514 char signal_packet[32];
515 int signal_packet_len = 0;
516 signal_packet_len = ::snprintf (signal_packet,
517 sizeof (signal_packet),
518 "C%2.2x",
519 async_signal);
520
521 if (log)
522 log->Printf ("async: stopped with signal %s, resume with %s",
523 Host::GetSignalAsCString (signo),
524 Host::GetSignalAsCString (async_signal));
525
526 // Set the continue packet to resume even if the
Greg Claytonc2c822c2012-05-15 02:50:49 +0000527 // interrupt didn't cause our stop (ignore continue_after_async)
Greg Clayton05e4d972012-03-29 01:55:41 +0000528 continue_packet.assign(signal_packet, signal_packet_len);
529 continue;
530 }
531 }
532 else if (m_async_packet_predicate.GetValue())
533 {
534 LogSP packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
535
536 // We are supposed to send an asynchronous packet while
537 // we are running.
538 m_async_response.Clear();
539 if (m_async_packet.empty())
540 {
541 if (packet_log)
542 packet_log->Printf ("async: error: empty async packet");
543
544 }
545 else
546 {
547 if (packet_log)
548 packet_log->Printf ("async: sending packet");
549
550 SendPacketAndWaitForResponse (&m_async_packet[0],
551 m_async_packet.size(),
552 m_async_response,
553 false);
554 }
555 // Let the other thread that was trying to send the async
556 // packet know that the packet has been sent and response is
557 // ready...
558 m_async_packet_predicate.SetValue(false, eBroadcastAlways);
559
560 if (packet_log)
Greg Claytonc2c822c2012-05-15 02:50:49 +0000561 packet_log->Printf ("async: sent packet, continue_after_async = %i", continue_after_async);
Greg Clayton05e4d972012-03-29 01:55:41 +0000562
563 // Set the continue packet to resume if our interrupt
564 // for the async packet did cause the stop
Greg Claytonc2c822c2012-05-15 02:50:49 +0000565 if (continue_after_async)
Greg Clayton05e4d972012-03-29 01:55:41 +0000566 {
Greg Clayton8ca4fa72012-05-24 23:42:14 +0000567 // Reverting this for now as it is causing deadlocks
568 // in programs (<rdar://problem/11529853>). In the future
569 // we should check our thread list and "do the right thing"
570 // for new threads that show up while we stop and run async
571 // packets. Setting the packet to 'c' to continue all threads
572 // is the right thing to do 99.99% of the time because if a
573 // thread was single stepping, and we sent an interrupt, we
574 // will notice above that we didn't stop due to an interrupt
575 // but stopped due to stepping and we would _not_ continue.
576 continue_packet.assign (1, 'c');
Greg Clayton05e4d972012-03-29 01:55:41 +0000577 continue;
578 }
579 }
580 // Stop with signal and thread info
581 state = eStateStopped;
Greg Clayton61d043b2011-03-22 04:00:09 +0000582 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000583 break;
584
585 case 'W':
586 case 'X':
587 // process exited
588 state = eStateExited;
589 break;
590
591 case 'O':
592 // STDOUT
593 {
Greg Clayton628cead2011-05-19 03:54:16 +0000594 got_stdout = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000595 std::string inferior_stdout;
596 inferior_stdout.reserve(response.GetBytesLeft () / 2);
597 char ch;
598 while ((ch = response.GetHexU8()) != '\0')
599 inferior_stdout.append(1, ch);
600 process->AppendSTDOUT (inferior_stdout.c_str(), inferior_stdout.size());
601 }
602 break;
603
604 case 'E':
605 // ERROR
606 state = eStateInvalid;
607 break;
608
609 default:
610 if (log)
611 log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__);
612 state = eStateInvalid;
613 break;
614 }
615 }
616 }
617 else
618 {
619 if (log)
620 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__);
621 state = eStateInvalid;
622 }
623 }
624 if (log)
625 log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state));
626 response.SetFilePos(0);
627 m_private_is_running.SetValue (false, eBroadcastAlways);
628 m_public_is_running.SetValue (false, eBroadcastAlways);
629 return state;
630}
631
632bool
633GDBRemoteCommunicationClient::SendAsyncSignal (int signo)
634{
Greg Clayton05e4d972012-03-29 01:55:41 +0000635 Mutex::Locker async_locker (m_async_mutex);
Greg Clayton61d043b2011-03-22 04:00:09 +0000636 m_async_signal = signo;
637 bool timed_out = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000638 Mutex::Locker locker;
Greg Clayton05e4d972012-03-29 01:55:41 +0000639 if (SendInterrupt (locker, 1, timed_out))
Greg Clayton61d043b2011-03-22 04:00:09 +0000640 return true;
641 m_async_signal = -1;
642 return false;
643}
644
Greg Clayton516f0842012-04-11 00:24:49 +0000645// This function takes a mutex locker as a parameter in case the GetSequenceMutex
Greg Clayton61d043b2011-03-22 04:00:09 +0000646// actually succeeds. If it doesn't succeed in acquiring the sequence mutex
647// (the expected result), then it will send the halt packet. If it does succeed
648// then the caller that requested the interrupt will want to keep the sequence
649// locked down so that no one else can send packets while the caller has control.
650// This function usually gets called when we are running and need to stop the
651// target. It can also be used when we are running and and we need to do something
652// else (like read/write memory), so we need to interrupt the running process
653// (gdb remote protocol requires this), and do what we need to do, then resume.
654
655bool
Greg Clayton05e4d972012-03-29 01:55:41 +0000656GDBRemoteCommunicationClient::SendInterrupt
Greg Clayton61d043b2011-03-22 04:00:09 +0000657(
658 Mutex::Locker& locker,
659 uint32_t seconds_to_wait_for_stop,
Greg Clayton61d043b2011-03-22 04:00:09 +0000660 bool &timed_out
661)
662{
Greg Clayton05e4d972012-03-29 01:55:41 +0000663 m_interrupt_sent = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000664 timed_out = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000665 LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Clayton61d043b2011-03-22 04:00:09 +0000666
667 if (IsRunning())
668 {
669 // Only send an interrupt if our debugserver is running...
Greg Claytonc8dd5702012-04-12 19:04:34 +0000670 if (GetSequenceMutex (locker))
Greg Clayton516f0842012-04-11 00:24:49 +0000671 {
672 if (log)
673 log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt");
674 }
675 else
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 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000721 }
Greg Clayton05e4d972012-03-29 01:55:41 +0000722 else
723 {
724 if (log)
725 log->Printf ("SendInterrupt () - not running");
726 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000727 return true;
728}
729
730lldb::pid_t
731GDBRemoteCommunicationClient::GetCurrentProcessID ()
732{
733 StringExtractorGDBRemote response;
734 if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false))
735 {
736 if (response.GetChar() == 'Q')
737 if (response.GetChar() == 'C')
738 return response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID);
739 }
740 return LLDB_INVALID_PROCESS_ID;
741}
742
743bool
744GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str)
745{
746 error_str.clear();
747 StringExtractorGDBRemote response;
748 if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false))
749 {
750 if (response.IsOKResponse())
751 return true;
752 if (response.GetChar() == 'E')
753 {
754 // A string the describes what failed when launching...
755 error_str = response.GetStringRef().substr(1);
756 }
757 else
758 {
759 error_str.assign ("unknown error occurred launching process");
760 }
761 }
762 else
763 {
764 error_str.assign ("failed to send the qLaunchSuccess packet");
765 }
766 return false;
767}
768
769int
770GDBRemoteCommunicationClient::SendArgumentsPacket (char const *argv[])
771{
772 if (argv && argv[0])
773 {
774 StreamString packet;
775 packet.PutChar('A');
776 const char *arg;
777 for (uint32_t i = 0; (arg = argv[i]) != NULL; ++i)
778 {
779 const int arg_len = strlen(arg);
780 if (i > 0)
781 packet.PutChar(',');
782 packet.Printf("%i,%i,", arg_len * 2, i);
783 packet.PutBytesAsRawHex8 (arg, arg_len);
784 }
785
786 StringExtractorGDBRemote response;
787 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
788 {
789 if (response.IsOKResponse())
790 return 0;
791 uint8_t error = response.GetError();
792 if (error)
793 return error;
794 }
795 }
796 return -1;
797}
798
799int
800GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value)
801{
802 if (name_equal_value && name_equal_value[0])
803 {
804 StreamString packet;
805 packet.Printf("QEnvironment:%s", name_equal_value);
806 StringExtractorGDBRemote response;
807 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
808 {
809 if (response.IsOKResponse())
810 return 0;
811 uint8_t error = response.GetError();
812 if (error)
813 return error;
814 }
815 }
816 return -1;
817}
818
Greg Claytona4582402011-05-08 04:53:50 +0000819int
820GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch)
821{
822 if (arch && arch[0])
823 {
824 StreamString packet;
825 packet.Printf("QLaunchArch:%s", arch);
826 StringExtractorGDBRemote response;
827 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
828 {
829 if (response.IsOKResponse())
830 return 0;
831 uint8_t error = response.GetError();
832 if (error)
833 return error;
834 }
835 }
836 return -1;
837}
838
Greg Clayton61d043b2011-03-22 04:00:09 +0000839bool
Greg Clayton58e26e02011-03-24 04:28:38 +0000840GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major,
841 uint32_t &minor,
842 uint32_t &update)
843{
844 if (GetHostInfo ())
845 {
846 if (m_os_version_major != UINT32_MAX)
847 {
848 major = m_os_version_major;
849 minor = m_os_version_minor;
850 update = m_os_version_update;
851 return true;
852 }
853 }
854 return false;
855}
856
857bool
858GDBRemoteCommunicationClient::GetOSBuildString (std::string &s)
859{
860 if (GetHostInfo ())
861 {
862 if (!m_os_build.empty())
863 {
864 s = m_os_build;
865 return true;
866 }
867 }
868 s.clear();
869 return false;
870}
871
872
873bool
874GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s)
875{
876 if (GetHostInfo ())
877 {
878 if (!m_os_kernel.empty())
879 {
880 s = m_os_kernel;
881 return true;
882 }
883 }
884 s.clear();
885 return false;
886}
887
888bool
889GDBRemoteCommunicationClient::GetHostname (std::string &s)
890{
891 if (GetHostInfo ())
892 {
893 if (!m_hostname.empty())
894 {
895 s = m_hostname;
896 return true;
897 }
898 }
899 s.clear();
900 return false;
901}
902
903ArchSpec
904GDBRemoteCommunicationClient::GetSystemArchitecture ()
905{
906 if (GetHostInfo ())
907 return m_host_arch;
908 return ArchSpec();
909}
910
911
912bool
Greg Clayton06d7cc82011-04-04 18:18:57 +0000913GDBRemoteCommunicationClient::GetHostInfo (bool force)
Greg Clayton61d043b2011-03-22 04:00:09 +0000914{
Greg Clayton06d7cc82011-04-04 18:18:57 +0000915 if (force || m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton61d043b2011-03-22 04:00:09 +0000916 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000917 m_qHostInfo_is_valid = eLazyBoolNo;
Greg Clayton61d043b2011-03-22 04:00:09 +0000918 StringExtractorGDBRemote response;
919 if (SendPacketAndWaitForResponse ("qHostInfo", response, false))
920 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +0000921 if (response.IsNormalResponse())
Greg Claytoncb8977d2011-03-23 00:09:55 +0000922 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000923 std::string name;
924 std::string value;
925 uint32_t cpu = LLDB_INVALID_CPUTYPE;
926 uint32_t sub = 0;
927 std::string arch_name;
928 std::string os_name;
929 std::string vendor_name;
930 std::string triple;
931 uint32_t pointer_byte_size = 0;
932 StringExtractor extractor;
933 ByteOrder byte_order = eByteOrderInvalid;
934 uint32_t num_keys_decoded = 0;
935 while (response.GetNameColonValue(name, value))
Greg Claytoncb8977d2011-03-23 00:09:55 +0000936 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000937 if (name.compare("cputype") == 0)
Greg Clayton58e26e02011-03-24 04:28:38 +0000938 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000939 // exception type in big endian hex
940 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0);
941 if (cpu != LLDB_INVALID_CPUTYPE)
942 ++num_keys_decoded;
943 }
944 else if (name.compare("cpusubtype") == 0)
945 {
946 // exception count in big endian hex
947 sub = Args::StringToUInt32 (value.c_str(), 0, 0);
948 if (sub != 0)
949 ++num_keys_decoded;
950 }
951 else if (name.compare("arch") == 0)
952 {
953 arch_name.swap (value);
954 ++num_keys_decoded;
955 }
956 else if (name.compare("triple") == 0)
957 {
958 // The triple comes as ASCII hex bytes since it contains '-' chars
959 extractor.GetStringRef().swap(value);
960 extractor.SetFilePos(0);
961 extractor.GetHexByteString (triple);
962 ++num_keys_decoded;
963 }
964 else if (name.compare("os_build") == 0)
965 {
966 extractor.GetStringRef().swap(value);
967 extractor.SetFilePos(0);
968 extractor.GetHexByteString (m_os_build);
969 ++num_keys_decoded;
970 }
971 else if (name.compare("hostname") == 0)
972 {
973 extractor.GetStringRef().swap(value);
974 extractor.SetFilePos(0);
975 extractor.GetHexByteString (m_hostname);
976 ++num_keys_decoded;
977 }
978 else if (name.compare("os_kernel") == 0)
979 {
980 extractor.GetStringRef().swap(value);
981 extractor.SetFilePos(0);
982 extractor.GetHexByteString (m_os_kernel);
983 ++num_keys_decoded;
984 }
985 else if (name.compare("ostype") == 0)
986 {
987 os_name.swap (value);
988 ++num_keys_decoded;
989 }
990 else if (name.compare("vendor") == 0)
991 {
992 vendor_name.swap(value);
993 ++num_keys_decoded;
994 }
995 else if (name.compare("endian") == 0)
996 {
997 ++num_keys_decoded;
998 if (value.compare("little") == 0)
999 byte_order = eByteOrderLittle;
1000 else if (value.compare("big") == 0)
1001 byte_order = eByteOrderBig;
1002 else if (value.compare("pdp") == 0)
1003 byte_order = eByteOrderPDP;
1004 else
1005 --num_keys_decoded;
1006 }
1007 else if (name.compare("ptrsize") == 0)
1008 {
1009 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 0);
1010 if (pointer_byte_size != 0)
1011 ++num_keys_decoded;
1012 }
1013 else if (name.compare("os_version") == 0)
1014 {
1015 Args::StringToVersion (value.c_str(),
1016 m_os_version_major,
1017 m_os_version_minor,
1018 m_os_version_update);
1019 if (m_os_version_major != UINT32_MAX)
1020 ++num_keys_decoded;
1021 }
1022 }
1023
1024 if (num_keys_decoded > 0)
1025 m_qHostInfo_is_valid = eLazyBoolYes;
1026
1027 if (triple.empty())
1028 {
1029 if (arch_name.empty())
1030 {
1031 if (cpu != LLDB_INVALID_CPUTYPE)
1032 {
1033 m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1034 if (pointer_byte_size)
1035 {
1036 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1037 }
1038 if (byte_order != eByteOrderInvalid)
1039 {
1040 assert (byte_order == m_host_arch.GetByteOrder());
1041 }
Greg Claytonb170aee2012-05-08 01:45:38 +00001042
1043 if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0)
1044 {
1045 switch (m_host_arch.GetMachine())
1046 {
1047 case llvm::Triple::arm:
1048 case llvm::Triple::thumb:
1049 os_name = "ios";
1050 break;
1051 default:
1052 os_name = "macosx";
1053 break;
1054 }
1055 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001056 if (!vendor_name.empty())
1057 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1058 if (!os_name.empty())
Greg Clayton89798cc2011-09-15 00:21:03 +00001059 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001060
1061 }
1062 }
1063 else
1064 {
1065 std::string triple;
1066 triple += arch_name;
Greg Claytonb170aee2012-05-08 01:45:38 +00001067 if (!vendor_name.empty() || !os_name.empty())
1068 {
1069 triple += '-';
1070 if (vendor_name.empty())
1071 triple += "unknown";
1072 else
1073 triple += vendor_name;
1074 triple += '-';
1075 if (os_name.empty())
1076 triple += "unknown";
1077 else
1078 triple += os_name;
1079 }
1080 m_host_arch.SetTriple (triple.c_str());
1081
1082 llvm::Triple &host_triple = m_host_arch.GetTriple();
1083 if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin)
1084 {
1085 switch (m_host_arch.GetMachine())
1086 {
1087 case llvm::Triple::arm:
1088 case llvm::Triple::thumb:
1089 host_triple.setOS(llvm::Triple::IOS);
1090 break;
1091 default:
1092 host_triple.setOS(llvm::Triple::MacOSX);
1093 break;
1094 }
1095 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001096 if (pointer_byte_size)
1097 {
1098 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1099 }
1100 if (byte_order != eByteOrderInvalid)
1101 {
1102 assert (byte_order == m_host_arch.GetByteOrder());
1103 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001104
Greg Clayton58e26e02011-03-24 04:28:38 +00001105 }
1106 }
1107 else
1108 {
Greg Claytonb170aee2012-05-08 01:45:38 +00001109 m_host_arch.SetTriple (triple.c_str());
Greg Claytoncb8977d2011-03-23 00:09:55 +00001110 if (pointer_byte_size)
1111 {
1112 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1113 }
1114 if (byte_order != eByteOrderInvalid)
1115 {
1116 assert (byte_order == m_host_arch.GetByteOrder());
1117 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001118 }
Greg Claytoncb8977d2011-03-23 00:09:55 +00001119 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001120 }
1121 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001122 return m_qHostInfo_is_valid == eLazyBoolYes;
Greg Clayton61d043b2011-03-22 04:00:09 +00001123}
1124
1125int
1126GDBRemoteCommunicationClient::SendAttach
1127(
1128 lldb::pid_t pid,
1129 StringExtractorGDBRemote& response
1130)
1131{
1132 if (pid != LLDB_INVALID_PROCESS_ID)
1133 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001134 char packet[64];
Greg Claytond9919d32011-12-01 23:28:38 +00001135 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%llx", pid);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001136 assert (packet_len < sizeof(packet));
1137 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton61d043b2011-03-22 04:00:09 +00001138 {
1139 if (response.IsErrorResponse())
1140 return response.GetError();
1141 return 0;
1142 }
1143 }
1144 return -1;
1145}
1146
1147const lldb_private::ArchSpec &
1148GDBRemoteCommunicationClient::GetHostArchitecture ()
1149{
Greg Clayton24bc5d92011-03-30 18:16:51 +00001150 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton61d043b2011-03-22 04:00:09 +00001151 GetHostInfo ();
Greg Claytoncb8977d2011-03-23 00:09:55 +00001152 return m_host_arch;
Greg Clayton61d043b2011-03-22 04:00:09 +00001153}
1154
1155addr_t
1156GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions)
1157{
Greg Clayton2f085c62011-05-15 01:25:55 +00001158 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton61d043b2011-03-22 04:00:09 +00001159 {
Greg Clayton2f085c62011-05-15 01:25:55 +00001160 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton989816b2011-05-14 01:50:35 +00001161 char packet[64];
1162 const int packet_len = ::snprintf (packet, sizeof(packet), "_M%zx,%s%s%s", size,
1163 permissions & lldb::ePermissionsReadable ? "r" : "",
1164 permissions & lldb::ePermissionsWritable ? "w" : "",
1165 permissions & lldb::ePermissionsExecutable ? "x" : "");
1166 assert (packet_len < sizeof(packet));
1167 StringExtractorGDBRemote response;
1168 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1169 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001170 if (!response.IsErrorResponse())
Greg Clayton989816b2011-05-14 01:50:35 +00001171 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1172 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001173 else
1174 {
1175 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1176 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001177 }
1178 return LLDB_INVALID_ADDRESS;
1179}
1180
1181bool
1182GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr)
1183{
Greg Clayton2f085c62011-05-15 01:25:55 +00001184 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton61d043b2011-03-22 04:00:09 +00001185 {
Greg Clayton2f085c62011-05-15 01:25:55 +00001186 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton989816b2011-05-14 01:50:35 +00001187 char packet[64];
1188 const int packet_len = ::snprintf(packet, sizeof(packet), "_m%llx", (uint64_t)addr);
1189 assert (packet_len < sizeof(packet));
1190 StringExtractorGDBRemote response;
1191 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1192 {
1193 if (response.IsOKResponse())
1194 return true;
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001195 }
1196 else
1197 {
1198 m_supports_alloc_dealloc_memory = eLazyBoolNo;
Greg Clayton989816b2011-05-14 01:50:35 +00001199 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001200 }
1201 return false;
1202}
1203
Greg Clayton516f0842012-04-11 00:24:49 +00001204bool
1205GDBRemoteCommunicationClient::Detach ()
1206{
1207 return SendPacket ("D", 1) > 0;
1208}
1209
Greg Claytona9385532011-11-18 07:03:08 +00001210Error
1211GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr,
1212 lldb_private::MemoryRegionInfo &region_info)
1213{
1214 Error error;
1215 region_info.Clear();
1216
1217 if (m_supports_memory_region_info != eLazyBoolNo)
1218 {
1219 m_supports_memory_region_info = eLazyBoolYes;
1220 char packet[64];
1221 const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%llx", (uint64_t)addr);
1222 assert (packet_len < sizeof(packet));
1223 StringExtractorGDBRemote response;
1224 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1225 {
1226 std::string name;
1227 std::string value;
1228 addr_t addr_value;
1229 bool success = true;
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001230 bool saw_permissions = false;
Greg Claytona9385532011-11-18 07:03:08 +00001231 while (success && response.GetNameColonValue(name, value))
1232 {
1233 if (name.compare ("start") == 0)
1234 {
1235 addr_value = Args::StringToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success);
1236 if (success)
1237 region_info.GetRange().SetRangeBase(addr_value);
1238 }
1239 else if (name.compare ("size") == 0)
1240 {
1241 addr_value = Args::StringToUInt64(value.c_str(), 0, 16, &success);
1242 if (success)
1243 region_info.GetRange().SetByteSize (addr_value);
1244 }
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001245 else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid())
Greg Claytona9385532011-11-18 07:03:08 +00001246 {
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001247 saw_permissions = true;
1248 if (region_info.GetRange().Contains (addr))
1249 {
1250 if (value.find('r') != std::string::npos)
1251 region_info.SetReadable (MemoryRegionInfo::eYes);
1252 else
1253 region_info.SetReadable (MemoryRegionInfo::eNo);
1254
1255 if (value.find('w') != std::string::npos)
1256 region_info.SetWritable (MemoryRegionInfo::eYes);
1257 else
1258 region_info.SetWritable (MemoryRegionInfo::eNo);
1259
1260 if (value.find('x') != std::string::npos)
1261 region_info.SetExecutable (MemoryRegionInfo::eYes);
1262 else
1263 region_info.SetExecutable (MemoryRegionInfo::eNo);
1264 }
1265 else
1266 {
1267 // The reported region does not contain this address -- we're looking at an unmapped page
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 if (name.compare ("error") == 0)
1274 {
1275 StringExtractorGDBRemote name_extractor;
1276 // Swap "value" over into "name_extractor"
1277 name_extractor.GetStringRef().swap(value);
1278 // Now convert the HEX bytes into a string value
1279 name_extractor.GetHexByteString (value);
1280 error.SetErrorString(value.c_str());
1281 }
1282 }
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001283
1284 // We got a valid address range back but no permissions -- which means this is an unmapped page
1285 if (region_info.GetRange().IsValid() && saw_permissions == false)
1286 {
1287 region_info.SetReadable (MemoryRegionInfo::eNo);
1288 region_info.SetWritable (MemoryRegionInfo::eNo);
1289 region_info.SetExecutable (MemoryRegionInfo::eNo);
1290 }
Greg Claytona9385532011-11-18 07:03:08 +00001291 }
1292 else
1293 {
1294 m_supports_memory_region_info = eLazyBoolNo;
1295 }
1296 }
1297
1298 if (m_supports_memory_region_info == eLazyBoolNo)
1299 {
1300 error.SetErrorString("qMemoryRegionInfo is not supported");
1301 }
1302 if (error.Fail())
1303 region_info.Clear();
1304 return error;
1305
1306}
1307
Johnny Chen7cbdcfb2012-05-23 21:09:52 +00001308Error
1309GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num)
1310{
1311 Error error;
1312
1313 if (m_supports_watchpoint_support_info == eLazyBoolYes)
1314 {
1315 num = m_num_supported_hardware_watchpoints;
1316 return error;
1317 }
1318
1319 // Set num to 0 first.
1320 num = 0;
1321 if (m_supports_watchpoint_support_info != eLazyBoolNo)
1322 {
1323 char packet[64];
1324 const int packet_len = ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:");
1325 assert (packet_len < sizeof(packet));
1326 StringExtractorGDBRemote response;
1327 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1328 {
1329 m_supports_watchpoint_support_info = eLazyBoolYes;
1330 std::string name;
1331 std::string value;
1332 while (response.GetNameColonValue(name, value))
1333 {
1334 if (name.compare ("num") == 0)
1335 {
1336 num = Args::StringToUInt32(value.c_str(), 0, 0);
1337 m_num_supported_hardware_watchpoints = num;
1338 }
1339 }
1340 }
1341 else
1342 {
1343 m_supports_watchpoint_support_info = eLazyBoolNo;
1344 }
1345 }
1346
1347 if (m_supports_watchpoint_support_info == eLazyBoolNo)
1348 {
1349 error.SetErrorString("qWatchpointSupportInfo is not supported");
1350 }
1351 return error;
1352
1353}
Greg Claytona9385532011-11-18 07:03:08 +00001354
Greg Clayton61d043b2011-03-22 04:00:09 +00001355int
1356GDBRemoteCommunicationClient::SetSTDIN (char const *path)
1357{
1358 if (path && path[0])
1359 {
1360 StreamString packet;
1361 packet.PutCString("QSetSTDIN:");
1362 packet.PutBytesAsRawHex8(path, strlen(path));
1363
1364 StringExtractorGDBRemote response;
1365 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1366 {
1367 if (response.IsOKResponse())
1368 return 0;
1369 uint8_t error = response.GetError();
1370 if (error)
1371 return error;
1372 }
1373 }
1374 return -1;
1375}
1376
1377int
1378GDBRemoteCommunicationClient::SetSTDOUT (char const *path)
1379{
1380 if (path && path[0])
1381 {
1382 StreamString packet;
1383 packet.PutCString("QSetSTDOUT:");
1384 packet.PutBytesAsRawHex8(path, strlen(path));
1385
1386 StringExtractorGDBRemote response;
1387 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1388 {
1389 if (response.IsOKResponse())
1390 return 0;
1391 uint8_t error = response.GetError();
1392 if (error)
1393 return error;
1394 }
1395 }
1396 return -1;
1397}
1398
1399int
1400GDBRemoteCommunicationClient::SetSTDERR (char const *path)
1401{
1402 if (path && path[0])
1403 {
1404 StreamString packet;
1405 packet.PutCString("QSetSTDERR:");
1406 packet.PutBytesAsRawHex8(path, strlen(path));
1407
1408 StringExtractorGDBRemote response;
1409 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1410 {
1411 if (response.IsOKResponse())
1412 return 0;
1413 uint8_t error = response.GetError();
1414 if (error)
1415 return error;
1416 }
1417 }
1418 return -1;
1419}
1420
1421int
1422GDBRemoteCommunicationClient::SetWorkingDir (char const *path)
1423{
1424 if (path && path[0])
1425 {
1426 StreamString packet;
1427 packet.PutCString("QSetWorkingDir:");
1428 packet.PutBytesAsRawHex8(path, strlen(path));
1429
1430 StringExtractorGDBRemote response;
1431 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1432 {
1433 if (response.IsOKResponse())
1434 return 0;
1435 uint8_t error = response.GetError();
1436 if (error)
1437 return error;
1438 }
1439 }
1440 return -1;
1441}
1442
1443int
1444GDBRemoteCommunicationClient::SetDisableASLR (bool enable)
1445{
Greg Clayton24bc5d92011-03-30 18:16:51 +00001446 char packet[32];
1447 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0);
1448 assert (packet_len < sizeof(packet));
Greg Clayton61d043b2011-03-22 04:00:09 +00001449 StringExtractorGDBRemote response;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001450 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton61d043b2011-03-22 04:00:09 +00001451 {
1452 if (response.IsOKResponse())
1453 return 0;
1454 uint8_t error = response.GetError();
1455 if (error)
1456 return error;
1457 }
1458 return -1;
1459}
Greg Clayton24bc5d92011-03-30 18:16:51 +00001460
1461bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001462GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001463{
1464 if (response.IsNormalResponse())
1465 {
1466 std::string name;
1467 std::string value;
1468 StringExtractor extractor;
1469
1470 while (response.GetNameColonValue(name, value))
1471 {
1472 if (name.compare("pid") == 0)
1473 {
1474 process_info.SetProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1475 }
1476 else if (name.compare("ppid") == 0)
1477 {
1478 process_info.SetParentProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1479 }
1480 else if (name.compare("uid") == 0)
1481 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001482 process_info.SetUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001483 }
1484 else if (name.compare("euid") == 0)
1485 {
1486 process_info.SetEffectiveUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1487 }
1488 else if (name.compare("gid") == 0)
1489 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001490 process_info.SetGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001491 }
1492 else if (name.compare("egid") == 0)
1493 {
1494 process_info.SetEffectiveGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1495 }
1496 else if (name.compare("triple") == 0)
1497 {
1498 // The triple comes as ASCII hex bytes since it contains '-' chars
1499 extractor.GetStringRef().swap(value);
1500 extractor.SetFilePos(0);
1501 extractor.GetHexByteString (value);
Greg Claytonb170aee2012-05-08 01:45:38 +00001502 process_info.GetArchitecture ().SetTriple (value.c_str());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001503 }
1504 else if (name.compare("name") == 0)
1505 {
1506 StringExtractor extractor;
Filipe Cabecinhase61ec7f2012-05-07 09:30:51 +00001507 // The process name from ASCII hex bytes since we can't
Greg Clayton24bc5d92011-03-30 18:16:51 +00001508 // control the characters in a process name
1509 extractor.GetStringRef().swap(value);
1510 extractor.SetFilePos(0);
1511 extractor.GetHexByteString (value);
Greg Clayton527154d2011-11-15 03:53:30 +00001512 process_info.GetExecutableFile().SetFile (value.c_str(), false);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001513 }
1514 }
1515
1516 if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
1517 return true;
1518 }
1519 return false;
1520}
1521
1522bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001523GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001524{
1525 process_info.Clear();
1526
1527 if (m_supports_qProcessInfoPID)
1528 {
1529 char packet[32];
Greg Claytond9919d32011-12-01 23:28:38 +00001530 const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%llu", pid);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001531 assert (packet_len < sizeof(packet));
1532 StringExtractorGDBRemote response;
1533 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1534 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001535 return DecodeProcessInfoResponse (response, process_info);
1536 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001537 else
1538 {
1539 m_supports_qProcessInfoPID = false;
1540 return false;
1541 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001542 }
1543 return false;
1544}
1545
1546uint32_t
Greg Claytonb72d0f02011-04-12 05:54:46 +00001547GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1548 ProcessInstanceInfoList &process_infos)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001549{
1550 process_infos.Clear();
1551
1552 if (m_supports_qfProcessInfo)
1553 {
1554 StreamString packet;
1555 packet.PutCString ("qfProcessInfo");
1556 if (!match_info.MatchAllProcesses())
1557 {
1558 packet.PutChar (':');
1559 const char *name = match_info.GetProcessInfo().GetName();
1560 bool has_name_match = false;
1561 if (name && name[0])
1562 {
1563 has_name_match = true;
1564 NameMatchType name_match_type = match_info.GetNameMatchType();
1565 switch (name_match_type)
1566 {
1567 case eNameMatchIgnore:
1568 has_name_match = false;
1569 break;
1570
1571 case eNameMatchEquals:
1572 packet.PutCString ("name_match:equals;");
1573 break;
1574
1575 case eNameMatchContains:
1576 packet.PutCString ("name_match:contains;");
1577 break;
1578
1579 case eNameMatchStartsWith:
1580 packet.PutCString ("name_match:starts_with;");
1581 break;
1582
1583 case eNameMatchEndsWith:
1584 packet.PutCString ("name_match:ends_with;");
1585 break;
1586
1587 case eNameMatchRegularExpression:
1588 packet.PutCString ("name_match:regex;");
1589 break;
1590 }
1591 if (has_name_match)
1592 {
1593 packet.PutCString ("name:");
1594 packet.PutBytesAsRawHex8(name, ::strlen(name));
1595 packet.PutChar (';');
1596 }
1597 }
1598
1599 if (match_info.GetProcessInfo().ProcessIDIsValid())
Greg Claytond9919d32011-12-01 23:28:38 +00001600 packet.Printf("pid:%llu;",match_info.GetProcessInfo().GetProcessID());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001601 if (match_info.GetProcessInfo().ParentProcessIDIsValid())
Greg Claytond9919d32011-12-01 23:28:38 +00001602 packet.Printf("parent_pid:%llu;",match_info.GetProcessInfo().GetParentProcessID());
Greg Claytonb72d0f02011-04-12 05:54:46 +00001603 if (match_info.GetProcessInfo().UserIDIsValid())
1604 packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID());
1605 if (match_info.GetProcessInfo().GroupIDIsValid())
1606 packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001607 if (match_info.GetProcessInfo().EffectiveUserIDIsValid())
1608 packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID());
1609 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1610 packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID());
1611 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1612 packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0);
1613 if (match_info.GetProcessInfo().GetArchitecture().IsValid())
1614 {
1615 const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture();
1616 const llvm::Triple &triple = match_arch.GetTriple();
1617 packet.PutCString("triple:");
1618 packet.PutCStringAsRawHex8(triple.getTriple().c_str());
1619 packet.PutChar (';');
1620 }
1621 }
1622 StringExtractorGDBRemote response;
1623 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1624 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001625 do
1626 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001627 ProcessInstanceInfo process_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001628 if (!DecodeProcessInfoResponse (response, process_info))
1629 break;
1630 process_infos.Append(process_info);
1631 response.GetStringRef().clear();
1632 response.SetFilePos(0);
1633 } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false));
1634 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001635 else
1636 {
1637 m_supports_qfProcessInfo = false;
1638 return 0;
1639 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001640 }
1641 return process_infos.GetSize();
1642
1643}
1644
1645bool
1646GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name)
1647{
1648 if (m_supports_qUserName)
1649 {
1650 char packet[32];
1651 const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid);
1652 assert (packet_len < sizeof(packet));
1653 StringExtractorGDBRemote response;
1654 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1655 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001656 if (response.IsNormalResponse())
1657 {
1658 // Make sure we parsed the right number of characters. The response is
1659 // the hex encoded user name and should make up the entire packet.
1660 // If there are any non-hex ASCII bytes, the length won't match below..
1661 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
1662 return true;
1663 }
1664 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001665 else
1666 {
1667 m_supports_qUserName = false;
1668 return false;
1669 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001670 }
1671 return false;
1672
1673}
1674
1675bool
1676GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name)
1677{
1678 if (m_supports_qGroupName)
1679 {
1680 char packet[32];
1681 const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid);
1682 assert (packet_len < sizeof(packet));
1683 StringExtractorGDBRemote response;
1684 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1685 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001686 if (response.IsNormalResponse())
1687 {
1688 // Make sure we parsed the right number of characters. The response is
1689 // the hex encoded group name and should make up the entire packet.
1690 // If there are any non-hex ASCII bytes, the length won't match below..
1691 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
1692 return true;
1693 }
1694 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001695 else
1696 {
1697 m_supports_qGroupName = false;
1698 return false;
1699 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001700 }
1701 return false;
Greg Clayton06d7cc82011-04-04 18:18:57 +00001702}
Greg Clayton24bc5d92011-03-30 18:16:51 +00001703
Greg Clayton06d7cc82011-04-04 18:18:57 +00001704void
1705GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets)
1706{
1707 uint32_t i;
1708 TimeValue start_time, end_time;
1709 uint64_t total_time_nsec;
1710 float packets_per_second;
1711 if (SendSpeedTestPacket (0, 0))
1712 {
1713 for (uint32_t send_size = 0; send_size <= 1024; send_size *= 2)
1714 {
1715 for (uint32_t recv_size = 0; recv_size <= 1024; recv_size *= 2)
1716 {
1717 start_time = TimeValue::Now();
1718 for (i=0; i<num_packets; ++i)
1719 {
1720 SendSpeedTestPacket (send_size, recv_size);
1721 }
1722 end_time = TimeValue::Now();
1723 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001724 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Greg Clayton153ccd72011-08-10 02:10:13 +00001725 printf ("%u qSpeedTest(send=%-5u, recv=%-5u) in %llu.%9.9llu sec for %f packets/sec.\n",
Greg Clayton06d7cc82011-04-04 18:18:57 +00001726 num_packets,
1727 send_size,
1728 recv_size,
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001729 total_time_nsec / TimeValue::NanoSecPerSec,
1730 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton06d7cc82011-04-04 18:18:57 +00001731 packets_per_second);
1732 if (recv_size == 0)
1733 recv_size = 32;
1734 }
1735 if (send_size == 0)
1736 send_size = 32;
1737 }
1738 }
1739 else
1740 {
1741 start_time = TimeValue::Now();
1742 for (i=0; i<num_packets; ++i)
1743 {
1744 GetCurrentProcessID ();
1745 }
1746 end_time = TimeValue::Now();
1747 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001748 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Greg Clayton153ccd72011-08-10 02:10:13 +00001749 printf ("%u 'qC' packets packets in 0x%llu%9.9llu sec for %f packets/sec.\n",
Greg Clayton06d7cc82011-04-04 18:18:57 +00001750 num_packets,
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00001751 total_time_nsec / TimeValue::NanoSecPerSec,
1752 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton06d7cc82011-04-04 18:18:57 +00001753 packets_per_second);
1754 }
1755}
1756
1757bool
1758GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size)
1759{
1760 StreamString packet;
1761 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size);
1762 uint32_t bytes_left = send_size;
1763 while (bytes_left > 0)
1764 {
1765 if (bytes_left >= 26)
1766 {
1767 packet.PutCString("abcdefghijklmnopqrstuvwxyz");
1768 bytes_left -= 26;
1769 }
1770 else
1771 {
1772 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz");
1773 bytes_left = 0;
1774 }
1775 }
1776
1777 StringExtractorGDBRemote response;
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001778 return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) > 0;
Greg Clayton06d7cc82011-04-04 18:18:57 +00001779 return false;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001780}
Greg Claytonb72d0f02011-04-12 05:54:46 +00001781
1782uint16_t
1783GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort ()
1784{
1785 StringExtractorGDBRemote response;
1786 if (SendPacketAndWaitForResponse("qLaunchGDBServer", strlen("qLaunchGDBServer"), response, false))
1787 {
1788 std::string name;
1789 std::string value;
1790 uint16_t port = 0;
1791 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
1792 while (response.GetNameColonValue(name, value))
1793 {
1794 if (name.size() == 4 && name.compare("port") == 0)
1795 port = Args::StringToUInt32(value.c_str(), 0, 0);
1796 if (name.size() == 3 && name.compare("pid") == 0)
1797 pid = Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0);
1798 }
1799 return port;
1800 }
1801 return 0;
1802}
1803
1804bool
1805GDBRemoteCommunicationClient::SetCurrentThread (int tid)
1806{
1807 if (m_curr_tid == tid)
1808 return true;
1809
1810 char packet[32];
1811 int packet_len;
1812 if (tid <= 0)
1813 packet_len = ::snprintf (packet, sizeof(packet), "Hg%i", tid);
1814 else
1815 packet_len = ::snprintf (packet, sizeof(packet), "Hg%x", tid);
1816 assert (packet_len + 1 < sizeof(packet));
1817 StringExtractorGDBRemote response;
1818 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
1819 {
1820 if (response.IsOKResponse())
1821 {
1822 m_curr_tid = tid;
1823 return true;
1824 }
1825 }
1826 return false;
1827}
1828
1829bool
1830GDBRemoteCommunicationClient::SetCurrentThreadForRun (int tid)
1831{
1832 if (m_curr_tid_run == tid)
1833 return true;
1834
1835 char packet[32];
1836 int packet_len;
1837 if (tid <= 0)
1838 packet_len = ::snprintf (packet, sizeof(packet), "Hc%i", tid);
1839 else
1840 packet_len = ::snprintf (packet, sizeof(packet), "Hc%x", tid);
1841
1842 assert (packet_len + 1 < sizeof(packet));
1843 StringExtractorGDBRemote response;
1844 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
1845 {
1846 if (response.IsOKResponse())
1847 {
1848 m_curr_tid_run = tid;
1849 return true;
1850 }
1851 }
1852 return false;
1853}
1854
1855bool
1856GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response)
1857{
1858 if (SendPacketAndWaitForResponse("?", 1, response, false))
1859 return response.IsNormalResponse();
1860 return false;
1861}
1862
1863bool
1864GDBRemoteCommunicationClient::GetThreadStopInfo (uint32_t tid, StringExtractorGDBRemote &response)
1865{
1866 if (m_supports_qThreadStopInfo)
1867 {
1868 char packet[256];
1869 int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%x", tid);
1870 assert (packet_len < sizeof(packet));
1871 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
1872 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001873 if (response.IsNormalResponse())
Greg Claytonb72d0f02011-04-12 05:54:46 +00001874 return true;
1875 else
1876 return false;
1877 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001878 else
1879 {
1880 m_supports_qThreadStopInfo = false;
1881 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00001882 }
Greg Clayton139da722011-05-20 03:15:54 +00001883// if (SetCurrentThread (tid))
1884// return GetStopReply (response);
Greg Claytonb72d0f02011-04-12 05:54:46 +00001885 return false;
1886}
1887
1888
1889uint8_t
1890GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length)
1891{
1892 switch (type)
1893 {
1894 case eBreakpointSoftware: if (!m_supports_z0) return UINT8_MAX; break;
1895 case eBreakpointHardware: if (!m_supports_z1) return UINT8_MAX; break;
1896 case eWatchpointWrite: if (!m_supports_z2) return UINT8_MAX; break;
1897 case eWatchpointRead: if (!m_supports_z3) return UINT8_MAX; break;
1898 case eWatchpointReadWrite: if (!m_supports_z4) return UINT8_MAX; break;
1899 default: return UINT8_MAX;
1900 }
1901
1902 char packet[64];
1903 const int packet_len = ::snprintf (packet,
1904 sizeof(packet),
1905 "%c%i,%llx,%x",
1906 insert ? 'Z' : 'z',
1907 type,
1908 addr,
1909 length);
1910
1911 assert (packet_len + 1 < sizeof(packet));
1912 StringExtractorGDBRemote response;
1913 if (SendPacketAndWaitForResponse(packet, packet_len, response, true))
1914 {
1915 if (response.IsOKResponse())
1916 return 0;
Greg Claytonb72d0f02011-04-12 05:54:46 +00001917 else if (response.IsErrorResponse())
1918 return response.GetError();
1919 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001920 else
1921 {
1922 switch (type)
1923 {
1924 case eBreakpointSoftware: m_supports_z0 = false; break;
1925 case eBreakpointHardware: m_supports_z1 = false; break;
1926 case eWatchpointWrite: m_supports_z2 = false; break;
1927 case eWatchpointRead: m_supports_z3 = false; break;
1928 case eWatchpointReadWrite: m_supports_z4 = false; break;
1929 default: break;
1930 }
1931 }
1932
Greg Claytonb72d0f02011-04-12 05:54:46 +00001933 return UINT8_MAX;
1934}
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001935
1936size_t
1937GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
1938 bool &sequence_mutex_unavailable)
1939{
1940 Mutex::Locker locker;
1941 thread_ids.clear();
1942
Greg Claytonc8dd5702012-04-12 19:04:34 +00001943 if (GetSequenceMutex (locker))
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001944 {
1945 sequence_mutex_unavailable = false;
1946 StringExtractorGDBRemote response;
1947
Greg Clayton63afdb02011-06-17 01:22:15 +00001948 for (SendPacketNoLock ("qfThreadInfo", strlen("qfThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001949 response.IsNormalResponse();
Greg Clayton63afdb02011-06-17 01:22:15 +00001950 SendPacketNoLock ("qsThreadInfo", strlen("qsThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()))
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001951 {
1952 char ch = response.GetChar();
1953 if (ch == 'l')
1954 break;
1955 if (ch == 'm')
1956 {
1957 do
1958 {
1959 tid_t tid = response.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID);
1960
1961 if (tid != LLDB_INVALID_THREAD_ID)
1962 {
1963 thread_ids.push_back (tid);
1964 }
1965 ch = response.GetChar(); // Skip the command separator
1966 } while (ch == ','); // Make sure we got a comma separator
1967 }
1968 }
1969 }
1970 else
1971 {
Greg Claytonc8dd5702012-04-12 19:04:34 +00001972 LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
1973 if (log)
1974 log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'");
Greg Clayton4a60f9e2011-05-20 23:38:13 +00001975 sequence_mutex_unavailable = true;
1976 }
1977 return thread_ids.size();
1978}
Greg Clayton516f0842012-04-11 00:24:49 +00001979
1980lldb::addr_t
1981GDBRemoteCommunicationClient::GetShlibInfoAddr()
1982{
1983 if (!IsRunning())
1984 {
1985 StringExtractorGDBRemote response;
1986 if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false))
1987 {
1988 if (response.IsNormalResponse())
1989 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1990 }
1991 }
1992 return LLDB_INVALID_ADDRESS;
1993}
1994