blob: 3eb3d9788ec555d35da4b111234fcab4bb2572f3 [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
Daniel Maleab89d0492013-08-28 16:06:16 +000014#include <sys/stat.h>
15
Greg Clayton576d8832011-03-22 04:00:09 +000016// C++ Includes
Han Ming Ong4b6459f2013-01-18 23:11:53 +000017#include <sstream>
18
Greg Clayton576d8832011-03-22 04:00:09 +000019// Other libraries and framework includes
Saleem Abdulrasool28606952014-06-27 05:17:41 +000020#include "llvm/ADT/STLExtras.h"
Greg Clayton576d8832011-03-22 04:00:09 +000021#include "llvm/ADT/Triple.h"
22#include "lldb/Interpreter/Args.h"
23#include "lldb/Core/ConnectionFileDescriptor.h"
24#include "lldb/Core/Log.h"
25#include "lldb/Core/State.h"
Daniel Maleae0f8f572013-08-26 23:57:52 +000026#include "lldb/Core/StreamGDBRemote.h"
Greg Clayton576d8832011-03-22 04:00:09 +000027#include "lldb/Core/StreamString.h"
28#include "lldb/Host/Endian.h"
29#include "lldb/Host/Host.h"
30#include "lldb/Host/TimeValue.h"
Jason Molendaa3329782014-03-29 18:54:20 +000031#include "lldb/Target/Target.h"
Greg Clayton576d8832011-03-22 04:00:09 +000032
33// Project includes
34#include "Utility/StringExtractorGDBRemote.h"
35#include "ProcessGDBRemote.h"
36#include "ProcessGDBRemoteLog.h"
Virgile Bellob2f1fb22013-08-23 12:44:05 +000037#include "lldb/Host/Config.h"
Greg Clayton576d8832011-03-22 04:00:09 +000038
39using namespace lldb;
40using namespace lldb_private;
41
Todd Fiala0a70a842014-05-28 16:43:26 +000042#if defined(LLDB_DISABLE_POSIX) && !defined(SIGSTOP)
Virgile Bellob2f1fb22013-08-23 12:44:05 +000043#define SIGSTOP 17
44#endif
45
Greg Clayton576d8832011-03-22 04:00:09 +000046//----------------------------------------------------------------------
47// GDBRemoteCommunicationClient constructor
48//----------------------------------------------------------------------
Greg Clayton8b82f082011-04-12 05:54:46 +000049GDBRemoteCommunicationClient::GDBRemoteCommunicationClient(bool is_platform) :
50 GDBRemoteCommunication("gdb-remote.client", "gdb-remote.client.rx_packet", is_platform),
Greg Clayton576d8832011-03-22 04:00:09 +000051 m_supports_not_sending_acks (eLazyBoolCalculate),
52 m_supports_thread_suffix (eLazyBoolCalculate),
Greg Clayton44633992012-04-10 03:22:03 +000053 m_supports_threads_in_stop_reply (eLazyBoolCalculate),
Greg Clayton576d8832011-03-22 04:00:09 +000054 m_supports_vCont_all (eLazyBoolCalculate),
55 m_supports_vCont_any (eLazyBoolCalculate),
56 m_supports_vCont_c (eLazyBoolCalculate),
57 m_supports_vCont_C (eLazyBoolCalculate),
58 m_supports_vCont_s (eLazyBoolCalculate),
59 m_supports_vCont_S (eLazyBoolCalculate),
Greg Clayton32e0a752011-03-30 18:16:51 +000060 m_qHostInfo_is_valid (eLazyBoolCalculate),
Todd Fiala9f72b3a2014-05-07 19:28:21 +000061 m_curr_pid_is_valid (eLazyBoolCalculate),
Jason Molendaf17b5ac2012-12-19 02:54:03 +000062 m_qProcessInfo_is_valid (eLazyBoolCalculate),
Jason Molendaa3329782014-03-29 18:54:20 +000063 m_qGDBServerVersion_is_valid (eLazyBoolCalculate),
Greg Clayton70b57652011-05-15 01:25:55 +000064 m_supports_alloc_dealloc_memory (eLazyBoolCalculate),
Greg Clayton46fb5582011-11-18 07:03:08 +000065 m_supports_memory_region_info (eLazyBoolCalculate),
Johnny Chen64637202012-05-23 21:09:52 +000066 m_supports_watchpoint_support_info (eLazyBoolCalculate),
Jim Inghamacff8952013-05-02 00:27:30 +000067 m_supports_detach_stay_stopped (eLazyBoolCalculate),
Enrico Granataf04a2192012-07-13 23:18:48 +000068 m_watchpoints_trigger_after_instruction(eLazyBoolCalculate),
Jim Inghamcd16df92012-07-20 21:37:13 +000069 m_attach_or_wait_reply(eLazyBoolCalculate),
Jim Ingham279ceec2012-07-25 21:12:43 +000070 m_prepare_for_reg_writing_reply (eLazyBoolCalculate),
Eric Christopher2490f5c2013-08-30 17:50:57 +000071 m_supports_p (eLazyBoolCalculate),
Jason Molendabdc4f122014-05-06 02:59:39 +000072 m_supports_x (eLazyBoolCalculate),
Jason Molendaa3329782014-03-29 18:54:20 +000073 m_avoid_g_packets (eLazyBoolCalculate),
Greg Claytonf74cf862013-11-13 23:28:31 +000074 m_supports_QSaveRegisterState (eLazyBoolCalculate),
Steve Pucci03904ac2014-03-04 23:18:46 +000075 m_supports_qXfer_auxv_read (eLazyBoolCalculate),
Steve Pucci5ae54ae2014-01-25 05:46:51 +000076 m_supports_qXfer_libraries_read (eLazyBoolCalculate),
77 m_supports_qXfer_libraries_svr4_read (eLazyBoolCalculate),
78 m_supports_augmented_libraries_svr4_read (eLazyBoolCalculate),
Jason Molenda705b1802014-06-13 02:37:02 +000079 m_supports_jThreadExtendedInfo (eLazyBoolCalculate),
Greg Clayton32e0a752011-03-30 18:16:51 +000080 m_supports_qProcessInfoPID (true),
81 m_supports_qfProcessInfo (true),
82 m_supports_qUserName (true),
83 m_supports_qGroupName (true),
Greg Clayton8b82f082011-04-12 05:54:46 +000084 m_supports_qThreadStopInfo (true),
85 m_supports_z0 (true),
86 m_supports_z1 (true),
87 m_supports_z2 (true),
88 m_supports_z3 (true),
89 m_supports_z4 (true),
Greg Clayton89600582013-10-10 17:53:50 +000090 m_supports_QEnvironment (true),
91 m_supports_QEnvironmentHexEncoded (true),
Todd Fiala9f72b3a2014-05-07 19:28:21 +000092 m_curr_pid (LLDB_INVALID_PROCESS_ID),
Greg Clayton8b82f082011-04-12 05:54:46 +000093 m_curr_tid (LLDB_INVALID_THREAD_ID),
94 m_curr_tid_run (LLDB_INVALID_THREAD_ID),
Johnny Chen64637202012-05-23 21:09:52 +000095 m_num_supported_hardware_watchpoints (0),
Greg Clayton576d8832011-03-22 04:00:09 +000096 m_async_mutex (Mutex::eMutexTypeRecursive),
97 m_async_packet_predicate (false),
98 m_async_packet (),
Jim Inghama6195b72013-12-18 01:24:33 +000099 m_async_result (PacketResult::Success),
Greg Clayton576d8832011-03-22 04:00:09 +0000100 m_async_response (),
101 m_async_signal (-1),
Jim Inghamb8cd5752014-04-16 02:24:17 +0000102 m_interrupt_sent (false),
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000103 m_thread_id_to_used_usec_map (),
Greg Clayton1cb64962011-03-24 04:28:38 +0000104 m_host_arch(),
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000105 m_process_arch(),
Greg Clayton1cb64962011-03-24 04:28:38 +0000106 m_os_version_major (UINT32_MAX),
107 m_os_version_minor (UINT32_MAX),
Greg Clayton9ac6d2d2013-10-25 18:13:17 +0000108 m_os_version_update (UINT32_MAX),
109 m_os_build (),
110 m_os_kernel (),
111 m_hostname (),
Jason Molendaa3329782014-03-29 18:54:20 +0000112 m_gdb_server_name(),
113 m_gdb_server_version(UINT32_MAX),
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000114 m_default_packet_timeout (0),
115 m_max_packet_size (0)
Greg Clayton576d8832011-03-22 04:00:09 +0000116{
Greg Clayton576d8832011-03-22 04:00:09 +0000117}
118
119//----------------------------------------------------------------------
120// Destructor
121//----------------------------------------------------------------------
122GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient()
123{
Greg Clayton576d8832011-03-22 04:00:09 +0000124 if (IsConnected())
Greg Clayton576d8832011-03-22 04:00:09 +0000125 Disconnect();
Greg Clayton576d8832011-03-22 04:00:09 +0000126}
127
128bool
Greg Clayton1cb64962011-03-24 04:28:38 +0000129GDBRemoteCommunicationClient::HandshakeWithServer (Error *error_ptr)
130{
Greg Claytonfb909312013-11-23 01:58:15 +0000131 ResetDiscoverableSettings();
132
Greg Clayton1cb64962011-03-24 04:28:38 +0000133 // Start the read thread after we send the handshake ack since if we
134 // fail to send the handshake ack, there is no reason to continue...
135 if (SendAck())
Greg Claytonfb909312013-11-23 01:58:15 +0000136 {
Ed Maste48f986f2013-12-18 15:31:45 +0000137 // Wait for any responses that might have been queued up in the remote
138 // GDB server and flush them all
139 StringExtractorGDBRemote response;
140 PacketResult packet_result = PacketResult::Success;
141 const uint32_t timeout_usec = 10 * 1000; // Wait for 10 ms for a response
142 while (packet_result == PacketResult::Success)
143 packet_result = WaitForPacketWithTimeoutMicroSecondsNoLock (response, timeout_usec);
144
Greg Claytonfb909312013-11-23 01:58:15 +0000145 // The return value from QueryNoAckModeSupported() is true if the packet
146 // was sent and _any_ response (including UNIMPLEMENTED) was received),
147 // or false if no response was received. This quickly tells us if we have
148 // a live connection to a remote GDB server...
149 if (QueryNoAckModeSupported())
150 {
Greg Clayton700e5082014-02-21 19:11:28 +0000151#if 0
152 // Set above line to "#if 1" to test packet speed if remote GDB server
153 // supports the qSpeedTest packet...
154 TestPacketSpeed(10000);
155#endif
Greg Claytonfb909312013-11-23 01:58:15 +0000156 return true;
157 }
158 else
159 {
160 if (error_ptr)
161 error_ptr->SetErrorString("failed to get reply to handshake packet");
162 }
163 }
164 else
165 {
166 if (error_ptr)
167 error_ptr->SetErrorString("failed to send the handshake ack");
168 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000169 return false;
170}
171
Greg Claytonfb909312013-11-23 01:58:15 +0000172bool
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000173GDBRemoteCommunicationClient::GetAugmentedLibrariesSVR4ReadSupported ()
174{
175 if (m_supports_augmented_libraries_svr4_read == eLazyBoolCalculate)
176 {
177 GetRemoteQSupported();
178 }
179 return (m_supports_augmented_libraries_svr4_read == eLazyBoolYes);
180}
181
182bool
183GDBRemoteCommunicationClient::GetQXferLibrariesSVR4ReadSupported ()
184{
185 if (m_supports_qXfer_libraries_svr4_read == eLazyBoolCalculate)
186 {
187 GetRemoteQSupported();
188 }
189 return (m_supports_qXfer_libraries_svr4_read == eLazyBoolYes);
190}
191
192bool
193GDBRemoteCommunicationClient::GetQXferLibrariesReadSupported ()
194{
195 if (m_supports_qXfer_libraries_read == eLazyBoolCalculate)
196 {
197 GetRemoteQSupported();
198 }
199 return (m_supports_qXfer_libraries_read == eLazyBoolYes);
200}
201
Steve Pucci03904ac2014-03-04 23:18:46 +0000202bool
203GDBRemoteCommunicationClient::GetQXferAuxvReadSupported ()
204{
205 if (m_supports_qXfer_auxv_read == eLazyBoolCalculate)
206 {
207 GetRemoteQSupported();
208 }
209 return (m_supports_qXfer_auxv_read == eLazyBoolYes);
210}
211
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000212uint64_t
213GDBRemoteCommunicationClient::GetRemoteMaxPacketSize()
214{
215 if (m_max_packet_size == 0)
216 {
217 GetRemoteQSupported();
218 }
219 return m_max_packet_size;
220}
221
222bool
Greg Clayton1cb64962011-03-24 04:28:38 +0000223GDBRemoteCommunicationClient::QueryNoAckModeSupported ()
Greg Clayton576d8832011-03-22 04:00:09 +0000224{
225 if (m_supports_not_sending_acks == eLazyBoolCalculate)
226 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000227 m_send_acks = true;
Greg Clayton576d8832011-03-22 04:00:09 +0000228 m_supports_not_sending_acks = eLazyBoolNo;
Greg Clayton1cb64962011-03-24 04:28:38 +0000229
Jason Molenda36a216e2014-07-24 01:36:24 +0000230 // This is the first real packet that we'll send in a debug session and it may take a little
231 // longer than normal to receive a reply. Wait at least 6 seconds for a reply to this packet.
232
233 const uint32_t minimum_timeout = 6;
234 uint32_t old_timeout = GetPacketTimeoutInMicroSeconds() / lldb_private::TimeValue::MicroSecPerSec;
235 SetPacketTimeout (std::max (old_timeout, minimum_timeout));
236
Greg Clayton1cb64962011-03-24 04:28:38 +0000237 StringExtractorGDBRemote response;
Jason Molenda36a216e2014-07-24 01:36:24 +0000238 PacketResult packet_send_result = SendPacketAndWaitForResponse("QStartNoAckMode", response, false);
239 SetPacketTimeout (old_timeout);
240
241 if (packet_send_result == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000242 {
243 if (response.IsOKResponse())
Greg Clayton1cb64962011-03-24 04:28:38 +0000244 {
245 m_send_acks = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000246 m_supports_not_sending_acks = eLazyBoolYes;
Greg Clayton1cb64962011-03-24 04:28:38 +0000247 }
Greg Claytonfb909312013-11-23 01:58:15 +0000248 return true;
Greg Clayton576d8832011-03-22 04:00:09 +0000249 }
250 }
Greg Claytonfb909312013-11-23 01:58:15 +0000251 return false;
Greg Clayton576d8832011-03-22 04:00:09 +0000252}
253
254void
Greg Clayton44633992012-04-10 03:22:03 +0000255GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported ()
256{
257 if (m_supports_threads_in_stop_reply == eLazyBoolCalculate)
258 {
259 m_supports_threads_in_stop_reply = eLazyBoolNo;
260
261 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000262 if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response, false) == PacketResult::Success)
Greg Clayton44633992012-04-10 03:22:03 +0000263 {
264 if (response.IsOKResponse())
265 m_supports_threads_in_stop_reply = eLazyBoolYes;
266 }
267 }
268}
269
Jim Inghamcd16df92012-07-20 21:37:13 +0000270bool
271GDBRemoteCommunicationClient::GetVAttachOrWaitSupported ()
272{
273 if (m_attach_or_wait_reply == eLazyBoolCalculate)
274 {
275 m_attach_or_wait_reply = eLazyBoolNo;
276
277 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000278 if (SendPacketAndWaitForResponse("qVAttachOrWaitSupported", response, false) == PacketResult::Success)
Jim Inghamcd16df92012-07-20 21:37:13 +0000279 {
280 if (response.IsOKResponse())
281 m_attach_or_wait_reply = eLazyBoolYes;
282 }
283 }
284 if (m_attach_or_wait_reply == eLazyBoolYes)
285 return true;
286 else
287 return false;
288}
289
Jim Ingham279ceec2012-07-25 21:12:43 +0000290bool
291GDBRemoteCommunicationClient::GetSyncThreadStateSupported ()
292{
293 if (m_prepare_for_reg_writing_reply == eLazyBoolCalculate)
294 {
295 m_prepare_for_reg_writing_reply = eLazyBoolNo;
296
297 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000298 if (SendPacketAndWaitForResponse("qSyncThreadStateSupported", response, false) == PacketResult::Success)
Jim Ingham279ceec2012-07-25 21:12:43 +0000299 {
300 if (response.IsOKResponse())
301 m_prepare_for_reg_writing_reply = eLazyBoolYes;
302 }
303 }
304 if (m_prepare_for_reg_writing_reply == eLazyBoolYes)
305 return true;
306 else
307 return false;
308}
309
Greg Clayton44633992012-04-10 03:22:03 +0000310
311void
Greg Clayton576d8832011-03-22 04:00:09 +0000312GDBRemoteCommunicationClient::ResetDiscoverableSettings()
313{
314 m_supports_not_sending_acks = eLazyBoolCalculate;
315 m_supports_thread_suffix = eLazyBoolCalculate;
Greg Clayton44633992012-04-10 03:22:03 +0000316 m_supports_threads_in_stop_reply = eLazyBoolCalculate;
Greg Clayton576d8832011-03-22 04:00:09 +0000317 m_supports_vCont_c = eLazyBoolCalculate;
318 m_supports_vCont_C = eLazyBoolCalculate;
319 m_supports_vCont_s = eLazyBoolCalculate;
320 m_supports_vCont_S = eLazyBoolCalculate;
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000321 m_supports_p = eLazyBoolCalculate;
Jason Molendabdc4f122014-05-06 02:59:39 +0000322 m_supports_x = eLazyBoolCalculate;
Greg Claytonf74cf862013-11-13 23:28:31 +0000323 m_supports_QSaveRegisterState = eLazyBoolCalculate;
Greg Clayton32e0a752011-03-30 18:16:51 +0000324 m_qHostInfo_is_valid = eLazyBoolCalculate;
Todd Fiala9f72b3a2014-05-07 19:28:21 +0000325 m_curr_pid_is_valid = eLazyBoolCalculate;
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000326 m_qProcessInfo_is_valid = eLazyBoolCalculate;
Jason Molendaa3329782014-03-29 18:54:20 +0000327 m_qGDBServerVersion_is_valid = eLazyBoolCalculate;
Greg Clayton70b57652011-05-15 01:25:55 +0000328 m_supports_alloc_dealloc_memory = eLazyBoolCalculate;
Greg Clayton46fb5582011-11-18 07:03:08 +0000329 m_supports_memory_region_info = eLazyBoolCalculate;
Jim Ingham279ceec2012-07-25 21:12:43 +0000330 m_prepare_for_reg_writing_reply = eLazyBoolCalculate;
331 m_attach_or_wait_reply = eLazyBoolCalculate;
Jason Molendaa3329782014-03-29 18:54:20 +0000332 m_avoid_g_packets = eLazyBoolCalculate;
Steve Pucci03904ac2014-03-04 23:18:46 +0000333 m_supports_qXfer_auxv_read = eLazyBoolCalculate;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000334 m_supports_qXfer_libraries_read = eLazyBoolCalculate;
335 m_supports_qXfer_libraries_svr4_read = eLazyBoolCalculate;
336 m_supports_augmented_libraries_svr4_read = eLazyBoolCalculate;
Greg Clayton2a48f522011-05-14 01:50:35 +0000337
Greg Clayton32e0a752011-03-30 18:16:51 +0000338 m_supports_qProcessInfoPID = true;
339 m_supports_qfProcessInfo = true;
340 m_supports_qUserName = true;
341 m_supports_qGroupName = true;
Greg Clayton8b82f082011-04-12 05:54:46 +0000342 m_supports_qThreadStopInfo = true;
343 m_supports_z0 = true;
344 m_supports_z1 = true;
345 m_supports_z2 = true;
346 m_supports_z3 = true;
347 m_supports_z4 = true;
Greg Clayton89600582013-10-10 17:53:50 +0000348 m_supports_QEnvironment = true;
349 m_supports_QEnvironmentHexEncoded = true;
Jason Molendaa3329782014-03-29 18:54:20 +0000350
Greg Claytond314e812011-03-23 00:09:55 +0000351 m_host_arch.Clear();
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000352 m_process_arch.Clear();
Jason Molendaa3329782014-03-29 18:54:20 +0000353 m_os_version_major = UINT32_MAX;
354 m_os_version_minor = UINT32_MAX;
355 m_os_version_update = UINT32_MAX;
356 m_os_build.clear();
357 m_os_kernel.clear();
358 m_hostname.clear();
359 m_gdb_server_name.clear();
360 m_gdb_server_version = UINT32_MAX;
361 m_default_packet_timeout = 0;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000362
363 m_max_packet_size = 0;
Greg Clayton576d8832011-03-22 04:00:09 +0000364}
365
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000366void
367GDBRemoteCommunicationClient::GetRemoteQSupported ()
368{
369 // Clear out any capabilities we expect to see in the qSupported response
Steve Pucci03904ac2014-03-04 23:18:46 +0000370 m_supports_qXfer_auxv_read = eLazyBoolNo;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000371 m_supports_qXfer_libraries_read = eLazyBoolNo;
Steve Pucci03904ac2014-03-04 23:18:46 +0000372 m_supports_qXfer_libraries_svr4_read = eLazyBoolNo;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000373 m_supports_augmented_libraries_svr4_read = eLazyBoolNo;
374 m_max_packet_size = UINT64_MAX; // It's supposed to always be there, but if not, we assume no limit
375
376 StringExtractorGDBRemote response;
377 if (SendPacketAndWaitForResponse("qSupported",
378 response,
379 /*send_async=*/false) == PacketResult::Success)
380 {
381 const char *response_cstr = response.GetStringRef().c_str();
Steve Pucci03904ac2014-03-04 23:18:46 +0000382 if (::strstr (response_cstr, "qXfer:auxv:read+"))
383 m_supports_qXfer_auxv_read = eLazyBoolYes;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000384 if (::strstr (response_cstr, "qXfer:libraries-svr4:read+"))
385 m_supports_qXfer_libraries_svr4_read = eLazyBoolYes;
386 if (::strstr (response_cstr, "augmented-libraries-svr4-read"))
387 {
388 m_supports_qXfer_libraries_svr4_read = eLazyBoolYes; // implied
389 m_supports_augmented_libraries_svr4_read = eLazyBoolYes;
390 }
391 if (::strstr (response_cstr, "qXfer:libraries:read+"))
392 m_supports_qXfer_libraries_read = eLazyBoolYes;
393
394 const char *packet_size_str = ::strstr (response_cstr, "PacketSize=");
395 if (packet_size_str)
396 {
397 StringExtractorGDBRemote packet_response(packet_size_str + strlen("PacketSize="));
398 m_max_packet_size = packet_response.GetHexMaxU64(/*little_endian=*/false, UINT64_MAX);
399 if (m_max_packet_size == 0)
400 {
401 m_max_packet_size = UINT64_MAX; // Must have been a garbled response
402 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
403 if (log)
404 log->Printf ("Garbled PacketSize spec in qSupported response");
405 }
406 }
407 }
408}
Greg Clayton576d8832011-03-22 04:00:09 +0000409
410bool
411GDBRemoteCommunicationClient::GetThreadSuffixSupported ()
412{
413 if (m_supports_thread_suffix == eLazyBoolCalculate)
414 {
415 StringExtractorGDBRemote response;
416 m_supports_thread_suffix = eLazyBoolNo;
Greg Clayton3dedae12013-12-06 21:45:27 +0000417 if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000418 {
419 if (response.IsOKResponse())
420 m_supports_thread_suffix = eLazyBoolYes;
421 }
422 }
423 return m_supports_thread_suffix;
424}
425bool
426GDBRemoteCommunicationClient::GetVContSupported (char flavor)
427{
428 if (m_supports_vCont_c == eLazyBoolCalculate)
429 {
430 StringExtractorGDBRemote response;
431 m_supports_vCont_any = eLazyBoolNo;
432 m_supports_vCont_all = eLazyBoolNo;
433 m_supports_vCont_c = eLazyBoolNo;
434 m_supports_vCont_C = eLazyBoolNo;
435 m_supports_vCont_s = eLazyBoolNo;
436 m_supports_vCont_S = eLazyBoolNo;
Greg Clayton3dedae12013-12-06 21:45:27 +0000437 if (SendPacketAndWaitForResponse("vCont?", response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000438 {
439 const char *response_cstr = response.GetStringRef().c_str();
440 if (::strstr (response_cstr, ";c"))
441 m_supports_vCont_c = eLazyBoolYes;
442
443 if (::strstr (response_cstr, ";C"))
444 m_supports_vCont_C = eLazyBoolYes;
445
446 if (::strstr (response_cstr, ";s"))
447 m_supports_vCont_s = eLazyBoolYes;
448
449 if (::strstr (response_cstr, ";S"))
450 m_supports_vCont_S = eLazyBoolYes;
451
452 if (m_supports_vCont_c == eLazyBoolYes &&
453 m_supports_vCont_C == eLazyBoolYes &&
454 m_supports_vCont_s == eLazyBoolYes &&
455 m_supports_vCont_S == eLazyBoolYes)
456 {
457 m_supports_vCont_all = eLazyBoolYes;
458 }
459
460 if (m_supports_vCont_c == eLazyBoolYes ||
461 m_supports_vCont_C == eLazyBoolYes ||
462 m_supports_vCont_s == eLazyBoolYes ||
463 m_supports_vCont_S == eLazyBoolYes)
464 {
465 m_supports_vCont_any = eLazyBoolYes;
466 }
467 }
468 }
469
470 switch (flavor)
471 {
472 case 'a': return m_supports_vCont_any;
473 case 'A': return m_supports_vCont_all;
474 case 'c': return m_supports_vCont_c;
475 case 'C': return m_supports_vCont_C;
476 case 's': return m_supports_vCont_s;
477 case 'S': return m_supports_vCont_S;
478 default: break;
479 }
480 return false;
481}
482
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000483// Check if the target supports 'p' packet. It sends out a 'p'
484// packet and checks the response. A normal packet will tell us
485// that support is available.
Sean Callananb1de1142013-09-04 23:24:15 +0000486//
487// Takes a valid thread ID because p needs to apply to a thread.
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000488bool
Sean Callananb1de1142013-09-04 23:24:15 +0000489GDBRemoteCommunicationClient::GetpPacketSupported (lldb::tid_t tid)
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000490{
491 if (m_supports_p == eLazyBoolCalculate)
492 {
493 StringExtractorGDBRemote response;
494 m_supports_p = eLazyBoolNo;
Sean Callananb1de1142013-09-04 23:24:15 +0000495 char packet[256];
496 if (GetThreadSuffixSupported())
497 snprintf(packet, sizeof(packet), "p0;thread:%" PRIx64 ";", tid);
498 else
499 snprintf(packet, sizeof(packet), "p0");
500
Greg Clayton3dedae12013-12-06 21:45:27 +0000501 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000502 {
503 if (response.IsNormalResponse())
504 m_supports_p = eLazyBoolYes;
505 }
506 }
507 return m_supports_p;
508}
Greg Clayton576d8832011-03-22 04:00:09 +0000509
Jason Molendabdc4f122014-05-06 02:59:39 +0000510bool
Jason Molenda705b1802014-06-13 02:37:02 +0000511GDBRemoteCommunicationClient::GetThreadExtendedInfoSupported ()
512{
513 if (m_supports_jThreadExtendedInfo == eLazyBoolCalculate)
514 {
515 StringExtractorGDBRemote response;
516 m_supports_jThreadExtendedInfo = eLazyBoolNo;
517 if (SendPacketAndWaitForResponse("jThreadExtendedInfo:", response, false) == PacketResult::Success)
518 {
519 if (response.IsOKResponse())
520 {
521 m_supports_jThreadExtendedInfo = eLazyBoolYes;
522 }
523 }
524 }
525 return m_supports_jThreadExtendedInfo;
526}
527
528bool
Jason Molendabdc4f122014-05-06 02:59:39 +0000529GDBRemoteCommunicationClient::GetxPacketSupported ()
530{
531 if (m_supports_x == eLazyBoolCalculate)
532 {
533 StringExtractorGDBRemote response;
534 m_supports_x = eLazyBoolNo;
535 char packet[256];
536 snprintf (packet, sizeof (packet), "x0,0");
537 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
538 {
539 if (response.IsOKResponse())
540 m_supports_x = eLazyBoolYes;
541 }
542 }
543 return m_supports_x;
544}
545
Greg Clayton3dedae12013-12-06 21:45:27 +0000546GDBRemoteCommunicationClient::PacketResult
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000547GDBRemoteCommunicationClient::SendPacketsAndConcatenateResponses
548(
549 const char *payload_prefix,
550 std::string &response_string
551)
552{
553 Mutex::Locker locker;
554 if (!GetSequenceMutex(locker,
555 "ProcessGDBRemote::SendPacketsAndConcatenateResponses() failed due to not getting the sequence mutex"))
556 {
557 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
558 if (log)
559 log->Printf("error: failed to get packet sequence mutex, not sending packets with prefix '%s'",
560 payload_prefix);
561 return PacketResult::ErrorNoSequenceLock;
562 }
563
564 response_string = "";
565 std::string payload_prefix_str(payload_prefix);
566 unsigned int response_size = 0x1000;
567 if (response_size > GetRemoteMaxPacketSize()) { // May send qSupported packet
568 response_size = GetRemoteMaxPacketSize();
569 }
570
571 for (unsigned int offset = 0; true; offset += response_size)
572 {
573 StringExtractorGDBRemote this_response;
574 // Construct payload
575 char sizeDescriptor[128];
576 snprintf(sizeDescriptor, sizeof(sizeDescriptor), "%x,%x", offset, response_size);
577 PacketResult result = SendPacketAndWaitForResponse((payload_prefix_str + sizeDescriptor).c_str(),
578 this_response,
579 /*send_async=*/false);
580 if (result != PacketResult::Success)
581 return result;
582
583 const std::string &this_string = this_response.GetStringRef();
584
585 // Check for m or l as first character; l seems to mean this is the last chunk
586 char first_char = *this_string.c_str();
587 if (first_char != 'm' && first_char != 'l')
588 {
589 return PacketResult::ErrorReplyInvalid;
590 }
Steve Pucci03904ac2014-03-04 23:18:46 +0000591 // Concatenate the result so far (skipping 'm' or 'l')
592 response_string.append(this_string, 1, std::string::npos);
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000593 if (first_char == 'l')
594 // We're done
595 return PacketResult::Success;
596 }
597}
598
599GDBRemoteCommunicationClient::PacketResult
Greg Clayton576d8832011-03-22 04:00:09 +0000600GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
601(
602 const char *payload,
603 StringExtractorGDBRemote &response,
604 bool send_async
605)
606{
607 return SendPacketAndWaitForResponse (payload,
608 ::strlen (payload),
609 response,
610 send_async);
611}
612
Greg Clayton3dedae12013-12-06 21:45:27 +0000613GDBRemoteCommunicationClient::PacketResult
614GDBRemoteCommunicationClient::SendPacketAndWaitForResponseNoLock (const char *payload,
615 size_t payload_length,
616 StringExtractorGDBRemote &response)
617{
618 PacketResult packet_result = SendPacketNoLock (payload, payload_length);
619 if (packet_result == PacketResult::Success)
620 packet_result = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
621 return packet_result;
622}
623
624GDBRemoteCommunicationClient::PacketResult
Greg Clayton576d8832011-03-22 04:00:09 +0000625GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
626(
627 const char *payload,
628 size_t payload_length,
629 StringExtractorGDBRemote &response,
630 bool send_async
631)
632{
Greg Clayton3dedae12013-12-06 21:45:27 +0000633 PacketResult packet_result = PacketResult::ErrorSendFailed;
Greg Clayton576d8832011-03-22 04:00:09 +0000634 Mutex::Locker locker;
Greg Clayton5160ce52013-03-27 23:08:40 +0000635 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000636 if (GetSequenceMutex (locker))
Greg Clayton576d8832011-03-22 04:00:09 +0000637 {
Greg Clayton3dedae12013-12-06 21:45:27 +0000638 packet_result = SendPacketAndWaitForResponseNoLock (payload, payload_length, response);
Greg Clayton576d8832011-03-22 04:00:09 +0000639 }
640 else
641 {
642 if (send_async)
643 {
Greg Claytond3544052012-05-31 21:24:20 +0000644 if (IsRunning())
Greg Clayton576d8832011-03-22 04:00:09 +0000645 {
Greg Claytond3544052012-05-31 21:24:20 +0000646 Mutex::Locker async_locker (m_async_mutex);
647 m_async_packet.assign(payload, payload_length);
648 m_async_packet_predicate.SetValue (true, eBroadcastNever);
649
650 if (log)
651 log->Printf ("async: async packet = %s", m_async_packet.c_str());
652
653 bool timed_out = false;
654 if (SendInterrupt(locker, 2, timed_out))
Greg Clayton576d8832011-03-22 04:00:09 +0000655 {
Greg Claytond3544052012-05-31 21:24:20 +0000656 if (m_interrupt_sent)
Greg Clayton576d8832011-03-22 04:00:09 +0000657 {
Jim Inghambabfc382012-06-06 00:32:39 +0000658 m_interrupt_sent = false;
Greg Claytond3544052012-05-31 21:24:20 +0000659 TimeValue timeout_time;
660 timeout_time = TimeValue::Now();
661 timeout_time.OffsetWithSeconds (m_packet_timeout);
662
Greg Clayton576d8832011-03-22 04:00:09 +0000663 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000664 log->Printf ("async: sent interrupt");
Greg Clayton644247c2011-07-07 01:59:51 +0000665
Greg Claytond3544052012-05-31 21:24:20 +0000666 if (m_async_packet_predicate.WaitForValueEqualTo (false, &timeout_time, &timed_out))
Greg Claytone889ad62011-10-27 22:04:16 +0000667 {
Greg Claytond3544052012-05-31 21:24:20 +0000668 if (log)
669 log->Printf ("async: got response");
670
671 // Swap the response buffer to avoid malloc and string copy
672 response.GetStringRef().swap (m_async_response.GetStringRef());
Jim Inghama6195b72013-12-18 01:24:33 +0000673 packet_result = m_async_result;
Greg Claytond3544052012-05-31 21:24:20 +0000674 }
675 else
676 {
677 if (log)
678 log->Printf ("async: timed out waiting for response");
679 }
680
681 // Make sure we wait until the continue packet has been sent again...
682 if (m_private_is_running.WaitForValueEqualTo (true, &timeout_time, &timed_out))
683 {
684 if (log)
685 {
686 if (timed_out)
687 log->Printf ("async: timed out waiting for process to resume, but process was resumed");
688 else
689 log->Printf ("async: async packet sent");
690 }
691 }
692 else
693 {
694 if (log)
695 log->Printf ("async: timed out waiting for process to resume");
Greg Claytone889ad62011-10-27 22:04:16 +0000696 }
697 }
698 else
699 {
Greg Claytond3544052012-05-31 21:24:20 +0000700 // We had a racy condition where we went to send the interrupt
701 // yet we were able to get the lock, so the process must have
702 // just stopped?
Greg Clayton576d8832011-03-22 04:00:09 +0000703 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000704 log->Printf ("async: got lock without sending interrupt");
705 // Send the packet normally since we got the lock
Greg Clayton3dedae12013-12-06 21:45:27 +0000706 packet_result = SendPacketAndWaitForResponseNoLock (payload, payload_length, response);
Greg Clayton576d8832011-03-22 04:00:09 +0000707 }
708 }
709 else
710 {
Greg Clayton644247c2011-07-07 01:59:51 +0000711 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000712 log->Printf ("async: failed to interrupt");
Greg Clayton576d8832011-03-22 04:00:09 +0000713 }
714 }
715 else
716 {
717 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000718 log->Printf ("async: not running, async is ignored");
Greg Clayton576d8832011-03-22 04:00:09 +0000719 }
720 }
721 else
722 {
723 if (log)
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000724 log->Printf("error: failed to get packet sequence mutex, not sending packet '%*s'", (int) payload_length, payload);
Greg Clayton576d8832011-03-22 04:00:09 +0000725 }
726 }
Greg Clayton3dedae12013-12-06 21:45:27 +0000727 return packet_result;
Greg Clayton576d8832011-03-22 04:00:09 +0000728}
729
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000730static const char *end_delimiter = "--end--;";
731static const int end_delimiter_len = 8;
732
733std::string
734GDBRemoteCommunicationClient::HarmonizeThreadIdsForProfileData
735( ProcessGDBRemote *process,
736 StringExtractorGDBRemote& profileDataExtractor
737)
738{
739 std::map<uint64_t, uint32_t> new_thread_id_to_used_usec_map;
740 std::stringstream final_output;
741 std::string name, value;
742
743 // Going to assuming thread_used_usec comes first, else bail out.
744 while (profileDataExtractor.GetNameColonValue(name, value))
745 {
746 if (name.compare("thread_used_id") == 0)
747 {
748 StringExtractor threadIDHexExtractor(value.c_str());
749 uint64_t thread_id = threadIDHexExtractor.GetHexMaxU64(false, 0);
750
751 bool has_used_usec = false;
752 uint32_t curr_used_usec = 0;
753 std::string usec_name, usec_value;
754 uint32_t input_file_pos = profileDataExtractor.GetFilePos();
755 if (profileDataExtractor.GetNameColonValue(usec_name, usec_value))
756 {
757 if (usec_name.compare("thread_used_usec") == 0)
758 {
759 has_used_usec = true;
760 curr_used_usec = strtoull(usec_value.c_str(), NULL, 0);
761 }
762 else
763 {
764 // We didn't find what we want, it is probably
765 // an older version. Bail out.
766 profileDataExtractor.SetFilePos(input_file_pos);
767 }
768 }
769
770 if (has_used_usec)
771 {
772 uint32_t prev_used_usec = 0;
773 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_used_usec_map.find(thread_id);
774 if (iterator != m_thread_id_to_used_usec_map.end())
775 {
776 prev_used_usec = m_thread_id_to_used_usec_map[thread_id];
777 }
778
779 uint32_t real_used_usec = curr_used_usec - prev_used_usec;
780 // A good first time record is one that runs for at least 0.25 sec
781 bool good_first_time = (prev_used_usec == 0) && (real_used_usec > 250000);
782 bool good_subsequent_time = (prev_used_usec > 0) &&
783 ((real_used_usec > 0) || (process->HasAssignedIndexIDToThread(thread_id)));
784
785 if (good_first_time || good_subsequent_time)
786 {
787 // We try to avoid doing too many index id reservation,
788 // resulting in fast increase of index ids.
789
790 final_output << name << ":";
791 int32_t index_id = process->AssignIndexIDToThread(thread_id);
792 final_output << index_id << ";";
793
794 final_output << usec_name << ":" << usec_value << ";";
795 }
796 else
797 {
798 // Skip past 'thread_used_name'.
799 std::string local_name, local_value;
800 profileDataExtractor.GetNameColonValue(local_name, local_value);
801 }
802
803 // Store current time as previous time so that they can be compared later.
804 new_thread_id_to_used_usec_map[thread_id] = curr_used_usec;
805 }
806 else
807 {
808 // Bail out and use old string.
809 final_output << name << ":" << value << ";";
810 }
811 }
812 else
813 {
814 final_output << name << ":" << value << ";";
815 }
816 }
817 final_output << end_delimiter;
818 m_thread_id_to_used_usec_map = new_thread_id_to_used_usec_map;
819
820 return final_output.str();
821}
822
Greg Clayton576d8832011-03-22 04:00:09 +0000823StateType
824GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse
825(
826 ProcessGDBRemote *process,
827 const char *payload,
828 size_t packet_length,
829 StringExtractorGDBRemote &response
830)
831{
Greg Clayton1f5181a2012-07-02 22:05:25 +0000832 m_curr_tid = LLDB_INVALID_THREAD_ID;
Greg Clayton5160ce52013-03-27 23:08:40 +0000833 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton576d8832011-03-22 04:00:09 +0000834 if (log)
835 log->Printf ("GDBRemoteCommunicationClient::%s ()", __FUNCTION__);
836
837 Mutex::Locker locker(m_sequence_mutex);
838 StateType state = eStateRunning;
839
840 BroadcastEvent(eBroadcastBitRunPacketSent, NULL);
841 m_public_is_running.SetValue (true, eBroadcastNever);
842 // Set the starting continue packet into "continue_packet". This packet
Jim Inghambabfc382012-06-06 00:32:39 +0000843 // may change if we are interrupted and we continue after an async packet...
Greg Clayton576d8832011-03-22 04:00:09 +0000844 std::string continue_packet(payload, packet_length);
845
Greg Clayton3f875c52013-02-22 22:23:55 +0000846 bool got_async_packet = false;
Greg Claytonaf247d72011-05-19 03:54:16 +0000847
Greg Clayton576d8832011-03-22 04:00:09 +0000848 while (state == eStateRunning)
849 {
Greg Clayton3f875c52013-02-22 22:23:55 +0000850 if (!got_async_packet)
Greg Claytonaf247d72011-05-19 03:54:16 +0000851 {
852 if (log)
853 log->Printf ("GDBRemoteCommunicationClient::%s () sending continue packet: %s", __FUNCTION__, continue_packet.c_str());
Greg Clayton3dedae12013-12-06 21:45:27 +0000854 if (SendPacketNoLock(continue_packet.c_str(), continue_packet.size()) != PacketResult::Success)
Greg Claytonaf247d72011-05-19 03:54:16 +0000855 state = eStateInvalid;
Jim Inghamb8cd5752014-04-16 02:24:17 +0000856 else
857 m_interrupt_sent = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000858
Greg Claytone889ad62011-10-27 22:04:16 +0000859 m_private_is_running.SetValue (true, eBroadcastAlways);
Greg Claytonaf247d72011-05-19 03:54:16 +0000860 }
861
Greg Clayton3f875c52013-02-22 22:23:55 +0000862 got_async_packet = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000863
864 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000865 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(%s)", __FUNCTION__, continue_packet.c_str());
Greg Clayton576d8832011-03-22 04:00:09 +0000866
Greg Clayton3dedae12013-12-06 21:45:27 +0000867 if (WaitForPacketWithTimeoutMicroSecondsNoLock(response, UINT32_MAX) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000868 {
869 if (response.Empty())
870 state = eStateInvalid;
871 else
872 {
873 const char stop_type = response.GetChar();
874 if (log)
875 log->Printf ("GDBRemoteCommunicationClient::%s () got packet: %s", __FUNCTION__, response.GetStringRef().c_str());
876 switch (stop_type)
877 {
878 case 'T':
879 case 'S':
Greg Clayton576d8832011-03-22 04:00:09 +0000880 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000881 if (process->GetStopID() == 0)
Greg Clayton576d8832011-03-22 04:00:09 +0000882 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000883 if (process->GetID() == LLDB_INVALID_PROCESS_ID)
884 {
885 lldb::pid_t pid = GetCurrentProcessID ();
886 if (pid != LLDB_INVALID_PROCESS_ID)
887 process->SetID (pid);
888 }
889 process->BuildDynamicRegisterInfo (true);
Greg Clayton576d8832011-03-22 04:00:09 +0000890 }
Greg Clayton2687cd12012-03-29 01:55:41 +0000891
892 // Privately notify any internal threads that we have stopped
893 // in case we wanted to interrupt our process, yet we might
894 // send a packet and continue without returning control to the
895 // user.
896 m_private_is_running.SetValue (false, eBroadcastAlways);
897
898 const uint8_t signo = response.GetHexU8 (UINT8_MAX);
899
Jim Inghambabfc382012-06-06 00:32:39 +0000900 bool continue_after_async = m_async_signal != -1 || m_async_packet_predicate.GetValue();
901 if (continue_after_async || m_interrupt_sent)
Greg Clayton2687cd12012-03-29 01:55:41 +0000902 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000903 // We sent an interrupt packet to stop the inferior process
904 // for an async signal or to send an async packet while running
905 // but we might have been single stepping and received the
906 // stop packet for the step instead of for the interrupt packet.
907 // Typically when an interrupt is sent a SIGINT or SIGSTOP
908 // is used, so if we get anything else, we need to try and
909 // get another stop reply packet that may have been sent
910 // due to sending the interrupt when the target is stopped
911 // which will just re-send a copy of the last stop reply
912 // packet. If we don't do this, then the reply for our
913 // async packet will be the repeat stop reply packet and cause
914 // a lot of trouble for us!
915 if (signo != SIGINT && signo != SIGSTOP)
916 {
Greg Claytonfb72fde2012-05-15 02:50:49 +0000917 continue_after_async = false;
Greg Clayton2687cd12012-03-29 01:55:41 +0000918
919 // We didn't get a a SIGINT or SIGSTOP, so try for a
920 // very brief time (1 ms) to get another stop reply
921 // packet to make sure it doesn't get in the way
922 StringExtractorGDBRemote extra_stop_reply_packet;
923 uint32_t timeout_usec = 1000;
Greg Clayton3dedae12013-12-06 21:45:27 +0000924 if (WaitForPacketWithTimeoutMicroSecondsNoLock (extra_stop_reply_packet, timeout_usec) == PacketResult::Success)
Greg Clayton2687cd12012-03-29 01:55:41 +0000925 {
926 switch (extra_stop_reply_packet.GetChar())
927 {
928 case 'T':
929 case 'S':
930 // We did get an extra stop reply, which means
931 // our interrupt didn't stop the target so we
932 // shouldn't continue after the async signal
933 // or packet is sent...
Greg Claytonfb72fde2012-05-15 02:50:49 +0000934 continue_after_async = false;
Greg Clayton2687cd12012-03-29 01:55:41 +0000935 break;
936 }
937 }
938 }
939 }
940
941 if (m_async_signal != -1)
942 {
943 if (log)
944 log->Printf ("async: send signo = %s", Host::GetSignalAsCString (m_async_signal));
945
946 // Save off the async signal we are supposed to send
947 const int async_signal = m_async_signal;
948 // Clear the async signal member so we don't end up
949 // sending the signal multiple times...
950 m_async_signal = -1;
951 // Check which signal we stopped with
952 if (signo == async_signal)
953 {
954 if (log)
955 log->Printf ("async: stopped with signal %s, we are done running", Host::GetSignalAsCString (signo));
956
957 // We already stopped with a signal that we wanted
958 // to stop with, so we are done
959 }
960 else
961 {
962 // We stopped with a different signal that the one
963 // we wanted to stop with, so now we must resume
964 // with the signal we want
965 char signal_packet[32];
966 int signal_packet_len = 0;
967 signal_packet_len = ::snprintf (signal_packet,
968 sizeof (signal_packet),
969 "C%2.2x",
970 async_signal);
971
972 if (log)
973 log->Printf ("async: stopped with signal %s, resume with %s",
974 Host::GetSignalAsCString (signo),
975 Host::GetSignalAsCString (async_signal));
976
977 // Set the continue packet to resume even if the
Greg Claytonfb72fde2012-05-15 02:50:49 +0000978 // interrupt didn't cause our stop (ignore continue_after_async)
Greg Clayton2687cd12012-03-29 01:55:41 +0000979 continue_packet.assign(signal_packet, signal_packet_len);
980 continue;
981 }
982 }
983 else if (m_async_packet_predicate.GetValue())
984 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000985 Log * packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Greg Clayton2687cd12012-03-29 01:55:41 +0000986
987 // We are supposed to send an asynchronous packet while
Jim Inghama6195b72013-12-18 01:24:33 +0000988 // we are running.
Greg Clayton2687cd12012-03-29 01:55:41 +0000989 m_async_response.Clear();
990 if (m_async_packet.empty())
991 {
Jim Inghama6195b72013-12-18 01:24:33 +0000992 m_async_result = PacketResult::ErrorSendFailed;
993 if (packet_log)
Greg Clayton2687cd12012-03-29 01:55:41 +0000994 packet_log->Printf ("async: error: empty async packet");
995
996 }
997 else
998 {
999 if (packet_log)
1000 packet_log->Printf ("async: sending packet");
1001
Jim Inghama6195b72013-12-18 01:24:33 +00001002 m_async_result = SendPacketAndWaitForResponse (&m_async_packet[0],
1003 m_async_packet.size(),
1004 m_async_response,
1005 false);
Greg Clayton2687cd12012-03-29 01:55:41 +00001006 }
1007 // Let the other thread that was trying to send the async
1008 // packet know that the packet has been sent and response is
1009 // ready...
1010 m_async_packet_predicate.SetValue(false, eBroadcastAlways);
1011
1012 if (packet_log)
Greg Claytonfb72fde2012-05-15 02:50:49 +00001013 packet_log->Printf ("async: sent packet, continue_after_async = %i", continue_after_async);
Greg Clayton2687cd12012-03-29 01:55:41 +00001014
1015 // Set the continue packet to resume if our interrupt
1016 // for the async packet did cause the stop
Greg Claytonfb72fde2012-05-15 02:50:49 +00001017 if (continue_after_async)
Greg Clayton2687cd12012-03-29 01:55:41 +00001018 {
Greg Claytonf1186de2012-05-24 23:42:14 +00001019 // Reverting this for now as it is causing deadlocks
1020 // in programs (<rdar://problem/11529853>). In the future
1021 // we should check our thread list and "do the right thing"
1022 // for new threads that show up while we stop and run async
1023 // packets. Setting the packet to 'c' to continue all threads
1024 // is the right thing to do 99.99% of the time because if a
1025 // thread was single stepping, and we sent an interrupt, we
1026 // will notice above that we didn't stop due to an interrupt
1027 // but stopped due to stepping and we would _not_ continue.
1028 continue_packet.assign (1, 'c');
Greg Clayton2687cd12012-03-29 01:55:41 +00001029 continue;
1030 }
1031 }
1032 // Stop with signal and thread info
1033 state = eStateStopped;
Greg Clayton576d8832011-03-22 04:00:09 +00001034 }
Greg Clayton576d8832011-03-22 04:00:09 +00001035 break;
1036
1037 case 'W':
1038 case 'X':
1039 // process exited
1040 state = eStateExited;
1041 break;
1042
1043 case 'O':
1044 // STDOUT
1045 {
Greg Clayton3f875c52013-02-22 22:23:55 +00001046 got_async_packet = true;
Greg Clayton576d8832011-03-22 04:00:09 +00001047 std::string inferior_stdout;
1048 inferior_stdout.reserve(response.GetBytesLeft () / 2);
1049 char ch;
1050 while ((ch = response.GetHexU8()) != '\0')
1051 inferior_stdout.append(1, ch);
1052 process->AppendSTDOUT (inferior_stdout.c_str(), inferior_stdout.size());
1053 }
1054 break;
1055
Han Ming Ongab3b8b22012-11-17 00:21:04 +00001056 case 'A':
1057 // Async miscellaneous reply. Right now, only profile data is coming through this channel.
1058 {
Greg Clayton3f875c52013-02-22 22:23:55 +00001059 got_async_packet = true;
Han Ming Ong4b6459f2013-01-18 23:11:53 +00001060 std::string input = response.GetStringRef().substr(1); // '1' to move beyond 'A'
1061 if (m_partial_profile_data.length() > 0)
1062 {
1063 m_partial_profile_data.append(input);
1064 input = m_partial_profile_data;
1065 m_partial_profile_data.clear();
1066 }
1067
1068 size_t found, pos = 0, len = input.length();
1069 while ((found = input.find(end_delimiter, pos)) != std::string::npos)
1070 {
1071 StringExtractorGDBRemote profileDataExtractor(input.substr(pos, found).c_str());
Han Ming Ong91ed6b82013-06-24 18:15:05 +00001072 std::string profile_data = HarmonizeThreadIdsForProfileData(process, profileDataExtractor);
1073 process->BroadcastAsyncProfileData (profile_data);
Han Ming Ong4b6459f2013-01-18 23:11:53 +00001074
1075 pos = found + end_delimiter_len;
1076 }
1077
1078 if (pos < len)
1079 {
1080 // Last incomplete chunk.
1081 m_partial_profile_data = input.substr(pos);
1082 }
Han Ming Ongab3b8b22012-11-17 00:21:04 +00001083 }
1084 break;
1085
Greg Clayton576d8832011-03-22 04:00:09 +00001086 case 'E':
1087 // ERROR
1088 state = eStateInvalid;
1089 break;
1090
1091 default:
1092 if (log)
1093 log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__);
1094 state = eStateInvalid;
1095 break;
1096 }
1097 }
1098 }
1099 else
1100 {
1101 if (log)
1102 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__);
1103 state = eStateInvalid;
1104 }
1105 }
1106 if (log)
1107 log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state));
1108 response.SetFilePos(0);
1109 m_private_is_running.SetValue (false, eBroadcastAlways);
1110 m_public_is_running.SetValue (false, eBroadcastAlways);
1111 return state;
1112}
1113
1114bool
1115GDBRemoteCommunicationClient::SendAsyncSignal (int signo)
1116{
Greg Clayton2687cd12012-03-29 01:55:41 +00001117 Mutex::Locker async_locker (m_async_mutex);
Greg Clayton576d8832011-03-22 04:00:09 +00001118 m_async_signal = signo;
1119 bool timed_out = false;
Greg Clayton576d8832011-03-22 04:00:09 +00001120 Mutex::Locker locker;
Greg Clayton2687cd12012-03-29 01:55:41 +00001121 if (SendInterrupt (locker, 1, timed_out))
Greg Clayton576d8832011-03-22 04:00:09 +00001122 return true;
1123 m_async_signal = -1;
1124 return false;
1125}
1126
Greg Clayton37a0a242012-04-11 00:24:49 +00001127// This function takes a mutex locker as a parameter in case the GetSequenceMutex
Greg Clayton576d8832011-03-22 04:00:09 +00001128// actually succeeds. If it doesn't succeed in acquiring the sequence mutex
1129// (the expected result), then it will send the halt packet. If it does succeed
1130// then the caller that requested the interrupt will want to keep the sequence
1131// locked down so that no one else can send packets while the caller has control.
1132// This function usually gets called when we are running and need to stop the
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001133// target. It can also be used when we are running and we need to do something
Greg Clayton576d8832011-03-22 04:00:09 +00001134// else (like read/write memory), so we need to interrupt the running process
1135// (gdb remote protocol requires this), and do what we need to do, then resume.
1136
1137bool
Greg Clayton2687cd12012-03-29 01:55:41 +00001138GDBRemoteCommunicationClient::SendInterrupt
Greg Clayton576d8832011-03-22 04:00:09 +00001139(
1140 Mutex::Locker& locker,
1141 uint32_t seconds_to_wait_for_stop,
Greg Clayton576d8832011-03-22 04:00:09 +00001142 bool &timed_out
1143)
1144{
Greg Clayton576d8832011-03-22 04:00:09 +00001145 timed_out = false;
Greg Clayton5160ce52013-03-27 23:08:40 +00001146 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Clayton576d8832011-03-22 04:00:09 +00001147
1148 if (IsRunning())
1149 {
1150 // Only send an interrupt if our debugserver is running...
Greg Claytonc3c0b0e2012-04-12 19:04:34 +00001151 if (GetSequenceMutex (locker))
Greg Clayton37a0a242012-04-11 00:24:49 +00001152 {
1153 if (log)
1154 log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt");
1155 }
1156 else
Greg Clayton576d8832011-03-22 04:00:09 +00001157 {
1158 // Someone has the mutex locked waiting for a response or for the
1159 // inferior to stop, so send the interrupt on the down low...
1160 char ctrl_c = '\x03';
1161 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton576d8832011-03-22 04:00:09 +00001162 size_t bytes_written = Write (&ctrl_c, 1, status, NULL);
Greg Clayton2687cd12012-03-29 01:55:41 +00001163 if (log)
1164 log->PutCString("send packet: \\x03");
Greg Clayton576d8832011-03-22 04:00:09 +00001165 if (bytes_written > 0)
1166 {
Greg Clayton2687cd12012-03-29 01:55:41 +00001167 m_interrupt_sent = true;
Greg Clayton576d8832011-03-22 04:00:09 +00001168 if (seconds_to_wait_for_stop)
1169 {
Greg Clayton2687cd12012-03-29 01:55:41 +00001170 TimeValue timeout;
1171 if (seconds_to_wait_for_stop)
1172 {
1173 timeout = TimeValue::Now();
1174 timeout.OffsetWithSeconds (seconds_to_wait_for_stop);
1175 }
Greg Clayton576d8832011-03-22 04:00:09 +00001176 if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out))
1177 {
1178 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +00001179 log->PutCString ("SendInterrupt () - sent interrupt, private state stopped");
Greg Clayton576d8832011-03-22 04:00:09 +00001180 return true;
1181 }
1182 else
1183 {
1184 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +00001185 log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume");
Greg Clayton576d8832011-03-22 04:00:09 +00001186 }
1187 }
1188 else
1189 {
1190 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +00001191 log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop...");
Greg Clayton576d8832011-03-22 04:00:09 +00001192 return true;
1193 }
1194 }
1195 else
1196 {
1197 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +00001198 log->Printf ("SendInterrupt () - failed to write interrupt");
Greg Clayton576d8832011-03-22 04:00:09 +00001199 }
1200 return false;
1201 }
Greg Clayton576d8832011-03-22 04:00:09 +00001202 }
Greg Clayton2687cd12012-03-29 01:55:41 +00001203 else
1204 {
1205 if (log)
1206 log->Printf ("SendInterrupt () - not running");
1207 }
Greg Clayton576d8832011-03-22 04:00:09 +00001208 return true;
1209}
1210
1211lldb::pid_t
1212GDBRemoteCommunicationClient::GetCurrentProcessID ()
1213{
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001214 if (m_curr_pid_is_valid == eLazyBoolYes)
1215 return m_curr_pid;
1216
1217 // First try to retrieve the pid via the qProcessInfo request.
1218 GetCurrentProcessInfo ();
1219 if (m_curr_pid_is_valid == eLazyBoolYes)
Greg Clayton576d8832011-03-22 04:00:09 +00001220 {
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001221 // We really got it.
1222 return m_curr_pid;
Greg Clayton576d8832011-03-22 04:00:09 +00001223 }
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001224 else
1225 {
Todd Fialae24614f2014-05-14 00:15:32 +00001226 // If we don't get a response for qProcessInfo, check if $qC gives us a result.
1227 // $qC only returns a real process id on older debugserver and lldb-platform stubs.
1228 // The gdb remote protocol documents $qC as returning the thread id, which newer
1229 // debugserver and lldb-gdbserver stubs return correctly.
1230 StringExtractorGDBRemote response;
1231 if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false) == PacketResult::Success)
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001232 {
Todd Fialae24614f2014-05-14 00:15:32 +00001233 if (response.GetChar() == 'Q')
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001234 {
Todd Fialae24614f2014-05-14 00:15:32 +00001235 if (response.GetChar() == 'C')
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001236 {
Todd Fialae24614f2014-05-14 00:15:32 +00001237 m_curr_pid = response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID);
1238 if (m_curr_pid != LLDB_INVALID_PROCESS_ID)
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001239 {
Todd Fialae24614f2014-05-14 00:15:32 +00001240 m_curr_pid_is_valid = eLazyBoolYes;
1241 return m_curr_pid;
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001242 }
1243 }
1244 }
1245 }
1246 }
1247
Greg Clayton576d8832011-03-22 04:00:09 +00001248 return LLDB_INVALID_PROCESS_ID;
1249}
1250
1251bool
1252GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str)
1253{
1254 error_str.clear();
1255 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001256 if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001257 {
1258 if (response.IsOKResponse())
1259 return true;
1260 if (response.GetChar() == 'E')
1261 {
1262 // A string the describes what failed when launching...
1263 error_str = response.GetStringRef().substr(1);
1264 }
1265 else
1266 {
1267 error_str.assign ("unknown error occurred launching process");
1268 }
1269 }
1270 else
1271 {
Jim Ingham98d6da52012-06-28 20:30:23 +00001272 error_str.assign ("timed out waiting for app to launch");
Greg Clayton576d8832011-03-22 04:00:09 +00001273 }
1274 return false;
1275}
1276
1277int
Greg Claytonfbb76342013-11-20 21:07:01 +00001278GDBRemoteCommunicationClient::SendArgumentsPacket (const ProcessLaunchInfo &launch_info)
Greg Clayton576d8832011-03-22 04:00:09 +00001279{
Greg Claytonfbb76342013-11-20 21:07:01 +00001280 // Since we don't get the send argv0 separate from the executable path, we need to
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001281 // make sure to use the actual executable path found in the launch_info...
Greg Claytonfbb76342013-11-20 21:07:01 +00001282 std::vector<const char *> argv;
1283 FileSpec exe_file = launch_info.GetExecutableFile();
1284 std::string exe_path;
1285 const char *arg = NULL;
1286 const Args &launch_args = launch_info.GetArguments();
1287 if (exe_file)
1288 exe_path = exe_file.GetPath();
1289 else
1290 {
1291 arg = launch_args.GetArgumentAtIndex(0);
1292 if (arg)
1293 exe_path = arg;
1294 }
1295 if (!exe_path.empty())
1296 {
1297 argv.push_back(exe_path.c_str());
1298 for (uint32_t i=1; (arg = launch_args.GetArgumentAtIndex(i)) != NULL; ++i)
1299 {
1300 if (arg)
1301 argv.push_back(arg);
1302 }
1303 }
1304 if (!argv.empty())
Greg Clayton576d8832011-03-22 04:00:09 +00001305 {
1306 StreamString packet;
1307 packet.PutChar('A');
Greg Claytonfbb76342013-11-20 21:07:01 +00001308 for (size_t i = 0, n = argv.size(); i < n; ++i)
Greg Clayton576d8832011-03-22 04:00:09 +00001309 {
Greg Claytonfbb76342013-11-20 21:07:01 +00001310 arg = argv[i];
Greg Clayton576d8832011-03-22 04:00:09 +00001311 const int arg_len = strlen(arg);
1312 if (i > 0)
1313 packet.PutChar(',');
Greg Claytonfbb76342013-11-20 21:07:01 +00001314 packet.Printf("%i,%i,", arg_len * 2, (int)i);
Greg Clayton576d8832011-03-22 04:00:09 +00001315 packet.PutBytesAsRawHex8 (arg, arg_len);
1316 }
1317
1318 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001319 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001320 {
1321 if (response.IsOKResponse())
1322 return 0;
1323 uint8_t error = response.GetError();
1324 if (error)
1325 return error;
1326 }
1327 }
1328 return -1;
1329}
1330
1331int
1332GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value)
1333{
1334 if (name_equal_value && name_equal_value[0])
1335 {
1336 StreamString packet;
Greg Clayton89600582013-10-10 17:53:50 +00001337 bool send_hex_encoding = false;
1338 for (const char *p = name_equal_value; *p != '\0' && send_hex_encoding == false; ++p)
Greg Clayton576d8832011-03-22 04:00:09 +00001339 {
Greg Clayton89600582013-10-10 17:53:50 +00001340 if (isprint(*p))
1341 {
1342 switch (*p)
1343 {
1344 case '$':
1345 case '#':
1346 send_hex_encoding = true;
1347 break;
1348 default:
1349 break;
1350 }
1351 }
1352 else
1353 {
1354 // We have non printable characters, lets hex encode this...
1355 send_hex_encoding = true;
1356 }
1357 }
1358
1359 StringExtractorGDBRemote response;
1360 if (send_hex_encoding)
1361 {
1362 if (m_supports_QEnvironmentHexEncoded)
1363 {
1364 packet.PutCString("QEnvironmentHexEncoded:");
1365 packet.PutBytesAsRawHex8 (name_equal_value, strlen(name_equal_value));
Greg Clayton3dedae12013-12-06 21:45:27 +00001366 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton89600582013-10-10 17:53:50 +00001367 {
1368 if (response.IsOKResponse())
1369 return 0;
1370 uint8_t error = response.GetError();
1371 if (error)
1372 return error;
1373 if (response.IsUnsupportedResponse())
1374 m_supports_QEnvironmentHexEncoded = false;
1375 }
1376 }
1377
1378 }
1379 else if (m_supports_QEnvironment)
1380 {
1381 packet.Printf("QEnvironment:%s", name_equal_value);
Greg Clayton3dedae12013-12-06 21:45:27 +00001382 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton89600582013-10-10 17:53:50 +00001383 {
1384 if (response.IsOKResponse())
1385 return 0;
1386 uint8_t error = response.GetError();
1387 if (error)
1388 return error;
1389 if (response.IsUnsupportedResponse())
1390 m_supports_QEnvironment = false;
1391 }
Greg Clayton576d8832011-03-22 04:00:09 +00001392 }
1393 }
1394 return -1;
1395}
1396
Greg Claytonc4103b32011-05-08 04:53:50 +00001397int
1398GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch)
1399{
1400 if (arch && arch[0])
1401 {
1402 StreamString packet;
1403 packet.Printf("QLaunchArch:%s", arch);
1404 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001405 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Claytonc4103b32011-05-08 04:53:50 +00001406 {
1407 if (response.IsOKResponse())
1408 return 0;
1409 uint8_t error = response.GetError();
1410 if (error)
1411 return error;
1412 }
1413 }
1414 return -1;
1415}
1416
Jason Molendaa3329782014-03-29 18:54:20 +00001417int
1418GDBRemoteCommunicationClient::SendLaunchEventDataPacket (char const *data, bool *was_supported)
1419{
1420 if (data && *data != '\0')
1421 {
1422 StreamString packet;
1423 packet.Printf("QSetProcessEvent:%s", data);
1424 StringExtractorGDBRemote response;
1425 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
1426 {
1427 if (response.IsOKResponse())
1428 {
1429 if (was_supported)
1430 *was_supported = true;
1431 return 0;
1432 }
1433 else if (response.IsUnsupportedResponse())
1434 {
1435 if (was_supported)
1436 *was_supported = false;
1437 return -1;
1438 }
1439 else
1440 {
1441 uint8_t error = response.GetError();
1442 if (was_supported)
1443 *was_supported = true;
1444 if (error)
1445 return error;
1446 }
1447 }
1448 }
1449 return -1;
1450}
1451
Greg Clayton576d8832011-03-22 04:00:09 +00001452bool
Greg Clayton1cb64962011-03-24 04:28:38 +00001453GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major,
1454 uint32_t &minor,
1455 uint32_t &update)
1456{
1457 if (GetHostInfo ())
1458 {
1459 if (m_os_version_major != UINT32_MAX)
1460 {
1461 major = m_os_version_major;
1462 minor = m_os_version_minor;
1463 update = m_os_version_update;
1464 return true;
1465 }
1466 }
1467 return false;
1468}
1469
1470bool
1471GDBRemoteCommunicationClient::GetOSBuildString (std::string &s)
1472{
1473 if (GetHostInfo ())
1474 {
1475 if (!m_os_build.empty())
1476 {
1477 s = m_os_build;
1478 return true;
1479 }
1480 }
1481 s.clear();
1482 return false;
1483}
1484
1485
1486bool
1487GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s)
1488{
1489 if (GetHostInfo ())
1490 {
1491 if (!m_os_kernel.empty())
1492 {
1493 s = m_os_kernel;
1494 return true;
1495 }
1496 }
1497 s.clear();
1498 return false;
1499}
1500
1501bool
1502GDBRemoteCommunicationClient::GetHostname (std::string &s)
1503{
1504 if (GetHostInfo ())
1505 {
1506 if (!m_hostname.empty())
1507 {
1508 s = m_hostname;
1509 return true;
1510 }
1511 }
1512 s.clear();
1513 return false;
1514}
1515
1516ArchSpec
1517GDBRemoteCommunicationClient::GetSystemArchitecture ()
1518{
1519 if (GetHostInfo ())
1520 return m_host_arch;
1521 return ArchSpec();
1522}
1523
Jason Molendaf17b5ac2012-12-19 02:54:03 +00001524const lldb_private::ArchSpec &
1525GDBRemoteCommunicationClient::GetProcessArchitecture ()
1526{
1527 if (m_qProcessInfo_is_valid == eLazyBoolCalculate)
1528 GetCurrentProcessInfo ();
1529 return m_process_arch;
1530}
1531
Jason Molendaa3329782014-03-29 18:54:20 +00001532bool
1533GDBRemoteCommunicationClient::GetGDBServerVersion()
1534{
1535 if (m_qGDBServerVersion_is_valid == eLazyBoolCalculate)
1536 {
1537 m_gdb_server_name.clear();
1538 m_gdb_server_version = 0;
1539 m_qGDBServerVersion_is_valid = eLazyBoolNo;
1540
1541 StringExtractorGDBRemote response;
1542 if (SendPacketAndWaitForResponse ("qGDBServerVersion", response, false) == PacketResult::Success)
1543 {
1544 if (response.IsNormalResponse())
1545 {
1546 std::string name;
1547 std::string value;
1548 bool success = false;
1549 while (response.GetNameColonValue(name, value))
1550 {
1551 if (name.compare("name") == 0)
1552 {
1553 success = true;
1554 m_gdb_server_name.swap(value);
1555 }
1556 else if (name.compare("version") == 0)
1557 {
1558 size_t dot_pos = value.find('.');
1559 if (dot_pos != std::string::npos)
1560 value[dot_pos] = '\0';
1561 const uint32_t version = Args::StringToUInt32(value.c_str(), UINT32_MAX, 0);
1562 if (version != UINT32_MAX)
1563 {
1564 success = true;
1565 m_gdb_server_version = version;
1566 }
1567 }
1568 }
1569 if (success)
1570 m_qGDBServerVersion_is_valid = eLazyBoolYes;
1571 }
1572 }
1573 }
1574 return m_qGDBServerVersion_is_valid == eLazyBoolYes;
1575}
1576
1577const char *
1578GDBRemoteCommunicationClient::GetGDBServerProgramName()
1579{
1580 if (GetGDBServerVersion())
1581 {
1582 if (!m_gdb_server_name.empty())
1583 return m_gdb_server_name.c_str();
1584 }
1585 return NULL;
1586}
1587
1588uint32_t
1589GDBRemoteCommunicationClient::GetGDBServerProgramVersion()
1590{
1591 if (GetGDBServerVersion())
1592 return m_gdb_server_version;
1593 return 0;
1594}
Greg Clayton1cb64962011-03-24 04:28:38 +00001595
1596bool
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00001597GDBRemoteCommunicationClient::GetHostInfo (bool force)
Greg Clayton576d8832011-03-22 04:00:09 +00001598{
Todd Fialaaf245d12014-06-30 21:05:18 +00001599 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS));
1600
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00001601 if (force || m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton576d8832011-03-22 04:00:09 +00001602 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001603 m_qHostInfo_is_valid = eLazyBoolNo;
Greg Clayton576d8832011-03-22 04:00:09 +00001604 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001605 if (SendPacketAndWaitForResponse ("qHostInfo", response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001606 {
Greg Clayton17a0cb62011-05-15 23:46:54 +00001607 if (response.IsNormalResponse())
Greg Claytond314e812011-03-23 00:09:55 +00001608 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001609 std::string name;
1610 std::string value;
1611 uint32_t cpu = LLDB_INVALID_CPUTYPE;
1612 uint32_t sub = 0;
1613 std::string arch_name;
1614 std::string os_name;
1615 std::string vendor_name;
1616 std::string triple;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +00001617 std::string distribution_id;
Greg Clayton32e0a752011-03-30 18:16:51 +00001618 uint32_t pointer_byte_size = 0;
1619 StringExtractor extractor;
1620 ByteOrder byte_order = eByteOrderInvalid;
1621 uint32_t num_keys_decoded = 0;
1622 while (response.GetNameColonValue(name, value))
Greg Claytond314e812011-03-23 00:09:55 +00001623 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001624 if (name.compare("cputype") == 0)
Greg Clayton1cb64962011-03-24 04:28:38 +00001625 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001626 // exception type in big endian hex
1627 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0);
1628 if (cpu != LLDB_INVALID_CPUTYPE)
1629 ++num_keys_decoded;
1630 }
1631 else if (name.compare("cpusubtype") == 0)
1632 {
1633 // exception count in big endian hex
1634 sub = Args::StringToUInt32 (value.c_str(), 0, 0);
1635 if (sub != 0)
1636 ++num_keys_decoded;
1637 }
1638 else if (name.compare("arch") == 0)
1639 {
1640 arch_name.swap (value);
1641 ++num_keys_decoded;
1642 }
1643 else if (name.compare("triple") == 0)
1644 {
1645 // The triple comes as ASCII hex bytes since it contains '-' chars
1646 extractor.GetStringRef().swap(value);
1647 extractor.SetFilePos(0);
1648 extractor.GetHexByteString (triple);
1649 ++num_keys_decoded;
1650 }
Todd Fialaa9ddb0e2014-01-18 03:02:39 +00001651 else if (name.compare ("distribution_id") == 0)
1652 {
1653 extractor.GetStringRef ().swap (value);
1654 extractor.SetFilePos (0);
1655 extractor.GetHexByteString (distribution_id);
1656 ++num_keys_decoded;
1657 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001658 else if (name.compare("os_build") == 0)
1659 {
1660 extractor.GetStringRef().swap(value);
1661 extractor.SetFilePos(0);
1662 extractor.GetHexByteString (m_os_build);
1663 ++num_keys_decoded;
1664 }
1665 else if (name.compare("hostname") == 0)
1666 {
1667 extractor.GetStringRef().swap(value);
1668 extractor.SetFilePos(0);
1669 extractor.GetHexByteString (m_hostname);
1670 ++num_keys_decoded;
1671 }
1672 else if (name.compare("os_kernel") == 0)
1673 {
1674 extractor.GetStringRef().swap(value);
1675 extractor.SetFilePos(0);
1676 extractor.GetHexByteString (m_os_kernel);
1677 ++num_keys_decoded;
1678 }
1679 else if (name.compare("ostype") == 0)
1680 {
1681 os_name.swap (value);
1682 ++num_keys_decoded;
1683 }
1684 else if (name.compare("vendor") == 0)
1685 {
1686 vendor_name.swap(value);
1687 ++num_keys_decoded;
1688 }
1689 else if (name.compare("endian") == 0)
1690 {
1691 ++num_keys_decoded;
1692 if (value.compare("little") == 0)
1693 byte_order = eByteOrderLittle;
1694 else if (value.compare("big") == 0)
1695 byte_order = eByteOrderBig;
1696 else if (value.compare("pdp") == 0)
1697 byte_order = eByteOrderPDP;
1698 else
1699 --num_keys_decoded;
1700 }
1701 else if (name.compare("ptrsize") == 0)
1702 {
1703 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 0);
1704 if (pointer_byte_size != 0)
1705 ++num_keys_decoded;
1706 }
1707 else if (name.compare("os_version") == 0)
1708 {
1709 Args::StringToVersion (value.c_str(),
1710 m_os_version_major,
1711 m_os_version_minor,
1712 m_os_version_update);
1713 if (m_os_version_major != UINT32_MAX)
1714 ++num_keys_decoded;
1715 }
Enrico Granataf04a2192012-07-13 23:18:48 +00001716 else if (name.compare("watchpoint_exceptions_received") == 0)
1717 {
1718 ++num_keys_decoded;
1719 if (strcmp(value.c_str(),"before") == 0)
1720 m_watchpoints_trigger_after_instruction = eLazyBoolNo;
1721 else if (strcmp(value.c_str(),"after") == 0)
1722 m_watchpoints_trigger_after_instruction = eLazyBoolYes;
1723 else
1724 --num_keys_decoded;
1725 }
Greg Clayton9ac6d2d2013-10-25 18:13:17 +00001726 else if (name.compare("default_packet_timeout") == 0)
1727 {
1728 m_default_packet_timeout = Args::StringToUInt32(value.c_str(), 0);
1729 if (m_default_packet_timeout > 0)
1730 {
1731 SetPacketTimeout(m_default_packet_timeout);
1732 ++num_keys_decoded;
1733 }
1734 }
Enrico Granataf04a2192012-07-13 23:18:48 +00001735
Greg Clayton32e0a752011-03-30 18:16:51 +00001736 }
1737
1738 if (num_keys_decoded > 0)
1739 m_qHostInfo_is_valid = eLazyBoolYes;
1740
1741 if (triple.empty())
1742 {
1743 if (arch_name.empty())
1744 {
1745 if (cpu != LLDB_INVALID_CPUTYPE)
1746 {
1747 m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1748 if (pointer_byte_size)
1749 {
1750 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1751 }
1752 if (byte_order != eByteOrderInvalid)
1753 {
1754 assert (byte_order == m_host_arch.GetByteOrder());
1755 }
Greg Clayton70512312012-05-08 01:45:38 +00001756
1757 if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0)
1758 {
1759 switch (m_host_arch.GetMachine())
1760 {
Todd Fialad8eaa172014-07-23 14:37:35 +00001761 case llvm::Triple::aarch64:
Greg Clayton70512312012-05-08 01:45:38 +00001762 case llvm::Triple::arm:
1763 case llvm::Triple::thumb:
1764 os_name = "ios";
1765 break;
1766 default:
1767 os_name = "macosx";
1768 break;
1769 }
1770 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001771 if (!vendor_name.empty())
1772 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1773 if (!os_name.empty())
Greg Claytone1dadb82011-09-15 00:21:03 +00001774 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Greg Clayton32e0a752011-03-30 18:16:51 +00001775
1776 }
1777 }
1778 else
1779 {
1780 std::string triple;
1781 triple += arch_name;
Greg Clayton70512312012-05-08 01:45:38 +00001782 if (!vendor_name.empty() || !os_name.empty())
1783 {
1784 triple += '-';
1785 if (vendor_name.empty())
1786 triple += "unknown";
1787 else
1788 triple += vendor_name;
1789 triple += '-';
1790 if (os_name.empty())
1791 triple += "unknown";
1792 else
1793 triple += os_name;
1794 }
1795 m_host_arch.SetTriple (triple.c_str());
1796
1797 llvm::Triple &host_triple = m_host_arch.GetTriple();
1798 if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin)
1799 {
1800 switch (m_host_arch.GetMachine())
1801 {
Todd Fialad8eaa172014-07-23 14:37:35 +00001802 case llvm::Triple::aarch64:
Greg Clayton70512312012-05-08 01:45:38 +00001803 case llvm::Triple::arm:
1804 case llvm::Triple::thumb:
1805 host_triple.setOS(llvm::Triple::IOS);
1806 break;
1807 default:
1808 host_triple.setOS(llvm::Triple::MacOSX);
1809 break;
1810 }
1811 }
Greg Clayton1cb64962011-03-24 04:28:38 +00001812 if (pointer_byte_size)
1813 {
1814 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1815 }
1816 if (byte_order != eByteOrderInvalid)
1817 {
1818 assert (byte_order == m_host_arch.GetByteOrder());
1819 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001820
Greg Clayton1cb64962011-03-24 04:28:38 +00001821 }
1822 }
1823 else
1824 {
Greg Clayton70512312012-05-08 01:45:38 +00001825 m_host_arch.SetTriple (triple.c_str());
Greg Claytond314e812011-03-23 00:09:55 +00001826 if (pointer_byte_size)
1827 {
1828 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1829 }
1830 if (byte_order != eByteOrderInvalid)
1831 {
1832 assert (byte_order == m_host_arch.GetByteOrder());
1833 }
Todd Fialaaf245d12014-06-30 21:05:18 +00001834
1835 if (log)
1836 log->Printf ("GDBRemoteCommunicationClient::%s parsed host architecture as %s, triple as %s from triple text %s", __FUNCTION__, m_host_arch.GetArchitectureName () ? m_host_arch.GetArchitectureName () : "<null-arch-name>", m_host_arch.GetTriple ().getTriple ().c_str(), triple.c_str ());
Todd Fialaa9ddb0e2014-01-18 03:02:39 +00001837 }
1838 if (!distribution_id.empty ())
1839 m_host_arch.SetDistributionId (distribution_id.c_str ());
Greg Claytond314e812011-03-23 00:09:55 +00001840 }
Greg Clayton576d8832011-03-22 04:00:09 +00001841 }
1842 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001843 return m_qHostInfo_is_valid == eLazyBoolYes;
Greg Clayton576d8832011-03-22 04:00:09 +00001844}
1845
1846int
1847GDBRemoteCommunicationClient::SendAttach
1848(
1849 lldb::pid_t pid,
1850 StringExtractorGDBRemote& response
1851)
1852{
1853 if (pid != LLDB_INVALID_PROCESS_ID)
1854 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001855 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001856 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, pid);
Andy Gibbsa297a972013-06-19 19:04:53 +00001857 assert (packet_len < (int)sizeof(packet));
Greg Clayton3dedae12013-12-06 21:45:27 +00001858 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001859 {
1860 if (response.IsErrorResponse())
1861 return response.GetError();
1862 return 0;
1863 }
1864 }
1865 return -1;
1866}
1867
1868const lldb_private::ArchSpec &
1869GDBRemoteCommunicationClient::GetHostArchitecture ()
1870{
Greg Clayton32e0a752011-03-30 18:16:51 +00001871 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton576d8832011-03-22 04:00:09 +00001872 GetHostInfo ();
Greg Claytond314e812011-03-23 00:09:55 +00001873 return m_host_arch;
Greg Clayton576d8832011-03-22 04:00:09 +00001874}
1875
Greg Clayton9ac6d2d2013-10-25 18:13:17 +00001876uint32_t
1877GDBRemoteCommunicationClient::GetHostDefaultPacketTimeout ()
1878{
1879 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
1880 GetHostInfo ();
1881 return m_default_packet_timeout;
1882}
1883
Greg Clayton576d8832011-03-22 04:00:09 +00001884addr_t
1885GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions)
1886{
Greg Clayton70b57652011-05-15 01:25:55 +00001887 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton576d8832011-03-22 04:00:09 +00001888 {
Greg Clayton70b57652011-05-15 01:25:55 +00001889 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton2a48f522011-05-14 01:50:35 +00001890 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001891 const int packet_len = ::snprintf (packet, sizeof(packet), "_M%" PRIx64 ",%s%s%s",
Greg Clayton43e0af02012-09-18 18:04:04 +00001892 (uint64_t)size,
Greg Clayton2a48f522011-05-14 01:50:35 +00001893 permissions & lldb::ePermissionsReadable ? "r" : "",
1894 permissions & lldb::ePermissionsWritable ? "w" : "",
1895 permissions & lldb::ePermissionsExecutable ? "x" : "");
Andy Gibbsa297a972013-06-19 19:04:53 +00001896 assert (packet_len < (int)sizeof(packet));
Greg Clayton2a48f522011-05-14 01:50:35 +00001897 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001898 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton2a48f522011-05-14 01:50:35 +00001899 {
Todd Fialaf105f582014-06-21 00:48:09 +00001900 if (response.IsUnsupportedResponse())
1901 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1902 else if (!response.IsErrorResponse())
Greg Clayton2a48f522011-05-14 01:50:35 +00001903 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1904 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00001905 else
1906 {
1907 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1908 }
Greg Clayton576d8832011-03-22 04:00:09 +00001909 }
1910 return LLDB_INVALID_ADDRESS;
1911}
1912
1913bool
1914GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr)
1915{
Greg Clayton70b57652011-05-15 01:25:55 +00001916 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton576d8832011-03-22 04:00:09 +00001917 {
Greg Clayton70b57652011-05-15 01:25:55 +00001918 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton2a48f522011-05-14 01:50:35 +00001919 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001920 const int packet_len = ::snprintf(packet, sizeof(packet), "_m%" PRIx64, (uint64_t)addr);
Andy Gibbsa297a972013-06-19 19:04:53 +00001921 assert (packet_len < (int)sizeof(packet));
Greg Clayton2a48f522011-05-14 01:50:35 +00001922 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001923 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton2a48f522011-05-14 01:50:35 +00001924 {
Todd Fialaf105f582014-06-21 00:48:09 +00001925 if (response.IsUnsupportedResponse())
1926 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1927 else if (response.IsOKResponse())
Greg Clayton2a48f522011-05-14 01:50:35 +00001928 return true;
Greg Clayton17a0cb62011-05-15 23:46:54 +00001929 }
1930 else
1931 {
1932 m_supports_alloc_dealloc_memory = eLazyBoolNo;
Greg Clayton2a48f522011-05-14 01:50:35 +00001933 }
Greg Clayton576d8832011-03-22 04:00:09 +00001934 }
1935 return false;
1936}
1937
Jim Inghamacff8952013-05-02 00:27:30 +00001938Error
1939GDBRemoteCommunicationClient::Detach (bool keep_stopped)
Greg Clayton37a0a242012-04-11 00:24:49 +00001940{
Jim Inghamacff8952013-05-02 00:27:30 +00001941 Error error;
1942
1943 if (keep_stopped)
1944 {
1945 if (m_supports_detach_stay_stopped == eLazyBoolCalculate)
1946 {
1947 char packet[64];
1948 const int packet_len = ::snprintf(packet, sizeof(packet), "qSupportsDetachAndStayStopped:");
Andy Gibbsa297a972013-06-19 19:04:53 +00001949 assert (packet_len < (int)sizeof(packet));
Jim Inghamacff8952013-05-02 00:27:30 +00001950 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001951 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Jim Inghamacff8952013-05-02 00:27:30 +00001952 {
1953 m_supports_detach_stay_stopped = eLazyBoolYes;
1954 }
1955 else
1956 {
1957 m_supports_detach_stay_stopped = eLazyBoolNo;
1958 }
1959 }
1960
1961 if (m_supports_detach_stay_stopped == eLazyBoolNo)
1962 {
1963 error.SetErrorString("Stays stopped not supported by this target.");
1964 return error;
1965 }
1966 else
1967 {
Jim Ingham6c8824d2014-03-28 20:00:07 +00001968 StringExtractorGDBRemote response;
1969 PacketResult packet_result = SendPacketAndWaitForResponse ("D1", 1, response, false);
Greg Clayton3dedae12013-12-06 21:45:27 +00001970 if (packet_result != PacketResult::Success)
Jim Inghamacff8952013-05-02 00:27:30 +00001971 error.SetErrorString ("Sending extended disconnect packet failed.");
1972 }
1973 }
1974 else
1975 {
Jim Ingham6c8824d2014-03-28 20:00:07 +00001976 StringExtractorGDBRemote response;
1977 PacketResult packet_result = SendPacketAndWaitForResponse ("D", 1, response, false);
Greg Clayton3dedae12013-12-06 21:45:27 +00001978 if (packet_result != PacketResult::Success)
Jim Inghamacff8952013-05-02 00:27:30 +00001979 error.SetErrorString ("Sending disconnect packet failed.");
1980 }
1981 return error;
Greg Clayton37a0a242012-04-11 00:24:49 +00001982}
1983
Greg Clayton46fb5582011-11-18 07:03:08 +00001984Error
1985GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr,
1986 lldb_private::MemoryRegionInfo &region_info)
1987{
1988 Error error;
1989 region_info.Clear();
1990
1991 if (m_supports_memory_region_info != eLazyBoolNo)
1992 {
1993 m_supports_memory_region_info = eLazyBoolYes;
1994 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001995 const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%" PRIx64, (uint64_t)addr);
Andy Gibbsa297a972013-06-19 19:04:53 +00001996 assert (packet_len < (int)sizeof(packet));
Greg Clayton46fb5582011-11-18 07:03:08 +00001997 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001998 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton46fb5582011-11-18 07:03:08 +00001999 {
2000 std::string name;
2001 std::string value;
2002 addr_t addr_value;
2003 bool success = true;
Jason Molendacb349ee2011-12-13 05:39:38 +00002004 bool saw_permissions = false;
Greg Clayton46fb5582011-11-18 07:03:08 +00002005 while (success && response.GetNameColonValue(name, value))
2006 {
2007 if (name.compare ("start") == 0)
2008 {
2009 addr_value = Args::StringToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success);
2010 if (success)
2011 region_info.GetRange().SetRangeBase(addr_value);
2012 }
2013 else if (name.compare ("size") == 0)
2014 {
2015 addr_value = Args::StringToUInt64(value.c_str(), 0, 16, &success);
2016 if (success)
2017 region_info.GetRange().SetByteSize (addr_value);
2018 }
Jason Molendacb349ee2011-12-13 05:39:38 +00002019 else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid())
Greg Clayton46fb5582011-11-18 07:03:08 +00002020 {
Jason Molendacb349ee2011-12-13 05:39:38 +00002021 saw_permissions = true;
2022 if (region_info.GetRange().Contains (addr))
2023 {
2024 if (value.find('r') != std::string::npos)
2025 region_info.SetReadable (MemoryRegionInfo::eYes);
2026 else
2027 region_info.SetReadable (MemoryRegionInfo::eNo);
2028
2029 if (value.find('w') != std::string::npos)
2030 region_info.SetWritable (MemoryRegionInfo::eYes);
2031 else
2032 region_info.SetWritable (MemoryRegionInfo::eNo);
2033
2034 if (value.find('x') != std::string::npos)
2035 region_info.SetExecutable (MemoryRegionInfo::eYes);
2036 else
2037 region_info.SetExecutable (MemoryRegionInfo::eNo);
2038 }
2039 else
2040 {
2041 // The reported region does not contain this address -- we're looking at an unmapped page
2042 region_info.SetReadable (MemoryRegionInfo::eNo);
2043 region_info.SetWritable (MemoryRegionInfo::eNo);
2044 region_info.SetExecutable (MemoryRegionInfo::eNo);
2045 }
Greg Clayton46fb5582011-11-18 07:03:08 +00002046 }
2047 else if (name.compare ("error") == 0)
2048 {
2049 StringExtractorGDBRemote name_extractor;
2050 // Swap "value" over into "name_extractor"
2051 name_extractor.GetStringRef().swap(value);
2052 // Now convert the HEX bytes into a string value
2053 name_extractor.GetHexByteString (value);
2054 error.SetErrorString(value.c_str());
2055 }
2056 }
Jason Molendacb349ee2011-12-13 05:39:38 +00002057
2058 // We got a valid address range back but no permissions -- which means this is an unmapped page
2059 if (region_info.GetRange().IsValid() && saw_permissions == false)
2060 {
2061 region_info.SetReadable (MemoryRegionInfo::eNo);
2062 region_info.SetWritable (MemoryRegionInfo::eNo);
2063 region_info.SetExecutable (MemoryRegionInfo::eNo);
2064 }
Greg Clayton46fb5582011-11-18 07:03:08 +00002065 }
2066 else
2067 {
2068 m_supports_memory_region_info = eLazyBoolNo;
2069 }
2070 }
2071
2072 if (m_supports_memory_region_info == eLazyBoolNo)
2073 {
2074 error.SetErrorString("qMemoryRegionInfo is not supported");
2075 }
2076 if (error.Fail())
2077 region_info.Clear();
2078 return error;
2079
2080}
2081
Johnny Chen64637202012-05-23 21:09:52 +00002082Error
2083GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num)
2084{
2085 Error error;
2086
2087 if (m_supports_watchpoint_support_info == eLazyBoolYes)
2088 {
2089 num = m_num_supported_hardware_watchpoints;
2090 return error;
2091 }
2092
2093 // Set num to 0 first.
2094 num = 0;
2095 if (m_supports_watchpoint_support_info != eLazyBoolNo)
2096 {
2097 char packet[64];
2098 const int packet_len = ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:");
Andy Gibbsa297a972013-06-19 19:04:53 +00002099 assert (packet_len < (int)sizeof(packet));
Johnny Chen64637202012-05-23 21:09:52 +00002100 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002101 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Johnny Chen64637202012-05-23 21:09:52 +00002102 {
2103 m_supports_watchpoint_support_info = eLazyBoolYes;
2104 std::string name;
2105 std::string value;
2106 while (response.GetNameColonValue(name, value))
2107 {
2108 if (name.compare ("num") == 0)
2109 {
2110 num = Args::StringToUInt32(value.c_str(), 0, 0);
2111 m_num_supported_hardware_watchpoints = num;
2112 }
2113 }
2114 }
2115 else
2116 {
2117 m_supports_watchpoint_support_info = eLazyBoolNo;
2118 }
2119 }
2120
2121 if (m_supports_watchpoint_support_info == eLazyBoolNo)
2122 {
2123 error.SetErrorString("qWatchpointSupportInfo is not supported");
2124 }
2125 return error;
2126
2127}
Greg Clayton46fb5582011-11-18 07:03:08 +00002128
Enrico Granataf04a2192012-07-13 23:18:48 +00002129lldb_private::Error
2130GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num, bool& after)
2131{
2132 Error error(GetWatchpointSupportInfo(num));
2133 if (error.Success())
2134 error = GetWatchpointsTriggerAfterInstruction(after);
2135 return error;
2136}
2137
2138lldb_private::Error
2139GDBRemoteCommunicationClient::GetWatchpointsTriggerAfterInstruction (bool &after)
2140{
2141 Error error;
2142
2143 // we assume watchpoints will happen after running the relevant opcode
2144 // and we only want to override this behavior if we have explicitly
2145 // received a qHostInfo telling us otherwise
2146 if (m_qHostInfo_is_valid != eLazyBoolYes)
2147 after = true;
2148 else
2149 after = (m_watchpoints_trigger_after_instruction != eLazyBoolNo);
2150 return error;
2151}
2152
Greg Clayton576d8832011-03-22 04:00:09 +00002153int
2154GDBRemoteCommunicationClient::SetSTDIN (char const *path)
2155{
2156 if (path && path[0])
2157 {
2158 StreamString packet;
2159 packet.PutCString("QSetSTDIN:");
2160 packet.PutBytesAsRawHex8(path, strlen(path));
2161
2162 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002163 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002164 {
2165 if (response.IsOKResponse())
2166 return 0;
2167 uint8_t error = response.GetError();
2168 if (error)
2169 return error;
2170 }
2171 }
2172 return -1;
2173}
2174
2175int
2176GDBRemoteCommunicationClient::SetSTDOUT (char const *path)
2177{
2178 if (path && path[0])
2179 {
2180 StreamString packet;
2181 packet.PutCString("QSetSTDOUT:");
2182 packet.PutBytesAsRawHex8(path, strlen(path));
2183
2184 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002185 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002186 {
2187 if (response.IsOKResponse())
2188 return 0;
2189 uint8_t error = response.GetError();
2190 if (error)
2191 return error;
2192 }
2193 }
2194 return -1;
2195}
2196
2197int
2198GDBRemoteCommunicationClient::SetSTDERR (char const *path)
2199{
2200 if (path && path[0])
2201 {
2202 StreamString packet;
2203 packet.PutCString("QSetSTDERR:");
2204 packet.PutBytesAsRawHex8(path, strlen(path));
2205
2206 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002207 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002208 {
2209 if (response.IsOKResponse())
2210 return 0;
2211 uint8_t error = response.GetError();
2212 if (error)
2213 return error;
2214 }
2215 }
2216 return -1;
2217}
2218
Greg Claytonfbb76342013-11-20 21:07:01 +00002219bool
2220GDBRemoteCommunicationClient::GetWorkingDir (std::string &cwd)
2221{
2222 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002223 if (SendPacketAndWaitForResponse ("qGetWorkingDir", response, false) == PacketResult::Success)
Greg Claytonfbb76342013-11-20 21:07:01 +00002224 {
2225 if (response.IsUnsupportedResponse())
2226 return false;
2227 if (response.IsErrorResponse())
2228 return false;
2229 response.GetHexByteString (cwd);
2230 return !cwd.empty();
2231 }
2232 return false;
2233}
2234
Greg Clayton576d8832011-03-22 04:00:09 +00002235int
2236GDBRemoteCommunicationClient::SetWorkingDir (char const *path)
2237{
2238 if (path && path[0])
2239 {
2240 StreamString packet;
2241 packet.PutCString("QSetWorkingDir:");
2242 packet.PutBytesAsRawHex8(path, strlen(path));
2243
2244 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002245 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002246 {
2247 if (response.IsOKResponse())
2248 return 0;
2249 uint8_t error = response.GetError();
2250 if (error)
2251 return error;
2252 }
2253 }
2254 return -1;
2255}
2256
2257int
2258GDBRemoteCommunicationClient::SetDisableASLR (bool enable)
2259{
Greg Clayton32e0a752011-03-30 18:16:51 +00002260 char packet[32];
2261 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0);
Andy Gibbsa297a972013-06-19 19:04:53 +00002262 assert (packet_len < (int)sizeof(packet));
Greg Clayton576d8832011-03-22 04:00:09 +00002263 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002264 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002265 {
2266 if (response.IsOKResponse())
2267 return 0;
2268 uint8_t error = response.GetError();
2269 if (error)
2270 return error;
2271 }
2272 return -1;
2273}
Greg Clayton32e0a752011-03-30 18:16:51 +00002274
Jim Ingham106d0282014-06-25 02:32:56 +00002275int
2276GDBRemoteCommunicationClient::SetDetachOnError (bool enable)
2277{
2278 char packet[32];
2279 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDetachOnError:%i", enable ? 1 : 0);
2280 assert (packet_len < (int)sizeof(packet));
2281 StringExtractorGDBRemote response;
2282 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
2283 {
2284 if (response.IsOKResponse())
2285 return 0;
2286 uint8_t error = response.GetError();
2287 if (error)
2288 return error;
2289 }
2290 return -1;
2291}
2292
2293
Greg Clayton32e0a752011-03-30 18:16:51 +00002294bool
Greg Clayton8b82f082011-04-12 05:54:46 +00002295GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00002296{
2297 if (response.IsNormalResponse())
2298 {
2299 std::string name;
2300 std::string value;
2301 StringExtractor extractor;
Jason Molenda89c37492014-01-27 22:23:20 +00002302
2303 uint32_t cpu = LLDB_INVALID_CPUTYPE;
2304 uint32_t sub = 0;
2305 std::string vendor;
2306 std::string os_type;
Greg Clayton32e0a752011-03-30 18:16:51 +00002307
2308 while (response.GetNameColonValue(name, value))
2309 {
2310 if (name.compare("pid") == 0)
2311 {
2312 process_info.SetProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
2313 }
2314 else if (name.compare("ppid") == 0)
2315 {
2316 process_info.SetParentProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
2317 }
2318 else if (name.compare("uid") == 0)
2319 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002320 process_info.SetUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002321 }
2322 else if (name.compare("euid") == 0)
2323 {
2324 process_info.SetEffectiveUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
2325 }
2326 else if (name.compare("gid") == 0)
2327 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002328 process_info.SetGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002329 }
2330 else if (name.compare("egid") == 0)
2331 {
2332 process_info.SetEffectiveGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0));
2333 }
2334 else if (name.compare("triple") == 0)
2335 {
2336 // The triple comes as ASCII hex bytes since it contains '-' chars
2337 extractor.GetStringRef().swap(value);
2338 extractor.SetFilePos(0);
2339 extractor.GetHexByteString (value);
Greg Clayton70512312012-05-08 01:45:38 +00002340 process_info.GetArchitecture ().SetTriple (value.c_str());
Greg Clayton32e0a752011-03-30 18:16:51 +00002341 }
2342 else if (name.compare("name") == 0)
2343 {
2344 StringExtractor extractor;
Filipe Cabecinhasf86cf782012-05-07 09:30:51 +00002345 // The process name from ASCII hex bytes since we can't
Greg Clayton32e0a752011-03-30 18:16:51 +00002346 // control the characters in a process name
2347 extractor.GetStringRef().swap(value);
2348 extractor.SetFilePos(0);
2349 extractor.GetHexByteString (value);
Greg Clayton144f3a92011-11-15 03:53:30 +00002350 process_info.GetExecutableFile().SetFile (value.c_str(), false);
Greg Clayton32e0a752011-03-30 18:16:51 +00002351 }
Jason Molenda89c37492014-01-27 22:23:20 +00002352 else if (name.compare("cputype") == 0)
2353 {
2354 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 16);
2355 }
2356 else if (name.compare("cpusubtype") == 0)
2357 {
2358 sub = Args::StringToUInt32 (value.c_str(), 0, 16);
2359 }
2360 else if (name.compare("vendor") == 0)
2361 {
2362 vendor = value;
2363 }
2364 else if (name.compare("ostype") == 0)
2365 {
2366 os_type = value;
2367 }
2368 }
2369
2370 if (cpu != LLDB_INVALID_CPUTYPE && !vendor.empty() && !os_type.empty())
2371 {
2372 if (vendor == "apple")
2373 {
2374 process_info.GetArchitecture().SetArchitecture (eArchTypeMachO, cpu, sub);
2375 process_info.GetArchitecture().GetTriple().setVendorName (llvm::StringRef (vendor));
2376 process_info.GetArchitecture().GetTriple().setOSName (llvm::StringRef (os_type));
2377 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002378 }
2379
2380 if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
2381 return true;
2382 }
2383 return false;
2384}
2385
2386bool
Greg Clayton8b82f082011-04-12 05:54:46 +00002387GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00002388{
2389 process_info.Clear();
2390
2391 if (m_supports_qProcessInfoPID)
2392 {
2393 char packet[32];
Daniel Malead01b2952012-11-29 21:49:15 +00002394 const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%" PRIu64, pid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002395 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00002396 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002397 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002398 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002399 return DecodeProcessInfoResponse (response, process_info);
2400 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002401 else
2402 {
2403 m_supports_qProcessInfoPID = false;
2404 return false;
2405 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002406 }
2407 return false;
2408}
2409
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002410bool
2411GDBRemoteCommunicationClient::GetCurrentProcessInfo ()
2412{
2413 if (m_qProcessInfo_is_valid == eLazyBoolYes)
2414 return true;
2415 if (m_qProcessInfo_is_valid == eLazyBoolNo)
2416 return false;
2417
2418 GetHostInfo ();
2419
2420 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002421 if (SendPacketAndWaitForResponse ("qProcessInfo", response, false) == PacketResult::Success)
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002422 {
2423 if (response.IsNormalResponse())
2424 {
2425 std::string name;
2426 std::string value;
2427 uint32_t cpu = LLDB_INVALID_CPUTYPE;
2428 uint32_t sub = 0;
2429 std::string arch_name;
2430 std::string os_name;
2431 std::string vendor_name;
2432 std::string triple;
2433 uint32_t pointer_byte_size = 0;
2434 StringExtractor extractor;
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002435 uint32_t num_keys_decoded = 0;
Todd Fiala9f72b3a2014-05-07 19:28:21 +00002436 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002437 while (response.GetNameColonValue(name, value))
2438 {
2439 if (name.compare("cputype") == 0)
2440 {
2441 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 16);
2442 if (cpu != LLDB_INVALID_CPUTYPE)
2443 ++num_keys_decoded;
2444 }
2445 else if (name.compare("cpusubtype") == 0)
2446 {
2447 sub = Args::StringToUInt32 (value.c_str(), 0, 16);
2448 if (sub != 0)
2449 ++num_keys_decoded;
2450 }
2451 else if (name.compare("ostype") == 0)
2452 {
2453 os_name.swap (value);
2454 ++num_keys_decoded;
2455 }
2456 else if (name.compare("vendor") == 0)
2457 {
2458 vendor_name.swap(value);
2459 ++num_keys_decoded;
2460 }
2461 else if (name.compare("endian") == 0)
2462 {
Saleem Abdulrasool3924d752014-06-13 03:30:39 +00002463 if (value.compare("little") == 0 ||
2464 value.compare("big") == 0 ||
2465 value.compare("pdp") == 0)
2466 ++num_keys_decoded;
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002467 }
2468 else if (name.compare("ptrsize") == 0)
2469 {
2470 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 16);
2471 if (pointer_byte_size != 0)
2472 ++num_keys_decoded;
2473 }
Todd Fiala9f72b3a2014-05-07 19:28:21 +00002474 else if (name.compare("pid") == 0)
2475 {
2476 pid = Args::StringToUInt64(value.c_str(), 0, 16);
2477 if (pid != LLDB_INVALID_PROCESS_ID)
2478 ++num_keys_decoded;
2479 }
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002480 }
2481 if (num_keys_decoded > 0)
2482 m_qProcessInfo_is_valid = eLazyBoolYes;
Todd Fiala9f72b3a2014-05-07 19:28:21 +00002483 if (pid != LLDB_INVALID_PROCESS_ID)
2484 {
2485 m_curr_pid_is_valid = eLazyBoolYes;
2486 m_curr_pid = pid;
2487 }
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002488 if (cpu != LLDB_INVALID_CPUTYPE && !os_name.empty() && !vendor_name.empty())
2489 {
2490 m_process_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
2491 if (pointer_byte_size)
2492 {
2493 assert (pointer_byte_size == m_process_arch.GetAddressByteSize());
2494 }
Greg Clayton7ab7f892014-05-29 21:33:45 +00002495 m_process_arch.GetTriple().setOSName(llvm::StringRef (os_name));
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002496 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
2497 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002498 }
Greg Clayton7ab7f892014-05-29 21:33:45 +00002499 return true;
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002500 }
2501 }
2502 else
2503 {
2504 m_qProcessInfo_is_valid = eLazyBoolNo;
2505 }
2506
2507 return false;
2508}
2509
2510
Greg Clayton32e0a752011-03-30 18:16:51 +00002511uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +00002512GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info,
2513 ProcessInstanceInfoList &process_infos)
Greg Clayton32e0a752011-03-30 18:16:51 +00002514{
2515 process_infos.Clear();
2516
2517 if (m_supports_qfProcessInfo)
2518 {
2519 StreamString packet;
2520 packet.PutCString ("qfProcessInfo");
2521 if (!match_info.MatchAllProcesses())
2522 {
2523 packet.PutChar (':');
2524 const char *name = match_info.GetProcessInfo().GetName();
2525 bool has_name_match = false;
2526 if (name && name[0])
2527 {
2528 has_name_match = true;
2529 NameMatchType name_match_type = match_info.GetNameMatchType();
2530 switch (name_match_type)
2531 {
2532 case eNameMatchIgnore:
2533 has_name_match = false;
2534 break;
2535
2536 case eNameMatchEquals:
2537 packet.PutCString ("name_match:equals;");
2538 break;
2539
2540 case eNameMatchContains:
2541 packet.PutCString ("name_match:contains;");
2542 break;
2543
2544 case eNameMatchStartsWith:
2545 packet.PutCString ("name_match:starts_with;");
2546 break;
2547
2548 case eNameMatchEndsWith:
2549 packet.PutCString ("name_match:ends_with;");
2550 break;
2551
2552 case eNameMatchRegularExpression:
2553 packet.PutCString ("name_match:regex;");
2554 break;
2555 }
2556 if (has_name_match)
2557 {
2558 packet.PutCString ("name:");
2559 packet.PutBytesAsRawHex8(name, ::strlen(name));
2560 packet.PutChar (';');
2561 }
2562 }
2563
2564 if (match_info.GetProcessInfo().ProcessIDIsValid())
Daniel Malead01b2952012-11-29 21:49:15 +00002565 packet.Printf("pid:%" PRIu64 ";",match_info.GetProcessInfo().GetProcessID());
Greg Clayton32e0a752011-03-30 18:16:51 +00002566 if (match_info.GetProcessInfo().ParentProcessIDIsValid())
Daniel Malead01b2952012-11-29 21:49:15 +00002567 packet.Printf("parent_pid:%" PRIu64 ";",match_info.GetProcessInfo().GetParentProcessID());
Greg Clayton8b82f082011-04-12 05:54:46 +00002568 if (match_info.GetProcessInfo().UserIDIsValid())
2569 packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID());
2570 if (match_info.GetProcessInfo().GroupIDIsValid())
2571 packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID());
Greg Clayton32e0a752011-03-30 18:16:51 +00002572 if (match_info.GetProcessInfo().EffectiveUserIDIsValid())
2573 packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID());
2574 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
2575 packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID());
2576 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
2577 packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0);
2578 if (match_info.GetProcessInfo().GetArchitecture().IsValid())
2579 {
2580 const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture();
2581 const llvm::Triple &triple = match_arch.GetTriple();
2582 packet.PutCString("triple:");
2583 packet.PutCStringAsRawHex8(triple.getTriple().c_str());
2584 packet.PutChar (';');
2585 }
2586 }
2587 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002588 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002589 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002590 do
2591 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002592 ProcessInstanceInfo process_info;
Greg Clayton32e0a752011-03-30 18:16:51 +00002593 if (!DecodeProcessInfoResponse (response, process_info))
2594 break;
2595 process_infos.Append(process_info);
2596 response.GetStringRef().clear();
2597 response.SetFilePos(0);
Greg Clayton3dedae12013-12-06 21:45:27 +00002598 } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false) == PacketResult::Success);
Greg Clayton32e0a752011-03-30 18:16:51 +00002599 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002600 else
2601 {
2602 m_supports_qfProcessInfo = false;
2603 return 0;
2604 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002605 }
2606 return process_infos.GetSize();
2607
2608}
2609
2610bool
2611GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name)
2612{
2613 if (m_supports_qUserName)
2614 {
2615 char packet[32];
2616 const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002617 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00002618 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002619 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002620 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002621 if (response.IsNormalResponse())
2622 {
2623 // Make sure we parsed the right number of characters. The response is
2624 // the hex encoded user name and should make up the entire packet.
2625 // If there are any non-hex ASCII bytes, the length won't match below..
2626 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
2627 return true;
2628 }
2629 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002630 else
2631 {
2632 m_supports_qUserName = false;
2633 return false;
2634 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002635 }
2636 return false;
2637
2638}
2639
2640bool
2641GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name)
2642{
2643 if (m_supports_qGroupName)
2644 {
2645 char packet[32];
2646 const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002647 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00002648 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002649 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002650 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002651 if (response.IsNormalResponse())
2652 {
2653 // Make sure we parsed the right number of characters. The response is
2654 // the hex encoded group name and should make up the entire packet.
2655 // If there are any non-hex ASCII bytes, the length won't match below..
2656 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
2657 return true;
2658 }
2659 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002660 else
2661 {
2662 m_supports_qGroupName = false;
2663 return false;
2664 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002665 }
2666 return false;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002667}
Greg Clayton32e0a752011-03-30 18:16:51 +00002668
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002669void
2670GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets)
2671{
2672 uint32_t i;
2673 TimeValue start_time, end_time;
2674 uint64_t total_time_nsec;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002675 if (SendSpeedTestPacket (0, 0))
2676 {
Greg Clayton700e5082014-02-21 19:11:28 +00002677 static uint32_t g_send_sizes[] = { 0, 64, 128, 512, 1024 };
2678 static uint32_t g_recv_sizes[] = { 0, 64, 128, 512, 1024 }; //, 4*1024, 8*1024, 16*1024, 32*1024, 48*1024, 64*1024, 96*1024, 128*1024 };
Saleem Abdulrasool28606952014-06-27 05:17:41 +00002679 const size_t k_num_send_sizes = llvm::array_lengthof(g_send_sizes);
2680 const size_t k_num_recv_sizes = llvm::array_lengthof(g_recv_sizes);
Greg Clayton700e5082014-02-21 19:11:28 +00002681 const uint64_t k_recv_amount = 4*1024*1024; // Receive 4MB
2682 for (uint32_t send_idx = 0; send_idx < k_num_send_sizes; ++send_idx)
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002683 {
Greg Clayton700e5082014-02-21 19:11:28 +00002684 const uint32_t send_size = g_send_sizes[send_idx];
2685 for (uint32_t recv_idx = 0; recv_idx < k_num_recv_sizes; ++recv_idx)
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002686 {
Greg Clayton700e5082014-02-21 19:11:28 +00002687 const uint32_t recv_size = g_recv_sizes[recv_idx];
2688 StreamString packet;
2689 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size);
2690 uint32_t bytes_left = send_size;
2691 while (bytes_left > 0)
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002692 {
Greg Clayton700e5082014-02-21 19:11:28 +00002693 if (bytes_left >= 26)
2694 {
2695 packet.PutCString("abcdefghijklmnopqrstuvwxyz");
2696 bytes_left -= 26;
2697 }
2698 else
2699 {
2700 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz");
2701 bytes_left = 0;
2702 }
2703 }
2704
2705 start_time = TimeValue::Now();
2706 if (recv_size == 0)
2707 {
2708 for (i=0; i<num_packets; ++i)
2709 {
2710 StringExtractorGDBRemote response;
2711 SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false);
2712 }
2713 }
2714 else
2715 {
2716 uint32_t bytes_read = 0;
2717 while (bytes_read < k_recv_amount)
2718 {
2719 StringExtractorGDBRemote response;
2720 SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false);
2721 bytes_read += recv_size;
2722 }
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002723 }
2724 end_time = TimeValue::Now();
2725 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002726 if (recv_size == 0)
Greg Clayton700e5082014-02-21 19:11:28 +00002727 {
2728 float packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
2729 printf ("%u qSpeedTest(send=%-7u, recv=%-7u) in %" PRIu64 ".%9.9" PRIu64 " sec for %f packets/sec.\n",
2730 num_packets,
2731 send_size,
2732 recv_size,
2733 total_time_nsec / TimeValue::NanoSecPerSec,
2734 total_time_nsec % TimeValue::NanoSecPerSec,
2735 packets_per_second);
2736 }
2737 else
2738 {
2739 float mb_second = ((((float)k_recv_amount)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec) / (1024.0*1024.0);
2740 printf ("%u qSpeedTest(send=%-7u, recv=%-7u) sent 4MB in %" PRIu64 ".%9.9" PRIu64 " sec for %f MB/sec.\n",
2741 num_packets,
2742 send_size,
2743 recv_size,
2744 total_time_nsec / TimeValue::NanoSecPerSec,
2745 total_time_nsec % TimeValue::NanoSecPerSec,
2746 mb_second);
2747 }
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002748 }
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002749 }
2750 }
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002751}
2752
2753bool
2754GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size)
2755{
2756 StreamString packet;
2757 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size);
2758 uint32_t bytes_left = send_size;
2759 while (bytes_left > 0)
2760 {
2761 if (bytes_left >= 26)
2762 {
2763 packet.PutCString("abcdefghijklmnopqrstuvwxyz");
2764 bytes_left -= 26;
2765 }
2766 else
2767 {
2768 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz");
2769 bytes_left = 0;
2770 }
2771 }
2772
2773 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002774 return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success;
Greg Clayton32e0a752011-03-30 18:16:51 +00002775}
Greg Clayton8b82f082011-04-12 05:54:46 +00002776
2777uint16_t
Greg Claytondbf04572013-12-04 19:40:33 +00002778GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort (lldb::pid_t &pid, const char *remote_accept_hostname)
Greg Clayton8b82f082011-04-12 05:54:46 +00002779{
Daniel Maleae0f8f572013-08-26 23:57:52 +00002780 pid = LLDB_INVALID_PROCESS_ID;
Greg Clayton8b82f082011-04-12 05:54:46 +00002781 StringExtractorGDBRemote response;
Daniel Maleae0f8f572013-08-26 23:57:52 +00002782 StreamString stream;
Greg Clayton29b8fc42013-11-21 01:44:58 +00002783 stream.PutCString("qLaunchGDBServer;");
Daniel Maleae0f8f572013-08-26 23:57:52 +00002784 std::string hostname;
Greg Claytondbf04572013-12-04 19:40:33 +00002785 if (remote_accept_hostname && remote_accept_hostname[0])
2786 hostname = remote_accept_hostname;
Daniel Maleae0f8f572013-08-26 23:57:52 +00002787 else
2788 {
Greg Claytondbf04572013-12-04 19:40:33 +00002789 if (Host::GetHostname (hostname))
2790 {
2791 // Make the GDB server we launch only accept connections from this host
2792 stream.Printf("host:%s;", hostname.c_str());
2793 }
2794 else
2795 {
2796 // Make the GDB server we launch accept connections from any host since we can't figure out the hostname
2797 stream.Printf("host:*;");
2798 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00002799 }
2800 const char *packet = stream.GetData();
2801 int packet_len = stream.GetSize();
2802
Greg Clayton3dedae12013-12-06 21:45:27 +00002803 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002804 {
2805 std::string name;
2806 std::string value;
2807 uint16_t port = 0;
Greg Clayton8b82f082011-04-12 05:54:46 +00002808 while (response.GetNameColonValue(name, value))
2809 {
Daniel Maleae0f8f572013-08-26 23:57:52 +00002810 if (name.compare("port") == 0)
Greg Clayton8b82f082011-04-12 05:54:46 +00002811 port = Args::StringToUInt32(value.c_str(), 0, 0);
Daniel Maleae0f8f572013-08-26 23:57:52 +00002812 else if (name.compare("pid") == 0)
2813 pid = Args::StringToUInt64(value.c_str(), LLDB_INVALID_PROCESS_ID, 0);
Greg Clayton8b82f082011-04-12 05:54:46 +00002814 }
2815 return port;
2816 }
2817 return 0;
2818}
2819
2820bool
Daniel Maleae0f8f572013-08-26 23:57:52 +00002821GDBRemoteCommunicationClient::KillSpawnedProcess (lldb::pid_t pid)
2822{
2823 StreamString stream;
2824 stream.Printf ("qKillSpawnedProcess:%" PRId64 , pid);
2825 const char *packet = stream.GetData();
2826 int packet_len = stream.GetSize();
Sylvestre Ledrufd654c42013-10-06 09:51:02 +00002827
Daniel Maleae0f8f572013-08-26 23:57:52 +00002828 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002829 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00002830 {
2831 if (response.IsOKResponse())
2832 return true;
2833 }
2834 return false;
2835}
2836
2837bool
Jason Molendae9ca4af2013-02-23 02:04:45 +00002838GDBRemoteCommunicationClient::SetCurrentThread (uint64_t tid)
Greg Clayton8b82f082011-04-12 05:54:46 +00002839{
2840 if (m_curr_tid == tid)
2841 return true;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002842
Greg Clayton8b82f082011-04-12 05:54:46 +00002843 char packet[32];
2844 int packet_len;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002845 if (tid == UINT64_MAX)
2846 packet_len = ::snprintf (packet, sizeof(packet), "Hg-1");
Greg Clayton8b82f082011-04-12 05:54:46 +00002847 else
Jason Molendae9ca4af2013-02-23 02:04:45 +00002848 packet_len = ::snprintf (packet, sizeof(packet), "Hg%" PRIx64, tid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002849 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002850 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002851 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002852 {
2853 if (response.IsOKResponse())
2854 {
2855 m_curr_tid = tid;
2856 return true;
2857 }
2858 }
2859 return false;
2860}
2861
2862bool
Jason Molendae9ca4af2013-02-23 02:04:45 +00002863GDBRemoteCommunicationClient::SetCurrentThreadForRun (uint64_t tid)
Greg Clayton8b82f082011-04-12 05:54:46 +00002864{
2865 if (m_curr_tid_run == tid)
2866 return true;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002867
Greg Clayton8b82f082011-04-12 05:54:46 +00002868 char packet[32];
2869 int packet_len;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002870 if (tid == UINT64_MAX)
2871 packet_len = ::snprintf (packet, sizeof(packet), "Hc-1");
Greg Clayton8b82f082011-04-12 05:54:46 +00002872 else
Jason Molendae9ca4af2013-02-23 02:04:45 +00002873 packet_len = ::snprintf (packet, sizeof(packet), "Hc%" PRIx64, tid);
2874
Andy Gibbsa297a972013-06-19 19:04:53 +00002875 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002876 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002877 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002878 {
2879 if (response.IsOKResponse())
2880 {
2881 m_curr_tid_run = tid;
2882 return true;
2883 }
2884 }
2885 return false;
2886}
2887
2888bool
2889GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response)
2890{
Greg Clayton3dedae12013-12-06 21:45:27 +00002891 if (SendPacketAndWaitForResponse("?", 1, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002892 return response.IsNormalResponse();
2893 return false;
2894}
2895
2896bool
Greg Claytonf402f782012-10-13 02:11:55 +00002897GDBRemoteCommunicationClient::GetThreadStopInfo (lldb::tid_t tid, StringExtractorGDBRemote &response)
Greg Clayton8b82f082011-04-12 05:54:46 +00002898{
2899 if (m_supports_qThreadStopInfo)
2900 {
2901 char packet[256];
Daniel Malead01b2952012-11-29 21:49:15 +00002902 int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%" PRIx64, tid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002903 assert (packet_len < (int)sizeof(packet));
Greg Clayton3dedae12013-12-06 21:45:27 +00002904 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002905 {
Greg Claytonef8180a2013-10-15 00:14:28 +00002906 if (response.IsUnsupportedResponse())
2907 m_supports_qThreadStopInfo = false;
2908 else if (response.IsNormalResponse())
Greg Clayton8b82f082011-04-12 05:54:46 +00002909 return true;
2910 else
2911 return false;
2912 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002913 else
2914 {
2915 m_supports_qThreadStopInfo = false;
2916 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002917 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002918 return false;
2919}
2920
2921
2922uint8_t
2923GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length)
2924{
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002925 // Check if the stub is known not to support this breakpoint type
2926 if (!SupportsGDBStoppointPacket(type))
2927 return UINT8_MAX;
2928 // Construct the breakpoint packet
Greg Clayton8b82f082011-04-12 05:54:46 +00002929 char packet[64];
2930 const int packet_len = ::snprintf (packet,
2931 sizeof(packet),
Daniel Malead01b2952012-11-29 21:49:15 +00002932 "%c%i,%" PRIx64 ",%x",
Greg Clayton8b82f082011-04-12 05:54:46 +00002933 insert ? 'Z' : 'z',
2934 type,
2935 addr,
2936 length);
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002937 // Check we haven't overwritten the end of the packet buffer
Andy Gibbsa297a972013-06-19 19:04:53 +00002938 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002939 StringExtractorGDBRemote response;
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002940 // Try to send the breakpoint packet, and check that it was correctly sent
Greg Clayton3dedae12013-12-06 21:45:27 +00002941 if (SendPacketAndWaitForResponse(packet, packet_len, response, true) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002942 {
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002943 // Receive and OK packet when the breakpoint successfully placed
Greg Clayton8b82f082011-04-12 05:54:46 +00002944 if (response.IsOKResponse())
2945 return 0;
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002946
2947 // Error while setting breakpoint, send back specific error
2948 if (response.IsErrorResponse())
Greg Clayton8b82f082011-04-12 05:54:46 +00002949 return response.GetError();
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002950
2951 // Empty packet informs us that breakpoint is not supported
2952 if (response.IsUnsupportedResponse())
Greg Clayton17a0cb62011-05-15 23:46:54 +00002953 {
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002954 // Disable this breakpoint type since it is unsupported
2955 switch (type)
2956 {
Greg Clayton17a0cb62011-05-15 23:46:54 +00002957 case eBreakpointSoftware: m_supports_z0 = false; break;
2958 case eBreakpointHardware: m_supports_z1 = false; break;
2959 case eWatchpointWrite: m_supports_z2 = false; break;
2960 case eWatchpointRead: m_supports_z3 = false; break;
2961 case eWatchpointReadWrite: m_supports_z4 = false; break;
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00002962 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002963 }
2964 }
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00002965 // Signal generic failure
Greg Clayton8b82f082011-04-12 05:54:46 +00002966 return UINT8_MAX;
2967}
Greg Claytonadc00cb2011-05-20 23:38:13 +00002968
2969size_t
2970GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
2971 bool &sequence_mutex_unavailable)
2972{
2973 Mutex::Locker locker;
2974 thread_ids.clear();
2975
Jim Ingham4ceb9282012-06-08 22:50:40 +00002976 if (GetSequenceMutex (locker, "ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex"))
Greg Claytonadc00cb2011-05-20 23:38:13 +00002977 {
2978 sequence_mutex_unavailable = false;
2979 StringExtractorGDBRemote response;
2980
Greg Clayton3dedae12013-12-06 21:45:27 +00002981 PacketResult packet_result;
2982 for (packet_result = SendPacketAndWaitForResponseNoLock ("qfThreadInfo", strlen("qfThreadInfo"), response);
2983 packet_result == PacketResult::Success && response.IsNormalResponse();
2984 packet_result = SendPacketAndWaitForResponseNoLock ("qsThreadInfo", strlen("qsThreadInfo"), response))
Greg Claytonadc00cb2011-05-20 23:38:13 +00002985 {
2986 char ch = response.GetChar();
2987 if (ch == 'l')
2988 break;
2989 if (ch == 'm')
2990 {
2991 do
2992 {
Jason Molendae9ca4af2013-02-23 02:04:45 +00002993 tid_t tid = response.GetHexMaxU64(false, LLDB_INVALID_THREAD_ID);
Greg Claytonadc00cb2011-05-20 23:38:13 +00002994
2995 if (tid != LLDB_INVALID_THREAD_ID)
2996 {
2997 thread_ids.push_back (tid);
2998 }
2999 ch = response.GetChar(); // Skip the command separator
3000 } while (ch == ','); // Make sure we got a comma separator
3001 }
3002 }
3003 }
3004 else
3005 {
Jim Ingham4ceb9282012-06-08 22:50:40 +00003006#if defined (LLDB_CONFIGURATION_DEBUG)
3007 // assert(!"ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex");
3008#else
Greg Clayton5160ce52013-03-27 23:08:40 +00003009 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Claytonc3c0b0e2012-04-12 19:04:34 +00003010 if (log)
3011 log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'");
Jim Ingham4ceb9282012-06-08 22:50:40 +00003012#endif
Greg Claytonadc00cb2011-05-20 23:38:13 +00003013 sequence_mutex_unavailable = true;
3014 }
3015 return thread_ids.size();
3016}
Greg Clayton37a0a242012-04-11 00:24:49 +00003017
3018lldb::addr_t
3019GDBRemoteCommunicationClient::GetShlibInfoAddr()
3020{
3021 if (!IsRunning())
3022 {
3023 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003024 if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false) == PacketResult::Success)
Greg Clayton37a0a242012-04-11 00:24:49 +00003025 {
3026 if (response.IsNormalResponse())
3027 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
3028 }
3029 }
3030 return LLDB_INVALID_ADDRESS;
3031}
3032
Daniel Maleae0f8f572013-08-26 23:57:52 +00003033lldb_private::Error
3034GDBRemoteCommunicationClient::RunShellCommand (const char *command, // Shouldn't be NULL
3035 const char *working_dir, // Pass NULL to use the current working directory
3036 int *status_ptr, // Pass NULL if you don't want the process exit status
3037 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
3038 std::string *command_output, // Pass NULL if you don't want the command output
3039 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish
3040{
3041 lldb_private::StreamString stream;
Greg Claytonfbb76342013-11-20 21:07:01 +00003042 stream.PutCString("qPlatform_shell:");
Daniel Maleae0f8f572013-08-26 23:57:52 +00003043 stream.PutBytesAsRawHex8(command, strlen(command));
3044 stream.PutChar(',');
3045 stream.PutHex32(timeout_sec);
3046 if (working_dir && *working_dir)
3047 {
3048 stream.PutChar(',');
3049 stream.PutBytesAsRawHex8(working_dir, strlen(working_dir));
3050 }
3051 const char *packet = stream.GetData();
3052 int packet_len = stream.GetSize();
3053 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003054 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003055 {
3056 if (response.GetChar() != 'F')
3057 return Error("malformed reply");
3058 if (response.GetChar() != ',')
3059 return Error("malformed reply");
3060 uint32_t exitcode = response.GetHexMaxU32(false, UINT32_MAX);
3061 if (exitcode == UINT32_MAX)
3062 return Error("unable to run remote process");
3063 else if (status_ptr)
3064 *status_ptr = exitcode;
3065 if (response.GetChar() != ',')
3066 return Error("malformed reply");
3067 uint32_t signo = response.GetHexMaxU32(false, UINT32_MAX);
3068 if (signo_ptr)
3069 *signo_ptr = signo;
3070 if (response.GetChar() != ',')
3071 return Error("malformed reply");
3072 std::string output;
3073 response.GetEscapedBinaryData(output);
3074 if (command_output)
3075 command_output->assign(output);
3076 return Error();
3077 }
3078 return Error("unable to send packet");
3079}
3080
Greg Claytonfbb76342013-11-20 21:07:01 +00003081Error
3082GDBRemoteCommunicationClient::MakeDirectory (const char *path,
3083 uint32_t file_permissions)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003084{
3085 lldb_private::StreamString stream;
Greg Claytonfbb76342013-11-20 21:07:01 +00003086 stream.PutCString("qPlatform_mkdir:");
3087 stream.PutHex32(file_permissions);
Daniel Maleae0f8f572013-08-26 23:57:52 +00003088 stream.PutChar(',');
Greg Claytonfbb76342013-11-20 21:07:01 +00003089 stream.PutBytesAsRawHex8(path, strlen(path));
Daniel Maleae0f8f572013-08-26 23:57:52 +00003090 const char *packet = stream.GetData();
3091 int packet_len = stream.GetSize();
3092 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003093 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003094 {
Greg Claytonfbb76342013-11-20 21:07:01 +00003095 return Error(response.GetHexMaxU32(false, UINT32_MAX), eErrorTypePOSIX);
Daniel Maleae0f8f572013-08-26 23:57:52 +00003096 }
Greg Claytonfbb76342013-11-20 21:07:01 +00003097 return Error();
Daniel Maleae0f8f572013-08-26 23:57:52 +00003098
3099}
3100
Greg Claytonfbb76342013-11-20 21:07:01 +00003101Error
3102GDBRemoteCommunicationClient::SetFilePermissions (const char *path,
3103 uint32_t file_permissions)
3104{
3105 lldb_private::StreamString stream;
3106 stream.PutCString("qPlatform_chmod:");
3107 stream.PutHex32(file_permissions);
3108 stream.PutChar(',');
3109 stream.PutBytesAsRawHex8(path, strlen(path));
3110 const char *packet = stream.GetData();
3111 int packet_len = stream.GetSize();
3112 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003113 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Claytonfbb76342013-11-20 21:07:01 +00003114 {
3115 return Error(response.GetHexMaxU32(false, UINT32_MAX), eErrorTypePOSIX);
3116 }
3117 return Error();
3118
3119}
3120
Daniel Maleae0f8f572013-08-26 23:57:52 +00003121static uint64_t
3122ParseHostIOPacketResponse (StringExtractorGDBRemote &response,
3123 uint64_t fail_result,
3124 Error &error)
3125{
3126 response.SetFilePos(0);
3127 if (response.GetChar() != 'F')
3128 return fail_result;
3129 int32_t result = response.GetS32 (-2);
3130 if (result == -2)
3131 return fail_result;
3132 if (response.GetChar() == ',')
3133 {
3134 int result_errno = response.GetS32 (-2);
3135 if (result_errno != -2)
3136 error.SetError(result_errno, eErrorTypePOSIX);
3137 else
3138 error.SetError(-1, eErrorTypeGeneric);
3139 }
3140 else
3141 error.Clear();
3142 return result;
3143}
3144lldb::user_id_t
3145GDBRemoteCommunicationClient::OpenFile (const lldb_private::FileSpec& file_spec,
3146 uint32_t flags,
3147 mode_t mode,
3148 Error &error)
3149{
3150 lldb_private::StreamString stream;
3151 stream.PutCString("vFile:open:");
3152 std::string path (file_spec.GetPath());
3153 if (path.empty())
3154 return UINT64_MAX;
3155 stream.PutCStringAsRawHex8(path.c_str());
3156 stream.PutChar(',');
3157 const uint32_t posix_open_flags = File::ConvertOpenOptionsForPOSIXOpen(flags);
3158 stream.PutHex32(posix_open_flags);
3159 stream.PutChar(',');
3160 stream.PutHex32(mode);
3161 const char* packet = stream.GetData();
3162 int packet_len = stream.GetSize();
3163 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003164 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003165 {
3166 return ParseHostIOPacketResponse (response, UINT64_MAX, error);
3167 }
3168 return UINT64_MAX;
3169}
3170
3171bool
3172GDBRemoteCommunicationClient::CloseFile (lldb::user_id_t fd,
3173 Error &error)
3174{
3175 lldb_private::StreamString stream;
3176 stream.Printf("vFile:close:%i", (int)fd);
3177 const char* packet = stream.GetData();
3178 int packet_len = stream.GetSize();
3179 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003180 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003181 {
3182 return ParseHostIOPacketResponse (response, -1, error) == 0;
3183 }
Deepak Panickald66b50c2013-10-22 12:27:43 +00003184 return false;
Daniel Maleae0f8f572013-08-26 23:57:52 +00003185}
3186
3187// Extension of host I/O packets to get the file size.
3188lldb::user_id_t
3189GDBRemoteCommunicationClient::GetFileSize (const lldb_private::FileSpec& file_spec)
3190{
3191 lldb_private::StreamString stream;
3192 stream.PutCString("vFile:size:");
3193 std::string path (file_spec.GetPath());
3194 stream.PutCStringAsRawHex8(path.c_str());
3195 const char* packet = stream.GetData();
3196 int packet_len = stream.GetSize();
3197 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003198 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003199 {
3200 if (response.GetChar() != 'F')
3201 return UINT64_MAX;
3202 uint32_t retcode = response.GetHexMaxU64(false, UINT64_MAX);
3203 return retcode;
3204 }
3205 return UINT64_MAX;
3206}
3207
Greg Claytonfbb76342013-11-20 21:07:01 +00003208Error
3209GDBRemoteCommunicationClient::GetFilePermissions(const char *path, uint32_t &file_permissions)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003210{
Greg Claytonfbb76342013-11-20 21:07:01 +00003211 Error error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00003212 lldb_private::StreamString stream;
3213 stream.PutCString("vFile:mode:");
Greg Claytonfbb76342013-11-20 21:07:01 +00003214 stream.PutCStringAsRawHex8(path);
Daniel Maleae0f8f572013-08-26 23:57:52 +00003215 const char* packet = stream.GetData();
3216 int packet_len = stream.GetSize();
3217 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003218 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003219 {
3220 if (response.GetChar() != 'F')
3221 {
3222 error.SetErrorStringWithFormat ("invalid response to '%s' packet", packet);
Daniel Maleae0f8f572013-08-26 23:57:52 +00003223 }
Greg Claytonfbb76342013-11-20 21:07:01 +00003224 else
Daniel Maleae0f8f572013-08-26 23:57:52 +00003225 {
Greg Claytonfbb76342013-11-20 21:07:01 +00003226 const uint32_t mode = response.GetS32(-1);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00003227 if (static_cast<int32_t>(mode) == -1)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003228 {
Greg Claytonfbb76342013-11-20 21:07:01 +00003229 if (response.GetChar() == ',')
3230 {
3231 int response_errno = response.GetS32(-1);
3232 if (response_errno > 0)
3233 error.SetError(response_errno, lldb::eErrorTypePOSIX);
3234 else
3235 error.SetErrorToGenericError();
3236 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00003237 else
3238 error.SetErrorToGenericError();
3239 }
Greg Claytonfbb76342013-11-20 21:07:01 +00003240 else
3241 {
3242 file_permissions = mode & (S_IRWXU|S_IRWXG|S_IRWXO);
3243 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00003244 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00003245 }
3246 else
3247 {
3248 error.SetErrorStringWithFormat ("failed to send '%s' packet", packet);
3249 }
Greg Claytonfbb76342013-11-20 21:07:01 +00003250 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00003251}
3252
3253uint64_t
3254GDBRemoteCommunicationClient::ReadFile (lldb::user_id_t fd,
3255 uint64_t offset,
3256 void *dst,
3257 uint64_t dst_len,
3258 Error &error)
3259{
3260 lldb_private::StreamString stream;
3261 stream.Printf("vFile:pread:%i,%" PRId64 ",%" PRId64, (int)fd, dst_len, offset);
3262 const char* packet = stream.GetData();
3263 int packet_len = stream.GetSize();
3264 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003265 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003266 {
3267 if (response.GetChar() != 'F')
3268 return 0;
3269 uint32_t retcode = response.GetHexMaxU32(false, UINT32_MAX);
3270 if (retcode == UINT32_MAX)
3271 return retcode;
3272 const char next = (response.Peek() ? *response.Peek() : 0);
3273 if (next == ',')
3274 return 0;
3275 if (next == ';')
3276 {
3277 response.GetChar(); // skip the semicolon
3278 std::string buffer;
3279 if (response.GetEscapedBinaryData(buffer))
3280 {
3281 const uint64_t data_to_write = std::min<uint64_t>(dst_len, buffer.size());
3282 if (data_to_write > 0)
3283 memcpy(dst, &buffer[0], data_to_write);
3284 return data_to_write;
3285 }
3286 }
3287 }
3288 return 0;
3289}
3290
3291uint64_t
3292GDBRemoteCommunicationClient::WriteFile (lldb::user_id_t fd,
3293 uint64_t offset,
3294 const void* src,
3295 uint64_t src_len,
3296 Error &error)
3297{
3298 lldb_private::StreamGDBRemote stream;
3299 stream.Printf("vFile:pwrite:%i,%" PRId64 ",", (int)fd, offset);
3300 stream.PutEscapedBytes(src, src_len);
3301 const char* packet = stream.GetData();
3302 int packet_len = stream.GetSize();
3303 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003304 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003305 {
3306 if (response.GetChar() != 'F')
3307 {
3308 error.SetErrorStringWithFormat("write file failed");
3309 return 0;
3310 }
3311 uint64_t bytes_written = response.GetU64(UINT64_MAX);
3312 if (bytes_written == UINT64_MAX)
3313 {
3314 error.SetErrorToGenericError();
3315 if (response.GetChar() == ',')
3316 {
3317 int response_errno = response.GetS32(-1);
3318 if (response_errno > 0)
3319 error.SetError(response_errno, lldb::eErrorTypePOSIX);
3320 }
3321 return 0;
3322 }
3323 return bytes_written;
3324 }
3325 else
3326 {
3327 error.SetErrorString ("failed to send vFile:pwrite packet");
3328 }
3329 return 0;
3330}
3331
Greg Claytonfbb76342013-11-20 21:07:01 +00003332Error
3333GDBRemoteCommunicationClient::CreateSymlink (const char *src, const char *dst)
3334{
3335 Error error;
3336 lldb_private::StreamGDBRemote stream;
3337 stream.PutCString("vFile:symlink:");
3338 // the unix symlink() command reverses its parameters where the dst if first,
3339 // so we follow suit here
3340 stream.PutCStringAsRawHex8(dst);
3341 stream.PutChar(',');
3342 stream.PutCStringAsRawHex8(src);
3343 const char* packet = stream.GetData();
3344 int packet_len = stream.GetSize();
3345 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003346 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Claytonfbb76342013-11-20 21:07:01 +00003347 {
3348 if (response.GetChar() == 'F')
3349 {
3350 uint32_t result = response.GetU32(UINT32_MAX);
3351 if (result != 0)
3352 {
3353 error.SetErrorToGenericError();
3354 if (response.GetChar() == ',')
3355 {
3356 int response_errno = response.GetS32(-1);
3357 if (response_errno > 0)
3358 error.SetError(response_errno, lldb::eErrorTypePOSIX);
3359 }
3360 }
3361 }
3362 else
3363 {
3364 // Should have returned with 'F<result>[,<errno>]'
3365 error.SetErrorStringWithFormat("symlink failed");
3366 }
3367 }
3368 else
3369 {
3370 error.SetErrorString ("failed to send vFile:symlink packet");
3371 }
3372 return error;
3373}
3374
3375Error
3376GDBRemoteCommunicationClient::Unlink (const char *path)
3377{
3378 Error error;
3379 lldb_private::StreamGDBRemote stream;
3380 stream.PutCString("vFile:unlink:");
3381 // the unix symlink() command reverses its parameters where the dst if first,
3382 // so we follow suit here
3383 stream.PutCStringAsRawHex8(path);
3384 const char* packet = stream.GetData();
3385 int packet_len = stream.GetSize();
3386 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003387 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Claytonfbb76342013-11-20 21:07:01 +00003388 {
3389 if (response.GetChar() == 'F')
3390 {
3391 uint32_t result = response.GetU32(UINT32_MAX);
3392 if (result != 0)
3393 {
3394 error.SetErrorToGenericError();
3395 if (response.GetChar() == ',')
3396 {
3397 int response_errno = response.GetS32(-1);
3398 if (response_errno > 0)
3399 error.SetError(response_errno, lldb::eErrorTypePOSIX);
3400 }
3401 }
3402 }
3403 else
3404 {
3405 // Should have returned with 'F<result>[,<errno>]'
3406 error.SetErrorStringWithFormat("unlink failed");
3407 }
3408 }
3409 else
3410 {
3411 error.SetErrorString ("failed to send vFile:unlink packet");
3412 }
3413 return error;
3414}
3415
Daniel Maleae0f8f572013-08-26 23:57:52 +00003416// Extension of host I/O packets to get whether a file exists.
3417bool
3418GDBRemoteCommunicationClient::GetFileExists (const lldb_private::FileSpec& file_spec)
3419{
3420 lldb_private::StreamString stream;
3421 stream.PutCString("vFile:exists:");
3422 std::string path (file_spec.GetPath());
3423 stream.PutCStringAsRawHex8(path.c_str());
3424 const char* packet = stream.GetData();
3425 int packet_len = stream.GetSize();
3426 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003427 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003428 {
3429 if (response.GetChar() != 'F')
3430 return false;
3431 if (response.GetChar() != ',')
3432 return false;
3433 bool retcode = (response.GetChar() != '0');
3434 return retcode;
3435 }
3436 return false;
3437}
3438
3439bool
3440GDBRemoteCommunicationClient::CalculateMD5 (const lldb_private::FileSpec& file_spec,
3441 uint64_t &high,
3442 uint64_t &low)
3443{
3444 lldb_private::StreamString stream;
3445 stream.PutCString("vFile:MD5:");
3446 std::string path (file_spec.GetPath());
3447 stream.PutCStringAsRawHex8(path.c_str());
3448 const char* packet = stream.GetData();
3449 int packet_len = stream.GetSize();
3450 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003451 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003452 {
3453 if (response.GetChar() != 'F')
3454 return false;
3455 if (response.GetChar() != ',')
3456 return false;
3457 if (response.Peek() && *response.Peek() == 'x')
3458 return false;
3459 low = response.GetHexMaxU64(false, UINT64_MAX);
3460 high = response.GetHexMaxU64(false, UINT64_MAX);
3461 return true;
3462 }
3463 return false;
3464}
Greg Claytonf74cf862013-11-13 23:28:31 +00003465
3466bool
Jason Molendaa3329782014-03-29 18:54:20 +00003467GDBRemoteCommunicationClient::AvoidGPackets (ProcessGDBRemote *process)
3468{
3469 // Some targets have issues with g/G packets and we need to avoid using them
3470 if (m_avoid_g_packets == eLazyBoolCalculate)
3471 {
3472 if (process)
3473 {
3474 m_avoid_g_packets = eLazyBoolNo;
3475 const ArchSpec &arch = process->GetTarget().GetArchitecture();
3476 if (arch.IsValid()
3477 && arch.GetTriple().getVendor() == llvm::Triple::Apple
3478 && arch.GetTriple().getOS() == llvm::Triple::IOS
Todd Fialad8eaa172014-07-23 14:37:35 +00003479 && arch.GetTriple().getArch() == llvm::Triple::aarch64)
Jason Molendaa3329782014-03-29 18:54:20 +00003480 {
3481 m_avoid_g_packets = eLazyBoolYes;
3482 uint32_t gdb_server_version = GetGDBServerProgramVersion();
3483 if (gdb_server_version != 0)
3484 {
3485 const char *gdb_server_name = GetGDBServerProgramName();
3486 if (gdb_server_name && strcmp(gdb_server_name, "debugserver") == 0)
3487 {
3488 if (gdb_server_version >= 310)
3489 m_avoid_g_packets = eLazyBoolNo;
3490 }
3491 }
3492 }
3493 }
3494 }
3495 return m_avoid_g_packets == eLazyBoolYes;
3496}
3497
3498bool
Greg Claytonf74cf862013-11-13 23:28:31 +00003499GDBRemoteCommunicationClient::ReadRegister(lldb::tid_t tid, uint32_t reg, StringExtractorGDBRemote &response)
3500{
3501 Mutex::Locker locker;
3502 if (GetSequenceMutex (locker, "Didn't get sequence mutex for p packet."))
3503 {
3504 const bool thread_suffix_supported = GetThreadSuffixSupported();
3505
3506 if (thread_suffix_supported || SetCurrentThread(tid))
3507 {
3508 char packet[64];
3509 int packet_len = 0;
3510 if (thread_suffix_supported)
3511 packet_len = ::snprintf (packet, sizeof(packet), "p%x;thread:%4.4" PRIx64 ";", reg, tid);
3512 else
3513 packet_len = ::snprintf (packet, sizeof(packet), "p%x", reg);
3514 assert (packet_len < ((int)sizeof(packet) - 1));
Greg Clayton3dedae12013-12-06 21:45:27 +00003515 return SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success;
Greg Claytonf74cf862013-11-13 23:28:31 +00003516 }
3517 }
3518 return false;
3519
3520}
3521
3522
3523bool
3524GDBRemoteCommunicationClient::ReadAllRegisters (lldb::tid_t tid, StringExtractorGDBRemote &response)
3525{
3526 Mutex::Locker locker;
3527 if (GetSequenceMutex (locker, "Didn't get sequence mutex for g packet."))
3528 {
3529 const bool thread_suffix_supported = GetThreadSuffixSupported();
3530
3531 if (thread_suffix_supported || SetCurrentThread(tid))
3532 {
3533 char packet[64];
3534 int packet_len = 0;
3535 // Get all registers in one packet
3536 if (thread_suffix_supported)
3537 packet_len = ::snprintf (packet, sizeof(packet), "g;thread:%4.4" PRIx64 ";", tid);
3538 else
3539 packet_len = ::snprintf (packet, sizeof(packet), "g");
3540 assert (packet_len < ((int)sizeof(packet) - 1));
Greg Clayton3dedae12013-12-06 21:45:27 +00003541 return SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success;
Greg Claytonf74cf862013-11-13 23:28:31 +00003542 }
3543 }
3544 return false;
3545}
3546bool
3547GDBRemoteCommunicationClient::SaveRegisterState (lldb::tid_t tid, uint32_t &save_id)
3548{
3549 save_id = 0; // Set to invalid save ID
3550 if (m_supports_QSaveRegisterState == eLazyBoolNo)
3551 return false;
3552
3553 m_supports_QSaveRegisterState = eLazyBoolYes;
3554 Mutex::Locker locker;
3555 if (GetSequenceMutex (locker, "Didn't get sequence mutex for QSaveRegisterState."))
3556 {
3557 const bool thread_suffix_supported = GetThreadSuffixSupported();
3558 if (thread_suffix_supported || SetCurrentThread(tid))
3559 {
3560 char packet[256];
3561 if (thread_suffix_supported)
3562 ::snprintf (packet, sizeof(packet), "QSaveRegisterState;thread:%4.4" PRIx64 ";", tid);
3563 else
3564 ::strncpy (packet, "QSaveRegisterState", sizeof(packet));
3565
3566 StringExtractorGDBRemote response;
3567
Greg Clayton3dedae12013-12-06 21:45:27 +00003568 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
Greg Claytonf74cf862013-11-13 23:28:31 +00003569 {
3570 if (response.IsUnsupportedResponse())
3571 {
3572 // This packet isn't supported, don't try calling it again
3573 m_supports_QSaveRegisterState = eLazyBoolNo;
3574 }
3575
3576 const uint32_t response_save_id = response.GetU32(0);
3577 if (response_save_id != 0)
3578 {
3579 save_id = response_save_id;
3580 return true;
3581 }
3582 }
3583 }
3584 }
3585 return false;
3586}
3587
3588bool
3589GDBRemoteCommunicationClient::RestoreRegisterState (lldb::tid_t tid, uint32_t save_id)
3590{
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003591 // We use the "m_supports_QSaveRegisterState" variable here because the
Greg Claytonf74cf862013-11-13 23:28:31 +00003592 // QSaveRegisterState and QRestoreRegisterState packets must both be supported in
3593 // order to be useful
3594 if (m_supports_QSaveRegisterState == eLazyBoolNo)
3595 return false;
3596
3597 Mutex::Locker locker;
3598 if (GetSequenceMutex (locker, "Didn't get sequence mutex for QRestoreRegisterState."))
3599 {
3600 const bool thread_suffix_supported = GetThreadSuffixSupported();
3601 if (thread_suffix_supported || SetCurrentThread(tid))
3602 {
3603 char packet[256];
3604 if (thread_suffix_supported)
3605 ::snprintf (packet, sizeof(packet), "QRestoreRegisterState:%u;thread:%4.4" PRIx64 ";", save_id, tid);
3606 else
3607 ::snprintf (packet, sizeof(packet), "QRestoreRegisterState:%u" PRIx64 ";", save_id);
3608
3609 StringExtractorGDBRemote response;
3610
Greg Clayton3dedae12013-12-06 21:45:27 +00003611 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
Greg Claytonf74cf862013-11-13 23:28:31 +00003612 {
3613 if (response.IsOKResponse())
3614 {
3615 return true;
3616 }
3617 else if (response.IsUnsupportedResponse())
3618 {
3619 // This packet isn't supported, don't try calling this packet or
3620 // QSaveRegisterState again...
3621 m_supports_QSaveRegisterState = eLazyBoolNo;
3622 }
3623 }
3624 }
3625 }
3626 return false;
3627}