blob: 9351b93761bb3288ade7d224196941e9ea756bcb [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 Clayton952e9dc2013-03-27 23:08:40 +0000314 Log *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 Clayton952e9dc2013-03-27 23:08:40 +0000530 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton61d043b2011-03-22 04:00:09 +0000531 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 Clayton04ce77f2013-02-22 22:23:55 +0000543 bool got_async_packet = false;
Greg Clayton628cead2011-05-19 03:54:16 +0000544
Greg Clayton61d043b2011-03-22 04:00:09 +0000545 while (state == eStateRunning)
546 {
Greg Clayton04ce77f2013-02-22 22:23:55 +0000547 if (!got_async_packet)
Greg Clayton628cead2011-05-19 03:54:16 +0000548 {
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
Greg Clayton04ce77f2013-02-22 22:23:55 +0000557 got_async_packet = 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 {
Greg Clayton952e9dc2013-03-27 23:08:40 +0000680 Log * packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Greg Clayton05e4d972012-03-29 01:55:41 +0000681
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 Clayton04ce77f2013-02-22 22:23:55 +0000740 got_async_packet = 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 {
Greg Clayton04ce77f2013-02-22 22:23:55 +0000753 got_async_packet = true;
Han Ming Ong0df19cc2013-01-18 23:11:53 +0000754 std::string input = response.GetStringRef().substr(1); // '1' to move beyond 'A'
755 if (m_partial_profile_data.length() > 0)
756 {
757 m_partial_profile_data.append(input);
758 input = m_partial_profile_data;
759 m_partial_profile_data.clear();
760 }
761
762 size_t found, pos = 0, len = input.length();
763 while ((found = input.find(end_delimiter, pos)) != std::string::npos)
764 {
765 StringExtractorGDBRemote profileDataExtractor(input.substr(pos, found).c_str());
766 const std::string& profile_data = HarmonizeThreadIdsForProfileData(process, profileDataExtractor);
767 process->BroadcastAsyncProfileData (profile_data.c_str(), profile_data.length());
768
769 pos = found + end_delimiter_len;
770 }
771
772 if (pos < len)
773 {
774 // Last incomplete chunk.
775 m_partial_profile_data = input.substr(pos);
776 }
Han Ming Ongfb9cee62012-11-17 00:21:04 +0000777 }
778 break;
779
Greg Clayton61d043b2011-03-22 04:00:09 +0000780 case 'E':
781 // ERROR
782 state = eStateInvalid;
783 break;
784
785 default:
786 if (log)
787 log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__);
788 state = eStateInvalid;
789 break;
790 }
791 }
792 }
793 else
794 {
795 if (log)
796 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__);
797 state = eStateInvalid;
798 }
799 }
800 if (log)
801 log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state));
802 response.SetFilePos(0);
803 m_private_is_running.SetValue (false, eBroadcastAlways);
804 m_public_is_running.SetValue (false, eBroadcastAlways);
805 return state;
806}
807
808bool
809GDBRemoteCommunicationClient::SendAsyncSignal (int signo)
810{
Greg Clayton05e4d972012-03-29 01:55:41 +0000811 Mutex::Locker async_locker (m_async_mutex);
Greg Clayton61d043b2011-03-22 04:00:09 +0000812 m_async_signal = signo;
813 bool timed_out = false;
Greg Clayton61d043b2011-03-22 04:00:09 +0000814 Mutex::Locker locker;
Greg Clayton05e4d972012-03-29 01:55:41 +0000815 if (SendInterrupt (locker, 1, timed_out))
Greg Clayton61d043b2011-03-22 04:00:09 +0000816 return true;
817 m_async_signal = -1;
818 return false;
819}
820
Greg Clayton516f0842012-04-11 00:24:49 +0000821// This function takes a mutex locker as a parameter in case the GetSequenceMutex
Greg Clayton61d043b2011-03-22 04:00:09 +0000822// actually succeeds. If it doesn't succeed in acquiring the sequence mutex
823// (the expected result), then it will send the halt packet. If it does succeed
824// then the caller that requested the interrupt will want to keep the sequence
825// locked down so that no one else can send packets while the caller has control.
826// This function usually gets called when we are running and need to stop the
827// target. It can also be used when we are running and and we need to do something
828// else (like read/write memory), so we need to interrupt the running process
829// (gdb remote protocol requires this), and do what we need to do, then resume.
830
831bool
Greg Clayton05e4d972012-03-29 01:55:41 +0000832GDBRemoteCommunicationClient::SendInterrupt
Greg Clayton61d043b2011-03-22 04:00:09 +0000833(
834 Mutex::Locker& locker,
835 uint32_t seconds_to_wait_for_stop,
Greg Clayton61d043b2011-03-22 04:00:09 +0000836 bool &timed_out
837)
838{
Greg Clayton61d043b2011-03-22 04:00:09 +0000839 timed_out = false;
Greg Clayton952e9dc2013-03-27 23:08:40 +0000840 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Clayton61d043b2011-03-22 04:00:09 +0000841
842 if (IsRunning())
843 {
844 // Only send an interrupt if our debugserver is running...
Greg Claytonc8dd5702012-04-12 19:04:34 +0000845 if (GetSequenceMutex (locker))
Greg Clayton516f0842012-04-11 00:24:49 +0000846 {
847 if (log)
848 log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt");
849 }
850 else
Greg Clayton61d043b2011-03-22 04:00:09 +0000851 {
852 // Someone has the mutex locked waiting for a response or for the
853 // inferior to stop, so send the interrupt on the down low...
854 char ctrl_c = '\x03';
855 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton61d043b2011-03-22 04:00:09 +0000856 size_t bytes_written = Write (&ctrl_c, 1, status, NULL);
Greg Clayton05e4d972012-03-29 01:55:41 +0000857 if (log)
858 log->PutCString("send packet: \\x03");
Greg Clayton61d043b2011-03-22 04:00:09 +0000859 if (bytes_written > 0)
860 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000861 m_interrupt_sent = true;
Greg Clayton61d043b2011-03-22 04:00:09 +0000862 if (seconds_to_wait_for_stop)
863 {
Greg Clayton05e4d972012-03-29 01:55:41 +0000864 TimeValue timeout;
865 if (seconds_to_wait_for_stop)
866 {
867 timeout = TimeValue::Now();
868 timeout.OffsetWithSeconds (seconds_to_wait_for_stop);
869 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000870 if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out))
871 {
872 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000873 log->PutCString ("SendInterrupt () - sent interrupt, private state stopped");
Greg Clayton61d043b2011-03-22 04:00:09 +0000874 return true;
875 }
876 else
877 {
878 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000879 log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume");
Greg Clayton61d043b2011-03-22 04:00:09 +0000880 }
881 }
882 else
883 {
884 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000885 log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop...");
Greg Clayton61d043b2011-03-22 04:00:09 +0000886 return true;
887 }
888 }
889 else
890 {
891 if (log)
Greg Clayton05e4d972012-03-29 01:55:41 +0000892 log->Printf ("SendInterrupt () - failed to write interrupt");
Greg Clayton61d043b2011-03-22 04:00:09 +0000893 }
894 return false;
895 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000896 }
Greg Clayton05e4d972012-03-29 01:55:41 +0000897 else
898 {
899 if (log)
900 log->Printf ("SendInterrupt () - not running");
901 }
Greg Clayton61d043b2011-03-22 04:00:09 +0000902 return true;
903}
904
905lldb::pid_t
906GDBRemoteCommunicationClient::GetCurrentProcessID ()
907{
908 StringExtractorGDBRemote response;
909 if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false))
910 {
911 if (response.GetChar() == 'Q')
912 if (response.GetChar() == 'C')
913 return response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID);
914 }
915 return LLDB_INVALID_PROCESS_ID;
916}
917
918bool
919GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str)
920{
921 error_str.clear();
922 StringExtractorGDBRemote response;
923 if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false))
924 {
925 if (response.IsOKResponse())
926 return true;
927 if (response.GetChar() == 'E')
928 {
929 // A string the describes what failed when launching...
930 error_str = response.GetStringRef().substr(1);
931 }
932 else
933 {
934 error_str.assign ("unknown error occurred launching process");
935 }
936 }
937 else
938 {
Jim Ingham7b3a49b2012-06-28 20:30:23 +0000939 error_str.assign ("timed out waiting for app to launch");
Greg Clayton61d043b2011-03-22 04:00:09 +0000940 }
941 return false;
942}
943
944int
945GDBRemoteCommunicationClient::SendArgumentsPacket (char const *argv[])
946{
947 if (argv && argv[0])
948 {
949 StreamString packet;
950 packet.PutChar('A');
951 const char *arg;
952 for (uint32_t i = 0; (arg = argv[i]) != NULL; ++i)
953 {
954 const int arg_len = strlen(arg);
955 if (i > 0)
956 packet.PutChar(',');
957 packet.Printf("%i,%i,", arg_len * 2, i);
958 packet.PutBytesAsRawHex8 (arg, arg_len);
959 }
960
961 StringExtractorGDBRemote response;
962 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
963 {
964 if (response.IsOKResponse())
965 return 0;
966 uint8_t error = response.GetError();
967 if (error)
968 return error;
969 }
970 }
971 return -1;
972}
973
974int
975GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value)
976{
977 if (name_equal_value && name_equal_value[0])
978 {
979 StreamString packet;
980 packet.Printf("QEnvironment:%s", name_equal_value);
981 StringExtractorGDBRemote response;
982 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
983 {
984 if (response.IsOKResponse())
985 return 0;
986 uint8_t error = response.GetError();
987 if (error)
988 return error;
989 }
990 }
991 return -1;
992}
993
Greg Claytona4582402011-05-08 04:53:50 +0000994int
995GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch)
996{
997 if (arch && arch[0])
998 {
999 StreamString packet;
1000 packet.Printf("QLaunchArch:%s", arch);
1001 StringExtractorGDBRemote response;
1002 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1003 {
1004 if (response.IsOKResponse())
1005 return 0;
1006 uint8_t error = response.GetError();
1007 if (error)
1008 return error;
1009 }
1010 }
1011 return -1;
1012}
1013
Greg Clayton61d043b2011-03-22 04:00:09 +00001014bool
Greg Clayton58e26e02011-03-24 04:28:38 +00001015GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major,
1016 uint32_t &minor,
1017 uint32_t &update)
1018{
1019 if (GetHostInfo ())
1020 {
1021 if (m_os_version_major != UINT32_MAX)
1022 {
1023 major = m_os_version_major;
1024 minor = m_os_version_minor;
1025 update = m_os_version_update;
1026 return true;
1027 }
1028 }
1029 return false;
1030}
1031
1032bool
1033GDBRemoteCommunicationClient::GetOSBuildString (std::string &s)
1034{
1035 if (GetHostInfo ())
1036 {
1037 if (!m_os_build.empty())
1038 {
1039 s = m_os_build;
1040 return true;
1041 }
1042 }
1043 s.clear();
1044 return false;
1045}
1046
1047
1048bool
1049GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s)
1050{
1051 if (GetHostInfo ())
1052 {
1053 if (!m_os_kernel.empty())
1054 {
1055 s = m_os_kernel;
1056 return true;
1057 }
1058 }
1059 s.clear();
1060 return false;
1061}
1062
1063bool
1064GDBRemoteCommunicationClient::GetHostname (std::string &s)
1065{
1066 if (GetHostInfo ())
1067 {
1068 if (!m_hostname.empty())
1069 {
1070 s = m_hostname;
1071 return true;
1072 }
1073 }
1074 s.clear();
1075 return false;
1076}
1077
1078ArchSpec
1079GDBRemoteCommunicationClient::GetSystemArchitecture ()
1080{
1081 if (GetHostInfo ())
1082 return m_host_arch;
1083 return ArchSpec();
1084}
1085
Jason Molendafe555672012-12-19 02:54:03 +00001086const lldb_private::ArchSpec &
1087GDBRemoteCommunicationClient::GetProcessArchitecture ()
1088{
1089 if (m_qProcessInfo_is_valid == eLazyBoolCalculate)
1090 GetCurrentProcessInfo ();
1091 return m_process_arch;
1092}
1093
Greg Clayton58e26e02011-03-24 04:28:38 +00001094
1095bool
Greg Clayton06d7cc82011-04-04 18:18:57 +00001096GDBRemoteCommunicationClient::GetHostInfo (bool force)
Greg Clayton61d043b2011-03-22 04:00:09 +00001097{
Greg Clayton06d7cc82011-04-04 18:18:57 +00001098 if (force || m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton61d043b2011-03-22 04:00:09 +00001099 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001100 m_qHostInfo_is_valid = eLazyBoolNo;
Greg Clayton61d043b2011-03-22 04:00:09 +00001101 StringExtractorGDBRemote response;
1102 if (SendPacketAndWaitForResponse ("qHostInfo", response, false))
1103 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001104 if (response.IsNormalResponse())
Greg Claytoncb8977d2011-03-23 00:09:55 +00001105 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001106 std::string name;
1107 std::string value;
1108 uint32_t cpu = LLDB_INVALID_CPUTYPE;
1109 uint32_t sub = 0;
1110 std::string arch_name;
1111 std::string os_name;
1112 std::string vendor_name;
1113 std::string triple;
1114 uint32_t pointer_byte_size = 0;
1115 StringExtractor extractor;
1116 ByteOrder byte_order = eByteOrderInvalid;
1117 uint32_t num_keys_decoded = 0;
1118 while (response.GetNameColonValue(name, value))
Greg Claytoncb8977d2011-03-23 00:09:55 +00001119 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001120 if (name.compare("cputype") == 0)
Greg Clayton58e26e02011-03-24 04:28:38 +00001121 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001122 // exception type in big endian hex
1123 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0);
1124 if (cpu != LLDB_INVALID_CPUTYPE)
1125 ++num_keys_decoded;
1126 }
1127 else if (name.compare("cpusubtype") == 0)
1128 {
1129 // exception count in big endian hex
1130 sub = Args::StringToUInt32 (value.c_str(), 0, 0);
1131 if (sub != 0)
1132 ++num_keys_decoded;
1133 }
1134 else if (name.compare("arch") == 0)
1135 {
1136 arch_name.swap (value);
1137 ++num_keys_decoded;
1138 }
1139 else if (name.compare("triple") == 0)
1140 {
1141 // The triple comes as ASCII hex bytes since it contains '-' chars
1142 extractor.GetStringRef().swap(value);
1143 extractor.SetFilePos(0);
1144 extractor.GetHexByteString (triple);
1145 ++num_keys_decoded;
1146 }
1147 else if (name.compare("os_build") == 0)
1148 {
1149 extractor.GetStringRef().swap(value);
1150 extractor.SetFilePos(0);
1151 extractor.GetHexByteString (m_os_build);
1152 ++num_keys_decoded;
1153 }
1154 else if (name.compare("hostname") == 0)
1155 {
1156 extractor.GetStringRef().swap(value);
1157 extractor.SetFilePos(0);
1158 extractor.GetHexByteString (m_hostname);
1159 ++num_keys_decoded;
1160 }
1161 else if (name.compare("os_kernel") == 0)
1162 {
1163 extractor.GetStringRef().swap(value);
1164 extractor.SetFilePos(0);
1165 extractor.GetHexByteString (m_os_kernel);
1166 ++num_keys_decoded;
1167 }
1168 else if (name.compare("ostype") == 0)
1169 {
1170 os_name.swap (value);
1171 ++num_keys_decoded;
1172 }
1173 else if (name.compare("vendor") == 0)
1174 {
1175 vendor_name.swap(value);
1176 ++num_keys_decoded;
1177 }
1178 else if (name.compare("endian") == 0)
1179 {
1180 ++num_keys_decoded;
1181 if (value.compare("little") == 0)
1182 byte_order = eByteOrderLittle;
1183 else if (value.compare("big") == 0)
1184 byte_order = eByteOrderBig;
1185 else if (value.compare("pdp") == 0)
1186 byte_order = eByteOrderPDP;
1187 else
1188 --num_keys_decoded;
1189 }
1190 else if (name.compare("ptrsize") == 0)
1191 {
1192 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 0);
1193 if (pointer_byte_size != 0)
1194 ++num_keys_decoded;
1195 }
1196 else if (name.compare("os_version") == 0)
1197 {
1198 Args::StringToVersion (value.c_str(),
1199 m_os_version_major,
1200 m_os_version_minor,
1201 m_os_version_update);
1202 if (m_os_version_major != UINT32_MAX)
1203 ++num_keys_decoded;
1204 }
Enrico Granata7de2a3b2012-07-13 23:18:48 +00001205 else if (name.compare("watchpoint_exceptions_received") == 0)
1206 {
1207 ++num_keys_decoded;
1208 if (strcmp(value.c_str(),"before") == 0)
1209 m_watchpoints_trigger_after_instruction = eLazyBoolNo;
1210 else if (strcmp(value.c_str(),"after") == 0)
1211 m_watchpoints_trigger_after_instruction = eLazyBoolYes;
1212 else
1213 --num_keys_decoded;
1214 }
1215
Greg Clayton24bc5d92011-03-30 18:16:51 +00001216 }
1217
1218 if (num_keys_decoded > 0)
1219 m_qHostInfo_is_valid = eLazyBoolYes;
1220
1221 if (triple.empty())
1222 {
1223 if (arch_name.empty())
1224 {
1225 if (cpu != LLDB_INVALID_CPUTYPE)
1226 {
1227 m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1228 if (pointer_byte_size)
1229 {
1230 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1231 }
1232 if (byte_order != eByteOrderInvalid)
1233 {
1234 assert (byte_order == m_host_arch.GetByteOrder());
1235 }
Greg Claytonb170aee2012-05-08 01:45:38 +00001236
1237 if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0)
1238 {
1239 switch (m_host_arch.GetMachine())
1240 {
1241 case llvm::Triple::arm:
1242 case llvm::Triple::thumb:
1243 os_name = "ios";
1244 break;
1245 default:
1246 os_name = "macosx";
1247 break;
1248 }
1249 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001250 if (!vendor_name.empty())
1251 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1252 if (!os_name.empty())
Greg Clayton89798cc2011-09-15 00:21:03 +00001253 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001254
1255 }
1256 }
1257 else
1258 {
1259 std::string triple;
1260 triple += arch_name;
Greg Claytonb170aee2012-05-08 01:45:38 +00001261 if (!vendor_name.empty() || !os_name.empty())
1262 {
1263 triple += '-';
1264 if (vendor_name.empty())
1265 triple += "unknown";
1266 else
1267 triple += vendor_name;
1268 triple += '-';
1269 if (os_name.empty())
1270 triple += "unknown";
1271 else
1272 triple += os_name;
1273 }
1274 m_host_arch.SetTriple (triple.c_str());
1275
1276 llvm::Triple &host_triple = m_host_arch.GetTriple();
1277 if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin)
1278 {
1279 switch (m_host_arch.GetMachine())
1280 {
1281 case llvm::Triple::arm:
1282 case llvm::Triple::thumb:
1283 host_triple.setOS(llvm::Triple::IOS);
1284 break;
1285 default:
1286 host_triple.setOS(llvm::Triple::MacOSX);
1287 break;
1288 }
1289 }
Greg Clayton58e26e02011-03-24 04:28:38 +00001290 if (pointer_byte_size)
1291 {
1292 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1293 }
1294 if (byte_order != eByteOrderInvalid)
1295 {
1296 assert (byte_order == m_host_arch.GetByteOrder());
1297 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001298
Greg Clayton58e26e02011-03-24 04:28:38 +00001299 }
1300 }
1301 else
1302 {
Greg Claytonb170aee2012-05-08 01:45:38 +00001303 m_host_arch.SetTriple (triple.c_str());
Greg Claytoncb8977d2011-03-23 00:09:55 +00001304 if (pointer_byte_size)
1305 {
1306 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1307 }
1308 if (byte_order != eByteOrderInvalid)
1309 {
1310 assert (byte_order == m_host_arch.GetByteOrder());
1311 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001312 }
Greg Claytoncb8977d2011-03-23 00:09:55 +00001313 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001314 }
1315 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001316 return m_qHostInfo_is_valid == eLazyBoolYes;
Greg Clayton61d043b2011-03-22 04:00:09 +00001317}
1318
1319int
1320GDBRemoteCommunicationClient::SendAttach
1321(
1322 lldb::pid_t pid,
1323 StringExtractorGDBRemote& response
1324)
1325{
1326 if (pid != LLDB_INVALID_PROCESS_ID)
1327 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001328 char packet[64];
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001329 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, pid);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001330 assert (packet_len < sizeof(packet));
1331 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton61d043b2011-03-22 04:00:09 +00001332 {
1333 if (response.IsErrorResponse())
1334 return response.GetError();
1335 return 0;
1336 }
1337 }
1338 return -1;
1339}
1340
1341const lldb_private::ArchSpec &
1342GDBRemoteCommunicationClient::GetHostArchitecture ()
1343{
Greg Clayton24bc5d92011-03-30 18:16:51 +00001344 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton61d043b2011-03-22 04:00:09 +00001345 GetHostInfo ();
Greg Claytoncb8977d2011-03-23 00:09:55 +00001346 return m_host_arch;
Greg Clayton61d043b2011-03-22 04:00:09 +00001347}
1348
1349addr_t
1350GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions)
1351{
Greg Clayton2f085c62011-05-15 01:25:55 +00001352 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton61d043b2011-03-22 04:00:09 +00001353 {
Greg Clayton2f085c62011-05-15 01:25:55 +00001354 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton989816b2011-05-14 01:50:35 +00001355 char packet[64];
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001356 const int packet_len = ::snprintf (packet, sizeof(packet), "_M%" PRIx64 ",%s%s%s",
Greg Clayton851e30e2012-09-18 18:04:04 +00001357 (uint64_t)size,
Greg Clayton989816b2011-05-14 01:50:35 +00001358 permissions & lldb::ePermissionsReadable ? "r" : "",
1359 permissions & lldb::ePermissionsWritable ? "w" : "",
1360 permissions & lldb::ePermissionsExecutable ? "x" : "");
1361 assert (packet_len < sizeof(packet));
1362 StringExtractorGDBRemote response;
1363 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1364 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001365 if (!response.IsErrorResponse())
Greg Clayton989816b2011-05-14 01:50:35 +00001366 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1367 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001368 else
1369 {
1370 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1371 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001372 }
1373 return LLDB_INVALID_ADDRESS;
1374}
1375
1376bool
1377GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr)
1378{
Greg Clayton2f085c62011-05-15 01:25:55 +00001379 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton61d043b2011-03-22 04:00:09 +00001380 {
Greg Clayton2f085c62011-05-15 01:25:55 +00001381 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton989816b2011-05-14 01:50:35 +00001382 char packet[64];
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001383 const int packet_len = ::snprintf(packet, sizeof(packet), "_m%" PRIx64, (uint64_t)addr);
Greg Clayton989816b2011-05-14 01:50:35 +00001384 assert (packet_len < sizeof(packet));
1385 StringExtractorGDBRemote response;
1386 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1387 {
1388 if (response.IsOKResponse())
1389 return true;
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001390 }
1391 else
1392 {
1393 m_supports_alloc_dealloc_memory = eLazyBoolNo;
Greg Clayton989816b2011-05-14 01:50:35 +00001394 }
Greg Clayton61d043b2011-03-22 04:00:09 +00001395 }
1396 return false;
1397}
1398
Greg Clayton516f0842012-04-11 00:24:49 +00001399bool
1400GDBRemoteCommunicationClient::Detach ()
1401{
1402 return SendPacket ("D", 1) > 0;
1403}
1404
Greg Claytona9385532011-11-18 07:03:08 +00001405Error
1406GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr,
1407 lldb_private::MemoryRegionInfo &region_info)
1408{
1409 Error error;
1410 region_info.Clear();
1411
1412 if (m_supports_memory_region_info != eLazyBoolNo)
1413 {
1414 m_supports_memory_region_info = eLazyBoolYes;
1415 char packet[64];
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001416 const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%" PRIx64, (uint64_t)addr);
Greg Claytona9385532011-11-18 07:03:08 +00001417 assert (packet_len < sizeof(packet));
1418 StringExtractorGDBRemote response;
1419 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1420 {
1421 std::string name;
1422 std::string value;
1423 addr_t addr_value;
1424 bool success = true;
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001425 bool saw_permissions = false;
Greg Claytona9385532011-11-18 07:03:08 +00001426 while (success && response.GetNameColonValue(name, value))
1427 {
1428 if (name.compare ("start") == 0)
1429 {
1430 addr_value = Args::StringToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success);
1431 if (success)
1432 region_info.GetRange().SetRangeBase(addr_value);
1433 }
1434 else if (name.compare ("size") == 0)
1435 {
1436 addr_value = Args::StringToUInt64(value.c_str(), 0, 16, &success);
1437 if (success)
1438 region_info.GetRange().SetByteSize (addr_value);
1439 }
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001440 else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid())
Greg Claytona9385532011-11-18 07:03:08 +00001441 {
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001442 saw_permissions = true;
1443 if (region_info.GetRange().Contains (addr))
1444 {
1445 if (value.find('r') != std::string::npos)
1446 region_info.SetReadable (MemoryRegionInfo::eYes);
1447 else
1448 region_info.SetReadable (MemoryRegionInfo::eNo);
1449
1450 if (value.find('w') != std::string::npos)
1451 region_info.SetWritable (MemoryRegionInfo::eYes);
1452 else
1453 region_info.SetWritable (MemoryRegionInfo::eNo);
1454
1455 if (value.find('x') != std::string::npos)
1456 region_info.SetExecutable (MemoryRegionInfo::eYes);
1457 else
1458 region_info.SetExecutable (MemoryRegionInfo::eNo);
1459 }
1460 else
1461 {
1462 // The reported region does not contain this address -- we're looking at an unmapped page
1463 region_info.SetReadable (MemoryRegionInfo::eNo);
1464 region_info.SetWritable (MemoryRegionInfo::eNo);
1465 region_info.SetExecutable (MemoryRegionInfo::eNo);
1466 }
Greg Claytona9385532011-11-18 07:03:08 +00001467 }
1468 else if (name.compare ("error") == 0)
1469 {
1470 StringExtractorGDBRemote name_extractor;
1471 // Swap "value" over into "name_extractor"
1472 name_extractor.GetStringRef().swap(value);
1473 // Now convert the HEX bytes into a string value
1474 name_extractor.GetHexByteString (value);
1475 error.SetErrorString(value.c_str());
1476 }
1477 }
Jason Molenda1f9c39c2011-12-13 05:39:38 +00001478
1479 // We got a valid address range back but no permissions -- which means this is an unmapped page
1480 if (region_info.GetRange().IsValid() && saw_permissions == false)
1481 {
1482 region_info.SetReadable (MemoryRegionInfo::eNo);
1483 region_info.SetWritable (MemoryRegionInfo::eNo);
1484 region_info.SetExecutable (MemoryRegionInfo::eNo);
1485 }
Greg Claytona9385532011-11-18 07:03:08 +00001486 }
1487 else
1488 {
1489 m_supports_memory_region_info = eLazyBoolNo;
1490 }
1491 }
1492
1493 if (m_supports_memory_region_info == eLazyBoolNo)
1494 {
1495 error.SetErrorString("qMemoryRegionInfo is not supported");
1496 }
1497 if (error.Fail())
1498 region_info.Clear();
1499 return error;
1500
1501}
1502
Johnny Chen7cbdcfb2012-05-23 21:09:52 +00001503Error
1504GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num)
1505{
1506 Error error;
1507
1508 if (m_supports_watchpoint_support_info == eLazyBoolYes)
1509 {
1510 num = m_num_supported_hardware_watchpoints;
1511 return error;
1512 }
1513
1514 // Set num to 0 first.
1515 num = 0;
1516 if (m_supports_watchpoint_support_info != eLazyBoolNo)
1517 {
1518 char packet[64];
1519 const int packet_len = ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:");
1520 assert (packet_len < sizeof(packet));
1521 StringExtractorGDBRemote response;
1522 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1523 {
1524 m_supports_watchpoint_support_info = eLazyBoolYes;
1525 std::string name;
1526 std::string value;
1527 while (response.GetNameColonValue(name, value))
1528 {
1529 if (name.compare ("num") == 0)
1530 {
1531 num = Args::StringToUInt32(value.c_str(), 0, 0);
1532 m_num_supported_hardware_watchpoints = num;
1533 }
1534 }
1535 }
1536 else
1537 {
1538 m_supports_watchpoint_support_info = eLazyBoolNo;
1539 }
1540 }
1541
1542 if (m_supports_watchpoint_support_info == eLazyBoolNo)
1543 {
1544 error.SetErrorString("qWatchpointSupportInfo is not supported");
1545 }
1546 return error;
1547
1548}
Greg Claytona9385532011-11-18 07:03:08 +00001549
Enrico Granata7de2a3b2012-07-13 23:18:48 +00001550lldb_private::Error
1551GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num, bool& after)
1552{
1553 Error error(GetWatchpointSupportInfo(num));
1554 if (error.Success())
1555 error = GetWatchpointsTriggerAfterInstruction(after);
1556 return error;
1557}
1558
1559lldb_private::Error
1560GDBRemoteCommunicationClient::GetWatchpointsTriggerAfterInstruction (bool &after)
1561{
1562 Error error;
1563
1564 // we assume watchpoints will happen after running the relevant opcode
1565 // and we only want to override this behavior if we have explicitly
1566 // received a qHostInfo telling us otherwise
1567 if (m_qHostInfo_is_valid != eLazyBoolYes)
1568 after = true;
1569 else
1570 after = (m_watchpoints_trigger_after_instruction != eLazyBoolNo);
1571 return error;
1572}
1573
Greg Clayton61d043b2011-03-22 04:00:09 +00001574int
1575GDBRemoteCommunicationClient::SetSTDIN (char const *path)
1576{
1577 if (path && path[0])
1578 {
1579 StreamString packet;
1580 packet.PutCString("QSetSTDIN:");
1581 packet.PutBytesAsRawHex8(path, strlen(path));
1582
1583 StringExtractorGDBRemote response;
1584 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1585 {
1586 if (response.IsOKResponse())
1587 return 0;
1588 uint8_t error = response.GetError();
1589 if (error)
1590 return error;
1591 }
1592 }
1593 return -1;
1594}
1595
1596int
1597GDBRemoteCommunicationClient::SetSTDOUT (char const *path)
1598{
1599 if (path && path[0])
1600 {
1601 StreamString packet;
1602 packet.PutCString("QSetSTDOUT:");
1603 packet.PutBytesAsRawHex8(path, strlen(path));
1604
1605 StringExtractorGDBRemote response;
1606 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1607 {
1608 if (response.IsOKResponse())
1609 return 0;
1610 uint8_t error = response.GetError();
1611 if (error)
1612 return error;
1613 }
1614 }
1615 return -1;
1616}
1617
1618int
1619GDBRemoteCommunicationClient::SetSTDERR (char const *path)
1620{
1621 if (path && path[0])
1622 {
1623 StreamString packet;
1624 packet.PutCString("QSetSTDERR:");
1625 packet.PutBytesAsRawHex8(path, strlen(path));
1626
1627 StringExtractorGDBRemote response;
1628 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1629 {
1630 if (response.IsOKResponse())
1631 return 0;
1632 uint8_t error = response.GetError();
1633 if (error)
1634 return error;
1635 }
1636 }
1637 return -1;
1638}
1639
1640int
1641GDBRemoteCommunicationClient::SetWorkingDir (char const *path)
1642{
1643 if (path && path[0])
1644 {
1645 StreamString packet;
1646 packet.PutCString("QSetWorkingDir:");
1647 packet.PutBytesAsRawHex8(path, strlen(path));
1648
1649 StringExtractorGDBRemote response;
1650 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1651 {
1652 if (response.IsOKResponse())
1653 return 0;
1654 uint8_t error = response.GetError();
1655 if (error)
1656 return error;
1657 }
1658 }
1659 return -1;
1660}
1661
1662int
1663GDBRemoteCommunicationClient::SetDisableASLR (bool enable)
1664{
Greg Clayton24bc5d92011-03-30 18:16:51 +00001665 char packet[32];
1666 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0);
1667 assert (packet_len < sizeof(packet));
Greg Clayton61d043b2011-03-22 04:00:09 +00001668 StringExtractorGDBRemote response;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001669 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton61d043b2011-03-22 04:00:09 +00001670 {
1671 if (response.IsOKResponse())
1672 return 0;
1673 uint8_t error = response.GetError();
1674 if (error)
1675 return error;
1676 }
1677 return -1;
1678}
Greg Clayton24bc5d92011-03-30 18:16:51 +00001679
1680bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001681GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001682{
1683 if (response.IsNormalResponse())
1684 {
1685 std::string name;
1686 std::string value;
1687 StringExtractor extractor;
1688
1689 while (response.GetNameColonValue(name, value))
1690 {
1691 if (name.compare("pid") == 0)
1692 {
1693 process_info.SetProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1694 }
1695 else if (name.compare("ppid") == 0)
1696 {
1697 process_info.SetParentProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1698 }
1699 else if (name.compare("uid") == 0)
1700 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001701 process_info.SetUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001702 }
1703 else if (name.compare("euid") == 0)
1704 {
1705 process_info.SetEffectiveUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1706 }
1707 else if (name.compare("gid") == 0)
1708 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001709 process_info.SetGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton24bc5d92011-03-30 18:16:51 +00001710 }
1711 else if (name.compare("egid") == 0)
1712 {
1713 process_info.SetEffectiveGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1714 }
1715 else if (name.compare("triple") == 0)
1716 {
1717 // The triple comes as ASCII hex bytes since it contains '-' chars
1718 extractor.GetStringRef().swap(value);
1719 extractor.SetFilePos(0);
1720 extractor.GetHexByteString (value);
Greg Claytonb170aee2012-05-08 01:45:38 +00001721 process_info.GetArchitecture ().SetTriple (value.c_str());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001722 }
1723 else if (name.compare("name") == 0)
1724 {
1725 StringExtractor extractor;
Filipe Cabecinhase61ec7f2012-05-07 09:30:51 +00001726 // The process name from ASCII hex bytes since we can't
Greg Clayton24bc5d92011-03-30 18:16:51 +00001727 // control the characters in a process name
1728 extractor.GetStringRef().swap(value);
1729 extractor.SetFilePos(0);
1730 extractor.GetHexByteString (value);
Greg Clayton527154d2011-11-15 03:53:30 +00001731 process_info.GetExecutableFile().SetFile (value.c_str(), false);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001732 }
1733 }
1734
1735 if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
1736 return true;
1737 }
1738 return false;
1739}
1740
1741bool
Greg Claytonb72d0f02011-04-12 05:54:46 +00001742GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001743{
1744 process_info.Clear();
1745
1746 if (m_supports_qProcessInfoPID)
1747 {
1748 char packet[32];
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001749 const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%" PRIu64, pid);
Greg Clayton24bc5d92011-03-30 18:16:51 +00001750 assert (packet_len < sizeof(packet));
1751 StringExtractorGDBRemote response;
1752 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1753 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001754 return DecodeProcessInfoResponse (response, process_info);
1755 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001756 else
1757 {
1758 m_supports_qProcessInfoPID = false;
1759 return false;
1760 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001761 }
1762 return false;
1763}
1764
Jason Molendafe555672012-12-19 02:54:03 +00001765bool
1766GDBRemoteCommunicationClient::GetCurrentProcessInfo ()
1767{
1768 if (m_qProcessInfo_is_valid == eLazyBoolYes)
1769 return true;
1770 if (m_qProcessInfo_is_valid == eLazyBoolNo)
1771 return false;
1772
1773 GetHostInfo ();
1774
1775 StringExtractorGDBRemote response;
1776 if (SendPacketAndWaitForResponse ("qProcessInfo", response, false))
1777 {
1778 if (response.IsNormalResponse())
1779 {
1780 std::string name;
1781 std::string value;
1782 uint32_t cpu = LLDB_INVALID_CPUTYPE;
1783 uint32_t sub = 0;
1784 std::string arch_name;
1785 std::string os_name;
1786 std::string vendor_name;
1787 std::string triple;
1788 uint32_t pointer_byte_size = 0;
1789 StringExtractor extractor;
1790 ByteOrder byte_order = eByteOrderInvalid;
1791 uint32_t num_keys_decoded = 0;
1792 while (response.GetNameColonValue(name, value))
1793 {
1794 if (name.compare("cputype") == 0)
1795 {
1796 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 16);
1797 if (cpu != LLDB_INVALID_CPUTYPE)
1798 ++num_keys_decoded;
1799 }
1800 else if (name.compare("cpusubtype") == 0)
1801 {
1802 sub = Args::StringToUInt32 (value.c_str(), 0, 16);
1803 if (sub != 0)
1804 ++num_keys_decoded;
1805 }
1806 else if (name.compare("ostype") == 0)
1807 {
1808 os_name.swap (value);
1809 ++num_keys_decoded;
1810 }
1811 else if (name.compare("vendor") == 0)
1812 {
1813 vendor_name.swap(value);
1814 ++num_keys_decoded;
1815 }
1816 else if (name.compare("endian") == 0)
1817 {
1818 ++num_keys_decoded;
1819 if (value.compare("little") == 0)
1820 byte_order = eByteOrderLittle;
1821 else if (value.compare("big") == 0)
1822 byte_order = eByteOrderBig;
1823 else if (value.compare("pdp") == 0)
1824 byte_order = eByteOrderPDP;
1825 else
1826 --num_keys_decoded;
1827 }
1828 else if (name.compare("ptrsize") == 0)
1829 {
1830 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 16);
1831 if (pointer_byte_size != 0)
1832 ++num_keys_decoded;
1833 }
1834 }
1835 if (num_keys_decoded > 0)
1836 m_qProcessInfo_is_valid = eLazyBoolYes;
1837 if (cpu != LLDB_INVALID_CPUTYPE && !os_name.empty() && !vendor_name.empty())
1838 {
1839 m_process_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1840 if (pointer_byte_size)
1841 {
1842 assert (pointer_byte_size == m_process_arch.GetAddressByteSize());
1843 }
1844 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1845 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
1846 return true;
1847 }
1848 }
1849 }
1850 else
1851 {
1852 m_qProcessInfo_is_valid = eLazyBoolNo;
1853 }
1854
1855 return false;
1856}
1857
1858
Greg Clayton24bc5d92011-03-30 18:16:51 +00001859uint32_t
Greg Claytonb72d0f02011-04-12 05:54:46 +00001860GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1861 ProcessInstanceInfoList &process_infos)
Greg Clayton24bc5d92011-03-30 18:16:51 +00001862{
1863 process_infos.Clear();
1864
1865 if (m_supports_qfProcessInfo)
1866 {
1867 StreamString packet;
1868 packet.PutCString ("qfProcessInfo");
1869 if (!match_info.MatchAllProcesses())
1870 {
1871 packet.PutChar (':');
1872 const char *name = match_info.GetProcessInfo().GetName();
1873 bool has_name_match = false;
1874 if (name && name[0])
1875 {
1876 has_name_match = true;
1877 NameMatchType name_match_type = match_info.GetNameMatchType();
1878 switch (name_match_type)
1879 {
1880 case eNameMatchIgnore:
1881 has_name_match = false;
1882 break;
1883
1884 case eNameMatchEquals:
1885 packet.PutCString ("name_match:equals;");
1886 break;
1887
1888 case eNameMatchContains:
1889 packet.PutCString ("name_match:contains;");
1890 break;
1891
1892 case eNameMatchStartsWith:
1893 packet.PutCString ("name_match:starts_with;");
1894 break;
1895
1896 case eNameMatchEndsWith:
1897 packet.PutCString ("name_match:ends_with;");
1898 break;
1899
1900 case eNameMatchRegularExpression:
1901 packet.PutCString ("name_match:regex;");
1902 break;
1903 }
1904 if (has_name_match)
1905 {
1906 packet.PutCString ("name:");
1907 packet.PutBytesAsRawHex8(name, ::strlen(name));
1908 packet.PutChar (';');
1909 }
1910 }
1911
1912 if (match_info.GetProcessInfo().ProcessIDIsValid())
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001913 packet.Printf("pid:%" PRIu64 ";",match_info.GetProcessInfo().GetProcessID());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001914 if (match_info.GetProcessInfo().ParentProcessIDIsValid())
Daniel Malea5f35a4b2012-11-29 21:49:15 +00001915 packet.Printf("parent_pid:%" PRIu64 ";",match_info.GetProcessInfo().GetParentProcessID());
Greg Claytonb72d0f02011-04-12 05:54:46 +00001916 if (match_info.GetProcessInfo().UserIDIsValid())
1917 packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID());
1918 if (match_info.GetProcessInfo().GroupIDIsValid())
1919 packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID());
Greg Clayton24bc5d92011-03-30 18:16:51 +00001920 if (match_info.GetProcessInfo().EffectiveUserIDIsValid())
1921 packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID());
1922 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1923 packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID());
1924 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1925 packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0);
1926 if (match_info.GetProcessInfo().GetArchitecture().IsValid())
1927 {
1928 const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture();
1929 const llvm::Triple &triple = match_arch.GetTriple();
1930 packet.PutCString("triple:");
1931 packet.PutCStringAsRawHex8(triple.getTriple().c_str());
1932 packet.PutChar (';');
1933 }
1934 }
1935 StringExtractorGDBRemote response;
1936 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1937 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001938 do
1939 {
Greg Claytonb72d0f02011-04-12 05:54:46 +00001940 ProcessInstanceInfo process_info;
Greg Clayton24bc5d92011-03-30 18:16:51 +00001941 if (!DecodeProcessInfoResponse (response, process_info))
1942 break;
1943 process_infos.Append(process_info);
1944 response.GetStringRef().clear();
1945 response.SetFilePos(0);
1946 } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false));
1947 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001948 else
1949 {
1950 m_supports_qfProcessInfo = false;
1951 return 0;
1952 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001953 }
1954 return process_infos.GetSize();
1955
1956}
1957
1958bool
1959GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name)
1960{
1961 if (m_supports_qUserName)
1962 {
1963 char packet[32];
1964 const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid);
1965 assert (packet_len < sizeof(packet));
1966 StringExtractorGDBRemote response;
1967 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1968 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001969 if (response.IsNormalResponse())
1970 {
1971 // Make sure we parsed the right number of characters. The response is
1972 // the hex encoded user name and should make up the entire packet.
1973 // If there are any non-hex ASCII bytes, the length won't match below..
1974 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
1975 return true;
1976 }
1977 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00001978 else
1979 {
1980 m_supports_qUserName = false;
1981 return false;
1982 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00001983 }
1984 return false;
1985
1986}
1987
1988bool
1989GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name)
1990{
1991 if (m_supports_qGroupName)
1992 {
1993 char packet[32];
1994 const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid);
1995 assert (packet_len < sizeof(packet));
1996 StringExtractorGDBRemote response;
1997 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1998 {
Greg Clayton24bc5d92011-03-30 18:16:51 +00001999 if (response.IsNormalResponse())
2000 {
2001 // Make sure we parsed the right number of characters. The response is
2002 // the hex encoded group name and should make up the entire packet.
2003 // If there are any non-hex ASCII bytes, the length won't match below..
2004 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
2005 return true;
2006 }
2007 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00002008 else
2009 {
2010 m_supports_qGroupName = false;
2011 return false;
2012 }
Greg Clayton24bc5d92011-03-30 18:16:51 +00002013 }
2014 return false;
Greg Clayton06d7cc82011-04-04 18:18:57 +00002015}
Greg Clayton24bc5d92011-03-30 18:16:51 +00002016
Greg Clayton06d7cc82011-04-04 18:18:57 +00002017void
2018GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets)
2019{
2020 uint32_t i;
2021 TimeValue start_time, end_time;
2022 uint64_t total_time_nsec;
2023 float packets_per_second;
2024 if (SendSpeedTestPacket (0, 0))
2025 {
2026 for (uint32_t send_size = 0; send_size <= 1024; send_size *= 2)
2027 {
2028 for (uint32_t recv_size = 0; recv_size <= 1024; recv_size *= 2)
2029 {
2030 start_time = TimeValue::Now();
2031 for (i=0; i<num_packets; ++i)
2032 {
2033 SendSpeedTestPacket (send_size, recv_size);
2034 }
2035 end_time = TimeValue::Now();
2036 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00002037 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002038 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 +00002039 num_packets,
2040 send_size,
2041 recv_size,
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00002042 total_time_nsec / TimeValue::NanoSecPerSec,
2043 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton06d7cc82011-04-04 18:18:57 +00002044 packets_per_second);
2045 if (recv_size == 0)
2046 recv_size = 32;
2047 }
2048 if (send_size == 0)
2049 send_size = 32;
2050 }
2051 }
2052 else
2053 {
2054 start_time = TimeValue::Now();
2055 for (i=0; i<num_packets; ++i)
2056 {
2057 GetCurrentProcessID ();
2058 }
2059 end_time = TimeValue::Now();
2060 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00002061 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002062 printf ("%u 'qC' packets packets in 0x%" PRIu64 "%9.9" PRIu64 " sec for %f packets/sec.\n",
Greg Clayton06d7cc82011-04-04 18:18:57 +00002063 num_packets,
Peter Collingbourne20fe30c2011-06-18 23:52:14 +00002064 total_time_nsec / TimeValue::NanoSecPerSec,
2065 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton06d7cc82011-04-04 18:18:57 +00002066 packets_per_second);
2067 }
2068}
2069
2070bool
2071GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size)
2072{
2073 StreamString packet;
2074 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size);
2075 uint32_t bytes_left = send_size;
2076 while (bytes_left > 0)
2077 {
2078 if (bytes_left >= 26)
2079 {
2080 packet.PutCString("abcdefghijklmnopqrstuvwxyz");
2081 bytes_left -= 26;
2082 }
2083 else
2084 {
2085 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz");
2086 bytes_left = 0;
2087 }
2088 }
2089
2090 StringExtractorGDBRemote response;
Greg Clayton5b53e8e2011-05-15 23:46:54 +00002091 return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) > 0;
Greg Clayton06d7cc82011-04-04 18:18:57 +00002092 return false;
Greg Clayton24bc5d92011-03-30 18:16:51 +00002093}
Greg Claytonb72d0f02011-04-12 05:54:46 +00002094
2095uint16_t
2096GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort ()
2097{
2098 StringExtractorGDBRemote response;
2099 if (SendPacketAndWaitForResponse("qLaunchGDBServer", strlen("qLaunchGDBServer"), response, false))
2100 {
2101 std::string name;
2102 std::string value;
2103 uint16_t port = 0;
Greg Clayton4a379b12012-07-17 03:23:13 +00002104 //lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
Greg Claytonb72d0f02011-04-12 05:54:46 +00002105 while (response.GetNameColonValue(name, value))
2106 {
2107 if (name.size() == 4 && name.compare("port") == 0)
2108 port = Args::StringToUInt32(value.c_str(), 0, 0);
Greg Clayton4a379b12012-07-17 03:23:13 +00002109// if (name.size() == 3 && name.compare("pid") == 0)
2110// pid = Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0);
Greg Claytonb72d0f02011-04-12 05:54:46 +00002111 }
2112 return port;
2113 }
2114 return 0;
2115}
2116
2117bool
Jason Molenda4a5dfcf2013-02-23 02:04:45 +00002118GDBRemoteCommunicationClient::SetCurrentThread (uint64_t tid)
Greg Claytonb72d0f02011-04-12 05:54:46 +00002119{
2120 if (m_curr_tid == tid)
2121 return true;
Jason Molenda4a5dfcf2013-02-23 02:04:45 +00002122
Greg Claytonb72d0f02011-04-12 05:54:46 +00002123 char packet[32];
2124 int packet_len;
Jason Molenda4a5dfcf2013-02-23 02:04:45 +00002125 if (tid == UINT64_MAX)
2126 packet_len = ::snprintf (packet, sizeof(packet), "Hg-1");
Greg Claytonb72d0f02011-04-12 05:54:46 +00002127 else
Jason Molenda4a5dfcf2013-02-23 02:04:45 +00002128 packet_len = ::snprintf (packet, sizeof(packet), "Hg%" PRIx64, tid);
Greg Claytonb72d0f02011-04-12 05:54:46 +00002129 assert (packet_len + 1 < sizeof(packet));
2130 StringExtractorGDBRemote response;
2131 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2132 {
2133 if (response.IsOKResponse())
2134 {
2135 m_curr_tid = tid;
2136 return true;
2137 }
2138 }
2139 return false;
2140}
2141
2142bool
Jason Molenda4a5dfcf2013-02-23 02:04:45 +00002143GDBRemoteCommunicationClient::SetCurrentThreadForRun (uint64_t tid)
Greg Claytonb72d0f02011-04-12 05:54:46 +00002144{
2145 if (m_curr_tid_run == tid)
2146 return true;
Jason Molenda4a5dfcf2013-02-23 02:04:45 +00002147
Greg Claytonb72d0f02011-04-12 05:54:46 +00002148 char packet[32];
2149 int packet_len;
Jason Molenda4a5dfcf2013-02-23 02:04:45 +00002150 if (tid == UINT64_MAX)
2151 packet_len = ::snprintf (packet, sizeof(packet), "Hc-1");
Greg Claytonb72d0f02011-04-12 05:54:46 +00002152 else
Jason Molenda4a5dfcf2013-02-23 02:04:45 +00002153 packet_len = ::snprintf (packet, sizeof(packet), "Hc%" PRIx64, tid);
2154
Greg Claytonb72d0f02011-04-12 05:54:46 +00002155 assert (packet_len + 1 < sizeof(packet));
2156 StringExtractorGDBRemote response;
2157 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2158 {
2159 if (response.IsOKResponse())
2160 {
2161 m_curr_tid_run = tid;
2162 return true;
2163 }
2164 }
2165 return false;
2166}
2167
2168bool
2169GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response)
2170{
2171 if (SendPacketAndWaitForResponse("?", 1, response, false))
2172 return response.IsNormalResponse();
2173 return false;
2174}
2175
2176bool
Greg Clayton37d3fce2012-10-13 02:11:55 +00002177GDBRemoteCommunicationClient::GetThreadStopInfo (lldb::tid_t tid, StringExtractorGDBRemote &response)
Greg Claytonb72d0f02011-04-12 05:54:46 +00002178{
2179 if (m_supports_qThreadStopInfo)
2180 {
2181 char packet[256];
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002182 int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%" PRIx64, tid);
Greg Claytonb72d0f02011-04-12 05:54:46 +00002183 assert (packet_len < sizeof(packet));
2184 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2185 {
Greg Clayton5b53e8e2011-05-15 23:46:54 +00002186 if (response.IsNormalResponse())
Greg Claytonb72d0f02011-04-12 05:54:46 +00002187 return true;
2188 else
2189 return false;
2190 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00002191 else
2192 {
2193 m_supports_qThreadStopInfo = false;
2194 }
Greg Claytonb72d0f02011-04-12 05:54:46 +00002195 }
Greg Clayton139da722011-05-20 03:15:54 +00002196// if (SetCurrentThread (tid))
2197// return GetStopReply (response);
Greg Claytonb72d0f02011-04-12 05:54:46 +00002198 return false;
2199}
2200
2201
2202uint8_t
2203GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length)
2204{
2205 switch (type)
2206 {
2207 case eBreakpointSoftware: if (!m_supports_z0) return UINT8_MAX; break;
2208 case eBreakpointHardware: if (!m_supports_z1) return UINT8_MAX; break;
2209 case eWatchpointWrite: if (!m_supports_z2) return UINT8_MAX; break;
2210 case eWatchpointRead: if (!m_supports_z3) return UINT8_MAX; break;
2211 case eWatchpointReadWrite: if (!m_supports_z4) return UINT8_MAX; break;
Greg Claytonb72d0f02011-04-12 05:54:46 +00002212 }
2213
2214 char packet[64];
2215 const int packet_len = ::snprintf (packet,
2216 sizeof(packet),
Daniel Malea5f35a4b2012-11-29 21:49:15 +00002217 "%c%i,%" PRIx64 ",%x",
Greg Claytonb72d0f02011-04-12 05:54:46 +00002218 insert ? 'Z' : 'z',
2219 type,
2220 addr,
2221 length);
2222
2223 assert (packet_len + 1 < sizeof(packet));
2224 StringExtractorGDBRemote response;
2225 if (SendPacketAndWaitForResponse(packet, packet_len, response, true))
2226 {
2227 if (response.IsOKResponse())
2228 return 0;
Greg Claytonb72d0f02011-04-12 05:54:46 +00002229 else if (response.IsErrorResponse())
2230 return response.GetError();
2231 }
Greg Clayton5b53e8e2011-05-15 23:46:54 +00002232 else
2233 {
2234 switch (type)
2235 {
2236 case eBreakpointSoftware: m_supports_z0 = false; break;
2237 case eBreakpointHardware: m_supports_z1 = false; break;
2238 case eWatchpointWrite: m_supports_z2 = false; break;
2239 case eWatchpointRead: m_supports_z3 = false; break;
2240 case eWatchpointReadWrite: m_supports_z4 = false; break;
Greg Clayton5b53e8e2011-05-15 23:46:54 +00002241 }
2242 }
2243
Greg Claytonb72d0f02011-04-12 05:54:46 +00002244 return UINT8_MAX;
2245}
Greg Clayton4a60f9e2011-05-20 23:38:13 +00002246
2247size_t
2248GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
2249 bool &sequence_mutex_unavailable)
2250{
2251 Mutex::Locker locker;
2252 thread_ids.clear();
2253
Jim Ingham088684d2012-06-08 22:50:40 +00002254 if (GetSequenceMutex (locker, "ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex"))
Greg Clayton4a60f9e2011-05-20 23:38:13 +00002255 {
2256 sequence_mutex_unavailable = false;
2257 StringExtractorGDBRemote response;
2258
Greg Clayton63afdb02011-06-17 01:22:15 +00002259 for (SendPacketNoLock ("qfThreadInfo", strlen("qfThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
Greg Clayton4a60f9e2011-05-20 23:38:13 +00002260 response.IsNormalResponse();
Greg Clayton63afdb02011-06-17 01:22:15 +00002261 SendPacketNoLock ("qsThreadInfo", strlen("qsThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()))
Greg Clayton4a60f9e2011-05-20 23:38:13 +00002262 {
2263 char ch = response.GetChar();
2264 if (ch == 'l')
2265 break;
2266 if (ch == 'm')
2267 {
2268 do
2269 {
Jason Molenda4a5dfcf2013-02-23 02:04:45 +00002270 tid_t tid = response.GetHexMaxU64(false, LLDB_INVALID_THREAD_ID);
Greg Clayton4a60f9e2011-05-20 23:38:13 +00002271
2272 if (tid != LLDB_INVALID_THREAD_ID)
2273 {
2274 thread_ids.push_back (tid);
2275 }
2276 ch = response.GetChar(); // Skip the command separator
2277 } while (ch == ','); // Make sure we got a comma separator
2278 }
2279 }
2280 }
2281 else
2282 {
Jim Ingham088684d2012-06-08 22:50:40 +00002283#if defined (LLDB_CONFIGURATION_DEBUG)
2284 // assert(!"ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex");
2285#else
Greg Clayton952e9dc2013-03-27 23:08:40 +00002286 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Claytonc8dd5702012-04-12 19:04:34 +00002287 if (log)
2288 log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'");
Jim Ingham088684d2012-06-08 22:50:40 +00002289#endif
Greg Clayton4a60f9e2011-05-20 23:38:13 +00002290 sequence_mutex_unavailable = true;
2291 }
2292 return thread_ids.size();
2293}
Greg Clayton516f0842012-04-11 00:24:49 +00002294
2295lldb::addr_t
2296GDBRemoteCommunicationClient::GetShlibInfoAddr()
2297{
2298 if (!IsRunning())
2299 {
2300 StringExtractorGDBRemote response;
2301 if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false))
2302 {
2303 if (response.IsNormalResponse())
2304 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
2305 }
2306 }
2307 return LLDB_INVALID_ADDRESS;
2308}
2309