blob: 7d33658a31c502f231b51c9e83ae6b1e47e839fd [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"
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +000024#include "lldb/Core/ModuleSpec.h"
Greg Clayton576d8832011-03-22 04:00:09 +000025#include "lldb/Core/State.h"
Daniel Maleae0f8f572013-08-26 23:57:52 +000026#include "lldb/Core/StreamGDBRemote.h"
Greg Clayton576d8832011-03-22 04:00:09 +000027#include "lldb/Core/StreamString.h"
Zachary Turner93a66fc2014-10-06 21:22:36 +000028#include "lldb/Host/ConnectionFileDescriptor.h"
Greg Clayton576d8832011-03-22 04:00:09 +000029#include "lldb/Host/Endian.h"
30#include "lldb/Host/Host.h"
Zachary Turner97a14e62014-08-19 17:18:29 +000031#include "lldb/Host/HostInfo.h"
Vince Harron5275aaa2015-01-15 20:08:35 +000032#include "lldb/Host/StringConvert.h"
Greg Clayton576d8832011-03-22 04:00:09 +000033#include "lldb/Host/TimeValue.h"
Jason Molendaa3329782014-03-29 18:54:20 +000034#include "lldb/Target/Target.h"
Zachary Turner93749ab2015-03-03 21:51:25 +000035#include "lldb/Target/MemoryRegionInfo.h"
Greg Clayton576d8832011-03-22 04:00:09 +000036
37// Project includes
38#include "Utility/StringExtractorGDBRemote.h"
39#include "ProcessGDBRemote.h"
40#include "ProcessGDBRemoteLog.h"
Virgile Bellob2f1fb22013-08-23 12:44:05 +000041#include "lldb/Host/Config.h"
Greg Clayton576d8832011-03-22 04:00:09 +000042
43using namespace lldb;
44using namespace lldb_private;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000045using namespace lldb_private::process_gdb_remote;
Greg Clayton576d8832011-03-22 04:00:09 +000046
Todd Fiala0a70a842014-05-28 16:43:26 +000047#if defined(LLDB_DISABLE_POSIX) && !defined(SIGSTOP)
Virgile Bellob2f1fb22013-08-23 12:44:05 +000048#define SIGSTOP 17
49#endif
50
Greg Clayton576d8832011-03-22 04:00:09 +000051//----------------------------------------------------------------------
52// GDBRemoteCommunicationClient constructor
53//----------------------------------------------------------------------
Tamas Berghammere13c2732015-02-11 10:29:30 +000054GDBRemoteCommunicationClient::GDBRemoteCommunicationClient() :
55 GDBRemoteCommunication("gdb-remote.client", "gdb-remote.client.rx_packet"),
Greg Clayton576d8832011-03-22 04:00:09 +000056 m_supports_not_sending_acks (eLazyBoolCalculate),
57 m_supports_thread_suffix (eLazyBoolCalculate),
Greg Clayton44633992012-04-10 03:22:03 +000058 m_supports_threads_in_stop_reply (eLazyBoolCalculate),
Greg Clayton576d8832011-03-22 04:00:09 +000059 m_supports_vCont_all (eLazyBoolCalculate),
60 m_supports_vCont_any (eLazyBoolCalculate),
61 m_supports_vCont_c (eLazyBoolCalculate),
62 m_supports_vCont_C (eLazyBoolCalculate),
63 m_supports_vCont_s (eLazyBoolCalculate),
64 m_supports_vCont_S (eLazyBoolCalculate),
Greg Clayton32e0a752011-03-30 18:16:51 +000065 m_qHostInfo_is_valid (eLazyBoolCalculate),
Todd Fiala9f72b3a2014-05-07 19:28:21 +000066 m_curr_pid_is_valid (eLazyBoolCalculate),
Jason Molendaf17b5ac2012-12-19 02:54:03 +000067 m_qProcessInfo_is_valid (eLazyBoolCalculate),
Jason Molendaa3329782014-03-29 18:54:20 +000068 m_qGDBServerVersion_is_valid (eLazyBoolCalculate),
Greg Clayton70b57652011-05-15 01:25:55 +000069 m_supports_alloc_dealloc_memory (eLazyBoolCalculate),
Greg Clayton46fb5582011-11-18 07:03:08 +000070 m_supports_memory_region_info (eLazyBoolCalculate),
Johnny Chen64637202012-05-23 21:09:52 +000071 m_supports_watchpoint_support_info (eLazyBoolCalculate),
Jim Inghamacff8952013-05-02 00:27:30 +000072 m_supports_detach_stay_stopped (eLazyBoolCalculate),
Enrico Granataf04a2192012-07-13 23:18:48 +000073 m_watchpoints_trigger_after_instruction(eLazyBoolCalculate),
Jim Inghamcd16df92012-07-20 21:37:13 +000074 m_attach_or_wait_reply(eLazyBoolCalculate),
Jim Ingham279ceec2012-07-25 21:12:43 +000075 m_prepare_for_reg_writing_reply (eLazyBoolCalculate),
Eric Christopher2490f5c2013-08-30 17:50:57 +000076 m_supports_p (eLazyBoolCalculate),
Jason Molendabdc4f122014-05-06 02:59:39 +000077 m_supports_x (eLazyBoolCalculate),
Jason Molendaa3329782014-03-29 18:54:20 +000078 m_avoid_g_packets (eLazyBoolCalculate),
Greg Claytonf74cf862013-11-13 23:28:31 +000079 m_supports_QSaveRegisterState (eLazyBoolCalculate),
Steve Pucci03904ac2014-03-04 23:18:46 +000080 m_supports_qXfer_auxv_read (eLazyBoolCalculate),
Steve Pucci5ae54ae2014-01-25 05:46:51 +000081 m_supports_qXfer_libraries_read (eLazyBoolCalculate),
82 m_supports_qXfer_libraries_svr4_read (eLazyBoolCalculate),
Greg Clayton253204e2015-04-16 23:11:06 +000083 m_supports_qXfer_features_read (eLazyBoolCalculate),
Steve Pucci5ae54ae2014-01-25 05:46:51 +000084 m_supports_augmented_libraries_svr4_read (eLazyBoolCalculate),
Jason Molenda705b1802014-06-13 02:37:02 +000085 m_supports_jThreadExtendedInfo (eLazyBoolCalculate),
Greg Clayton32e0a752011-03-30 18:16:51 +000086 m_supports_qProcessInfoPID (true),
87 m_supports_qfProcessInfo (true),
88 m_supports_qUserName (true),
89 m_supports_qGroupName (true),
Greg Clayton8b82f082011-04-12 05:54:46 +000090 m_supports_qThreadStopInfo (true),
91 m_supports_z0 (true),
92 m_supports_z1 (true),
93 m_supports_z2 (true),
94 m_supports_z3 (true),
95 m_supports_z4 (true),
Greg Clayton89600582013-10-10 17:53:50 +000096 m_supports_QEnvironment (true),
97 m_supports_QEnvironmentHexEncoded (true),
Todd Fiala9f72b3a2014-05-07 19:28:21 +000098 m_curr_pid (LLDB_INVALID_PROCESS_ID),
Greg Clayton8b82f082011-04-12 05:54:46 +000099 m_curr_tid (LLDB_INVALID_THREAD_ID),
100 m_curr_tid_run (LLDB_INVALID_THREAD_ID),
Johnny Chen64637202012-05-23 21:09:52 +0000101 m_num_supported_hardware_watchpoints (0),
Greg Clayton576d8832011-03-22 04:00:09 +0000102 m_async_mutex (Mutex::eMutexTypeRecursive),
103 m_async_packet_predicate (false),
104 m_async_packet (),
Jim Inghama6195b72013-12-18 01:24:33 +0000105 m_async_result (PacketResult::Success),
Greg Clayton576d8832011-03-22 04:00:09 +0000106 m_async_response (),
107 m_async_signal (-1),
Jim Inghamb8cd5752014-04-16 02:24:17 +0000108 m_interrupt_sent (false),
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000109 m_thread_id_to_used_usec_map (),
Greg Clayton1cb64962011-03-24 04:28:38 +0000110 m_host_arch(),
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000111 m_process_arch(),
Greg Clayton1cb64962011-03-24 04:28:38 +0000112 m_os_version_major (UINT32_MAX),
113 m_os_version_minor (UINT32_MAX),
Greg Clayton9ac6d2d2013-10-25 18:13:17 +0000114 m_os_version_update (UINT32_MAX),
115 m_os_build (),
116 m_os_kernel (),
117 m_hostname (),
Jason Molendaa3329782014-03-29 18:54:20 +0000118 m_gdb_server_name(),
119 m_gdb_server_version(UINT32_MAX),
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000120 m_default_packet_timeout (0),
121 m_max_packet_size (0)
Greg Clayton576d8832011-03-22 04:00:09 +0000122{
Greg Clayton576d8832011-03-22 04:00:09 +0000123}
124
125//----------------------------------------------------------------------
126// Destructor
127//----------------------------------------------------------------------
128GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient()
129{
Greg Clayton576d8832011-03-22 04:00:09 +0000130 if (IsConnected())
Greg Clayton576d8832011-03-22 04:00:09 +0000131 Disconnect();
Greg Clayton576d8832011-03-22 04:00:09 +0000132}
133
134bool
Greg Clayton1cb64962011-03-24 04:28:38 +0000135GDBRemoteCommunicationClient::HandshakeWithServer (Error *error_ptr)
136{
Greg Claytonfb909312013-11-23 01:58:15 +0000137 ResetDiscoverableSettings();
138
Greg Clayton1cb64962011-03-24 04:28:38 +0000139 // Start the read thread after we send the handshake ack since if we
140 // fail to send the handshake ack, there is no reason to continue...
141 if (SendAck())
Greg Claytonfb909312013-11-23 01:58:15 +0000142 {
Ed Maste48f986f2013-12-18 15:31:45 +0000143 // Wait for any responses that might have been queued up in the remote
144 // GDB server and flush them all
145 StringExtractorGDBRemote response;
146 PacketResult packet_result = PacketResult::Success;
147 const uint32_t timeout_usec = 10 * 1000; // Wait for 10 ms for a response
148 while (packet_result == PacketResult::Success)
149 packet_result = WaitForPacketWithTimeoutMicroSecondsNoLock (response, timeout_usec);
150
Greg Claytonfb909312013-11-23 01:58:15 +0000151 // The return value from QueryNoAckModeSupported() is true if the packet
152 // was sent and _any_ response (including UNIMPLEMENTED) was received),
153 // or false if no response was received. This quickly tells us if we have
154 // a live connection to a remote GDB server...
155 if (QueryNoAckModeSupported())
156 {
Greg Clayton700e5082014-02-21 19:11:28 +0000157#if 0
158 // Set above line to "#if 1" to test packet speed if remote GDB server
159 // supports the qSpeedTest packet...
160 TestPacketSpeed(10000);
161#endif
Greg Claytonfb909312013-11-23 01:58:15 +0000162 return true;
163 }
164 else
165 {
166 if (error_ptr)
167 error_ptr->SetErrorString("failed to get reply to handshake packet");
168 }
169 }
170 else
171 {
172 if (error_ptr)
173 error_ptr->SetErrorString("failed to send the handshake ack");
174 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000175 return false;
176}
177
Greg Claytonfb909312013-11-23 01:58:15 +0000178bool
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000179GDBRemoteCommunicationClient::GetAugmentedLibrariesSVR4ReadSupported ()
180{
181 if (m_supports_augmented_libraries_svr4_read == eLazyBoolCalculate)
182 {
183 GetRemoteQSupported();
184 }
185 return (m_supports_augmented_libraries_svr4_read == eLazyBoolYes);
186}
187
188bool
189GDBRemoteCommunicationClient::GetQXferLibrariesSVR4ReadSupported ()
190{
191 if (m_supports_qXfer_libraries_svr4_read == eLazyBoolCalculate)
192 {
193 GetRemoteQSupported();
194 }
195 return (m_supports_qXfer_libraries_svr4_read == eLazyBoolYes);
196}
197
198bool
199GDBRemoteCommunicationClient::GetQXferLibrariesReadSupported ()
200{
201 if (m_supports_qXfer_libraries_read == eLazyBoolCalculate)
202 {
203 GetRemoteQSupported();
204 }
205 return (m_supports_qXfer_libraries_read == eLazyBoolYes);
206}
207
Steve Pucci03904ac2014-03-04 23:18:46 +0000208bool
209GDBRemoteCommunicationClient::GetQXferAuxvReadSupported ()
210{
211 if (m_supports_qXfer_auxv_read == eLazyBoolCalculate)
212 {
213 GetRemoteQSupported();
214 }
215 return (m_supports_qXfer_auxv_read == eLazyBoolYes);
216}
217
Colin Rileyc3c95b22015-04-16 15:51:33 +0000218bool
219GDBRemoteCommunicationClient::GetQXferFeaturesReadSupported ()
220{
221 if (m_supports_qXfer_features_read == eLazyBoolCalculate)
222 {
223 GetRemoteQSupported();
224 }
225 return (m_supports_qXfer_features_read == eLazyBoolYes);
226}
227
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000228uint64_t
229GDBRemoteCommunicationClient::GetRemoteMaxPacketSize()
230{
231 if (m_max_packet_size == 0)
232 {
233 GetRemoteQSupported();
234 }
235 return m_max_packet_size;
236}
237
238bool
Greg Clayton1cb64962011-03-24 04:28:38 +0000239GDBRemoteCommunicationClient::QueryNoAckModeSupported ()
Greg Clayton576d8832011-03-22 04:00:09 +0000240{
241 if (m_supports_not_sending_acks == eLazyBoolCalculate)
242 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000243 m_send_acks = true;
Greg Clayton576d8832011-03-22 04:00:09 +0000244 m_supports_not_sending_acks = eLazyBoolNo;
Greg Clayton1cb64962011-03-24 04:28:38 +0000245
Jason Molenda36a216e2014-07-24 01:36:24 +0000246 // This is the first real packet that we'll send in a debug session and it may take a little
247 // longer than normal to receive a reply. Wait at least 6 seconds for a reply to this packet.
248
249 const uint32_t minimum_timeout = 6;
250 uint32_t old_timeout = GetPacketTimeoutInMicroSeconds() / lldb_private::TimeValue::MicroSecPerSec;
Tamas Berghammer912800c2015-02-24 10:23:39 +0000251 GDBRemoteCommunication::ScopedTimeout timeout (*this, std::max (old_timeout, minimum_timeout));
Jason Molenda36a216e2014-07-24 01:36:24 +0000252
Greg Clayton1cb64962011-03-24 04:28:38 +0000253 StringExtractorGDBRemote response;
Tamas Berghammer912800c2015-02-24 10:23:39 +0000254 if (SendPacketAndWaitForResponse("QStartNoAckMode", response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000255 {
256 if (response.IsOKResponse())
Greg Clayton1cb64962011-03-24 04:28:38 +0000257 {
258 m_send_acks = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000259 m_supports_not_sending_acks = eLazyBoolYes;
Greg Clayton1cb64962011-03-24 04:28:38 +0000260 }
Greg Claytonfb909312013-11-23 01:58:15 +0000261 return true;
Greg Clayton576d8832011-03-22 04:00:09 +0000262 }
263 }
Greg Claytonfb909312013-11-23 01:58:15 +0000264 return false;
Greg Clayton576d8832011-03-22 04:00:09 +0000265}
266
267void
Greg Clayton44633992012-04-10 03:22:03 +0000268GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported ()
269{
270 if (m_supports_threads_in_stop_reply == eLazyBoolCalculate)
271 {
272 m_supports_threads_in_stop_reply = eLazyBoolNo;
273
274 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000275 if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response, false) == PacketResult::Success)
Greg Clayton44633992012-04-10 03:22:03 +0000276 {
277 if (response.IsOKResponse())
278 m_supports_threads_in_stop_reply = eLazyBoolYes;
279 }
280 }
281}
282
Jim Inghamcd16df92012-07-20 21:37:13 +0000283bool
284GDBRemoteCommunicationClient::GetVAttachOrWaitSupported ()
285{
286 if (m_attach_or_wait_reply == eLazyBoolCalculate)
287 {
288 m_attach_or_wait_reply = eLazyBoolNo;
289
290 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000291 if (SendPacketAndWaitForResponse("qVAttachOrWaitSupported", response, false) == PacketResult::Success)
Jim Inghamcd16df92012-07-20 21:37:13 +0000292 {
293 if (response.IsOKResponse())
294 m_attach_or_wait_reply = eLazyBoolYes;
295 }
296 }
297 if (m_attach_or_wait_reply == eLazyBoolYes)
298 return true;
299 else
300 return false;
301}
302
Jim Ingham279ceec2012-07-25 21:12:43 +0000303bool
304GDBRemoteCommunicationClient::GetSyncThreadStateSupported ()
305{
306 if (m_prepare_for_reg_writing_reply == eLazyBoolCalculate)
307 {
308 m_prepare_for_reg_writing_reply = eLazyBoolNo;
309
310 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000311 if (SendPacketAndWaitForResponse("qSyncThreadStateSupported", response, false) == PacketResult::Success)
Jim Ingham279ceec2012-07-25 21:12:43 +0000312 {
313 if (response.IsOKResponse())
314 m_prepare_for_reg_writing_reply = eLazyBoolYes;
315 }
316 }
317 if (m_prepare_for_reg_writing_reply == eLazyBoolYes)
318 return true;
319 else
320 return false;
321}
322
Greg Clayton44633992012-04-10 03:22:03 +0000323
324void
Greg Clayton576d8832011-03-22 04:00:09 +0000325GDBRemoteCommunicationClient::ResetDiscoverableSettings()
326{
327 m_supports_not_sending_acks = eLazyBoolCalculate;
328 m_supports_thread_suffix = eLazyBoolCalculate;
Greg Clayton44633992012-04-10 03:22:03 +0000329 m_supports_threads_in_stop_reply = eLazyBoolCalculate;
Greg Clayton576d8832011-03-22 04:00:09 +0000330 m_supports_vCont_c = eLazyBoolCalculate;
331 m_supports_vCont_C = eLazyBoolCalculate;
332 m_supports_vCont_s = eLazyBoolCalculate;
333 m_supports_vCont_S = eLazyBoolCalculate;
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000334 m_supports_p = eLazyBoolCalculate;
Jason Molendabdc4f122014-05-06 02:59:39 +0000335 m_supports_x = eLazyBoolCalculate;
Greg Claytonf74cf862013-11-13 23:28:31 +0000336 m_supports_QSaveRegisterState = eLazyBoolCalculate;
Greg Clayton32e0a752011-03-30 18:16:51 +0000337 m_qHostInfo_is_valid = eLazyBoolCalculate;
Todd Fiala9f72b3a2014-05-07 19:28:21 +0000338 m_curr_pid_is_valid = eLazyBoolCalculate;
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000339 m_qProcessInfo_is_valid = eLazyBoolCalculate;
Jason Molendaa3329782014-03-29 18:54:20 +0000340 m_qGDBServerVersion_is_valid = eLazyBoolCalculate;
Greg Clayton70b57652011-05-15 01:25:55 +0000341 m_supports_alloc_dealloc_memory = eLazyBoolCalculate;
Greg Clayton46fb5582011-11-18 07:03:08 +0000342 m_supports_memory_region_info = eLazyBoolCalculate;
Jim Ingham279ceec2012-07-25 21:12:43 +0000343 m_prepare_for_reg_writing_reply = eLazyBoolCalculate;
344 m_attach_or_wait_reply = eLazyBoolCalculate;
Jason Molendaa3329782014-03-29 18:54:20 +0000345 m_avoid_g_packets = eLazyBoolCalculate;
Steve Pucci03904ac2014-03-04 23:18:46 +0000346 m_supports_qXfer_auxv_read = eLazyBoolCalculate;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000347 m_supports_qXfer_libraries_read = eLazyBoolCalculate;
348 m_supports_qXfer_libraries_svr4_read = eLazyBoolCalculate;
Colin Rileyc3c95b22015-04-16 15:51:33 +0000349 m_supports_qXfer_features_read = eLazyBoolCalculate;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000350 m_supports_augmented_libraries_svr4_read = eLazyBoolCalculate;
Greg Clayton2a48f522011-05-14 01:50:35 +0000351
Greg Clayton32e0a752011-03-30 18:16:51 +0000352 m_supports_qProcessInfoPID = true;
353 m_supports_qfProcessInfo = true;
354 m_supports_qUserName = true;
355 m_supports_qGroupName = true;
Greg Clayton8b82f082011-04-12 05:54:46 +0000356 m_supports_qThreadStopInfo = true;
357 m_supports_z0 = true;
358 m_supports_z1 = true;
359 m_supports_z2 = true;
360 m_supports_z3 = true;
361 m_supports_z4 = true;
Greg Clayton89600582013-10-10 17:53:50 +0000362 m_supports_QEnvironment = true;
363 m_supports_QEnvironmentHexEncoded = true;
Jason Molendaa3329782014-03-29 18:54:20 +0000364
Greg Claytond314e812011-03-23 00:09:55 +0000365 m_host_arch.Clear();
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000366 m_process_arch.Clear();
Jason Molendaa3329782014-03-29 18:54:20 +0000367 m_os_version_major = UINT32_MAX;
368 m_os_version_minor = UINT32_MAX;
369 m_os_version_update = UINT32_MAX;
370 m_os_build.clear();
371 m_os_kernel.clear();
372 m_hostname.clear();
373 m_gdb_server_name.clear();
374 m_gdb_server_version = UINT32_MAX;
375 m_default_packet_timeout = 0;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000376
377 m_max_packet_size = 0;
Greg Clayton576d8832011-03-22 04:00:09 +0000378}
379
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000380void
381GDBRemoteCommunicationClient::GetRemoteQSupported ()
382{
383 // Clear out any capabilities we expect to see in the qSupported response
Steve Pucci03904ac2014-03-04 23:18:46 +0000384 m_supports_qXfer_auxv_read = eLazyBoolNo;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000385 m_supports_qXfer_libraries_read = eLazyBoolNo;
Steve Pucci03904ac2014-03-04 23:18:46 +0000386 m_supports_qXfer_libraries_svr4_read = eLazyBoolNo;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000387 m_supports_augmented_libraries_svr4_read = eLazyBoolNo;
Colin Rileyc3c95b22015-04-16 15:51:33 +0000388 m_supports_qXfer_features_read = eLazyBoolNo;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000389 m_max_packet_size = UINT64_MAX; // It's supposed to always be there, but if not, we assume no limit
390
Colin Rileyc3c95b22015-04-16 15:51:33 +0000391 // build the qSupported packet
392 std::vector<std::string> features = {"xmlRegisters=i386,arm,mips"};
393 StreamString packet;
394 packet.PutCString( "qSupported" );
395 for ( uint32_t i = 0; i < features.size( ); ++i )
396 {
397 packet.PutCString( i==0 ? ":" : ";");
398 packet.PutCString( features[i].c_str( ) );
399 }
400
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000401 StringExtractorGDBRemote response;
Colin Rileyc3c95b22015-04-16 15:51:33 +0000402 if (SendPacketAndWaitForResponse(packet.GetData(),
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000403 response,
404 /*send_async=*/false) == PacketResult::Success)
405 {
406 const char *response_cstr = response.GetStringRef().c_str();
Steve Pucci03904ac2014-03-04 23:18:46 +0000407 if (::strstr (response_cstr, "qXfer:auxv:read+"))
408 m_supports_qXfer_auxv_read = eLazyBoolYes;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000409 if (::strstr (response_cstr, "qXfer:libraries-svr4:read+"))
410 m_supports_qXfer_libraries_svr4_read = eLazyBoolYes;
411 if (::strstr (response_cstr, "augmented-libraries-svr4-read"))
412 {
413 m_supports_qXfer_libraries_svr4_read = eLazyBoolYes; // implied
414 m_supports_augmented_libraries_svr4_read = eLazyBoolYes;
415 }
416 if (::strstr (response_cstr, "qXfer:libraries:read+"))
417 m_supports_qXfer_libraries_read = eLazyBoolYes;
Colin Rileyc3c95b22015-04-16 15:51:33 +0000418 if (::strstr (response_cstr, "qXfer:features:read+"))
419 m_supports_qXfer_features_read = eLazyBoolYes;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000420
421 const char *packet_size_str = ::strstr (response_cstr, "PacketSize=");
422 if (packet_size_str)
423 {
424 StringExtractorGDBRemote packet_response(packet_size_str + strlen("PacketSize="));
425 m_max_packet_size = packet_response.GetHexMaxU64(/*little_endian=*/false, UINT64_MAX);
426 if (m_max_packet_size == 0)
427 {
428 m_max_packet_size = UINT64_MAX; // Must have been a garbled response
429 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
430 if (log)
431 log->Printf ("Garbled PacketSize spec in qSupported response");
432 }
433 }
434 }
435}
Greg Clayton576d8832011-03-22 04:00:09 +0000436
437bool
438GDBRemoteCommunicationClient::GetThreadSuffixSupported ()
439{
440 if (m_supports_thread_suffix == eLazyBoolCalculate)
441 {
442 StringExtractorGDBRemote response;
443 m_supports_thread_suffix = eLazyBoolNo;
Greg Clayton3dedae12013-12-06 21:45:27 +0000444 if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000445 {
446 if (response.IsOKResponse())
447 m_supports_thread_suffix = eLazyBoolYes;
448 }
449 }
450 return m_supports_thread_suffix;
451}
452bool
453GDBRemoteCommunicationClient::GetVContSupported (char flavor)
454{
455 if (m_supports_vCont_c == eLazyBoolCalculate)
456 {
457 StringExtractorGDBRemote response;
458 m_supports_vCont_any = eLazyBoolNo;
459 m_supports_vCont_all = eLazyBoolNo;
460 m_supports_vCont_c = eLazyBoolNo;
461 m_supports_vCont_C = eLazyBoolNo;
462 m_supports_vCont_s = eLazyBoolNo;
463 m_supports_vCont_S = eLazyBoolNo;
Greg Clayton3dedae12013-12-06 21:45:27 +0000464 if (SendPacketAndWaitForResponse("vCont?", response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000465 {
466 const char *response_cstr = response.GetStringRef().c_str();
467 if (::strstr (response_cstr, ";c"))
468 m_supports_vCont_c = eLazyBoolYes;
469
470 if (::strstr (response_cstr, ";C"))
471 m_supports_vCont_C = eLazyBoolYes;
472
473 if (::strstr (response_cstr, ";s"))
474 m_supports_vCont_s = eLazyBoolYes;
475
476 if (::strstr (response_cstr, ";S"))
477 m_supports_vCont_S = eLazyBoolYes;
478
479 if (m_supports_vCont_c == eLazyBoolYes &&
480 m_supports_vCont_C == eLazyBoolYes &&
481 m_supports_vCont_s == eLazyBoolYes &&
482 m_supports_vCont_S == eLazyBoolYes)
483 {
484 m_supports_vCont_all = eLazyBoolYes;
485 }
486
487 if (m_supports_vCont_c == eLazyBoolYes ||
488 m_supports_vCont_C == eLazyBoolYes ||
489 m_supports_vCont_s == eLazyBoolYes ||
490 m_supports_vCont_S == eLazyBoolYes)
491 {
492 m_supports_vCont_any = eLazyBoolYes;
493 }
494 }
495 }
496
497 switch (flavor)
498 {
499 case 'a': return m_supports_vCont_any;
500 case 'A': return m_supports_vCont_all;
501 case 'c': return m_supports_vCont_c;
502 case 'C': return m_supports_vCont_C;
503 case 's': return m_supports_vCont_s;
504 case 'S': return m_supports_vCont_S;
505 default: break;
506 }
507 return false;
508}
509
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000510// Check if the target supports 'p' packet. It sends out a 'p'
511// packet and checks the response. A normal packet will tell us
512// that support is available.
Sean Callananb1de1142013-09-04 23:24:15 +0000513//
514// Takes a valid thread ID because p needs to apply to a thread.
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000515bool
Sean Callananb1de1142013-09-04 23:24:15 +0000516GDBRemoteCommunicationClient::GetpPacketSupported (lldb::tid_t tid)
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000517{
518 if (m_supports_p == eLazyBoolCalculate)
519 {
520 StringExtractorGDBRemote response;
521 m_supports_p = eLazyBoolNo;
Sean Callananb1de1142013-09-04 23:24:15 +0000522 char packet[256];
523 if (GetThreadSuffixSupported())
524 snprintf(packet, sizeof(packet), "p0;thread:%" PRIx64 ";", tid);
525 else
526 snprintf(packet, sizeof(packet), "p0");
527
Greg Clayton3dedae12013-12-06 21:45:27 +0000528 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000529 {
530 if (response.IsNormalResponse())
531 m_supports_p = eLazyBoolYes;
532 }
533 }
534 return m_supports_p;
535}
Greg Clayton576d8832011-03-22 04:00:09 +0000536
Jason Molendabdc4f122014-05-06 02:59:39 +0000537bool
Jason Molenda705b1802014-06-13 02:37:02 +0000538GDBRemoteCommunicationClient::GetThreadExtendedInfoSupported ()
539{
540 if (m_supports_jThreadExtendedInfo == eLazyBoolCalculate)
541 {
542 StringExtractorGDBRemote response;
543 m_supports_jThreadExtendedInfo = eLazyBoolNo;
544 if (SendPacketAndWaitForResponse("jThreadExtendedInfo:", response, false) == PacketResult::Success)
545 {
546 if (response.IsOKResponse())
547 {
548 m_supports_jThreadExtendedInfo = eLazyBoolYes;
549 }
550 }
551 }
552 return m_supports_jThreadExtendedInfo;
553}
554
555bool
Jason Molendabdc4f122014-05-06 02:59:39 +0000556GDBRemoteCommunicationClient::GetxPacketSupported ()
557{
558 if (m_supports_x == eLazyBoolCalculate)
559 {
560 StringExtractorGDBRemote response;
561 m_supports_x = eLazyBoolNo;
562 char packet[256];
563 snprintf (packet, sizeof (packet), "x0,0");
564 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
565 {
566 if (response.IsOKResponse())
567 m_supports_x = eLazyBoolYes;
568 }
569 }
570 return m_supports_x;
571}
572
Greg Clayton3dedae12013-12-06 21:45:27 +0000573GDBRemoteCommunicationClient::PacketResult
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000574GDBRemoteCommunicationClient::SendPacketsAndConcatenateResponses
575(
576 const char *payload_prefix,
577 std::string &response_string
578)
579{
580 Mutex::Locker locker;
581 if (!GetSequenceMutex(locker,
582 "ProcessGDBRemote::SendPacketsAndConcatenateResponses() failed due to not getting the sequence mutex"))
583 {
584 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
585 if (log)
586 log->Printf("error: failed to get packet sequence mutex, not sending packets with prefix '%s'",
587 payload_prefix);
588 return PacketResult::ErrorNoSequenceLock;
589 }
590
591 response_string = "";
592 std::string payload_prefix_str(payload_prefix);
593 unsigned int response_size = 0x1000;
594 if (response_size > GetRemoteMaxPacketSize()) { // May send qSupported packet
595 response_size = GetRemoteMaxPacketSize();
596 }
597
598 for (unsigned int offset = 0; true; offset += response_size)
599 {
600 StringExtractorGDBRemote this_response;
601 // Construct payload
602 char sizeDescriptor[128];
603 snprintf(sizeDescriptor, sizeof(sizeDescriptor), "%x,%x", offset, response_size);
604 PacketResult result = SendPacketAndWaitForResponse((payload_prefix_str + sizeDescriptor).c_str(),
605 this_response,
606 /*send_async=*/false);
607 if (result != PacketResult::Success)
608 return result;
609
610 const std::string &this_string = this_response.GetStringRef();
611
612 // Check for m or l as first character; l seems to mean this is the last chunk
613 char first_char = *this_string.c_str();
614 if (first_char != 'm' && first_char != 'l')
615 {
616 return PacketResult::ErrorReplyInvalid;
617 }
Steve Pucci03904ac2014-03-04 23:18:46 +0000618 // Concatenate the result so far (skipping 'm' or 'l')
619 response_string.append(this_string, 1, std::string::npos);
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000620 if (first_char == 'l')
621 // We're done
622 return PacketResult::Success;
623 }
624}
625
626GDBRemoteCommunicationClient::PacketResult
Greg Clayton576d8832011-03-22 04:00:09 +0000627GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
628(
629 const char *payload,
630 StringExtractorGDBRemote &response,
631 bool send_async
632)
633{
634 return SendPacketAndWaitForResponse (payload,
635 ::strlen (payload),
636 response,
637 send_async);
638}
639
Greg Clayton3dedae12013-12-06 21:45:27 +0000640GDBRemoteCommunicationClient::PacketResult
641GDBRemoteCommunicationClient::SendPacketAndWaitForResponseNoLock (const char *payload,
642 size_t payload_length,
643 StringExtractorGDBRemote &response)
644{
645 PacketResult packet_result = SendPacketNoLock (payload, payload_length);
646 if (packet_result == PacketResult::Success)
647 packet_result = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
648 return packet_result;
649}
650
651GDBRemoteCommunicationClient::PacketResult
Greg Clayton576d8832011-03-22 04:00:09 +0000652GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
653(
654 const char *payload,
655 size_t payload_length,
656 StringExtractorGDBRemote &response,
657 bool send_async
658)
659{
Greg Clayton3dedae12013-12-06 21:45:27 +0000660 PacketResult packet_result = PacketResult::ErrorSendFailed;
Greg Clayton576d8832011-03-22 04:00:09 +0000661 Mutex::Locker locker;
Greg Clayton5160ce52013-03-27 23:08:40 +0000662 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000663 if (GetSequenceMutex (locker))
Greg Clayton576d8832011-03-22 04:00:09 +0000664 {
Greg Clayton3dedae12013-12-06 21:45:27 +0000665 packet_result = SendPacketAndWaitForResponseNoLock (payload, payload_length, response);
Greg Clayton576d8832011-03-22 04:00:09 +0000666 }
667 else
668 {
669 if (send_async)
670 {
Greg Claytond3544052012-05-31 21:24:20 +0000671 if (IsRunning())
Greg Clayton576d8832011-03-22 04:00:09 +0000672 {
Greg Claytond3544052012-05-31 21:24:20 +0000673 Mutex::Locker async_locker (m_async_mutex);
674 m_async_packet.assign(payload, payload_length);
675 m_async_packet_predicate.SetValue (true, eBroadcastNever);
676
677 if (log)
678 log->Printf ("async: async packet = %s", m_async_packet.c_str());
679
680 bool timed_out = false;
681 if (SendInterrupt(locker, 2, timed_out))
Greg Clayton576d8832011-03-22 04:00:09 +0000682 {
Greg Claytond3544052012-05-31 21:24:20 +0000683 if (m_interrupt_sent)
Greg Clayton576d8832011-03-22 04:00:09 +0000684 {
Jim Inghambabfc382012-06-06 00:32:39 +0000685 m_interrupt_sent = false;
Greg Claytond3544052012-05-31 21:24:20 +0000686 TimeValue timeout_time;
687 timeout_time = TimeValue::Now();
688 timeout_time.OffsetWithSeconds (m_packet_timeout);
689
Greg Clayton576d8832011-03-22 04:00:09 +0000690 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000691 log->Printf ("async: sent interrupt");
Greg Clayton644247c2011-07-07 01:59:51 +0000692
Greg Claytond3544052012-05-31 21:24:20 +0000693 if (m_async_packet_predicate.WaitForValueEqualTo (false, &timeout_time, &timed_out))
Greg Claytone889ad62011-10-27 22:04:16 +0000694 {
Greg Claytond3544052012-05-31 21:24:20 +0000695 if (log)
696 log->Printf ("async: got response");
697
698 // Swap the response buffer to avoid malloc and string copy
699 response.GetStringRef().swap (m_async_response.GetStringRef());
Jim Inghama6195b72013-12-18 01:24:33 +0000700 packet_result = m_async_result;
Greg Claytond3544052012-05-31 21:24:20 +0000701 }
702 else
703 {
704 if (log)
705 log->Printf ("async: timed out waiting for response");
706 }
707
708 // Make sure we wait until the continue packet has been sent again...
709 if (m_private_is_running.WaitForValueEqualTo (true, &timeout_time, &timed_out))
710 {
711 if (log)
712 {
713 if (timed_out)
714 log->Printf ("async: timed out waiting for process to resume, but process was resumed");
715 else
716 log->Printf ("async: async packet sent");
717 }
718 }
719 else
720 {
721 if (log)
722 log->Printf ("async: timed out waiting for process to resume");
Greg Claytone889ad62011-10-27 22:04:16 +0000723 }
724 }
725 else
726 {
Greg Claytond3544052012-05-31 21:24:20 +0000727 // We had a racy condition where we went to send the interrupt
728 // yet we were able to get the lock, so the process must have
729 // just stopped?
Greg Clayton576d8832011-03-22 04:00:09 +0000730 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000731 log->Printf ("async: got lock without sending interrupt");
732 // Send the packet normally since we got the lock
Greg Clayton3dedae12013-12-06 21:45:27 +0000733 packet_result = SendPacketAndWaitForResponseNoLock (payload, payload_length, response);
Greg Clayton576d8832011-03-22 04:00:09 +0000734 }
735 }
736 else
737 {
Greg Clayton644247c2011-07-07 01:59:51 +0000738 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000739 log->Printf ("async: failed to interrupt");
Greg Clayton576d8832011-03-22 04:00:09 +0000740 }
741 }
742 else
743 {
744 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000745 log->Printf ("async: not running, async is ignored");
Greg Clayton576d8832011-03-22 04:00:09 +0000746 }
747 }
748 else
749 {
750 if (log)
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000751 log->Printf("error: failed to get packet sequence mutex, not sending packet '%*s'", (int) payload_length, payload);
Greg Clayton576d8832011-03-22 04:00:09 +0000752 }
753 }
Greg Clayton3dedae12013-12-06 21:45:27 +0000754 return packet_result;
Greg Clayton576d8832011-03-22 04:00:09 +0000755}
756
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000757static const char *end_delimiter = "--end--;";
758static const int end_delimiter_len = 8;
759
760std::string
761GDBRemoteCommunicationClient::HarmonizeThreadIdsForProfileData
762( ProcessGDBRemote *process,
763 StringExtractorGDBRemote& profileDataExtractor
764)
765{
766 std::map<uint64_t, uint32_t> new_thread_id_to_used_usec_map;
767 std::stringstream final_output;
768 std::string name, value;
769
770 // Going to assuming thread_used_usec comes first, else bail out.
771 while (profileDataExtractor.GetNameColonValue(name, value))
772 {
773 if (name.compare("thread_used_id") == 0)
774 {
775 StringExtractor threadIDHexExtractor(value.c_str());
776 uint64_t thread_id = threadIDHexExtractor.GetHexMaxU64(false, 0);
777
778 bool has_used_usec = false;
779 uint32_t curr_used_usec = 0;
780 std::string usec_name, usec_value;
781 uint32_t input_file_pos = profileDataExtractor.GetFilePos();
782 if (profileDataExtractor.GetNameColonValue(usec_name, usec_value))
783 {
784 if (usec_name.compare("thread_used_usec") == 0)
785 {
786 has_used_usec = true;
787 curr_used_usec = strtoull(usec_value.c_str(), NULL, 0);
788 }
789 else
790 {
791 // We didn't find what we want, it is probably
792 // an older version. Bail out.
793 profileDataExtractor.SetFilePos(input_file_pos);
794 }
795 }
796
797 if (has_used_usec)
798 {
799 uint32_t prev_used_usec = 0;
800 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_used_usec_map.find(thread_id);
801 if (iterator != m_thread_id_to_used_usec_map.end())
802 {
803 prev_used_usec = m_thread_id_to_used_usec_map[thread_id];
804 }
805
806 uint32_t real_used_usec = curr_used_usec - prev_used_usec;
807 // A good first time record is one that runs for at least 0.25 sec
808 bool good_first_time = (prev_used_usec == 0) && (real_used_usec > 250000);
809 bool good_subsequent_time = (prev_used_usec > 0) &&
810 ((real_used_usec > 0) || (process->HasAssignedIndexIDToThread(thread_id)));
811
812 if (good_first_time || good_subsequent_time)
813 {
814 // We try to avoid doing too many index id reservation,
815 // resulting in fast increase of index ids.
816
817 final_output << name << ":";
818 int32_t index_id = process->AssignIndexIDToThread(thread_id);
819 final_output << index_id << ";";
820
821 final_output << usec_name << ":" << usec_value << ";";
822 }
823 else
824 {
825 // Skip past 'thread_used_name'.
826 std::string local_name, local_value;
827 profileDataExtractor.GetNameColonValue(local_name, local_value);
828 }
829
830 // Store current time as previous time so that they can be compared later.
831 new_thread_id_to_used_usec_map[thread_id] = curr_used_usec;
832 }
833 else
834 {
835 // Bail out and use old string.
836 final_output << name << ":" << value << ";";
837 }
838 }
839 else
840 {
841 final_output << name << ":" << value << ";";
842 }
843 }
844 final_output << end_delimiter;
845 m_thread_id_to_used_usec_map = new_thread_id_to_used_usec_map;
846
847 return final_output.str();
848}
849
Greg Clayton576d8832011-03-22 04:00:09 +0000850StateType
851GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse
852(
853 ProcessGDBRemote *process,
854 const char *payload,
855 size_t packet_length,
856 StringExtractorGDBRemote &response
857)
858{
Greg Clayton1f5181a2012-07-02 22:05:25 +0000859 m_curr_tid = LLDB_INVALID_THREAD_ID;
Greg Clayton5160ce52013-03-27 23:08:40 +0000860 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton576d8832011-03-22 04:00:09 +0000861 if (log)
862 log->Printf ("GDBRemoteCommunicationClient::%s ()", __FUNCTION__);
863
864 Mutex::Locker locker(m_sequence_mutex);
865 StateType state = eStateRunning;
866
867 BroadcastEvent(eBroadcastBitRunPacketSent, NULL);
868 m_public_is_running.SetValue (true, eBroadcastNever);
869 // Set the starting continue packet into "continue_packet". This packet
Jim Inghambabfc382012-06-06 00:32:39 +0000870 // may change if we are interrupted and we continue after an async packet...
Greg Clayton576d8832011-03-22 04:00:09 +0000871 std::string continue_packet(payload, packet_length);
872
Greg Clayton3f875c52013-02-22 22:23:55 +0000873 bool got_async_packet = false;
Greg Claytonaf247d72011-05-19 03:54:16 +0000874
Greg Clayton576d8832011-03-22 04:00:09 +0000875 while (state == eStateRunning)
876 {
Greg Clayton3f875c52013-02-22 22:23:55 +0000877 if (!got_async_packet)
Greg Claytonaf247d72011-05-19 03:54:16 +0000878 {
879 if (log)
880 log->Printf ("GDBRemoteCommunicationClient::%s () sending continue packet: %s", __FUNCTION__, continue_packet.c_str());
Greg Clayton3dedae12013-12-06 21:45:27 +0000881 if (SendPacketNoLock(continue_packet.c_str(), continue_packet.size()) != PacketResult::Success)
Greg Claytonaf247d72011-05-19 03:54:16 +0000882 state = eStateInvalid;
Jim Inghamb8cd5752014-04-16 02:24:17 +0000883 else
884 m_interrupt_sent = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000885
Greg Claytone889ad62011-10-27 22:04:16 +0000886 m_private_is_running.SetValue (true, eBroadcastAlways);
Greg Claytonaf247d72011-05-19 03:54:16 +0000887 }
888
Greg Clayton3f875c52013-02-22 22:23:55 +0000889 got_async_packet = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000890
891 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000892 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(%s)", __FUNCTION__, continue_packet.c_str());
Greg Clayton576d8832011-03-22 04:00:09 +0000893
Greg Clayton3dedae12013-12-06 21:45:27 +0000894 if (WaitForPacketWithTimeoutMicroSecondsNoLock(response, UINT32_MAX) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000895 {
896 if (response.Empty())
897 state = eStateInvalid;
898 else
899 {
900 const char stop_type = response.GetChar();
901 if (log)
902 log->Printf ("GDBRemoteCommunicationClient::%s () got packet: %s", __FUNCTION__, response.GetStringRef().c_str());
903 switch (stop_type)
904 {
905 case 'T':
906 case 'S':
Greg Clayton576d8832011-03-22 04:00:09 +0000907 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000908 if (process->GetStopID() == 0)
Greg Clayton576d8832011-03-22 04:00:09 +0000909 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000910 if (process->GetID() == LLDB_INVALID_PROCESS_ID)
911 {
912 lldb::pid_t pid = GetCurrentProcessID ();
913 if (pid != LLDB_INVALID_PROCESS_ID)
914 process->SetID (pid);
915 }
916 process->BuildDynamicRegisterInfo (true);
Greg Clayton576d8832011-03-22 04:00:09 +0000917 }
Greg Clayton2687cd12012-03-29 01:55:41 +0000918
919 // Privately notify any internal threads that we have stopped
920 // in case we wanted to interrupt our process, yet we might
921 // send a packet and continue without returning control to the
922 // user.
923 m_private_is_running.SetValue (false, eBroadcastAlways);
924
925 const uint8_t signo = response.GetHexU8 (UINT8_MAX);
926
Jim Inghambabfc382012-06-06 00:32:39 +0000927 bool continue_after_async = m_async_signal != -1 || m_async_packet_predicate.GetValue();
928 if (continue_after_async || m_interrupt_sent)
Greg Clayton2687cd12012-03-29 01:55:41 +0000929 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000930 // We sent an interrupt packet to stop the inferior process
931 // for an async signal or to send an async packet while running
932 // but we might have been single stepping and received the
933 // stop packet for the step instead of for the interrupt packet.
934 // Typically when an interrupt is sent a SIGINT or SIGSTOP
935 // is used, so if we get anything else, we need to try and
936 // get another stop reply packet that may have been sent
937 // due to sending the interrupt when the target is stopped
938 // which will just re-send a copy of the last stop reply
939 // packet. If we don't do this, then the reply for our
940 // async packet will be the repeat stop reply packet and cause
941 // a lot of trouble for us!
942 if (signo != SIGINT && signo != SIGSTOP)
943 {
Greg Claytonfb72fde2012-05-15 02:50:49 +0000944 continue_after_async = false;
Greg Clayton2687cd12012-03-29 01:55:41 +0000945
946 // We didn't get a a SIGINT or SIGSTOP, so try for a
947 // very brief time (1 ms) to get another stop reply
948 // packet to make sure it doesn't get in the way
949 StringExtractorGDBRemote extra_stop_reply_packet;
950 uint32_t timeout_usec = 1000;
Greg Clayton3dedae12013-12-06 21:45:27 +0000951 if (WaitForPacketWithTimeoutMicroSecondsNoLock (extra_stop_reply_packet, timeout_usec) == PacketResult::Success)
Greg Clayton2687cd12012-03-29 01:55:41 +0000952 {
953 switch (extra_stop_reply_packet.GetChar())
954 {
955 case 'T':
956 case 'S':
957 // We did get an extra stop reply, which means
958 // our interrupt didn't stop the target so we
959 // shouldn't continue after the async signal
960 // or packet is sent...
Greg Claytonfb72fde2012-05-15 02:50:49 +0000961 continue_after_async = false;
Greg Clayton2687cd12012-03-29 01:55:41 +0000962 break;
963 }
964 }
965 }
966 }
967
968 if (m_async_signal != -1)
969 {
970 if (log)
971 log->Printf ("async: send signo = %s", Host::GetSignalAsCString (m_async_signal));
972
973 // Save off the async signal we are supposed to send
974 const int async_signal = m_async_signal;
975 // Clear the async signal member so we don't end up
976 // sending the signal multiple times...
977 m_async_signal = -1;
978 // Check which signal we stopped with
979 if (signo == async_signal)
980 {
981 if (log)
982 log->Printf ("async: stopped with signal %s, we are done running", Host::GetSignalAsCString (signo));
983
984 // We already stopped with a signal that we wanted
985 // to stop with, so we are done
986 }
987 else
988 {
989 // We stopped with a different signal that the one
990 // we wanted to stop with, so now we must resume
991 // with the signal we want
992 char signal_packet[32];
993 int signal_packet_len = 0;
994 signal_packet_len = ::snprintf (signal_packet,
995 sizeof (signal_packet),
996 "C%2.2x",
997 async_signal);
998
999 if (log)
1000 log->Printf ("async: stopped with signal %s, resume with %s",
1001 Host::GetSignalAsCString (signo),
1002 Host::GetSignalAsCString (async_signal));
1003
1004 // Set the continue packet to resume even if the
Greg Claytonfb72fde2012-05-15 02:50:49 +00001005 // interrupt didn't cause our stop (ignore continue_after_async)
Greg Clayton2687cd12012-03-29 01:55:41 +00001006 continue_packet.assign(signal_packet, signal_packet_len);
1007 continue;
1008 }
1009 }
1010 else if (m_async_packet_predicate.GetValue())
1011 {
Greg Clayton5160ce52013-03-27 23:08:40 +00001012 Log * packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Greg Clayton2687cd12012-03-29 01:55:41 +00001013
1014 // We are supposed to send an asynchronous packet while
Jim Inghama6195b72013-12-18 01:24:33 +00001015 // we are running.
Greg Clayton2687cd12012-03-29 01:55:41 +00001016 m_async_response.Clear();
1017 if (m_async_packet.empty())
1018 {
Jim Inghama6195b72013-12-18 01:24:33 +00001019 m_async_result = PacketResult::ErrorSendFailed;
1020 if (packet_log)
Greg Clayton2687cd12012-03-29 01:55:41 +00001021 packet_log->Printf ("async: error: empty async packet");
1022
1023 }
1024 else
1025 {
1026 if (packet_log)
1027 packet_log->Printf ("async: sending packet");
1028
Jim Inghama6195b72013-12-18 01:24:33 +00001029 m_async_result = SendPacketAndWaitForResponse (&m_async_packet[0],
1030 m_async_packet.size(),
1031 m_async_response,
1032 false);
Greg Clayton2687cd12012-03-29 01:55:41 +00001033 }
1034 // Let the other thread that was trying to send the async
1035 // packet know that the packet has been sent and response is
1036 // ready...
1037 m_async_packet_predicate.SetValue(false, eBroadcastAlways);
1038
1039 if (packet_log)
Greg Claytonfb72fde2012-05-15 02:50:49 +00001040 packet_log->Printf ("async: sent packet, continue_after_async = %i", continue_after_async);
Greg Clayton2687cd12012-03-29 01:55:41 +00001041
1042 // Set the continue packet to resume if our interrupt
1043 // for the async packet did cause the stop
Greg Claytonfb72fde2012-05-15 02:50:49 +00001044 if (continue_after_async)
Greg Clayton2687cd12012-03-29 01:55:41 +00001045 {
Greg Claytonf1186de2012-05-24 23:42:14 +00001046 // Reverting this for now as it is causing deadlocks
1047 // in programs (<rdar://problem/11529853>). In the future
1048 // we should check our thread list and "do the right thing"
1049 // for new threads that show up while we stop and run async
1050 // packets. Setting the packet to 'c' to continue all threads
1051 // is the right thing to do 99.99% of the time because if a
1052 // thread was single stepping, and we sent an interrupt, we
1053 // will notice above that we didn't stop due to an interrupt
1054 // but stopped due to stepping and we would _not_ continue.
1055 continue_packet.assign (1, 'c');
Greg Clayton2687cd12012-03-29 01:55:41 +00001056 continue;
1057 }
1058 }
1059 // Stop with signal and thread info
1060 state = eStateStopped;
Greg Clayton576d8832011-03-22 04:00:09 +00001061 }
Greg Clayton576d8832011-03-22 04:00:09 +00001062 break;
1063
1064 case 'W':
1065 case 'X':
1066 // process exited
1067 state = eStateExited;
1068 break;
1069
1070 case 'O':
1071 // STDOUT
1072 {
Greg Clayton3f875c52013-02-22 22:23:55 +00001073 got_async_packet = true;
Greg Clayton576d8832011-03-22 04:00:09 +00001074 std::string inferior_stdout;
1075 inferior_stdout.reserve(response.GetBytesLeft () / 2);
1076 char ch;
1077 while ((ch = response.GetHexU8()) != '\0')
1078 inferior_stdout.append(1, ch);
1079 process->AppendSTDOUT (inferior_stdout.c_str(), inferior_stdout.size());
1080 }
1081 break;
1082
Han Ming Ongab3b8b22012-11-17 00:21:04 +00001083 case 'A':
1084 // Async miscellaneous reply. Right now, only profile data is coming through this channel.
1085 {
Greg Clayton3f875c52013-02-22 22:23:55 +00001086 got_async_packet = true;
Han Ming Ong4b6459f2013-01-18 23:11:53 +00001087 std::string input = response.GetStringRef().substr(1); // '1' to move beyond 'A'
1088 if (m_partial_profile_data.length() > 0)
1089 {
1090 m_partial_profile_data.append(input);
1091 input = m_partial_profile_data;
1092 m_partial_profile_data.clear();
1093 }
1094
1095 size_t found, pos = 0, len = input.length();
1096 while ((found = input.find(end_delimiter, pos)) != std::string::npos)
1097 {
1098 StringExtractorGDBRemote profileDataExtractor(input.substr(pos, found).c_str());
Han Ming Ong91ed6b82013-06-24 18:15:05 +00001099 std::string profile_data = HarmonizeThreadIdsForProfileData(process, profileDataExtractor);
1100 process->BroadcastAsyncProfileData (profile_data);
Han Ming Ong4b6459f2013-01-18 23:11:53 +00001101
1102 pos = found + end_delimiter_len;
1103 }
1104
1105 if (pos < len)
1106 {
1107 // Last incomplete chunk.
1108 m_partial_profile_data = input.substr(pos);
1109 }
Han Ming Ongab3b8b22012-11-17 00:21:04 +00001110 }
1111 break;
1112
Greg Clayton576d8832011-03-22 04:00:09 +00001113 case 'E':
1114 // ERROR
1115 state = eStateInvalid;
1116 break;
1117
1118 default:
1119 if (log)
1120 log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__);
1121 state = eStateInvalid;
1122 break;
1123 }
1124 }
1125 }
1126 else
1127 {
1128 if (log)
1129 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__);
1130 state = eStateInvalid;
1131 }
1132 }
1133 if (log)
1134 log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state));
1135 response.SetFilePos(0);
1136 m_private_is_running.SetValue (false, eBroadcastAlways);
1137 m_public_is_running.SetValue (false, eBroadcastAlways);
1138 return state;
1139}
1140
1141bool
1142GDBRemoteCommunicationClient::SendAsyncSignal (int signo)
1143{
Greg Clayton2687cd12012-03-29 01:55:41 +00001144 Mutex::Locker async_locker (m_async_mutex);
Greg Clayton576d8832011-03-22 04:00:09 +00001145 m_async_signal = signo;
1146 bool timed_out = false;
Greg Clayton576d8832011-03-22 04:00:09 +00001147 Mutex::Locker locker;
Greg Clayton2687cd12012-03-29 01:55:41 +00001148 if (SendInterrupt (locker, 1, timed_out))
Greg Clayton576d8832011-03-22 04:00:09 +00001149 return true;
1150 m_async_signal = -1;
1151 return false;
1152}
1153
Greg Clayton37a0a242012-04-11 00:24:49 +00001154// This function takes a mutex locker as a parameter in case the GetSequenceMutex
Greg Clayton576d8832011-03-22 04:00:09 +00001155// actually succeeds. If it doesn't succeed in acquiring the sequence mutex
1156// (the expected result), then it will send the halt packet. If it does succeed
1157// then the caller that requested the interrupt will want to keep the sequence
1158// locked down so that no one else can send packets while the caller has control.
1159// This function usually gets called when we are running and need to stop the
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001160// target. It can also be used when we are running and we need to do something
Greg Clayton576d8832011-03-22 04:00:09 +00001161// else (like read/write memory), so we need to interrupt the running process
1162// (gdb remote protocol requires this), and do what we need to do, then resume.
1163
1164bool
Greg Clayton2687cd12012-03-29 01:55:41 +00001165GDBRemoteCommunicationClient::SendInterrupt
Greg Clayton576d8832011-03-22 04:00:09 +00001166(
1167 Mutex::Locker& locker,
1168 uint32_t seconds_to_wait_for_stop,
Greg Clayton576d8832011-03-22 04:00:09 +00001169 bool &timed_out
1170)
1171{
Greg Clayton576d8832011-03-22 04:00:09 +00001172 timed_out = false;
Greg Clayton5160ce52013-03-27 23:08:40 +00001173 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Clayton576d8832011-03-22 04:00:09 +00001174
1175 if (IsRunning())
1176 {
1177 // Only send an interrupt if our debugserver is running...
Greg Claytonc3c0b0e2012-04-12 19:04:34 +00001178 if (GetSequenceMutex (locker))
Greg Clayton37a0a242012-04-11 00:24:49 +00001179 {
1180 if (log)
1181 log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt");
1182 }
1183 else
Greg Clayton576d8832011-03-22 04:00:09 +00001184 {
1185 // Someone has the mutex locked waiting for a response or for the
1186 // inferior to stop, so send the interrupt on the down low...
1187 char ctrl_c = '\x03';
1188 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton576d8832011-03-22 04:00:09 +00001189 size_t bytes_written = Write (&ctrl_c, 1, status, NULL);
Greg Clayton2687cd12012-03-29 01:55:41 +00001190 if (log)
1191 log->PutCString("send packet: \\x03");
Greg Clayton576d8832011-03-22 04:00:09 +00001192 if (bytes_written > 0)
1193 {
Greg Clayton2687cd12012-03-29 01:55:41 +00001194 m_interrupt_sent = true;
Greg Clayton576d8832011-03-22 04:00:09 +00001195 if (seconds_to_wait_for_stop)
1196 {
Greg Clayton2687cd12012-03-29 01:55:41 +00001197 TimeValue timeout;
1198 if (seconds_to_wait_for_stop)
1199 {
1200 timeout = TimeValue::Now();
1201 timeout.OffsetWithSeconds (seconds_to_wait_for_stop);
1202 }
Greg Clayton576d8832011-03-22 04:00:09 +00001203 if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out))
1204 {
1205 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +00001206 log->PutCString ("SendInterrupt () - sent interrupt, private state stopped");
Greg Clayton576d8832011-03-22 04:00:09 +00001207 return true;
1208 }
1209 else
1210 {
1211 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +00001212 log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume");
Greg Clayton576d8832011-03-22 04:00:09 +00001213 }
1214 }
1215 else
1216 {
1217 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +00001218 log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop...");
Greg Clayton576d8832011-03-22 04:00:09 +00001219 return true;
1220 }
1221 }
1222 else
1223 {
1224 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +00001225 log->Printf ("SendInterrupt () - failed to write interrupt");
Greg Clayton576d8832011-03-22 04:00:09 +00001226 }
1227 return false;
1228 }
Greg Clayton576d8832011-03-22 04:00:09 +00001229 }
Greg Clayton2687cd12012-03-29 01:55:41 +00001230 else
1231 {
1232 if (log)
1233 log->Printf ("SendInterrupt () - not running");
1234 }
Greg Clayton576d8832011-03-22 04:00:09 +00001235 return true;
1236}
1237
1238lldb::pid_t
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001239GDBRemoteCommunicationClient::GetCurrentProcessID (bool allow_lazy)
Greg Clayton576d8832011-03-22 04:00:09 +00001240{
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001241 if (allow_lazy && m_curr_pid_is_valid == eLazyBoolYes)
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001242 return m_curr_pid;
1243
1244 // First try to retrieve the pid via the qProcessInfo request.
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001245 GetCurrentProcessInfo (allow_lazy);
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001246 if (m_curr_pid_is_valid == eLazyBoolYes)
Greg Clayton576d8832011-03-22 04:00:09 +00001247 {
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001248 // We really got it.
1249 return m_curr_pid;
Greg Clayton576d8832011-03-22 04:00:09 +00001250 }
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001251 else
1252 {
Todd Fialae24614f2014-05-14 00:15:32 +00001253 // If we don't get a response for qProcessInfo, check if $qC gives us a result.
1254 // $qC only returns a real process id on older debugserver and lldb-platform stubs.
1255 // The gdb remote protocol documents $qC as returning the thread id, which newer
1256 // debugserver and lldb-gdbserver stubs return correctly.
1257 StringExtractorGDBRemote response;
1258 if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false) == PacketResult::Success)
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001259 {
Todd Fialae24614f2014-05-14 00:15:32 +00001260 if (response.GetChar() == 'Q')
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001261 {
Todd Fialae24614f2014-05-14 00:15:32 +00001262 if (response.GetChar() == 'C')
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001263 {
Todd Fialae24614f2014-05-14 00:15:32 +00001264 m_curr_pid = response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID);
1265 if (m_curr_pid != LLDB_INVALID_PROCESS_ID)
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001266 {
Todd Fialae24614f2014-05-14 00:15:32 +00001267 m_curr_pid_is_valid = eLazyBoolYes;
1268 return m_curr_pid;
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001269 }
1270 }
1271 }
1272 }
1273 }
1274
Greg Clayton576d8832011-03-22 04:00:09 +00001275 return LLDB_INVALID_PROCESS_ID;
1276}
1277
1278bool
1279GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str)
1280{
1281 error_str.clear();
1282 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001283 if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001284 {
1285 if (response.IsOKResponse())
1286 return true;
1287 if (response.GetChar() == 'E')
1288 {
1289 // A string the describes what failed when launching...
1290 error_str = response.GetStringRef().substr(1);
1291 }
1292 else
1293 {
1294 error_str.assign ("unknown error occurred launching process");
1295 }
1296 }
1297 else
1298 {
Jim Ingham98d6da52012-06-28 20:30:23 +00001299 error_str.assign ("timed out waiting for app to launch");
Greg Clayton576d8832011-03-22 04:00:09 +00001300 }
1301 return false;
1302}
1303
1304int
Greg Claytonfbb76342013-11-20 21:07:01 +00001305GDBRemoteCommunicationClient::SendArgumentsPacket (const ProcessLaunchInfo &launch_info)
Greg Clayton576d8832011-03-22 04:00:09 +00001306{
Greg Claytonfbb76342013-11-20 21:07:01 +00001307 // Since we don't get the send argv0 separate from the executable path, we need to
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001308 // make sure to use the actual executable path found in the launch_info...
Greg Claytonfbb76342013-11-20 21:07:01 +00001309 std::vector<const char *> argv;
1310 FileSpec exe_file = launch_info.GetExecutableFile();
1311 std::string exe_path;
1312 const char *arg = NULL;
1313 const Args &launch_args = launch_info.GetArguments();
1314 if (exe_file)
Chaoren Linf34f4102015-05-09 01:21:32 +00001315 exe_path = exe_file.GetPath();
Greg Claytonfbb76342013-11-20 21:07:01 +00001316 else
1317 {
1318 arg = launch_args.GetArgumentAtIndex(0);
1319 if (arg)
1320 exe_path = arg;
1321 }
1322 if (!exe_path.empty())
1323 {
1324 argv.push_back(exe_path.c_str());
1325 for (uint32_t i=1; (arg = launch_args.GetArgumentAtIndex(i)) != NULL; ++i)
1326 {
1327 if (arg)
1328 argv.push_back(arg);
1329 }
1330 }
1331 if (!argv.empty())
Greg Clayton576d8832011-03-22 04:00:09 +00001332 {
1333 StreamString packet;
1334 packet.PutChar('A');
Greg Claytonfbb76342013-11-20 21:07:01 +00001335 for (size_t i = 0, n = argv.size(); i < n; ++i)
Greg Clayton576d8832011-03-22 04:00:09 +00001336 {
Greg Claytonfbb76342013-11-20 21:07:01 +00001337 arg = argv[i];
Greg Clayton576d8832011-03-22 04:00:09 +00001338 const int arg_len = strlen(arg);
1339 if (i > 0)
1340 packet.PutChar(',');
Greg Claytonfbb76342013-11-20 21:07:01 +00001341 packet.Printf("%i,%i,", arg_len * 2, (int)i);
Greg Clayton576d8832011-03-22 04:00:09 +00001342 packet.PutBytesAsRawHex8 (arg, arg_len);
1343 }
1344
1345 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001346 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001347 {
1348 if (response.IsOKResponse())
1349 return 0;
1350 uint8_t error = response.GetError();
1351 if (error)
1352 return error;
1353 }
1354 }
1355 return -1;
1356}
1357
1358int
1359GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value)
1360{
1361 if (name_equal_value && name_equal_value[0])
1362 {
1363 StreamString packet;
Greg Clayton89600582013-10-10 17:53:50 +00001364 bool send_hex_encoding = false;
1365 for (const char *p = name_equal_value; *p != '\0' && send_hex_encoding == false; ++p)
Greg Clayton576d8832011-03-22 04:00:09 +00001366 {
Greg Clayton89600582013-10-10 17:53:50 +00001367 if (isprint(*p))
1368 {
1369 switch (*p)
1370 {
1371 case '$':
1372 case '#':
1373 send_hex_encoding = true;
1374 break;
1375 default:
1376 break;
1377 }
1378 }
1379 else
1380 {
1381 // We have non printable characters, lets hex encode this...
1382 send_hex_encoding = true;
1383 }
1384 }
1385
1386 StringExtractorGDBRemote response;
1387 if (send_hex_encoding)
1388 {
1389 if (m_supports_QEnvironmentHexEncoded)
1390 {
1391 packet.PutCString("QEnvironmentHexEncoded:");
1392 packet.PutBytesAsRawHex8 (name_equal_value, strlen(name_equal_value));
Greg Clayton3dedae12013-12-06 21:45:27 +00001393 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton89600582013-10-10 17:53:50 +00001394 {
1395 if (response.IsOKResponse())
1396 return 0;
1397 uint8_t error = response.GetError();
1398 if (error)
1399 return error;
1400 if (response.IsUnsupportedResponse())
1401 m_supports_QEnvironmentHexEncoded = false;
1402 }
1403 }
1404
1405 }
1406 else if (m_supports_QEnvironment)
1407 {
1408 packet.Printf("QEnvironment:%s", name_equal_value);
Greg Clayton3dedae12013-12-06 21:45:27 +00001409 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton89600582013-10-10 17:53:50 +00001410 {
1411 if (response.IsOKResponse())
1412 return 0;
1413 uint8_t error = response.GetError();
1414 if (error)
1415 return error;
1416 if (response.IsUnsupportedResponse())
1417 m_supports_QEnvironment = false;
1418 }
Greg Clayton576d8832011-03-22 04:00:09 +00001419 }
1420 }
1421 return -1;
1422}
1423
Greg Claytonc4103b32011-05-08 04:53:50 +00001424int
1425GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch)
1426{
1427 if (arch && arch[0])
1428 {
1429 StreamString packet;
1430 packet.Printf("QLaunchArch:%s", arch);
1431 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001432 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Claytonc4103b32011-05-08 04:53:50 +00001433 {
1434 if (response.IsOKResponse())
1435 return 0;
1436 uint8_t error = response.GetError();
1437 if (error)
1438 return error;
1439 }
1440 }
1441 return -1;
1442}
1443
Jason Molendaa3329782014-03-29 18:54:20 +00001444int
1445GDBRemoteCommunicationClient::SendLaunchEventDataPacket (char const *data, bool *was_supported)
1446{
1447 if (data && *data != '\0')
1448 {
1449 StreamString packet;
1450 packet.Printf("QSetProcessEvent:%s", data);
1451 StringExtractorGDBRemote response;
1452 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
1453 {
1454 if (response.IsOKResponse())
1455 {
1456 if (was_supported)
1457 *was_supported = true;
1458 return 0;
1459 }
1460 else if (response.IsUnsupportedResponse())
1461 {
1462 if (was_supported)
1463 *was_supported = false;
1464 return -1;
1465 }
1466 else
1467 {
1468 uint8_t error = response.GetError();
1469 if (was_supported)
1470 *was_supported = true;
1471 if (error)
1472 return error;
1473 }
1474 }
1475 }
1476 return -1;
1477}
1478
Greg Clayton576d8832011-03-22 04:00:09 +00001479bool
Greg Clayton1cb64962011-03-24 04:28:38 +00001480GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major,
1481 uint32_t &minor,
1482 uint32_t &update)
1483{
1484 if (GetHostInfo ())
1485 {
1486 if (m_os_version_major != UINT32_MAX)
1487 {
1488 major = m_os_version_major;
1489 minor = m_os_version_minor;
1490 update = m_os_version_update;
1491 return true;
1492 }
1493 }
1494 return false;
1495}
1496
1497bool
1498GDBRemoteCommunicationClient::GetOSBuildString (std::string &s)
1499{
1500 if (GetHostInfo ())
1501 {
1502 if (!m_os_build.empty())
1503 {
1504 s = m_os_build;
1505 return true;
1506 }
1507 }
1508 s.clear();
1509 return false;
1510}
1511
1512
1513bool
1514GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s)
1515{
1516 if (GetHostInfo ())
1517 {
1518 if (!m_os_kernel.empty())
1519 {
1520 s = m_os_kernel;
1521 return true;
1522 }
1523 }
1524 s.clear();
1525 return false;
1526}
1527
1528bool
1529GDBRemoteCommunicationClient::GetHostname (std::string &s)
1530{
1531 if (GetHostInfo ())
1532 {
1533 if (!m_hostname.empty())
1534 {
1535 s = m_hostname;
1536 return true;
1537 }
1538 }
1539 s.clear();
1540 return false;
1541}
1542
1543ArchSpec
1544GDBRemoteCommunicationClient::GetSystemArchitecture ()
1545{
1546 if (GetHostInfo ())
1547 return m_host_arch;
1548 return ArchSpec();
1549}
1550
Jason Molendaf17b5ac2012-12-19 02:54:03 +00001551const lldb_private::ArchSpec &
1552GDBRemoteCommunicationClient::GetProcessArchitecture ()
1553{
1554 if (m_qProcessInfo_is_valid == eLazyBoolCalculate)
1555 GetCurrentProcessInfo ();
1556 return m_process_arch;
1557}
1558
Jason Molendaa3329782014-03-29 18:54:20 +00001559bool
1560GDBRemoteCommunicationClient::GetGDBServerVersion()
1561{
1562 if (m_qGDBServerVersion_is_valid == eLazyBoolCalculate)
1563 {
1564 m_gdb_server_name.clear();
1565 m_gdb_server_version = 0;
1566 m_qGDBServerVersion_is_valid = eLazyBoolNo;
1567
1568 StringExtractorGDBRemote response;
1569 if (SendPacketAndWaitForResponse ("qGDBServerVersion", response, false) == PacketResult::Success)
1570 {
1571 if (response.IsNormalResponse())
1572 {
1573 std::string name;
1574 std::string value;
1575 bool success = false;
1576 while (response.GetNameColonValue(name, value))
1577 {
1578 if (name.compare("name") == 0)
1579 {
1580 success = true;
1581 m_gdb_server_name.swap(value);
1582 }
1583 else if (name.compare("version") == 0)
1584 {
1585 size_t dot_pos = value.find('.');
1586 if (dot_pos != std::string::npos)
1587 value[dot_pos] = '\0';
Vince Harron5275aaa2015-01-15 20:08:35 +00001588 const uint32_t version = StringConvert::ToUInt32(value.c_str(), UINT32_MAX, 0);
Jason Molendaa3329782014-03-29 18:54:20 +00001589 if (version != UINT32_MAX)
1590 {
1591 success = true;
1592 m_gdb_server_version = version;
1593 }
1594 }
1595 }
1596 if (success)
1597 m_qGDBServerVersion_is_valid = eLazyBoolYes;
1598 }
1599 }
1600 }
1601 return m_qGDBServerVersion_is_valid == eLazyBoolYes;
1602}
1603
1604const char *
1605GDBRemoteCommunicationClient::GetGDBServerProgramName()
1606{
1607 if (GetGDBServerVersion())
1608 {
1609 if (!m_gdb_server_name.empty())
1610 return m_gdb_server_name.c_str();
1611 }
1612 return NULL;
1613}
1614
1615uint32_t
1616GDBRemoteCommunicationClient::GetGDBServerProgramVersion()
1617{
1618 if (GetGDBServerVersion())
1619 return m_gdb_server_version;
1620 return 0;
1621}
Greg Clayton1cb64962011-03-24 04:28:38 +00001622
1623bool
Ewan Crawford78baa192015-05-13 09:18:18 +00001624GDBRemoteCommunicationClient::GetDefaultThreadId (lldb::tid_t &tid)
1625{
1626 StringExtractorGDBRemote response;
1627 if (SendPacketAndWaitForResponse("qC",response,false) != PacketResult::Success)
1628 return false;
1629
1630 if (!response.IsNormalResponse())
1631 return false;
1632
1633 if (response.GetChar() == 'Q' && response.GetChar() == 'C')
1634 tid = response.GetHexMaxU32(true, -1);
1635
1636 return true;
1637}
1638
1639bool
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00001640GDBRemoteCommunicationClient::GetHostInfo (bool force)
Greg Clayton576d8832011-03-22 04:00:09 +00001641{
Todd Fialaaf245d12014-06-30 21:05:18 +00001642 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS));
1643
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00001644 if (force || m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton576d8832011-03-22 04:00:09 +00001645 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001646 m_qHostInfo_is_valid = eLazyBoolNo;
Greg Clayton576d8832011-03-22 04:00:09 +00001647 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001648 if (SendPacketAndWaitForResponse ("qHostInfo", response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001649 {
Greg Clayton17a0cb62011-05-15 23:46:54 +00001650 if (response.IsNormalResponse())
Greg Claytond314e812011-03-23 00:09:55 +00001651 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001652 std::string name;
1653 std::string value;
1654 uint32_t cpu = LLDB_INVALID_CPUTYPE;
1655 uint32_t sub = 0;
1656 std::string arch_name;
1657 std::string os_name;
1658 std::string vendor_name;
1659 std::string triple;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +00001660 std::string distribution_id;
Greg Clayton32e0a752011-03-30 18:16:51 +00001661 uint32_t pointer_byte_size = 0;
1662 StringExtractor extractor;
1663 ByteOrder byte_order = eByteOrderInvalid;
1664 uint32_t num_keys_decoded = 0;
1665 while (response.GetNameColonValue(name, value))
Greg Claytond314e812011-03-23 00:09:55 +00001666 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001667 if (name.compare("cputype") == 0)
Greg Clayton1cb64962011-03-24 04:28:38 +00001668 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001669 // exception type in big endian hex
Vince Harron5275aaa2015-01-15 20:08:35 +00001670 cpu = StringConvert::ToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0);
Greg Clayton32e0a752011-03-30 18:16:51 +00001671 if (cpu != LLDB_INVALID_CPUTYPE)
1672 ++num_keys_decoded;
1673 }
1674 else if (name.compare("cpusubtype") == 0)
1675 {
1676 // exception count in big endian hex
Vince Harron5275aaa2015-01-15 20:08:35 +00001677 sub = StringConvert::ToUInt32 (value.c_str(), 0, 0);
Greg Clayton32e0a752011-03-30 18:16:51 +00001678 if (sub != 0)
1679 ++num_keys_decoded;
1680 }
1681 else if (name.compare("arch") == 0)
1682 {
1683 arch_name.swap (value);
1684 ++num_keys_decoded;
1685 }
1686 else if (name.compare("triple") == 0)
1687 {
Greg Clayton44272a42014-09-18 00:18:32 +00001688 extractor.GetStringRef ().swap (value);
1689 extractor.SetFilePos(0);
1690 extractor.GetHexByteString (triple);
Greg Clayton32e0a752011-03-30 18:16:51 +00001691 ++num_keys_decoded;
1692 }
Todd Fialaa9ddb0e2014-01-18 03:02:39 +00001693 else if (name.compare ("distribution_id") == 0)
1694 {
1695 extractor.GetStringRef ().swap (value);
1696 extractor.SetFilePos (0);
1697 extractor.GetHexByteString (distribution_id);
1698 ++num_keys_decoded;
1699 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001700 else if (name.compare("os_build") == 0)
1701 {
1702 extractor.GetStringRef().swap(value);
1703 extractor.SetFilePos(0);
1704 extractor.GetHexByteString (m_os_build);
1705 ++num_keys_decoded;
1706 }
1707 else if (name.compare("hostname") == 0)
1708 {
1709 extractor.GetStringRef().swap(value);
1710 extractor.SetFilePos(0);
1711 extractor.GetHexByteString (m_hostname);
1712 ++num_keys_decoded;
1713 }
1714 else if (name.compare("os_kernel") == 0)
1715 {
1716 extractor.GetStringRef().swap(value);
1717 extractor.SetFilePos(0);
1718 extractor.GetHexByteString (m_os_kernel);
1719 ++num_keys_decoded;
1720 }
1721 else if (name.compare("ostype") == 0)
1722 {
1723 os_name.swap (value);
1724 ++num_keys_decoded;
1725 }
1726 else if (name.compare("vendor") == 0)
1727 {
1728 vendor_name.swap(value);
1729 ++num_keys_decoded;
1730 }
1731 else if (name.compare("endian") == 0)
1732 {
1733 ++num_keys_decoded;
1734 if (value.compare("little") == 0)
1735 byte_order = eByteOrderLittle;
1736 else if (value.compare("big") == 0)
1737 byte_order = eByteOrderBig;
1738 else if (value.compare("pdp") == 0)
1739 byte_order = eByteOrderPDP;
1740 else
1741 --num_keys_decoded;
1742 }
1743 else if (name.compare("ptrsize") == 0)
1744 {
Vince Harron5275aaa2015-01-15 20:08:35 +00001745 pointer_byte_size = StringConvert::ToUInt32 (value.c_str(), 0, 0);
Greg Clayton32e0a752011-03-30 18:16:51 +00001746 if (pointer_byte_size != 0)
1747 ++num_keys_decoded;
1748 }
1749 else if (name.compare("os_version") == 0)
1750 {
1751 Args::StringToVersion (value.c_str(),
1752 m_os_version_major,
1753 m_os_version_minor,
1754 m_os_version_update);
1755 if (m_os_version_major != UINT32_MAX)
1756 ++num_keys_decoded;
1757 }
Enrico Granataf04a2192012-07-13 23:18:48 +00001758 else if (name.compare("watchpoint_exceptions_received") == 0)
1759 {
1760 ++num_keys_decoded;
1761 if (strcmp(value.c_str(),"before") == 0)
1762 m_watchpoints_trigger_after_instruction = eLazyBoolNo;
1763 else if (strcmp(value.c_str(),"after") == 0)
1764 m_watchpoints_trigger_after_instruction = eLazyBoolYes;
1765 else
1766 --num_keys_decoded;
1767 }
Greg Clayton9ac6d2d2013-10-25 18:13:17 +00001768 else if (name.compare("default_packet_timeout") == 0)
1769 {
Vince Harron5275aaa2015-01-15 20:08:35 +00001770 m_default_packet_timeout = StringConvert::ToUInt32(value.c_str(), 0);
Greg Clayton9ac6d2d2013-10-25 18:13:17 +00001771 if (m_default_packet_timeout > 0)
1772 {
1773 SetPacketTimeout(m_default_packet_timeout);
1774 ++num_keys_decoded;
1775 }
1776 }
Enrico Granataf04a2192012-07-13 23:18:48 +00001777
Greg Clayton32e0a752011-03-30 18:16:51 +00001778 }
1779
1780 if (num_keys_decoded > 0)
1781 m_qHostInfo_is_valid = eLazyBoolYes;
1782
1783 if (triple.empty())
1784 {
1785 if (arch_name.empty())
1786 {
1787 if (cpu != LLDB_INVALID_CPUTYPE)
1788 {
1789 m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1790 if (pointer_byte_size)
1791 {
1792 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1793 }
1794 if (byte_order != eByteOrderInvalid)
1795 {
1796 assert (byte_order == m_host_arch.GetByteOrder());
1797 }
Greg Clayton70512312012-05-08 01:45:38 +00001798
1799 if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0)
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 os_name = "ios";
1807 break;
1808 default:
1809 os_name = "macosx";
1810 break;
1811 }
1812 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001813 if (!vendor_name.empty())
1814 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1815 if (!os_name.empty())
Greg Claytone1dadb82011-09-15 00:21:03 +00001816 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Greg Clayton32e0a752011-03-30 18:16:51 +00001817
1818 }
1819 }
1820 else
1821 {
1822 std::string triple;
1823 triple += arch_name;
Greg Clayton70512312012-05-08 01:45:38 +00001824 if (!vendor_name.empty() || !os_name.empty())
1825 {
1826 triple += '-';
1827 if (vendor_name.empty())
1828 triple += "unknown";
1829 else
1830 triple += vendor_name;
1831 triple += '-';
1832 if (os_name.empty())
1833 triple += "unknown";
1834 else
1835 triple += os_name;
1836 }
1837 m_host_arch.SetTriple (triple.c_str());
1838
1839 llvm::Triple &host_triple = m_host_arch.GetTriple();
1840 if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin)
1841 {
1842 switch (m_host_arch.GetMachine())
1843 {
Todd Fialad8eaa172014-07-23 14:37:35 +00001844 case llvm::Triple::aarch64:
Greg Clayton70512312012-05-08 01:45:38 +00001845 case llvm::Triple::arm:
1846 case llvm::Triple::thumb:
1847 host_triple.setOS(llvm::Triple::IOS);
1848 break;
1849 default:
1850 host_triple.setOS(llvm::Triple::MacOSX);
1851 break;
1852 }
1853 }
Greg Clayton1cb64962011-03-24 04:28:38 +00001854 if (pointer_byte_size)
1855 {
1856 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1857 }
1858 if (byte_order != eByteOrderInvalid)
1859 {
1860 assert (byte_order == m_host_arch.GetByteOrder());
1861 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001862
Greg Clayton1cb64962011-03-24 04:28:38 +00001863 }
1864 }
1865 else
1866 {
Greg Clayton70512312012-05-08 01:45:38 +00001867 m_host_arch.SetTriple (triple.c_str());
Greg Claytond314e812011-03-23 00:09:55 +00001868 if (pointer_byte_size)
1869 {
1870 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1871 }
1872 if (byte_order != eByteOrderInvalid)
1873 {
1874 assert (byte_order == m_host_arch.GetByteOrder());
1875 }
Todd Fialaaf245d12014-06-30 21:05:18 +00001876
1877 if (log)
1878 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 +00001879 }
1880 if (!distribution_id.empty ())
1881 m_host_arch.SetDistributionId (distribution_id.c_str ());
Greg Claytond314e812011-03-23 00:09:55 +00001882 }
Greg Clayton576d8832011-03-22 04:00:09 +00001883 }
1884 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001885 return m_qHostInfo_is_valid == eLazyBoolYes;
Greg Clayton576d8832011-03-22 04:00:09 +00001886}
1887
1888int
1889GDBRemoteCommunicationClient::SendAttach
1890(
1891 lldb::pid_t pid,
1892 StringExtractorGDBRemote& response
1893)
1894{
1895 if (pid != LLDB_INVALID_PROCESS_ID)
1896 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001897 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001898 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, pid);
Andy Gibbsa297a972013-06-19 19:04:53 +00001899 assert (packet_len < (int)sizeof(packet));
Greg Clayton3dedae12013-12-06 21:45:27 +00001900 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001901 {
1902 if (response.IsErrorResponse())
1903 return response.GetError();
1904 return 0;
1905 }
1906 }
1907 return -1;
1908}
1909
Vince Harrone0be4252015-02-06 18:32:57 +00001910int
1911GDBRemoteCommunicationClient::SendStdinNotification (const char* data, size_t data_len)
1912{
1913 StreamString packet;
1914 packet.PutCString("I");
1915 packet.PutBytesAsRawHex8(data, data_len);
1916 StringExtractorGDBRemote response;
1917 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
1918 {
1919 return 0;
1920 }
1921 return response.GetError();
1922
1923}
1924
Greg Clayton576d8832011-03-22 04:00:09 +00001925const lldb_private::ArchSpec &
1926GDBRemoteCommunicationClient::GetHostArchitecture ()
1927{
Greg Clayton32e0a752011-03-30 18:16:51 +00001928 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton576d8832011-03-22 04:00:09 +00001929 GetHostInfo ();
Greg Claytond314e812011-03-23 00:09:55 +00001930 return m_host_arch;
Greg Clayton576d8832011-03-22 04:00:09 +00001931}
1932
Greg Clayton9ac6d2d2013-10-25 18:13:17 +00001933uint32_t
1934GDBRemoteCommunicationClient::GetHostDefaultPacketTimeout ()
1935{
1936 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
1937 GetHostInfo ();
1938 return m_default_packet_timeout;
1939}
1940
Greg Clayton576d8832011-03-22 04:00:09 +00001941addr_t
1942GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions)
1943{
Greg Clayton70b57652011-05-15 01:25:55 +00001944 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton576d8832011-03-22 04:00:09 +00001945 {
Greg Clayton70b57652011-05-15 01:25:55 +00001946 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton2a48f522011-05-14 01:50:35 +00001947 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001948 const int packet_len = ::snprintf (packet, sizeof(packet), "_M%" PRIx64 ",%s%s%s",
Greg Clayton43e0af02012-09-18 18:04:04 +00001949 (uint64_t)size,
Greg Clayton2a48f522011-05-14 01:50:35 +00001950 permissions & lldb::ePermissionsReadable ? "r" : "",
1951 permissions & lldb::ePermissionsWritable ? "w" : "",
1952 permissions & lldb::ePermissionsExecutable ? "x" : "");
Andy Gibbsa297a972013-06-19 19:04:53 +00001953 assert (packet_len < (int)sizeof(packet));
Greg Clayton2a48f522011-05-14 01:50:35 +00001954 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001955 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton2a48f522011-05-14 01:50:35 +00001956 {
Todd Fialaf105f582014-06-21 00:48:09 +00001957 if (response.IsUnsupportedResponse())
1958 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1959 else if (!response.IsErrorResponse())
Greg Clayton2a48f522011-05-14 01:50:35 +00001960 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1961 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00001962 else
1963 {
1964 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1965 }
Greg Clayton576d8832011-03-22 04:00:09 +00001966 }
1967 return LLDB_INVALID_ADDRESS;
1968}
1969
1970bool
1971GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr)
1972{
Greg Clayton70b57652011-05-15 01:25:55 +00001973 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton576d8832011-03-22 04:00:09 +00001974 {
Greg Clayton70b57652011-05-15 01:25:55 +00001975 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton2a48f522011-05-14 01:50:35 +00001976 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001977 const int packet_len = ::snprintf(packet, sizeof(packet), "_m%" PRIx64, (uint64_t)addr);
Andy Gibbsa297a972013-06-19 19:04:53 +00001978 assert (packet_len < (int)sizeof(packet));
Greg Clayton2a48f522011-05-14 01:50:35 +00001979 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001980 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton2a48f522011-05-14 01:50:35 +00001981 {
Todd Fialaf105f582014-06-21 00:48:09 +00001982 if (response.IsUnsupportedResponse())
1983 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1984 else if (response.IsOKResponse())
Greg Clayton2a48f522011-05-14 01:50:35 +00001985 return true;
Greg Clayton17a0cb62011-05-15 23:46:54 +00001986 }
1987 else
1988 {
1989 m_supports_alloc_dealloc_memory = eLazyBoolNo;
Greg Clayton2a48f522011-05-14 01:50:35 +00001990 }
Greg Clayton576d8832011-03-22 04:00:09 +00001991 }
1992 return false;
1993}
1994
Jim Inghamacff8952013-05-02 00:27:30 +00001995Error
1996GDBRemoteCommunicationClient::Detach (bool keep_stopped)
Greg Clayton37a0a242012-04-11 00:24:49 +00001997{
Jim Inghamacff8952013-05-02 00:27:30 +00001998 Error error;
1999
2000 if (keep_stopped)
2001 {
2002 if (m_supports_detach_stay_stopped == eLazyBoolCalculate)
2003 {
2004 char packet[64];
2005 const int packet_len = ::snprintf(packet, sizeof(packet), "qSupportsDetachAndStayStopped:");
Andy Gibbsa297a972013-06-19 19:04:53 +00002006 assert (packet_len < (int)sizeof(packet));
Jim Inghamacff8952013-05-02 00:27:30 +00002007 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002008 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Jim Inghamacff8952013-05-02 00:27:30 +00002009 {
2010 m_supports_detach_stay_stopped = eLazyBoolYes;
2011 }
2012 else
2013 {
2014 m_supports_detach_stay_stopped = eLazyBoolNo;
2015 }
2016 }
2017
2018 if (m_supports_detach_stay_stopped == eLazyBoolNo)
2019 {
2020 error.SetErrorString("Stays stopped not supported by this target.");
2021 return error;
2022 }
2023 else
2024 {
Jim Ingham6c8824d2014-03-28 20:00:07 +00002025 StringExtractorGDBRemote response;
2026 PacketResult packet_result = SendPacketAndWaitForResponse ("D1", 1, response, false);
Greg Clayton3dedae12013-12-06 21:45:27 +00002027 if (packet_result != PacketResult::Success)
Jim Inghamacff8952013-05-02 00:27:30 +00002028 error.SetErrorString ("Sending extended disconnect packet failed.");
2029 }
2030 }
2031 else
2032 {
Jim Ingham6c8824d2014-03-28 20:00:07 +00002033 StringExtractorGDBRemote response;
2034 PacketResult packet_result = SendPacketAndWaitForResponse ("D", 1, response, false);
Greg Clayton3dedae12013-12-06 21:45:27 +00002035 if (packet_result != PacketResult::Success)
Jim Inghamacff8952013-05-02 00:27:30 +00002036 error.SetErrorString ("Sending disconnect packet failed.");
2037 }
2038 return error;
Greg Clayton37a0a242012-04-11 00:24:49 +00002039}
2040
Greg Clayton46fb5582011-11-18 07:03:08 +00002041Error
2042GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr,
2043 lldb_private::MemoryRegionInfo &region_info)
2044{
2045 Error error;
2046 region_info.Clear();
2047
2048 if (m_supports_memory_region_info != eLazyBoolNo)
2049 {
2050 m_supports_memory_region_info = eLazyBoolYes;
2051 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00002052 const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%" PRIx64, (uint64_t)addr);
Andy Gibbsa297a972013-06-19 19:04:53 +00002053 assert (packet_len < (int)sizeof(packet));
Greg Clayton46fb5582011-11-18 07:03:08 +00002054 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002055 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton46fb5582011-11-18 07:03:08 +00002056 {
2057 std::string name;
2058 std::string value;
2059 addr_t addr_value;
2060 bool success = true;
Jason Molendacb349ee2011-12-13 05:39:38 +00002061 bool saw_permissions = false;
Greg Clayton46fb5582011-11-18 07:03:08 +00002062 while (success && response.GetNameColonValue(name, value))
2063 {
2064 if (name.compare ("start") == 0)
2065 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002066 addr_value = StringConvert::ToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success);
Greg Clayton46fb5582011-11-18 07:03:08 +00002067 if (success)
2068 region_info.GetRange().SetRangeBase(addr_value);
2069 }
2070 else if (name.compare ("size") == 0)
2071 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002072 addr_value = StringConvert::ToUInt64(value.c_str(), 0, 16, &success);
Greg Clayton46fb5582011-11-18 07:03:08 +00002073 if (success)
2074 region_info.GetRange().SetByteSize (addr_value);
2075 }
Jason Molendacb349ee2011-12-13 05:39:38 +00002076 else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid())
Greg Clayton46fb5582011-11-18 07:03:08 +00002077 {
Jason Molendacb349ee2011-12-13 05:39:38 +00002078 saw_permissions = true;
2079 if (region_info.GetRange().Contains (addr))
2080 {
2081 if (value.find('r') != std::string::npos)
2082 region_info.SetReadable (MemoryRegionInfo::eYes);
2083 else
2084 region_info.SetReadable (MemoryRegionInfo::eNo);
2085
2086 if (value.find('w') != std::string::npos)
2087 region_info.SetWritable (MemoryRegionInfo::eYes);
2088 else
2089 region_info.SetWritable (MemoryRegionInfo::eNo);
2090
2091 if (value.find('x') != std::string::npos)
2092 region_info.SetExecutable (MemoryRegionInfo::eYes);
2093 else
2094 region_info.SetExecutable (MemoryRegionInfo::eNo);
2095 }
2096 else
2097 {
2098 // The reported region does not contain this address -- we're looking at an unmapped page
2099 region_info.SetReadable (MemoryRegionInfo::eNo);
2100 region_info.SetWritable (MemoryRegionInfo::eNo);
2101 region_info.SetExecutable (MemoryRegionInfo::eNo);
2102 }
Greg Clayton46fb5582011-11-18 07:03:08 +00002103 }
2104 else if (name.compare ("error") == 0)
2105 {
2106 StringExtractorGDBRemote name_extractor;
2107 // Swap "value" over into "name_extractor"
2108 name_extractor.GetStringRef().swap(value);
2109 // Now convert the HEX bytes into a string value
2110 name_extractor.GetHexByteString (value);
2111 error.SetErrorString(value.c_str());
2112 }
2113 }
Jason Molendacb349ee2011-12-13 05:39:38 +00002114
2115 // We got a valid address range back but no permissions -- which means this is an unmapped page
2116 if (region_info.GetRange().IsValid() && saw_permissions == false)
2117 {
2118 region_info.SetReadable (MemoryRegionInfo::eNo);
2119 region_info.SetWritable (MemoryRegionInfo::eNo);
2120 region_info.SetExecutable (MemoryRegionInfo::eNo);
2121 }
Greg Clayton46fb5582011-11-18 07:03:08 +00002122 }
2123 else
2124 {
2125 m_supports_memory_region_info = eLazyBoolNo;
2126 }
2127 }
2128
2129 if (m_supports_memory_region_info == eLazyBoolNo)
2130 {
2131 error.SetErrorString("qMemoryRegionInfo is not supported");
2132 }
2133 if (error.Fail())
2134 region_info.Clear();
2135 return error;
2136
2137}
2138
Johnny Chen64637202012-05-23 21:09:52 +00002139Error
2140GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num)
2141{
2142 Error error;
2143
2144 if (m_supports_watchpoint_support_info == eLazyBoolYes)
2145 {
2146 num = m_num_supported_hardware_watchpoints;
2147 return error;
2148 }
2149
2150 // Set num to 0 first.
2151 num = 0;
2152 if (m_supports_watchpoint_support_info != eLazyBoolNo)
2153 {
2154 char packet[64];
2155 const int packet_len = ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:");
Andy Gibbsa297a972013-06-19 19:04:53 +00002156 assert (packet_len < (int)sizeof(packet));
Johnny Chen64637202012-05-23 21:09:52 +00002157 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002158 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Johnny Chen64637202012-05-23 21:09:52 +00002159 {
2160 m_supports_watchpoint_support_info = eLazyBoolYes;
2161 std::string name;
2162 std::string value;
2163 while (response.GetNameColonValue(name, value))
2164 {
2165 if (name.compare ("num") == 0)
2166 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002167 num = StringConvert::ToUInt32(value.c_str(), 0, 0);
Johnny Chen64637202012-05-23 21:09:52 +00002168 m_num_supported_hardware_watchpoints = num;
2169 }
2170 }
2171 }
2172 else
2173 {
2174 m_supports_watchpoint_support_info = eLazyBoolNo;
2175 }
2176 }
2177
2178 if (m_supports_watchpoint_support_info == eLazyBoolNo)
2179 {
2180 error.SetErrorString("qWatchpointSupportInfo is not supported");
2181 }
2182 return error;
2183
2184}
Greg Clayton46fb5582011-11-18 07:03:08 +00002185
Enrico Granataf04a2192012-07-13 23:18:48 +00002186lldb_private::Error
2187GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num, bool& after)
2188{
2189 Error error(GetWatchpointSupportInfo(num));
2190 if (error.Success())
2191 error = GetWatchpointsTriggerAfterInstruction(after);
2192 return error;
2193}
2194
2195lldb_private::Error
2196GDBRemoteCommunicationClient::GetWatchpointsTriggerAfterInstruction (bool &after)
2197{
2198 Error error;
2199
2200 // we assume watchpoints will happen after running the relevant opcode
2201 // and we only want to override this behavior if we have explicitly
2202 // received a qHostInfo telling us otherwise
2203 if (m_qHostInfo_is_valid != eLazyBoolYes)
2204 after = true;
2205 else
2206 after = (m_watchpoints_trigger_after_instruction != eLazyBoolNo);
2207 return error;
2208}
2209
Greg Clayton576d8832011-03-22 04:00:09 +00002210int
2211GDBRemoteCommunicationClient::SetSTDIN (char const *path)
2212{
2213 if (path && path[0])
2214 {
2215 StreamString packet;
2216 packet.PutCString("QSetSTDIN:");
2217 packet.PutBytesAsRawHex8(path, strlen(path));
2218
2219 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002220 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002221 {
2222 if (response.IsOKResponse())
2223 return 0;
2224 uint8_t error = response.GetError();
2225 if (error)
2226 return error;
2227 }
2228 }
2229 return -1;
2230}
2231
2232int
2233GDBRemoteCommunicationClient::SetSTDOUT (char const *path)
2234{
2235 if (path && path[0])
2236 {
2237 StreamString packet;
2238 packet.PutCString("QSetSTDOUT:");
2239 packet.PutBytesAsRawHex8(path, strlen(path));
2240
2241 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002242 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002243 {
2244 if (response.IsOKResponse())
2245 return 0;
2246 uint8_t error = response.GetError();
2247 if (error)
2248 return error;
2249 }
2250 }
2251 return -1;
2252}
2253
2254int
2255GDBRemoteCommunicationClient::SetSTDERR (char const *path)
2256{
2257 if (path && path[0])
2258 {
2259 StreamString packet;
2260 packet.PutCString("QSetSTDERR:");
2261 packet.PutBytesAsRawHex8(path, strlen(path));
2262
2263 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002264 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002265 {
2266 if (response.IsOKResponse())
2267 return 0;
2268 uint8_t error = response.GetError();
2269 if (error)
2270 return error;
2271 }
2272 }
2273 return -1;
2274}
2275
Greg Claytonfbb76342013-11-20 21:07:01 +00002276bool
2277GDBRemoteCommunicationClient::GetWorkingDir (std::string &cwd)
2278{
2279 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002280 if (SendPacketAndWaitForResponse ("qGetWorkingDir", response, false) == PacketResult::Success)
Greg Claytonfbb76342013-11-20 21:07:01 +00002281 {
2282 if (response.IsUnsupportedResponse())
2283 return false;
2284 if (response.IsErrorResponse())
2285 return false;
2286 response.GetHexByteString (cwd);
2287 return !cwd.empty();
2288 }
2289 return false;
2290}
2291
Greg Clayton576d8832011-03-22 04:00:09 +00002292int
2293GDBRemoteCommunicationClient::SetWorkingDir (char const *path)
2294{
2295 if (path && path[0])
2296 {
2297 StreamString packet;
2298 packet.PutCString("QSetWorkingDir:");
2299 packet.PutBytesAsRawHex8(path, strlen(path));
2300
2301 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002302 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002303 {
2304 if (response.IsOKResponse())
2305 return 0;
2306 uint8_t error = response.GetError();
2307 if (error)
2308 return error;
2309 }
2310 }
2311 return -1;
2312}
2313
2314int
2315GDBRemoteCommunicationClient::SetDisableASLR (bool enable)
2316{
Greg Clayton32e0a752011-03-30 18:16:51 +00002317 char packet[32];
2318 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0);
Andy Gibbsa297a972013-06-19 19:04:53 +00002319 assert (packet_len < (int)sizeof(packet));
Greg Clayton576d8832011-03-22 04:00:09 +00002320 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002321 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002322 {
2323 if (response.IsOKResponse())
2324 return 0;
2325 uint8_t error = response.GetError();
2326 if (error)
2327 return error;
2328 }
2329 return -1;
2330}
Greg Clayton32e0a752011-03-30 18:16:51 +00002331
Jim Ingham106d0282014-06-25 02:32:56 +00002332int
2333GDBRemoteCommunicationClient::SetDetachOnError (bool enable)
2334{
2335 char packet[32];
2336 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDetachOnError:%i", enable ? 1 : 0);
2337 assert (packet_len < (int)sizeof(packet));
2338 StringExtractorGDBRemote response;
2339 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
2340 {
2341 if (response.IsOKResponse())
2342 return 0;
2343 uint8_t error = response.GetError();
2344 if (error)
2345 return error;
2346 }
2347 return -1;
2348}
2349
2350
Greg Clayton32e0a752011-03-30 18:16:51 +00002351bool
Greg Clayton8b82f082011-04-12 05:54:46 +00002352GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00002353{
2354 if (response.IsNormalResponse())
2355 {
2356 std::string name;
2357 std::string value;
2358 StringExtractor extractor;
Jason Molenda89c37492014-01-27 22:23:20 +00002359
2360 uint32_t cpu = LLDB_INVALID_CPUTYPE;
2361 uint32_t sub = 0;
2362 std::string vendor;
2363 std::string os_type;
Greg Clayton32e0a752011-03-30 18:16:51 +00002364
2365 while (response.GetNameColonValue(name, value))
2366 {
2367 if (name.compare("pid") == 0)
2368 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002369 process_info.SetProcessID (StringConvert::ToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002370 }
2371 else if (name.compare("ppid") == 0)
2372 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002373 process_info.SetParentProcessID (StringConvert::ToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002374 }
2375 else if (name.compare("uid") == 0)
2376 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002377 process_info.SetUserID (StringConvert::ToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002378 }
2379 else if (name.compare("euid") == 0)
2380 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002381 process_info.SetEffectiveUserID (StringConvert::ToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002382 }
2383 else if (name.compare("gid") == 0)
2384 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002385 process_info.SetGroupID (StringConvert::ToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002386 }
2387 else if (name.compare("egid") == 0)
2388 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002389 process_info.SetEffectiveGroupID (StringConvert::ToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002390 }
2391 else if (name.compare("triple") == 0)
2392 {
Greg Clayton44272a42014-09-18 00:18:32 +00002393 StringExtractor extractor;
2394 extractor.GetStringRef().swap(value);
2395 extractor.SetFilePos(0);
2396 extractor.GetHexByteString (value);
Greg Clayton70512312012-05-08 01:45:38 +00002397 process_info.GetArchitecture ().SetTriple (value.c_str());
Greg Clayton32e0a752011-03-30 18:16:51 +00002398 }
2399 else if (name.compare("name") == 0)
2400 {
2401 StringExtractor extractor;
Filipe Cabecinhasf86cf782012-05-07 09:30:51 +00002402 // The process name from ASCII hex bytes since we can't
Greg Clayton32e0a752011-03-30 18:16:51 +00002403 // control the characters in a process name
2404 extractor.GetStringRef().swap(value);
2405 extractor.SetFilePos(0);
2406 extractor.GetHexByteString (value);
Greg Clayton144f3a92011-11-15 03:53:30 +00002407 process_info.GetExecutableFile().SetFile (value.c_str(), false);
Greg Clayton32e0a752011-03-30 18:16:51 +00002408 }
Jason Molenda89c37492014-01-27 22:23:20 +00002409 else if (name.compare("cputype") == 0)
2410 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002411 cpu = StringConvert::ToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 16);
Jason Molenda89c37492014-01-27 22:23:20 +00002412 }
2413 else if (name.compare("cpusubtype") == 0)
2414 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002415 sub = StringConvert::ToUInt32 (value.c_str(), 0, 16);
Jason Molenda89c37492014-01-27 22:23:20 +00002416 }
2417 else if (name.compare("vendor") == 0)
2418 {
2419 vendor = value;
2420 }
2421 else if (name.compare("ostype") == 0)
2422 {
2423 os_type = value;
2424 }
2425 }
2426
2427 if (cpu != LLDB_INVALID_CPUTYPE && !vendor.empty() && !os_type.empty())
2428 {
2429 if (vendor == "apple")
2430 {
2431 process_info.GetArchitecture().SetArchitecture (eArchTypeMachO, cpu, sub);
2432 process_info.GetArchitecture().GetTriple().setVendorName (llvm::StringRef (vendor));
2433 process_info.GetArchitecture().GetTriple().setOSName (llvm::StringRef (os_type));
2434 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002435 }
2436
2437 if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
2438 return true;
2439 }
2440 return false;
2441}
2442
2443bool
Greg Clayton8b82f082011-04-12 05:54:46 +00002444GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00002445{
2446 process_info.Clear();
2447
2448 if (m_supports_qProcessInfoPID)
2449 {
2450 char packet[32];
Daniel Malead01b2952012-11-29 21:49:15 +00002451 const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%" PRIu64, pid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002452 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00002453 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002454 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002455 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002456 return DecodeProcessInfoResponse (response, process_info);
2457 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002458 else
2459 {
2460 m_supports_qProcessInfoPID = false;
2461 return false;
2462 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002463 }
2464 return false;
2465}
2466
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002467bool
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00002468GDBRemoteCommunicationClient::GetCurrentProcessInfo (bool allow_lazy)
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002469{
Todd Fiala3daa1762014-09-15 16:01:29 +00002470 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
2471
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00002472 if (allow_lazy)
2473 {
2474 if (m_qProcessInfo_is_valid == eLazyBoolYes)
2475 return true;
2476 if (m_qProcessInfo_is_valid == eLazyBoolNo)
2477 return false;
2478 }
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002479
2480 GetHostInfo ();
2481
2482 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002483 if (SendPacketAndWaitForResponse ("qProcessInfo", response, false) == PacketResult::Success)
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002484 {
2485 if (response.IsNormalResponse())
2486 {
2487 std::string name;
2488 std::string value;
2489 uint32_t cpu = LLDB_INVALID_CPUTYPE;
2490 uint32_t sub = 0;
2491 std::string arch_name;
2492 std::string os_name;
2493 std::string vendor_name;
2494 std::string triple;
2495 uint32_t pointer_byte_size = 0;
2496 StringExtractor extractor;
Todd Fiala5c9d5bf2014-09-15 15:31:11 +00002497 ByteOrder byte_order = eByteOrderInvalid;
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002498 uint32_t num_keys_decoded = 0;
Todd Fiala9f72b3a2014-05-07 19:28:21 +00002499 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002500 while (response.GetNameColonValue(name, value))
2501 {
2502 if (name.compare("cputype") == 0)
2503 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002504 cpu = StringConvert::ToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 16);
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002505 if (cpu != LLDB_INVALID_CPUTYPE)
2506 ++num_keys_decoded;
2507 }
2508 else if (name.compare("cpusubtype") == 0)
2509 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002510 sub = StringConvert::ToUInt32 (value.c_str(), 0, 16);
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002511 if (sub != 0)
2512 ++num_keys_decoded;
2513 }
Todd Fialac540dd02014-08-26 18:21:02 +00002514 else if (name.compare("triple") == 0)
2515 {
Greg Clayton44272a42014-09-18 00:18:32 +00002516 StringExtractor extractor;
2517 extractor.GetStringRef().swap(value);
2518 extractor.SetFilePos(0);
2519 extractor.GetHexByteString (triple);
Todd Fialac540dd02014-08-26 18:21:02 +00002520 ++num_keys_decoded;
2521 }
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002522 else if (name.compare("ostype") == 0)
2523 {
2524 os_name.swap (value);
2525 ++num_keys_decoded;
2526 }
2527 else if (name.compare("vendor") == 0)
2528 {
2529 vendor_name.swap(value);
2530 ++num_keys_decoded;
2531 }
2532 else if (name.compare("endian") == 0)
2533 {
Todd Fiala5c9d5bf2014-09-15 15:31:11 +00002534 ++num_keys_decoded;
2535 if (value.compare("little") == 0)
2536 byte_order = eByteOrderLittle;
2537 else if (value.compare("big") == 0)
2538 byte_order = eByteOrderBig;
2539 else if (value.compare("pdp") == 0)
2540 byte_order = eByteOrderPDP;
2541 else
2542 --num_keys_decoded;
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002543 }
2544 else if (name.compare("ptrsize") == 0)
2545 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002546 pointer_byte_size = StringConvert::ToUInt32 (value.c_str(), 0, 16);
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002547 if (pointer_byte_size != 0)
2548 ++num_keys_decoded;
2549 }
Todd Fiala9f72b3a2014-05-07 19:28:21 +00002550 else if (name.compare("pid") == 0)
2551 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002552 pid = StringConvert::ToUInt64(value.c_str(), 0, 16);
Todd Fiala9f72b3a2014-05-07 19:28:21 +00002553 if (pid != LLDB_INVALID_PROCESS_ID)
2554 ++num_keys_decoded;
2555 }
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002556 }
2557 if (num_keys_decoded > 0)
2558 m_qProcessInfo_is_valid = eLazyBoolYes;
Todd Fiala9f72b3a2014-05-07 19:28:21 +00002559 if (pid != LLDB_INVALID_PROCESS_ID)
2560 {
2561 m_curr_pid_is_valid = eLazyBoolYes;
2562 m_curr_pid = pid;
2563 }
Todd Fialac540dd02014-08-26 18:21:02 +00002564
2565 // Set the ArchSpec from the triple if we have it.
2566 if (!triple.empty ())
2567 {
2568 m_process_arch.SetTriple (triple.c_str ());
2569 if (pointer_byte_size)
2570 {
2571 assert (pointer_byte_size == m_process_arch.GetAddressByteSize());
2572 }
2573 }
2574 else if (cpu != LLDB_INVALID_CPUTYPE && !os_name.empty() && !vendor_name.empty())
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002575 {
Todd Fiala3daa1762014-09-15 16:01:29 +00002576 llvm::Triple triple(llvm::Twine("-") + vendor_name + "-" + os_name);
2577
2578 assert(triple.getObjectFormat() != llvm::Triple::UnknownObjectFormat);
2579 switch (triple.getObjectFormat()) {
2580 case llvm::Triple::MachO:
2581 m_process_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
2582 break;
2583 case llvm::Triple::ELF:
2584 m_process_arch.SetArchitecture (eArchTypeELF, cpu, sub);
2585 break;
2586 case llvm::Triple::COFF:
2587 m_process_arch.SetArchitecture (eArchTypeCOFF, cpu, sub);
2588 break;
2589 case llvm::Triple::UnknownObjectFormat:
2590 if (log)
2591 log->Printf("error: failed to determine target architecture");
2592 return false;
2593 }
2594
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002595 if (pointer_byte_size)
2596 {
2597 assert (pointer_byte_size == m_process_arch.GetAddressByteSize());
2598 }
Todd Fiala5c9d5bf2014-09-15 15:31:11 +00002599 if (byte_order != eByteOrderInvalid)
2600 {
2601 assert (byte_order == m_process_arch.GetByteOrder());
2602 }
Todd Fiala0cc371c2014-09-05 14:56:13 +00002603 m_process_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
Greg Clayton7ab7f892014-05-29 21:33:45 +00002604 m_process_arch.GetTriple().setOSName(llvm::StringRef (os_name));
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002605 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
2606 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002607 }
Greg Clayton7ab7f892014-05-29 21:33:45 +00002608 return true;
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002609 }
2610 }
2611 else
2612 {
2613 m_qProcessInfo_is_valid = eLazyBoolNo;
2614 }
2615
2616 return false;
2617}
2618
2619
Greg Clayton32e0a752011-03-30 18:16:51 +00002620uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +00002621GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info,
2622 ProcessInstanceInfoList &process_infos)
Greg Clayton32e0a752011-03-30 18:16:51 +00002623{
2624 process_infos.Clear();
2625
2626 if (m_supports_qfProcessInfo)
2627 {
2628 StreamString packet;
2629 packet.PutCString ("qfProcessInfo");
2630 if (!match_info.MatchAllProcesses())
2631 {
2632 packet.PutChar (':');
2633 const char *name = match_info.GetProcessInfo().GetName();
2634 bool has_name_match = false;
2635 if (name && name[0])
2636 {
2637 has_name_match = true;
2638 NameMatchType name_match_type = match_info.GetNameMatchType();
2639 switch (name_match_type)
2640 {
2641 case eNameMatchIgnore:
2642 has_name_match = false;
2643 break;
2644
2645 case eNameMatchEquals:
2646 packet.PutCString ("name_match:equals;");
2647 break;
2648
2649 case eNameMatchContains:
2650 packet.PutCString ("name_match:contains;");
2651 break;
2652
2653 case eNameMatchStartsWith:
2654 packet.PutCString ("name_match:starts_with;");
2655 break;
2656
2657 case eNameMatchEndsWith:
2658 packet.PutCString ("name_match:ends_with;");
2659 break;
2660
2661 case eNameMatchRegularExpression:
2662 packet.PutCString ("name_match:regex;");
2663 break;
2664 }
2665 if (has_name_match)
2666 {
2667 packet.PutCString ("name:");
2668 packet.PutBytesAsRawHex8(name, ::strlen(name));
2669 packet.PutChar (';');
2670 }
2671 }
2672
2673 if (match_info.GetProcessInfo().ProcessIDIsValid())
Daniel Malead01b2952012-11-29 21:49:15 +00002674 packet.Printf("pid:%" PRIu64 ";",match_info.GetProcessInfo().GetProcessID());
Greg Clayton32e0a752011-03-30 18:16:51 +00002675 if (match_info.GetProcessInfo().ParentProcessIDIsValid())
Daniel Malead01b2952012-11-29 21:49:15 +00002676 packet.Printf("parent_pid:%" PRIu64 ";",match_info.GetProcessInfo().GetParentProcessID());
Greg Clayton8b82f082011-04-12 05:54:46 +00002677 if (match_info.GetProcessInfo().UserIDIsValid())
2678 packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID());
2679 if (match_info.GetProcessInfo().GroupIDIsValid())
2680 packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID());
Greg Clayton32e0a752011-03-30 18:16:51 +00002681 if (match_info.GetProcessInfo().EffectiveUserIDIsValid())
2682 packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID());
2683 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
2684 packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID());
2685 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
2686 packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0);
2687 if (match_info.GetProcessInfo().GetArchitecture().IsValid())
2688 {
2689 const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture();
2690 const llvm::Triple &triple = match_arch.GetTriple();
2691 packet.PutCString("triple:");
Matthew Gardinerf39ebbe2014-08-01 05:12:23 +00002692 packet.PutCString(triple.getTriple().c_str());
Greg Clayton32e0a752011-03-30 18:16:51 +00002693 packet.PutChar (';');
2694 }
2695 }
2696 StringExtractorGDBRemote response;
Siva Chandra8fd94c92015-05-20 00:30:31 +00002697 // Increase timeout as the first qfProcessInfo packet takes a long time
2698 // on Android. The value of 1min was arrived at empirically.
2699 GDBRemoteCommunication::ScopedTimeout timeout (*this, 60);
Greg Clayton3dedae12013-12-06 21:45:27 +00002700 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002701 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002702 do
2703 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002704 ProcessInstanceInfo process_info;
Greg Clayton32e0a752011-03-30 18:16:51 +00002705 if (!DecodeProcessInfoResponse (response, process_info))
2706 break;
2707 process_infos.Append(process_info);
2708 response.GetStringRef().clear();
2709 response.SetFilePos(0);
Greg Clayton3dedae12013-12-06 21:45:27 +00002710 } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false) == PacketResult::Success);
Greg Clayton32e0a752011-03-30 18:16:51 +00002711 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002712 else
2713 {
2714 m_supports_qfProcessInfo = false;
2715 return 0;
2716 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002717 }
2718 return process_infos.GetSize();
2719
2720}
2721
2722bool
2723GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name)
2724{
2725 if (m_supports_qUserName)
2726 {
2727 char packet[32];
2728 const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002729 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00002730 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002731 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002732 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002733 if (response.IsNormalResponse())
2734 {
2735 // Make sure we parsed the right number of characters. The response is
2736 // the hex encoded user name and should make up the entire packet.
2737 // If there are any non-hex ASCII bytes, the length won't match below..
2738 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
2739 return true;
2740 }
2741 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002742 else
2743 {
2744 m_supports_qUserName = false;
2745 return false;
2746 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002747 }
2748 return false;
2749
2750}
2751
2752bool
2753GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name)
2754{
2755 if (m_supports_qGroupName)
2756 {
2757 char packet[32];
2758 const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002759 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00002760 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002761 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002762 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002763 if (response.IsNormalResponse())
2764 {
2765 // Make sure we parsed the right number of characters. The response is
2766 // the hex encoded group name and should make up the entire packet.
2767 // If there are any non-hex ASCII bytes, the length won't match below..
2768 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
2769 return true;
2770 }
2771 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002772 else
2773 {
2774 m_supports_qGroupName = false;
2775 return false;
2776 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002777 }
2778 return false;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002779}
Greg Clayton32e0a752011-03-30 18:16:51 +00002780
Ewan Crawford78baa192015-05-13 09:18:18 +00002781bool
2782GDBRemoteCommunicationClient::SetNonStopMode (const bool enable)
2783{
2784 // Form non-stop packet request
2785 char packet[32];
2786 const int packet_len = ::snprintf(packet, sizeof(packet), "QNonStop:%1d", (int)enable);
2787 assert(packet_len < (int)sizeof(packet));
2788
2789 StringExtractorGDBRemote response;
2790 // Send to target
2791 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
2792 if (response.IsOKResponse())
2793 return true;
2794
2795 // Failed or not supported
2796 return false;
2797
2798}
2799
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002800void
2801GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets)
2802{
2803 uint32_t i;
2804 TimeValue start_time, end_time;
2805 uint64_t total_time_nsec;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002806 if (SendSpeedTestPacket (0, 0))
2807 {
Greg Clayton700e5082014-02-21 19:11:28 +00002808 static uint32_t g_send_sizes[] = { 0, 64, 128, 512, 1024 };
2809 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 +00002810 const size_t k_num_send_sizes = llvm::array_lengthof(g_send_sizes);
2811 const size_t k_num_recv_sizes = llvm::array_lengthof(g_recv_sizes);
Greg Clayton700e5082014-02-21 19:11:28 +00002812 const uint64_t k_recv_amount = 4*1024*1024; // Receive 4MB
2813 for (uint32_t send_idx = 0; send_idx < k_num_send_sizes; ++send_idx)
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002814 {
Greg Clayton700e5082014-02-21 19:11:28 +00002815 const uint32_t send_size = g_send_sizes[send_idx];
2816 for (uint32_t recv_idx = 0; recv_idx < k_num_recv_sizes; ++recv_idx)
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002817 {
Greg Clayton700e5082014-02-21 19:11:28 +00002818 const uint32_t recv_size = g_recv_sizes[recv_idx];
2819 StreamString packet;
2820 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size);
2821 uint32_t bytes_left = send_size;
2822 while (bytes_left > 0)
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002823 {
Greg Clayton700e5082014-02-21 19:11:28 +00002824 if (bytes_left >= 26)
2825 {
2826 packet.PutCString("abcdefghijklmnopqrstuvwxyz");
2827 bytes_left -= 26;
2828 }
2829 else
2830 {
2831 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz");
2832 bytes_left = 0;
2833 }
2834 }
2835
2836 start_time = TimeValue::Now();
2837 if (recv_size == 0)
2838 {
2839 for (i=0; i<num_packets; ++i)
2840 {
2841 StringExtractorGDBRemote response;
2842 SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false);
2843 }
2844 }
2845 else
2846 {
2847 uint32_t bytes_read = 0;
2848 while (bytes_read < k_recv_amount)
2849 {
2850 StringExtractorGDBRemote response;
2851 SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false);
2852 bytes_read += recv_size;
2853 }
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002854 }
2855 end_time = TimeValue::Now();
2856 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002857 if (recv_size == 0)
Greg Clayton700e5082014-02-21 19:11:28 +00002858 {
2859 float packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
2860 printf ("%u qSpeedTest(send=%-7u, recv=%-7u) in %" PRIu64 ".%9.9" PRIu64 " sec for %f packets/sec.\n",
2861 num_packets,
2862 send_size,
2863 recv_size,
2864 total_time_nsec / TimeValue::NanoSecPerSec,
2865 total_time_nsec % TimeValue::NanoSecPerSec,
2866 packets_per_second);
2867 }
2868 else
2869 {
2870 float mb_second = ((((float)k_recv_amount)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec) / (1024.0*1024.0);
2871 printf ("%u qSpeedTest(send=%-7u, recv=%-7u) sent 4MB in %" PRIu64 ".%9.9" PRIu64 " sec for %f MB/sec.\n",
2872 num_packets,
2873 send_size,
2874 recv_size,
2875 total_time_nsec / TimeValue::NanoSecPerSec,
2876 total_time_nsec % TimeValue::NanoSecPerSec,
2877 mb_second);
2878 }
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002879 }
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002880 }
2881 }
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002882}
2883
2884bool
2885GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size)
2886{
2887 StreamString packet;
2888 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size);
2889 uint32_t bytes_left = send_size;
2890 while (bytes_left > 0)
2891 {
2892 if (bytes_left >= 26)
2893 {
2894 packet.PutCString("abcdefghijklmnopqrstuvwxyz");
2895 bytes_left -= 26;
2896 }
2897 else
2898 {
2899 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz");
2900 bytes_left = 0;
2901 }
2902 }
2903
2904 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002905 return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success;
Greg Clayton32e0a752011-03-30 18:16:51 +00002906}
Greg Clayton8b82f082011-04-12 05:54:46 +00002907
2908uint16_t
Greg Claytondbf04572013-12-04 19:40:33 +00002909GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort (lldb::pid_t &pid, const char *remote_accept_hostname)
Greg Clayton8b82f082011-04-12 05:54:46 +00002910{
Daniel Maleae0f8f572013-08-26 23:57:52 +00002911 pid = LLDB_INVALID_PROCESS_ID;
Greg Clayton8b82f082011-04-12 05:54:46 +00002912 StringExtractorGDBRemote response;
Daniel Maleae0f8f572013-08-26 23:57:52 +00002913 StreamString stream;
Greg Clayton29b8fc42013-11-21 01:44:58 +00002914 stream.PutCString("qLaunchGDBServer;");
Daniel Maleae0f8f572013-08-26 23:57:52 +00002915 std::string hostname;
Greg Claytondbf04572013-12-04 19:40:33 +00002916 if (remote_accept_hostname && remote_accept_hostname[0])
2917 hostname = remote_accept_hostname;
Daniel Maleae0f8f572013-08-26 23:57:52 +00002918 else
2919 {
Zachary Turner97a14e62014-08-19 17:18:29 +00002920 if (HostInfo::GetHostname(hostname))
Greg Claytondbf04572013-12-04 19:40:33 +00002921 {
2922 // Make the GDB server we launch only accept connections from this host
2923 stream.Printf("host:%s;", hostname.c_str());
2924 }
2925 else
2926 {
2927 // Make the GDB server we launch accept connections from any host since we can't figure out the hostname
2928 stream.Printf("host:*;");
2929 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00002930 }
2931 const char *packet = stream.GetData();
2932 int packet_len = stream.GetSize();
2933
Vince Harron1b5a74e2015-01-21 22:42:49 +00002934 // give the process a few seconds to startup
Tamas Berghammer912800c2015-02-24 10:23:39 +00002935 GDBRemoteCommunication::ScopedTimeout timeout (*this, 10);
2936
2937 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002938 {
2939 std::string name;
2940 std::string value;
2941 uint16_t port = 0;
Greg Clayton8b82f082011-04-12 05:54:46 +00002942 while (response.GetNameColonValue(name, value))
2943 {
Daniel Maleae0f8f572013-08-26 23:57:52 +00002944 if (name.compare("port") == 0)
Vince Harron5275aaa2015-01-15 20:08:35 +00002945 port = StringConvert::ToUInt32(value.c_str(), 0, 0);
Daniel Maleae0f8f572013-08-26 23:57:52 +00002946 else if (name.compare("pid") == 0)
Vince Harron5275aaa2015-01-15 20:08:35 +00002947 pid = StringConvert::ToUInt64(value.c_str(), LLDB_INVALID_PROCESS_ID, 0);
Greg Clayton8b82f082011-04-12 05:54:46 +00002948 }
2949 return port;
2950 }
2951 return 0;
2952}
2953
2954bool
Daniel Maleae0f8f572013-08-26 23:57:52 +00002955GDBRemoteCommunicationClient::KillSpawnedProcess (lldb::pid_t pid)
2956{
2957 StreamString stream;
2958 stream.Printf ("qKillSpawnedProcess:%" PRId64 , pid);
2959 const char *packet = stream.GetData();
2960 int packet_len = stream.GetSize();
Sylvestre Ledrufd654c42013-10-06 09:51:02 +00002961
Daniel Maleae0f8f572013-08-26 23:57:52 +00002962 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002963 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00002964 {
2965 if (response.IsOKResponse())
2966 return true;
2967 }
2968 return false;
2969}
2970
2971bool
Jason Molendae9ca4af2013-02-23 02:04:45 +00002972GDBRemoteCommunicationClient::SetCurrentThread (uint64_t tid)
Greg Clayton8b82f082011-04-12 05:54:46 +00002973{
2974 if (m_curr_tid == tid)
2975 return true;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002976
Greg Clayton8b82f082011-04-12 05:54:46 +00002977 char packet[32];
2978 int packet_len;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002979 if (tid == UINT64_MAX)
2980 packet_len = ::snprintf (packet, sizeof(packet), "Hg-1");
Greg Clayton8b82f082011-04-12 05:54:46 +00002981 else
Jason Molendae9ca4af2013-02-23 02:04:45 +00002982 packet_len = ::snprintf (packet, sizeof(packet), "Hg%" PRIx64, tid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002983 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002984 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002985 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002986 {
2987 if (response.IsOKResponse())
2988 {
2989 m_curr_tid = tid;
2990 return true;
2991 }
2992 }
2993 return false;
2994}
2995
2996bool
Jason Molendae9ca4af2013-02-23 02:04:45 +00002997GDBRemoteCommunicationClient::SetCurrentThreadForRun (uint64_t tid)
Greg Clayton8b82f082011-04-12 05:54:46 +00002998{
2999 if (m_curr_tid_run == tid)
3000 return true;
Jason Molendae9ca4af2013-02-23 02:04:45 +00003001
Greg Clayton8b82f082011-04-12 05:54:46 +00003002 char packet[32];
3003 int packet_len;
Jason Molendae9ca4af2013-02-23 02:04:45 +00003004 if (tid == UINT64_MAX)
3005 packet_len = ::snprintf (packet, sizeof(packet), "Hc-1");
Greg Clayton8b82f082011-04-12 05:54:46 +00003006 else
Jason Molendae9ca4af2013-02-23 02:04:45 +00003007 packet_len = ::snprintf (packet, sizeof(packet), "Hc%" PRIx64, tid);
3008
Andy Gibbsa297a972013-06-19 19:04:53 +00003009 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00003010 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003011 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00003012 {
3013 if (response.IsOKResponse())
3014 {
3015 m_curr_tid_run = tid;
3016 return true;
3017 }
3018 }
3019 return false;
3020}
3021
3022bool
3023GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response)
3024{
Greg Clayton3dedae12013-12-06 21:45:27 +00003025 if (SendPacketAndWaitForResponse("?", 1, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00003026 return response.IsNormalResponse();
3027 return false;
3028}
3029
3030bool
Greg Claytonf402f782012-10-13 02:11:55 +00003031GDBRemoteCommunicationClient::GetThreadStopInfo (lldb::tid_t tid, StringExtractorGDBRemote &response)
Greg Clayton8b82f082011-04-12 05:54:46 +00003032{
3033 if (m_supports_qThreadStopInfo)
3034 {
3035 char packet[256];
Daniel Malead01b2952012-11-29 21:49:15 +00003036 int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%" PRIx64, tid);
Andy Gibbsa297a972013-06-19 19:04:53 +00003037 assert (packet_len < (int)sizeof(packet));
Greg Clayton3dedae12013-12-06 21:45:27 +00003038 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00003039 {
Greg Claytonef8180a2013-10-15 00:14:28 +00003040 if (response.IsUnsupportedResponse())
3041 m_supports_qThreadStopInfo = false;
3042 else if (response.IsNormalResponse())
Greg Clayton8b82f082011-04-12 05:54:46 +00003043 return true;
3044 else
3045 return false;
3046 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00003047 else
3048 {
3049 m_supports_qThreadStopInfo = false;
3050 }
Greg Clayton8b82f082011-04-12 05:54:46 +00003051 }
Greg Clayton8b82f082011-04-12 05:54:46 +00003052 return false;
3053}
3054
3055
3056uint8_t
3057GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length)
3058{
Todd Fiala616b8272014-10-09 00:55:04 +00003059 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
3060 if (log)
3061 log->Printf ("GDBRemoteCommunicationClient::%s() %s at addr = 0x%" PRIx64,
3062 __FUNCTION__, insert ? "add" : "remove", addr);
3063
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003064 // Check if the stub is known not to support this breakpoint type
3065 if (!SupportsGDBStoppointPacket(type))
3066 return UINT8_MAX;
3067 // Construct the breakpoint packet
Greg Clayton8b82f082011-04-12 05:54:46 +00003068 char packet[64];
3069 const int packet_len = ::snprintf (packet,
3070 sizeof(packet),
Daniel Malead01b2952012-11-29 21:49:15 +00003071 "%c%i,%" PRIx64 ",%x",
Greg Clayton8b82f082011-04-12 05:54:46 +00003072 insert ? 'Z' : 'z',
3073 type,
3074 addr,
3075 length);
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003076 // Check we haven't overwritten the end of the packet buffer
Andy Gibbsa297a972013-06-19 19:04:53 +00003077 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00003078 StringExtractorGDBRemote response;
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003079 // Try to send the breakpoint packet, and check that it was correctly sent
Greg Clayton3dedae12013-12-06 21:45:27 +00003080 if (SendPacketAndWaitForResponse(packet, packet_len, response, true) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00003081 {
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003082 // Receive and OK packet when the breakpoint successfully placed
Greg Clayton8b82f082011-04-12 05:54:46 +00003083 if (response.IsOKResponse())
3084 return 0;
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003085
3086 // Error while setting breakpoint, send back specific error
3087 if (response.IsErrorResponse())
Greg Clayton8b82f082011-04-12 05:54:46 +00003088 return response.GetError();
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003089
3090 // Empty packet informs us that breakpoint is not supported
3091 if (response.IsUnsupportedResponse())
Greg Clayton17a0cb62011-05-15 23:46:54 +00003092 {
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003093 // Disable this breakpoint type since it is unsupported
3094 switch (type)
3095 {
Greg Clayton17a0cb62011-05-15 23:46:54 +00003096 case eBreakpointSoftware: m_supports_z0 = false; break;
3097 case eBreakpointHardware: m_supports_z1 = false; break;
3098 case eWatchpointWrite: m_supports_z2 = false; break;
3099 case eWatchpointRead: m_supports_z3 = false; break;
3100 case eWatchpointReadWrite: m_supports_z4 = false; break;
Chaoren Lin0be9ebb2015-02-03 01:51:50 +00003101 case eStoppointInvalid: return UINT8_MAX;
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003102 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00003103 }
3104 }
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003105 // Signal generic failure
Greg Clayton8b82f082011-04-12 05:54:46 +00003106 return UINT8_MAX;
3107}
Greg Claytonadc00cb2011-05-20 23:38:13 +00003108
3109size_t
3110GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
3111 bool &sequence_mutex_unavailable)
3112{
3113 Mutex::Locker locker;
3114 thread_ids.clear();
3115
Jim Ingham4ceb9282012-06-08 22:50:40 +00003116 if (GetSequenceMutex (locker, "ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex"))
Greg Claytonadc00cb2011-05-20 23:38:13 +00003117 {
3118 sequence_mutex_unavailable = false;
3119 StringExtractorGDBRemote response;
3120
Greg Clayton3dedae12013-12-06 21:45:27 +00003121 PacketResult packet_result;
3122 for (packet_result = SendPacketAndWaitForResponseNoLock ("qfThreadInfo", strlen("qfThreadInfo"), response);
3123 packet_result == PacketResult::Success && response.IsNormalResponse();
3124 packet_result = SendPacketAndWaitForResponseNoLock ("qsThreadInfo", strlen("qsThreadInfo"), response))
Greg Claytonadc00cb2011-05-20 23:38:13 +00003125 {
3126 char ch = response.GetChar();
3127 if (ch == 'l')
3128 break;
3129 if (ch == 'm')
3130 {
3131 do
3132 {
Jason Molendae9ca4af2013-02-23 02:04:45 +00003133 tid_t tid = response.GetHexMaxU64(false, LLDB_INVALID_THREAD_ID);
Greg Claytonadc00cb2011-05-20 23:38:13 +00003134
3135 if (tid != LLDB_INVALID_THREAD_ID)
3136 {
3137 thread_ids.push_back (tid);
3138 }
3139 ch = response.GetChar(); // Skip the command separator
3140 } while (ch == ','); // Make sure we got a comma separator
3141 }
3142 }
3143 }
3144 else
3145 {
Jim Ingham4ceb9282012-06-08 22:50:40 +00003146#if defined (LLDB_CONFIGURATION_DEBUG)
3147 // assert(!"ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex");
3148#else
Greg Clayton5160ce52013-03-27 23:08:40 +00003149 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Claytonc3c0b0e2012-04-12 19:04:34 +00003150 if (log)
3151 log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'");
Jim Ingham4ceb9282012-06-08 22:50:40 +00003152#endif
Greg Claytonadc00cb2011-05-20 23:38:13 +00003153 sequence_mutex_unavailable = true;
3154 }
3155 return thread_ids.size();
3156}
Greg Clayton37a0a242012-04-11 00:24:49 +00003157
3158lldb::addr_t
3159GDBRemoteCommunicationClient::GetShlibInfoAddr()
3160{
3161 if (!IsRunning())
3162 {
3163 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003164 if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false) == PacketResult::Success)
Greg Clayton37a0a242012-04-11 00:24:49 +00003165 {
3166 if (response.IsNormalResponse())
3167 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
3168 }
3169 }
3170 return LLDB_INVALID_ADDRESS;
3171}
3172
Daniel Maleae0f8f572013-08-26 23:57:52 +00003173lldb_private::Error
3174GDBRemoteCommunicationClient::RunShellCommand (const char *command, // Shouldn't be NULL
3175 const char *working_dir, // Pass NULL to use the current working directory
3176 int *status_ptr, // Pass NULL if you don't want the process exit status
3177 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
3178 std::string *command_output, // Pass NULL if you don't want the command output
3179 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish
3180{
3181 lldb_private::StreamString stream;
Greg Claytonfbb76342013-11-20 21:07:01 +00003182 stream.PutCString("qPlatform_shell:");
Daniel Maleae0f8f572013-08-26 23:57:52 +00003183 stream.PutBytesAsRawHex8(command, strlen(command));
3184 stream.PutChar(',');
3185 stream.PutHex32(timeout_sec);
3186 if (working_dir && *working_dir)
3187 {
3188 stream.PutChar(',');
3189 stream.PutBytesAsRawHex8(working_dir, strlen(working_dir));
3190 }
3191 const char *packet = stream.GetData();
3192 int packet_len = stream.GetSize();
3193 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003194 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003195 {
3196 if (response.GetChar() != 'F')
3197 return Error("malformed reply");
3198 if (response.GetChar() != ',')
3199 return Error("malformed reply");
3200 uint32_t exitcode = response.GetHexMaxU32(false, UINT32_MAX);
3201 if (exitcode == UINT32_MAX)
3202 return Error("unable to run remote process");
3203 else if (status_ptr)
3204 *status_ptr = exitcode;
3205 if (response.GetChar() != ',')
3206 return Error("malformed reply");
3207 uint32_t signo = response.GetHexMaxU32(false, UINT32_MAX);
3208 if (signo_ptr)
3209 *signo_ptr = signo;
3210 if (response.GetChar() != ',')
3211 return Error("malformed reply");
3212 std::string output;
3213 response.GetEscapedBinaryData(output);
3214 if (command_output)
3215 command_output->assign(output);
3216 return Error();
3217 }
3218 return Error("unable to send packet");
3219}
3220
Greg Claytonfbb76342013-11-20 21:07:01 +00003221Error
3222GDBRemoteCommunicationClient::MakeDirectory (const char *path,
3223 uint32_t file_permissions)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003224{
3225 lldb_private::StreamString stream;
Greg Claytonfbb76342013-11-20 21:07:01 +00003226 stream.PutCString("qPlatform_mkdir:");
3227 stream.PutHex32(file_permissions);
Daniel Maleae0f8f572013-08-26 23:57:52 +00003228 stream.PutChar(',');
Greg Claytonfbb76342013-11-20 21:07:01 +00003229 stream.PutBytesAsRawHex8(path, strlen(path));
Daniel Maleae0f8f572013-08-26 23:57:52 +00003230 const char *packet = stream.GetData();
3231 int packet_len = stream.GetSize();
3232 StringExtractorGDBRemote response;
Daniel Maleae0f8f572013-08-26 23:57:52 +00003233
Tamas Berghammer0f86b742015-02-23 11:03:08 +00003234 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) != PacketResult::Success)
3235 return Error("failed to send '%s' packet", packet);
3236
3237 if (response.GetChar() != 'F')
3238 return Error("invalid response to '%s' packet", packet);
3239
3240 return Error(response.GetU32(UINT32_MAX), eErrorTypePOSIX);
Daniel Maleae0f8f572013-08-26 23:57:52 +00003241}
3242
Greg Claytonfbb76342013-11-20 21:07:01 +00003243Error
3244GDBRemoteCommunicationClient::SetFilePermissions (const char *path,
3245 uint32_t file_permissions)
3246{
3247 lldb_private::StreamString stream;
3248 stream.PutCString("qPlatform_chmod:");
3249 stream.PutHex32(file_permissions);
3250 stream.PutChar(',');
3251 stream.PutBytesAsRawHex8(path, strlen(path));
3252 const char *packet = stream.GetData();
3253 int packet_len = stream.GetSize();
3254 StringExtractorGDBRemote response;
Tamas Berghammer0f86b742015-02-23 11:03:08 +00003255
3256 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) != PacketResult::Success)
3257 return Error("failed to send '%s' packet", packet);
3258
3259 if (response.GetChar() != 'F')
3260 return Error("invalid response to '%s' packet", packet);
3261
Chaoren Lince36c4c2015-05-05 18:43:19 +00003262 return Error(response.GetU32(UINT32_MAX), eErrorTypePOSIX);
Greg Claytonfbb76342013-11-20 21:07:01 +00003263}
3264
Daniel Maleae0f8f572013-08-26 23:57:52 +00003265static uint64_t
3266ParseHostIOPacketResponse (StringExtractorGDBRemote &response,
3267 uint64_t fail_result,
3268 Error &error)
3269{
3270 response.SetFilePos(0);
3271 if (response.GetChar() != 'F')
3272 return fail_result;
3273 int32_t result = response.GetS32 (-2);
3274 if (result == -2)
3275 return fail_result;
3276 if (response.GetChar() == ',')
3277 {
3278 int result_errno = response.GetS32 (-2);
3279 if (result_errno != -2)
3280 error.SetError(result_errno, eErrorTypePOSIX);
3281 else
3282 error.SetError(-1, eErrorTypeGeneric);
3283 }
3284 else
3285 error.Clear();
3286 return result;
3287}
3288lldb::user_id_t
3289GDBRemoteCommunicationClient::OpenFile (const lldb_private::FileSpec& file_spec,
3290 uint32_t flags,
3291 mode_t mode,
3292 Error &error)
3293{
3294 lldb_private::StreamString stream;
3295 stream.PutCString("vFile:open:");
Oleksiy Vyalov7d9d9412015-04-16 07:02:56 +00003296 std::string path (file_spec.GetPath(false));
Daniel Maleae0f8f572013-08-26 23:57:52 +00003297 if (path.empty())
3298 return UINT64_MAX;
3299 stream.PutCStringAsRawHex8(path.c_str());
3300 stream.PutChar(',');
Robert Flackebc56092015-03-18 13:55:48 +00003301 stream.PutHex32(flags);
Daniel Maleae0f8f572013-08-26 23:57:52 +00003302 stream.PutChar(',');
3303 stream.PutHex32(mode);
3304 const char* packet = stream.GetData();
3305 int packet_len = stream.GetSize();
3306 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003307 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003308 {
3309 return ParseHostIOPacketResponse (response, UINT64_MAX, error);
3310 }
3311 return UINT64_MAX;
3312}
3313
3314bool
3315GDBRemoteCommunicationClient::CloseFile (lldb::user_id_t fd,
3316 Error &error)
3317{
3318 lldb_private::StreamString stream;
3319 stream.Printf("vFile:close:%i", (int)fd);
3320 const char* packet = stream.GetData();
3321 int packet_len = stream.GetSize();
3322 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003323 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003324 {
3325 return ParseHostIOPacketResponse (response, -1, error) == 0;
3326 }
Deepak Panickald66b50c2013-10-22 12:27:43 +00003327 return false;
Daniel Maleae0f8f572013-08-26 23:57:52 +00003328}
3329
3330// Extension of host I/O packets to get the file size.
3331lldb::user_id_t
3332GDBRemoteCommunicationClient::GetFileSize (const lldb_private::FileSpec& file_spec)
3333{
3334 lldb_private::StreamString stream;
3335 stream.PutCString("vFile:size:");
3336 std::string path (file_spec.GetPath());
3337 stream.PutCStringAsRawHex8(path.c_str());
3338 const char* packet = stream.GetData();
3339 int packet_len = stream.GetSize();
3340 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003341 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003342 {
3343 if (response.GetChar() != 'F')
3344 return UINT64_MAX;
3345 uint32_t retcode = response.GetHexMaxU64(false, UINT64_MAX);
3346 return retcode;
3347 }
3348 return UINT64_MAX;
3349}
3350
Greg Claytonfbb76342013-11-20 21:07:01 +00003351Error
3352GDBRemoteCommunicationClient::GetFilePermissions(const char *path, uint32_t &file_permissions)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003353{
Greg Claytonfbb76342013-11-20 21:07:01 +00003354 Error error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00003355 lldb_private::StreamString stream;
3356 stream.PutCString("vFile:mode:");
Greg Claytonfbb76342013-11-20 21:07:01 +00003357 stream.PutCStringAsRawHex8(path);
Daniel Maleae0f8f572013-08-26 23:57:52 +00003358 const char* packet = stream.GetData();
3359 int packet_len = stream.GetSize();
3360 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003361 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003362 {
3363 if (response.GetChar() != 'F')
3364 {
3365 error.SetErrorStringWithFormat ("invalid response to '%s' packet", packet);
Daniel Maleae0f8f572013-08-26 23:57:52 +00003366 }
Greg Claytonfbb76342013-11-20 21:07:01 +00003367 else
Daniel Maleae0f8f572013-08-26 23:57:52 +00003368 {
Greg Claytonfbb76342013-11-20 21:07:01 +00003369 const uint32_t mode = response.GetS32(-1);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00003370 if (static_cast<int32_t>(mode) == -1)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003371 {
Greg Claytonfbb76342013-11-20 21:07:01 +00003372 if (response.GetChar() == ',')
3373 {
3374 int response_errno = response.GetS32(-1);
3375 if (response_errno > 0)
3376 error.SetError(response_errno, lldb::eErrorTypePOSIX);
3377 else
3378 error.SetErrorToGenericError();
3379 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00003380 else
3381 error.SetErrorToGenericError();
3382 }
Greg Claytonfbb76342013-11-20 21:07:01 +00003383 else
3384 {
3385 file_permissions = mode & (S_IRWXU|S_IRWXG|S_IRWXO);
3386 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00003387 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00003388 }
3389 else
3390 {
3391 error.SetErrorStringWithFormat ("failed to send '%s' packet", packet);
3392 }
Greg Claytonfbb76342013-11-20 21:07:01 +00003393 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00003394}
3395
3396uint64_t
3397GDBRemoteCommunicationClient::ReadFile (lldb::user_id_t fd,
3398 uint64_t offset,
3399 void *dst,
3400 uint64_t dst_len,
3401 Error &error)
3402{
3403 lldb_private::StreamString stream;
3404 stream.Printf("vFile:pread:%i,%" PRId64 ",%" PRId64, (int)fd, dst_len, offset);
3405 const char* packet = stream.GetData();
3406 int packet_len = stream.GetSize();
3407 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003408 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003409 {
3410 if (response.GetChar() != 'F')
3411 return 0;
3412 uint32_t retcode = response.GetHexMaxU32(false, UINT32_MAX);
3413 if (retcode == UINT32_MAX)
3414 return retcode;
3415 const char next = (response.Peek() ? *response.Peek() : 0);
3416 if (next == ',')
3417 return 0;
3418 if (next == ';')
3419 {
3420 response.GetChar(); // skip the semicolon
3421 std::string buffer;
3422 if (response.GetEscapedBinaryData(buffer))
3423 {
3424 const uint64_t data_to_write = std::min<uint64_t>(dst_len, buffer.size());
3425 if (data_to_write > 0)
3426 memcpy(dst, &buffer[0], data_to_write);
3427 return data_to_write;
3428 }
3429 }
3430 }
3431 return 0;
3432}
3433
3434uint64_t
3435GDBRemoteCommunicationClient::WriteFile (lldb::user_id_t fd,
3436 uint64_t offset,
3437 const void* src,
3438 uint64_t src_len,
3439 Error &error)
3440{
3441 lldb_private::StreamGDBRemote stream;
3442 stream.Printf("vFile:pwrite:%i,%" PRId64 ",", (int)fd, offset);
3443 stream.PutEscapedBytes(src, src_len);
3444 const char* packet = stream.GetData();
3445 int packet_len = stream.GetSize();
3446 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003447 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003448 {
3449 if (response.GetChar() != 'F')
3450 {
3451 error.SetErrorStringWithFormat("write file failed");
3452 return 0;
3453 }
3454 uint64_t bytes_written = response.GetU64(UINT64_MAX);
3455 if (bytes_written == UINT64_MAX)
3456 {
3457 error.SetErrorToGenericError();
3458 if (response.GetChar() == ',')
3459 {
3460 int response_errno = response.GetS32(-1);
3461 if (response_errno > 0)
3462 error.SetError(response_errno, lldb::eErrorTypePOSIX);
3463 }
3464 return 0;
3465 }
3466 return bytes_written;
3467 }
3468 else
3469 {
3470 error.SetErrorString ("failed to send vFile:pwrite packet");
3471 }
3472 return 0;
3473}
3474
Greg Claytonfbb76342013-11-20 21:07:01 +00003475Error
3476GDBRemoteCommunicationClient::CreateSymlink (const char *src, const char *dst)
3477{
3478 Error error;
3479 lldb_private::StreamGDBRemote stream;
3480 stream.PutCString("vFile:symlink:");
3481 // the unix symlink() command reverses its parameters where the dst if first,
3482 // so we follow suit here
3483 stream.PutCStringAsRawHex8(dst);
3484 stream.PutChar(',');
3485 stream.PutCStringAsRawHex8(src);
3486 const char* packet = stream.GetData();
3487 int packet_len = stream.GetSize();
3488 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003489 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Claytonfbb76342013-11-20 21:07:01 +00003490 {
3491 if (response.GetChar() == 'F')
3492 {
3493 uint32_t result = response.GetU32(UINT32_MAX);
3494 if (result != 0)
3495 {
3496 error.SetErrorToGenericError();
3497 if (response.GetChar() == ',')
3498 {
3499 int response_errno = response.GetS32(-1);
3500 if (response_errno > 0)
3501 error.SetError(response_errno, lldb::eErrorTypePOSIX);
3502 }
3503 }
3504 }
3505 else
3506 {
3507 // Should have returned with 'F<result>[,<errno>]'
3508 error.SetErrorStringWithFormat("symlink failed");
3509 }
3510 }
3511 else
3512 {
3513 error.SetErrorString ("failed to send vFile:symlink packet");
3514 }
3515 return error;
3516}
3517
3518Error
3519GDBRemoteCommunicationClient::Unlink (const char *path)
3520{
3521 Error error;
3522 lldb_private::StreamGDBRemote stream;
3523 stream.PutCString("vFile:unlink:");
3524 // the unix symlink() command reverses its parameters where the dst if first,
3525 // so we follow suit here
3526 stream.PutCStringAsRawHex8(path);
3527 const char* packet = stream.GetData();
3528 int packet_len = stream.GetSize();
3529 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003530 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Claytonfbb76342013-11-20 21:07:01 +00003531 {
3532 if (response.GetChar() == 'F')
3533 {
3534 uint32_t result = response.GetU32(UINT32_MAX);
3535 if (result != 0)
3536 {
3537 error.SetErrorToGenericError();
3538 if (response.GetChar() == ',')
3539 {
3540 int response_errno = response.GetS32(-1);
3541 if (response_errno > 0)
3542 error.SetError(response_errno, lldb::eErrorTypePOSIX);
3543 }
3544 }
3545 }
3546 else
3547 {
3548 // Should have returned with 'F<result>[,<errno>]'
3549 error.SetErrorStringWithFormat("unlink failed");
3550 }
3551 }
3552 else
3553 {
3554 error.SetErrorString ("failed to send vFile:unlink packet");
3555 }
3556 return error;
3557}
3558
Daniel Maleae0f8f572013-08-26 23:57:52 +00003559// Extension of host I/O packets to get whether a file exists.
3560bool
3561GDBRemoteCommunicationClient::GetFileExists (const lldb_private::FileSpec& file_spec)
3562{
3563 lldb_private::StreamString stream;
3564 stream.PutCString("vFile:exists:");
3565 std::string path (file_spec.GetPath());
3566 stream.PutCStringAsRawHex8(path.c_str());
3567 const char* packet = stream.GetData();
3568 int packet_len = stream.GetSize();
3569 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003570 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003571 {
3572 if (response.GetChar() != 'F')
3573 return false;
3574 if (response.GetChar() != ',')
3575 return false;
3576 bool retcode = (response.GetChar() != '0');
3577 return retcode;
3578 }
3579 return false;
3580}
3581
3582bool
3583GDBRemoteCommunicationClient::CalculateMD5 (const lldb_private::FileSpec& file_spec,
3584 uint64_t &high,
3585 uint64_t &low)
3586{
3587 lldb_private::StreamString stream;
3588 stream.PutCString("vFile:MD5:");
3589 std::string path (file_spec.GetPath());
3590 stream.PutCStringAsRawHex8(path.c_str());
3591 const char* packet = stream.GetData();
3592 int packet_len = stream.GetSize();
3593 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003594 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003595 {
3596 if (response.GetChar() != 'F')
3597 return false;
3598 if (response.GetChar() != ',')
3599 return false;
3600 if (response.Peek() && *response.Peek() == 'x')
3601 return false;
3602 low = response.GetHexMaxU64(false, UINT64_MAX);
3603 high = response.GetHexMaxU64(false, UINT64_MAX);
3604 return true;
3605 }
3606 return false;
3607}
Greg Claytonf74cf862013-11-13 23:28:31 +00003608
3609bool
Jason Molendaa3329782014-03-29 18:54:20 +00003610GDBRemoteCommunicationClient::AvoidGPackets (ProcessGDBRemote *process)
3611{
3612 // Some targets have issues with g/G packets and we need to avoid using them
3613 if (m_avoid_g_packets == eLazyBoolCalculate)
3614 {
3615 if (process)
3616 {
3617 m_avoid_g_packets = eLazyBoolNo;
3618 const ArchSpec &arch = process->GetTarget().GetArchitecture();
3619 if (arch.IsValid()
3620 && arch.GetTriple().getVendor() == llvm::Triple::Apple
3621 && arch.GetTriple().getOS() == llvm::Triple::IOS
Todd Fialad8eaa172014-07-23 14:37:35 +00003622 && arch.GetTriple().getArch() == llvm::Triple::aarch64)
Jason Molendaa3329782014-03-29 18:54:20 +00003623 {
3624 m_avoid_g_packets = eLazyBoolYes;
3625 uint32_t gdb_server_version = GetGDBServerProgramVersion();
3626 if (gdb_server_version != 0)
3627 {
3628 const char *gdb_server_name = GetGDBServerProgramName();
3629 if (gdb_server_name && strcmp(gdb_server_name, "debugserver") == 0)
3630 {
3631 if (gdb_server_version >= 310)
3632 m_avoid_g_packets = eLazyBoolNo;
3633 }
3634 }
3635 }
3636 }
3637 }
3638 return m_avoid_g_packets == eLazyBoolYes;
3639}
3640
3641bool
Greg Claytonf74cf862013-11-13 23:28:31 +00003642GDBRemoteCommunicationClient::ReadRegister(lldb::tid_t tid, uint32_t reg, StringExtractorGDBRemote &response)
3643{
3644 Mutex::Locker locker;
3645 if (GetSequenceMutex (locker, "Didn't get sequence mutex for p packet."))
3646 {
3647 const bool thread_suffix_supported = GetThreadSuffixSupported();
3648
3649 if (thread_suffix_supported || SetCurrentThread(tid))
3650 {
3651 char packet[64];
3652 int packet_len = 0;
3653 if (thread_suffix_supported)
3654 packet_len = ::snprintf (packet, sizeof(packet), "p%x;thread:%4.4" PRIx64 ";", reg, tid);
3655 else
3656 packet_len = ::snprintf (packet, sizeof(packet), "p%x", reg);
3657 assert (packet_len < ((int)sizeof(packet) - 1));
Greg Clayton3dedae12013-12-06 21:45:27 +00003658 return SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success;
Greg Claytonf74cf862013-11-13 23:28:31 +00003659 }
3660 }
3661 return false;
3662
3663}
3664
3665
3666bool
3667GDBRemoteCommunicationClient::ReadAllRegisters (lldb::tid_t tid, StringExtractorGDBRemote &response)
3668{
3669 Mutex::Locker locker;
3670 if (GetSequenceMutex (locker, "Didn't get sequence mutex for g packet."))
3671 {
3672 const bool thread_suffix_supported = GetThreadSuffixSupported();
3673
3674 if (thread_suffix_supported || SetCurrentThread(tid))
3675 {
3676 char packet[64];
3677 int packet_len = 0;
3678 // Get all registers in one packet
3679 if (thread_suffix_supported)
3680 packet_len = ::snprintf (packet, sizeof(packet), "g;thread:%4.4" PRIx64 ";", tid);
3681 else
3682 packet_len = ::snprintf (packet, sizeof(packet), "g");
3683 assert (packet_len < ((int)sizeof(packet) - 1));
Greg Clayton3dedae12013-12-06 21:45:27 +00003684 return SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success;
Greg Claytonf74cf862013-11-13 23:28:31 +00003685 }
3686 }
3687 return false;
3688}
3689bool
3690GDBRemoteCommunicationClient::SaveRegisterState (lldb::tid_t tid, uint32_t &save_id)
3691{
3692 save_id = 0; // Set to invalid save ID
3693 if (m_supports_QSaveRegisterState == eLazyBoolNo)
3694 return false;
3695
3696 m_supports_QSaveRegisterState = eLazyBoolYes;
3697 Mutex::Locker locker;
3698 if (GetSequenceMutex (locker, "Didn't get sequence mutex for QSaveRegisterState."))
3699 {
3700 const bool thread_suffix_supported = GetThreadSuffixSupported();
3701 if (thread_suffix_supported || SetCurrentThread(tid))
3702 {
3703 char packet[256];
3704 if (thread_suffix_supported)
3705 ::snprintf (packet, sizeof(packet), "QSaveRegisterState;thread:%4.4" PRIx64 ";", tid);
3706 else
Ilia K686b1fe2015-02-27 19:43:08 +00003707 ::snprintf(packet, sizeof(packet), "QSaveRegisterState");
Greg Claytonf74cf862013-11-13 23:28:31 +00003708
3709 StringExtractorGDBRemote response;
3710
Greg Clayton3dedae12013-12-06 21:45:27 +00003711 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
Greg Claytonf74cf862013-11-13 23:28:31 +00003712 {
3713 if (response.IsUnsupportedResponse())
3714 {
3715 // This packet isn't supported, don't try calling it again
3716 m_supports_QSaveRegisterState = eLazyBoolNo;
3717 }
3718
3719 const uint32_t response_save_id = response.GetU32(0);
3720 if (response_save_id != 0)
3721 {
3722 save_id = response_save_id;
3723 return true;
3724 }
3725 }
3726 }
3727 }
3728 return false;
3729}
3730
3731bool
3732GDBRemoteCommunicationClient::RestoreRegisterState (lldb::tid_t tid, uint32_t save_id)
3733{
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003734 // We use the "m_supports_QSaveRegisterState" variable here because the
Greg Claytonf74cf862013-11-13 23:28:31 +00003735 // QSaveRegisterState and QRestoreRegisterState packets must both be supported in
3736 // order to be useful
3737 if (m_supports_QSaveRegisterState == eLazyBoolNo)
3738 return false;
3739
3740 Mutex::Locker locker;
3741 if (GetSequenceMutex (locker, "Didn't get sequence mutex for QRestoreRegisterState."))
3742 {
3743 const bool thread_suffix_supported = GetThreadSuffixSupported();
3744 if (thread_suffix_supported || SetCurrentThread(tid))
3745 {
3746 char packet[256];
3747 if (thread_suffix_supported)
3748 ::snprintf (packet, sizeof(packet), "QRestoreRegisterState:%u;thread:%4.4" PRIx64 ";", save_id, tid);
3749 else
3750 ::snprintf (packet, sizeof(packet), "QRestoreRegisterState:%u" PRIx64 ";", save_id);
3751
3752 StringExtractorGDBRemote response;
3753
Greg Clayton3dedae12013-12-06 21:45:27 +00003754 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
Greg Claytonf74cf862013-11-13 23:28:31 +00003755 {
3756 if (response.IsOKResponse())
3757 {
3758 return true;
3759 }
3760 else if (response.IsUnsupportedResponse())
3761 {
3762 // This packet isn't supported, don't try calling this packet or
3763 // QSaveRegisterState again...
3764 m_supports_QSaveRegisterState = eLazyBoolNo;
3765 }
3766 }
3767 }
3768 }
3769 return false;
3770}
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00003771
3772bool
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003773GDBRemoteCommunicationClient::GetModuleInfo (const FileSpec& module_file_spec,
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00003774 const lldb_private::ArchSpec& arch_spec,
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003775 ModuleSpec &module_spec)
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00003776{
Oleksiy Vyalov7d9d9412015-04-16 07:02:56 +00003777 std::string module_path = module_file_spec.GetPath (false);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003778 if (module_path.empty ())
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00003779 return false;
3780
3781 StreamString packet;
3782 packet.PutCString("qModuleInfo:");
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003783 packet.PutCStringAsRawHex8(module_path.c_str());
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00003784 packet.PutCString(";");
Chaoren Linf34f4102015-05-09 01:21:32 +00003785 const auto& triple = arch_spec.GetTriple().getTriple();
3786 packet.PutBytesAsRawHex8(triple.c_str(), triple.size());
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00003787
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003788 StringExtractorGDBRemote response;
3789 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) != PacketResult::Success)
3790 return false;
3791
Aidan Doddsdf627e72015-05-05 08:31:55 +00003792 if (response.IsErrorResponse () || response.IsUnsupportedResponse ())
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003793 return false;
3794
3795 std::string name;
3796 std::string value;
3797 bool success;
3798 StringExtractor extractor;
3799
3800 module_spec.Clear ();
3801 module_spec.GetFileSpec () = module_file_spec;
3802
3803 while (response.GetNameColonValue (name, value))
3804 {
3805 if (name == "uuid" || name == "md5")
3806 {
3807 extractor.GetStringRef ().swap (value);
3808 extractor.SetFilePos (0);
3809 extractor.GetHexByteString (value);
3810 module_spec.GetUUID().SetFromCString (value.c_str(), value.size() / 2);
3811 }
3812 else if (name == "triple")
3813 {
3814 extractor.GetStringRef ().swap (value);
3815 extractor.SetFilePos (0);
3816 extractor.GetHexByteString (value);
3817 module_spec.GetArchitecture().SetTriple (value.c_str ());
3818 }
3819 else if (name == "file_offset")
3820 {
3821 const auto ival = StringConvert::ToUInt64 (value.c_str (), 0, 16, &success);
3822 if (success)
3823 module_spec.SetObjectOffset (ival);
3824 }
3825 else if (name == "file_size")
3826 {
3827 const auto ival = StringConvert::ToUInt64 (value.c_str (), 0, 16, &success);
3828 if (success)
3829 module_spec.SetObjectSize (ival);
3830 }
3831 else if (name == "file_path")
3832 {
3833 extractor.GetStringRef ().swap (value);
3834 extractor.SetFilePos (0);
3835 extractor.GetHexByteString (value);
Chaoren Linf34f4102015-05-09 01:21:32 +00003836 module_spec.GetFileSpec() = FileSpec(value.c_str(), false, arch_spec);
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003837 }
3838 }
3839
3840 return true;
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00003841}
Colin Rileyc3c95b22015-04-16 15:51:33 +00003842
3843// query the target remote for extended information using the qXfer packet
3844//
3845// example: object='features', annex='target.xml', out=<xml output>
3846// return: 'true' on success
3847// 'false' on failure (err set)
3848bool
3849GDBRemoteCommunicationClient::ReadExtFeature (const lldb_private::ConstString object,
3850 const lldb_private::ConstString annex,
3851 std::string & out,
3852 lldb_private::Error & err) {
3853
3854 std::stringstream output;
3855 StringExtractorGDBRemote chunk;
3856
3857 const int size = 0xfff;
3858 int offset = 0;
3859 bool active = true;
3860
3861 // loop until all data has been read
3862 while ( active ) {
3863
3864 // send query extended feature packet
3865 std::stringstream packet;
3866 packet << "qXfer:"
3867 << object.AsCString( ) << ":read:"
3868 << annex.AsCString( ) << ":"
3869 << std::hex << offset << ","
3870 << std::hex << size;
3871
3872 GDBRemoteCommunication::PacketResult res =
3873 SendPacketAndWaitForResponse( packet.str().c_str(),
3874 chunk,
3875 false );
3876
3877 if ( res != GDBRemoteCommunication::PacketResult::Success ) {
3878 err.SetErrorString( "Error sending $qXfer packet" );
3879 return false;
3880 }
3881
3882 const std::string & str = chunk.GetStringRef( );
3883 if ( str.length() == 0 ) {
3884 // should have some data in chunk
3885 err.SetErrorString( "Empty response from $qXfer packet" );
3886 return false;
3887 }
3888
3889 // check packet code
3890 switch ( str[0] ) {
3891 // last chunk
3892 case ( 'l' ):
3893 active = false;
Aidan Doddsed9f6122015-04-29 10:08:17 +00003894 // fall through intentional
Colin Rileyc3c95b22015-04-16 15:51:33 +00003895
3896 // more chunks
3897 case ( 'm' ) :
3898 if ( str.length() > 1 )
3899 output << &str[1];
Aidan Doddsed9f6122015-04-29 10:08:17 +00003900 offset += size;
Colin Rileyc3c95b22015-04-16 15:51:33 +00003901 break;
3902
3903 // unknown chunk
3904 default:
3905 err.SetErrorString( "Invalid continuation code from $qXfer packet" );
3906 return false;
3907 }
3908 }
3909
3910 out = output.str( );
3911 err.Success( );
3912 return true;
3913}