blob: 1de76a44690e06bdd522738f9abf83d04cbe30cb [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
Han Ming Ong0df19cc2013-01-18 23:11:53 +000015#include <sstream>
16
Greg Clayton61d043b2011-03-22 04:00:09 +000017// Other libraries and framework includes
18#include "llvm/ADT/Triple.h"
19#include "lldb/Interpreter/Args.h"
20#include "lldb/Core/ConnectionFileDescriptor.h"
21#include "lldb/Core/Log.h"
22#include "lldb/Core/State.h"
23#include "lldb/Core/StreamString.h"
24#include "lldb/Host/Endian.h"
25#include "lldb/Host/Host.h"
26#include "lldb/Host/TimeValue.h"
27
28// Project includes
29#include "Utility/StringExtractorGDBRemote.h"
30#include "ProcessGDBRemote.h"
31#include "ProcessGDBRemoteLog.h"
32
33using namespace lldb;
34using namespace lldb_private;
35
36//----------------------------------------------------------------------
37// GDBRemoteCommunicationClient constructor
38//----------------------------------------------------------------------
Greg Claytonb72d0f02011-04-12 05:54:46 +000039GDBRemoteCommunicationClient::GDBRemoteCommunicationClient(bool is_platform) :
40 GDBRemoteCommunication("gdb-remote.client", "gdb-remote.client.rx_packet", is_platform),
Greg Clayton61d043b2011-03-22 04:00:09 +000041 m_supports_not_sending_acks (eLazyBoolCalculate),
42 m_supports_thread_suffix (eLazyBoolCalculate),
Greg Claytona1f645e2012-04-10 03:22:03 +000043 m_supports_threads_in_stop_reply (eLazyBoolCalculate),
Greg Clayton61d043b2011-03-22 04:00:09 +000044 m_supports_vCont_all (eLazyBoolCalculate),
45 m_supports_vCont_any (eLazyBoolCalculate),
46 m_supports_vCont_c (eLazyBoolCalculate),
47 m_supports_vCont_C (eLazyBoolCalculate),
48 m_supports_vCont_s (eLazyBoolCalculate),
49 m_supports_vCont_S (eLazyBoolCalculate),
Greg Clayton24bc5d92011-03-30 18:16:51 +000050 m_qHostInfo_is_valid (eLazyBoolCalculate),
Jason Molendafe555672012-12-19 02:54:03 +000051 m_qProcessInfo_is_valid (eLazyBoolCalculate),
Greg Clayton2f085c62011-05-15 01:25:55 +000052 m_supports_alloc_dealloc_memory (eLazyBoolCalculate),
Greg Claytona9385532011-11-18 07:03:08 +000053 m_supports_memory_region_info (eLazyBoolCalculate),
Johnny Chen7cbdcfb2012-05-23 21:09:52 +000054 m_supports_watchpoint_support_info (eLazyBoolCalculate),
Enrico Granata7de2a3b2012-07-13 23:18:48 +000055 m_watchpoints_trigger_after_instruction(eLazyBoolCalculate),
Jim Ingham3a458eb2012-07-20 21:37:13 +000056 m_attach_or_wait_reply(eLazyBoolCalculate),
Jim Ingham73f6b492012-07-25 21:12:43 +000057 m_prepare_for_reg_writing_reply (eLazyBoolCalculate),
Greg Clayton24bc5d92011-03-30 18:16:51 +000058 m_supports_qProcessInfoPID (true),
59 m_supports_qfProcessInfo (true),
60 m_supports_qUserName (true),
61 m_supports_qGroupName (true),
Greg Claytonb72d0f02011-04-12 05:54:46 +000062 m_supports_qThreadStopInfo (true),
63 m_supports_z0 (true),
64 m_supports_z1 (true),
65 m_supports_z2 (true),
66 m_supports_z3 (true),
67 m_supports_z4 (true),
68 m_curr_tid (LLDB_INVALID_THREAD_ID),
69 m_curr_tid_run (LLDB_INVALID_THREAD_ID),
Johnny Chen7cbdcfb2012-05-23 21:09:52 +000070 m_num_supported_hardware_watchpoints (0),
Greg Clayton61d043b2011-03-22 04:00:09 +000071 m_async_mutex (Mutex::eMutexTypeRecursive),
72 m_async_packet_predicate (false),
73 m_async_packet (),
74 m_async_response (),
75 m_async_signal (-1),
Han Ming Ong0df19cc2013-01-18 23:11:53 +000076 m_thread_id_to_used_usec_map (),
Greg Clayton58e26e02011-03-24 04:28:38 +000077 m_host_arch(),
Jason Molendafe555672012-12-19 02:54:03 +000078 m_process_arch(),
Greg Clayton58e26e02011-03-24 04:28:38 +000079 m_os_version_major (UINT32_MAX),
80 m_os_version_minor (UINT32_MAX),
81 m_os_version_update (UINT32_MAX)
Greg Clayton61d043b2011-03-22 04:00:09 +000082{
Greg Clayton61d043b2011-03-22 04:00:09 +000083}
84
85//----------------------------------------------------------------------
86// Destructor
87//----------------------------------------------------------------------
88GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient()
89{
Greg Clayton61d043b2011-03-22 04:00:09 +000090 if (IsConnected())
Greg Clayton61d043b2011-03-22 04:00:09 +000091 Disconnect();
Greg Clayton61d043b2011-03-22 04:00:09 +000092}
93
94bool
Greg Clayton58e26e02011-03-24 04:28:38 +000095GDBRemoteCommunicationClient::HandshakeWithServer (Error *error_ptr)
96{
97 // Start the read thread after we send the handshake ack since if we
98 // fail to send the handshake ack, there is no reason to continue...
99 if (SendAck())
Greg Clayton63afdb02011-06-17 01:22:15 +0000100 return true;
Greg Clayton58e26e02011-03-24 04:28:38 +0000101
102 if (error_ptr)
103 error_ptr->SetErrorString("failed to send the handshake ack");
104 return false;
105}
106
107void
108GDBRemoteCommunicationClient::QueryNoAckModeSupported ()
Greg Clayton61d043b2011-03-22 04:00:09 +0000109{
110 if (m_supports_not_sending_acks == eLazyBoolCalculate)
111 {
Greg Clayton58e26e02011-03-24 04:28:38 +0000112 m_send_acks = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000113 m_supports_not_sending_acks = eLazyBoolNo;
Greg Clayton58e26e02011-03-24 04:28:38 +0000114
115 StringExtractorGDBRemote response;
Greg Clayton61d043b2011-03-22 04:00:09 +0000116 if (SendPacketAndWaitForResponse("QStartNoAckMode", response, false))
117 {
118 if (response.IsOKResponse())
Greg Clayton58e26e02011-03-24 04:28:38 +0000119 {
120 m_send_acks = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000121 m_supports_not_sending_acks = eLazyBoolYes;
Greg Clayton58e26e02011-03-24 04:28:38 +0000122 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000123 }
124 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000125}
126
127void
Greg Claytona1f645e2012-04-10 03:22:03 +0000128GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported ()
129{
130 if (m_supports_threads_in_stop_reply == eLazyBoolCalculate)
131 {
132 m_supports_threads_in_stop_reply = eLazyBoolNo;
133
134 StringExtractorGDBRemote response;
135 if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response, false))
136 {
137 if (response.IsOKResponse())
138 m_supports_threads_in_stop_reply = eLazyBoolYes;
139 }
140 }
141}
142
Jim Ingham3a458eb2012-07-20 21:37:13 +0000143bool
144GDBRemoteCommunicationClient::GetVAttachOrWaitSupported ()
145{
146 if (m_attach_or_wait_reply == eLazyBoolCalculate)
147 {
148 m_attach_or_wait_reply = eLazyBoolNo;
149
150 StringExtractorGDBRemote response;
151 if (SendPacketAndWaitForResponse("qVAttachOrWaitSupported", response, false))
152 {
153 if (response.IsOKResponse())
154 m_attach_or_wait_reply = eLazyBoolYes;
155 }
156 }
157 if (m_attach_or_wait_reply == eLazyBoolYes)
158 return true;
159 else
160 return false;
161}
162
Jim Ingham73f6b492012-07-25 21:12:43 +0000163bool
164GDBRemoteCommunicationClient::GetSyncThreadStateSupported ()
165{
166 if (m_prepare_for_reg_writing_reply == eLazyBoolCalculate)
167 {
168 m_prepare_for_reg_writing_reply = eLazyBoolNo;
169
170 StringExtractorGDBRemote response;
171 if (SendPacketAndWaitForResponse("qSyncThreadStateSupported", response, false))
172 {
173 if (response.IsOKResponse())
174 m_prepare_for_reg_writing_reply = eLazyBoolYes;
175 }
176 }
177 if (m_prepare_for_reg_writing_reply == eLazyBoolYes)
178 return true;
179 else
180 return false;
181}
182
Greg Claytona1f645e2012-04-10 03:22:03 +0000183
184void
Greg Clayton61d043b2011-03-22 04:00:09 +0000185GDBRemoteCommunicationClient::ResetDiscoverableSettings()
186{
187 m_supports_not_sending_acks = eLazyBoolCalculate;
188 m_supports_thread_suffix = eLazyBoolCalculate;
Greg Claytona1f645e2012-04-10 03:22:03 +0000189 m_supports_threads_in_stop_reply = eLazyBoolCalculate;
Greg Clayton61d043b2011-03-22 04:00:09 +0000190 m_supports_vCont_c = eLazyBoolCalculate;
191 m_supports_vCont_C = eLazyBoolCalculate;
192 m_supports_vCont_s = eLazyBoolCalculate;
193 m_supports_vCont_S = eLazyBoolCalculate;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000194 m_qHostInfo_is_valid = eLazyBoolCalculate;
Jason Molendafe555672012-12-19 02:54:03 +0000195 m_qProcessInfo_is_valid = eLazyBoolCalculate;
Greg Clayton2f085c62011-05-15 01:25:55 +0000196 m_supports_alloc_dealloc_memory = eLazyBoolCalculate;
Greg Claytona9385532011-11-18 07:03:08 +0000197 m_supports_memory_region_info = eLazyBoolCalculate;
Jim Ingham73f6b492012-07-25 21:12:43 +0000198 m_prepare_for_reg_writing_reply = eLazyBoolCalculate;
199 m_attach_or_wait_reply = eLazyBoolCalculate;
Greg Clayton989816b2011-05-14 01:50:35 +0000200
Greg Clayton24bc5d92011-03-30 18:16:51 +0000201 m_supports_qProcessInfoPID = true;
202 m_supports_qfProcessInfo = true;
203 m_supports_qUserName = true;
204 m_supports_qGroupName = true;
Greg Claytonb72d0f02011-04-12 05:54:46 +0000205 m_supports_qThreadStopInfo = true;
206 m_supports_z0 = true;
207 m_supports_z1 = true;
208 m_supports_z2 = true;
209 m_supports_z3 = true;
210 m_supports_z4 = true;
Greg Claytoncb8977d2011-03-23 00:09:55 +0000211 m_host_arch.Clear();
Jason Molendafe555672012-12-19 02:54:03 +0000212 m_process_arch.Clear();
Greg Clayton61d043b2011-03-22 04:00:09 +0000213}
214
215
216bool
217GDBRemoteCommunicationClient::GetThreadSuffixSupported ()
218{
219 if (m_supports_thread_suffix == eLazyBoolCalculate)
220 {
221 StringExtractorGDBRemote response;
222 m_supports_thread_suffix = eLazyBoolNo;
223 if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response, false))
224 {
225 if (response.IsOKResponse())
226 m_supports_thread_suffix = eLazyBoolYes;
227 }
228 }
229 return m_supports_thread_suffix;
230}
231bool
232GDBRemoteCommunicationClient::GetVContSupported (char flavor)
233{
234 if (m_supports_vCont_c == eLazyBoolCalculate)
235 {
236 StringExtractorGDBRemote response;
237 m_supports_vCont_any = eLazyBoolNo;
238 m_supports_vCont_all = eLazyBoolNo;
239 m_supports_vCont_c = eLazyBoolNo;
240 m_supports_vCont_C = eLazyBoolNo;
241 m_supports_vCont_s = eLazyBoolNo;
242 m_supports_vCont_S = eLazyBoolNo;
243 if (SendPacketAndWaitForResponse("vCont?", response, false))
244 {
245 const char *response_cstr = response.GetStringRef().c_str();
246 if (::strstr (response_cstr, ";c"))
247 m_supports_vCont_c = eLazyBoolYes;
248
249 if (::strstr (response_cstr, ";C"))
250 m_supports_vCont_C = eLazyBoolYes;
251
252 if (::strstr (response_cstr, ";s"))
253 m_supports_vCont_s = eLazyBoolYes;
254
255 if (::strstr (response_cstr, ";S"))
256 m_supports_vCont_S = eLazyBoolYes;
257
258 if (m_supports_vCont_c == eLazyBoolYes &&
259 m_supports_vCont_C == eLazyBoolYes &&
260 m_supports_vCont_s == eLazyBoolYes &&
261 m_supports_vCont_S == eLazyBoolYes)
262 {
263 m_supports_vCont_all = eLazyBoolYes;
264 }
265
266 if (m_supports_vCont_c == eLazyBoolYes ||
267 m_supports_vCont_C == eLazyBoolYes ||
268 m_supports_vCont_s == eLazyBoolYes ||
269 m_supports_vCont_S == eLazyBoolYes)
270 {
271 m_supports_vCont_any = eLazyBoolYes;
272 }
273 }
274 }
275
276 switch (flavor)
277 {
278 case 'a': return m_supports_vCont_any;
279 case 'A': return m_supports_vCont_all;
280 case 'c': return m_supports_vCont_c;
281 case 'C': return m_supports_vCont_C;
282 case 's': return m_supports_vCont_s;
283 case 'S': return m_supports_vCont_S;
284 default: break;
285 }
286 return false;
287}
288
289
290size_t
291GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
292(
293 const char *payload,
294 StringExtractorGDBRemote &response,
295 bool send_async
296)
297{
298 return SendPacketAndWaitForResponse (payload,
299 ::strlen (payload),
300 response,
301 send_async);
302}
303
304size_t
305GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
306(
307 const char *payload,
308 size_t payload_length,
309 StringExtractorGDBRemote &response,
310 bool send_async
311)
312{
313 Mutex::Locker locker;
Greg Clayton61d043b2011-03-22 04:00:09 +0000314 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton801417e2011-07-07 01:59:51 +0000315 size_t response_len = 0;
Greg Claytonc8dd5702012-04-12 19:04:34 +0000316 if (GetSequenceMutex (locker))
Greg Clayton61d043b2011-03-22 04:00:09 +0000317 {
Greg Clayton139da722011-05-20 03:15:54 +0000318 if (SendPacketNoLock (payload, payload_length))
Greg Clayton801417e2011-07-07 01:59:51 +0000319 response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
320 else
321 {
322 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000323 log->Printf("error: failed to send '%*s'", (int) payload_length, payload);
Greg Clayton801417e2011-07-07 01:59:51 +0000324 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000325 }
326 else
327 {
328 if (send_async)
329 {
Greg Clayton70e167f2012-05-31 21:24:20 +0000330 if (IsRunning())
Greg Clayton61d043b2011-03-22 04:00:09 +0000331 {
Greg Clayton70e167f2012-05-31 21:24:20 +0000332 Mutex::Locker async_locker (m_async_mutex);
333 m_async_packet.assign(payload, payload_length);
334 m_async_packet_predicate.SetValue (true, eBroadcastNever);
335
336 if (log)
337 log->Printf ("async: async packet = %s", m_async_packet.c_str());
338
339 bool timed_out = false;
340 if (SendInterrupt(locker, 2, timed_out))
Greg Clayton61d043b2011-03-22 04:00:09 +0000341 {
Greg Clayton70e167f2012-05-31 21:24:20 +0000342 if (m_interrupt_sent)
Greg Clayton61d043b2011-03-22 04:00:09 +0000343 {
Jim Ingham8247e622012-06-06 00:32:39 +0000344 m_interrupt_sent = false;
Greg Clayton70e167f2012-05-31 21:24:20 +0000345 TimeValue timeout_time;
346 timeout_time = TimeValue::Now();
347 timeout_time.OffsetWithSeconds (m_packet_timeout);
348
Greg Clayton61d043b2011-03-22 04:00:09 +0000349 if (log)
Greg Clayton70e167f2012-05-31 21:24:20 +0000350 log->Printf ("async: sent interrupt");
Greg Clayton801417e2011-07-07 01:59:51 +0000351
Greg Clayton70e167f2012-05-31 21:24:20 +0000352 if (m_async_packet_predicate.WaitForValueEqualTo (false, &timeout_time, &timed_out))
Greg Claytonb7669b32011-10-27 22:04:16 +0000353 {
Greg Clayton70e167f2012-05-31 21:24:20 +0000354 if (log)
355 log->Printf ("async: got response");
356
357 // Swap the response buffer to avoid malloc and string copy
358 response.GetStringRef().swap (m_async_response.GetStringRef());
359 response_len = response.GetStringRef().size();
360 }
361 else
362 {
363 if (log)
364 log->Printf ("async: timed out waiting for response");
365 }
366
367 // Make sure we wait until the continue packet has been sent again...
368 if (m_private_is_running.WaitForValueEqualTo (true, &timeout_time, &timed_out))
369 {
370 if (log)
371 {
372 if (timed_out)
373 log->Printf ("async: timed out waiting for process to resume, but process was resumed");
374 else
375 log->Printf ("async: async packet sent");
376 }
377 }
378 else
379 {
380 if (log)
381 log->Printf ("async: timed out waiting for process to resume");
Greg Claytonb7669b32011-10-27 22:04:16 +0000382 }
383 }
384 else
385 {
Greg Clayton70e167f2012-05-31 21:24:20 +0000386 // We had a racy condition where we went to send the interrupt
387 // yet we were able to get the lock, so the process must have
388 // just stopped?
Greg Clayton61d043b2011-03-22 04:00:09 +0000389 if (log)
Greg Clayton70e167f2012-05-31 21:24:20 +0000390 log->Printf ("async: got lock without sending interrupt");
391 // Send the packet normally since we got the lock
392 if (SendPacketNoLock (payload, payload_length))
393 response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
394 else
395 {
396 if (log)
397 log->Printf("error: failed to send '%*s'", (int) payload_length, payload);
398 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000399 }
400 }
401 else
402 {
Greg Clayton801417e2011-07-07 01:59:51 +0000403 if (log)
Greg Clayton70e167f2012-05-31 21:24:20 +0000404 log->Printf ("async: failed to interrupt");
Greg Clayton61d043b2011-03-22 04:00:09 +0000405 }
406 }
407 else
408 {
409 if (log)
Greg Clayton70e167f2012-05-31 21:24:20 +0000410 log->Printf ("async: not running, async is ignored");
Greg Clayton61d043b2011-03-22 04:00:09 +0000411 }
412 }
413 else
414 {
415 if (log)
Greg Claytonc8dd5702012-04-12 19:04:34 +0000416 log->Printf("error: failed to get packet sequence mutex, not sending packet '%*s'", (int) payload_length, payload);
Greg Clayton61d043b2011-03-22 04:00:09 +0000417 }
418 }
Greg Clayton801417e2011-07-07 01:59:51 +0000419 if (response_len == 0)
420 {
421 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000422 log->Printf("error: failed to get response for '%*s'", (int) payload_length, payload);
Greg Clayton801417e2011-07-07 01:59:51 +0000423 }
424 return response_len;
Greg Clayton61d043b2011-03-22 04:00:09 +0000425}
426
Han Ming Ong0df19cc2013-01-18 23:11:53 +0000427static const char *end_delimiter = "--end--;";
428static const int end_delimiter_len = 8;
429
430std::string
431GDBRemoteCommunicationClient::HarmonizeThreadIdsForProfileData
432( ProcessGDBRemote *process,
433 StringExtractorGDBRemote& profileDataExtractor
434)
435{
436 std::map<uint64_t, uint32_t> new_thread_id_to_used_usec_map;
437 std::stringstream final_output;
438 std::string name, value;
439
440 // Going to assuming thread_used_usec comes first, else bail out.
441 while (profileDataExtractor.GetNameColonValue(name, value))
442 {
443 if (name.compare("thread_used_id") == 0)
444 {
445 StringExtractor threadIDHexExtractor(value.c_str());
446 uint64_t thread_id = threadIDHexExtractor.GetHexMaxU64(false, 0);
447
448 bool has_used_usec = false;
449 uint32_t curr_used_usec = 0;
450 std::string usec_name, usec_value;
451 uint32_t input_file_pos = profileDataExtractor.GetFilePos();
452 if (profileDataExtractor.GetNameColonValue(usec_name, usec_value))
453 {
454 if (usec_name.compare("thread_used_usec") == 0)
455 {
456 has_used_usec = true;
457 curr_used_usec = strtoull(usec_value.c_str(), NULL, 0);
458 }
459 else
460 {
461 // We didn't find what we want, it is probably
462 // an older version. Bail out.
463 profileDataExtractor.SetFilePos(input_file_pos);
464 }
465 }
466
467 if (has_used_usec)
468 {
469 uint32_t prev_used_usec = 0;
470 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_used_usec_map.find(thread_id);
471 if (iterator != m_thread_id_to_used_usec_map.end())
472 {
473 prev_used_usec = m_thread_id_to_used_usec_map[thread_id];
474 }
475
476 uint32_t real_used_usec = curr_used_usec - prev_used_usec;
477 // A good first time record is one that runs for at least 0.25 sec
478 bool good_first_time = (prev_used_usec == 0) && (real_used_usec > 250000);
479 bool good_subsequent_time = (prev_used_usec > 0) &&
480 ((real_used_usec > 0) || (process->HasAssignedIndexIDToThread(thread_id)));
481
482 if (good_first_time || good_subsequent_time)
483 {
484 // We try to avoid doing too many index id reservation,
485 // resulting in fast increase of index ids.
486
487 final_output << name << ":";
488 int32_t index_id = process->AssignIndexIDToThread(thread_id);
489 final_output << index_id << ";";
490
491 final_output << usec_name << ":" << usec_value << ";";
492 }
493 else
494 {
495 // Skip past 'thread_used_name'.
496 std::string local_name, local_value;
497 profileDataExtractor.GetNameColonValue(local_name, local_value);
498 }
499
500 // Store current time as previous time so that they can be compared later.
501 new_thread_id_to_used_usec_map[thread_id] = curr_used_usec;
502 }
503 else
504 {
505 // Bail out and use old string.
506 final_output << name << ":" << value << ";";
507 }
508 }
509 else
510 {
511 final_output << name << ":" << value << ";";
512 }
513 }
514 final_output << end_delimiter;
515 m_thread_id_to_used_usec_map = new_thread_id_to_used_usec_map;
516
517 return final_output.str();
518}
519
Greg Clayton61d043b2011-03-22 04:00:09 +0000520StateType
521GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse
522(
523 ProcessGDBRemote *process,
524 const char *payload,
525 size_t packet_length,
526 StringExtractorGDBRemote &response
527)
528{
Greg Clayton0d2c5412012-07-02 22:05:25 +0000529 m_curr_tid = LLDB_INVALID_THREAD_ID;
Greg Clayton61d043b2011-03-22 04:00:09 +0000530 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
531 if (log)
532 log->Printf ("GDBRemoteCommunicationClient::%s ()", __FUNCTION__);
533
534 Mutex::Locker locker(m_sequence_mutex);
535 StateType state = eStateRunning;
536
537 BroadcastEvent(eBroadcastBitRunPacketSent, NULL);
538 m_public_is_running.SetValue (true, eBroadcastNever);
539 // Set the starting continue packet into "continue_packet". This packet
Jim Ingham8247e622012-06-06 00:32:39 +0000540 // may change if we are interrupted and we continue after an async packet...
Greg Clayton61d043b2011-03-22 04:00:09 +0000541 std::string continue_packet(payload, packet_length);
542
Greg Clayton628cead2011-05-19 03:54:16 +0000543 bool got_stdout = false;
544
Greg Clayton61d043b2011-03-22 04:00:09 +0000545 while (state == eStateRunning)
546 {
Greg Clayton628cead2011-05-19 03:54:16 +0000547 if (!got_stdout)
548 {
549 if (log)
550 log->Printf ("GDBRemoteCommunicationClient::%s () sending continue packet: %s", __FUNCTION__, continue_packet.c_str());
Greg Clayton516f0842012-04-11 00:24:49 +0000551 if (SendPacketNoLock(continue_packet.c_str(), continue_packet.size()) == 0)
Greg Clayton628cead2011-05-19 03:54:16 +0000552 state = eStateInvalid;
Greg Clayton61d043b2011-03-22 04:00:09 +0000553
Greg Claytonb7669b32011-10-27 22:04:16 +0000554 m_private_is_running.SetValue (true, eBroadcastAlways);
Greg Clayton628cead2011-05-19 03:54:16 +0000555 }
556
557 got_stdout = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000558
559 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000560 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(%s)", __FUNCTION__, continue_packet.c_str());
Greg Clayton61d043b2011-03-22 04:00:09 +0000561
Greg Clayton516f0842012-04-11 00:24:49 +0000562 if (WaitForPacketWithTimeoutMicroSecondsNoLock(response, UINT32_MAX))
Greg Clayton61d043b2011-03-22 04:00:09 +0000563 {
564 if (response.Empty())
565 state = eStateInvalid;
566 else
567 {
568 const char stop_type = response.GetChar();
569 if (log)
570 log->Printf ("GDBRemoteCommunicationClient::%s () got packet: %s", __FUNCTION__, response.GetStringRef().c_str());
571 switch (stop_type)
572 {
573 case 'T':
574 case 'S':
Greg Clayton61d043b2011-03-22 04:00:09 +0000575 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000576 if (process->GetStopID() == 0)
Greg Clayton61d043b2011-03-22 04:00:09 +0000577 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000578 if (process->GetID() == LLDB_INVALID_PROCESS_ID)
579 {
580 lldb::pid_t pid = GetCurrentProcessID ();
581 if (pid != LLDB_INVALID_PROCESS_ID)
582 process->SetID (pid);
583 }
584 process->BuildDynamicRegisterInfo (true);
Greg Clayton61d043b2011-03-22 04:00:09 +0000585 }
Greg Clayton05e4d972012-03-29 01:55:41 +0000586
587 // Privately notify any internal threads that we have stopped
588 // in case we wanted to interrupt our process, yet we might
589 // send a packet and continue without returning control to the
590 // user.
591 m_private_is_running.SetValue (false, eBroadcastAlways);
592
593 const uint8_t signo = response.GetHexU8 (UINT8_MAX);
594
Jim Ingham8247e622012-06-06 00:32:39 +0000595 bool continue_after_async = m_async_signal != -1 || m_async_packet_predicate.GetValue();
596 if (continue_after_async || m_interrupt_sent)
Greg Clayton05e4d972012-03-29 01:55:41 +0000597 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000598 // We sent an interrupt packet to stop the inferior process
599 // for an async signal or to send an async packet while running
600 // but we might have been single stepping and received the
601 // stop packet for the step instead of for the interrupt packet.
602 // Typically when an interrupt is sent a SIGINT or SIGSTOP
603 // is used, so if we get anything else, we need to try and
604 // get another stop reply packet that may have been sent
605 // due to sending the interrupt when the target is stopped
606 // which will just re-send a copy of the last stop reply
607 // packet. If we don't do this, then the reply for our
608 // async packet will be the repeat stop reply packet and cause
609 // a lot of trouble for us!
610 if (signo != SIGINT && signo != SIGSTOP)
611 {
Greg Claytonc2c822c2012-05-15 02:50:49 +0000612 continue_after_async = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000613
614 // We didn't get a a SIGINT or SIGSTOP, so try for a
615 // very brief time (1 ms) to get another stop reply
616 // packet to make sure it doesn't get in the way
617 StringExtractorGDBRemote extra_stop_reply_packet;
618 uint32_t timeout_usec = 1000;
619 if (WaitForPacketWithTimeoutMicroSecondsNoLock (extra_stop_reply_packet, timeout_usec))
620 {
621 switch (extra_stop_reply_packet.GetChar())
622 {
623 case 'T':
624 case 'S':
625 // We did get an extra stop reply, which means
626 // our interrupt didn't stop the target so we
627 // shouldn't continue after the async signal
628 // or packet is sent...
Greg Claytonc2c822c2012-05-15 02:50:49 +0000629 continue_after_async = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000630 break;
631 }
632 }
633 }
634 }
635
636 if (m_async_signal != -1)
637 {
638 if (log)
639 log->Printf ("async: send signo = %s", Host::GetSignalAsCString (m_async_signal));
640
641 // Save off the async signal we are supposed to send
642 const int async_signal = m_async_signal;
643 // Clear the async signal member so we don't end up
644 // sending the signal multiple times...
645 m_async_signal = -1;
646 // Check which signal we stopped with
647 if (signo == async_signal)
648 {
649 if (log)
650 log->Printf ("async: stopped with signal %s, we are done running", Host::GetSignalAsCString (signo));
651
652 // We already stopped with a signal that we wanted
653 // to stop with, so we are done
654 }
655 else
656 {
657 // We stopped with a different signal that the one
658 // we wanted to stop with, so now we must resume
659 // with the signal we want
660 char signal_packet[32];
661 int signal_packet_len = 0;
662 signal_packet_len = ::snprintf (signal_packet,
663 sizeof (signal_packet),
664 "C%2.2x",
665 async_signal);
666
667 if (log)
668 log->Printf ("async: stopped with signal %s, resume with %s",
669 Host::GetSignalAsCString (signo),
670 Host::GetSignalAsCString (async_signal));
671
672 // Set the continue packet to resume even if the
Greg Claytonc2c822c2012-05-15 02:50:49 +0000673 // interrupt didn't cause our stop (ignore continue_after_async)
Greg Clayton05e4d972012-03-29 01:55:41 +0000674 continue_packet.assign(signal_packet, signal_packet_len);
675 continue;
676 }
677 }
678 else if (m_async_packet_predicate.GetValue())
679 {
680 LogSP packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
681
682 // We are supposed to send an asynchronous packet while
683 // we are running.
684 m_async_response.Clear();
685 if (m_async_packet.empty())
686 {
687 if (packet_log)
688 packet_log->Printf ("async: error: empty async packet");
689
690 }
691 else
692 {
693 if (packet_log)
694 packet_log->Printf ("async: sending packet");
695
696 SendPacketAndWaitForResponse (&m_async_packet[0],
697 m_async_packet.size(),
698 m_async_response,
699 false);
700 }
701 // Let the other thread that was trying to send the async
702 // packet know that the packet has been sent and response is
703 // ready...
704 m_async_packet_predicate.SetValue(false, eBroadcastAlways);
705
706 if (packet_log)
Greg Claytonc2c822c2012-05-15 02:50:49 +0000707 packet_log->Printf ("async: sent packet, continue_after_async = %i", continue_after_async);
Greg Clayton05e4d972012-03-29 01:55:41 +0000708
709 // Set the continue packet to resume if our interrupt
710 // for the async packet did cause the stop
Greg Claytonc2c822c2012-05-15 02:50:49 +0000711 if (continue_after_async)
Greg Clayton05e4d972012-03-29 01:55:41 +0000712 {
Greg Clayton8ca4fa72012-05-24 23:42:14 +0000713 // Reverting this for now as it is causing deadlocks
714 // in programs (<rdar://problem/11529853>). In the future
715 // we should check our thread list and "do the right thing"
716 // for new threads that show up while we stop and run async
717 // packets. Setting the packet to 'c' to continue all threads
718 // is the right thing to do 99.99% of the time because if a
719 // thread was single stepping, and we sent an interrupt, we
720 // will notice above that we didn't stop due to an interrupt
721 // but stopped due to stepping and we would _not_ continue.
722 continue_packet.assign (1, 'c');
Greg Clayton05e4d972012-03-29 01:55:41 +0000723 continue;
724 }
725 }
726 // Stop with signal and thread info
727 state = eStateStopped;
Greg Clayton61d043b2011-03-22 04:00:09 +0000728 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000729 break;
730
731 case 'W':
732 case 'X':
733 // process exited
734 state = eStateExited;
735 break;
736
737 case 'O':
738 // STDOUT
739 {
Greg Clayton628cead2011-05-19 03:54:16 +0000740 got_stdout = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000741 std::string inferior_stdout;
742 inferior_stdout.reserve(response.GetBytesLeft () / 2);
743 char ch;
744 while ((ch = response.GetHexU8()) != '\0')
745 inferior_stdout.append(1, ch);
746 process->AppendSTDOUT (inferior_stdout.c_str(), inferior_stdout.size());
747 }
748 break;
749
Han Ming Ongfb9cee62012-11-17 00:21:04 +0000750 case 'A':
751 // Async miscellaneous reply. Right now, only profile data is coming through this channel.
752 {
Han Ming Ong0df19cc2013-01-18 23:11:53 +0000753 std::string input = response.GetStringRef().substr(1); // '1' to move beyond 'A'
754 if (m_partial_profile_data.length() > 0)
755 {
756 m_partial_profile_data.append(input);
757 input = m_partial_profile_data;
758 m_partial_profile_data.clear();
759 }
760
761 size_t found, pos = 0, len = input.length();
762 while ((found = input.find(end_delimiter, pos)) != std::string::npos)
763 {
764 StringExtractorGDBRemote profileDataExtractor(input.substr(pos, found).c_str());
765 const std::string& profile_data = HarmonizeThreadIdsForProfileData(process, profileDataExtractor);
766 process->BroadcastAsyncProfileData (profile_data.c_str(), profile_data.length());
767
768 pos = found + end_delimiter_len;
769 }
770
771 if (pos < len)
772 {
773 // Last incomplete chunk.
774 m_partial_profile_data = input.substr(pos);
775 }
Han Ming Ongfb9cee62012-11-17 00:21:04 +0000776 }
777 break;
778
Greg Clayton61d043b2011-03-22 04:00:09 +0000779 case 'E':
780 // ERROR
781 state = eStateInvalid;
782 break;
783
784 default:
785 if (log)
786 log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__);
787 state = eStateInvalid;
788 break;
789 }
790 }
791 }
792 else
793 {
794 if (log)
795 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__);
796 state = eStateInvalid;
797 }
798 }
799 if (log)
800 log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state));
801 response.SetFilePos(0);
802 m_private_is_running.SetValue (false, eBroadcastAlways);
803 m_public_is_running.SetValue (false, eBroadcastAlways);
804 return state;
805}
806
807bool
808GDBRemoteCommunicationClient::SendAsyncSignal (int signo)
809{
Greg Clayton05e4d972012-03-29 01:55:41 +0000810 Mutex::Locker async_locker (m_async_mutex);
Greg Clayton61d043b2011-03-22 04:00:09 +0000811 m_async_signal = signo;
812 bool timed_out = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000813 Mutex::Locker locker;
Greg Clayton05e4d972012-03-29 01:55:41 +0000814 if (SendInterrupt (locker, 1, timed_out))
Greg Clayton61d043b2011-03-22 04:00:09 +0000815 return true;
816 m_async_signal = -1;
817 return false;
818}
819
Greg Clayton516f0842012-04-11 00:24:49 +0000820// This function takes a mutex locker as a parameter in case the GetSequenceMutex
Greg Clayton61d043b2011-03-22 04:00:09 +0000821// actually succeeds. If it doesn't succeed in acquiring the sequence mutex
822// (the expected result), then it will send the halt packet. If it does succeed
823// then the caller that requested the interrupt will want to keep the sequence
824// locked down so that no one else can send packets while the caller has control.
825// This function usually gets called when we are running and need to stop the
826// target. It can also be used when we are running and and we need to do something
827// else (like read/write memory), so we need to interrupt the running process
828// (gdb remote protocol requires this), and do what we need to do, then resume.
829
830bool
Greg Clayton05e4d972012-03-29 01:55:41 +0000831GDBRemoteCommunicationClient::SendInterrupt
Greg Clayton61d043b2011-03-22 04:00:09 +0000832(
833 Mutex::Locker& locker,
834 uint32_t seconds_to_wait_for_stop,
Greg Clayton61d043b2011-03-22 04:00:09 +0000835 bool &timed_out
836)
837{
Greg Clayton61d043b2011-03-22 04:00:09 +0000838 timed_out = false;
Greg Clayton05e4d972012-03-29 01:55:41 +0000839 LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Clayton61d043b2011-03-22 04:00:09 +0000840
841 if (IsRunning())
842 {
843 // Only send an interrupt if our debugserver is running...
Greg Claytonc8dd5702012-04-12 19:04:34 +0000844 if (GetSequenceMutex (locker))
Greg Clayton516f0842012-04-11 00:24:49 +0000845 {
846 if (log)
847 log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt");
848 }
849 else
Greg Clayton61d043b2011-03-22 04:00:09 +0000850 {
851 // Someone has the mutex locked waiting for a response or for the
852 // inferior to stop, so send the interrupt on the down low...
853 char ctrl_c = '\x03';
854 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton61d043b2011-03-22 04:00:09 +0000855 size_t bytes_written = Write (&ctrl_c, 1, status, NULL);
Greg Clayton05e4d972012-03-29 01:55:41 +0000856 if (log)
857 log->PutCString("send packet: \\x03");
Greg Clayton61d043b2011-03-22 04:00:09 +0000858 if (bytes_written > 0)
859 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000860 m_interrupt_sent = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000861 if (seconds_to_wait_for_stop)
862 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000863 TimeValue timeout;
864 if (seconds_to_wait_for_stop)
865 {
866 timeout = TimeValue::Now();
867 timeout.OffsetWithSeconds (seconds_to_wait_for_stop);
868 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000869 if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out))
870 {
871 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000872 log->PutCString ("SendInterrupt () - sent interrupt, private state stopped");
Greg Clayton61d043b2011-03-22 04:00:09 +0000873 return true;
874 }
875 else
876 {
877 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000878 log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume");
Greg Clayton61d043b2011-03-22 04:00:09 +0000879 }
880 }
881 else
882 {
883 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000884 log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop...");
Greg Clayton61d043b2011-03-22 04:00:09 +0000885 return true;
886 }
887 }
888 else
889 {
890 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000891 log->Printf ("SendInterrupt () - failed to write interrupt");
Greg Clayton61d043b2011-03-22 04:00:09 +0000892 }
893 return false;
894 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000895 }
Greg Clayton05e4d972012-03-29 01:55:41 +0000896 else
897 {
898 if (log)
899 log->Printf ("SendInterrupt () - not running");
900 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000901 return true;
902}
903
904lldb::pid_t
905GDBRemoteCommunicationClient::GetCurrentProcessID ()
906{
907 StringExtractorGDBRemote response;
908 if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false))
909 {
910 if (response.GetChar() == 'Q')
911 if (response.GetChar() == 'C')
912 return response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID);
913 }
914 return LLDB_INVALID_PROCESS_ID;
915}
916
917bool
918GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str)
919{
920 error_str.clear();
921 StringExtractorGDBRemote response;
922 if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false))
923 {
924 if (response.IsOKResponse())
925 return true;
926 if (response.GetChar() == 'E')
927 {
928 // A string the describes what failed when launching...
929 error_str = response.GetStringRef().substr(1);
930 }
931 else
932 {
933 error_str.assign ("unknown error occurred launching process");
934 }
935 }
936 else
937 {
Jim Ingham7b3a49b2012-06-28 20:30:23 +0000938 error_str.assign ("timed out waiting for app to launch");
Greg Clayton61d043b2011-03-22 04:00:09 +0000939 }
940 return false;
941}
942
943int
944GDBRemoteCommunicationClient::SendArgumentsPacket (char const *argv[])
945{
946 if (argv && argv[0])
947 {
948 StreamString packet;
949 packet.PutChar('A');
950 const char *arg;
951 for (uint32_t i = 0; (arg = argv[i]) != NULL; ++i)
952 {
953 const int arg_len = strlen(arg);
954 if (i > 0)
955 packet.PutChar(',');
956 packet.Printf("%i,%i,", arg_len * 2, i);
957 packet.PutBytesAsRawHex8 (arg, arg_len);
958 }
959
960 StringExtractorGDBRemote response;
961 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
962 {
963 if (response.IsOKResponse())
964 return 0;
965 uint8_t error = response.GetError();
966 if (error)
967 return error;
968 }
969 }
970 return -1;
971}
972
973int
974GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value)
975{
976 if (name_equal_value && name_equal_value[0])
977 {
978 StreamString packet;
979 packet.Printf("QEnvironment:%s", name_equal_value);
980 StringExtractorGDBRemote response;
981 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
982 {
983 if (response.IsOKResponse())
984 return 0;
985 uint8_t error = response.GetError();
986 if (error)
987 return error;
988 }
989 }
990 return -1;
991}
992
Greg Claytona4582402011-05-08 04:53:50 +0000993int
994GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch)
995{
996 if (arch && arch[0])
997 {
998 StreamString packet;
999 packet.Printf("QLaunchArch:%s", arch);
1000 StringExtractorGDBRemote response;
1001 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1002 {
1003 if (response.IsOKResponse())
1004 return 0;
1005 uint8_t error = response.GetError();
1006 if (error)
1007 return error;
1008 }
1009 }
1010 return -1;
1011}
1012
Greg Clayton61d043b2011-03-22 04:00:09 +00001013bool
Greg Clayton58e26e02011-03-24 04:28:38 +00001014GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major,
1015 uint32_t &minor,
1016 uint32_t &update)
1017{
1018 if (GetHostInfo ())
1019 {
1020 if (m_os_version_major != UINT32_MAX)
1021 {
1022 major = m_os_version_major;
1023 minor = m_os_version_minor;
1024 update = m_os_version_update;
1025 return true;
1026 }
1027 }
1028 return false;
1029}
1030
1031bool
1032GDBRemoteCommunicationClient::GetOSBuildString (std::string &s)
1033{
1034 if (GetHostInfo ())
1035 {
1036 if (!m_os_build.empty())
1037 {
1038 s = m_os_build;
1039 return true;
1040 }
1041 }
1042 s.clear();
1043 return false;
1044}
1045
1046
1047bool
1048GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s)
1049{
1050 if (GetHostInfo ())
1051 {
1052 if (!m_os_kernel.empty())
1053 {
1054 s = m_os_kernel;
1055 return true;
1056 }
1057 }
1058 s.clear();
1059 return false;
1060}
1061
1062bool
1063GDBRemoteCommunicationClient::GetHostname (std::string &s)
1064{
1065 if (GetHostInfo ())
1066 {
1067 if (!m_hostname.empty())
1068 {
1069 s = m_hostname;
1070 return true;
1071 }
1072 }
1073 s.clear();
1074 return false;
1075}
1076
1077ArchSpec
1078GDBRemoteCommunicationClient::GetSystemArchitecture ()
1079{
1080 if (GetHostInfo ())
1081 return m_host_arch;
1082 return ArchSpec();
1083}
1084
Jason Molendafe555672012-12-19 02:54:03 +00001085const lldb_private::ArchSpec &
1086GDBRemoteCommunicationClient::GetProcessArchitecture ()
1087{
1088 if (m_qProcessInfo_is_valid == eLazyBoolCalculate)
1089 GetCurrentProcessInfo ();
1090 return m_process_arch;
1091}
1092
Greg Clayton58e26e02011-03-24 04:28:38 +00001093
1094bool
Greg Clayton06d7cc82011-04-04 18:18:57 +00001095GDBRemoteCommunicationClient::GetHostInfo (bool force)
Greg Clayton61d043b2011-03-22 04:00:09 +00001096{
Greg Clayton06d7cc82011-04-04 18:18:57 +00001097 if (force || m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton61d043b2011-03-22 04:00:09 +00001098 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001099 m_qHostInfo_is_valid = eLazyBoolNo;
Greg Clayton61d043b2011-03-22 04:00:09 +00001100 StringExtractorGDBRemote response;
1101 if (SendPacketAndWaitForResponse ("qHostInfo", response, false))
1102 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001103 if (response.IsNormalResponse())
Greg Claytoncb8977d2011-03-23 00:09:55 +00001104 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001105 std::string name;
1106 std::string value;
1107 uint32_t cpu = LLDB_INVALID_CPUTYPE;
1108 uint32_t sub = 0;
1109 std::string arch_name;
1110 std::string os_name;
1111 std::string vendor_name;
1112 std::string triple;
1113 uint32_t pointer_byte_size = 0;
1114 StringExtractor extractor;
1115 ByteOrder byte_order = eByteOrderInvalid;
1116 uint32_t num_keys_decoded = 0;
1117 while (response.GetNameColonValue(name, value))
Greg Claytoncb8977d2011-03-23 00:09:55 +00001118 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001119 if (name.compare("cputype") == 0)
Greg Clayton58e26e02011-03-24 04:28:38 +00001120 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001121 // exception type in big endian hex
1122 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0);
1123 if (cpu != LLDB_INVALID_CPUTYPE)
1124 ++num_keys_decoded;
1125 }
1126 else if (name.compare("cpusubtype") == 0)
1127 {
1128 // exception count in big endian hex
1129 sub = Args::StringToUInt32 (value.c_str(), 0, 0);
1130 if (sub != 0)
1131 ++num_keys_decoded;
1132 }
1133 else if (name.compare("arch") == 0)
1134 {
1135 arch_name.swap (value);
1136 ++num_keys_decoded;
1137 }
1138 else if (name.compare("triple") == 0)
1139 {
1140 // The triple comes as ASCII hex bytes since it contains '-' chars
1141 extractor.GetStringRef().swap(value);
1142 extractor.SetFilePos(0);
1143 extractor.GetHexByteString (triple);
1144 ++num_keys_decoded;
1145 }
1146 else if (name.compare("os_build") == 0)
1147 {
1148 extractor.GetStringRef().swap(value);
1149 extractor.SetFilePos(0);
1150 extractor.GetHexByteString (m_os_build);
1151 ++num_keys_decoded;
1152 }
1153 else if (name.compare("hostname") == 0)
1154 {
1155 extractor.GetStringRef().swap(value);
1156 extractor.SetFilePos(0);
1157 extractor.GetHexByteString (m_hostname);
1158 ++num_keys_decoded;
1159 }
1160 else if (name.compare("os_kernel") == 0)
1161 {
1162 extractor.GetStringRef().swap(value);
1163 extractor.SetFilePos(0);
1164 extractor.GetHexByteString (m_os_kernel);
1165 ++num_keys_decoded;
1166 }
1167 else if (name.compare("ostype") == 0)
1168 {
1169 os_name.swap (value);
1170 ++num_keys_decoded;
1171 }
1172 else if (name.compare("vendor") == 0)
1173 {
1174 vendor_name.swap(value);
1175 ++num_keys_decoded;
1176 }
1177 else if (name.compare("endian") == 0)
1178 {
1179 ++num_keys_decoded;
1180 if (value.compare("little") == 0)
1181 byte_order = eByteOrderLittle;
1182 else if (value.compare("big") == 0)
1183 byte_order = eByteOrderBig;
1184 else if (value.compare("pdp") == 0)
1185 byte_order = eByteOrderPDP;
1186 else
1187 --num_keys_decoded;
1188 }
1189 else if (name.compare("ptrsize") == 0)
1190 {
1191 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 0);
1192 if (pointer_byte_size != 0)
1193 ++num_keys_decoded;
1194 }
1195 else if (name.compare("os_version") == 0)
1196 {
1197 Args::StringToVersion (value.c_str(),
1198 m_os_version_major,
1199 m_os_version_minor,
1200 m_os_version_update);
1201 if (m_os_version_major != UINT32_MAX)
1202 ++num_keys_decoded;
1203 }
Enrico Granata7de2a3b2012-07-13 23:18:48 +00001204 else if (name.compare("watchpoint_exceptions_received") == 0)
1205 {
1206 ++num_keys_decoded;
1207 if (strcmp(value.c_str(),"before") == 0)
1208 m_watchpoints_trigger_after_instruction = eLazyBoolNo;
1209 else if (strcmp(value.c_str(),"after") == 0)
1210 m_watchpoints_trigger_after_instruction = eLazyBoolYes;
1211 else
1212 --num_keys_decoded;
1213 }
1214
Greg Clayton24bc5d92011-03-30 18:16:51 +00001215 }
1216
1217 if (num_keys_decoded > 0)
1218 m_qHostInfo_is_valid = eLazyBoolYes;
1219
1220 if (triple.empty())
1221 {
1222 if (arch_name.empty())
1223 {
1224 if (cpu != LLDB_INVALID_CPUTYPE)
1225 {
1226 m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1227 if (pointer_byte_size)
1228 {
1229 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1230 }
1231 if (byte_order != eByteOrderInvalid)
1232 {
1233 assert (byte_order == m_host_arch.GetByteOrder());
1234 }
Greg Claytonb170aee2012-05-08 01:45:38 +00001235
1236 if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0)
1237 {
1238 switch (m_host_arch.GetMachine())
1239 {
1240 case llvm::Triple::arm:
1241 case llvm::Triple::thumb:
1242 os_name = "ios";
1243 break;
1244 default:
1245 os_name = "macosx";
1246 break;
1247 }
1248 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001249 if (!vendor_name.empty())
1250 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1251 if (!os_name.empty())
Greg Clayton89798cc2011-09-15 00:21:03 +00001252 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001253
1254 }
1255 }
1256 else
1257 {
1258 std::string triple;
1259 triple += arch_name;
Greg Claytonb170aee2012-05-08 01:45:38 +00001260 if (!vendor_name.empty() || !os_name.empty())
1261 {
1262 triple += '-';
1263 if (vendor_name.empty())
1264 triple += "unknown";
1265 else
1266 triple += vendor_name;
1267 triple += '-';
1268 if (os_name.empty())
1269 triple += "unknown";
1270 else
1271 triple += os_name;
1272 }
1273 m_host_arch.SetTriple (triple.c_str());
1274
1275 llvm::Triple &host_triple = m_host_arch.GetTriple();
1276 if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin)
1277 {
1278 switch (m_host_arch.GetMachine())
1279 {
1280 case llvm::Triple::arm:
1281 case llvm::Triple::thumb:
1282 host_triple.setOS(llvm::Triple::IOS);
1283 break;
1284 default:
1285 host_triple.setOS(llvm::Triple::MacOSX);
1286 break;
1287 }
1288 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001289 if (pointer_byte_size)
1290 {
1291 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1292 }
1293 if (byte_order != eByteOrderInvalid)
1294 {
1295 assert (byte_order == m_host_arch.GetByteOrder());
1296 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001297
Greg Clayton58e26e02011-03-24 04:28:38 +00001298 }
1299 }
1300 else
1301 {
Greg Claytonb170aee2012-05-08 01:45:38 +00001302 m_host_arch.SetTriple (triple.c_str());
Greg Claytoncb8977d2011-03-23 00:09:55 +00001303 if (pointer_byte_size)
1304 {
1305 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1306 }
1307 if (byte_order != eByteOrderInvalid)
1308 {
1309 assert (byte_order == m_host_arch.GetByteOrder());
1310 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001311 }
Greg Claytoncb8977d2011-03-23 00:09:55 +00001312 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001313 }
1314 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001315 return m_qHostInfo_is_valid == eLazyBoolYes;
Greg Clayton61d043b2011-03-22 04:00:09 +00001316}
1317
1318int
1319GDBRemoteCommunicationClient::SendAttach
1320(
1321 lldb::pid_t pid,
1322 StringExtractorGDBRemote& response
1323)
1324{
1325 if (pid != LLDB_INVALID_PROCESS_ID)
1326 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001327 char packet[64];
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001328 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, pid);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001329 assert (packet_len < sizeof(packet));
1330 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton61d043b2011-03-22 04:00:09 +00001331 {
1332 if (response.IsErrorResponse())
1333 return response.GetError();
1334 return 0;
1335 }
1336 }
1337 return -1;
1338}
1339
1340const lldb_private::ArchSpec &
1341GDBRemoteCommunicationClient::GetHostArchitecture ()
1342{
Greg Clayton24bc5d92011-03-30 18:16:51 +00001343 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton61d043b2011-03-22 04:00:09 +00001344 GetHostInfo ();
Greg Claytoncb8977d2011-03-23 00:09:55 +00001345 return m_host_arch;
Greg Clayton61d043b2011-03-22 04:00:09 +00001346}
1347
1348addr_t
1349GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions)
1350{
Greg Clayton2f085c62011-05-15 01:25:55 +00001351 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton61d043b2011-03-22 04:00:09 +00001352 {
Greg Clayton2f085c62011-05-15 01:25:55 +00001353 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton989816b2011-05-14 01:50:35 +00001354 char packet[64];
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001355 const int packet_len = ::snprintf (packet, sizeof(packet), "_M%" PRIx64 ",%s%s%s",
Greg Clayton851e30e2012-09-18 18:04:04 +00001356 (uint64_t)size,
Greg Clayton989816b2011-05-14 01:50:35 +00001357 permissions & lldb::ePermissionsReadable ? "r" : "",
1358 permissions & lldb::ePermissionsWritable ? "w" : "",
1359 permissions & lldb::ePermissionsExecutable ? "x" : "");
1360 assert (packet_len < sizeof(packet));
1361 StringExtractorGDBRemote response;
1362 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1363 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001364 if (!response.IsErrorResponse())
Greg Clayton989816b2011-05-14 01:50:35 +00001365 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1366 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001367 else
1368 {
1369 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1370 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001371 }
1372 return LLDB_INVALID_ADDRESS;
1373}
1374
1375bool
1376GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr)
1377{
Greg Clayton2f085c62011-05-15 01:25:55 +00001378 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton61d043b2011-03-22 04:00:09 +00001379 {
Greg Clayton2f085c62011-05-15 01:25:55 +00001380 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton989816b2011-05-14 01:50:35 +00001381 char packet[64];
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001382 const int packet_len = ::snprintf(packet, sizeof(packet), "_m%" PRIx64, (uint64_t)addr);
Greg Clayton989816b2011-05-14 01:50:35 +00001383 assert (packet_len < sizeof(packet));
1384 StringExtractorGDBRemote response;
1385 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1386 {
1387 if (response.IsOKResponse())
1388 return true;
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001389 }
1390 else
1391 {
1392 m_supports_alloc_dealloc_memory = eLazyBoolNo;
Greg Clayton989816b2011-05-14 01:50:35 +00001393 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001394 }
1395 return false;
1396}
1397
Greg Clayton516f0842012-04-11 00:24:49 +00001398bool
1399GDBRemoteCommunicationClient::Detach ()
1400{
1401 return SendPacket ("D", 1) > 0;
1402}
1403
Greg Claytona9385532011-11-18 07:03:08 +00001404Error
1405GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr,
1406 lldb_private::MemoryRegionInfo &region_info)
1407{
1408 Error error;
1409 region_info.Clear();
1410
1411 if (m_supports_memory_region_info != eLazyBoolNo)
1412 {
1413 m_supports_memory_region_info = eLazyBoolYes;
1414 char packet[64];
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001415 const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%" PRIx64, (uint64_t)addr);
Greg Claytona9385532011-11-18 07:03:08 +00001416 assert (packet_len < sizeof(packet));
1417 StringExtractorGDBRemote response;
1418 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1419 {
1420 std::string name;
1421 std::string value;
1422 addr_t addr_value;
1423 bool success = true;
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001424 bool saw_permissions = false;
Greg Claytona9385532011-11-18 07:03:08 +00001425 while (success && response.GetNameColonValue(name, value))
1426 {
1427 if (name.compare ("start") == 0)
1428 {
1429 addr_value = Args::StringToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success);
1430 if (success)
1431 region_info.GetRange().SetRangeBase(addr_value);
1432 }
1433 else if (name.compare ("size") == 0)
1434 {
1435 addr_value = Args::StringToUInt64(value.c_str(), 0, 16, &success);
1436 if (success)
1437 region_info.GetRange().SetByteSize (addr_value);
1438 }
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001439 else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid())
Greg Claytona9385532011-11-18 07:03:08 +00001440 {
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001441 saw_permissions = true;
1442 if (region_info.GetRange().Contains (addr))
1443 {
1444 if (value.find('r') != std::string::npos)
1445 region_info.SetReadable (MemoryRegionInfo::eYes);
1446 else
1447 region_info.SetReadable (MemoryRegionInfo::eNo);
1448
1449 if (value.find('w') != std::string::npos)
1450 region_info.SetWritable (MemoryRegionInfo::eYes);
1451 else
1452 region_info.SetWritable (MemoryRegionInfo::eNo);
1453
1454 if (value.find('x') != std::string::npos)
1455 region_info.SetExecutable (MemoryRegionInfo::eYes);
1456 else
1457 region_info.SetExecutable (MemoryRegionInfo::eNo);
1458 }
1459 else
1460 {
1461 // The reported region does not contain this address -- we're looking at an unmapped page
1462 region_info.SetReadable (MemoryRegionInfo::eNo);
1463 region_info.SetWritable (MemoryRegionInfo::eNo);
1464 region_info.SetExecutable (MemoryRegionInfo::eNo);
1465 }
Greg Claytona9385532011-11-18 07:03:08 +00001466 }
1467 else if (name.compare ("error") == 0)
1468 {
1469 StringExtractorGDBRemote name_extractor;
1470 // Swap "value" over into "name_extractor"
1471 name_extractor.GetStringRef().swap(value);
1472 // Now convert the HEX bytes into a string value
1473 name_extractor.GetHexByteString (value);
1474 error.SetErrorString(value.c_str());
1475 }
1476 }
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001477
1478 // We got a valid address range back but no permissions -- which means this is an unmapped page
1479 if (region_info.GetRange().IsValid() && saw_permissions == false)
1480 {
1481 region_info.SetReadable (MemoryRegionInfo::eNo);
1482 region_info.SetWritable (MemoryRegionInfo::eNo);
1483 region_info.SetExecutable (MemoryRegionInfo::eNo);
1484 }
Greg Claytona9385532011-11-18 07:03:08 +00001485 }
1486 else
1487 {
1488 m_supports_memory_region_info = eLazyBoolNo;
1489 }
1490 }
1491
1492 if (m_supports_memory_region_info == eLazyBoolNo)
1493 {
1494 error.SetErrorString("qMemoryRegionInfo is not supported");
1495 }
1496 if (error.Fail())
1497 region_info.Clear();
1498 return error;
1499
1500}
1501
Johnny Chen7cbdcfb2012-05-23 21:09:52 +00001502Error
1503GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num)
1504{
1505 Error error;
1506
1507 if (m_supports_watchpoint_support_info == eLazyBoolYes)
1508 {
1509 num = m_num_supported_hardware_watchpoints;
1510 return error;
1511 }
1512
1513 // Set num to 0 first.
1514 num = 0;
1515 if (m_supports_watchpoint_support_info != eLazyBoolNo)
1516 {
1517 char packet[64];
1518 const int packet_len = ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:");
1519 assert (packet_len < sizeof(packet));
1520 StringExtractorGDBRemote response;
1521 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1522 {
1523 m_supports_watchpoint_support_info = eLazyBoolYes;
1524 std::string name;
1525 std::string value;
1526 while (response.GetNameColonValue(name, value))
1527 {
1528 if (name.compare ("num") == 0)
1529 {
1530 num = Args::StringToUInt32(value.c_str(), 0, 0);
1531 m_num_supported_hardware_watchpoints = num;
1532 }
1533 }
1534 }
1535 else
1536 {
1537 m_supports_watchpoint_support_info = eLazyBoolNo;
1538 }
1539 }
1540
1541 if (m_supports_watchpoint_support_info == eLazyBoolNo)
1542 {
1543 error.SetErrorString("qWatchpointSupportInfo is not supported");
1544 }
1545 return error;
1546
1547}
Greg Claytona9385532011-11-18 07:03:08 +00001548
Enrico Granata7de2a3b2012-07-13 23:18:48 +00001549lldb_private::Error
1550GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num, bool& after)
1551{
1552 Error error(GetWatchpointSupportInfo(num));
1553 if (error.Success())
1554 error = GetWatchpointsTriggerAfterInstruction(after);
1555 return error;
1556}
1557
1558lldb_private::Error
1559GDBRemoteCommunicationClient::GetWatchpointsTriggerAfterInstruction (bool &after)
1560{
1561 Error error;
1562
1563 // we assume watchpoints will happen after running the relevant opcode
1564 // and we only want to override this behavior if we have explicitly
1565 // received a qHostInfo telling us otherwise
1566 if (m_qHostInfo_is_valid != eLazyBoolYes)
1567 after = true;
1568 else
1569 after = (m_watchpoints_trigger_after_instruction != eLazyBoolNo);
1570 return error;
1571}
1572
Greg Clayton61d043b2011-03-22 04:00:09 +00001573int
1574GDBRemoteCommunicationClient::SetSTDIN (char const *path)
1575{
1576 if (path && path[0])
1577 {
1578 StreamString packet;
1579 packet.PutCString("QSetSTDIN:");
1580 packet.PutBytesAsRawHex8(path, strlen(path));
1581
1582 StringExtractorGDBRemote response;
1583 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1584 {
1585 if (response.IsOKResponse())
1586 return 0;
1587 uint8_t error = response.GetError();
1588 if (error)
1589 return error;
1590 }
1591 }
1592 return -1;
1593}
1594
1595int
1596GDBRemoteCommunicationClient::SetSTDOUT (char const *path)
1597{
1598 if (path && path[0])
1599 {
1600 StreamString packet;
1601 packet.PutCString("QSetSTDOUT:");
1602 packet.PutBytesAsRawHex8(path, strlen(path));
1603
1604 StringExtractorGDBRemote response;
1605 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1606 {
1607 if (response.IsOKResponse())
1608 return 0;
1609 uint8_t error = response.GetError();
1610 if (error)
1611 return error;
1612 }
1613 }
1614 return -1;
1615}
1616
1617int
1618GDBRemoteCommunicationClient::SetSTDERR (char const *path)
1619{
1620 if (path && path[0])
1621 {
1622 StreamString packet;
1623 packet.PutCString("QSetSTDERR:");
1624 packet.PutBytesAsRawHex8(path, strlen(path));
1625
1626 StringExtractorGDBRemote response;
1627 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1628 {
1629 if (response.IsOKResponse())
1630 return 0;
1631 uint8_t error = response.GetError();
1632 if (error)
1633 return error;
1634 }
1635 }
1636 return -1;
1637}
1638
1639int
1640GDBRemoteCommunicationClient::SetWorkingDir (char const *path)
1641{
1642 if (path && path[0])
1643 {
1644 StreamString packet;
1645 packet.PutCString("QSetWorkingDir:");
1646 packet.PutBytesAsRawHex8(path, strlen(path));
1647
1648 StringExtractorGDBRemote response;
1649 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1650 {
1651 if (response.IsOKResponse())
1652 return 0;
1653 uint8_t error = response.GetError();
1654 if (error)
1655 return error;
1656 }
1657 }
1658 return -1;
1659}
1660
1661int
1662GDBRemoteCommunicationClient::SetDisableASLR (bool enable)
1663{
Greg Clayton24bc5d92011-03-30 18:16:51 +00001664 char packet[32];
1665 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0);
1666 assert (packet_len < sizeof(packet));
Greg Clayton61d043b2011-03-22 04:00:09 +00001667 StringExtractorGDBRemote response;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001668 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton61d043b2011-03-22 04:00:09 +00001669 {
1670 if (response.IsOKResponse())
1671 return 0;
1672 uint8_t error = response.GetError();
1673 if (error)
1674 return error;
1675 }
1676 return -1;
1677}
Greg Clayton24bc5d92011-03-30 18:16:51 +00001678
1679bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001680GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001681{
1682 if (response.IsNormalResponse())
1683 {
1684 std::string name;
1685 std::string value;
1686 StringExtractor extractor;
1687
1688 while (response.GetNameColonValue(name, value))
1689 {
1690 if (name.compare("pid") == 0)
1691 {
1692 process_info.SetProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1693 }
1694 else if (name.compare("ppid") == 0)
1695 {
1696 process_info.SetParentProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1697 }
1698 else if (name.compare("uid") == 0)
1699 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001700 process_info.SetUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001701 }
1702 else if (name.compare("euid") == 0)
1703 {
1704 process_info.SetEffectiveUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1705 }
1706 else if (name.compare("gid") == 0)
1707 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001708 process_info.SetGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001709 }
1710 else if (name.compare("egid") == 0)
1711 {
1712 process_info.SetEffectiveGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1713 }
1714 else if (name.compare("triple") == 0)
1715 {
1716 // The triple comes as ASCII hex bytes since it contains '-' chars
1717 extractor.GetStringRef().swap(value);
1718 extractor.SetFilePos(0);
1719 extractor.GetHexByteString (value);
Greg Claytonb170aee2012-05-08 01:45:38 +00001720 process_info.GetArchitecture ().SetTriple (value.c_str());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001721 }
1722 else if (name.compare("name") == 0)
1723 {
1724 StringExtractor extractor;
Filipe Cabecinhase61ec7f2012-05-07 09:30:51 +00001725 // The process name from ASCII hex bytes since we can't
Greg Clayton24bc5d92011-03-30 18:16:51 +00001726 // control the characters in a process name
1727 extractor.GetStringRef().swap(value);
1728 extractor.SetFilePos(0);
1729 extractor.GetHexByteString (value);
Greg Clayton527154d2011-11-15 03:53:30 +00001730 process_info.GetExecutableFile().SetFile (value.c_str(), false);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001731 }
1732 }
1733
1734 if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
1735 return true;
1736 }
1737 return false;
1738}
1739
1740bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001741GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001742{
1743 process_info.Clear();
1744
1745 if (m_supports_qProcessInfoPID)
1746 {
1747 char packet[32];
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001748 const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%" PRIu64, pid);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001749 assert (packet_len < sizeof(packet));
1750 StringExtractorGDBRemote response;
1751 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1752 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001753 return DecodeProcessInfoResponse (response, process_info);
1754 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001755 else
1756 {
1757 m_supports_qProcessInfoPID = false;
1758 return false;
1759 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001760 }
1761 return false;
1762}
1763
Jason Molendafe555672012-12-19 02:54:03 +00001764bool
1765GDBRemoteCommunicationClient::GetCurrentProcessInfo ()
1766{
1767 if (m_qProcessInfo_is_valid == eLazyBoolYes)
1768 return true;
1769 if (m_qProcessInfo_is_valid == eLazyBoolNo)
1770 return false;
1771
1772 GetHostInfo ();
1773
1774 StringExtractorGDBRemote response;
1775 if (SendPacketAndWaitForResponse ("qProcessInfo", response, false))
1776 {
1777 if (response.IsNormalResponse())
1778 {
1779 std::string name;
1780 std::string value;
1781 uint32_t cpu = LLDB_INVALID_CPUTYPE;
1782 uint32_t sub = 0;
1783 std::string arch_name;
1784 std::string os_name;
1785 std::string vendor_name;
1786 std::string triple;
1787 uint32_t pointer_byte_size = 0;
1788 StringExtractor extractor;
1789 ByteOrder byte_order = eByteOrderInvalid;
1790 uint32_t num_keys_decoded = 0;
1791 while (response.GetNameColonValue(name, value))
1792 {
1793 if (name.compare("cputype") == 0)
1794 {
1795 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 16);
1796 if (cpu != LLDB_INVALID_CPUTYPE)
1797 ++num_keys_decoded;
1798 }
1799 else if (name.compare("cpusubtype") == 0)
1800 {
1801 sub = Args::StringToUInt32 (value.c_str(), 0, 16);
1802 if (sub != 0)
1803 ++num_keys_decoded;
1804 }
1805 else if (name.compare("ostype") == 0)
1806 {
1807 os_name.swap (value);
1808 ++num_keys_decoded;
1809 }
1810 else if (name.compare("vendor") == 0)
1811 {
1812 vendor_name.swap(value);
1813 ++num_keys_decoded;
1814 }
1815 else if (name.compare("endian") == 0)
1816 {
1817 ++num_keys_decoded;
1818 if (value.compare("little") == 0)
1819 byte_order = eByteOrderLittle;
1820 else if (value.compare("big") == 0)
1821 byte_order = eByteOrderBig;
1822 else if (value.compare("pdp") == 0)
1823 byte_order = eByteOrderPDP;
1824 else
1825 --num_keys_decoded;
1826 }
1827 else if (name.compare("ptrsize") == 0)
1828 {
1829 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 16);
1830 if (pointer_byte_size != 0)
1831 ++num_keys_decoded;
1832 }
1833 }
1834 if (num_keys_decoded > 0)
1835 m_qProcessInfo_is_valid = eLazyBoolYes;
1836 if (cpu != LLDB_INVALID_CPUTYPE && !os_name.empty() && !vendor_name.empty())
1837 {
1838 m_process_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1839 if (pointer_byte_size)
1840 {
1841 assert (pointer_byte_size == m_process_arch.GetAddressByteSize());
1842 }
1843 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1844 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
1845 return true;
1846 }
1847 }
1848 }
1849 else
1850 {
1851 m_qProcessInfo_is_valid = eLazyBoolNo;
1852 }
1853
1854 return false;
1855}
1856
1857
Greg Clayton24bc5d92011-03-30 18:16:51 +00001858uint32_t
Greg Claytonb72d0f02011-04-12 05:54:46 +00001859GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1860 ProcessInstanceInfoList &process_infos)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001861{
1862 process_infos.Clear();
1863
1864 if (m_supports_qfProcessInfo)
1865 {
1866 StreamString packet;
1867 packet.PutCString ("qfProcessInfo");
1868 if (!match_info.MatchAllProcesses())
1869 {
1870 packet.PutChar (':');
1871 const char *name = match_info.GetProcessInfo().GetName();
1872 bool has_name_match = false;
1873 if (name && name[0])
1874 {
1875 has_name_match = true;
1876 NameMatchType name_match_type = match_info.GetNameMatchType();
1877 switch (name_match_type)
1878 {
1879 case eNameMatchIgnore:
1880 has_name_match = false;
1881 break;
1882
1883 case eNameMatchEquals:
1884 packet.PutCString ("name_match:equals;");
1885 break;
1886
1887 case eNameMatchContains:
1888 packet.PutCString ("name_match:contains;");
1889 break;
1890
1891 case eNameMatchStartsWith:
1892 packet.PutCString ("name_match:starts_with;");
1893 break;
1894
1895 case eNameMatchEndsWith:
1896 packet.PutCString ("name_match:ends_with;");
1897 break;
1898
1899 case eNameMatchRegularExpression:
1900 packet.PutCString ("name_match:regex;");
1901 break;
1902 }
1903 if (has_name_match)
1904 {
1905 packet.PutCString ("name:");
1906 packet.PutBytesAsRawHex8(name, ::strlen(name));
1907 packet.PutChar (';');
1908 }
1909 }
1910
1911 if (match_info.GetProcessInfo().ProcessIDIsValid())
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001912 packet.Printf("pid:%" PRIu64 ";",match_info.GetProcessInfo().GetProcessID());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001913 if (match_info.GetProcessInfo().ParentProcessIDIsValid())
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001914 packet.Printf("parent_pid:%" PRIu64 ";",match_info.GetProcessInfo().GetParentProcessID());
Greg Claytonb72d0f02011-04-12 05:54:46 +00001915 if (match_info.GetProcessInfo().UserIDIsValid())
1916 packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID());
1917 if (match_info.GetProcessInfo().GroupIDIsValid())
1918 packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001919 if (match_info.GetProcessInfo().EffectiveUserIDIsValid())
1920 packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID());
1921 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1922 packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID());
1923 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1924 packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0);
1925 if (match_info.GetProcessInfo().GetArchitecture().IsValid())
1926 {
1927 const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture();
1928 const llvm::Triple &triple = match_arch.GetTriple();
1929 packet.PutCString("triple:");
1930 packet.PutCStringAsRawHex8(triple.getTriple().c_str());
1931 packet.PutChar (';');
1932 }
1933 }
1934 StringExtractorGDBRemote response;
1935 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1936 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001937 do
1938 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001939 ProcessInstanceInfo process_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001940 if (!DecodeProcessInfoResponse (response, process_info))
1941 break;
1942 process_infos.Append(process_info);
1943 response.GetStringRef().clear();
1944 response.SetFilePos(0);
1945 } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false));
1946 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001947 else
1948 {
1949 m_supports_qfProcessInfo = false;
1950 return 0;
1951 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001952 }
1953 return process_infos.GetSize();
1954
1955}
1956
1957bool
1958GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name)
1959{
1960 if (m_supports_qUserName)
1961 {
1962 char packet[32];
1963 const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid);
1964 assert (packet_len < sizeof(packet));
1965 StringExtractorGDBRemote response;
1966 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1967 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001968 if (response.IsNormalResponse())
1969 {
1970 // Make sure we parsed the right number of characters. The response is
1971 // the hex encoded user name and should make up the entire packet.
1972 // If there are any non-hex ASCII bytes, the length won't match below..
1973 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
1974 return true;
1975 }
1976 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001977 else
1978 {
1979 m_supports_qUserName = false;
1980 return false;
1981 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001982 }
1983 return false;
1984
1985}
1986
1987bool
1988GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name)
1989{
1990 if (m_supports_qGroupName)
1991 {
1992 char packet[32];
1993 const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid);
1994 assert (packet_len < sizeof(packet));
1995 StringExtractorGDBRemote response;
1996 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1997 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001998 if (response.IsNormalResponse())
1999 {
2000 // Make sure we parsed the right number of characters. The response is
2001 // the hex encoded group name and should make up the entire packet.
2002 // If there are any non-hex ASCII bytes, the length won't match below..
2003 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
2004 return true;
2005 }
2006 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00002007 else
2008 {
2009 m_supports_qGroupName = false;
2010 return false;
2011 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00002012 }
2013 return false;
Greg Clayton06d7cc82011-04-04 18:18:57 +00002014}
Greg Clayton24bc5d92011-03-30 18:16:51 +00002015
Greg Clayton06d7cc82011-04-04 18:18:57 +00002016void
2017GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets)
2018{
2019 uint32_t i;
2020 TimeValue start_time, end_time;
2021 uint64_t total_time_nsec;
2022 float packets_per_second;
2023 if (SendSpeedTestPacket (0, 0))
2024 {
2025 for (uint32_t send_size = 0; send_size <= 1024; send_size *= 2)
2026 {
2027 for (uint32_t recv_size = 0; recv_size <= 1024; recv_size *= 2)
2028 {
2029 start_time = TimeValue::Now();
2030 for (i=0; i<num_packets; ++i)
2031 {
2032 SendSpeedTestPacket (send_size, recv_size);
2033 }
2034 end_time = TimeValue::Now();
2035 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00002036 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002037 printf ("%u qSpeedTest(send=%-5u, recv=%-5u) in %" PRIu64 ".%9.9" PRIu64 " sec for %f packets/sec.\n",
Greg Clayton06d7cc82011-04-04 18:18:57 +00002038 num_packets,
2039 send_size,
2040 recv_size,
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00002041 total_time_nsec / TimeValue::NanoSecPerSec,
2042 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton06d7cc82011-04-04 18:18:57 +00002043 packets_per_second);
2044 if (recv_size == 0)
2045 recv_size = 32;
2046 }
2047 if (send_size == 0)
2048 send_size = 32;
2049 }
2050 }
2051 else
2052 {
2053 start_time = TimeValue::Now();
2054 for (i=0; i<num_packets; ++i)
2055 {
2056 GetCurrentProcessID ();
2057 }
2058 end_time = TimeValue::Now();
2059 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00002060 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002061 printf ("%u 'qC' packets packets in 0x%" PRIu64 "%9.9" PRIu64 " sec for %f packets/sec.\n",
Greg Clayton06d7cc82011-04-04 18:18:57 +00002062 num_packets,
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00002063 total_time_nsec / TimeValue::NanoSecPerSec,
2064 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton06d7cc82011-04-04 18:18:57 +00002065 packets_per_second);
2066 }
2067}
2068
2069bool
2070GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size)
2071{
2072 StreamString packet;
2073 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size);
2074 uint32_t bytes_left = send_size;
2075 while (bytes_left > 0)
2076 {
2077 if (bytes_left >= 26)
2078 {
2079 packet.PutCString("abcdefghijklmnopqrstuvwxyz");
2080 bytes_left -= 26;
2081 }
2082 else
2083 {
2084 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz");
2085 bytes_left = 0;
2086 }
2087 }
2088
2089 StringExtractorGDBRemote response;
Greg Clayton5b53e8e2011-05-15 23:46:54 +00002090 return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) > 0;
Greg Clayton06d7cc82011-04-04 18:18:57 +00002091 return false;
Greg Clayton24bc5d92011-03-30 18:16:51 +00002092}
Greg Claytonb72d0f02011-04-12 05:54:46 +00002093
2094uint16_t
2095GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort ()
2096{
2097 StringExtractorGDBRemote response;
2098 if (SendPacketAndWaitForResponse("qLaunchGDBServer", strlen("qLaunchGDBServer"), response, false))
2099 {
2100 std::string name;
2101 std::string value;
2102 uint16_t port = 0;
Greg Clayton4a379b12012-07-17 03:23:13 +00002103 //lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
Greg Claytonb72d0f02011-04-12 05:54:46 +00002104 while (response.GetNameColonValue(name, value))
2105 {
2106 if (name.size() == 4 && name.compare("port") == 0)
2107 port = Args::StringToUInt32(value.c_str(), 0, 0);
Greg Clayton4a379b12012-07-17 03:23:13 +00002108// if (name.size() == 3 && name.compare("pid") == 0)
2109// pid = Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0);
Greg Claytonb72d0f02011-04-12 05:54:46 +00002110 }
2111 return port;
2112 }
2113 return 0;
2114}
2115
2116bool
2117GDBRemoteCommunicationClient::SetCurrentThread (int tid)
2118{
2119 if (m_curr_tid == tid)
2120 return true;
2121
2122 char packet[32];
2123 int packet_len;
2124 if (tid <= 0)
2125 packet_len = ::snprintf (packet, sizeof(packet), "Hg%i", tid);
2126 else
2127 packet_len = ::snprintf (packet, sizeof(packet), "Hg%x", tid);
2128 assert (packet_len + 1 < sizeof(packet));
2129 StringExtractorGDBRemote response;
2130 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2131 {
2132 if (response.IsOKResponse())
2133 {
2134 m_curr_tid = tid;
2135 return true;
2136 }
2137 }
2138 return false;
2139}
2140
2141bool
2142GDBRemoteCommunicationClient::SetCurrentThreadForRun (int tid)
2143{
2144 if (m_curr_tid_run == tid)
2145 return true;
2146
2147 char packet[32];
2148 int packet_len;
2149 if (tid <= 0)
2150 packet_len = ::snprintf (packet, sizeof(packet), "Hc%i", tid);
2151 else
2152 packet_len = ::snprintf (packet, sizeof(packet), "Hc%x", tid);
2153
2154 assert (packet_len + 1 < sizeof(packet));
2155 StringExtractorGDBRemote response;
2156 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2157 {
2158 if (response.IsOKResponse())
2159 {
2160 m_curr_tid_run = tid;
2161 return true;
2162 }
2163 }
2164 return false;
2165}
2166
2167bool
2168GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response)
2169{
2170 if (SendPacketAndWaitForResponse("?", 1, response, false))
2171 return response.IsNormalResponse();
2172 return false;
2173}
2174
2175bool
Greg Clayton37d3fce2012-10-13 02:11:55 +00002176GDBRemoteCommunicationClient::GetThreadStopInfo (lldb::tid_t tid, StringExtractorGDBRemote &response)
Greg Claytonb72d0f02011-04-12 05:54:46 +00002177{
2178 if (m_supports_qThreadStopInfo)
2179 {
2180 char packet[256];
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002181 int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%" PRIx64, tid);
Greg Claytonb72d0f02011-04-12 05:54:46 +00002182 assert (packet_len < sizeof(packet));
2183 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2184 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +00002185 if (response.IsNormalResponse())
Greg Claytonb72d0f02011-04-12 05:54:46 +00002186 return true;
2187 else
2188 return false;
2189 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00002190 else
2191 {
2192 m_supports_qThreadStopInfo = false;
2193 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00002194 }
Greg Clayton139da722011-05-20 03:15:54 +00002195// if (SetCurrentThread (tid))
2196// return GetStopReply (response);
Greg Claytonb72d0f02011-04-12 05:54:46 +00002197 return false;
2198}
2199
2200
2201uint8_t
2202GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length)
2203{
2204 switch (type)
2205 {
2206 case eBreakpointSoftware: if (!m_supports_z0) return UINT8_MAX; break;
2207 case eBreakpointHardware: if (!m_supports_z1) return UINT8_MAX; break;
2208 case eWatchpointWrite: if (!m_supports_z2) return UINT8_MAX; break;
2209 case eWatchpointRead: if (!m_supports_z3) return UINT8_MAX; break;
2210 case eWatchpointReadWrite: if (!m_supports_z4) return UINT8_MAX; break;
Greg Claytonb72d0f02011-04-12 05:54:46 +00002211 }
2212
2213 char packet[64];
2214 const int packet_len = ::snprintf (packet,
2215 sizeof(packet),
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002216 "%c%i,%" PRIx64 ",%x",
Greg Claytonb72d0f02011-04-12 05:54:46 +00002217 insert ? 'Z' : 'z',
2218 type,
2219 addr,
2220 length);
2221
2222 assert (packet_len + 1 < sizeof(packet));
2223 StringExtractorGDBRemote response;
2224 if (SendPacketAndWaitForResponse(packet, packet_len, response, true))
2225 {
2226 if (response.IsOKResponse())
2227 return 0;
Greg Claytonb72d0f02011-04-12 05:54:46 +00002228 else if (response.IsErrorResponse())
2229 return response.GetError();
2230 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00002231 else
2232 {
2233 switch (type)
2234 {
2235 case eBreakpointSoftware: m_supports_z0 = false; break;
2236 case eBreakpointHardware: m_supports_z1 = false; break;
2237 case eWatchpointWrite: m_supports_z2 = false; break;
2238 case eWatchpointRead: m_supports_z3 = false; break;
2239 case eWatchpointReadWrite: m_supports_z4 = false; break;
Greg Clayton5b53e8e2011-05-15 23:46:54 +00002240 }
2241 }
2242
Greg Claytonb72d0f02011-04-12 05:54:46 +00002243 return UINT8_MAX;
2244}
Greg Clayton4a60f9e2011-05-20 23:38:13 +00002245
2246size_t
2247GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
2248 bool &sequence_mutex_unavailable)
2249{
2250 Mutex::Locker locker;
2251 thread_ids.clear();
2252
Jim Ingham088684d2012-06-08 22:50:40 +00002253 if (GetSequenceMutex (locker, "ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex"))
Greg Clayton4a60f9e2011-05-20 23:38:13 +00002254 {
2255 sequence_mutex_unavailable = false;
2256 StringExtractorGDBRemote response;
2257
Greg Clayton63afdb02011-06-17 01:22:15 +00002258 for (SendPacketNoLock ("qfThreadInfo", strlen("qfThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
Greg Clayton4a60f9e2011-05-20 23:38:13 +00002259 response.IsNormalResponse();
Greg Clayton63afdb02011-06-17 01:22:15 +00002260 SendPacketNoLock ("qsThreadInfo", strlen("qsThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()))
Greg Clayton4a60f9e2011-05-20 23:38:13 +00002261 {
2262 char ch = response.GetChar();
2263 if (ch == 'l')
2264 break;
2265 if (ch == 'm')
2266 {
2267 do
2268 {
2269 tid_t tid = response.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID);
2270
2271 if (tid != LLDB_INVALID_THREAD_ID)
2272 {
2273 thread_ids.push_back (tid);
2274 }
2275 ch = response.GetChar(); // Skip the command separator
2276 } while (ch == ','); // Make sure we got a comma separator
2277 }
2278 }
2279 }
2280 else
2281 {
Jim Ingham088684d2012-06-08 22:50:40 +00002282#if defined (LLDB_CONFIGURATION_DEBUG)
2283 // assert(!"ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex");
2284#else
Greg Claytonc8dd5702012-04-12 19:04:34 +00002285 LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
2286 if (log)
2287 log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'");
Jim Ingham088684d2012-06-08 22:50:40 +00002288#endif
Greg Clayton4a60f9e2011-05-20 23:38:13 +00002289 sequence_mutex_unavailable = true;
2290 }
2291 return thread_ids.size();
2292}
Greg Clayton516f0842012-04-11 00:24:49 +00002293
2294lldb::addr_t
2295GDBRemoteCommunicationClient::GetShlibInfoAddr()
2296{
2297 if (!IsRunning())
2298 {
2299 StringExtractorGDBRemote response;
2300 if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false))
2301 {
2302 if (response.IsNormalResponse())
2303 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
2304 }
2305 }
2306 return LLDB_INVALID_ADDRESS;
2307}
2308