blob: 6a6e063e35b0a1f84869e24dbf324faf3f533791 [file] [log] [blame]
Greg Clayton576d8832011-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 Ong4b6459f2013-01-18 23:11:53 +000015#include <sstream>
16
Greg Clayton576d8832011-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"
Virgile Bellob2f1fb22013-08-23 12:44:05 +000032#include "lldb/Host/Config.h"
Greg Clayton576d8832011-03-22 04:00:09 +000033
34using namespace lldb;
35using namespace lldb_private;
36
Virgile Bellob2f1fb22013-08-23 12:44:05 +000037#ifdef LLDB_DISABLE_POSIX
38#define SIGSTOP 17
39#endif
40
Greg Clayton576d8832011-03-22 04:00:09 +000041//----------------------------------------------------------------------
42// GDBRemoteCommunicationClient constructor
43//----------------------------------------------------------------------
Greg Clayton8b82f082011-04-12 05:54:46 +000044GDBRemoteCommunicationClient::GDBRemoteCommunicationClient(bool is_platform) :
45 GDBRemoteCommunication("gdb-remote.client", "gdb-remote.client.rx_packet", is_platform),
Greg Clayton576d8832011-03-22 04:00:09 +000046 m_supports_not_sending_acks (eLazyBoolCalculate),
47 m_supports_thread_suffix (eLazyBoolCalculate),
Greg Clayton44633992012-04-10 03:22:03 +000048 m_supports_threads_in_stop_reply (eLazyBoolCalculate),
Greg Clayton576d8832011-03-22 04:00:09 +000049 m_supports_vCont_all (eLazyBoolCalculate),
50 m_supports_vCont_any (eLazyBoolCalculate),
51 m_supports_vCont_c (eLazyBoolCalculate),
52 m_supports_vCont_C (eLazyBoolCalculate),
53 m_supports_vCont_s (eLazyBoolCalculate),
54 m_supports_vCont_S (eLazyBoolCalculate),
Greg Clayton32e0a752011-03-30 18:16:51 +000055 m_qHostInfo_is_valid (eLazyBoolCalculate),
Jason Molendaf17b5ac2012-12-19 02:54:03 +000056 m_qProcessInfo_is_valid (eLazyBoolCalculate),
Greg Clayton70b57652011-05-15 01:25:55 +000057 m_supports_alloc_dealloc_memory (eLazyBoolCalculate),
Greg Clayton46fb5582011-11-18 07:03:08 +000058 m_supports_memory_region_info (eLazyBoolCalculate),
Johnny Chen64637202012-05-23 21:09:52 +000059 m_supports_watchpoint_support_info (eLazyBoolCalculate),
Jim Inghamacff8952013-05-02 00:27:30 +000060 m_supports_detach_stay_stopped (eLazyBoolCalculate),
Enrico Granataf04a2192012-07-13 23:18:48 +000061 m_watchpoints_trigger_after_instruction(eLazyBoolCalculate),
Jim Inghamcd16df92012-07-20 21:37:13 +000062 m_attach_or_wait_reply(eLazyBoolCalculate),
Jim Ingham279ceec2012-07-25 21:12:43 +000063 m_prepare_for_reg_writing_reply (eLazyBoolCalculate),
Greg Clayton32e0a752011-03-30 18:16:51 +000064 m_supports_qProcessInfoPID (true),
65 m_supports_qfProcessInfo (true),
66 m_supports_qUserName (true),
67 m_supports_qGroupName (true),
Greg Clayton8b82f082011-04-12 05:54:46 +000068 m_supports_qThreadStopInfo (true),
69 m_supports_z0 (true),
70 m_supports_z1 (true),
71 m_supports_z2 (true),
72 m_supports_z3 (true),
73 m_supports_z4 (true),
74 m_curr_tid (LLDB_INVALID_THREAD_ID),
75 m_curr_tid_run (LLDB_INVALID_THREAD_ID),
Johnny Chen64637202012-05-23 21:09:52 +000076 m_num_supported_hardware_watchpoints (0),
Greg Clayton576d8832011-03-22 04:00:09 +000077 m_async_mutex (Mutex::eMutexTypeRecursive),
78 m_async_packet_predicate (false),
79 m_async_packet (),
80 m_async_response (),
81 m_async_signal (-1),
Han Ming Ong4b6459f2013-01-18 23:11:53 +000082 m_thread_id_to_used_usec_map (),
Greg Clayton1cb64962011-03-24 04:28:38 +000083 m_host_arch(),
Jason Molendaf17b5ac2012-12-19 02:54:03 +000084 m_process_arch(),
Greg Clayton1cb64962011-03-24 04:28:38 +000085 m_os_version_major (UINT32_MAX),
86 m_os_version_minor (UINT32_MAX),
87 m_os_version_update (UINT32_MAX)
Greg Clayton576d8832011-03-22 04:00:09 +000088{
Greg Clayton576d8832011-03-22 04:00:09 +000089}
90
91//----------------------------------------------------------------------
92// Destructor
93//----------------------------------------------------------------------
94GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient()
95{
Greg Clayton576d8832011-03-22 04:00:09 +000096 if (IsConnected())
Greg Clayton576d8832011-03-22 04:00:09 +000097 Disconnect();
Greg Clayton576d8832011-03-22 04:00:09 +000098}
99
100bool
Greg Clayton1cb64962011-03-24 04:28:38 +0000101GDBRemoteCommunicationClient::HandshakeWithServer (Error *error_ptr)
102{
103 // Start the read thread after we send the handshake ack since if we
104 // fail to send the handshake ack, there is no reason to continue...
105 if (SendAck())
Greg Clayton73bf5db2011-06-17 01:22:15 +0000106 return true;
Greg Clayton1cb64962011-03-24 04:28:38 +0000107
108 if (error_ptr)
109 error_ptr->SetErrorString("failed to send the handshake ack");
110 return false;
111}
112
113void
114GDBRemoteCommunicationClient::QueryNoAckModeSupported ()
Greg Clayton576d8832011-03-22 04:00:09 +0000115{
116 if (m_supports_not_sending_acks == eLazyBoolCalculate)
117 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000118 m_send_acks = true;
Greg Clayton576d8832011-03-22 04:00:09 +0000119 m_supports_not_sending_acks = eLazyBoolNo;
Greg Clayton1cb64962011-03-24 04:28:38 +0000120
121 StringExtractorGDBRemote response;
Greg Clayton576d8832011-03-22 04:00:09 +0000122 if (SendPacketAndWaitForResponse("QStartNoAckMode", response, false))
123 {
124 if (response.IsOKResponse())
Greg Clayton1cb64962011-03-24 04:28:38 +0000125 {
126 m_send_acks = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000127 m_supports_not_sending_acks = eLazyBoolYes;
Greg Clayton1cb64962011-03-24 04:28:38 +0000128 }
Greg Clayton576d8832011-03-22 04:00:09 +0000129 }
130 }
Greg Clayton576d8832011-03-22 04:00:09 +0000131}
132
133void
Greg Clayton44633992012-04-10 03:22:03 +0000134GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported ()
135{
136 if (m_supports_threads_in_stop_reply == eLazyBoolCalculate)
137 {
138 m_supports_threads_in_stop_reply = eLazyBoolNo;
139
140 StringExtractorGDBRemote response;
141 if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response, false))
142 {
143 if (response.IsOKResponse())
144 m_supports_threads_in_stop_reply = eLazyBoolYes;
145 }
146 }
147}
148
Jim Inghamcd16df92012-07-20 21:37:13 +0000149bool
150GDBRemoteCommunicationClient::GetVAttachOrWaitSupported ()
151{
152 if (m_attach_or_wait_reply == eLazyBoolCalculate)
153 {
154 m_attach_or_wait_reply = eLazyBoolNo;
155
156 StringExtractorGDBRemote response;
157 if (SendPacketAndWaitForResponse("qVAttachOrWaitSupported", response, false))
158 {
159 if (response.IsOKResponse())
160 m_attach_or_wait_reply = eLazyBoolYes;
161 }
162 }
163 if (m_attach_or_wait_reply == eLazyBoolYes)
164 return true;
165 else
166 return false;
167}
168
Jim Ingham279ceec2012-07-25 21:12:43 +0000169bool
170GDBRemoteCommunicationClient::GetSyncThreadStateSupported ()
171{
172 if (m_prepare_for_reg_writing_reply == eLazyBoolCalculate)
173 {
174 m_prepare_for_reg_writing_reply = eLazyBoolNo;
175
176 StringExtractorGDBRemote response;
177 if (SendPacketAndWaitForResponse("qSyncThreadStateSupported", response, false))
178 {
179 if (response.IsOKResponse())
180 m_prepare_for_reg_writing_reply = eLazyBoolYes;
181 }
182 }
183 if (m_prepare_for_reg_writing_reply == eLazyBoolYes)
184 return true;
185 else
186 return false;
187}
188
Greg Clayton44633992012-04-10 03:22:03 +0000189
190void
Greg Clayton576d8832011-03-22 04:00:09 +0000191GDBRemoteCommunicationClient::ResetDiscoverableSettings()
192{
193 m_supports_not_sending_acks = eLazyBoolCalculate;
194 m_supports_thread_suffix = eLazyBoolCalculate;
Greg Clayton44633992012-04-10 03:22:03 +0000195 m_supports_threads_in_stop_reply = eLazyBoolCalculate;
Greg Clayton576d8832011-03-22 04:00:09 +0000196 m_supports_vCont_c = eLazyBoolCalculate;
197 m_supports_vCont_C = eLazyBoolCalculate;
198 m_supports_vCont_s = eLazyBoolCalculate;
199 m_supports_vCont_S = eLazyBoolCalculate;
Greg Clayton32e0a752011-03-30 18:16:51 +0000200 m_qHostInfo_is_valid = eLazyBoolCalculate;
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000201 m_qProcessInfo_is_valid = eLazyBoolCalculate;
Greg Clayton70b57652011-05-15 01:25:55 +0000202 m_supports_alloc_dealloc_memory = eLazyBoolCalculate;
Greg Clayton46fb5582011-11-18 07:03:08 +0000203 m_supports_memory_region_info = eLazyBoolCalculate;
Jim Ingham279ceec2012-07-25 21:12:43 +0000204 m_prepare_for_reg_writing_reply = eLazyBoolCalculate;
205 m_attach_or_wait_reply = eLazyBoolCalculate;
Greg Clayton2a48f522011-05-14 01:50:35 +0000206
Greg Clayton32e0a752011-03-30 18:16:51 +0000207 m_supports_qProcessInfoPID = true;
208 m_supports_qfProcessInfo = true;
209 m_supports_qUserName = true;
210 m_supports_qGroupName = true;
Greg Clayton8b82f082011-04-12 05:54:46 +0000211 m_supports_qThreadStopInfo = true;
212 m_supports_z0 = true;
213 m_supports_z1 = true;
214 m_supports_z2 = true;
215 m_supports_z3 = true;
216 m_supports_z4 = true;
Greg Claytond314e812011-03-23 00:09:55 +0000217 m_host_arch.Clear();
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000218 m_process_arch.Clear();
Greg Clayton576d8832011-03-22 04:00:09 +0000219}
220
221
222bool
223GDBRemoteCommunicationClient::GetThreadSuffixSupported ()
224{
225 if (m_supports_thread_suffix == eLazyBoolCalculate)
226 {
227 StringExtractorGDBRemote response;
228 m_supports_thread_suffix = eLazyBoolNo;
229 if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response, false))
230 {
231 if (response.IsOKResponse())
232 m_supports_thread_suffix = eLazyBoolYes;
233 }
234 }
235 return m_supports_thread_suffix;
236}
237bool
238GDBRemoteCommunicationClient::GetVContSupported (char flavor)
239{
240 if (m_supports_vCont_c == eLazyBoolCalculate)
241 {
242 StringExtractorGDBRemote response;
243 m_supports_vCont_any = eLazyBoolNo;
244 m_supports_vCont_all = eLazyBoolNo;
245 m_supports_vCont_c = eLazyBoolNo;
246 m_supports_vCont_C = eLazyBoolNo;
247 m_supports_vCont_s = eLazyBoolNo;
248 m_supports_vCont_S = eLazyBoolNo;
249 if (SendPacketAndWaitForResponse("vCont?", response, false))
250 {
251 const char *response_cstr = response.GetStringRef().c_str();
252 if (::strstr (response_cstr, ";c"))
253 m_supports_vCont_c = eLazyBoolYes;
254
255 if (::strstr (response_cstr, ";C"))
256 m_supports_vCont_C = eLazyBoolYes;
257
258 if (::strstr (response_cstr, ";s"))
259 m_supports_vCont_s = eLazyBoolYes;
260
261 if (::strstr (response_cstr, ";S"))
262 m_supports_vCont_S = eLazyBoolYes;
263
264 if (m_supports_vCont_c == eLazyBoolYes &&
265 m_supports_vCont_C == eLazyBoolYes &&
266 m_supports_vCont_s == eLazyBoolYes &&
267 m_supports_vCont_S == eLazyBoolYes)
268 {
269 m_supports_vCont_all = eLazyBoolYes;
270 }
271
272 if (m_supports_vCont_c == eLazyBoolYes ||
273 m_supports_vCont_C == eLazyBoolYes ||
274 m_supports_vCont_s == eLazyBoolYes ||
275 m_supports_vCont_S == eLazyBoolYes)
276 {
277 m_supports_vCont_any = eLazyBoolYes;
278 }
279 }
280 }
281
282 switch (flavor)
283 {
284 case 'a': return m_supports_vCont_any;
285 case 'A': return m_supports_vCont_all;
286 case 'c': return m_supports_vCont_c;
287 case 'C': return m_supports_vCont_C;
288 case 's': return m_supports_vCont_s;
289 case 'S': return m_supports_vCont_S;
290 default: break;
291 }
292 return false;
293}
294
295
296size_t
297GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
298(
299 const char *payload,
300 StringExtractorGDBRemote &response,
301 bool send_async
302)
303{
304 return SendPacketAndWaitForResponse (payload,
305 ::strlen (payload),
306 response,
307 send_async);
308}
309
310size_t
311GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
312(
313 const char *payload,
314 size_t payload_length,
315 StringExtractorGDBRemote &response,
316 bool send_async
317)
318{
319 Mutex::Locker locker;
Greg Clayton5160ce52013-03-27 23:08:40 +0000320 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton644247c2011-07-07 01:59:51 +0000321 size_t response_len = 0;
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000322 if (GetSequenceMutex (locker))
Greg Clayton576d8832011-03-22 04:00:09 +0000323 {
Greg Clayton5fe15d22011-05-20 03:15:54 +0000324 if (SendPacketNoLock (payload, payload_length))
Greg Clayton644247c2011-07-07 01:59:51 +0000325 response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
326 else
327 {
328 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000329 log->Printf("error: failed to send '%*s'", (int) payload_length, payload);
Greg Clayton644247c2011-07-07 01:59:51 +0000330 }
Greg Clayton576d8832011-03-22 04:00:09 +0000331 }
332 else
333 {
334 if (send_async)
335 {
Greg Claytond3544052012-05-31 21:24:20 +0000336 if (IsRunning())
Greg Clayton576d8832011-03-22 04:00:09 +0000337 {
Greg Claytond3544052012-05-31 21:24:20 +0000338 Mutex::Locker async_locker (m_async_mutex);
339 m_async_packet.assign(payload, payload_length);
340 m_async_packet_predicate.SetValue (true, eBroadcastNever);
341
342 if (log)
343 log->Printf ("async: async packet = %s", m_async_packet.c_str());
344
345 bool timed_out = false;
346 if (SendInterrupt(locker, 2, timed_out))
Greg Clayton576d8832011-03-22 04:00:09 +0000347 {
Greg Claytond3544052012-05-31 21:24:20 +0000348 if (m_interrupt_sent)
Greg Clayton576d8832011-03-22 04:00:09 +0000349 {
Jim Inghambabfc382012-06-06 00:32:39 +0000350 m_interrupt_sent = false;
Greg Claytond3544052012-05-31 21:24:20 +0000351 TimeValue timeout_time;
352 timeout_time = TimeValue::Now();
353 timeout_time.OffsetWithSeconds (m_packet_timeout);
354
Greg Clayton576d8832011-03-22 04:00:09 +0000355 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000356 log->Printf ("async: sent interrupt");
Greg Clayton644247c2011-07-07 01:59:51 +0000357
Greg Claytond3544052012-05-31 21:24:20 +0000358 if (m_async_packet_predicate.WaitForValueEqualTo (false, &timeout_time, &timed_out))
Greg Claytone889ad62011-10-27 22:04:16 +0000359 {
Greg Claytond3544052012-05-31 21:24:20 +0000360 if (log)
361 log->Printf ("async: got response");
362
363 // Swap the response buffer to avoid malloc and string copy
364 response.GetStringRef().swap (m_async_response.GetStringRef());
365 response_len = response.GetStringRef().size();
366 }
367 else
368 {
369 if (log)
370 log->Printf ("async: timed out waiting for response");
371 }
372
373 // Make sure we wait until the continue packet has been sent again...
374 if (m_private_is_running.WaitForValueEqualTo (true, &timeout_time, &timed_out))
375 {
376 if (log)
377 {
378 if (timed_out)
379 log->Printf ("async: timed out waiting for process to resume, but process was resumed");
380 else
381 log->Printf ("async: async packet sent");
382 }
383 }
384 else
385 {
386 if (log)
387 log->Printf ("async: timed out waiting for process to resume");
Greg Claytone889ad62011-10-27 22:04:16 +0000388 }
389 }
390 else
391 {
Greg Claytond3544052012-05-31 21:24:20 +0000392 // We had a racy condition where we went to send the interrupt
393 // yet we were able to get the lock, so the process must have
394 // just stopped?
Greg Clayton576d8832011-03-22 04:00:09 +0000395 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000396 log->Printf ("async: got lock without sending interrupt");
397 // Send the packet normally since we got the lock
398 if (SendPacketNoLock (payload, payload_length))
399 response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
400 else
401 {
402 if (log)
403 log->Printf("error: failed to send '%*s'", (int) payload_length, payload);
404 }
Greg Clayton576d8832011-03-22 04:00:09 +0000405 }
406 }
407 else
408 {
Greg Clayton644247c2011-07-07 01:59:51 +0000409 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000410 log->Printf ("async: failed to interrupt");
Greg Clayton576d8832011-03-22 04:00:09 +0000411 }
412 }
413 else
414 {
415 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000416 log->Printf ("async: not running, async is ignored");
Greg Clayton576d8832011-03-22 04:00:09 +0000417 }
418 }
419 else
420 {
421 if (log)
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000422 log->Printf("error: failed to get packet sequence mutex, not sending packet '%*s'", (int) payload_length, payload);
Greg Clayton576d8832011-03-22 04:00:09 +0000423 }
424 }
Greg Clayton644247c2011-07-07 01:59:51 +0000425 if (response_len == 0)
426 {
427 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000428 log->Printf("error: failed to get response for '%*s'", (int) payload_length, payload);
Greg Clayton644247c2011-07-07 01:59:51 +0000429 }
430 return response_len;
Greg Clayton576d8832011-03-22 04:00:09 +0000431}
432
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000433static const char *end_delimiter = "--end--;";
434static const int end_delimiter_len = 8;
435
436std::string
437GDBRemoteCommunicationClient::HarmonizeThreadIdsForProfileData
438( ProcessGDBRemote *process,
439 StringExtractorGDBRemote& profileDataExtractor
440)
441{
442 std::map<uint64_t, uint32_t> new_thread_id_to_used_usec_map;
443 std::stringstream final_output;
444 std::string name, value;
445
446 // Going to assuming thread_used_usec comes first, else bail out.
447 while (profileDataExtractor.GetNameColonValue(name, value))
448 {
449 if (name.compare("thread_used_id") == 0)
450 {
451 StringExtractor threadIDHexExtractor(value.c_str());
452 uint64_t thread_id = threadIDHexExtractor.GetHexMaxU64(false, 0);
453
454 bool has_used_usec = false;
455 uint32_t curr_used_usec = 0;
456 std::string usec_name, usec_value;
457 uint32_t input_file_pos = profileDataExtractor.GetFilePos();
458 if (profileDataExtractor.GetNameColonValue(usec_name, usec_value))
459 {
460 if (usec_name.compare("thread_used_usec") == 0)
461 {
462 has_used_usec = true;
463 curr_used_usec = strtoull(usec_value.c_str(), NULL, 0);
464 }
465 else
466 {
467 // We didn't find what we want, it is probably
468 // an older version. Bail out.
469 profileDataExtractor.SetFilePos(input_file_pos);
470 }
471 }
472
473 if (has_used_usec)
474 {
475 uint32_t prev_used_usec = 0;
476 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_used_usec_map.find(thread_id);
477 if (iterator != m_thread_id_to_used_usec_map.end())
478 {
479 prev_used_usec = m_thread_id_to_used_usec_map[thread_id];
480 }
481
482 uint32_t real_used_usec = curr_used_usec - prev_used_usec;
483 // A good first time record is one that runs for at least 0.25 sec
484 bool good_first_time = (prev_used_usec == 0) && (real_used_usec > 250000);
485 bool good_subsequent_time = (prev_used_usec > 0) &&
486 ((real_used_usec > 0) || (process->HasAssignedIndexIDToThread(thread_id)));
487
488 if (good_first_time || good_subsequent_time)
489 {
490 // We try to avoid doing too many index id reservation,
491 // resulting in fast increase of index ids.
492
493 final_output << name << ":";
494 int32_t index_id = process->AssignIndexIDToThread(thread_id);
495 final_output << index_id << ";";
496
497 final_output << usec_name << ":" << usec_value << ";";
498 }
499 else
500 {
501 // Skip past 'thread_used_name'.
502 std::string local_name, local_value;
503 profileDataExtractor.GetNameColonValue(local_name, local_value);
504 }
505
506 // Store current time as previous time so that they can be compared later.
507 new_thread_id_to_used_usec_map[thread_id] = curr_used_usec;
508 }
509 else
510 {
511 // Bail out and use old string.
512 final_output << name << ":" << value << ";";
513 }
514 }
515 else
516 {
517 final_output << name << ":" << value << ";";
518 }
519 }
520 final_output << end_delimiter;
521 m_thread_id_to_used_usec_map = new_thread_id_to_used_usec_map;
522
523 return final_output.str();
524}
525
Greg Clayton576d8832011-03-22 04:00:09 +0000526StateType
527GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse
528(
529 ProcessGDBRemote *process,
530 const char *payload,
531 size_t packet_length,
532 StringExtractorGDBRemote &response
533)
534{
Greg Clayton1f5181a2012-07-02 22:05:25 +0000535 m_curr_tid = LLDB_INVALID_THREAD_ID;
Greg Clayton5160ce52013-03-27 23:08:40 +0000536 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton576d8832011-03-22 04:00:09 +0000537 if (log)
538 log->Printf ("GDBRemoteCommunicationClient::%s ()", __FUNCTION__);
539
540 Mutex::Locker locker(m_sequence_mutex);
541 StateType state = eStateRunning;
542
543 BroadcastEvent(eBroadcastBitRunPacketSent, NULL);
544 m_public_is_running.SetValue (true, eBroadcastNever);
545 // Set the starting continue packet into "continue_packet". This packet
Jim Inghambabfc382012-06-06 00:32:39 +0000546 // may change if we are interrupted and we continue after an async packet...
Greg Clayton576d8832011-03-22 04:00:09 +0000547 std::string continue_packet(payload, packet_length);
548
Greg Clayton3f875c52013-02-22 22:23:55 +0000549 bool got_async_packet = false;
Greg Claytonaf247d72011-05-19 03:54:16 +0000550
Greg Clayton576d8832011-03-22 04:00:09 +0000551 while (state == eStateRunning)
552 {
Greg Clayton3f875c52013-02-22 22:23:55 +0000553 if (!got_async_packet)
Greg Claytonaf247d72011-05-19 03:54:16 +0000554 {
555 if (log)
556 log->Printf ("GDBRemoteCommunicationClient::%s () sending continue packet: %s", __FUNCTION__, continue_packet.c_str());
Greg Clayton37a0a242012-04-11 00:24:49 +0000557 if (SendPacketNoLock(continue_packet.c_str(), continue_packet.size()) == 0)
Greg Claytonaf247d72011-05-19 03:54:16 +0000558 state = eStateInvalid;
Greg Clayton576d8832011-03-22 04:00:09 +0000559
Greg Claytone889ad62011-10-27 22:04:16 +0000560 m_private_is_running.SetValue (true, eBroadcastAlways);
Greg Claytonaf247d72011-05-19 03:54:16 +0000561 }
562
Greg Clayton3f875c52013-02-22 22:23:55 +0000563 got_async_packet = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000564
565 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000566 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(%s)", __FUNCTION__, continue_packet.c_str());
Greg Clayton576d8832011-03-22 04:00:09 +0000567
Greg Clayton37a0a242012-04-11 00:24:49 +0000568 if (WaitForPacketWithTimeoutMicroSecondsNoLock(response, UINT32_MAX))
Greg Clayton576d8832011-03-22 04:00:09 +0000569 {
570 if (response.Empty())
571 state = eStateInvalid;
572 else
573 {
574 const char stop_type = response.GetChar();
575 if (log)
576 log->Printf ("GDBRemoteCommunicationClient::%s () got packet: %s", __FUNCTION__, response.GetStringRef().c_str());
577 switch (stop_type)
578 {
579 case 'T':
580 case 'S':
Greg Clayton576d8832011-03-22 04:00:09 +0000581 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000582 if (process->GetStopID() == 0)
Greg Clayton576d8832011-03-22 04:00:09 +0000583 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000584 if (process->GetID() == LLDB_INVALID_PROCESS_ID)
585 {
586 lldb::pid_t pid = GetCurrentProcessID ();
587 if (pid != LLDB_INVALID_PROCESS_ID)
588 process->SetID (pid);
589 }
590 process->BuildDynamicRegisterInfo (true);
Greg Clayton576d8832011-03-22 04:00:09 +0000591 }
Greg Clayton2687cd12012-03-29 01:55:41 +0000592
593 // Privately notify any internal threads that we have stopped
594 // in case we wanted to interrupt our process, yet we might
595 // send a packet and continue without returning control to the
596 // user.
597 m_private_is_running.SetValue (false, eBroadcastAlways);
598
599 const uint8_t signo = response.GetHexU8 (UINT8_MAX);
600
Jim Inghambabfc382012-06-06 00:32:39 +0000601 bool continue_after_async = m_async_signal != -1 || m_async_packet_predicate.GetValue();
602 if (continue_after_async || m_interrupt_sent)
Greg Clayton2687cd12012-03-29 01:55:41 +0000603 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000604 // We sent an interrupt packet to stop the inferior process
605 // for an async signal or to send an async packet while running
606 // but we might have been single stepping and received the
607 // stop packet for the step instead of for the interrupt packet.
608 // Typically when an interrupt is sent a SIGINT or SIGSTOP
609 // is used, so if we get anything else, we need to try and
610 // get another stop reply packet that may have been sent
611 // due to sending the interrupt when the target is stopped
612 // which will just re-send a copy of the last stop reply
613 // packet. If we don't do this, then the reply for our
614 // async packet will be the repeat stop reply packet and cause
615 // a lot of trouble for us!
616 if (signo != SIGINT && signo != SIGSTOP)
617 {
Greg Claytonfb72fde2012-05-15 02:50:49 +0000618 continue_after_async = false;
Greg Clayton2687cd12012-03-29 01:55:41 +0000619
620 // We didn't get a a SIGINT or SIGSTOP, so try for a
621 // very brief time (1 ms) to get another stop reply
622 // packet to make sure it doesn't get in the way
623 StringExtractorGDBRemote extra_stop_reply_packet;
624 uint32_t timeout_usec = 1000;
625 if (WaitForPacketWithTimeoutMicroSecondsNoLock (extra_stop_reply_packet, timeout_usec))
626 {
627 switch (extra_stop_reply_packet.GetChar())
628 {
629 case 'T':
630 case 'S':
631 // We did get an extra stop reply, which means
632 // our interrupt didn't stop the target so we
633 // shouldn't continue after the async signal
634 // or packet is sent...
Greg Claytonfb72fde2012-05-15 02:50:49 +0000635 continue_after_async = false;
Greg Clayton2687cd12012-03-29 01:55:41 +0000636 break;
637 }
638 }
639 }
640 }
641
642 if (m_async_signal != -1)
643 {
644 if (log)
645 log->Printf ("async: send signo = %s", Host::GetSignalAsCString (m_async_signal));
646
647 // Save off the async signal we are supposed to send
648 const int async_signal = m_async_signal;
649 // Clear the async signal member so we don't end up
650 // sending the signal multiple times...
651 m_async_signal = -1;
652 // Check which signal we stopped with
653 if (signo == async_signal)
654 {
655 if (log)
656 log->Printf ("async: stopped with signal %s, we are done running", Host::GetSignalAsCString (signo));
657
658 // We already stopped with a signal that we wanted
659 // to stop with, so we are done
660 }
661 else
662 {
663 // We stopped with a different signal that the one
664 // we wanted to stop with, so now we must resume
665 // with the signal we want
666 char signal_packet[32];
667 int signal_packet_len = 0;
668 signal_packet_len = ::snprintf (signal_packet,
669 sizeof (signal_packet),
670 "C%2.2x",
671 async_signal);
672
673 if (log)
674 log->Printf ("async: stopped with signal %s, resume with %s",
675 Host::GetSignalAsCString (signo),
676 Host::GetSignalAsCString (async_signal));
677
678 // Set the continue packet to resume even if the
Greg Claytonfb72fde2012-05-15 02:50:49 +0000679 // interrupt didn't cause our stop (ignore continue_after_async)
Greg Clayton2687cd12012-03-29 01:55:41 +0000680 continue_packet.assign(signal_packet, signal_packet_len);
681 continue;
682 }
683 }
684 else if (m_async_packet_predicate.GetValue())
685 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000686 Log * packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Greg Clayton2687cd12012-03-29 01:55:41 +0000687
688 // We are supposed to send an asynchronous packet while
689 // we are running.
690 m_async_response.Clear();
691 if (m_async_packet.empty())
692 {
693 if (packet_log)
694 packet_log->Printf ("async: error: empty async packet");
695
696 }
697 else
698 {
699 if (packet_log)
700 packet_log->Printf ("async: sending packet");
701
702 SendPacketAndWaitForResponse (&m_async_packet[0],
703 m_async_packet.size(),
704 m_async_response,
705 false);
706 }
707 // Let the other thread that was trying to send the async
708 // packet know that the packet has been sent and response is
709 // ready...
710 m_async_packet_predicate.SetValue(false, eBroadcastAlways);
711
712 if (packet_log)
Greg Claytonfb72fde2012-05-15 02:50:49 +0000713 packet_log->Printf ("async: sent packet, continue_after_async = %i", continue_after_async);
Greg Clayton2687cd12012-03-29 01:55:41 +0000714
715 // Set the continue packet to resume if our interrupt
716 // for the async packet did cause the stop
Greg Claytonfb72fde2012-05-15 02:50:49 +0000717 if (continue_after_async)
Greg Clayton2687cd12012-03-29 01:55:41 +0000718 {
Greg Claytonf1186de2012-05-24 23:42:14 +0000719 // Reverting this for now as it is causing deadlocks
720 // in programs (<rdar://problem/11529853>). In the future
721 // we should check our thread list and "do the right thing"
722 // for new threads that show up while we stop and run async
723 // packets. Setting the packet to 'c' to continue all threads
724 // is the right thing to do 99.99% of the time because if a
725 // thread was single stepping, and we sent an interrupt, we
726 // will notice above that we didn't stop due to an interrupt
727 // but stopped due to stepping and we would _not_ continue.
728 continue_packet.assign (1, 'c');
Greg Clayton2687cd12012-03-29 01:55:41 +0000729 continue;
730 }
731 }
732 // Stop with signal and thread info
733 state = eStateStopped;
Greg Clayton576d8832011-03-22 04:00:09 +0000734 }
Greg Clayton576d8832011-03-22 04:00:09 +0000735 break;
736
737 case 'W':
738 case 'X':
739 // process exited
740 state = eStateExited;
741 break;
742
743 case 'O':
744 // STDOUT
745 {
Greg Clayton3f875c52013-02-22 22:23:55 +0000746 got_async_packet = true;
Greg Clayton576d8832011-03-22 04:00:09 +0000747 std::string inferior_stdout;
748 inferior_stdout.reserve(response.GetBytesLeft () / 2);
749 char ch;
750 while ((ch = response.GetHexU8()) != '\0')
751 inferior_stdout.append(1, ch);
752 process->AppendSTDOUT (inferior_stdout.c_str(), inferior_stdout.size());
753 }
754 break;
755
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000756 case 'A':
757 // Async miscellaneous reply. Right now, only profile data is coming through this channel.
758 {
Greg Clayton3f875c52013-02-22 22:23:55 +0000759 got_async_packet = true;
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000760 std::string input = response.GetStringRef().substr(1); // '1' to move beyond 'A'
761 if (m_partial_profile_data.length() > 0)
762 {
763 m_partial_profile_data.append(input);
764 input = m_partial_profile_data;
765 m_partial_profile_data.clear();
766 }
767
768 size_t found, pos = 0, len = input.length();
769 while ((found = input.find(end_delimiter, pos)) != std::string::npos)
770 {
771 StringExtractorGDBRemote profileDataExtractor(input.substr(pos, found).c_str());
Han Ming Ong91ed6b82013-06-24 18:15:05 +0000772 std::string profile_data = HarmonizeThreadIdsForProfileData(process, profileDataExtractor);
773 process->BroadcastAsyncProfileData (profile_data);
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000774
775 pos = found + end_delimiter_len;
776 }
777
778 if (pos < len)
779 {
780 // Last incomplete chunk.
781 m_partial_profile_data = input.substr(pos);
782 }
Han Ming Ongab3b8b22012-11-17 00:21:04 +0000783 }
784 break;
785
Greg Clayton576d8832011-03-22 04:00:09 +0000786 case 'E':
787 // ERROR
788 state = eStateInvalid;
789 break;
790
791 default:
792 if (log)
793 log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__);
794 state = eStateInvalid;
795 break;
796 }
797 }
798 }
799 else
800 {
801 if (log)
802 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__);
803 state = eStateInvalid;
804 }
805 }
806 if (log)
807 log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state));
808 response.SetFilePos(0);
809 m_private_is_running.SetValue (false, eBroadcastAlways);
810 m_public_is_running.SetValue (false, eBroadcastAlways);
811 return state;
812}
813
814bool
815GDBRemoteCommunicationClient::SendAsyncSignal (int signo)
816{
Greg Clayton2687cd12012-03-29 01:55:41 +0000817 Mutex::Locker async_locker (m_async_mutex);
Greg Clayton576d8832011-03-22 04:00:09 +0000818 m_async_signal = signo;
819 bool timed_out = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000820 Mutex::Locker locker;
Greg Clayton2687cd12012-03-29 01:55:41 +0000821 if (SendInterrupt (locker, 1, timed_out))
Greg Clayton576d8832011-03-22 04:00:09 +0000822 return true;
823 m_async_signal = -1;
824 return false;
825}
826
Greg Clayton37a0a242012-04-11 00:24:49 +0000827// This function takes a mutex locker as a parameter in case the GetSequenceMutex
Greg Clayton576d8832011-03-22 04:00:09 +0000828// actually succeeds. If it doesn't succeed in acquiring the sequence mutex
829// (the expected result), then it will send the halt packet. If it does succeed
830// then the caller that requested the interrupt will want to keep the sequence
831// locked down so that no one else can send packets while the caller has control.
832// This function usually gets called when we are running and need to stop the
833// target. It can also be used when we are running and and we need to do something
834// else (like read/write memory), so we need to interrupt the running process
835// (gdb remote protocol requires this), and do what we need to do, then resume.
836
837bool
Greg Clayton2687cd12012-03-29 01:55:41 +0000838GDBRemoteCommunicationClient::SendInterrupt
Greg Clayton576d8832011-03-22 04:00:09 +0000839(
840 Mutex::Locker& locker,
841 uint32_t seconds_to_wait_for_stop,
Greg Clayton576d8832011-03-22 04:00:09 +0000842 bool &timed_out
843)
844{
Greg Clayton576d8832011-03-22 04:00:09 +0000845 timed_out = false;
Greg Clayton5160ce52013-03-27 23:08:40 +0000846 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Clayton576d8832011-03-22 04:00:09 +0000847
848 if (IsRunning())
849 {
850 // Only send an interrupt if our debugserver is running...
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000851 if (GetSequenceMutex (locker))
Greg Clayton37a0a242012-04-11 00:24:49 +0000852 {
853 if (log)
854 log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt");
855 }
856 else
Greg Clayton576d8832011-03-22 04:00:09 +0000857 {
858 // Someone has the mutex locked waiting for a response or for the
859 // inferior to stop, so send the interrupt on the down low...
860 char ctrl_c = '\x03';
861 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton576d8832011-03-22 04:00:09 +0000862 size_t bytes_written = Write (&ctrl_c, 1, status, NULL);
Greg Clayton2687cd12012-03-29 01:55:41 +0000863 if (log)
864 log->PutCString("send packet: \\x03");
Greg Clayton576d8832011-03-22 04:00:09 +0000865 if (bytes_written > 0)
866 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000867 m_interrupt_sent = true;
Greg Clayton576d8832011-03-22 04:00:09 +0000868 if (seconds_to_wait_for_stop)
869 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000870 TimeValue timeout;
871 if (seconds_to_wait_for_stop)
872 {
873 timeout = TimeValue::Now();
874 timeout.OffsetWithSeconds (seconds_to_wait_for_stop);
875 }
Greg Clayton576d8832011-03-22 04:00:09 +0000876 if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out))
877 {
878 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +0000879 log->PutCString ("SendInterrupt () - sent interrupt, private state stopped");
Greg Clayton576d8832011-03-22 04:00:09 +0000880 return true;
881 }
882 else
883 {
884 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +0000885 log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume");
Greg Clayton576d8832011-03-22 04:00:09 +0000886 }
887 }
888 else
889 {
890 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +0000891 log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop...");
Greg Clayton576d8832011-03-22 04:00:09 +0000892 return true;
893 }
894 }
895 else
896 {
897 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +0000898 log->Printf ("SendInterrupt () - failed to write interrupt");
Greg Clayton576d8832011-03-22 04:00:09 +0000899 }
900 return false;
901 }
Greg Clayton576d8832011-03-22 04:00:09 +0000902 }
Greg Clayton2687cd12012-03-29 01:55:41 +0000903 else
904 {
905 if (log)
906 log->Printf ("SendInterrupt () - not running");
907 }
Greg Clayton576d8832011-03-22 04:00:09 +0000908 return true;
909}
910
911lldb::pid_t
912GDBRemoteCommunicationClient::GetCurrentProcessID ()
913{
914 StringExtractorGDBRemote response;
915 if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false))
916 {
917 if (response.GetChar() == 'Q')
918 if (response.GetChar() == 'C')
919 return response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID);
920 }
921 return LLDB_INVALID_PROCESS_ID;
922}
923
924bool
925GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str)
926{
927 error_str.clear();
928 StringExtractorGDBRemote response;
929 if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false))
930 {
931 if (response.IsOKResponse())
932 return true;
933 if (response.GetChar() == 'E')
934 {
935 // A string the describes what failed when launching...
936 error_str = response.GetStringRef().substr(1);
937 }
938 else
939 {
940 error_str.assign ("unknown error occurred launching process");
941 }
942 }
943 else
944 {
Jim Ingham98d6da52012-06-28 20:30:23 +0000945 error_str.assign ("timed out waiting for app to launch");
Greg Clayton576d8832011-03-22 04:00:09 +0000946 }
947 return false;
948}
949
950int
951GDBRemoteCommunicationClient::SendArgumentsPacket (char const *argv[])
952{
953 if (argv && argv[0])
954 {
955 StreamString packet;
956 packet.PutChar('A');
957 const char *arg;
958 for (uint32_t i = 0; (arg = argv[i]) != NULL; ++i)
959 {
960 const int arg_len = strlen(arg);
961 if (i > 0)
962 packet.PutChar(',');
963 packet.Printf("%i,%i,", arg_len * 2, i);
964 packet.PutBytesAsRawHex8 (arg, arg_len);
965 }
966
967 StringExtractorGDBRemote response;
968 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
969 {
970 if (response.IsOKResponse())
971 return 0;
972 uint8_t error = response.GetError();
973 if (error)
974 return error;
975 }
976 }
977 return -1;
978}
979
980int
981GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value)
982{
983 if (name_equal_value && name_equal_value[0])
984 {
985 StreamString packet;
986 packet.Printf("QEnvironment:%s", name_equal_value);
987 StringExtractorGDBRemote response;
988 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
989 {
990 if (response.IsOKResponse())
991 return 0;
992 uint8_t error = response.GetError();
993 if (error)
994 return error;
995 }
996 }
997 return -1;
998}
999
Greg Claytonc4103b32011-05-08 04:53:50 +00001000int
1001GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch)
1002{
1003 if (arch && arch[0])
1004 {
1005 StreamString packet;
1006 packet.Printf("QLaunchArch:%s", arch);
1007 StringExtractorGDBRemote response;
1008 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1009 {
1010 if (response.IsOKResponse())
1011 return 0;
1012 uint8_t error = response.GetError();
1013 if (error)
1014 return error;
1015 }
1016 }
1017 return -1;
1018}
1019
Greg Clayton576d8832011-03-22 04:00:09 +00001020bool
Greg Clayton1cb64962011-03-24 04:28:38 +00001021GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major,
1022 uint32_t &minor,
1023 uint32_t &update)
1024{
1025 if (GetHostInfo ())
1026 {
1027 if (m_os_version_major != UINT32_MAX)
1028 {
1029 major = m_os_version_major;
1030 minor = m_os_version_minor;
1031 update = m_os_version_update;
1032 return true;
1033 }
1034 }
1035 return false;
1036}
1037
1038bool
1039GDBRemoteCommunicationClient::GetOSBuildString (std::string &s)
1040{
1041 if (GetHostInfo ())
1042 {
1043 if (!m_os_build.empty())
1044 {
1045 s = m_os_build;
1046 return true;
1047 }
1048 }
1049 s.clear();
1050 return false;
1051}
1052
1053
1054bool
1055GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s)
1056{
1057 if (GetHostInfo ())
1058 {
1059 if (!m_os_kernel.empty())
1060 {
1061 s = m_os_kernel;
1062 return true;
1063 }
1064 }
1065 s.clear();
1066 return false;
1067}
1068
1069bool
1070GDBRemoteCommunicationClient::GetHostname (std::string &s)
1071{
1072 if (GetHostInfo ())
1073 {
1074 if (!m_hostname.empty())
1075 {
1076 s = m_hostname;
1077 return true;
1078 }
1079 }
1080 s.clear();
1081 return false;
1082}
1083
1084ArchSpec
1085GDBRemoteCommunicationClient::GetSystemArchitecture ()
1086{
1087 if (GetHostInfo ())
1088 return m_host_arch;
1089 return ArchSpec();
1090}
1091
Jason Molendaf17b5ac2012-12-19 02:54:03 +00001092const lldb_private::ArchSpec &
1093GDBRemoteCommunicationClient::GetProcessArchitecture ()
1094{
1095 if (m_qProcessInfo_is_valid == eLazyBoolCalculate)
1096 GetCurrentProcessInfo ();
1097 return m_process_arch;
1098}
1099
Greg Clayton1cb64962011-03-24 04:28:38 +00001100
1101bool
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00001102GDBRemoteCommunicationClient::GetHostInfo (bool force)
Greg Clayton576d8832011-03-22 04:00:09 +00001103{
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00001104 if (force || m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton576d8832011-03-22 04:00:09 +00001105 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001106 m_qHostInfo_is_valid = eLazyBoolNo;
Greg Clayton576d8832011-03-22 04:00:09 +00001107 StringExtractorGDBRemote response;
1108 if (SendPacketAndWaitForResponse ("qHostInfo", response, false))
1109 {
Greg Clayton17a0cb62011-05-15 23:46:54 +00001110 if (response.IsNormalResponse())
Greg Claytond314e812011-03-23 00:09:55 +00001111 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001112 std::string name;
1113 std::string value;
1114 uint32_t cpu = LLDB_INVALID_CPUTYPE;
1115 uint32_t sub = 0;
1116 std::string arch_name;
1117 std::string os_name;
1118 std::string vendor_name;
1119 std::string triple;
1120 uint32_t pointer_byte_size = 0;
1121 StringExtractor extractor;
1122 ByteOrder byte_order = eByteOrderInvalid;
1123 uint32_t num_keys_decoded = 0;
1124 while (response.GetNameColonValue(name, value))
Greg Claytond314e812011-03-23 00:09:55 +00001125 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001126 if (name.compare("cputype") == 0)
Greg Clayton1cb64962011-03-24 04:28:38 +00001127 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001128 // exception type in big endian hex
1129 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0);
1130 if (cpu != LLDB_INVALID_CPUTYPE)
1131 ++num_keys_decoded;
1132 }
1133 else if (name.compare("cpusubtype") == 0)
1134 {
1135 // exception count in big endian hex
1136 sub = Args::StringToUInt32 (value.c_str(), 0, 0);
1137 if (sub != 0)
1138 ++num_keys_decoded;
1139 }
1140 else if (name.compare("arch") == 0)
1141 {
1142 arch_name.swap (value);
1143 ++num_keys_decoded;
1144 }
1145 else if (name.compare("triple") == 0)
1146 {
1147 // The triple comes as ASCII hex bytes since it contains '-' chars
1148 extractor.GetStringRef().swap(value);
1149 extractor.SetFilePos(0);
1150 extractor.GetHexByteString (triple);
1151 ++num_keys_decoded;
1152 }
1153 else if (name.compare("os_build") == 0)
1154 {
1155 extractor.GetStringRef().swap(value);
1156 extractor.SetFilePos(0);
1157 extractor.GetHexByteString (m_os_build);
1158 ++num_keys_decoded;
1159 }
1160 else if (name.compare("hostname") == 0)
1161 {
1162 extractor.GetStringRef().swap(value);
1163 extractor.SetFilePos(0);
1164 extractor.GetHexByteString (m_hostname);
1165 ++num_keys_decoded;
1166 }
1167 else if (name.compare("os_kernel") == 0)
1168 {
1169 extractor.GetStringRef().swap(value);
1170 extractor.SetFilePos(0);
1171 extractor.GetHexByteString (m_os_kernel);
1172 ++num_keys_decoded;
1173 }
1174 else if (name.compare("ostype") == 0)
1175 {
1176 os_name.swap (value);
1177 ++num_keys_decoded;
1178 }
1179 else if (name.compare("vendor") == 0)
1180 {
1181 vendor_name.swap(value);
1182 ++num_keys_decoded;
1183 }
1184 else if (name.compare("endian") == 0)
1185 {
1186 ++num_keys_decoded;
1187 if (value.compare("little") == 0)
1188 byte_order = eByteOrderLittle;
1189 else if (value.compare("big") == 0)
1190 byte_order = eByteOrderBig;
1191 else if (value.compare("pdp") == 0)
1192 byte_order = eByteOrderPDP;
1193 else
1194 --num_keys_decoded;
1195 }
1196 else if (name.compare("ptrsize") == 0)
1197 {
1198 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 0);
1199 if (pointer_byte_size != 0)
1200 ++num_keys_decoded;
1201 }
1202 else if (name.compare("os_version") == 0)
1203 {
1204 Args::StringToVersion (value.c_str(),
1205 m_os_version_major,
1206 m_os_version_minor,
1207 m_os_version_update);
1208 if (m_os_version_major != UINT32_MAX)
1209 ++num_keys_decoded;
1210 }
Enrico Granataf04a2192012-07-13 23:18:48 +00001211 else if (name.compare("watchpoint_exceptions_received") == 0)
1212 {
1213 ++num_keys_decoded;
1214 if (strcmp(value.c_str(),"before") == 0)
1215 m_watchpoints_trigger_after_instruction = eLazyBoolNo;
1216 else if (strcmp(value.c_str(),"after") == 0)
1217 m_watchpoints_trigger_after_instruction = eLazyBoolYes;
1218 else
1219 --num_keys_decoded;
1220 }
1221
Greg Clayton32e0a752011-03-30 18:16:51 +00001222 }
1223
1224 if (num_keys_decoded > 0)
1225 m_qHostInfo_is_valid = eLazyBoolYes;
1226
1227 if (triple.empty())
1228 {
1229 if (arch_name.empty())
1230 {
1231 if (cpu != LLDB_INVALID_CPUTYPE)
1232 {
1233 m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1234 if (pointer_byte_size)
1235 {
1236 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1237 }
1238 if (byte_order != eByteOrderInvalid)
1239 {
1240 assert (byte_order == m_host_arch.GetByteOrder());
1241 }
Greg Clayton70512312012-05-08 01:45:38 +00001242
1243 if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0)
1244 {
1245 switch (m_host_arch.GetMachine())
1246 {
1247 case llvm::Triple::arm:
1248 case llvm::Triple::thumb:
1249 os_name = "ios";
1250 break;
1251 default:
1252 os_name = "macosx";
1253 break;
1254 }
1255 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001256 if (!vendor_name.empty())
1257 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1258 if (!os_name.empty())
Greg Claytone1dadb82011-09-15 00:21:03 +00001259 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Greg Clayton32e0a752011-03-30 18:16:51 +00001260
1261 }
1262 }
1263 else
1264 {
1265 std::string triple;
1266 triple += arch_name;
Greg Clayton70512312012-05-08 01:45:38 +00001267 if (!vendor_name.empty() || !os_name.empty())
1268 {
1269 triple += '-';
1270 if (vendor_name.empty())
1271 triple += "unknown";
1272 else
1273 triple += vendor_name;
1274 triple += '-';
1275 if (os_name.empty())
1276 triple += "unknown";
1277 else
1278 triple += os_name;
1279 }
1280 m_host_arch.SetTriple (triple.c_str());
1281
1282 llvm::Triple &host_triple = m_host_arch.GetTriple();
1283 if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin)
1284 {
1285 switch (m_host_arch.GetMachine())
1286 {
1287 case llvm::Triple::arm:
1288 case llvm::Triple::thumb:
1289 host_triple.setOS(llvm::Triple::IOS);
1290 break;
1291 default:
1292 host_triple.setOS(llvm::Triple::MacOSX);
1293 break;
1294 }
1295 }
Greg Clayton1cb64962011-03-24 04:28:38 +00001296 if (pointer_byte_size)
1297 {
1298 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1299 }
1300 if (byte_order != eByteOrderInvalid)
1301 {
1302 assert (byte_order == m_host_arch.GetByteOrder());
1303 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001304
Greg Clayton1cb64962011-03-24 04:28:38 +00001305 }
1306 }
1307 else
1308 {
Greg Clayton70512312012-05-08 01:45:38 +00001309 m_host_arch.SetTriple (triple.c_str());
Greg Claytond314e812011-03-23 00:09:55 +00001310 if (pointer_byte_size)
1311 {
1312 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1313 }
1314 if (byte_order != eByteOrderInvalid)
1315 {
1316 assert (byte_order == m_host_arch.GetByteOrder());
1317 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001318 }
Greg Claytond314e812011-03-23 00:09:55 +00001319 }
Greg Clayton576d8832011-03-22 04:00:09 +00001320 }
1321 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001322 return m_qHostInfo_is_valid == eLazyBoolYes;
Greg Clayton576d8832011-03-22 04:00:09 +00001323}
1324
1325int
1326GDBRemoteCommunicationClient::SendAttach
1327(
1328 lldb::pid_t pid,
1329 StringExtractorGDBRemote& response
1330)
1331{
1332 if (pid != LLDB_INVALID_PROCESS_ID)
1333 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001334 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001335 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, pid);
Andy Gibbsa297a972013-06-19 19:04:53 +00001336 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00001337 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton576d8832011-03-22 04:00:09 +00001338 {
1339 if (response.IsErrorResponse())
1340 return response.GetError();
1341 return 0;
1342 }
1343 }
1344 return -1;
1345}
1346
1347const lldb_private::ArchSpec &
1348GDBRemoteCommunicationClient::GetHostArchitecture ()
1349{
Greg Clayton32e0a752011-03-30 18:16:51 +00001350 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton576d8832011-03-22 04:00:09 +00001351 GetHostInfo ();
Greg Claytond314e812011-03-23 00:09:55 +00001352 return m_host_arch;
Greg Clayton576d8832011-03-22 04:00:09 +00001353}
1354
1355addr_t
1356GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions)
1357{
Greg Clayton70b57652011-05-15 01:25:55 +00001358 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton576d8832011-03-22 04:00:09 +00001359 {
Greg Clayton70b57652011-05-15 01:25:55 +00001360 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton2a48f522011-05-14 01:50:35 +00001361 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001362 const int packet_len = ::snprintf (packet, sizeof(packet), "_M%" PRIx64 ",%s%s%s",
Greg Clayton43e0af02012-09-18 18:04:04 +00001363 (uint64_t)size,
Greg Clayton2a48f522011-05-14 01:50:35 +00001364 permissions & lldb::ePermissionsReadable ? "r" : "",
1365 permissions & lldb::ePermissionsWritable ? "w" : "",
1366 permissions & lldb::ePermissionsExecutable ? "x" : "");
Andy Gibbsa297a972013-06-19 19:04:53 +00001367 assert (packet_len < (int)sizeof(packet));
Greg Clayton2a48f522011-05-14 01:50:35 +00001368 StringExtractorGDBRemote response;
1369 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1370 {
Greg Clayton17a0cb62011-05-15 23:46:54 +00001371 if (!response.IsErrorResponse())
Greg Clayton2a48f522011-05-14 01:50:35 +00001372 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1373 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00001374 else
1375 {
1376 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1377 }
Greg Clayton576d8832011-03-22 04:00:09 +00001378 }
1379 return LLDB_INVALID_ADDRESS;
1380}
1381
1382bool
1383GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr)
1384{
Greg Clayton70b57652011-05-15 01:25:55 +00001385 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton576d8832011-03-22 04:00:09 +00001386 {
Greg Clayton70b57652011-05-15 01:25:55 +00001387 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton2a48f522011-05-14 01:50:35 +00001388 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001389 const int packet_len = ::snprintf(packet, sizeof(packet), "_m%" PRIx64, (uint64_t)addr);
Andy Gibbsa297a972013-06-19 19:04:53 +00001390 assert (packet_len < (int)sizeof(packet));
Greg Clayton2a48f522011-05-14 01:50:35 +00001391 StringExtractorGDBRemote response;
1392 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1393 {
1394 if (response.IsOKResponse())
1395 return true;
Greg Clayton17a0cb62011-05-15 23:46:54 +00001396 }
1397 else
1398 {
1399 m_supports_alloc_dealloc_memory = eLazyBoolNo;
Greg Clayton2a48f522011-05-14 01:50:35 +00001400 }
Greg Clayton576d8832011-03-22 04:00:09 +00001401 }
1402 return false;
1403}
1404
Jim Inghamacff8952013-05-02 00:27:30 +00001405Error
1406GDBRemoteCommunicationClient::Detach (bool keep_stopped)
Greg Clayton37a0a242012-04-11 00:24:49 +00001407{
Jim Inghamacff8952013-05-02 00:27:30 +00001408 Error error;
1409
1410 if (keep_stopped)
1411 {
1412 if (m_supports_detach_stay_stopped == eLazyBoolCalculate)
1413 {
1414 char packet[64];
1415 const int packet_len = ::snprintf(packet, sizeof(packet), "qSupportsDetachAndStayStopped:");
Andy Gibbsa297a972013-06-19 19:04:53 +00001416 assert (packet_len < (int)sizeof(packet));
Jim Inghamacff8952013-05-02 00:27:30 +00001417 StringExtractorGDBRemote response;
1418 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1419 {
1420 m_supports_detach_stay_stopped = eLazyBoolYes;
1421 }
1422 else
1423 {
1424 m_supports_detach_stay_stopped = eLazyBoolNo;
1425 }
1426 }
1427
1428 if (m_supports_detach_stay_stopped == eLazyBoolNo)
1429 {
1430 error.SetErrorString("Stays stopped not supported by this target.");
1431 return error;
1432 }
1433 else
1434 {
1435 size_t num_sent = SendPacket ("D1", 2);
1436 if (num_sent == 0)
1437 error.SetErrorString ("Sending extended disconnect packet failed.");
1438 }
1439 }
1440 else
1441 {
1442 size_t num_sent = SendPacket ("D", 1);
1443 if (num_sent == 0)
1444 error.SetErrorString ("Sending disconnect packet failed.");
1445 }
1446 return error;
Greg Clayton37a0a242012-04-11 00:24:49 +00001447}
1448
Greg Clayton46fb5582011-11-18 07:03:08 +00001449Error
1450GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr,
1451 lldb_private::MemoryRegionInfo &region_info)
1452{
1453 Error error;
1454 region_info.Clear();
1455
1456 if (m_supports_memory_region_info != eLazyBoolNo)
1457 {
1458 m_supports_memory_region_info = eLazyBoolYes;
1459 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001460 const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%" PRIx64, (uint64_t)addr);
Andy Gibbsa297a972013-06-19 19:04:53 +00001461 assert (packet_len < (int)sizeof(packet));
Greg Clayton46fb5582011-11-18 07:03:08 +00001462 StringExtractorGDBRemote response;
1463 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1464 {
1465 std::string name;
1466 std::string value;
1467 addr_t addr_value;
1468 bool success = true;
Jason Molendacb349ee2011-12-13 05:39:38 +00001469 bool saw_permissions = false;
Greg Clayton46fb5582011-11-18 07:03:08 +00001470 while (success && response.GetNameColonValue(name, value))
1471 {
1472 if (name.compare ("start") == 0)
1473 {
1474 addr_value = Args::StringToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success);
1475 if (success)
1476 region_info.GetRange().SetRangeBase(addr_value);
1477 }
1478 else if (name.compare ("size") == 0)
1479 {
1480 addr_value = Args::StringToUInt64(value.c_str(), 0, 16, &success);
1481 if (success)
1482 region_info.GetRange().SetByteSize (addr_value);
1483 }
Jason Molendacb349ee2011-12-13 05:39:38 +00001484 else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid())
Greg Clayton46fb5582011-11-18 07:03:08 +00001485 {
Jason Molendacb349ee2011-12-13 05:39:38 +00001486 saw_permissions = true;
1487 if (region_info.GetRange().Contains (addr))
1488 {
1489 if (value.find('r') != std::string::npos)
1490 region_info.SetReadable (MemoryRegionInfo::eYes);
1491 else
1492 region_info.SetReadable (MemoryRegionInfo::eNo);
1493
1494 if (value.find('w') != std::string::npos)
1495 region_info.SetWritable (MemoryRegionInfo::eYes);
1496 else
1497 region_info.SetWritable (MemoryRegionInfo::eNo);
1498
1499 if (value.find('x') != std::string::npos)
1500 region_info.SetExecutable (MemoryRegionInfo::eYes);
1501 else
1502 region_info.SetExecutable (MemoryRegionInfo::eNo);
1503 }
1504 else
1505 {
1506 // The reported region does not contain this address -- we're looking at an unmapped page
1507 region_info.SetReadable (MemoryRegionInfo::eNo);
1508 region_info.SetWritable (MemoryRegionInfo::eNo);
1509 region_info.SetExecutable (MemoryRegionInfo::eNo);
1510 }
Greg Clayton46fb5582011-11-18 07:03:08 +00001511 }
1512 else if (name.compare ("error") == 0)
1513 {
1514 StringExtractorGDBRemote name_extractor;
1515 // Swap "value" over into "name_extractor"
1516 name_extractor.GetStringRef().swap(value);
1517 // Now convert the HEX bytes into a string value
1518 name_extractor.GetHexByteString (value);
1519 error.SetErrorString(value.c_str());
1520 }
1521 }
Jason Molendacb349ee2011-12-13 05:39:38 +00001522
1523 // We got a valid address range back but no permissions -- which means this is an unmapped page
1524 if (region_info.GetRange().IsValid() && saw_permissions == false)
1525 {
1526 region_info.SetReadable (MemoryRegionInfo::eNo);
1527 region_info.SetWritable (MemoryRegionInfo::eNo);
1528 region_info.SetExecutable (MemoryRegionInfo::eNo);
1529 }
Greg Clayton46fb5582011-11-18 07:03:08 +00001530 }
1531 else
1532 {
1533 m_supports_memory_region_info = eLazyBoolNo;
1534 }
1535 }
1536
1537 if (m_supports_memory_region_info == eLazyBoolNo)
1538 {
1539 error.SetErrorString("qMemoryRegionInfo is not supported");
1540 }
1541 if (error.Fail())
1542 region_info.Clear();
1543 return error;
1544
1545}
1546
Johnny Chen64637202012-05-23 21:09:52 +00001547Error
1548GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num)
1549{
1550 Error error;
1551
1552 if (m_supports_watchpoint_support_info == eLazyBoolYes)
1553 {
1554 num = m_num_supported_hardware_watchpoints;
1555 return error;
1556 }
1557
1558 // Set num to 0 first.
1559 num = 0;
1560 if (m_supports_watchpoint_support_info != eLazyBoolNo)
1561 {
1562 char packet[64];
1563 const int packet_len = ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:");
Andy Gibbsa297a972013-06-19 19:04:53 +00001564 assert (packet_len < (int)sizeof(packet));
Johnny Chen64637202012-05-23 21:09:52 +00001565 StringExtractorGDBRemote response;
1566 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1567 {
1568 m_supports_watchpoint_support_info = eLazyBoolYes;
1569 std::string name;
1570 std::string value;
1571 while (response.GetNameColonValue(name, value))
1572 {
1573 if (name.compare ("num") == 0)
1574 {
1575 num = Args::StringToUInt32(value.c_str(), 0, 0);
1576 m_num_supported_hardware_watchpoints = num;
1577 }
1578 }
1579 }
1580 else
1581 {
1582 m_supports_watchpoint_support_info = eLazyBoolNo;
1583 }
1584 }
1585
1586 if (m_supports_watchpoint_support_info == eLazyBoolNo)
1587 {
1588 error.SetErrorString("qWatchpointSupportInfo is not supported");
1589 }
1590 return error;
1591
1592}
Greg Clayton46fb5582011-11-18 07:03:08 +00001593
Enrico Granataf04a2192012-07-13 23:18:48 +00001594lldb_private::Error
1595GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num, bool& after)
1596{
1597 Error error(GetWatchpointSupportInfo(num));
1598 if (error.Success())
1599 error = GetWatchpointsTriggerAfterInstruction(after);
1600 return error;
1601}
1602
1603lldb_private::Error
1604GDBRemoteCommunicationClient::GetWatchpointsTriggerAfterInstruction (bool &after)
1605{
1606 Error error;
1607
1608 // we assume watchpoints will happen after running the relevant opcode
1609 // and we only want to override this behavior if we have explicitly
1610 // received a qHostInfo telling us otherwise
1611 if (m_qHostInfo_is_valid != eLazyBoolYes)
1612 after = true;
1613 else
1614 after = (m_watchpoints_trigger_after_instruction != eLazyBoolNo);
1615 return error;
1616}
1617
Greg Clayton576d8832011-03-22 04:00:09 +00001618int
1619GDBRemoteCommunicationClient::SetSTDIN (char const *path)
1620{
1621 if (path && path[0])
1622 {
1623 StreamString packet;
1624 packet.PutCString("QSetSTDIN:");
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::SetSTDOUT (char const *path)
1642{
1643 if (path && path[0])
1644 {
1645 StreamString packet;
1646 packet.PutCString("QSetSTDOUT:");
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::SetSTDERR (char const *path)
1664{
1665 if (path && path[0])
1666 {
1667 StreamString packet;
1668 packet.PutCString("QSetSTDERR:");
1669 packet.PutBytesAsRawHex8(path, strlen(path));
1670
1671 StringExtractorGDBRemote response;
1672 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1673 {
1674 if (response.IsOKResponse())
1675 return 0;
1676 uint8_t error = response.GetError();
1677 if (error)
1678 return error;
1679 }
1680 }
1681 return -1;
1682}
1683
1684int
1685GDBRemoteCommunicationClient::SetWorkingDir (char const *path)
1686{
1687 if (path && path[0])
1688 {
1689 StreamString packet;
1690 packet.PutCString("QSetWorkingDir:");
1691 packet.PutBytesAsRawHex8(path, strlen(path));
1692
1693 StringExtractorGDBRemote response;
1694 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1695 {
1696 if (response.IsOKResponse())
1697 return 0;
1698 uint8_t error = response.GetError();
1699 if (error)
1700 return error;
1701 }
1702 }
1703 return -1;
1704}
1705
1706int
1707GDBRemoteCommunicationClient::SetDisableASLR (bool enable)
1708{
Greg Clayton32e0a752011-03-30 18:16:51 +00001709 char packet[32];
1710 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0);
Andy Gibbsa297a972013-06-19 19:04:53 +00001711 assert (packet_len < (int)sizeof(packet));
Greg Clayton576d8832011-03-22 04:00:09 +00001712 StringExtractorGDBRemote response;
Greg Clayton32e0a752011-03-30 18:16:51 +00001713 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
Greg Clayton576d8832011-03-22 04:00:09 +00001714 {
1715 if (response.IsOKResponse())
1716 return 0;
1717 uint8_t error = response.GetError();
1718 if (error)
1719 return error;
1720 }
1721 return -1;
1722}
Greg Clayton32e0a752011-03-30 18:16:51 +00001723
1724bool
Greg Clayton8b82f082011-04-12 05:54:46 +00001725GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00001726{
1727 if (response.IsNormalResponse())
1728 {
1729 std::string name;
1730 std::string value;
1731 StringExtractor extractor;
1732
1733 while (response.GetNameColonValue(name, value))
1734 {
1735 if (name.compare("pid") == 0)
1736 {
1737 process_info.SetProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1738 }
1739 else if (name.compare("ppid") == 0)
1740 {
1741 process_info.SetParentProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
1742 }
1743 else if (name.compare("uid") == 0)
1744 {
Greg Clayton8b82f082011-04-12 05:54:46 +00001745 process_info.SetUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00001746 }
1747 else if (name.compare("euid") == 0)
1748 {
1749 process_info.SetEffectiveUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1750 }
1751 else if (name.compare("gid") == 0)
1752 {
Greg Clayton8b82f082011-04-12 05:54:46 +00001753 process_info.SetGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00001754 }
1755 else if (name.compare("egid") == 0)
1756 {
1757 process_info.SetEffectiveGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
1758 }
1759 else if (name.compare("triple") == 0)
1760 {
1761 // The triple comes as ASCII hex bytes since it contains '-' chars
1762 extractor.GetStringRef().swap(value);
1763 extractor.SetFilePos(0);
1764 extractor.GetHexByteString (value);
Greg Clayton70512312012-05-08 01:45:38 +00001765 process_info.GetArchitecture ().SetTriple (value.c_str());
Greg Clayton32e0a752011-03-30 18:16:51 +00001766 }
1767 else if (name.compare("name") == 0)
1768 {
1769 StringExtractor extractor;
Filipe Cabecinhasf86cf782012-05-07 09:30:51 +00001770 // The process name from ASCII hex bytes since we can't
Greg Clayton32e0a752011-03-30 18:16:51 +00001771 // control the characters in a process name
1772 extractor.GetStringRef().swap(value);
1773 extractor.SetFilePos(0);
1774 extractor.GetHexByteString (value);
Greg Clayton144f3a92011-11-15 03:53:30 +00001775 process_info.GetExecutableFile().SetFile (value.c_str(), false);
Greg Clayton32e0a752011-03-30 18:16:51 +00001776 }
1777 }
1778
1779 if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
1780 return true;
1781 }
1782 return false;
1783}
1784
1785bool
Greg Clayton8b82f082011-04-12 05:54:46 +00001786GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00001787{
1788 process_info.Clear();
1789
1790 if (m_supports_qProcessInfoPID)
1791 {
1792 char packet[32];
Daniel Malead01b2952012-11-29 21:49:15 +00001793 const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%" PRIu64, pid);
Andy Gibbsa297a972013-06-19 19:04:53 +00001794 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00001795 StringExtractorGDBRemote response;
1796 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
1797 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001798 return DecodeProcessInfoResponse (response, process_info);
1799 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00001800 else
1801 {
1802 m_supports_qProcessInfoPID = false;
1803 return false;
1804 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001805 }
1806 return false;
1807}
1808
Jason Molendaf17b5ac2012-12-19 02:54:03 +00001809bool
1810GDBRemoteCommunicationClient::GetCurrentProcessInfo ()
1811{
1812 if (m_qProcessInfo_is_valid == eLazyBoolYes)
1813 return true;
1814 if (m_qProcessInfo_is_valid == eLazyBoolNo)
1815 return false;
1816
1817 GetHostInfo ();
1818
1819 StringExtractorGDBRemote response;
1820 if (SendPacketAndWaitForResponse ("qProcessInfo", response, false))
1821 {
1822 if (response.IsNormalResponse())
1823 {
1824 std::string name;
1825 std::string value;
1826 uint32_t cpu = LLDB_INVALID_CPUTYPE;
1827 uint32_t sub = 0;
1828 std::string arch_name;
1829 std::string os_name;
1830 std::string vendor_name;
1831 std::string triple;
1832 uint32_t pointer_byte_size = 0;
1833 StringExtractor extractor;
1834 ByteOrder byte_order = eByteOrderInvalid;
1835 uint32_t num_keys_decoded = 0;
1836 while (response.GetNameColonValue(name, value))
1837 {
1838 if (name.compare("cputype") == 0)
1839 {
1840 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 16);
1841 if (cpu != LLDB_INVALID_CPUTYPE)
1842 ++num_keys_decoded;
1843 }
1844 else if (name.compare("cpusubtype") == 0)
1845 {
1846 sub = Args::StringToUInt32 (value.c_str(), 0, 16);
1847 if (sub != 0)
1848 ++num_keys_decoded;
1849 }
1850 else if (name.compare("ostype") == 0)
1851 {
1852 os_name.swap (value);
1853 ++num_keys_decoded;
1854 }
1855 else if (name.compare("vendor") == 0)
1856 {
1857 vendor_name.swap(value);
1858 ++num_keys_decoded;
1859 }
1860 else if (name.compare("endian") == 0)
1861 {
1862 ++num_keys_decoded;
1863 if (value.compare("little") == 0)
1864 byte_order = eByteOrderLittle;
1865 else if (value.compare("big") == 0)
1866 byte_order = eByteOrderBig;
1867 else if (value.compare("pdp") == 0)
1868 byte_order = eByteOrderPDP;
1869 else
1870 --num_keys_decoded;
1871 }
1872 else if (name.compare("ptrsize") == 0)
1873 {
1874 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 16);
1875 if (pointer_byte_size != 0)
1876 ++num_keys_decoded;
1877 }
1878 }
1879 if (num_keys_decoded > 0)
1880 m_qProcessInfo_is_valid = eLazyBoolYes;
1881 if (cpu != LLDB_INVALID_CPUTYPE && !os_name.empty() && !vendor_name.empty())
1882 {
1883 m_process_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1884 if (pointer_byte_size)
1885 {
1886 assert (pointer_byte_size == m_process_arch.GetAddressByteSize());
1887 }
1888 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1889 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
1890 return true;
1891 }
1892 }
1893 }
1894 else
1895 {
1896 m_qProcessInfo_is_valid = eLazyBoolNo;
1897 }
1898
1899 return false;
1900}
1901
1902
Greg Clayton32e0a752011-03-30 18:16:51 +00001903uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +00001904GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info,
1905 ProcessInstanceInfoList &process_infos)
Greg Clayton32e0a752011-03-30 18:16:51 +00001906{
1907 process_infos.Clear();
1908
1909 if (m_supports_qfProcessInfo)
1910 {
1911 StreamString packet;
1912 packet.PutCString ("qfProcessInfo");
1913 if (!match_info.MatchAllProcesses())
1914 {
1915 packet.PutChar (':');
1916 const char *name = match_info.GetProcessInfo().GetName();
1917 bool has_name_match = false;
1918 if (name && name[0])
1919 {
1920 has_name_match = true;
1921 NameMatchType name_match_type = match_info.GetNameMatchType();
1922 switch (name_match_type)
1923 {
1924 case eNameMatchIgnore:
1925 has_name_match = false;
1926 break;
1927
1928 case eNameMatchEquals:
1929 packet.PutCString ("name_match:equals;");
1930 break;
1931
1932 case eNameMatchContains:
1933 packet.PutCString ("name_match:contains;");
1934 break;
1935
1936 case eNameMatchStartsWith:
1937 packet.PutCString ("name_match:starts_with;");
1938 break;
1939
1940 case eNameMatchEndsWith:
1941 packet.PutCString ("name_match:ends_with;");
1942 break;
1943
1944 case eNameMatchRegularExpression:
1945 packet.PutCString ("name_match:regex;");
1946 break;
1947 }
1948 if (has_name_match)
1949 {
1950 packet.PutCString ("name:");
1951 packet.PutBytesAsRawHex8(name, ::strlen(name));
1952 packet.PutChar (';');
1953 }
1954 }
1955
1956 if (match_info.GetProcessInfo().ProcessIDIsValid())
Daniel Malead01b2952012-11-29 21:49:15 +00001957 packet.Printf("pid:%" PRIu64 ";",match_info.GetProcessInfo().GetProcessID());
Greg Clayton32e0a752011-03-30 18:16:51 +00001958 if (match_info.GetProcessInfo().ParentProcessIDIsValid())
Daniel Malead01b2952012-11-29 21:49:15 +00001959 packet.Printf("parent_pid:%" PRIu64 ";",match_info.GetProcessInfo().GetParentProcessID());
Greg Clayton8b82f082011-04-12 05:54:46 +00001960 if (match_info.GetProcessInfo().UserIDIsValid())
1961 packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID());
1962 if (match_info.GetProcessInfo().GroupIDIsValid())
1963 packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID());
Greg Clayton32e0a752011-03-30 18:16:51 +00001964 if (match_info.GetProcessInfo().EffectiveUserIDIsValid())
1965 packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID());
1966 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1967 packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID());
1968 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
1969 packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0);
1970 if (match_info.GetProcessInfo().GetArchitecture().IsValid())
1971 {
1972 const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture();
1973 const llvm::Triple &triple = match_arch.GetTriple();
1974 packet.PutCString("triple:");
1975 packet.PutCStringAsRawHex8(triple.getTriple().c_str());
1976 packet.PutChar (';');
1977 }
1978 }
1979 StringExtractorGDBRemote response;
1980 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false))
1981 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001982 do
1983 {
Greg Clayton8b82f082011-04-12 05:54:46 +00001984 ProcessInstanceInfo process_info;
Greg Clayton32e0a752011-03-30 18:16:51 +00001985 if (!DecodeProcessInfoResponse (response, process_info))
1986 break;
1987 process_infos.Append(process_info);
1988 response.GetStringRef().clear();
1989 response.SetFilePos(0);
1990 } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false));
1991 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00001992 else
1993 {
1994 m_supports_qfProcessInfo = false;
1995 return 0;
1996 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001997 }
1998 return process_infos.GetSize();
1999
2000}
2001
2002bool
2003GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name)
2004{
2005 if (m_supports_qUserName)
2006 {
2007 char packet[32];
2008 const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002009 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00002010 StringExtractorGDBRemote response;
2011 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
2012 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002013 if (response.IsNormalResponse())
2014 {
2015 // Make sure we parsed the right number of characters. The response is
2016 // the hex encoded user name and should make up the entire packet.
2017 // If there are any non-hex ASCII bytes, the length won't match below..
2018 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
2019 return true;
2020 }
2021 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002022 else
2023 {
2024 m_supports_qUserName = false;
2025 return false;
2026 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002027 }
2028 return false;
2029
2030}
2031
2032bool
2033GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name)
2034{
2035 if (m_supports_qGroupName)
2036 {
2037 char packet[32];
2038 const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002039 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00002040 StringExtractorGDBRemote response;
2041 if (SendPacketAndWaitForResponse (packet, packet_len, response, false))
2042 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002043 if (response.IsNormalResponse())
2044 {
2045 // Make sure we parsed the right number of characters. The response is
2046 // the hex encoded group name and should make up the entire packet.
2047 // If there are any non-hex ASCII bytes, the length won't match below..
2048 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
2049 return true;
2050 }
2051 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002052 else
2053 {
2054 m_supports_qGroupName = false;
2055 return false;
2056 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002057 }
2058 return false;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002059}
Greg Clayton32e0a752011-03-30 18:16:51 +00002060
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002061void
2062GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets)
2063{
2064 uint32_t i;
2065 TimeValue start_time, end_time;
2066 uint64_t total_time_nsec;
2067 float packets_per_second;
2068 if (SendSpeedTestPacket (0, 0))
2069 {
2070 for (uint32_t send_size = 0; send_size <= 1024; send_size *= 2)
2071 {
2072 for (uint32_t recv_size = 0; recv_size <= 1024; recv_size *= 2)
2073 {
2074 start_time = TimeValue::Now();
2075 for (i=0; i<num_packets; ++i)
2076 {
2077 SendSpeedTestPacket (send_size, recv_size);
2078 }
2079 end_time = TimeValue::Now();
2080 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourneba23ca02011-06-18 23:52:14 +00002081 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Daniel Malead01b2952012-11-29 21:49:15 +00002082 printf ("%u qSpeedTest(send=%-5u, recv=%-5u) in %" PRIu64 ".%9.9" PRIu64 " sec for %f packets/sec.\n",
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002083 num_packets,
2084 send_size,
2085 recv_size,
Peter Collingbourneba23ca02011-06-18 23:52:14 +00002086 total_time_nsec / TimeValue::NanoSecPerSec,
2087 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002088 packets_per_second);
2089 if (recv_size == 0)
2090 recv_size = 32;
2091 }
2092 if (send_size == 0)
2093 send_size = 32;
2094 }
2095 }
2096 else
2097 {
2098 start_time = TimeValue::Now();
2099 for (i=0; i<num_packets; ++i)
2100 {
2101 GetCurrentProcessID ();
2102 }
2103 end_time = TimeValue::Now();
2104 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Peter Collingbourneba23ca02011-06-18 23:52:14 +00002105 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
Daniel Malead01b2952012-11-29 21:49:15 +00002106 printf ("%u 'qC' packets packets in 0x%" PRIu64 "%9.9" PRIu64 " sec for %f packets/sec.\n",
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002107 num_packets,
Peter Collingbourneba23ca02011-06-18 23:52:14 +00002108 total_time_nsec / TimeValue::NanoSecPerSec,
2109 total_time_nsec % TimeValue::NanoSecPerSec,
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002110 packets_per_second);
2111 }
2112}
2113
2114bool
2115GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size)
2116{
2117 StreamString packet;
2118 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size);
2119 uint32_t bytes_left = send_size;
2120 while (bytes_left > 0)
2121 {
2122 if (bytes_left >= 26)
2123 {
2124 packet.PutCString("abcdefghijklmnopqrstuvwxyz");
2125 bytes_left -= 26;
2126 }
2127 else
2128 {
2129 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz");
2130 bytes_left = 0;
2131 }
2132 }
2133
2134 StringExtractorGDBRemote response;
Greg Clayton17a0cb62011-05-15 23:46:54 +00002135 return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) > 0;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002136 return false;
Greg Clayton32e0a752011-03-30 18:16:51 +00002137}
Greg Clayton8b82f082011-04-12 05:54:46 +00002138
2139uint16_t
2140GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort ()
2141{
2142 StringExtractorGDBRemote response;
2143 if (SendPacketAndWaitForResponse("qLaunchGDBServer", strlen("qLaunchGDBServer"), response, false))
2144 {
2145 std::string name;
2146 std::string value;
2147 uint16_t port = 0;
Greg Clayton23f59502012-07-17 03:23:13 +00002148 //lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
Greg Clayton8b82f082011-04-12 05:54:46 +00002149 while (response.GetNameColonValue(name, value))
2150 {
2151 if (name.size() == 4 && name.compare("port") == 0)
2152 port = Args::StringToUInt32(value.c_str(), 0, 0);
Greg Clayton23f59502012-07-17 03:23:13 +00002153// if (name.size() == 3 && name.compare("pid") == 0)
2154// pid = Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0);
Greg Clayton8b82f082011-04-12 05:54:46 +00002155 }
2156 return port;
2157 }
2158 return 0;
2159}
2160
2161bool
Jason Molendae9ca4af2013-02-23 02:04:45 +00002162GDBRemoteCommunicationClient::SetCurrentThread (uint64_t tid)
Greg Clayton8b82f082011-04-12 05:54:46 +00002163{
2164 if (m_curr_tid == tid)
2165 return true;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002166
Greg Clayton8b82f082011-04-12 05:54:46 +00002167 char packet[32];
2168 int packet_len;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002169 if (tid == UINT64_MAX)
2170 packet_len = ::snprintf (packet, sizeof(packet), "Hg-1");
Greg Clayton8b82f082011-04-12 05:54:46 +00002171 else
Jason Molendae9ca4af2013-02-23 02:04:45 +00002172 packet_len = ::snprintf (packet, sizeof(packet), "Hg%" PRIx64, tid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002173 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002174 StringExtractorGDBRemote response;
2175 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2176 {
2177 if (response.IsOKResponse())
2178 {
2179 m_curr_tid = tid;
2180 return true;
2181 }
2182 }
2183 return false;
2184}
2185
2186bool
Jason Molendae9ca4af2013-02-23 02:04:45 +00002187GDBRemoteCommunicationClient::SetCurrentThreadForRun (uint64_t tid)
Greg Clayton8b82f082011-04-12 05:54:46 +00002188{
2189 if (m_curr_tid_run == tid)
2190 return true;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002191
Greg Clayton8b82f082011-04-12 05:54:46 +00002192 char packet[32];
2193 int packet_len;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002194 if (tid == UINT64_MAX)
2195 packet_len = ::snprintf (packet, sizeof(packet), "Hc-1");
Greg Clayton8b82f082011-04-12 05:54:46 +00002196 else
Jason Molendae9ca4af2013-02-23 02:04:45 +00002197 packet_len = ::snprintf (packet, sizeof(packet), "Hc%" PRIx64, tid);
2198
Andy Gibbsa297a972013-06-19 19:04:53 +00002199 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002200 StringExtractorGDBRemote response;
2201 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2202 {
2203 if (response.IsOKResponse())
2204 {
2205 m_curr_tid_run = tid;
2206 return true;
2207 }
2208 }
2209 return false;
2210}
2211
2212bool
2213GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response)
2214{
2215 if (SendPacketAndWaitForResponse("?", 1, response, false))
2216 return response.IsNormalResponse();
2217 return false;
2218}
2219
2220bool
Greg Claytonf402f782012-10-13 02:11:55 +00002221GDBRemoteCommunicationClient::GetThreadStopInfo (lldb::tid_t tid, StringExtractorGDBRemote &response)
Greg Clayton8b82f082011-04-12 05:54:46 +00002222{
2223 if (m_supports_qThreadStopInfo)
2224 {
2225 char packet[256];
Daniel Malead01b2952012-11-29 21:49:15 +00002226 int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%" PRIx64, tid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002227 assert (packet_len < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002228 if (SendPacketAndWaitForResponse(packet, packet_len, response, false))
2229 {
Greg Clayton17a0cb62011-05-15 23:46:54 +00002230 if (response.IsNormalResponse())
Greg Clayton8b82f082011-04-12 05:54:46 +00002231 return true;
2232 else
2233 return false;
2234 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002235 else
2236 {
2237 m_supports_qThreadStopInfo = false;
2238 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002239 }
Greg Clayton5fe15d22011-05-20 03:15:54 +00002240// if (SetCurrentThread (tid))
2241// return GetStopReply (response);
Greg Clayton8b82f082011-04-12 05:54:46 +00002242 return false;
2243}
2244
2245
2246uint8_t
2247GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length)
2248{
2249 switch (type)
2250 {
2251 case eBreakpointSoftware: if (!m_supports_z0) return UINT8_MAX; break;
2252 case eBreakpointHardware: if (!m_supports_z1) return UINT8_MAX; break;
2253 case eWatchpointWrite: if (!m_supports_z2) return UINT8_MAX; break;
2254 case eWatchpointRead: if (!m_supports_z3) return UINT8_MAX; break;
2255 case eWatchpointReadWrite: if (!m_supports_z4) return UINT8_MAX; break;
Greg Clayton8b82f082011-04-12 05:54:46 +00002256 }
2257
2258 char packet[64];
2259 const int packet_len = ::snprintf (packet,
2260 sizeof(packet),
Daniel Malead01b2952012-11-29 21:49:15 +00002261 "%c%i,%" PRIx64 ",%x",
Greg Clayton8b82f082011-04-12 05:54:46 +00002262 insert ? 'Z' : 'z',
2263 type,
2264 addr,
2265 length);
2266
Andy Gibbsa297a972013-06-19 19:04:53 +00002267 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002268 StringExtractorGDBRemote response;
2269 if (SendPacketAndWaitForResponse(packet, packet_len, response, true))
2270 {
2271 if (response.IsOKResponse())
2272 return 0;
Greg Clayton8b82f082011-04-12 05:54:46 +00002273 else if (response.IsErrorResponse())
2274 return response.GetError();
2275 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002276 else
2277 {
2278 switch (type)
2279 {
2280 case eBreakpointSoftware: m_supports_z0 = false; break;
2281 case eBreakpointHardware: m_supports_z1 = false; break;
2282 case eWatchpointWrite: m_supports_z2 = false; break;
2283 case eWatchpointRead: m_supports_z3 = false; break;
2284 case eWatchpointReadWrite: m_supports_z4 = false; break;
Greg Clayton17a0cb62011-05-15 23:46:54 +00002285 }
2286 }
2287
Greg Clayton8b82f082011-04-12 05:54:46 +00002288 return UINT8_MAX;
2289}
Greg Claytonadc00cb2011-05-20 23:38:13 +00002290
2291size_t
2292GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
2293 bool &sequence_mutex_unavailable)
2294{
2295 Mutex::Locker locker;
2296 thread_ids.clear();
2297
Jim Ingham4ceb9282012-06-08 22:50:40 +00002298 if (GetSequenceMutex (locker, "ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex"))
Greg Claytonadc00cb2011-05-20 23:38:13 +00002299 {
2300 sequence_mutex_unavailable = false;
2301 StringExtractorGDBRemote response;
2302
Greg Clayton73bf5db2011-06-17 01:22:15 +00002303 for (SendPacketNoLock ("qfThreadInfo", strlen("qfThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
Greg Claytonadc00cb2011-05-20 23:38:13 +00002304 response.IsNormalResponse();
Greg Clayton73bf5db2011-06-17 01:22:15 +00002305 SendPacketNoLock ("qsThreadInfo", strlen("qsThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()))
Greg Claytonadc00cb2011-05-20 23:38:13 +00002306 {
2307 char ch = response.GetChar();
2308 if (ch == 'l')
2309 break;
2310 if (ch == 'm')
2311 {
2312 do
2313 {
Jason Molendae9ca4af2013-02-23 02:04:45 +00002314 tid_t tid = response.GetHexMaxU64(false, LLDB_INVALID_THREAD_ID);
Greg Claytonadc00cb2011-05-20 23:38:13 +00002315
2316 if (tid != LLDB_INVALID_THREAD_ID)
2317 {
2318 thread_ids.push_back (tid);
2319 }
2320 ch = response.GetChar(); // Skip the command separator
2321 } while (ch == ','); // Make sure we got a comma separator
2322 }
2323 }
2324 }
2325 else
2326 {
Jim Ingham4ceb9282012-06-08 22:50:40 +00002327#if defined (LLDB_CONFIGURATION_DEBUG)
2328 // assert(!"ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex");
2329#else
Greg Clayton5160ce52013-03-27 23:08:40 +00002330 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Claytonc3c0b0e2012-04-12 19:04:34 +00002331 if (log)
2332 log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'");
Jim Ingham4ceb9282012-06-08 22:50:40 +00002333#endif
Greg Claytonadc00cb2011-05-20 23:38:13 +00002334 sequence_mutex_unavailable = true;
2335 }
2336 return thread_ids.size();
2337}
Greg Clayton37a0a242012-04-11 00:24:49 +00002338
2339lldb::addr_t
2340GDBRemoteCommunicationClient::GetShlibInfoAddr()
2341{
2342 if (!IsRunning())
2343 {
2344 StringExtractorGDBRemote response;
2345 if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false))
2346 {
2347 if (response.IsNormalResponse())
2348 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
2349 }
2350 }
2351 return LLDB_INVALID_ADDRESS;
2352}
2353