blob: 8f81278c7b58178b5a1ed3111ad234683d86f253 [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"
Greg Clayton576d8832011-03-22 04:00:09 +000023#include "lldb/Core/Log.h"
24#include "lldb/Core/State.h"
Daniel Maleae0f8f572013-08-26 23:57:52 +000025#include "lldb/Core/StreamGDBRemote.h"
Greg Clayton576d8832011-03-22 04:00:09 +000026#include "lldb/Core/StreamString.h"
Zachary Turner93a66fc2014-10-06 21:22:36 +000027#include "lldb/Host/ConnectionFileDescriptor.h"
Greg Clayton576d8832011-03-22 04:00:09 +000028#include "lldb/Host/Endian.h"
29#include "lldb/Host/Host.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000030#include "lldb/Host/HostInfo.h"
Vince Harron5275aaa2015-01-15 20:08:35 +000031#include "lldb/Host/StringConvert.h"
Greg Clayton576d8832011-03-22 04:00:09 +000032#include "lldb/Host/TimeValue.h"
Jason Molendaa3329782014-03-29 18:54:20 +000033#include "lldb/Target/Target.h"
Greg Clayton576d8832011-03-22 04:00:09 +000034
35// Project includes
36#include "Utility/StringExtractorGDBRemote.h"
37#include "ProcessGDBRemote.h"
38#include "ProcessGDBRemoteLog.h"
Virgile Bellob2f1fb22013-08-23 12:44:05 +000039#include "lldb/Host/Config.h"
Greg Clayton576d8832011-03-22 04:00:09 +000040
41using namespace lldb;
42using namespace lldb_private;
43
Todd Fiala0a70a842014-05-28 16:43:26 +000044#if defined(LLDB_DISABLE_POSIX) && !defined(SIGSTOP)
Virgile Bellob2f1fb22013-08-23 12:44:05 +000045#define SIGSTOP 17
46#endif
47
Greg Clayton576d8832011-03-22 04:00:09 +000048//----------------------------------------------------------------------
49// GDBRemoteCommunicationClient constructor
50//----------------------------------------------------------------------
Tamas Berghammere13c2732015-02-11 10:29:30 +000051GDBRemoteCommunicationClient::GDBRemoteCommunicationClient() :
52 GDBRemoteCommunication("gdb-remote.client", "gdb-remote.client.rx_packet"),
Greg Clayton576d8832011-03-22 04:00:09 +000053 m_supports_not_sending_acks (eLazyBoolCalculate),
54 m_supports_thread_suffix (eLazyBoolCalculate),
Greg Clayton44633992012-04-10 03:22:03 +000055 m_supports_threads_in_stop_reply (eLazyBoolCalculate),
Greg Clayton576d8832011-03-22 04:00:09 +000056 m_supports_vCont_all (eLazyBoolCalculate),
57 m_supports_vCont_any (eLazyBoolCalculate),
58 m_supports_vCont_c (eLazyBoolCalculate),
59 m_supports_vCont_C (eLazyBoolCalculate),
60 m_supports_vCont_s (eLazyBoolCalculate),
61 m_supports_vCont_S (eLazyBoolCalculate),
Greg Clayton32e0a752011-03-30 18:16:51 +000062 m_qHostInfo_is_valid (eLazyBoolCalculate),
Todd Fiala9f72b3a2014-05-07 19:28:21 +000063 m_curr_pid_is_valid (eLazyBoolCalculate),
Jason Molendaf17b5ac2012-12-19 02:54:03 +000064 m_qProcessInfo_is_valid (eLazyBoolCalculate),
Jason Molendaa3329782014-03-29 18:54:20 +000065 m_qGDBServerVersion_is_valid (eLazyBoolCalculate),
Greg Clayton70b57652011-05-15 01:25:55 +000066 m_supports_alloc_dealloc_memory (eLazyBoolCalculate),
Greg Clayton46fb5582011-11-18 07:03:08 +000067 m_supports_memory_region_info (eLazyBoolCalculate),
Johnny Chen64637202012-05-23 21:09:52 +000068 m_supports_watchpoint_support_info (eLazyBoolCalculate),
Jim Inghamacff8952013-05-02 00:27:30 +000069 m_supports_detach_stay_stopped (eLazyBoolCalculate),
Enrico Granataf04a2192012-07-13 23:18:48 +000070 m_watchpoints_trigger_after_instruction(eLazyBoolCalculate),
Jim Inghamcd16df92012-07-20 21:37:13 +000071 m_attach_or_wait_reply(eLazyBoolCalculate),
Jim Ingham279ceec2012-07-25 21:12:43 +000072 m_prepare_for_reg_writing_reply (eLazyBoolCalculate),
Eric Christopher2490f5c2013-08-30 17:50:57 +000073 m_supports_p (eLazyBoolCalculate),
Jason Molendabdc4f122014-05-06 02:59:39 +000074 m_supports_x (eLazyBoolCalculate),
Jason Molendaa3329782014-03-29 18:54:20 +000075 m_avoid_g_packets (eLazyBoolCalculate),
Greg Claytonf74cf862013-11-13 23:28:31 +000076 m_supports_QSaveRegisterState (eLazyBoolCalculate),
Steve Pucci03904ac2014-03-04 23:18:46 +000077 m_supports_qXfer_auxv_read (eLazyBoolCalculate),
Steve Pucci5ae54ae2014-01-25 05:46:51 +000078 m_supports_qXfer_libraries_read (eLazyBoolCalculate),
79 m_supports_qXfer_libraries_svr4_read (eLazyBoolCalculate),
80 m_supports_augmented_libraries_svr4_read (eLazyBoolCalculate),
Jason Molenda705b1802014-06-13 02:37:02 +000081 m_supports_jThreadExtendedInfo (eLazyBoolCalculate),
Greg Clayton32e0a752011-03-30 18:16:51 +000082 m_supports_qProcessInfoPID (true),
83 m_supports_qfProcessInfo (true),
84 m_supports_qUserName (true),
85 m_supports_qGroupName (true),
Greg Clayton8b82f082011-04-12 05:54:46 +000086 m_supports_qThreadStopInfo (true),
87 m_supports_z0 (true),
88 m_supports_z1 (true),
89 m_supports_z2 (true),
90 m_supports_z3 (true),
91 m_supports_z4 (true),
Greg Clayton89600582013-10-10 17:53:50 +000092 m_supports_QEnvironment (true),
93 m_supports_QEnvironmentHexEncoded (true),
Todd Fiala9f72b3a2014-05-07 19:28:21 +000094 m_curr_pid (LLDB_INVALID_PROCESS_ID),
Greg Clayton8b82f082011-04-12 05:54:46 +000095 m_curr_tid (LLDB_INVALID_THREAD_ID),
96 m_curr_tid_run (LLDB_INVALID_THREAD_ID),
Johnny Chen64637202012-05-23 21:09:52 +000097 m_num_supported_hardware_watchpoints (0),
Greg Clayton576d8832011-03-22 04:00:09 +000098 m_async_mutex (Mutex::eMutexTypeRecursive),
99 m_async_packet_predicate (false),
100 m_async_packet (),
Jim Inghama6195b72013-12-18 01:24:33 +0000101 m_async_result (PacketResult::Success),
Greg Clayton576d8832011-03-22 04:00:09 +0000102 m_async_response (),
103 m_async_signal (-1),
Jim Inghamb8cd5752014-04-16 02:24:17 +0000104 m_interrupt_sent (false),
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000105 m_thread_id_to_used_usec_map (),
Greg Clayton1cb64962011-03-24 04:28:38 +0000106 m_host_arch(),
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000107 m_process_arch(),
Greg Clayton1cb64962011-03-24 04:28:38 +0000108 m_os_version_major (UINT32_MAX),
109 m_os_version_minor (UINT32_MAX),
Greg Clayton9ac6d2d2013-10-25 18:13:17 +0000110 m_os_version_update (UINT32_MAX),
111 m_os_build (),
112 m_os_kernel (),
113 m_hostname (),
Jason Molendaa3329782014-03-29 18:54:20 +0000114 m_gdb_server_name(),
115 m_gdb_server_version(UINT32_MAX),
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000116 m_default_packet_timeout (0),
117 m_max_packet_size (0)
Greg Clayton576d8832011-03-22 04:00:09 +0000118{
Greg Clayton576d8832011-03-22 04:00:09 +0000119}
120
121//----------------------------------------------------------------------
122// Destructor
123//----------------------------------------------------------------------
124GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient()
125{
Greg Clayton576d8832011-03-22 04:00:09 +0000126 if (IsConnected())
Greg Clayton576d8832011-03-22 04:00:09 +0000127 Disconnect();
Greg Clayton576d8832011-03-22 04:00:09 +0000128}
129
130bool
Greg Clayton1cb64962011-03-24 04:28:38 +0000131GDBRemoteCommunicationClient::HandshakeWithServer (Error *error_ptr)
132{
Greg Claytonfb909312013-11-23 01:58:15 +0000133 ResetDiscoverableSettings();
134
Greg Clayton1cb64962011-03-24 04:28:38 +0000135 // Start the read thread after we send the handshake ack since if we
136 // fail to send the handshake ack, there is no reason to continue...
137 if (SendAck())
Greg Claytonfb909312013-11-23 01:58:15 +0000138 {
Ed Maste48f986f2013-12-18 15:31:45 +0000139 // Wait for any responses that might have been queued up in the remote
140 // GDB server and flush them all
141 StringExtractorGDBRemote response;
142 PacketResult packet_result = PacketResult::Success;
143 const uint32_t timeout_usec = 10 * 1000; // Wait for 10 ms for a response
144 while (packet_result == PacketResult::Success)
145 packet_result = WaitForPacketWithTimeoutMicroSecondsNoLock (response, timeout_usec);
146
Greg Claytonfb909312013-11-23 01:58:15 +0000147 // The return value from QueryNoAckModeSupported() is true if the packet
148 // was sent and _any_ response (including UNIMPLEMENTED) was received),
149 // or false if no response was received. This quickly tells us if we have
150 // a live connection to a remote GDB server...
151 if (QueryNoAckModeSupported())
152 {
Greg Clayton700e5082014-02-21 19:11:28 +0000153#if 0
154 // Set above line to "#if 1" to test packet speed if remote GDB server
155 // supports the qSpeedTest packet...
156 TestPacketSpeed(10000);
157#endif
Greg Claytonfb909312013-11-23 01:58:15 +0000158 return true;
159 }
160 else
161 {
162 if (error_ptr)
163 error_ptr->SetErrorString("failed to get reply to handshake packet");
164 }
165 }
166 else
167 {
168 if (error_ptr)
169 error_ptr->SetErrorString("failed to send the handshake ack");
170 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000171 return false;
172}
173
Greg Claytonfb909312013-11-23 01:58:15 +0000174bool
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000175GDBRemoteCommunicationClient::GetAugmentedLibrariesSVR4ReadSupported ()
176{
177 if (m_supports_augmented_libraries_svr4_read == eLazyBoolCalculate)
178 {
179 GetRemoteQSupported();
180 }
181 return (m_supports_augmented_libraries_svr4_read == eLazyBoolYes);
182}
183
184bool
185GDBRemoteCommunicationClient::GetQXferLibrariesSVR4ReadSupported ()
186{
187 if (m_supports_qXfer_libraries_svr4_read == eLazyBoolCalculate)
188 {
189 GetRemoteQSupported();
190 }
191 return (m_supports_qXfer_libraries_svr4_read == eLazyBoolYes);
192}
193
194bool
195GDBRemoteCommunicationClient::GetQXferLibrariesReadSupported ()
196{
197 if (m_supports_qXfer_libraries_read == eLazyBoolCalculate)
198 {
199 GetRemoteQSupported();
200 }
201 return (m_supports_qXfer_libraries_read == eLazyBoolYes);
202}
203
Steve Pucci03904ac2014-03-04 23:18:46 +0000204bool
205GDBRemoteCommunicationClient::GetQXferAuxvReadSupported ()
206{
207 if (m_supports_qXfer_auxv_read == eLazyBoolCalculate)
208 {
209 GetRemoteQSupported();
210 }
211 return (m_supports_qXfer_auxv_read == eLazyBoolYes);
212}
213
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000214uint64_t
215GDBRemoteCommunicationClient::GetRemoteMaxPacketSize()
216{
217 if (m_max_packet_size == 0)
218 {
219 GetRemoteQSupported();
220 }
221 return m_max_packet_size;
222}
223
224bool
Greg Clayton1cb64962011-03-24 04:28:38 +0000225GDBRemoteCommunicationClient::QueryNoAckModeSupported ()
Greg Clayton576d8832011-03-22 04:00:09 +0000226{
227 if (m_supports_not_sending_acks == eLazyBoolCalculate)
228 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000229 m_send_acks = true;
Greg Clayton576d8832011-03-22 04:00:09 +0000230 m_supports_not_sending_acks = eLazyBoolNo;
Greg Clayton1cb64962011-03-24 04:28:38 +0000231
Jason Molenda36a216e2014-07-24 01:36:24 +0000232 // This is the first real packet that we'll send in a debug session and it may take a little
233 // longer than normal to receive a reply. Wait at least 6 seconds for a reply to this packet.
234
235 const uint32_t minimum_timeout = 6;
236 uint32_t old_timeout = GetPacketTimeoutInMicroSeconds() / lldb_private::TimeValue::MicroSecPerSec;
237 SetPacketTimeout (std::max (old_timeout, minimum_timeout));
238
Greg Clayton1cb64962011-03-24 04:28:38 +0000239 StringExtractorGDBRemote response;
Jason Molenda36a216e2014-07-24 01:36:24 +0000240 PacketResult packet_send_result = SendPacketAndWaitForResponse("QStartNoAckMode", response, false);
241 SetPacketTimeout (old_timeout);
242
243 if (packet_send_result == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000244 {
245 if (response.IsOKResponse())
Greg Clayton1cb64962011-03-24 04:28:38 +0000246 {
247 m_send_acks = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000248 m_supports_not_sending_acks = eLazyBoolYes;
Greg Clayton1cb64962011-03-24 04:28:38 +0000249 }
Greg Claytonfb909312013-11-23 01:58:15 +0000250 return true;
Greg Clayton576d8832011-03-22 04:00:09 +0000251 }
252 }
Greg Claytonfb909312013-11-23 01:58:15 +0000253 return false;
Greg Clayton576d8832011-03-22 04:00:09 +0000254}
255
256void
Greg Clayton44633992012-04-10 03:22:03 +0000257GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported ()
258{
259 if (m_supports_threads_in_stop_reply == eLazyBoolCalculate)
260 {
261 m_supports_threads_in_stop_reply = eLazyBoolNo;
262
263 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000264 if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response, false) == PacketResult::Success)
Greg Clayton44633992012-04-10 03:22:03 +0000265 {
266 if (response.IsOKResponse())
267 m_supports_threads_in_stop_reply = eLazyBoolYes;
268 }
269 }
270}
271
Jim Inghamcd16df92012-07-20 21:37:13 +0000272bool
273GDBRemoteCommunicationClient::GetVAttachOrWaitSupported ()
274{
275 if (m_attach_or_wait_reply == eLazyBoolCalculate)
276 {
277 m_attach_or_wait_reply = eLazyBoolNo;
278
279 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000280 if (SendPacketAndWaitForResponse("qVAttachOrWaitSupported", response, false) == PacketResult::Success)
Jim Inghamcd16df92012-07-20 21:37:13 +0000281 {
282 if (response.IsOKResponse())
283 m_attach_or_wait_reply = eLazyBoolYes;
284 }
285 }
286 if (m_attach_or_wait_reply == eLazyBoolYes)
287 return true;
288 else
289 return false;
290}
291
Jim Ingham279ceec2012-07-25 21:12:43 +0000292bool
293GDBRemoteCommunicationClient::GetSyncThreadStateSupported ()
294{
295 if (m_prepare_for_reg_writing_reply == eLazyBoolCalculate)
296 {
297 m_prepare_for_reg_writing_reply = eLazyBoolNo;
298
299 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000300 if (SendPacketAndWaitForResponse("qSyncThreadStateSupported", response, false) == PacketResult::Success)
Jim Ingham279ceec2012-07-25 21:12:43 +0000301 {
302 if (response.IsOKResponse())
303 m_prepare_for_reg_writing_reply = eLazyBoolYes;
304 }
305 }
306 if (m_prepare_for_reg_writing_reply == eLazyBoolYes)
307 return true;
308 else
309 return false;
310}
311
Greg Clayton44633992012-04-10 03:22:03 +0000312
313void
Greg Clayton576d8832011-03-22 04:00:09 +0000314GDBRemoteCommunicationClient::ResetDiscoverableSettings()
315{
316 m_supports_not_sending_acks = eLazyBoolCalculate;
317 m_supports_thread_suffix = eLazyBoolCalculate;
Greg Clayton44633992012-04-10 03:22:03 +0000318 m_supports_threads_in_stop_reply = eLazyBoolCalculate;
Greg Clayton576d8832011-03-22 04:00:09 +0000319 m_supports_vCont_c = eLazyBoolCalculate;
320 m_supports_vCont_C = eLazyBoolCalculate;
321 m_supports_vCont_s = eLazyBoolCalculate;
322 m_supports_vCont_S = eLazyBoolCalculate;
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000323 m_supports_p = eLazyBoolCalculate;
Jason Molendabdc4f122014-05-06 02:59:39 +0000324 m_supports_x = eLazyBoolCalculate;
Greg Claytonf74cf862013-11-13 23:28:31 +0000325 m_supports_QSaveRegisterState = eLazyBoolCalculate;
Greg Clayton32e0a752011-03-30 18:16:51 +0000326 m_qHostInfo_is_valid = eLazyBoolCalculate;
Todd Fiala9f72b3a2014-05-07 19:28:21 +0000327 m_curr_pid_is_valid = eLazyBoolCalculate;
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000328 m_qProcessInfo_is_valid = eLazyBoolCalculate;
Jason Molendaa3329782014-03-29 18:54:20 +0000329 m_qGDBServerVersion_is_valid = eLazyBoolCalculate;
Greg Clayton70b57652011-05-15 01:25:55 +0000330 m_supports_alloc_dealloc_memory = eLazyBoolCalculate;
Greg Clayton46fb5582011-11-18 07:03:08 +0000331 m_supports_memory_region_info = eLazyBoolCalculate;
Jim Ingham279ceec2012-07-25 21:12:43 +0000332 m_prepare_for_reg_writing_reply = eLazyBoolCalculate;
333 m_attach_or_wait_reply = eLazyBoolCalculate;
Jason Molendaa3329782014-03-29 18:54:20 +0000334 m_avoid_g_packets = eLazyBoolCalculate;
Steve Pucci03904ac2014-03-04 23:18:46 +0000335 m_supports_qXfer_auxv_read = eLazyBoolCalculate;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000336 m_supports_qXfer_libraries_read = eLazyBoolCalculate;
337 m_supports_qXfer_libraries_svr4_read = eLazyBoolCalculate;
338 m_supports_augmented_libraries_svr4_read = eLazyBoolCalculate;
Greg Clayton2a48f522011-05-14 01:50:35 +0000339
Greg Clayton32e0a752011-03-30 18:16:51 +0000340 m_supports_qProcessInfoPID = true;
341 m_supports_qfProcessInfo = true;
342 m_supports_qUserName = true;
343 m_supports_qGroupName = true;
Greg Clayton8b82f082011-04-12 05:54:46 +0000344 m_supports_qThreadStopInfo = true;
345 m_supports_z0 = true;
346 m_supports_z1 = true;
347 m_supports_z2 = true;
348 m_supports_z3 = true;
349 m_supports_z4 = true;
Greg Clayton89600582013-10-10 17:53:50 +0000350 m_supports_QEnvironment = true;
351 m_supports_QEnvironmentHexEncoded = true;
Jason Molendaa3329782014-03-29 18:54:20 +0000352
Greg Claytond314e812011-03-23 00:09:55 +0000353 m_host_arch.Clear();
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000354 m_process_arch.Clear();
Jason Molendaa3329782014-03-29 18:54:20 +0000355 m_os_version_major = UINT32_MAX;
356 m_os_version_minor = UINT32_MAX;
357 m_os_version_update = UINT32_MAX;
358 m_os_build.clear();
359 m_os_kernel.clear();
360 m_hostname.clear();
361 m_gdb_server_name.clear();
362 m_gdb_server_version = UINT32_MAX;
363 m_default_packet_timeout = 0;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000364
365 m_max_packet_size = 0;
Greg Clayton576d8832011-03-22 04:00:09 +0000366}
367
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000368void
369GDBRemoteCommunicationClient::GetRemoteQSupported ()
370{
371 // Clear out any capabilities we expect to see in the qSupported response
Steve Pucci03904ac2014-03-04 23:18:46 +0000372 m_supports_qXfer_auxv_read = eLazyBoolNo;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000373 m_supports_qXfer_libraries_read = eLazyBoolNo;
Steve Pucci03904ac2014-03-04 23:18:46 +0000374 m_supports_qXfer_libraries_svr4_read = eLazyBoolNo;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000375 m_supports_augmented_libraries_svr4_read = eLazyBoolNo;
376 m_max_packet_size = UINT64_MAX; // It's supposed to always be there, but if not, we assume no limit
377
378 StringExtractorGDBRemote response;
379 if (SendPacketAndWaitForResponse("qSupported",
380 response,
381 /*send_async=*/false) == PacketResult::Success)
382 {
383 const char *response_cstr = response.GetStringRef().c_str();
Steve Pucci03904ac2014-03-04 23:18:46 +0000384 if (::strstr (response_cstr, "qXfer:auxv:read+"))
385 m_supports_qXfer_auxv_read = eLazyBoolYes;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000386 if (::strstr (response_cstr, "qXfer:libraries-svr4:read+"))
387 m_supports_qXfer_libraries_svr4_read = eLazyBoolYes;
388 if (::strstr (response_cstr, "augmented-libraries-svr4-read"))
389 {
390 m_supports_qXfer_libraries_svr4_read = eLazyBoolYes; // implied
391 m_supports_augmented_libraries_svr4_read = eLazyBoolYes;
392 }
393 if (::strstr (response_cstr, "qXfer:libraries:read+"))
394 m_supports_qXfer_libraries_read = eLazyBoolYes;
395
396 const char *packet_size_str = ::strstr (response_cstr, "PacketSize=");
397 if (packet_size_str)
398 {
399 StringExtractorGDBRemote packet_response(packet_size_str + strlen("PacketSize="));
400 m_max_packet_size = packet_response.GetHexMaxU64(/*little_endian=*/false, UINT64_MAX);
401 if (m_max_packet_size == 0)
402 {
403 m_max_packet_size = UINT64_MAX; // Must have been a garbled response
404 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
405 if (log)
406 log->Printf ("Garbled PacketSize spec in qSupported response");
407 }
408 }
409 }
410}
Greg Clayton576d8832011-03-22 04:00:09 +0000411
412bool
413GDBRemoteCommunicationClient::GetThreadSuffixSupported ()
414{
415 if (m_supports_thread_suffix == eLazyBoolCalculate)
416 {
417 StringExtractorGDBRemote response;
418 m_supports_thread_suffix = eLazyBoolNo;
Greg Clayton3dedae12013-12-06 21:45:27 +0000419 if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000420 {
421 if (response.IsOKResponse())
422 m_supports_thread_suffix = eLazyBoolYes;
423 }
424 }
425 return m_supports_thread_suffix;
426}
427bool
428GDBRemoteCommunicationClient::GetVContSupported (char flavor)
429{
430 if (m_supports_vCont_c == eLazyBoolCalculate)
431 {
432 StringExtractorGDBRemote response;
433 m_supports_vCont_any = eLazyBoolNo;
434 m_supports_vCont_all = eLazyBoolNo;
435 m_supports_vCont_c = eLazyBoolNo;
436 m_supports_vCont_C = eLazyBoolNo;
437 m_supports_vCont_s = eLazyBoolNo;
438 m_supports_vCont_S = eLazyBoolNo;
Greg Clayton3dedae12013-12-06 21:45:27 +0000439 if (SendPacketAndWaitForResponse("vCont?", response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000440 {
441 const char *response_cstr = response.GetStringRef().c_str();
442 if (::strstr (response_cstr, ";c"))
443 m_supports_vCont_c = eLazyBoolYes;
444
445 if (::strstr (response_cstr, ";C"))
446 m_supports_vCont_C = eLazyBoolYes;
447
448 if (::strstr (response_cstr, ";s"))
449 m_supports_vCont_s = eLazyBoolYes;
450
451 if (::strstr (response_cstr, ";S"))
452 m_supports_vCont_S = eLazyBoolYes;
453
454 if (m_supports_vCont_c == eLazyBoolYes &&
455 m_supports_vCont_C == eLazyBoolYes &&
456 m_supports_vCont_s == eLazyBoolYes &&
457 m_supports_vCont_S == eLazyBoolYes)
458 {
459 m_supports_vCont_all = eLazyBoolYes;
460 }
461
462 if (m_supports_vCont_c == eLazyBoolYes ||
463 m_supports_vCont_C == eLazyBoolYes ||
464 m_supports_vCont_s == eLazyBoolYes ||
465 m_supports_vCont_S == eLazyBoolYes)
466 {
467 m_supports_vCont_any = eLazyBoolYes;
468 }
469 }
470 }
471
472 switch (flavor)
473 {
474 case 'a': return m_supports_vCont_any;
475 case 'A': return m_supports_vCont_all;
476 case 'c': return m_supports_vCont_c;
477 case 'C': return m_supports_vCont_C;
478 case 's': return m_supports_vCont_s;
479 case 'S': return m_supports_vCont_S;
480 default: break;
481 }
482 return false;
483}
484
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000485// Check if the target supports 'p' packet. It sends out a 'p'
486// packet and checks the response. A normal packet will tell us
487// that support is available.
Sean Callananb1de1142013-09-04 23:24:15 +0000488//
489// Takes a valid thread ID because p needs to apply to a thread.
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000490bool
Sean Callananb1de1142013-09-04 23:24:15 +0000491GDBRemoteCommunicationClient::GetpPacketSupported (lldb::tid_t tid)
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000492{
493 if (m_supports_p == eLazyBoolCalculate)
494 {
495 StringExtractorGDBRemote response;
496 m_supports_p = eLazyBoolNo;
Sean Callananb1de1142013-09-04 23:24:15 +0000497 char packet[256];
498 if (GetThreadSuffixSupported())
499 snprintf(packet, sizeof(packet), "p0;thread:%" PRIx64 ";", tid);
500 else
501 snprintf(packet, sizeof(packet), "p0");
502
Greg Clayton3dedae12013-12-06 21:45:27 +0000503 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000504 {
505 if (response.IsNormalResponse())
506 m_supports_p = eLazyBoolYes;
507 }
508 }
509 return m_supports_p;
510}
Greg Clayton576d8832011-03-22 04:00:09 +0000511
Jason Molendabdc4f122014-05-06 02:59:39 +0000512bool
Jason Molenda705b1802014-06-13 02:37:02 +0000513GDBRemoteCommunicationClient::GetThreadExtendedInfoSupported ()
514{
515 if (m_supports_jThreadExtendedInfo == eLazyBoolCalculate)
516 {
517 StringExtractorGDBRemote response;
518 m_supports_jThreadExtendedInfo = eLazyBoolNo;
519 if (SendPacketAndWaitForResponse("jThreadExtendedInfo:", response, false) == PacketResult::Success)
520 {
521 if (response.IsOKResponse())
522 {
523 m_supports_jThreadExtendedInfo = eLazyBoolYes;
524 }
525 }
526 }
527 return m_supports_jThreadExtendedInfo;
528}
529
530bool
Jason Molendabdc4f122014-05-06 02:59:39 +0000531GDBRemoteCommunicationClient::GetxPacketSupported ()
532{
533 if (m_supports_x == eLazyBoolCalculate)
534 {
535 StringExtractorGDBRemote response;
536 m_supports_x = eLazyBoolNo;
537 char packet[256];
538 snprintf (packet, sizeof (packet), "x0,0");
539 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
540 {
541 if (response.IsOKResponse())
542 m_supports_x = eLazyBoolYes;
543 }
544 }
545 return m_supports_x;
546}
547
Greg Clayton3dedae12013-12-06 21:45:27 +0000548GDBRemoteCommunicationClient::PacketResult
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000549GDBRemoteCommunicationClient::SendPacketsAndConcatenateResponses
550(
551 const char *payload_prefix,
552 std::string &response_string
553)
554{
555 Mutex::Locker locker;
556 if (!GetSequenceMutex(locker,
557 "ProcessGDBRemote::SendPacketsAndConcatenateResponses() failed due to not getting the sequence mutex"))
558 {
559 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
560 if (log)
561 log->Printf("error: failed to get packet sequence mutex, not sending packets with prefix '%s'",
562 payload_prefix);
563 return PacketResult::ErrorNoSequenceLock;
564 }
565
566 response_string = "";
567 std::string payload_prefix_str(payload_prefix);
568 unsigned int response_size = 0x1000;
569 if (response_size > GetRemoteMaxPacketSize()) { // May send qSupported packet
570 response_size = GetRemoteMaxPacketSize();
571 }
572
573 for (unsigned int offset = 0; true; offset += response_size)
574 {
575 StringExtractorGDBRemote this_response;
576 // Construct payload
577 char sizeDescriptor[128];
578 snprintf(sizeDescriptor, sizeof(sizeDescriptor), "%x,%x", offset, response_size);
579 PacketResult result = SendPacketAndWaitForResponse((payload_prefix_str + sizeDescriptor).c_str(),
580 this_response,
581 /*send_async=*/false);
582 if (result != PacketResult::Success)
583 return result;
584
585 const std::string &this_string = this_response.GetStringRef();
586
587 // Check for m or l as first character; l seems to mean this is the last chunk
588 char first_char = *this_string.c_str();
589 if (first_char != 'm' && first_char != 'l')
590 {
591 return PacketResult::ErrorReplyInvalid;
592 }
Steve Pucci03904ac2014-03-04 23:18:46 +0000593 // Concatenate the result so far (skipping 'm' or 'l')
594 response_string.append(this_string, 1, std::string::npos);
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000595 if (first_char == 'l')
596 // We're done
597 return PacketResult::Success;
598 }
599}
600
601GDBRemoteCommunicationClient::PacketResult
Greg Clayton576d8832011-03-22 04:00:09 +0000602GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
603(
604 const char *payload,
605 StringExtractorGDBRemote &response,
606 bool send_async
607)
608{
609 return SendPacketAndWaitForResponse (payload,
610 ::strlen (payload),
611 response,
612 send_async);
613}
614
Greg Clayton3dedae12013-12-06 21:45:27 +0000615GDBRemoteCommunicationClient::PacketResult
616GDBRemoteCommunicationClient::SendPacketAndWaitForResponseNoLock (const char *payload,
617 size_t payload_length,
618 StringExtractorGDBRemote &response)
619{
620 PacketResult packet_result = SendPacketNoLock (payload, payload_length);
621 if (packet_result == PacketResult::Success)
622 packet_result = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
623 return packet_result;
624}
625
626GDBRemoteCommunicationClient::PacketResult
Greg Clayton576d8832011-03-22 04:00:09 +0000627GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
628(
629 const char *payload,
630 size_t payload_length,
631 StringExtractorGDBRemote &response,
632 bool send_async
633)
634{
Greg Clayton3dedae12013-12-06 21:45:27 +0000635 PacketResult packet_result = PacketResult::ErrorSendFailed;
Greg Clayton576d8832011-03-22 04:00:09 +0000636 Mutex::Locker locker;
Greg Clayton5160ce52013-03-27 23:08:40 +0000637 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000638 if (GetSequenceMutex (locker))
Greg Clayton576d8832011-03-22 04:00:09 +0000639 {
Greg Clayton3dedae12013-12-06 21:45:27 +0000640 packet_result = SendPacketAndWaitForResponseNoLock (payload, payload_length, response);
Greg Clayton576d8832011-03-22 04:00:09 +0000641 }
642 else
643 {
644 if (send_async)
645 {
Greg Claytond3544052012-05-31 21:24:20 +0000646 if (IsRunning())
Greg Clayton576d8832011-03-22 04:00:09 +0000647 {
Greg Claytond3544052012-05-31 21:24:20 +0000648 Mutex::Locker async_locker (m_async_mutex);
649 m_async_packet.assign(payload, payload_length);
650 m_async_packet_predicate.SetValue (true, eBroadcastNever);
651
652 if (log)
653 log->Printf ("async: async packet = %s", m_async_packet.c_str());
654
655 bool timed_out = false;
656 if (SendInterrupt(locker, 2, timed_out))
Greg Clayton576d8832011-03-22 04:00:09 +0000657 {
Greg Claytond3544052012-05-31 21:24:20 +0000658 if (m_interrupt_sent)
Greg Clayton576d8832011-03-22 04:00:09 +0000659 {
Jim Inghambabfc382012-06-06 00:32:39 +0000660 m_interrupt_sent = false;
Greg Claytond3544052012-05-31 21:24:20 +0000661 TimeValue timeout_time;
662 timeout_time = TimeValue::Now();
663 timeout_time.OffsetWithSeconds (m_packet_timeout);
664
Greg Clayton576d8832011-03-22 04:00:09 +0000665 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000666 log->Printf ("async: sent interrupt");
Greg Clayton644247c2011-07-07 01:59:51 +0000667
Greg Claytond3544052012-05-31 21:24:20 +0000668 if (m_async_packet_predicate.WaitForValueEqualTo (false, &timeout_time, &timed_out))
Greg Claytone889ad62011-10-27 22:04:16 +0000669 {
Greg Claytond3544052012-05-31 21:24:20 +0000670 if (log)
671 log->Printf ("async: got response");
672
673 // Swap the response buffer to avoid malloc and string copy
674 response.GetStringRef().swap (m_async_response.GetStringRef());
Jim Inghama6195b72013-12-18 01:24:33 +0000675 packet_result = m_async_result;
Greg Claytond3544052012-05-31 21:24:20 +0000676 }
677 else
678 {
679 if (log)
680 log->Printf ("async: timed out waiting for response");
681 }
682
683 // Make sure we wait until the continue packet has been sent again...
684 if (m_private_is_running.WaitForValueEqualTo (true, &timeout_time, &timed_out))
685 {
686 if (log)
687 {
688 if (timed_out)
689 log->Printf ("async: timed out waiting for process to resume, but process was resumed");
690 else
691 log->Printf ("async: async packet sent");
692 }
693 }
694 else
695 {
696 if (log)
697 log->Printf ("async: timed out waiting for process to resume");
Greg Claytone889ad62011-10-27 22:04:16 +0000698 }
699 }
700 else
701 {
Greg Claytond3544052012-05-31 21:24:20 +0000702 // We had a racy condition where we went to send the interrupt
703 // yet we were able to get the lock, so the process must have
704 // just stopped?
Greg Clayton576d8832011-03-22 04:00:09 +0000705 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000706 log->Printf ("async: got lock without sending interrupt");
707 // Send the packet normally since we got the lock
Greg Clayton3dedae12013-12-06 21:45:27 +0000708 packet_result = SendPacketAndWaitForResponseNoLock (payload, payload_length, response);
Greg Clayton576d8832011-03-22 04:00:09 +0000709 }
710 }
711 else
712 {
Greg Clayton644247c2011-07-07 01:59:51 +0000713 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000714 log->Printf ("async: failed to interrupt");
Greg Clayton576d8832011-03-22 04:00:09 +0000715 }
716 }
717 else
718 {
719 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000720 log->Printf ("async: not running, async is ignored");
Greg Clayton576d8832011-03-22 04:00:09 +0000721 }
722 }
723 else
724 {
725 if (log)
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000726 log->Printf("error: failed to get packet sequence mutex, not sending packet '%*s'", (int) payload_length, payload);
Greg Clayton576d8832011-03-22 04:00:09 +0000727 }
728 }
Greg Clayton3dedae12013-12-06 21:45:27 +0000729 return packet_result;
Greg Clayton576d8832011-03-22 04:00:09 +0000730}
731
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000732static const char *end_delimiter = "--end--;";
733static const int end_delimiter_len = 8;
734
735std::string
736GDBRemoteCommunicationClient::HarmonizeThreadIdsForProfileData
737( ProcessGDBRemote *process,
738 StringExtractorGDBRemote& profileDataExtractor
739)
740{
741 std::map<uint64_t, uint32_t> new_thread_id_to_used_usec_map;
742 std::stringstream final_output;
743 std::string name, value;
744
745 // Going to assuming thread_used_usec comes first, else bail out.
746 while (profileDataExtractor.GetNameColonValue(name, value))
747 {
748 if (name.compare("thread_used_id") == 0)
749 {
750 StringExtractor threadIDHexExtractor(value.c_str());
751 uint64_t thread_id = threadIDHexExtractor.GetHexMaxU64(false, 0);
752
753 bool has_used_usec = false;
754 uint32_t curr_used_usec = 0;
755 std::string usec_name, usec_value;
756 uint32_t input_file_pos = profileDataExtractor.GetFilePos();
757 if (profileDataExtractor.GetNameColonValue(usec_name, usec_value))
758 {
759 if (usec_name.compare("thread_used_usec") == 0)
760 {
761 has_used_usec = true;
762 curr_used_usec = strtoull(usec_value.c_str(), NULL, 0);
763 }
764 else
765 {
766 // We didn't find what we want, it is probably
767 // an older version. Bail out.
768 profileDataExtractor.SetFilePos(input_file_pos);
769 }
770 }
771
772 if (has_used_usec)
773 {
774 uint32_t prev_used_usec = 0;
775 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_used_usec_map.find(thread_id);
776 if (iterator != m_thread_id_to_used_usec_map.end())
777 {
778 prev_used_usec = m_thread_id_to_used_usec_map[thread_id];
779 }
780
781 uint32_t real_used_usec = curr_used_usec - prev_used_usec;
782 // A good first time record is one that runs for at least 0.25 sec
783 bool good_first_time = (prev_used_usec == 0) && (real_used_usec > 250000);
784 bool good_subsequent_time = (prev_used_usec > 0) &&
785 ((real_used_usec > 0) || (process->HasAssignedIndexIDToThread(thread_id)));
786
787 if (good_first_time || good_subsequent_time)
788 {
789 // We try to avoid doing too many index id reservation,
790 // resulting in fast increase of index ids.
791
792 final_output << name << ":";
793 int32_t index_id = process->AssignIndexIDToThread(thread_id);
794 final_output << index_id << ";";
795
796 final_output << usec_name << ":" << usec_value << ";";
797 }
798 else
799 {
800 // Skip past 'thread_used_name'.
801 std::string local_name, local_value;
802 profileDataExtractor.GetNameColonValue(local_name, local_value);
803 }
804
805 // Store current time as previous time so that they can be compared later.
806 new_thread_id_to_used_usec_map[thread_id] = curr_used_usec;
807 }
808 else
809 {
810 // Bail out and use old string.
811 final_output << name << ":" << value << ";";
812 }
813 }
814 else
815 {
816 final_output << name << ":" << value << ";";
817 }
818 }
819 final_output << end_delimiter;
820 m_thread_id_to_used_usec_map = new_thread_id_to_used_usec_map;
821
822 return final_output.str();
823}
824
Greg Clayton576d8832011-03-22 04:00:09 +0000825StateType
826GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse
827(
828 ProcessGDBRemote *process,
829 const char *payload,
830 size_t packet_length,
831 StringExtractorGDBRemote &response
832)
833{
Greg Clayton1f5181a2012-07-02 22:05:25 +0000834 m_curr_tid = LLDB_INVALID_THREAD_ID;
Greg Clayton5160ce52013-03-27 23:08:40 +0000835 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton576d8832011-03-22 04:00:09 +0000836 if (log)
837 log->Printf ("GDBRemoteCommunicationClient::%s ()", __FUNCTION__);
838
839 Mutex::Locker locker(m_sequence_mutex);
840 StateType state = eStateRunning;
841
842 BroadcastEvent(eBroadcastBitRunPacketSent, NULL);
843 m_public_is_running.SetValue (true, eBroadcastNever);
844 // Set the starting continue packet into "continue_packet". This packet
Jim Inghambabfc382012-06-06 00:32:39 +0000845 // may change if we are interrupted and we continue after an async packet...
Greg Clayton576d8832011-03-22 04:00:09 +0000846 std::string continue_packet(payload, packet_length);
847
Greg Clayton3f875c52013-02-22 22:23:55 +0000848 bool got_async_packet = false;
Greg Claytonaf247d72011-05-19 03:54:16 +0000849
Greg Clayton576d8832011-03-22 04:00:09 +0000850 while (state == eStateRunning)
851 {
Greg Clayton3f875c52013-02-22 22:23:55 +0000852 if (!got_async_packet)
Greg Claytonaf247d72011-05-19 03:54:16 +0000853 {
854 if (log)
855 log->Printf ("GDBRemoteCommunicationClient::%s () sending continue packet: %s", __FUNCTION__, continue_packet.c_str());
Greg Clayton3dedae12013-12-06 21:45:27 +0000856 if (SendPacketNoLock(continue_packet.c_str(), continue_packet.size()) != PacketResult::Success)
Greg Claytonaf247d72011-05-19 03:54:16 +0000857 state = eStateInvalid;
Jim Inghamb8cd5752014-04-16 02:24:17 +0000858 else
859 m_interrupt_sent = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000860
Greg Claytone889ad62011-10-27 22:04:16 +0000861 m_private_is_running.SetValue (true, eBroadcastAlways);
Greg Claytonaf247d72011-05-19 03:54:16 +0000862 }
863
Greg Clayton3f875c52013-02-22 22:23:55 +0000864 got_async_packet = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000865
866 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000867 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(%s)", __FUNCTION__, continue_packet.c_str());
Greg Clayton576d8832011-03-22 04:00:09 +0000868
Greg Clayton3dedae12013-12-06 21:45:27 +0000869 if (WaitForPacketWithTimeoutMicroSecondsNoLock(response, UINT32_MAX) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000870 {
871 if (response.Empty())
872 state = eStateInvalid;
873 else
874 {
875 const char stop_type = response.GetChar();
876 if (log)
877 log->Printf ("GDBRemoteCommunicationClient::%s () got packet: %s", __FUNCTION__, response.GetStringRef().c_str());
878 switch (stop_type)
879 {
880 case 'T':
881 case 'S':
Greg Clayton576d8832011-03-22 04:00:09 +0000882 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000883 if (process->GetStopID() == 0)
Greg Clayton576d8832011-03-22 04:00:09 +0000884 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000885 if (process->GetID() == LLDB_INVALID_PROCESS_ID)
886 {
887 lldb::pid_t pid = GetCurrentProcessID ();
888 if (pid != LLDB_INVALID_PROCESS_ID)
889 process->SetID (pid);
890 }
891 process->BuildDynamicRegisterInfo (true);
Greg Clayton576d8832011-03-22 04:00:09 +0000892 }
Greg Clayton2687cd12012-03-29 01:55:41 +0000893
894 // Privately notify any internal threads that we have stopped
895 // in case we wanted to interrupt our process, yet we might
896 // send a packet and continue without returning control to the
897 // user.
898 m_private_is_running.SetValue (false, eBroadcastAlways);
899
900 const uint8_t signo = response.GetHexU8 (UINT8_MAX);
901
Jim Inghambabfc382012-06-06 00:32:39 +0000902 bool continue_after_async = m_async_signal != -1 || m_async_packet_predicate.GetValue();
903 if (continue_after_async || m_interrupt_sent)
Greg Clayton2687cd12012-03-29 01:55:41 +0000904 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000905 // We sent an interrupt packet to stop the inferior process
906 // for an async signal or to send an async packet while running
907 // but we might have been single stepping and received the
908 // stop packet for the step instead of for the interrupt packet.
909 // Typically when an interrupt is sent a SIGINT or SIGSTOP
910 // is used, so if we get anything else, we need to try and
911 // get another stop reply packet that may have been sent
912 // due to sending the interrupt when the target is stopped
913 // which will just re-send a copy of the last stop reply
914 // packet. If we don't do this, then the reply for our
915 // async packet will be the repeat stop reply packet and cause
916 // a lot of trouble for us!
917 if (signo != SIGINT && signo != SIGSTOP)
918 {
Greg Claytonfb72fde2012-05-15 02:50:49 +0000919 continue_after_async = false;
Greg Clayton2687cd12012-03-29 01:55:41 +0000920
921 // We didn't get a a SIGINT or SIGSTOP, so try for a
922 // very brief time (1 ms) to get another stop reply
923 // packet to make sure it doesn't get in the way
924 StringExtractorGDBRemote extra_stop_reply_packet;
925 uint32_t timeout_usec = 1000;
Greg Clayton3dedae12013-12-06 21:45:27 +0000926 if (WaitForPacketWithTimeoutMicroSecondsNoLock (extra_stop_reply_packet, timeout_usec) == PacketResult::Success)
Greg Clayton2687cd12012-03-29 01:55:41 +0000927 {
928 switch (extra_stop_reply_packet.GetChar())
929 {
930 case 'T':
931 case 'S':
932 // We did get an extra stop reply, which means
933 // our interrupt didn't stop the target so we
934 // shouldn't continue after the async signal
935 // or packet is sent...
Greg Claytonfb72fde2012-05-15 02:50:49 +0000936 continue_after_async = false;
Greg Clayton2687cd12012-03-29 01:55:41 +0000937 break;
938 }
939 }
940 }
941 }
942
943 if (m_async_signal != -1)
944 {
945 if (log)
946 log->Printf ("async: send signo = %s", Host::GetSignalAsCString (m_async_signal));
947
948 // Save off the async signal we are supposed to send
949 const int async_signal = m_async_signal;
950 // Clear the async signal member so we don't end up
951 // sending the signal multiple times...
952 m_async_signal = -1;
953 // Check which signal we stopped with
954 if (signo == async_signal)
955 {
956 if (log)
957 log->Printf ("async: stopped with signal %s, we are done running", Host::GetSignalAsCString (signo));
958
959 // We already stopped with a signal that we wanted
960 // to stop with, so we are done
961 }
962 else
963 {
964 // We stopped with a different signal that the one
965 // we wanted to stop with, so now we must resume
966 // with the signal we want
967 char signal_packet[32];
968 int signal_packet_len = 0;
969 signal_packet_len = ::snprintf (signal_packet,
970 sizeof (signal_packet),
971 "C%2.2x",
972 async_signal);
973
974 if (log)
975 log->Printf ("async: stopped with signal %s, resume with %s",
976 Host::GetSignalAsCString (signo),
977 Host::GetSignalAsCString (async_signal));
978
979 // Set the continue packet to resume even if the
Greg Claytonfb72fde2012-05-15 02:50:49 +0000980 // interrupt didn't cause our stop (ignore continue_after_async)
Greg Clayton2687cd12012-03-29 01:55:41 +0000981 continue_packet.assign(signal_packet, signal_packet_len);
982 continue;
983 }
984 }
985 else if (m_async_packet_predicate.GetValue())
986 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000987 Log * packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Greg Clayton2687cd12012-03-29 01:55:41 +0000988
989 // We are supposed to send an asynchronous packet while
Jim Inghama6195b72013-12-18 01:24:33 +0000990 // we are running.
Greg Clayton2687cd12012-03-29 01:55:41 +0000991 m_async_response.Clear();
992 if (m_async_packet.empty())
993 {
Jim Inghama6195b72013-12-18 01:24:33 +0000994 m_async_result = PacketResult::ErrorSendFailed;
995 if (packet_log)
Greg Clayton2687cd12012-03-29 01:55:41 +0000996 packet_log->Printf ("async: error: empty async packet");
997
998 }
999 else
1000 {
1001 if (packet_log)
1002 packet_log->Printf ("async: sending packet");
1003
Jim Inghama6195b72013-12-18 01:24:33 +00001004 m_async_result = SendPacketAndWaitForResponse (&m_async_packet[0],
1005 m_async_packet.size(),
1006 m_async_response,
1007 false);
Greg Clayton2687cd12012-03-29 01:55:41 +00001008 }
1009 // Let the other thread that was trying to send the async
1010 // packet know that the packet has been sent and response is
1011 // ready...
1012 m_async_packet_predicate.SetValue(false, eBroadcastAlways);
1013
1014 if (packet_log)
Greg Claytonfb72fde2012-05-15 02:50:49 +00001015 packet_log->Printf ("async: sent packet, continue_after_async = %i", continue_after_async);
Greg Clayton2687cd12012-03-29 01:55:41 +00001016
1017 // Set the continue packet to resume if our interrupt
1018 // for the async packet did cause the stop
Greg Claytonfb72fde2012-05-15 02:50:49 +00001019 if (continue_after_async)
Greg Clayton2687cd12012-03-29 01:55:41 +00001020 {
Greg Claytonf1186de2012-05-24 23:42:14 +00001021 // Reverting this for now as it is causing deadlocks
1022 // in programs (<rdar://problem/11529853>). In the future
1023 // we should check our thread list and "do the right thing"
1024 // for new threads that show up while we stop and run async
1025 // packets. Setting the packet to 'c' to continue all threads
1026 // is the right thing to do 99.99% of the time because if a
1027 // thread was single stepping, and we sent an interrupt, we
1028 // will notice above that we didn't stop due to an interrupt
1029 // but stopped due to stepping and we would _not_ continue.
1030 continue_packet.assign (1, 'c');
Greg Clayton2687cd12012-03-29 01:55:41 +00001031 continue;
1032 }
1033 }
1034 // Stop with signal and thread info
1035 state = eStateStopped;
Greg Clayton576d8832011-03-22 04:00:09 +00001036 }
Greg Clayton576d8832011-03-22 04:00:09 +00001037 break;
1038
1039 case 'W':
1040 case 'X':
1041 // process exited
1042 state = eStateExited;
1043 break;
1044
1045 case 'O':
1046 // STDOUT
1047 {
Greg Clayton3f875c52013-02-22 22:23:55 +00001048 got_async_packet = true;
Greg Clayton576d8832011-03-22 04:00:09 +00001049 std::string inferior_stdout;
1050 inferior_stdout.reserve(response.GetBytesLeft () / 2);
1051 char ch;
1052 while ((ch = response.GetHexU8()) != '\0')
1053 inferior_stdout.append(1, ch);
1054 process->AppendSTDOUT (inferior_stdout.c_str(), inferior_stdout.size());
1055 }
1056 break;
1057
Han Ming Ongab3b8b22012-11-17 00:21:04 +00001058 case 'A':
1059 // Async miscellaneous reply. Right now, only profile data is coming through this channel.
1060 {
Greg Clayton3f875c52013-02-22 22:23:55 +00001061 got_async_packet = true;
Han Ming Ong4b6459f2013-01-18 23:11:53 +00001062 std::string input = response.GetStringRef().substr(1); // '1' to move beyond 'A'
1063 if (m_partial_profile_data.length() > 0)
1064 {
1065 m_partial_profile_data.append(input);
1066 input = m_partial_profile_data;
1067 m_partial_profile_data.clear();
1068 }
1069
1070 size_t found, pos = 0, len = input.length();
1071 while ((found = input.find(end_delimiter, pos)) != std::string::npos)
1072 {
1073 StringExtractorGDBRemote profileDataExtractor(input.substr(pos, found).c_str());
Han Ming Ong91ed6b82013-06-24 18:15:05 +00001074 std::string profile_data = HarmonizeThreadIdsForProfileData(process, profileDataExtractor);
1075 process->BroadcastAsyncProfileData (profile_data);
Han Ming Ong4b6459f2013-01-18 23:11:53 +00001076
1077 pos = found + end_delimiter_len;
1078 }
1079
1080 if (pos < len)
1081 {
1082 // Last incomplete chunk.
1083 m_partial_profile_data = input.substr(pos);
1084 }
Han Ming Ongab3b8b22012-11-17 00:21:04 +00001085 }
1086 break;
1087
Greg Clayton576d8832011-03-22 04:00:09 +00001088 case 'E':
1089 // ERROR
1090 state = eStateInvalid;
1091 break;
1092
1093 default:
1094 if (log)
1095 log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__);
1096 state = eStateInvalid;
1097 break;
1098 }
1099 }
1100 }
1101 else
1102 {
1103 if (log)
1104 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__);
1105 state = eStateInvalid;
1106 }
1107 }
1108 if (log)
1109 log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state));
1110 response.SetFilePos(0);
1111 m_private_is_running.SetValue (false, eBroadcastAlways);
1112 m_public_is_running.SetValue (false, eBroadcastAlways);
1113 return state;
1114}
1115
1116bool
1117GDBRemoteCommunicationClient::SendAsyncSignal (int signo)
1118{
Greg Clayton2687cd12012-03-29 01:55:41 +00001119 Mutex::Locker async_locker (m_async_mutex);
Greg Clayton576d8832011-03-22 04:00:09 +00001120 m_async_signal = signo;
1121 bool timed_out = false;
Greg Clayton576d8832011-03-22 04:00:09 +00001122 Mutex::Locker locker;
Greg Clayton2687cd12012-03-29 01:55:41 +00001123 if (SendInterrupt (locker, 1, timed_out))
Greg Clayton576d8832011-03-22 04:00:09 +00001124 return true;
1125 m_async_signal = -1;
1126 return false;
1127}
1128
Greg Clayton37a0a242012-04-11 00:24:49 +00001129// This function takes a mutex locker as a parameter in case the GetSequenceMutex
Greg Clayton576d8832011-03-22 04:00:09 +00001130// actually succeeds. If it doesn't succeed in acquiring the sequence mutex
1131// (the expected result), then it will send the halt packet. If it does succeed
1132// then the caller that requested the interrupt will want to keep the sequence
1133// locked down so that no one else can send packets while the caller has control.
1134// This function usually gets called when we are running and need to stop the
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001135// target. It can also be used when we are running and we need to do something
Greg Clayton576d8832011-03-22 04:00:09 +00001136// else (like read/write memory), so we need to interrupt the running process
1137// (gdb remote protocol requires this), and do what we need to do, then resume.
1138
1139bool
Greg Clayton2687cd12012-03-29 01:55:41 +00001140GDBRemoteCommunicationClient::SendInterrupt
Greg Clayton576d8832011-03-22 04:00:09 +00001141(
1142 Mutex::Locker& locker,
1143 uint32_t seconds_to_wait_for_stop,
Greg Clayton576d8832011-03-22 04:00:09 +00001144 bool &timed_out
1145)
1146{
Greg Clayton576d8832011-03-22 04:00:09 +00001147 timed_out = false;
Greg Clayton5160ce52013-03-27 23:08:40 +00001148 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Clayton576d8832011-03-22 04:00:09 +00001149
1150 if (IsRunning())
1151 {
1152 // Only send an interrupt if our debugserver is running...
Greg Claytonc3c0b0e2012-04-12 19:04:34 +00001153 if (GetSequenceMutex (locker))
Greg Clayton37a0a242012-04-11 00:24:49 +00001154 {
1155 if (log)
1156 log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt");
1157 }
1158 else
Greg Clayton576d8832011-03-22 04:00:09 +00001159 {
1160 // Someone has the mutex locked waiting for a response or for the
1161 // inferior to stop, so send the interrupt on the down low...
1162 char ctrl_c = '\x03';
1163 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton576d8832011-03-22 04:00:09 +00001164 size_t bytes_written = Write (&ctrl_c, 1, status, NULL);
Greg Clayton2687cd12012-03-29 01:55:41 +00001165 if (log)
1166 log->PutCString("send packet: \\x03");
Greg Clayton576d8832011-03-22 04:00:09 +00001167 if (bytes_written > 0)
1168 {
Greg Clayton2687cd12012-03-29 01:55:41 +00001169 m_interrupt_sent = true;
Greg Clayton576d8832011-03-22 04:00:09 +00001170 if (seconds_to_wait_for_stop)
1171 {
Greg Clayton2687cd12012-03-29 01:55:41 +00001172 TimeValue timeout;
1173 if (seconds_to_wait_for_stop)
1174 {
1175 timeout = TimeValue::Now();
1176 timeout.OffsetWithSeconds (seconds_to_wait_for_stop);
1177 }
Greg Clayton576d8832011-03-22 04:00:09 +00001178 if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out))
1179 {
1180 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +00001181 log->PutCString ("SendInterrupt () - sent interrupt, private state stopped");
Greg Clayton576d8832011-03-22 04:00:09 +00001182 return true;
1183 }
1184 else
1185 {
1186 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +00001187 log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume");
Greg Clayton576d8832011-03-22 04:00:09 +00001188 }
1189 }
1190 else
1191 {
1192 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +00001193 log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop...");
Greg Clayton576d8832011-03-22 04:00:09 +00001194 return true;
1195 }
1196 }
1197 else
1198 {
1199 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +00001200 log->Printf ("SendInterrupt () - failed to write interrupt");
Greg Clayton576d8832011-03-22 04:00:09 +00001201 }
1202 return false;
1203 }
Greg Clayton576d8832011-03-22 04:00:09 +00001204 }
Greg Clayton2687cd12012-03-29 01:55:41 +00001205 else
1206 {
1207 if (log)
1208 log->Printf ("SendInterrupt () - not running");
1209 }
Greg Clayton576d8832011-03-22 04:00:09 +00001210 return true;
1211}
1212
1213lldb::pid_t
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001214GDBRemoteCommunicationClient::GetCurrentProcessID (bool allow_lazy)
Greg Clayton576d8832011-03-22 04:00:09 +00001215{
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001216 if (allow_lazy && m_curr_pid_is_valid == eLazyBoolYes)
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001217 return m_curr_pid;
1218
1219 // First try to retrieve the pid via the qProcessInfo request.
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001220 GetCurrentProcessInfo (allow_lazy);
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001221 if (m_curr_pid_is_valid == eLazyBoolYes)
Greg Clayton576d8832011-03-22 04:00:09 +00001222 {
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001223 // We really got it.
1224 return m_curr_pid;
Greg Clayton576d8832011-03-22 04:00:09 +00001225 }
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001226 else
1227 {
Todd Fialae24614f2014-05-14 00:15:32 +00001228 // If we don't get a response for qProcessInfo, check if $qC gives us a result.
1229 // $qC only returns a real process id on older debugserver and lldb-platform stubs.
1230 // The gdb remote protocol documents $qC as returning the thread id, which newer
1231 // debugserver and lldb-gdbserver stubs return correctly.
1232 StringExtractorGDBRemote response;
1233 if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false) == PacketResult::Success)
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001234 {
Todd Fialae24614f2014-05-14 00:15:32 +00001235 if (response.GetChar() == 'Q')
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001236 {
Todd Fialae24614f2014-05-14 00:15:32 +00001237 if (response.GetChar() == 'C')
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001238 {
Todd Fialae24614f2014-05-14 00:15:32 +00001239 m_curr_pid = response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID);
1240 if (m_curr_pid != LLDB_INVALID_PROCESS_ID)
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001241 {
Todd Fialae24614f2014-05-14 00:15:32 +00001242 m_curr_pid_is_valid = eLazyBoolYes;
1243 return m_curr_pid;
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001244 }
1245 }
1246 }
1247 }
1248 }
1249
Greg Clayton576d8832011-03-22 04:00:09 +00001250 return LLDB_INVALID_PROCESS_ID;
1251}
1252
1253bool
1254GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str)
1255{
1256 error_str.clear();
1257 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001258 if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001259 {
1260 if (response.IsOKResponse())
1261 return true;
1262 if (response.GetChar() == 'E')
1263 {
1264 // A string the describes what failed when launching...
1265 error_str = response.GetStringRef().substr(1);
1266 }
1267 else
1268 {
1269 error_str.assign ("unknown error occurred launching process");
1270 }
1271 }
1272 else
1273 {
Jim Ingham98d6da52012-06-28 20:30:23 +00001274 error_str.assign ("timed out waiting for app to launch");
Greg Clayton576d8832011-03-22 04:00:09 +00001275 }
1276 return false;
1277}
1278
1279int
Greg Claytonfbb76342013-11-20 21:07:01 +00001280GDBRemoteCommunicationClient::SendArgumentsPacket (const ProcessLaunchInfo &launch_info)
Greg Clayton576d8832011-03-22 04:00:09 +00001281{
Greg Claytonfbb76342013-11-20 21:07:01 +00001282 // Since we don't get the send argv0 separate from the executable path, we need to
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001283 // make sure to use the actual executable path found in the launch_info...
Greg Claytonfbb76342013-11-20 21:07:01 +00001284 std::vector<const char *> argv;
1285 FileSpec exe_file = launch_info.GetExecutableFile();
1286 std::string exe_path;
1287 const char *arg = NULL;
1288 const Args &launch_args = launch_info.GetArguments();
1289 if (exe_file)
1290 exe_path = exe_file.GetPath();
1291 else
1292 {
1293 arg = launch_args.GetArgumentAtIndex(0);
1294 if (arg)
1295 exe_path = arg;
1296 }
1297 if (!exe_path.empty())
1298 {
1299 argv.push_back(exe_path.c_str());
1300 for (uint32_t i=1; (arg = launch_args.GetArgumentAtIndex(i)) != NULL; ++i)
1301 {
1302 if (arg)
1303 argv.push_back(arg);
1304 }
1305 }
1306 if (!argv.empty())
Greg Clayton576d8832011-03-22 04:00:09 +00001307 {
1308 StreamString packet;
1309 packet.PutChar('A');
Greg Claytonfbb76342013-11-20 21:07:01 +00001310 for (size_t i = 0, n = argv.size(); i < n; ++i)
Greg Clayton576d8832011-03-22 04:00:09 +00001311 {
Greg Claytonfbb76342013-11-20 21:07:01 +00001312 arg = argv[i];
Greg Clayton576d8832011-03-22 04:00:09 +00001313 const int arg_len = strlen(arg);
1314 if (i > 0)
1315 packet.PutChar(',');
Greg Claytonfbb76342013-11-20 21:07:01 +00001316 packet.Printf("%i,%i,", arg_len * 2, (int)i);
Greg Clayton576d8832011-03-22 04:00:09 +00001317 packet.PutBytesAsRawHex8 (arg, arg_len);
1318 }
1319
1320 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001321 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001322 {
1323 if (response.IsOKResponse())
1324 return 0;
1325 uint8_t error = response.GetError();
1326 if (error)
1327 return error;
1328 }
1329 }
1330 return -1;
1331}
1332
1333int
1334GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value)
1335{
1336 if (name_equal_value && name_equal_value[0])
1337 {
1338 StreamString packet;
Greg Clayton89600582013-10-10 17:53:50 +00001339 bool send_hex_encoding = false;
1340 for (const char *p = name_equal_value; *p != '\0' && send_hex_encoding == false; ++p)
Greg Clayton576d8832011-03-22 04:00:09 +00001341 {
Greg Clayton89600582013-10-10 17:53:50 +00001342 if (isprint(*p))
1343 {
1344 switch (*p)
1345 {
1346 case '$':
1347 case '#':
1348 send_hex_encoding = true;
1349 break;
1350 default:
1351 break;
1352 }
1353 }
1354 else
1355 {
1356 // We have non printable characters, lets hex encode this...
1357 send_hex_encoding = true;
1358 }
1359 }
1360
1361 StringExtractorGDBRemote response;
1362 if (send_hex_encoding)
1363 {
1364 if (m_supports_QEnvironmentHexEncoded)
1365 {
1366 packet.PutCString("QEnvironmentHexEncoded:");
1367 packet.PutBytesAsRawHex8 (name_equal_value, strlen(name_equal_value));
Greg Clayton3dedae12013-12-06 21:45:27 +00001368 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton89600582013-10-10 17:53:50 +00001369 {
1370 if (response.IsOKResponse())
1371 return 0;
1372 uint8_t error = response.GetError();
1373 if (error)
1374 return error;
1375 if (response.IsUnsupportedResponse())
1376 m_supports_QEnvironmentHexEncoded = false;
1377 }
1378 }
1379
1380 }
1381 else if (m_supports_QEnvironment)
1382 {
1383 packet.Printf("QEnvironment:%s", name_equal_value);
Greg Clayton3dedae12013-12-06 21:45:27 +00001384 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton89600582013-10-10 17:53:50 +00001385 {
1386 if (response.IsOKResponse())
1387 return 0;
1388 uint8_t error = response.GetError();
1389 if (error)
1390 return error;
1391 if (response.IsUnsupportedResponse())
1392 m_supports_QEnvironment = false;
1393 }
Greg Clayton576d8832011-03-22 04:00:09 +00001394 }
1395 }
1396 return -1;
1397}
1398
Greg Claytonc4103b32011-05-08 04:53:50 +00001399int
1400GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch)
1401{
1402 if (arch && arch[0])
1403 {
1404 StreamString packet;
1405 packet.Printf("QLaunchArch:%s", arch);
1406 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001407 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Claytonc4103b32011-05-08 04:53:50 +00001408 {
1409 if (response.IsOKResponse())
1410 return 0;
1411 uint8_t error = response.GetError();
1412 if (error)
1413 return error;
1414 }
1415 }
1416 return -1;
1417}
1418
Jason Molendaa3329782014-03-29 18:54:20 +00001419int
1420GDBRemoteCommunicationClient::SendLaunchEventDataPacket (char const *data, bool *was_supported)
1421{
1422 if (data && *data != '\0')
1423 {
1424 StreamString packet;
1425 packet.Printf("QSetProcessEvent:%s", data);
1426 StringExtractorGDBRemote response;
1427 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
1428 {
1429 if (response.IsOKResponse())
1430 {
1431 if (was_supported)
1432 *was_supported = true;
1433 return 0;
1434 }
1435 else if (response.IsUnsupportedResponse())
1436 {
1437 if (was_supported)
1438 *was_supported = false;
1439 return -1;
1440 }
1441 else
1442 {
1443 uint8_t error = response.GetError();
1444 if (was_supported)
1445 *was_supported = true;
1446 if (error)
1447 return error;
1448 }
1449 }
1450 }
1451 return -1;
1452}
1453
Greg Clayton576d8832011-03-22 04:00:09 +00001454bool
Greg Clayton1cb64962011-03-24 04:28:38 +00001455GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major,
1456 uint32_t &minor,
1457 uint32_t &update)
1458{
1459 if (GetHostInfo ())
1460 {
1461 if (m_os_version_major != UINT32_MAX)
1462 {
1463 major = m_os_version_major;
1464 minor = m_os_version_minor;
1465 update = m_os_version_update;
1466 return true;
1467 }
1468 }
1469 return false;
1470}
1471
1472bool
1473GDBRemoteCommunicationClient::GetOSBuildString (std::string &s)
1474{
1475 if (GetHostInfo ())
1476 {
1477 if (!m_os_build.empty())
1478 {
1479 s = m_os_build;
1480 return true;
1481 }
1482 }
1483 s.clear();
1484 return false;
1485}
1486
1487
1488bool
1489GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s)
1490{
1491 if (GetHostInfo ())
1492 {
1493 if (!m_os_kernel.empty())
1494 {
1495 s = m_os_kernel;
1496 return true;
1497 }
1498 }
1499 s.clear();
1500 return false;
1501}
1502
1503bool
1504GDBRemoteCommunicationClient::GetHostname (std::string &s)
1505{
1506 if (GetHostInfo ())
1507 {
1508 if (!m_hostname.empty())
1509 {
1510 s = m_hostname;
1511 return true;
1512 }
1513 }
1514 s.clear();
1515 return false;
1516}
1517
1518ArchSpec
1519GDBRemoteCommunicationClient::GetSystemArchitecture ()
1520{
1521 if (GetHostInfo ())
1522 return m_host_arch;
1523 return ArchSpec();
1524}
1525
Jason Molendaf17b5ac2012-12-19 02:54:03 +00001526const lldb_private::ArchSpec &
1527GDBRemoteCommunicationClient::GetProcessArchitecture ()
1528{
1529 if (m_qProcessInfo_is_valid == eLazyBoolCalculate)
1530 GetCurrentProcessInfo ();
1531 return m_process_arch;
1532}
1533
Jason Molendaa3329782014-03-29 18:54:20 +00001534bool
1535GDBRemoteCommunicationClient::GetGDBServerVersion()
1536{
1537 if (m_qGDBServerVersion_is_valid == eLazyBoolCalculate)
1538 {
1539 m_gdb_server_name.clear();
1540 m_gdb_server_version = 0;
1541 m_qGDBServerVersion_is_valid = eLazyBoolNo;
1542
1543 StringExtractorGDBRemote response;
1544 if (SendPacketAndWaitForResponse ("qGDBServerVersion", response, false) == PacketResult::Success)
1545 {
1546 if (response.IsNormalResponse())
1547 {
1548 std::string name;
1549 std::string value;
1550 bool success = false;
1551 while (response.GetNameColonValue(name, value))
1552 {
1553 if (name.compare("name") == 0)
1554 {
1555 success = true;
1556 m_gdb_server_name.swap(value);
1557 }
1558 else if (name.compare("version") == 0)
1559 {
1560 size_t dot_pos = value.find('.');
1561 if (dot_pos != std::string::npos)
1562 value[dot_pos] = '\0';
Vince Harron5275aaa2015-01-15 20:08:35 +00001563 const uint32_t version = StringConvert::ToUInt32(value.c_str(), UINT32_MAX, 0);
Jason Molendaa3329782014-03-29 18:54:20 +00001564 if (version != UINT32_MAX)
1565 {
1566 success = true;
1567 m_gdb_server_version = version;
1568 }
1569 }
1570 }
1571 if (success)
1572 m_qGDBServerVersion_is_valid = eLazyBoolYes;
1573 }
1574 }
1575 }
1576 return m_qGDBServerVersion_is_valid == eLazyBoolYes;
1577}
1578
1579const char *
1580GDBRemoteCommunicationClient::GetGDBServerProgramName()
1581{
1582 if (GetGDBServerVersion())
1583 {
1584 if (!m_gdb_server_name.empty())
1585 return m_gdb_server_name.c_str();
1586 }
1587 return NULL;
1588}
1589
1590uint32_t
1591GDBRemoteCommunicationClient::GetGDBServerProgramVersion()
1592{
1593 if (GetGDBServerVersion())
1594 return m_gdb_server_version;
1595 return 0;
1596}
Greg Clayton1cb64962011-03-24 04:28:38 +00001597
1598bool
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00001599GDBRemoteCommunicationClient::GetHostInfo (bool force)
Greg Clayton576d8832011-03-22 04:00:09 +00001600{
Todd Fialaaf245d12014-06-30 21:05:18 +00001601 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS));
1602
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00001603 if (force || m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton576d8832011-03-22 04:00:09 +00001604 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001605 m_qHostInfo_is_valid = eLazyBoolNo;
Greg Clayton576d8832011-03-22 04:00:09 +00001606 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001607 if (SendPacketAndWaitForResponse ("qHostInfo", response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001608 {
Greg Clayton17a0cb62011-05-15 23:46:54 +00001609 if (response.IsNormalResponse())
Greg Claytond314e812011-03-23 00:09:55 +00001610 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001611 std::string name;
1612 std::string value;
1613 uint32_t cpu = LLDB_INVALID_CPUTYPE;
1614 uint32_t sub = 0;
1615 std::string arch_name;
1616 std::string os_name;
1617 std::string vendor_name;
1618 std::string triple;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +00001619 std::string distribution_id;
Greg Clayton32e0a752011-03-30 18:16:51 +00001620 uint32_t pointer_byte_size = 0;
1621 StringExtractor extractor;
1622 ByteOrder byte_order = eByteOrderInvalid;
1623 uint32_t num_keys_decoded = 0;
1624 while (response.GetNameColonValue(name, value))
Greg Claytond314e812011-03-23 00:09:55 +00001625 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001626 if (name.compare("cputype") == 0)
Greg Clayton1cb64962011-03-24 04:28:38 +00001627 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001628 // exception type in big endian hex
Vince Harron5275aaa2015-01-15 20:08:35 +00001629 cpu = StringConvert::ToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0);
Greg Clayton32e0a752011-03-30 18:16:51 +00001630 if (cpu != LLDB_INVALID_CPUTYPE)
1631 ++num_keys_decoded;
1632 }
1633 else if (name.compare("cpusubtype") == 0)
1634 {
1635 // exception count in big endian hex
Vince Harron5275aaa2015-01-15 20:08:35 +00001636 sub = StringConvert::ToUInt32 (value.c_str(), 0, 0);
Greg Clayton32e0a752011-03-30 18:16:51 +00001637 if (sub != 0)
1638 ++num_keys_decoded;
1639 }
1640 else if (name.compare("arch") == 0)
1641 {
1642 arch_name.swap (value);
1643 ++num_keys_decoded;
1644 }
1645 else if (name.compare("triple") == 0)
1646 {
Greg Clayton44272a42014-09-18 00:18:32 +00001647 extractor.GetStringRef ().swap (value);
1648 extractor.SetFilePos(0);
1649 extractor.GetHexByteString (triple);
Greg Clayton32e0a752011-03-30 18:16:51 +00001650 ++num_keys_decoded;
1651 }
Todd Fialaa9ddb0e2014-01-18 03:02:39 +00001652 else if (name.compare ("distribution_id") == 0)
1653 {
1654 extractor.GetStringRef ().swap (value);
1655 extractor.SetFilePos (0);
1656 extractor.GetHexByteString (distribution_id);
1657 ++num_keys_decoded;
1658 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001659 else if (name.compare("os_build") == 0)
1660 {
1661 extractor.GetStringRef().swap(value);
1662 extractor.SetFilePos(0);
1663 extractor.GetHexByteString (m_os_build);
1664 ++num_keys_decoded;
1665 }
1666 else if (name.compare("hostname") == 0)
1667 {
1668 extractor.GetStringRef().swap(value);
1669 extractor.SetFilePos(0);
1670 extractor.GetHexByteString (m_hostname);
1671 ++num_keys_decoded;
1672 }
1673 else if (name.compare("os_kernel") == 0)
1674 {
1675 extractor.GetStringRef().swap(value);
1676 extractor.SetFilePos(0);
1677 extractor.GetHexByteString (m_os_kernel);
1678 ++num_keys_decoded;
1679 }
1680 else if (name.compare("ostype") == 0)
1681 {
1682 os_name.swap (value);
1683 ++num_keys_decoded;
1684 }
1685 else if (name.compare("vendor") == 0)
1686 {
1687 vendor_name.swap(value);
1688 ++num_keys_decoded;
1689 }
1690 else if (name.compare("endian") == 0)
1691 {
1692 ++num_keys_decoded;
1693 if (value.compare("little") == 0)
1694 byte_order = eByteOrderLittle;
1695 else if (value.compare("big") == 0)
1696 byte_order = eByteOrderBig;
1697 else if (value.compare("pdp") == 0)
1698 byte_order = eByteOrderPDP;
1699 else
1700 --num_keys_decoded;
1701 }
1702 else if (name.compare("ptrsize") == 0)
1703 {
Vince Harron5275aaa2015-01-15 20:08:35 +00001704 pointer_byte_size = StringConvert::ToUInt32 (value.c_str(), 0, 0);
Greg Clayton32e0a752011-03-30 18:16:51 +00001705 if (pointer_byte_size != 0)
1706 ++num_keys_decoded;
1707 }
1708 else if (name.compare("os_version") == 0)
1709 {
1710 Args::StringToVersion (value.c_str(),
1711 m_os_version_major,
1712 m_os_version_minor,
1713 m_os_version_update);
1714 if (m_os_version_major != UINT32_MAX)
1715 ++num_keys_decoded;
1716 }
Enrico Granataf04a2192012-07-13 23:18:48 +00001717 else if (name.compare("watchpoint_exceptions_received") == 0)
1718 {
1719 ++num_keys_decoded;
1720 if (strcmp(value.c_str(),"before") == 0)
1721 m_watchpoints_trigger_after_instruction = eLazyBoolNo;
1722 else if (strcmp(value.c_str(),"after") == 0)
1723 m_watchpoints_trigger_after_instruction = eLazyBoolYes;
1724 else
1725 --num_keys_decoded;
1726 }
Greg Clayton9ac6d2d2013-10-25 18:13:17 +00001727 else if (name.compare("default_packet_timeout") == 0)
1728 {
Vince Harron5275aaa2015-01-15 20:08:35 +00001729 m_default_packet_timeout = StringConvert::ToUInt32(value.c_str(), 0);
Greg Clayton9ac6d2d2013-10-25 18:13:17 +00001730 if (m_default_packet_timeout > 0)
1731 {
1732 SetPacketTimeout(m_default_packet_timeout);
1733 ++num_keys_decoded;
1734 }
1735 }
Enrico Granataf04a2192012-07-13 23:18:48 +00001736
Greg Clayton32e0a752011-03-30 18:16:51 +00001737 }
1738
1739 if (num_keys_decoded > 0)
1740 m_qHostInfo_is_valid = eLazyBoolYes;
1741
1742 if (triple.empty())
1743 {
1744 if (arch_name.empty())
1745 {
1746 if (cpu != LLDB_INVALID_CPUTYPE)
1747 {
1748 m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1749 if (pointer_byte_size)
1750 {
1751 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1752 }
1753 if (byte_order != eByteOrderInvalid)
1754 {
1755 assert (byte_order == m_host_arch.GetByteOrder());
1756 }
Greg Clayton70512312012-05-08 01:45:38 +00001757
1758 if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0)
1759 {
1760 switch (m_host_arch.GetMachine())
1761 {
Todd Fialad8eaa172014-07-23 14:37:35 +00001762 case llvm::Triple::aarch64:
Greg Clayton70512312012-05-08 01:45:38 +00001763 case llvm::Triple::arm:
1764 case llvm::Triple::thumb:
1765 os_name = "ios";
1766 break;
1767 default:
1768 os_name = "macosx";
1769 break;
1770 }
1771 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001772 if (!vendor_name.empty())
1773 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1774 if (!os_name.empty())
Greg Claytone1dadb82011-09-15 00:21:03 +00001775 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Greg Clayton32e0a752011-03-30 18:16:51 +00001776
1777 }
1778 }
1779 else
1780 {
1781 std::string triple;
1782 triple += arch_name;
Greg Clayton70512312012-05-08 01:45:38 +00001783 if (!vendor_name.empty() || !os_name.empty())
1784 {
1785 triple += '-';
1786 if (vendor_name.empty())
1787 triple += "unknown";
1788 else
1789 triple += vendor_name;
1790 triple += '-';
1791 if (os_name.empty())
1792 triple += "unknown";
1793 else
1794 triple += os_name;
1795 }
1796 m_host_arch.SetTriple (triple.c_str());
1797
1798 llvm::Triple &host_triple = m_host_arch.GetTriple();
1799 if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin)
1800 {
1801 switch (m_host_arch.GetMachine())
1802 {
Todd Fialad8eaa172014-07-23 14:37:35 +00001803 case llvm::Triple::aarch64:
Greg Clayton70512312012-05-08 01:45:38 +00001804 case llvm::Triple::arm:
1805 case llvm::Triple::thumb:
1806 host_triple.setOS(llvm::Triple::IOS);
1807 break;
1808 default:
1809 host_triple.setOS(llvm::Triple::MacOSX);
1810 break;
1811 }
1812 }
Greg Clayton1cb64962011-03-24 04:28:38 +00001813 if (pointer_byte_size)
1814 {
1815 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1816 }
1817 if (byte_order != eByteOrderInvalid)
1818 {
1819 assert (byte_order == m_host_arch.GetByteOrder());
1820 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001821
Greg Clayton1cb64962011-03-24 04:28:38 +00001822 }
1823 }
1824 else
1825 {
Greg Clayton70512312012-05-08 01:45:38 +00001826 m_host_arch.SetTriple (triple.c_str());
Greg Claytond314e812011-03-23 00:09:55 +00001827 if (pointer_byte_size)
1828 {
1829 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1830 }
1831 if (byte_order != eByteOrderInvalid)
1832 {
1833 assert (byte_order == m_host_arch.GetByteOrder());
1834 }
Todd Fialaaf245d12014-06-30 21:05:18 +00001835
1836 if (log)
1837 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 +00001838 }
1839 if (!distribution_id.empty ())
1840 m_host_arch.SetDistributionId (distribution_id.c_str ());
Greg Claytond314e812011-03-23 00:09:55 +00001841 }
Greg Clayton576d8832011-03-22 04:00:09 +00001842 }
1843 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001844 return m_qHostInfo_is_valid == eLazyBoolYes;
Greg Clayton576d8832011-03-22 04:00:09 +00001845}
1846
1847int
1848GDBRemoteCommunicationClient::SendAttach
1849(
1850 lldb::pid_t pid,
1851 StringExtractorGDBRemote& response
1852)
1853{
1854 if (pid != LLDB_INVALID_PROCESS_ID)
1855 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001856 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001857 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, pid);
Andy Gibbsa297a972013-06-19 19:04:53 +00001858 assert (packet_len < (int)sizeof(packet));
Greg Clayton3dedae12013-12-06 21:45:27 +00001859 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001860 {
1861 if (response.IsErrorResponse())
1862 return response.GetError();
1863 return 0;
1864 }
1865 }
1866 return -1;
1867}
1868
Vince Harrone0be4252015-02-06 18:32:57 +00001869int
1870GDBRemoteCommunicationClient::SendStdinNotification (const char* data, size_t data_len)
1871{
1872 StreamString packet;
1873 packet.PutCString("I");
1874 packet.PutBytesAsRawHex8(data, data_len);
1875 StringExtractorGDBRemote response;
1876 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
1877 {
1878 return 0;
1879 }
1880 return response.GetError();
1881
1882}
1883
Greg Clayton576d8832011-03-22 04:00:09 +00001884const lldb_private::ArchSpec &
1885GDBRemoteCommunicationClient::GetHostArchitecture ()
1886{
Greg Clayton32e0a752011-03-30 18:16:51 +00001887 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton576d8832011-03-22 04:00:09 +00001888 GetHostInfo ();
Greg Claytond314e812011-03-23 00:09:55 +00001889 return m_host_arch;
Greg Clayton576d8832011-03-22 04:00:09 +00001890}
1891
Greg Clayton9ac6d2d2013-10-25 18:13:17 +00001892uint32_t
1893GDBRemoteCommunicationClient::GetHostDefaultPacketTimeout ()
1894{
1895 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
1896 GetHostInfo ();
1897 return m_default_packet_timeout;
1898}
1899
Greg Clayton576d8832011-03-22 04:00:09 +00001900addr_t
1901GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions)
1902{
Greg Clayton70b57652011-05-15 01:25:55 +00001903 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton576d8832011-03-22 04:00:09 +00001904 {
Greg Clayton70b57652011-05-15 01:25:55 +00001905 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton2a48f522011-05-14 01:50:35 +00001906 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001907 const int packet_len = ::snprintf (packet, sizeof(packet), "_M%" PRIx64 ",%s%s%s",
Greg Clayton43e0af02012-09-18 18:04:04 +00001908 (uint64_t)size,
Greg Clayton2a48f522011-05-14 01:50:35 +00001909 permissions & lldb::ePermissionsReadable ? "r" : "",
1910 permissions & lldb::ePermissionsWritable ? "w" : "",
1911 permissions & lldb::ePermissionsExecutable ? "x" : "");
Andy Gibbsa297a972013-06-19 19:04:53 +00001912 assert (packet_len < (int)sizeof(packet));
Greg Clayton2a48f522011-05-14 01:50:35 +00001913 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001914 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton2a48f522011-05-14 01:50:35 +00001915 {
Todd Fialaf105f582014-06-21 00:48:09 +00001916 if (response.IsUnsupportedResponse())
1917 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1918 else if (!response.IsErrorResponse())
Greg Clayton2a48f522011-05-14 01:50:35 +00001919 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1920 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00001921 else
1922 {
1923 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1924 }
Greg Clayton576d8832011-03-22 04:00:09 +00001925 }
1926 return LLDB_INVALID_ADDRESS;
1927}
1928
1929bool
1930GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr)
1931{
Greg Clayton70b57652011-05-15 01:25:55 +00001932 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton576d8832011-03-22 04:00:09 +00001933 {
Greg Clayton70b57652011-05-15 01:25:55 +00001934 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton2a48f522011-05-14 01:50:35 +00001935 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001936 const int packet_len = ::snprintf(packet, sizeof(packet), "_m%" PRIx64, (uint64_t)addr);
Andy Gibbsa297a972013-06-19 19:04:53 +00001937 assert (packet_len < (int)sizeof(packet));
Greg Clayton2a48f522011-05-14 01:50:35 +00001938 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001939 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton2a48f522011-05-14 01:50:35 +00001940 {
Todd Fialaf105f582014-06-21 00:48:09 +00001941 if (response.IsUnsupportedResponse())
1942 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1943 else if (response.IsOKResponse())
Greg Clayton2a48f522011-05-14 01:50:35 +00001944 return true;
Greg Clayton17a0cb62011-05-15 23:46:54 +00001945 }
1946 else
1947 {
1948 m_supports_alloc_dealloc_memory = eLazyBoolNo;
Greg Clayton2a48f522011-05-14 01:50:35 +00001949 }
Greg Clayton576d8832011-03-22 04:00:09 +00001950 }
1951 return false;
1952}
1953
Jim Inghamacff8952013-05-02 00:27:30 +00001954Error
1955GDBRemoteCommunicationClient::Detach (bool keep_stopped)
Greg Clayton37a0a242012-04-11 00:24:49 +00001956{
Jim Inghamacff8952013-05-02 00:27:30 +00001957 Error error;
1958
1959 if (keep_stopped)
1960 {
1961 if (m_supports_detach_stay_stopped == eLazyBoolCalculate)
1962 {
1963 char packet[64];
1964 const int packet_len = ::snprintf(packet, sizeof(packet), "qSupportsDetachAndStayStopped:");
Andy Gibbsa297a972013-06-19 19:04:53 +00001965 assert (packet_len < (int)sizeof(packet));
Jim Inghamacff8952013-05-02 00:27:30 +00001966 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001967 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Jim Inghamacff8952013-05-02 00:27:30 +00001968 {
1969 m_supports_detach_stay_stopped = eLazyBoolYes;
1970 }
1971 else
1972 {
1973 m_supports_detach_stay_stopped = eLazyBoolNo;
1974 }
1975 }
1976
1977 if (m_supports_detach_stay_stopped == eLazyBoolNo)
1978 {
1979 error.SetErrorString("Stays stopped not supported by this target.");
1980 return error;
1981 }
1982 else
1983 {
Jim Ingham6c8824d2014-03-28 20:00:07 +00001984 StringExtractorGDBRemote response;
1985 PacketResult packet_result = SendPacketAndWaitForResponse ("D1", 1, response, false);
Greg Clayton3dedae12013-12-06 21:45:27 +00001986 if (packet_result != PacketResult::Success)
Jim Inghamacff8952013-05-02 00:27:30 +00001987 error.SetErrorString ("Sending extended disconnect packet failed.");
1988 }
1989 }
1990 else
1991 {
Jim Ingham6c8824d2014-03-28 20:00:07 +00001992 StringExtractorGDBRemote response;
1993 PacketResult packet_result = SendPacketAndWaitForResponse ("D", 1, response, false);
Greg Clayton3dedae12013-12-06 21:45:27 +00001994 if (packet_result != PacketResult::Success)
Jim Inghamacff8952013-05-02 00:27:30 +00001995 error.SetErrorString ("Sending disconnect packet failed.");
1996 }
1997 return error;
Greg Clayton37a0a242012-04-11 00:24:49 +00001998}
1999
Greg Clayton46fb5582011-11-18 07:03:08 +00002000Error
2001GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr,
2002 lldb_private::MemoryRegionInfo &region_info)
2003{
2004 Error error;
2005 region_info.Clear();
2006
2007 if (m_supports_memory_region_info != eLazyBoolNo)
2008 {
2009 m_supports_memory_region_info = eLazyBoolYes;
2010 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00002011 const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%" PRIx64, (uint64_t)addr);
Andy Gibbsa297a972013-06-19 19:04:53 +00002012 assert (packet_len < (int)sizeof(packet));
Greg Clayton46fb5582011-11-18 07:03:08 +00002013 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002014 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton46fb5582011-11-18 07:03:08 +00002015 {
2016 std::string name;
2017 std::string value;
2018 addr_t addr_value;
2019 bool success = true;
Jason Molendacb349ee2011-12-13 05:39:38 +00002020 bool saw_permissions = false;
Greg Clayton46fb5582011-11-18 07:03:08 +00002021 while (success && response.GetNameColonValue(name, value))
2022 {
2023 if (name.compare ("start") == 0)
2024 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002025 addr_value = StringConvert::ToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success);
Greg Clayton46fb5582011-11-18 07:03:08 +00002026 if (success)
2027 region_info.GetRange().SetRangeBase(addr_value);
2028 }
2029 else if (name.compare ("size") == 0)
2030 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002031 addr_value = StringConvert::ToUInt64(value.c_str(), 0, 16, &success);
Greg Clayton46fb5582011-11-18 07:03:08 +00002032 if (success)
2033 region_info.GetRange().SetByteSize (addr_value);
2034 }
Jason Molendacb349ee2011-12-13 05:39:38 +00002035 else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid())
Greg Clayton46fb5582011-11-18 07:03:08 +00002036 {
Jason Molendacb349ee2011-12-13 05:39:38 +00002037 saw_permissions = true;
2038 if (region_info.GetRange().Contains (addr))
2039 {
2040 if (value.find('r') != std::string::npos)
2041 region_info.SetReadable (MemoryRegionInfo::eYes);
2042 else
2043 region_info.SetReadable (MemoryRegionInfo::eNo);
2044
2045 if (value.find('w') != std::string::npos)
2046 region_info.SetWritable (MemoryRegionInfo::eYes);
2047 else
2048 region_info.SetWritable (MemoryRegionInfo::eNo);
2049
2050 if (value.find('x') != std::string::npos)
2051 region_info.SetExecutable (MemoryRegionInfo::eYes);
2052 else
2053 region_info.SetExecutable (MemoryRegionInfo::eNo);
2054 }
2055 else
2056 {
2057 // The reported region does not contain this address -- we're looking at an unmapped page
2058 region_info.SetReadable (MemoryRegionInfo::eNo);
2059 region_info.SetWritable (MemoryRegionInfo::eNo);
2060 region_info.SetExecutable (MemoryRegionInfo::eNo);
2061 }
Greg Clayton46fb5582011-11-18 07:03:08 +00002062 }
2063 else if (name.compare ("error") == 0)
2064 {
2065 StringExtractorGDBRemote name_extractor;
2066 // Swap "value" over into "name_extractor"
2067 name_extractor.GetStringRef().swap(value);
2068 // Now convert the HEX bytes into a string value
2069 name_extractor.GetHexByteString (value);
2070 error.SetErrorString(value.c_str());
2071 }
2072 }
Jason Molendacb349ee2011-12-13 05:39:38 +00002073
2074 // We got a valid address range back but no permissions -- which means this is an unmapped page
2075 if (region_info.GetRange().IsValid() && saw_permissions == false)
2076 {
2077 region_info.SetReadable (MemoryRegionInfo::eNo);
2078 region_info.SetWritable (MemoryRegionInfo::eNo);
2079 region_info.SetExecutable (MemoryRegionInfo::eNo);
2080 }
Greg Clayton46fb5582011-11-18 07:03:08 +00002081 }
2082 else
2083 {
2084 m_supports_memory_region_info = eLazyBoolNo;
2085 }
2086 }
2087
2088 if (m_supports_memory_region_info == eLazyBoolNo)
2089 {
2090 error.SetErrorString("qMemoryRegionInfo is not supported");
2091 }
2092 if (error.Fail())
2093 region_info.Clear();
2094 return error;
2095
2096}
2097
Johnny Chen64637202012-05-23 21:09:52 +00002098Error
2099GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num)
2100{
2101 Error error;
2102
2103 if (m_supports_watchpoint_support_info == eLazyBoolYes)
2104 {
2105 num = m_num_supported_hardware_watchpoints;
2106 return error;
2107 }
2108
2109 // Set num to 0 first.
2110 num = 0;
2111 if (m_supports_watchpoint_support_info != eLazyBoolNo)
2112 {
2113 char packet[64];
2114 const int packet_len = ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:");
Andy Gibbsa297a972013-06-19 19:04:53 +00002115 assert (packet_len < (int)sizeof(packet));
Johnny Chen64637202012-05-23 21:09:52 +00002116 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002117 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Johnny Chen64637202012-05-23 21:09:52 +00002118 {
2119 m_supports_watchpoint_support_info = eLazyBoolYes;
2120 std::string name;
2121 std::string value;
2122 while (response.GetNameColonValue(name, value))
2123 {
2124 if (name.compare ("num") == 0)
2125 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002126 num = StringConvert::ToUInt32(value.c_str(), 0, 0);
Johnny Chen64637202012-05-23 21:09:52 +00002127 m_num_supported_hardware_watchpoints = num;
2128 }
2129 }
2130 }
2131 else
2132 {
2133 m_supports_watchpoint_support_info = eLazyBoolNo;
2134 }
2135 }
2136
2137 if (m_supports_watchpoint_support_info == eLazyBoolNo)
2138 {
2139 error.SetErrorString("qWatchpointSupportInfo is not supported");
2140 }
2141 return error;
2142
2143}
Greg Clayton46fb5582011-11-18 07:03:08 +00002144
Enrico Granataf04a2192012-07-13 23:18:48 +00002145lldb_private::Error
2146GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num, bool& after)
2147{
2148 Error error(GetWatchpointSupportInfo(num));
2149 if (error.Success())
2150 error = GetWatchpointsTriggerAfterInstruction(after);
2151 return error;
2152}
2153
2154lldb_private::Error
2155GDBRemoteCommunicationClient::GetWatchpointsTriggerAfterInstruction (bool &after)
2156{
2157 Error error;
2158
2159 // we assume watchpoints will happen after running the relevant opcode
2160 // and we only want to override this behavior if we have explicitly
2161 // received a qHostInfo telling us otherwise
2162 if (m_qHostInfo_is_valid != eLazyBoolYes)
2163 after = true;
2164 else
2165 after = (m_watchpoints_trigger_after_instruction != eLazyBoolNo);
2166 return error;
2167}
2168
Greg Clayton576d8832011-03-22 04:00:09 +00002169int
2170GDBRemoteCommunicationClient::SetSTDIN (char const *path)
2171{
2172 if (path && path[0])
2173 {
2174 StreamString packet;
2175 packet.PutCString("QSetSTDIN:");
2176 packet.PutBytesAsRawHex8(path, strlen(path));
2177
2178 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002179 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002180 {
2181 if (response.IsOKResponse())
2182 return 0;
2183 uint8_t error = response.GetError();
2184 if (error)
2185 return error;
2186 }
2187 }
2188 return -1;
2189}
2190
2191int
2192GDBRemoteCommunicationClient::SetSTDOUT (char const *path)
2193{
2194 if (path && path[0])
2195 {
2196 StreamString packet;
2197 packet.PutCString("QSetSTDOUT:");
2198 packet.PutBytesAsRawHex8(path, strlen(path));
2199
2200 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002201 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002202 {
2203 if (response.IsOKResponse())
2204 return 0;
2205 uint8_t error = response.GetError();
2206 if (error)
2207 return error;
2208 }
2209 }
2210 return -1;
2211}
2212
2213int
2214GDBRemoteCommunicationClient::SetSTDERR (char const *path)
2215{
2216 if (path && path[0])
2217 {
2218 StreamString packet;
2219 packet.PutCString("QSetSTDERR:");
2220 packet.PutBytesAsRawHex8(path, strlen(path));
2221
2222 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002223 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002224 {
2225 if (response.IsOKResponse())
2226 return 0;
2227 uint8_t error = response.GetError();
2228 if (error)
2229 return error;
2230 }
2231 }
2232 return -1;
2233}
2234
Greg Claytonfbb76342013-11-20 21:07:01 +00002235bool
2236GDBRemoteCommunicationClient::GetWorkingDir (std::string &cwd)
2237{
2238 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002239 if (SendPacketAndWaitForResponse ("qGetWorkingDir", response, false) == PacketResult::Success)
Greg Claytonfbb76342013-11-20 21:07:01 +00002240 {
2241 if (response.IsUnsupportedResponse())
2242 return false;
2243 if (response.IsErrorResponse())
2244 return false;
2245 response.GetHexByteString (cwd);
2246 return !cwd.empty();
2247 }
2248 return false;
2249}
2250
Greg Clayton576d8832011-03-22 04:00:09 +00002251int
2252GDBRemoteCommunicationClient::SetWorkingDir (char const *path)
2253{
2254 if (path && path[0])
2255 {
2256 StreamString packet;
2257 packet.PutCString("QSetWorkingDir:");
2258 packet.PutBytesAsRawHex8(path, strlen(path));
2259
2260 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002261 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002262 {
2263 if (response.IsOKResponse())
2264 return 0;
2265 uint8_t error = response.GetError();
2266 if (error)
2267 return error;
2268 }
2269 }
2270 return -1;
2271}
2272
2273int
2274GDBRemoteCommunicationClient::SetDisableASLR (bool enable)
2275{
Greg Clayton32e0a752011-03-30 18:16:51 +00002276 char packet[32];
2277 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0);
Andy Gibbsa297a972013-06-19 19:04:53 +00002278 assert (packet_len < (int)sizeof(packet));
Greg Clayton576d8832011-03-22 04:00:09 +00002279 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002280 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002281 {
2282 if (response.IsOKResponse())
2283 return 0;
2284 uint8_t error = response.GetError();
2285 if (error)
2286 return error;
2287 }
2288 return -1;
2289}
Greg Clayton32e0a752011-03-30 18:16:51 +00002290
Jim Ingham106d0282014-06-25 02:32:56 +00002291int
2292GDBRemoteCommunicationClient::SetDetachOnError (bool enable)
2293{
2294 char packet[32];
2295 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDetachOnError:%i", enable ? 1 : 0);
2296 assert (packet_len < (int)sizeof(packet));
2297 StringExtractorGDBRemote response;
2298 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
2299 {
2300 if (response.IsOKResponse())
2301 return 0;
2302 uint8_t error = response.GetError();
2303 if (error)
2304 return error;
2305 }
2306 return -1;
2307}
2308
2309
Greg Clayton32e0a752011-03-30 18:16:51 +00002310bool
Greg Clayton8b82f082011-04-12 05:54:46 +00002311GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00002312{
2313 if (response.IsNormalResponse())
2314 {
2315 std::string name;
2316 std::string value;
2317 StringExtractor extractor;
Jason Molenda89c37492014-01-27 22:23:20 +00002318
2319 uint32_t cpu = LLDB_INVALID_CPUTYPE;
2320 uint32_t sub = 0;
2321 std::string vendor;
2322 std::string os_type;
Greg Clayton32e0a752011-03-30 18:16:51 +00002323
2324 while (response.GetNameColonValue(name, value))
2325 {
2326 if (name.compare("pid") == 0)
2327 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002328 process_info.SetProcessID (StringConvert::ToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002329 }
2330 else if (name.compare("ppid") == 0)
2331 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002332 process_info.SetParentProcessID (StringConvert::ToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002333 }
2334 else if (name.compare("uid") == 0)
2335 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002336 process_info.SetUserID (StringConvert::ToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002337 }
2338 else if (name.compare("euid") == 0)
2339 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002340 process_info.SetEffectiveUserID (StringConvert::ToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002341 }
2342 else if (name.compare("gid") == 0)
2343 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002344 process_info.SetGroupID (StringConvert::ToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002345 }
2346 else if (name.compare("egid") == 0)
2347 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002348 process_info.SetEffectiveGroupID (StringConvert::ToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002349 }
2350 else if (name.compare("triple") == 0)
2351 {
Greg Clayton44272a42014-09-18 00:18:32 +00002352 StringExtractor extractor;
2353 extractor.GetStringRef().swap(value);
2354 extractor.SetFilePos(0);
2355 extractor.GetHexByteString (value);
Greg Clayton70512312012-05-08 01:45:38 +00002356 process_info.GetArchitecture ().SetTriple (value.c_str());
Greg Clayton32e0a752011-03-30 18:16:51 +00002357 }
2358 else if (name.compare("name") == 0)
2359 {
2360 StringExtractor extractor;
Filipe Cabecinhasf86cf782012-05-07 09:30:51 +00002361 // The process name from ASCII hex bytes since we can't
Greg Clayton32e0a752011-03-30 18:16:51 +00002362 // control the characters in a process name
2363 extractor.GetStringRef().swap(value);
2364 extractor.SetFilePos(0);
2365 extractor.GetHexByteString (value);
Greg Clayton144f3a92011-11-15 03:53:30 +00002366 process_info.GetExecutableFile().SetFile (value.c_str(), false);
Greg Clayton32e0a752011-03-30 18:16:51 +00002367 }
Jason Molenda89c37492014-01-27 22:23:20 +00002368 else if (name.compare("cputype") == 0)
2369 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002370 cpu = StringConvert::ToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 16);
Jason Molenda89c37492014-01-27 22:23:20 +00002371 }
2372 else if (name.compare("cpusubtype") == 0)
2373 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002374 sub = StringConvert::ToUInt32 (value.c_str(), 0, 16);
Jason Molenda89c37492014-01-27 22:23:20 +00002375 }
2376 else if (name.compare("vendor") == 0)
2377 {
2378 vendor = value;
2379 }
2380 else if (name.compare("ostype") == 0)
2381 {
2382 os_type = value;
2383 }
2384 }
2385
2386 if (cpu != LLDB_INVALID_CPUTYPE && !vendor.empty() && !os_type.empty())
2387 {
2388 if (vendor == "apple")
2389 {
2390 process_info.GetArchitecture().SetArchitecture (eArchTypeMachO, cpu, sub);
2391 process_info.GetArchitecture().GetTriple().setVendorName (llvm::StringRef (vendor));
2392 process_info.GetArchitecture().GetTriple().setOSName (llvm::StringRef (os_type));
2393 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002394 }
2395
2396 if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
2397 return true;
2398 }
2399 return false;
2400}
2401
2402bool
Greg Clayton8b82f082011-04-12 05:54:46 +00002403GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00002404{
2405 process_info.Clear();
2406
2407 if (m_supports_qProcessInfoPID)
2408 {
2409 char packet[32];
Daniel Malead01b2952012-11-29 21:49:15 +00002410 const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%" PRIu64, pid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002411 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00002412 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002413 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002414 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002415 return DecodeProcessInfoResponse (response, process_info);
2416 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002417 else
2418 {
2419 m_supports_qProcessInfoPID = false;
2420 return false;
2421 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002422 }
2423 return false;
2424}
2425
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002426bool
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00002427GDBRemoteCommunicationClient::GetCurrentProcessInfo (bool allow_lazy)
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002428{
Todd Fiala3daa1762014-09-15 16:01:29 +00002429 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
2430
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00002431 if (allow_lazy)
2432 {
2433 if (m_qProcessInfo_is_valid == eLazyBoolYes)
2434 return true;
2435 if (m_qProcessInfo_is_valid == eLazyBoolNo)
2436 return false;
2437 }
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002438
2439 GetHostInfo ();
2440
2441 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002442 if (SendPacketAndWaitForResponse ("qProcessInfo", response, false) == PacketResult::Success)
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002443 {
2444 if (response.IsNormalResponse())
2445 {
2446 std::string name;
2447 std::string value;
2448 uint32_t cpu = LLDB_INVALID_CPUTYPE;
2449 uint32_t sub = 0;
2450 std::string arch_name;
2451 std::string os_name;
2452 std::string vendor_name;
2453 std::string triple;
2454 uint32_t pointer_byte_size = 0;
2455 StringExtractor extractor;
Todd Fiala5c9d5bf2014-09-15 15:31:11 +00002456 ByteOrder byte_order = eByteOrderInvalid;
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002457 uint32_t num_keys_decoded = 0;
Todd Fiala9f72b3a2014-05-07 19:28:21 +00002458 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002459 while (response.GetNameColonValue(name, value))
2460 {
2461 if (name.compare("cputype") == 0)
2462 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002463 cpu = StringConvert::ToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 16);
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002464 if (cpu != LLDB_INVALID_CPUTYPE)
2465 ++num_keys_decoded;
2466 }
2467 else if (name.compare("cpusubtype") == 0)
2468 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002469 sub = StringConvert::ToUInt32 (value.c_str(), 0, 16);
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002470 if (sub != 0)
2471 ++num_keys_decoded;
2472 }
Todd Fialac540dd02014-08-26 18:21:02 +00002473 else if (name.compare("triple") == 0)
2474 {
Greg Clayton44272a42014-09-18 00:18:32 +00002475 StringExtractor extractor;
2476 extractor.GetStringRef().swap(value);
2477 extractor.SetFilePos(0);
2478 extractor.GetHexByteString (triple);
Todd Fialac540dd02014-08-26 18:21:02 +00002479 ++num_keys_decoded;
2480 }
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002481 else if (name.compare("ostype") == 0)
2482 {
2483 os_name.swap (value);
2484 ++num_keys_decoded;
2485 }
2486 else if (name.compare("vendor") == 0)
2487 {
2488 vendor_name.swap(value);
2489 ++num_keys_decoded;
2490 }
2491 else if (name.compare("endian") == 0)
2492 {
Todd Fiala5c9d5bf2014-09-15 15:31:11 +00002493 ++num_keys_decoded;
2494 if (value.compare("little") == 0)
2495 byte_order = eByteOrderLittle;
2496 else if (value.compare("big") == 0)
2497 byte_order = eByteOrderBig;
2498 else if (value.compare("pdp") == 0)
2499 byte_order = eByteOrderPDP;
2500 else
2501 --num_keys_decoded;
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002502 }
2503 else if (name.compare("ptrsize") == 0)
2504 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002505 pointer_byte_size = StringConvert::ToUInt32 (value.c_str(), 0, 16);
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002506 if (pointer_byte_size != 0)
2507 ++num_keys_decoded;
2508 }
Todd Fiala9f72b3a2014-05-07 19:28:21 +00002509 else if (name.compare("pid") == 0)
2510 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002511 pid = StringConvert::ToUInt64(value.c_str(), 0, 16);
Todd Fiala9f72b3a2014-05-07 19:28:21 +00002512 if (pid != LLDB_INVALID_PROCESS_ID)
2513 ++num_keys_decoded;
2514 }
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002515 }
2516 if (num_keys_decoded > 0)
2517 m_qProcessInfo_is_valid = eLazyBoolYes;
Todd Fiala9f72b3a2014-05-07 19:28:21 +00002518 if (pid != LLDB_INVALID_PROCESS_ID)
2519 {
2520 m_curr_pid_is_valid = eLazyBoolYes;
2521 m_curr_pid = pid;
2522 }
Todd Fialac540dd02014-08-26 18:21:02 +00002523
2524 // Set the ArchSpec from the triple if we have it.
2525 if (!triple.empty ())
2526 {
2527 m_process_arch.SetTriple (triple.c_str ());
2528 if (pointer_byte_size)
2529 {
2530 assert (pointer_byte_size == m_process_arch.GetAddressByteSize());
2531 }
2532 }
2533 else if (cpu != LLDB_INVALID_CPUTYPE && !os_name.empty() && !vendor_name.empty())
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002534 {
Todd Fiala3daa1762014-09-15 16:01:29 +00002535 llvm::Triple triple(llvm::Twine("-") + vendor_name + "-" + os_name);
2536
2537 assert(triple.getObjectFormat() != llvm::Triple::UnknownObjectFormat);
2538 switch (triple.getObjectFormat()) {
2539 case llvm::Triple::MachO:
2540 m_process_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
2541 break;
2542 case llvm::Triple::ELF:
2543 m_process_arch.SetArchitecture (eArchTypeELF, cpu, sub);
2544 break;
2545 case llvm::Triple::COFF:
2546 m_process_arch.SetArchitecture (eArchTypeCOFF, cpu, sub);
2547 break;
2548 case llvm::Triple::UnknownObjectFormat:
2549 if (log)
2550 log->Printf("error: failed to determine target architecture");
2551 return false;
2552 }
2553
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002554 if (pointer_byte_size)
2555 {
2556 assert (pointer_byte_size == m_process_arch.GetAddressByteSize());
2557 }
Todd Fiala5c9d5bf2014-09-15 15:31:11 +00002558 if (byte_order != eByteOrderInvalid)
2559 {
2560 assert (byte_order == m_process_arch.GetByteOrder());
2561 }
Todd Fiala0cc371c2014-09-05 14:56:13 +00002562 m_process_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
Greg Clayton7ab7f892014-05-29 21:33:45 +00002563 m_process_arch.GetTriple().setOSName(llvm::StringRef (os_name));
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002564 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
2565 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002566 }
Greg Clayton7ab7f892014-05-29 21:33:45 +00002567 return true;
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002568 }
2569 }
2570 else
2571 {
2572 m_qProcessInfo_is_valid = eLazyBoolNo;
2573 }
2574
2575 return false;
2576}
2577
2578
Greg Clayton32e0a752011-03-30 18:16:51 +00002579uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +00002580GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info,
2581 ProcessInstanceInfoList &process_infos)
Greg Clayton32e0a752011-03-30 18:16:51 +00002582{
2583 process_infos.Clear();
2584
2585 if (m_supports_qfProcessInfo)
2586 {
2587 StreamString packet;
2588 packet.PutCString ("qfProcessInfo");
2589 if (!match_info.MatchAllProcesses())
2590 {
2591 packet.PutChar (':');
2592 const char *name = match_info.GetProcessInfo().GetName();
2593 bool has_name_match = false;
2594 if (name && name[0])
2595 {
2596 has_name_match = true;
2597 NameMatchType name_match_type = match_info.GetNameMatchType();
2598 switch (name_match_type)
2599 {
2600 case eNameMatchIgnore:
2601 has_name_match = false;
2602 break;
2603
2604 case eNameMatchEquals:
2605 packet.PutCString ("name_match:equals;");
2606 break;
2607
2608 case eNameMatchContains:
2609 packet.PutCString ("name_match:contains;");
2610 break;
2611
2612 case eNameMatchStartsWith:
2613 packet.PutCString ("name_match:starts_with;");
2614 break;
2615
2616 case eNameMatchEndsWith:
2617 packet.PutCString ("name_match:ends_with;");
2618 break;
2619
2620 case eNameMatchRegularExpression:
2621 packet.PutCString ("name_match:regex;");
2622 break;
2623 }
2624 if (has_name_match)
2625 {
2626 packet.PutCString ("name:");
2627 packet.PutBytesAsRawHex8(name, ::strlen(name));
2628 packet.PutChar (';');
2629 }
2630 }
2631
2632 if (match_info.GetProcessInfo().ProcessIDIsValid())
Daniel Malead01b2952012-11-29 21:49:15 +00002633 packet.Printf("pid:%" PRIu64 ";",match_info.GetProcessInfo().GetProcessID());
Greg Clayton32e0a752011-03-30 18:16:51 +00002634 if (match_info.GetProcessInfo().ParentProcessIDIsValid())
Daniel Malead01b2952012-11-29 21:49:15 +00002635 packet.Printf("parent_pid:%" PRIu64 ";",match_info.GetProcessInfo().GetParentProcessID());
Greg Clayton8b82f082011-04-12 05:54:46 +00002636 if (match_info.GetProcessInfo().UserIDIsValid())
2637 packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID());
2638 if (match_info.GetProcessInfo().GroupIDIsValid())
2639 packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID());
Greg Clayton32e0a752011-03-30 18:16:51 +00002640 if (match_info.GetProcessInfo().EffectiveUserIDIsValid())
2641 packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID());
2642 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
2643 packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID());
2644 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
2645 packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0);
2646 if (match_info.GetProcessInfo().GetArchitecture().IsValid())
2647 {
2648 const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture();
2649 const llvm::Triple &triple = match_arch.GetTriple();
2650 packet.PutCString("triple:");
Matthew Gardinerf39ebbe2014-08-01 05:12:23 +00002651 packet.PutCString(triple.getTriple().c_str());
Greg Clayton32e0a752011-03-30 18:16:51 +00002652 packet.PutChar (';');
2653 }
2654 }
2655 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002656 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002657 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002658 do
2659 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002660 ProcessInstanceInfo process_info;
Greg Clayton32e0a752011-03-30 18:16:51 +00002661 if (!DecodeProcessInfoResponse (response, process_info))
2662 break;
2663 process_infos.Append(process_info);
2664 response.GetStringRef().clear();
2665 response.SetFilePos(0);
Greg Clayton3dedae12013-12-06 21:45:27 +00002666 } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false) == PacketResult::Success);
Greg Clayton32e0a752011-03-30 18:16:51 +00002667 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002668 else
2669 {
2670 m_supports_qfProcessInfo = false;
2671 return 0;
2672 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002673 }
2674 return process_infos.GetSize();
2675
2676}
2677
2678bool
2679GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name)
2680{
2681 if (m_supports_qUserName)
2682 {
2683 char packet[32];
2684 const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002685 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00002686 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002687 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002688 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002689 if (response.IsNormalResponse())
2690 {
2691 // Make sure we parsed the right number of characters. The response is
2692 // the hex encoded user name and should make up the entire packet.
2693 // If there are any non-hex ASCII bytes, the length won't match below..
2694 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
2695 return true;
2696 }
2697 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002698 else
2699 {
2700 m_supports_qUserName = false;
2701 return false;
2702 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002703 }
2704 return false;
2705
2706}
2707
2708bool
2709GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name)
2710{
2711 if (m_supports_qGroupName)
2712 {
2713 char packet[32];
2714 const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002715 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00002716 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002717 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002718 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002719 if (response.IsNormalResponse())
2720 {
2721 // Make sure we parsed the right number of characters. The response is
2722 // the hex encoded group name and should make up the entire packet.
2723 // If there are any non-hex ASCII bytes, the length won't match below..
2724 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
2725 return true;
2726 }
2727 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002728 else
2729 {
2730 m_supports_qGroupName = false;
2731 return false;
2732 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002733 }
2734 return false;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002735}
Greg Clayton32e0a752011-03-30 18:16:51 +00002736
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002737void
2738GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets)
2739{
2740 uint32_t i;
2741 TimeValue start_time, end_time;
2742 uint64_t total_time_nsec;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002743 if (SendSpeedTestPacket (0, 0))
2744 {
Greg Clayton700e5082014-02-21 19:11:28 +00002745 static uint32_t g_send_sizes[] = { 0, 64, 128, 512, 1024 };
2746 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 +00002747 const size_t k_num_send_sizes = llvm::array_lengthof(g_send_sizes);
2748 const size_t k_num_recv_sizes = llvm::array_lengthof(g_recv_sizes);
Greg Clayton700e5082014-02-21 19:11:28 +00002749 const uint64_t k_recv_amount = 4*1024*1024; // Receive 4MB
2750 for (uint32_t send_idx = 0; send_idx < k_num_send_sizes; ++send_idx)
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002751 {
Greg Clayton700e5082014-02-21 19:11:28 +00002752 const uint32_t send_size = g_send_sizes[send_idx];
2753 for (uint32_t recv_idx = 0; recv_idx < k_num_recv_sizes; ++recv_idx)
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002754 {
Greg Clayton700e5082014-02-21 19:11:28 +00002755 const uint32_t recv_size = g_recv_sizes[recv_idx];
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)
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002760 {
Greg Clayton700e5082014-02-21 19:11:28 +00002761 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 start_time = TimeValue::Now();
2774 if (recv_size == 0)
2775 {
2776 for (i=0; i<num_packets; ++i)
2777 {
2778 StringExtractorGDBRemote response;
2779 SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false);
2780 }
2781 }
2782 else
2783 {
2784 uint32_t bytes_read = 0;
2785 while (bytes_read < k_recv_amount)
2786 {
2787 StringExtractorGDBRemote response;
2788 SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false);
2789 bytes_read += recv_size;
2790 }
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002791 }
2792 end_time = TimeValue::Now();
2793 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002794 if (recv_size == 0)
Greg Clayton700e5082014-02-21 19:11:28 +00002795 {
2796 float packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
2797 printf ("%u qSpeedTest(send=%-7u, recv=%-7u) in %" PRIu64 ".%9.9" PRIu64 " sec for %f packets/sec.\n",
2798 num_packets,
2799 send_size,
2800 recv_size,
2801 total_time_nsec / TimeValue::NanoSecPerSec,
2802 total_time_nsec % TimeValue::NanoSecPerSec,
2803 packets_per_second);
2804 }
2805 else
2806 {
2807 float mb_second = ((((float)k_recv_amount)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec) / (1024.0*1024.0);
2808 printf ("%u qSpeedTest(send=%-7u, recv=%-7u) sent 4MB in %" PRIu64 ".%9.9" PRIu64 " sec for %f MB/sec.\n",
2809 num_packets,
2810 send_size,
2811 recv_size,
2812 total_time_nsec / TimeValue::NanoSecPerSec,
2813 total_time_nsec % TimeValue::NanoSecPerSec,
2814 mb_second);
2815 }
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002816 }
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002817 }
2818 }
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002819}
2820
2821bool
2822GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size)
2823{
2824 StreamString packet;
2825 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size);
2826 uint32_t bytes_left = send_size;
2827 while (bytes_left > 0)
2828 {
2829 if (bytes_left >= 26)
2830 {
2831 packet.PutCString("abcdefghijklmnopqrstuvwxyz");
2832 bytes_left -= 26;
2833 }
2834 else
2835 {
2836 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz");
2837 bytes_left = 0;
2838 }
2839 }
2840
2841 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002842 return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success;
Greg Clayton32e0a752011-03-30 18:16:51 +00002843}
Greg Clayton8b82f082011-04-12 05:54:46 +00002844
2845uint16_t
Greg Claytondbf04572013-12-04 19:40:33 +00002846GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort (lldb::pid_t &pid, const char *remote_accept_hostname)
Greg Clayton8b82f082011-04-12 05:54:46 +00002847{
Daniel Maleae0f8f572013-08-26 23:57:52 +00002848 pid = LLDB_INVALID_PROCESS_ID;
Greg Clayton8b82f082011-04-12 05:54:46 +00002849 StringExtractorGDBRemote response;
Daniel Maleae0f8f572013-08-26 23:57:52 +00002850 StreamString stream;
Greg Clayton29b8fc42013-11-21 01:44:58 +00002851 stream.PutCString("qLaunchGDBServer;");
Daniel Maleae0f8f572013-08-26 23:57:52 +00002852 std::string hostname;
Greg Claytondbf04572013-12-04 19:40:33 +00002853 if (remote_accept_hostname && remote_accept_hostname[0])
2854 hostname = remote_accept_hostname;
Daniel Maleae0f8f572013-08-26 23:57:52 +00002855 else
2856 {
Zachary Turner97a14e62014-08-19 17:18:29 +00002857 if (HostInfo::GetHostname(hostname))
Greg Claytondbf04572013-12-04 19:40:33 +00002858 {
2859 // Make the GDB server we launch only accept connections from this host
2860 stream.Printf("host:%s;", hostname.c_str());
2861 }
2862 else
2863 {
2864 // Make the GDB server we launch accept connections from any host since we can't figure out the hostname
2865 stream.Printf("host:*;");
2866 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00002867 }
2868 const char *packet = stream.GetData();
2869 int packet_len = stream.GetSize();
2870
Vince Harron1b5a74e2015-01-21 22:42:49 +00002871 // give the process a few seconds to startup
2872 const uint32_t old_packet_timeout = SetPacketTimeout (10);
2873 auto result = SendPacketAndWaitForResponse(packet, packet_len, response, false);
2874 SetPacketTimeout (old_packet_timeout);
2875 if (result == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002876 {
2877 std::string name;
2878 std::string value;
2879 uint16_t port = 0;
Greg Clayton8b82f082011-04-12 05:54:46 +00002880 while (response.GetNameColonValue(name, value))
2881 {
Daniel Maleae0f8f572013-08-26 23:57:52 +00002882 if (name.compare("port") == 0)
Vince Harron5275aaa2015-01-15 20:08:35 +00002883 port = StringConvert::ToUInt32(value.c_str(), 0, 0);
Daniel Maleae0f8f572013-08-26 23:57:52 +00002884 else if (name.compare("pid") == 0)
Vince Harron5275aaa2015-01-15 20:08:35 +00002885 pid = StringConvert::ToUInt64(value.c_str(), LLDB_INVALID_PROCESS_ID, 0);
Greg Clayton8b82f082011-04-12 05:54:46 +00002886 }
2887 return port;
2888 }
2889 return 0;
2890}
2891
2892bool
Daniel Maleae0f8f572013-08-26 23:57:52 +00002893GDBRemoteCommunicationClient::KillSpawnedProcess (lldb::pid_t pid)
2894{
2895 StreamString stream;
2896 stream.Printf ("qKillSpawnedProcess:%" PRId64 , pid);
2897 const char *packet = stream.GetData();
2898 int packet_len = stream.GetSize();
Sylvestre Ledrufd654c42013-10-06 09:51:02 +00002899
Daniel Maleae0f8f572013-08-26 23:57:52 +00002900 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002901 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00002902 {
2903 if (response.IsOKResponse())
2904 return true;
2905 }
2906 return false;
2907}
2908
2909bool
Jason Molendae9ca4af2013-02-23 02:04:45 +00002910GDBRemoteCommunicationClient::SetCurrentThread (uint64_t tid)
Greg Clayton8b82f082011-04-12 05:54:46 +00002911{
2912 if (m_curr_tid == tid)
2913 return true;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002914
Greg Clayton8b82f082011-04-12 05:54:46 +00002915 char packet[32];
2916 int packet_len;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002917 if (tid == UINT64_MAX)
2918 packet_len = ::snprintf (packet, sizeof(packet), "Hg-1");
Greg Clayton8b82f082011-04-12 05:54:46 +00002919 else
Jason Molendae9ca4af2013-02-23 02:04:45 +00002920 packet_len = ::snprintf (packet, sizeof(packet), "Hg%" PRIx64, tid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002921 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002922 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002923 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002924 {
2925 if (response.IsOKResponse())
2926 {
2927 m_curr_tid = tid;
2928 return true;
2929 }
2930 }
2931 return false;
2932}
2933
2934bool
Jason Molendae9ca4af2013-02-23 02:04:45 +00002935GDBRemoteCommunicationClient::SetCurrentThreadForRun (uint64_t tid)
Greg Clayton8b82f082011-04-12 05:54:46 +00002936{
2937 if (m_curr_tid_run == tid)
2938 return true;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002939
Greg Clayton8b82f082011-04-12 05:54:46 +00002940 char packet[32];
2941 int packet_len;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002942 if (tid == UINT64_MAX)
2943 packet_len = ::snprintf (packet, sizeof(packet), "Hc-1");
Greg Clayton8b82f082011-04-12 05:54:46 +00002944 else
Jason Molendae9ca4af2013-02-23 02:04:45 +00002945 packet_len = ::snprintf (packet, sizeof(packet), "Hc%" PRIx64, tid);
2946
Andy Gibbsa297a972013-06-19 19:04:53 +00002947 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002948 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002949 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002950 {
2951 if (response.IsOKResponse())
2952 {
2953 m_curr_tid_run = tid;
2954 return true;
2955 }
2956 }
2957 return false;
2958}
2959
2960bool
2961GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response)
2962{
Greg Clayton3dedae12013-12-06 21:45:27 +00002963 if (SendPacketAndWaitForResponse("?", 1, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002964 return response.IsNormalResponse();
2965 return false;
2966}
2967
2968bool
Greg Claytonf402f782012-10-13 02:11:55 +00002969GDBRemoteCommunicationClient::GetThreadStopInfo (lldb::tid_t tid, StringExtractorGDBRemote &response)
Greg Clayton8b82f082011-04-12 05:54:46 +00002970{
2971 if (m_supports_qThreadStopInfo)
2972 {
2973 char packet[256];
Daniel Malead01b2952012-11-29 21:49:15 +00002974 int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%" PRIx64, tid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002975 assert (packet_len < (int)sizeof(packet));
Greg Clayton3dedae12013-12-06 21:45:27 +00002976 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002977 {
Greg Claytonef8180a2013-10-15 00:14:28 +00002978 if (response.IsUnsupportedResponse())
2979 m_supports_qThreadStopInfo = false;
2980 else if (response.IsNormalResponse())
Greg Clayton8b82f082011-04-12 05:54:46 +00002981 return true;
2982 else
2983 return false;
2984 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002985 else
2986 {
2987 m_supports_qThreadStopInfo = false;
2988 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002989 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002990 return false;
2991}
2992
2993
2994uint8_t
2995GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length)
2996{
Todd Fiala616b8272014-10-09 00:55:04 +00002997 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
2998 if (log)
2999 log->Printf ("GDBRemoteCommunicationClient::%s() %s at addr = 0x%" PRIx64,
3000 __FUNCTION__, insert ? "add" : "remove", addr);
3001
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003002 // Check if the stub is known not to support this breakpoint type
3003 if (!SupportsGDBStoppointPacket(type))
3004 return UINT8_MAX;
3005 // Construct the breakpoint packet
Greg Clayton8b82f082011-04-12 05:54:46 +00003006 char packet[64];
3007 const int packet_len = ::snprintf (packet,
3008 sizeof(packet),
Daniel Malead01b2952012-11-29 21:49:15 +00003009 "%c%i,%" PRIx64 ",%x",
Greg Clayton8b82f082011-04-12 05:54:46 +00003010 insert ? 'Z' : 'z',
3011 type,
3012 addr,
3013 length);
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003014 // Check we haven't overwritten the end of the packet buffer
Andy Gibbsa297a972013-06-19 19:04:53 +00003015 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00003016 StringExtractorGDBRemote response;
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003017 // Try to send the breakpoint packet, and check that it was correctly sent
Greg Clayton3dedae12013-12-06 21:45:27 +00003018 if (SendPacketAndWaitForResponse(packet, packet_len, response, true) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00003019 {
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003020 // Receive and OK packet when the breakpoint successfully placed
Greg Clayton8b82f082011-04-12 05:54:46 +00003021 if (response.IsOKResponse())
3022 return 0;
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003023
3024 // Error while setting breakpoint, send back specific error
3025 if (response.IsErrorResponse())
Greg Clayton8b82f082011-04-12 05:54:46 +00003026 return response.GetError();
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003027
3028 // Empty packet informs us that breakpoint is not supported
3029 if (response.IsUnsupportedResponse())
Greg Clayton17a0cb62011-05-15 23:46:54 +00003030 {
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003031 // Disable this breakpoint type since it is unsupported
3032 switch (type)
3033 {
Greg Clayton17a0cb62011-05-15 23:46:54 +00003034 case eBreakpointSoftware: m_supports_z0 = false; break;
3035 case eBreakpointHardware: m_supports_z1 = false; break;
3036 case eWatchpointWrite: m_supports_z2 = false; break;
3037 case eWatchpointRead: m_supports_z3 = false; break;
3038 case eWatchpointReadWrite: m_supports_z4 = false; break;
Chaoren Lin0be9ebb2015-02-03 01:51:50 +00003039 case eStoppointInvalid: return UINT8_MAX;
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003040 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00003041 }
3042 }
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003043 // Signal generic failure
Greg Clayton8b82f082011-04-12 05:54:46 +00003044 return UINT8_MAX;
3045}
Greg Claytonadc00cb2011-05-20 23:38:13 +00003046
3047size_t
3048GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
3049 bool &sequence_mutex_unavailable)
3050{
3051 Mutex::Locker locker;
3052 thread_ids.clear();
3053
Jim Ingham4ceb9282012-06-08 22:50:40 +00003054 if (GetSequenceMutex (locker, "ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex"))
Greg Claytonadc00cb2011-05-20 23:38:13 +00003055 {
3056 sequence_mutex_unavailable = false;
3057 StringExtractorGDBRemote response;
3058
Greg Clayton3dedae12013-12-06 21:45:27 +00003059 PacketResult packet_result;
3060 for (packet_result = SendPacketAndWaitForResponseNoLock ("qfThreadInfo", strlen("qfThreadInfo"), response);
3061 packet_result == PacketResult::Success && response.IsNormalResponse();
3062 packet_result = SendPacketAndWaitForResponseNoLock ("qsThreadInfo", strlen("qsThreadInfo"), response))
Greg Claytonadc00cb2011-05-20 23:38:13 +00003063 {
3064 char ch = response.GetChar();
3065 if (ch == 'l')
3066 break;
3067 if (ch == 'm')
3068 {
3069 do
3070 {
Jason Molendae9ca4af2013-02-23 02:04:45 +00003071 tid_t tid = response.GetHexMaxU64(false, LLDB_INVALID_THREAD_ID);
Greg Claytonadc00cb2011-05-20 23:38:13 +00003072
3073 if (tid != LLDB_INVALID_THREAD_ID)
3074 {
3075 thread_ids.push_back (tid);
3076 }
3077 ch = response.GetChar(); // Skip the command separator
3078 } while (ch == ','); // Make sure we got a comma separator
3079 }
3080 }
3081 }
3082 else
3083 {
Jim Ingham4ceb9282012-06-08 22:50:40 +00003084#if defined (LLDB_CONFIGURATION_DEBUG)
3085 // assert(!"ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex");
3086#else
Greg Clayton5160ce52013-03-27 23:08:40 +00003087 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Claytonc3c0b0e2012-04-12 19:04:34 +00003088 if (log)
3089 log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'");
Jim Ingham4ceb9282012-06-08 22:50:40 +00003090#endif
Greg Claytonadc00cb2011-05-20 23:38:13 +00003091 sequence_mutex_unavailable = true;
3092 }
3093 return thread_ids.size();
3094}
Greg Clayton37a0a242012-04-11 00:24:49 +00003095
3096lldb::addr_t
3097GDBRemoteCommunicationClient::GetShlibInfoAddr()
3098{
3099 if (!IsRunning())
3100 {
3101 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003102 if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false) == PacketResult::Success)
Greg Clayton37a0a242012-04-11 00:24:49 +00003103 {
3104 if (response.IsNormalResponse())
3105 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
3106 }
3107 }
3108 return LLDB_INVALID_ADDRESS;
3109}
3110
Daniel Maleae0f8f572013-08-26 23:57:52 +00003111lldb_private::Error
3112GDBRemoteCommunicationClient::RunShellCommand (const char *command, // Shouldn't be NULL
3113 const char *working_dir, // Pass NULL to use the current working directory
3114 int *status_ptr, // Pass NULL if you don't want the process exit status
3115 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
3116 std::string *command_output, // Pass NULL if you don't want the command output
3117 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish
3118{
3119 lldb_private::StreamString stream;
Greg Claytonfbb76342013-11-20 21:07:01 +00003120 stream.PutCString("qPlatform_shell:");
Daniel Maleae0f8f572013-08-26 23:57:52 +00003121 stream.PutBytesAsRawHex8(command, strlen(command));
3122 stream.PutChar(',');
3123 stream.PutHex32(timeout_sec);
3124 if (working_dir && *working_dir)
3125 {
3126 stream.PutChar(',');
3127 stream.PutBytesAsRawHex8(working_dir, strlen(working_dir));
3128 }
3129 const char *packet = stream.GetData();
3130 int packet_len = stream.GetSize();
3131 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003132 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003133 {
3134 if (response.GetChar() != 'F')
3135 return Error("malformed reply");
3136 if (response.GetChar() != ',')
3137 return Error("malformed reply");
3138 uint32_t exitcode = response.GetHexMaxU32(false, UINT32_MAX);
3139 if (exitcode == UINT32_MAX)
3140 return Error("unable to run remote process");
3141 else if (status_ptr)
3142 *status_ptr = exitcode;
3143 if (response.GetChar() != ',')
3144 return Error("malformed reply");
3145 uint32_t signo = response.GetHexMaxU32(false, UINT32_MAX);
3146 if (signo_ptr)
3147 *signo_ptr = signo;
3148 if (response.GetChar() != ',')
3149 return Error("malformed reply");
3150 std::string output;
3151 response.GetEscapedBinaryData(output);
3152 if (command_output)
3153 command_output->assign(output);
3154 return Error();
3155 }
3156 return Error("unable to send packet");
3157}
3158
Greg Claytonfbb76342013-11-20 21:07:01 +00003159Error
3160GDBRemoteCommunicationClient::MakeDirectory (const char *path,
3161 uint32_t file_permissions)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003162{
3163 lldb_private::StreamString stream;
Greg Claytonfbb76342013-11-20 21:07:01 +00003164 stream.PutCString("qPlatform_mkdir:");
3165 stream.PutHex32(file_permissions);
Daniel Maleae0f8f572013-08-26 23:57:52 +00003166 stream.PutChar(',');
Greg Claytonfbb76342013-11-20 21:07:01 +00003167 stream.PutBytesAsRawHex8(path, strlen(path));
Daniel Maleae0f8f572013-08-26 23:57:52 +00003168 const char *packet = stream.GetData();
3169 int packet_len = stream.GetSize();
3170 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003171 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003172 {
Greg Claytonfbb76342013-11-20 21:07:01 +00003173 return Error(response.GetHexMaxU32(false, UINT32_MAX), eErrorTypePOSIX);
Daniel Maleae0f8f572013-08-26 23:57:52 +00003174 }
Greg Claytonfbb76342013-11-20 21:07:01 +00003175 return Error();
Daniel Maleae0f8f572013-08-26 23:57:52 +00003176
3177}
3178
Greg Claytonfbb76342013-11-20 21:07:01 +00003179Error
3180GDBRemoteCommunicationClient::SetFilePermissions (const char *path,
3181 uint32_t file_permissions)
3182{
3183 lldb_private::StreamString stream;
3184 stream.PutCString("qPlatform_chmod:");
3185 stream.PutHex32(file_permissions);
3186 stream.PutChar(',');
3187 stream.PutBytesAsRawHex8(path, strlen(path));
3188 const char *packet = stream.GetData();
3189 int packet_len = stream.GetSize();
3190 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003191 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Claytonfbb76342013-11-20 21:07:01 +00003192 {
3193 return Error(response.GetHexMaxU32(false, UINT32_MAX), eErrorTypePOSIX);
3194 }
3195 return Error();
3196
3197}
3198
Daniel Maleae0f8f572013-08-26 23:57:52 +00003199static uint64_t
3200ParseHostIOPacketResponse (StringExtractorGDBRemote &response,
3201 uint64_t fail_result,
3202 Error &error)
3203{
3204 response.SetFilePos(0);
3205 if (response.GetChar() != 'F')
3206 return fail_result;
3207 int32_t result = response.GetS32 (-2);
3208 if (result == -2)
3209 return fail_result;
3210 if (response.GetChar() == ',')
3211 {
3212 int result_errno = response.GetS32 (-2);
3213 if (result_errno != -2)
3214 error.SetError(result_errno, eErrorTypePOSIX);
3215 else
3216 error.SetError(-1, eErrorTypeGeneric);
3217 }
3218 else
3219 error.Clear();
3220 return result;
3221}
3222lldb::user_id_t
3223GDBRemoteCommunicationClient::OpenFile (const lldb_private::FileSpec& file_spec,
3224 uint32_t flags,
3225 mode_t mode,
3226 Error &error)
3227{
3228 lldb_private::StreamString stream;
3229 stream.PutCString("vFile:open:");
3230 std::string path (file_spec.GetPath());
3231 if (path.empty())
3232 return UINT64_MAX;
3233 stream.PutCStringAsRawHex8(path.c_str());
3234 stream.PutChar(',');
3235 const uint32_t posix_open_flags = File::ConvertOpenOptionsForPOSIXOpen(flags);
3236 stream.PutHex32(posix_open_flags);
3237 stream.PutChar(',');
3238 stream.PutHex32(mode);
3239 const char* packet = stream.GetData();
3240 int packet_len = stream.GetSize();
3241 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003242 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003243 {
3244 return ParseHostIOPacketResponse (response, UINT64_MAX, error);
3245 }
3246 return UINT64_MAX;
3247}
3248
3249bool
3250GDBRemoteCommunicationClient::CloseFile (lldb::user_id_t fd,
3251 Error &error)
3252{
3253 lldb_private::StreamString stream;
3254 stream.Printf("vFile:close:%i", (int)fd);
3255 const char* packet = stream.GetData();
3256 int packet_len = stream.GetSize();
3257 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003258 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003259 {
3260 return ParseHostIOPacketResponse (response, -1, error) == 0;
3261 }
Deepak Panickald66b50c2013-10-22 12:27:43 +00003262 return false;
Daniel Maleae0f8f572013-08-26 23:57:52 +00003263}
3264
3265// Extension of host I/O packets to get the file size.
3266lldb::user_id_t
3267GDBRemoteCommunicationClient::GetFileSize (const lldb_private::FileSpec& file_spec)
3268{
3269 lldb_private::StreamString stream;
3270 stream.PutCString("vFile:size:");
3271 std::string path (file_spec.GetPath());
3272 stream.PutCStringAsRawHex8(path.c_str());
3273 const char* packet = stream.GetData();
3274 int packet_len = stream.GetSize();
3275 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003276 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003277 {
3278 if (response.GetChar() != 'F')
3279 return UINT64_MAX;
3280 uint32_t retcode = response.GetHexMaxU64(false, UINT64_MAX);
3281 return retcode;
3282 }
3283 return UINT64_MAX;
3284}
3285
Greg Claytonfbb76342013-11-20 21:07:01 +00003286Error
3287GDBRemoteCommunicationClient::GetFilePermissions(const char *path, uint32_t &file_permissions)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003288{
Greg Claytonfbb76342013-11-20 21:07:01 +00003289 Error error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00003290 lldb_private::StreamString stream;
3291 stream.PutCString("vFile:mode:");
Greg Claytonfbb76342013-11-20 21:07:01 +00003292 stream.PutCStringAsRawHex8(path);
Daniel Maleae0f8f572013-08-26 23:57:52 +00003293 const char* packet = stream.GetData();
3294 int packet_len = stream.GetSize();
3295 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003296 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003297 {
3298 if (response.GetChar() != 'F')
3299 {
3300 error.SetErrorStringWithFormat ("invalid response to '%s' packet", packet);
Daniel Maleae0f8f572013-08-26 23:57:52 +00003301 }
Greg Claytonfbb76342013-11-20 21:07:01 +00003302 else
Daniel Maleae0f8f572013-08-26 23:57:52 +00003303 {
Greg Claytonfbb76342013-11-20 21:07:01 +00003304 const uint32_t mode = response.GetS32(-1);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00003305 if (static_cast<int32_t>(mode) == -1)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003306 {
Greg Claytonfbb76342013-11-20 21:07:01 +00003307 if (response.GetChar() == ',')
3308 {
3309 int response_errno = response.GetS32(-1);
3310 if (response_errno > 0)
3311 error.SetError(response_errno, lldb::eErrorTypePOSIX);
3312 else
3313 error.SetErrorToGenericError();
3314 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00003315 else
3316 error.SetErrorToGenericError();
3317 }
Greg Claytonfbb76342013-11-20 21:07:01 +00003318 else
3319 {
3320 file_permissions = mode & (S_IRWXU|S_IRWXG|S_IRWXO);
3321 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00003322 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00003323 }
3324 else
3325 {
3326 error.SetErrorStringWithFormat ("failed to send '%s' packet", packet);
3327 }
Greg Claytonfbb76342013-11-20 21:07:01 +00003328 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00003329}
3330
3331uint64_t
3332GDBRemoteCommunicationClient::ReadFile (lldb::user_id_t fd,
3333 uint64_t offset,
3334 void *dst,
3335 uint64_t dst_len,
3336 Error &error)
3337{
3338 lldb_private::StreamString stream;
3339 stream.Printf("vFile:pread:%i,%" PRId64 ",%" PRId64, (int)fd, dst_len, offset);
3340 const char* packet = stream.GetData();
3341 int packet_len = stream.GetSize();
3342 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003343 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003344 {
3345 if (response.GetChar() != 'F')
3346 return 0;
3347 uint32_t retcode = response.GetHexMaxU32(false, UINT32_MAX);
3348 if (retcode == UINT32_MAX)
3349 return retcode;
3350 const char next = (response.Peek() ? *response.Peek() : 0);
3351 if (next == ',')
3352 return 0;
3353 if (next == ';')
3354 {
3355 response.GetChar(); // skip the semicolon
3356 std::string buffer;
3357 if (response.GetEscapedBinaryData(buffer))
3358 {
3359 const uint64_t data_to_write = std::min<uint64_t>(dst_len, buffer.size());
3360 if (data_to_write > 0)
3361 memcpy(dst, &buffer[0], data_to_write);
3362 return data_to_write;
3363 }
3364 }
3365 }
3366 return 0;
3367}
3368
3369uint64_t
3370GDBRemoteCommunicationClient::WriteFile (lldb::user_id_t fd,
3371 uint64_t offset,
3372 const void* src,
3373 uint64_t src_len,
3374 Error &error)
3375{
3376 lldb_private::StreamGDBRemote stream;
3377 stream.Printf("vFile:pwrite:%i,%" PRId64 ",", (int)fd, offset);
3378 stream.PutEscapedBytes(src, src_len);
3379 const char* packet = stream.GetData();
3380 int packet_len = stream.GetSize();
3381 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003382 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003383 {
3384 if (response.GetChar() != 'F')
3385 {
3386 error.SetErrorStringWithFormat("write file failed");
3387 return 0;
3388 }
3389 uint64_t bytes_written = response.GetU64(UINT64_MAX);
3390 if (bytes_written == UINT64_MAX)
3391 {
3392 error.SetErrorToGenericError();
3393 if (response.GetChar() == ',')
3394 {
3395 int response_errno = response.GetS32(-1);
3396 if (response_errno > 0)
3397 error.SetError(response_errno, lldb::eErrorTypePOSIX);
3398 }
3399 return 0;
3400 }
3401 return bytes_written;
3402 }
3403 else
3404 {
3405 error.SetErrorString ("failed to send vFile:pwrite packet");
3406 }
3407 return 0;
3408}
3409
Greg Claytonfbb76342013-11-20 21:07:01 +00003410Error
3411GDBRemoteCommunicationClient::CreateSymlink (const char *src, const char *dst)
3412{
3413 Error error;
3414 lldb_private::StreamGDBRemote stream;
3415 stream.PutCString("vFile:symlink:");
3416 // the unix symlink() command reverses its parameters where the dst if first,
3417 // so we follow suit here
3418 stream.PutCStringAsRawHex8(dst);
3419 stream.PutChar(',');
3420 stream.PutCStringAsRawHex8(src);
3421 const char* packet = stream.GetData();
3422 int packet_len = stream.GetSize();
3423 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003424 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Claytonfbb76342013-11-20 21:07:01 +00003425 {
3426 if (response.GetChar() == 'F')
3427 {
3428 uint32_t result = response.GetU32(UINT32_MAX);
3429 if (result != 0)
3430 {
3431 error.SetErrorToGenericError();
3432 if (response.GetChar() == ',')
3433 {
3434 int response_errno = response.GetS32(-1);
3435 if (response_errno > 0)
3436 error.SetError(response_errno, lldb::eErrorTypePOSIX);
3437 }
3438 }
3439 }
3440 else
3441 {
3442 // Should have returned with 'F<result>[,<errno>]'
3443 error.SetErrorStringWithFormat("symlink failed");
3444 }
3445 }
3446 else
3447 {
3448 error.SetErrorString ("failed to send vFile:symlink packet");
3449 }
3450 return error;
3451}
3452
3453Error
3454GDBRemoteCommunicationClient::Unlink (const char *path)
3455{
3456 Error error;
3457 lldb_private::StreamGDBRemote stream;
3458 stream.PutCString("vFile:unlink:");
3459 // the unix symlink() command reverses its parameters where the dst if first,
3460 // so we follow suit here
3461 stream.PutCStringAsRawHex8(path);
3462 const char* packet = stream.GetData();
3463 int packet_len = stream.GetSize();
3464 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003465 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Claytonfbb76342013-11-20 21:07:01 +00003466 {
3467 if (response.GetChar() == 'F')
3468 {
3469 uint32_t result = response.GetU32(UINT32_MAX);
3470 if (result != 0)
3471 {
3472 error.SetErrorToGenericError();
3473 if (response.GetChar() == ',')
3474 {
3475 int response_errno = response.GetS32(-1);
3476 if (response_errno > 0)
3477 error.SetError(response_errno, lldb::eErrorTypePOSIX);
3478 }
3479 }
3480 }
3481 else
3482 {
3483 // Should have returned with 'F<result>[,<errno>]'
3484 error.SetErrorStringWithFormat("unlink failed");
3485 }
3486 }
3487 else
3488 {
3489 error.SetErrorString ("failed to send vFile:unlink packet");
3490 }
3491 return error;
3492}
3493
Daniel Maleae0f8f572013-08-26 23:57:52 +00003494// Extension of host I/O packets to get whether a file exists.
3495bool
3496GDBRemoteCommunicationClient::GetFileExists (const lldb_private::FileSpec& file_spec)
3497{
3498 lldb_private::StreamString stream;
3499 stream.PutCString("vFile:exists:");
3500 std::string path (file_spec.GetPath());
3501 stream.PutCStringAsRawHex8(path.c_str());
3502 const char* packet = stream.GetData();
3503 int packet_len = stream.GetSize();
3504 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003505 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003506 {
3507 if (response.GetChar() != 'F')
3508 return false;
3509 if (response.GetChar() != ',')
3510 return false;
3511 bool retcode = (response.GetChar() != '0');
3512 return retcode;
3513 }
3514 return false;
3515}
3516
3517bool
3518GDBRemoteCommunicationClient::CalculateMD5 (const lldb_private::FileSpec& file_spec,
3519 uint64_t &high,
3520 uint64_t &low)
3521{
3522 lldb_private::StreamString stream;
3523 stream.PutCString("vFile:MD5:");
3524 std::string path (file_spec.GetPath());
3525 stream.PutCStringAsRawHex8(path.c_str());
3526 const char* packet = stream.GetData();
3527 int packet_len = stream.GetSize();
3528 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003529 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003530 {
3531 if (response.GetChar() != 'F')
3532 return false;
3533 if (response.GetChar() != ',')
3534 return false;
3535 if (response.Peek() && *response.Peek() == 'x')
3536 return false;
3537 low = response.GetHexMaxU64(false, UINT64_MAX);
3538 high = response.GetHexMaxU64(false, UINT64_MAX);
3539 return true;
3540 }
3541 return false;
3542}
Greg Claytonf74cf862013-11-13 23:28:31 +00003543
3544bool
Jason Molendaa3329782014-03-29 18:54:20 +00003545GDBRemoteCommunicationClient::AvoidGPackets (ProcessGDBRemote *process)
3546{
3547 // Some targets have issues with g/G packets and we need to avoid using them
3548 if (m_avoid_g_packets == eLazyBoolCalculate)
3549 {
3550 if (process)
3551 {
3552 m_avoid_g_packets = eLazyBoolNo;
3553 const ArchSpec &arch = process->GetTarget().GetArchitecture();
3554 if (arch.IsValid()
3555 && arch.GetTriple().getVendor() == llvm::Triple::Apple
3556 && arch.GetTriple().getOS() == llvm::Triple::IOS
Todd Fialad8eaa172014-07-23 14:37:35 +00003557 && arch.GetTriple().getArch() == llvm::Triple::aarch64)
Jason Molendaa3329782014-03-29 18:54:20 +00003558 {
3559 m_avoid_g_packets = eLazyBoolYes;
3560 uint32_t gdb_server_version = GetGDBServerProgramVersion();
3561 if (gdb_server_version != 0)
3562 {
3563 const char *gdb_server_name = GetGDBServerProgramName();
3564 if (gdb_server_name && strcmp(gdb_server_name, "debugserver") == 0)
3565 {
3566 if (gdb_server_version >= 310)
3567 m_avoid_g_packets = eLazyBoolNo;
3568 }
3569 }
3570 }
3571 }
3572 }
3573 return m_avoid_g_packets == eLazyBoolYes;
3574}
3575
3576bool
Greg Claytonf74cf862013-11-13 23:28:31 +00003577GDBRemoteCommunicationClient::ReadRegister(lldb::tid_t tid, uint32_t reg, StringExtractorGDBRemote &response)
3578{
3579 Mutex::Locker locker;
3580 if (GetSequenceMutex (locker, "Didn't get sequence mutex for p packet."))
3581 {
3582 const bool thread_suffix_supported = GetThreadSuffixSupported();
3583
3584 if (thread_suffix_supported || SetCurrentThread(tid))
3585 {
3586 char packet[64];
3587 int packet_len = 0;
3588 if (thread_suffix_supported)
3589 packet_len = ::snprintf (packet, sizeof(packet), "p%x;thread:%4.4" PRIx64 ";", reg, tid);
3590 else
3591 packet_len = ::snprintf (packet, sizeof(packet), "p%x", reg);
3592 assert (packet_len < ((int)sizeof(packet) - 1));
Greg Clayton3dedae12013-12-06 21:45:27 +00003593 return SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success;
Greg Claytonf74cf862013-11-13 23:28:31 +00003594 }
3595 }
3596 return false;
3597
3598}
3599
3600
3601bool
3602GDBRemoteCommunicationClient::ReadAllRegisters (lldb::tid_t tid, StringExtractorGDBRemote &response)
3603{
3604 Mutex::Locker locker;
3605 if (GetSequenceMutex (locker, "Didn't get sequence mutex for g packet."))
3606 {
3607 const bool thread_suffix_supported = GetThreadSuffixSupported();
3608
3609 if (thread_suffix_supported || SetCurrentThread(tid))
3610 {
3611 char packet[64];
3612 int packet_len = 0;
3613 // Get all registers in one packet
3614 if (thread_suffix_supported)
3615 packet_len = ::snprintf (packet, sizeof(packet), "g;thread:%4.4" PRIx64 ";", tid);
3616 else
3617 packet_len = ::snprintf (packet, sizeof(packet), "g");
3618 assert (packet_len < ((int)sizeof(packet) - 1));
Greg Clayton3dedae12013-12-06 21:45:27 +00003619 return SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success;
Greg Claytonf74cf862013-11-13 23:28:31 +00003620 }
3621 }
3622 return false;
3623}
3624bool
3625GDBRemoteCommunicationClient::SaveRegisterState (lldb::tid_t tid, uint32_t &save_id)
3626{
3627 save_id = 0; // Set to invalid save ID
3628 if (m_supports_QSaveRegisterState == eLazyBoolNo)
3629 return false;
3630
3631 m_supports_QSaveRegisterState = eLazyBoolYes;
3632 Mutex::Locker locker;
3633 if (GetSequenceMutex (locker, "Didn't get sequence mutex for QSaveRegisterState."))
3634 {
3635 const bool thread_suffix_supported = GetThreadSuffixSupported();
3636 if (thread_suffix_supported || SetCurrentThread(tid))
3637 {
3638 char packet[256];
3639 if (thread_suffix_supported)
3640 ::snprintf (packet, sizeof(packet), "QSaveRegisterState;thread:%4.4" PRIx64 ";", tid);
3641 else
3642 ::strncpy (packet, "QSaveRegisterState", sizeof(packet));
3643
3644 StringExtractorGDBRemote response;
3645
Greg Clayton3dedae12013-12-06 21:45:27 +00003646 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
Greg Claytonf74cf862013-11-13 23:28:31 +00003647 {
3648 if (response.IsUnsupportedResponse())
3649 {
3650 // This packet isn't supported, don't try calling it again
3651 m_supports_QSaveRegisterState = eLazyBoolNo;
3652 }
3653
3654 const uint32_t response_save_id = response.GetU32(0);
3655 if (response_save_id != 0)
3656 {
3657 save_id = response_save_id;
3658 return true;
3659 }
3660 }
3661 }
3662 }
3663 return false;
3664}
3665
3666bool
3667GDBRemoteCommunicationClient::RestoreRegisterState (lldb::tid_t tid, uint32_t save_id)
3668{
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003669 // We use the "m_supports_QSaveRegisterState" variable here because the
Greg Claytonf74cf862013-11-13 23:28:31 +00003670 // QSaveRegisterState and QRestoreRegisterState packets must both be supported in
3671 // order to be useful
3672 if (m_supports_QSaveRegisterState == eLazyBoolNo)
3673 return false;
3674
3675 Mutex::Locker locker;
3676 if (GetSequenceMutex (locker, "Didn't get sequence mutex for QRestoreRegisterState."))
3677 {
3678 const bool thread_suffix_supported = GetThreadSuffixSupported();
3679 if (thread_suffix_supported || SetCurrentThread(tid))
3680 {
3681 char packet[256];
3682 if (thread_suffix_supported)
3683 ::snprintf (packet, sizeof(packet), "QRestoreRegisterState:%u;thread:%4.4" PRIx64 ";", save_id, tid);
3684 else
3685 ::snprintf (packet, sizeof(packet), "QRestoreRegisterState:%u" PRIx64 ";", save_id);
3686
3687 StringExtractorGDBRemote response;
3688
Greg Clayton3dedae12013-12-06 21:45:27 +00003689 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
Greg Claytonf74cf862013-11-13 23:28:31 +00003690 {
3691 if (response.IsOKResponse())
3692 {
3693 return true;
3694 }
3695 else if (response.IsUnsupportedResponse())
3696 {
3697 // This packet isn't supported, don't try calling this packet or
3698 // QSaveRegisterState again...
3699 m_supports_QSaveRegisterState = eLazyBoolNo;
3700 }
3701 }
3702 }
3703 }
3704 return false;
3705}