blob: aad69a06b9c29b71d5fe3b8cc90445aa332c6fed [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;
45
Todd Fiala0a70a842014-05-28 16:43:26 +000046#if defined(LLDB_DISABLE_POSIX) && !defined(SIGSTOP)
Virgile Bellob2f1fb22013-08-23 12:44:05 +000047#define SIGSTOP 17
48#endif
49
Greg Clayton576d8832011-03-22 04:00:09 +000050//----------------------------------------------------------------------
51// GDBRemoteCommunicationClient constructor
52//----------------------------------------------------------------------
Tamas Berghammere13c2732015-02-11 10:29:30 +000053GDBRemoteCommunicationClient::GDBRemoteCommunicationClient() :
54 GDBRemoteCommunication("gdb-remote.client", "gdb-remote.client.rx_packet"),
Greg Clayton576d8832011-03-22 04:00:09 +000055 m_supports_not_sending_acks (eLazyBoolCalculate),
56 m_supports_thread_suffix (eLazyBoolCalculate),
Greg Clayton44633992012-04-10 03:22:03 +000057 m_supports_threads_in_stop_reply (eLazyBoolCalculate),
Greg Clayton576d8832011-03-22 04:00:09 +000058 m_supports_vCont_all (eLazyBoolCalculate),
59 m_supports_vCont_any (eLazyBoolCalculate),
60 m_supports_vCont_c (eLazyBoolCalculate),
61 m_supports_vCont_C (eLazyBoolCalculate),
62 m_supports_vCont_s (eLazyBoolCalculate),
63 m_supports_vCont_S (eLazyBoolCalculate),
Greg Clayton32e0a752011-03-30 18:16:51 +000064 m_qHostInfo_is_valid (eLazyBoolCalculate),
Todd Fiala9f72b3a2014-05-07 19:28:21 +000065 m_curr_pid_is_valid (eLazyBoolCalculate),
Jason Molendaf17b5ac2012-12-19 02:54:03 +000066 m_qProcessInfo_is_valid (eLazyBoolCalculate),
Jason Molendaa3329782014-03-29 18:54:20 +000067 m_qGDBServerVersion_is_valid (eLazyBoolCalculate),
Greg Clayton70b57652011-05-15 01:25:55 +000068 m_supports_alloc_dealloc_memory (eLazyBoolCalculate),
Greg Clayton46fb5582011-11-18 07:03:08 +000069 m_supports_memory_region_info (eLazyBoolCalculate),
Johnny Chen64637202012-05-23 21:09:52 +000070 m_supports_watchpoint_support_info (eLazyBoolCalculate),
Jim Inghamacff8952013-05-02 00:27:30 +000071 m_supports_detach_stay_stopped (eLazyBoolCalculate),
Enrico Granataf04a2192012-07-13 23:18:48 +000072 m_watchpoints_trigger_after_instruction(eLazyBoolCalculate),
Jim Inghamcd16df92012-07-20 21:37:13 +000073 m_attach_or_wait_reply(eLazyBoolCalculate),
Jim Ingham279ceec2012-07-25 21:12:43 +000074 m_prepare_for_reg_writing_reply (eLazyBoolCalculate),
Eric Christopher2490f5c2013-08-30 17:50:57 +000075 m_supports_p (eLazyBoolCalculate),
Jason Molendabdc4f122014-05-06 02:59:39 +000076 m_supports_x (eLazyBoolCalculate),
Jason Molendaa3329782014-03-29 18:54:20 +000077 m_avoid_g_packets (eLazyBoolCalculate),
Greg Claytonf74cf862013-11-13 23:28:31 +000078 m_supports_QSaveRegisterState (eLazyBoolCalculate),
Steve Pucci03904ac2014-03-04 23:18:46 +000079 m_supports_qXfer_auxv_read (eLazyBoolCalculate),
Steve Pucci5ae54ae2014-01-25 05:46:51 +000080 m_supports_qXfer_libraries_read (eLazyBoolCalculate),
81 m_supports_qXfer_libraries_svr4_read (eLazyBoolCalculate),
82 m_supports_augmented_libraries_svr4_read (eLazyBoolCalculate),
Jason Molenda705b1802014-06-13 02:37:02 +000083 m_supports_jThreadExtendedInfo (eLazyBoolCalculate),
Greg Clayton32e0a752011-03-30 18:16:51 +000084 m_supports_qProcessInfoPID (true),
85 m_supports_qfProcessInfo (true),
86 m_supports_qUserName (true),
87 m_supports_qGroupName (true),
Greg Clayton8b82f082011-04-12 05:54:46 +000088 m_supports_qThreadStopInfo (true),
89 m_supports_z0 (true),
90 m_supports_z1 (true),
91 m_supports_z2 (true),
92 m_supports_z3 (true),
93 m_supports_z4 (true),
Greg Clayton89600582013-10-10 17:53:50 +000094 m_supports_QEnvironment (true),
95 m_supports_QEnvironmentHexEncoded (true),
Todd Fiala9f72b3a2014-05-07 19:28:21 +000096 m_curr_pid (LLDB_INVALID_PROCESS_ID),
Greg Clayton8b82f082011-04-12 05:54:46 +000097 m_curr_tid (LLDB_INVALID_THREAD_ID),
98 m_curr_tid_run (LLDB_INVALID_THREAD_ID),
Johnny Chen64637202012-05-23 21:09:52 +000099 m_num_supported_hardware_watchpoints (0),
Greg Clayton576d8832011-03-22 04:00:09 +0000100 m_async_mutex (Mutex::eMutexTypeRecursive),
101 m_async_packet_predicate (false),
102 m_async_packet (),
Jim Inghama6195b72013-12-18 01:24:33 +0000103 m_async_result (PacketResult::Success),
Greg Clayton576d8832011-03-22 04:00:09 +0000104 m_async_response (),
105 m_async_signal (-1),
Jim Inghamb8cd5752014-04-16 02:24:17 +0000106 m_interrupt_sent (false),
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000107 m_thread_id_to_used_usec_map (),
Greg Clayton1cb64962011-03-24 04:28:38 +0000108 m_host_arch(),
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000109 m_process_arch(),
Greg Clayton1cb64962011-03-24 04:28:38 +0000110 m_os_version_major (UINT32_MAX),
111 m_os_version_minor (UINT32_MAX),
Greg Clayton9ac6d2d2013-10-25 18:13:17 +0000112 m_os_version_update (UINT32_MAX),
113 m_os_build (),
114 m_os_kernel (),
115 m_hostname (),
Jason Molendaa3329782014-03-29 18:54:20 +0000116 m_gdb_server_name(),
117 m_gdb_server_version(UINT32_MAX),
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000118 m_default_packet_timeout (0),
119 m_max_packet_size (0)
Greg Clayton576d8832011-03-22 04:00:09 +0000120{
Greg Clayton576d8832011-03-22 04:00:09 +0000121}
122
123//----------------------------------------------------------------------
124// Destructor
125//----------------------------------------------------------------------
126GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient()
127{
Greg Clayton576d8832011-03-22 04:00:09 +0000128 if (IsConnected())
Greg Clayton576d8832011-03-22 04:00:09 +0000129 Disconnect();
Greg Clayton576d8832011-03-22 04:00:09 +0000130}
131
132bool
Greg Clayton1cb64962011-03-24 04:28:38 +0000133GDBRemoteCommunicationClient::HandshakeWithServer (Error *error_ptr)
134{
Greg Claytonfb909312013-11-23 01:58:15 +0000135 ResetDiscoverableSettings();
136
Greg Clayton1cb64962011-03-24 04:28:38 +0000137 // Start the read thread after we send the handshake ack since if we
138 // fail to send the handshake ack, there is no reason to continue...
139 if (SendAck())
Greg Claytonfb909312013-11-23 01:58:15 +0000140 {
Ed Maste48f986f2013-12-18 15:31:45 +0000141 // Wait for any responses that might have been queued up in the remote
142 // GDB server and flush them all
143 StringExtractorGDBRemote response;
144 PacketResult packet_result = PacketResult::Success;
145 const uint32_t timeout_usec = 10 * 1000; // Wait for 10 ms for a response
146 while (packet_result == PacketResult::Success)
147 packet_result = WaitForPacketWithTimeoutMicroSecondsNoLock (response, timeout_usec);
148
Greg Claytonfb909312013-11-23 01:58:15 +0000149 // The return value from QueryNoAckModeSupported() is true if the packet
150 // was sent and _any_ response (including UNIMPLEMENTED) was received),
151 // or false if no response was received. This quickly tells us if we have
152 // a live connection to a remote GDB server...
153 if (QueryNoAckModeSupported())
154 {
Greg Clayton700e5082014-02-21 19:11:28 +0000155#if 0
156 // Set above line to "#if 1" to test packet speed if remote GDB server
157 // supports the qSpeedTest packet...
158 TestPacketSpeed(10000);
159#endif
Greg Claytonfb909312013-11-23 01:58:15 +0000160 return true;
161 }
162 else
163 {
164 if (error_ptr)
165 error_ptr->SetErrorString("failed to get reply to handshake packet");
166 }
167 }
168 else
169 {
170 if (error_ptr)
171 error_ptr->SetErrorString("failed to send the handshake ack");
172 }
Greg Clayton1cb64962011-03-24 04:28:38 +0000173 return false;
174}
175
Greg Claytonfb909312013-11-23 01:58:15 +0000176bool
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000177GDBRemoteCommunicationClient::GetAugmentedLibrariesSVR4ReadSupported ()
178{
179 if (m_supports_augmented_libraries_svr4_read == eLazyBoolCalculate)
180 {
181 GetRemoteQSupported();
182 }
183 return (m_supports_augmented_libraries_svr4_read == eLazyBoolYes);
184}
185
186bool
187GDBRemoteCommunicationClient::GetQXferLibrariesSVR4ReadSupported ()
188{
189 if (m_supports_qXfer_libraries_svr4_read == eLazyBoolCalculate)
190 {
191 GetRemoteQSupported();
192 }
193 return (m_supports_qXfer_libraries_svr4_read == eLazyBoolYes);
194}
195
196bool
197GDBRemoteCommunicationClient::GetQXferLibrariesReadSupported ()
198{
199 if (m_supports_qXfer_libraries_read == eLazyBoolCalculate)
200 {
201 GetRemoteQSupported();
202 }
203 return (m_supports_qXfer_libraries_read == eLazyBoolYes);
204}
205
Steve Pucci03904ac2014-03-04 23:18:46 +0000206bool
207GDBRemoteCommunicationClient::GetQXferAuxvReadSupported ()
208{
209 if (m_supports_qXfer_auxv_read == eLazyBoolCalculate)
210 {
211 GetRemoteQSupported();
212 }
213 return (m_supports_qXfer_auxv_read == eLazyBoolYes);
214}
215
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000216uint64_t
217GDBRemoteCommunicationClient::GetRemoteMaxPacketSize()
218{
219 if (m_max_packet_size == 0)
220 {
221 GetRemoteQSupported();
222 }
223 return m_max_packet_size;
224}
225
226bool
Greg Clayton1cb64962011-03-24 04:28:38 +0000227GDBRemoteCommunicationClient::QueryNoAckModeSupported ()
Greg Clayton576d8832011-03-22 04:00:09 +0000228{
229 if (m_supports_not_sending_acks == eLazyBoolCalculate)
230 {
Greg Clayton1cb64962011-03-24 04:28:38 +0000231 m_send_acks = true;
Greg Clayton576d8832011-03-22 04:00:09 +0000232 m_supports_not_sending_acks = eLazyBoolNo;
Greg Clayton1cb64962011-03-24 04:28:38 +0000233
Jason Molenda36a216e2014-07-24 01:36:24 +0000234 // This is the first real packet that we'll send in a debug session and it may take a little
235 // longer than normal to receive a reply. Wait at least 6 seconds for a reply to this packet.
236
237 const uint32_t minimum_timeout = 6;
238 uint32_t old_timeout = GetPacketTimeoutInMicroSeconds() / lldb_private::TimeValue::MicroSecPerSec;
Tamas Berghammer912800c2015-02-24 10:23:39 +0000239 GDBRemoteCommunication::ScopedTimeout timeout (*this, std::max (old_timeout, minimum_timeout));
Jason Molenda36a216e2014-07-24 01:36:24 +0000240
Greg Clayton1cb64962011-03-24 04:28:38 +0000241 StringExtractorGDBRemote response;
Tamas Berghammer912800c2015-02-24 10:23:39 +0000242 if (SendPacketAndWaitForResponse("QStartNoAckMode", response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000243 {
244 if (response.IsOKResponse())
Greg Clayton1cb64962011-03-24 04:28:38 +0000245 {
246 m_send_acks = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000247 m_supports_not_sending_acks = eLazyBoolYes;
Greg Clayton1cb64962011-03-24 04:28:38 +0000248 }
Greg Claytonfb909312013-11-23 01:58:15 +0000249 return true;
Greg Clayton576d8832011-03-22 04:00:09 +0000250 }
251 }
Greg Claytonfb909312013-11-23 01:58:15 +0000252 return false;
Greg Clayton576d8832011-03-22 04:00:09 +0000253}
254
255void
Greg Clayton44633992012-04-10 03:22:03 +0000256GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported ()
257{
258 if (m_supports_threads_in_stop_reply == eLazyBoolCalculate)
259 {
260 m_supports_threads_in_stop_reply = eLazyBoolNo;
261
262 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000263 if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response, false) == PacketResult::Success)
Greg Clayton44633992012-04-10 03:22:03 +0000264 {
265 if (response.IsOKResponse())
266 m_supports_threads_in_stop_reply = eLazyBoolYes;
267 }
268 }
269}
270
Jim Inghamcd16df92012-07-20 21:37:13 +0000271bool
272GDBRemoteCommunicationClient::GetVAttachOrWaitSupported ()
273{
274 if (m_attach_or_wait_reply == eLazyBoolCalculate)
275 {
276 m_attach_or_wait_reply = eLazyBoolNo;
277
278 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000279 if (SendPacketAndWaitForResponse("qVAttachOrWaitSupported", response, false) == PacketResult::Success)
Jim Inghamcd16df92012-07-20 21:37:13 +0000280 {
281 if (response.IsOKResponse())
282 m_attach_or_wait_reply = eLazyBoolYes;
283 }
284 }
285 if (m_attach_or_wait_reply == eLazyBoolYes)
286 return true;
287 else
288 return false;
289}
290
Jim Ingham279ceec2012-07-25 21:12:43 +0000291bool
292GDBRemoteCommunicationClient::GetSyncThreadStateSupported ()
293{
294 if (m_prepare_for_reg_writing_reply == eLazyBoolCalculate)
295 {
296 m_prepare_for_reg_writing_reply = eLazyBoolNo;
297
298 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +0000299 if (SendPacketAndWaitForResponse("qSyncThreadStateSupported", response, false) == PacketResult::Success)
Jim Ingham279ceec2012-07-25 21:12:43 +0000300 {
301 if (response.IsOKResponse())
302 m_prepare_for_reg_writing_reply = eLazyBoolYes;
303 }
304 }
305 if (m_prepare_for_reg_writing_reply == eLazyBoolYes)
306 return true;
307 else
308 return false;
309}
310
Greg Clayton44633992012-04-10 03:22:03 +0000311
312void
Greg Clayton576d8832011-03-22 04:00:09 +0000313GDBRemoteCommunicationClient::ResetDiscoverableSettings()
314{
315 m_supports_not_sending_acks = eLazyBoolCalculate;
316 m_supports_thread_suffix = eLazyBoolCalculate;
Greg Clayton44633992012-04-10 03:22:03 +0000317 m_supports_threads_in_stop_reply = eLazyBoolCalculate;
Greg Clayton576d8832011-03-22 04:00:09 +0000318 m_supports_vCont_c = eLazyBoolCalculate;
319 m_supports_vCont_C = eLazyBoolCalculate;
320 m_supports_vCont_s = eLazyBoolCalculate;
321 m_supports_vCont_S = eLazyBoolCalculate;
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000322 m_supports_p = eLazyBoolCalculate;
Jason Molendabdc4f122014-05-06 02:59:39 +0000323 m_supports_x = eLazyBoolCalculate;
Greg Claytonf74cf862013-11-13 23:28:31 +0000324 m_supports_QSaveRegisterState = eLazyBoolCalculate;
Greg Clayton32e0a752011-03-30 18:16:51 +0000325 m_qHostInfo_is_valid = eLazyBoolCalculate;
Todd Fiala9f72b3a2014-05-07 19:28:21 +0000326 m_curr_pid_is_valid = eLazyBoolCalculate;
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000327 m_qProcessInfo_is_valid = eLazyBoolCalculate;
Jason Molendaa3329782014-03-29 18:54:20 +0000328 m_qGDBServerVersion_is_valid = eLazyBoolCalculate;
Greg Clayton70b57652011-05-15 01:25:55 +0000329 m_supports_alloc_dealloc_memory = eLazyBoolCalculate;
Greg Clayton46fb5582011-11-18 07:03:08 +0000330 m_supports_memory_region_info = eLazyBoolCalculate;
Jim Ingham279ceec2012-07-25 21:12:43 +0000331 m_prepare_for_reg_writing_reply = eLazyBoolCalculate;
332 m_attach_or_wait_reply = eLazyBoolCalculate;
Jason Molendaa3329782014-03-29 18:54:20 +0000333 m_avoid_g_packets = eLazyBoolCalculate;
Steve Pucci03904ac2014-03-04 23:18:46 +0000334 m_supports_qXfer_auxv_read = eLazyBoolCalculate;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000335 m_supports_qXfer_libraries_read = eLazyBoolCalculate;
336 m_supports_qXfer_libraries_svr4_read = eLazyBoolCalculate;
337 m_supports_augmented_libraries_svr4_read = eLazyBoolCalculate;
Greg Clayton2a48f522011-05-14 01:50:35 +0000338
Greg Clayton32e0a752011-03-30 18:16:51 +0000339 m_supports_qProcessInfoPID = true;
340 m_supports_qfProcessInfo = true;
341 m_supports_qUserName = true;
342 m_supports_qGroupName = true;
Greg Clayton8b82f082011-04-12 05:54:46 +0000343 m_supports_qThreadStopInfo = true;
344 m_supports_z0 = true;
345 m_supports_z1 = true;
346 m_supports_z2 = true;
347 m_supports_z3 = true;
348 m_supports_z4 = true;
Greg Clayton89600582013-10-10 17:53:50 +0000349 m_supports_QEnvironment = true;
350 m_supports_QEnvironmentHexEncoded = true;
Jason Molendaa3329782014-03-29 18:54:20 +0000351
Greg Claytond314e812011-03-23 00:09:55 +0000352 m_host_arch.Clear();
Jason Molendaf17b5ac2012-12-19 02:54:03 +0000353 m_process_arch.Clear();
Jason Molendaa3329782014-03-29 18:54:20 +0000354 m_os_version_major = UINT32_MAX;
355 m_os_version_minor = UINT32_MAX;
356 m_os_version_update = UINT32_MAX;
357 m_os_build.clear();
358 m_os_kernel.clear();
359 m_hostname.clear();
360 m_gdb_server_name.clear();
361 m_gdb_server_version = UINT32_MAX;
362 m_default_packet_timeout = 0;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000363
364 m_max_packet_size = 0;
Greg Clayton576d8832011-03-22 04:00:09 +0000365}
366
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000367void
368GDBRemoteCommunicationClient::GetRemoteQSupported ()
369{
370 // Clear out any capabilities we expect to see in the qSupported response
Steve Pucci03904ac2014-03-04 23:18:46 +0000371 m_supports_qXfer_auxv_read = eLazyBoolNo;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000372 m_supports_qXfer_libraries_read = eLazyBoolNo;
Steve Pucci03904ac2014-03-04 23:18:46 +0000373 m_supports_qXfer_libraries_svr4_read = eLazyBoolNo;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000374 m_supports_augmented_libraries_svr4_read = eLazyBoolNo;
375 m_max_packet_size = UINT64_MAX; // It's supposed to always be there, but if not, we assume no limit
376
377 StringExtractorGDBRemote response;
378 if (SendPacketAndWaitForResponse("qSupported",
379 response,
380 /*send_async=*/false) == PacketResult::Success)
381 {
382 const char *response_cstr = response.GetStringRef().c_str();
Steve Pucci03904ac2014-03-04 23:18:46 +0000383 if (::strstr (response_cstr, "qXfer:auxv:read+"))
384 m_supports_qXfer_auxv_read = eLazyBoolYes;
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000385 if (::strstr (response_cstr, "qXfer:libraries-svr4:read+"))
386 m_supports_qXfer_libraries_svr4_read = eLazyBoolYes;
387 if (::strstr (response_cstr, "augmented-libraries-svr4-read"))
388 {
389 m_supports_qXfer_libraries_svr4_read = eLazyBoolYes; // implied
390 m_supports_augmented_libraries_svr4_read = eLazyBoolYes;
391 }
392 if (::strstr (response_cstr, "qXfer:libraries:read+"))
393 m_supports_qXfer_libraries_read = eLazyBoolYes;
394
395 const char *packet_size_str = ::strstr (response_cstr, "PacketSize=");
396 if (packet_size_str)
397 {
398 StringExtractorGDBRemote packet_response(packet_size_str + strlen("PacketSize="));
399 m_max_packet_size = packet_response.GetHexMaxU64(/*little_endian=*/false, UINT64_MAX);
400 if (m_max_packet_size == 0)
401 {
402 m_max_packet_size = UINT64_MAX; // Must have been a garbled response
403 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
404 if (log)
405 log->Printf ("Garbled PacketSize spec in qSupported response");
406 }
407 }
408 }
409}
Greg Clayton576d8832011-03-22 04:00:09 +0000410
411bool
412GDBRemoteCommunicationClient::GetThreadSuffixSupported ()
413{
414 if (m_supports_thread_suffix == eLazyBoolCalculate)
415 {
416 StringExtractorGDBRemote response;
417 m_supports_thread_suffix = eLazyBoolNo;
Greg Clayton3dedae12013-12-06 21:45:27 +0000418 if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000419 {
420 if (response.IsOKResponse())
421 m_supports_thread_suffix = eLazyBoolYes;
422 }
423 }
424 return m_supports_thread_suffix;
425}
426bool
427GDBRemoteCommunicationClient::GetVContSupported (char flavor)
428{
429 if (m_supports_vCont_c == eLazyBoolCalculate)
430 {
431 StringExtractorGDBRemote response;
432 m_supports_vCont_any = eLazyBoolNo;
433 m_supports_vCont_all = eLazyBoolNo;
434 m_supports_vCont_c = eLazyBoolNo;
435 m_supports_vCont_C = eLazyBoolNo;
436 m_supports_vCont_s = eLazyBoolNo;
437 m_supports_vCont_S = eLazyBoolNo;
Greg Clayton3dedae12013-12-06 21:45:27 +0000438 if (SendPacketAndWaitForResponse("vCont?", response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000439 {
440 const char *response_cstr = response.GetStringRef().c_str();
441 if (::strstr (response_cstr, ";c"))
442 m_supports_vCont_c = eLazyBoolYes;
443
444 if (::strstr (response_cstr, ";C"))
445 m_supports_vCont_C = eLazyBoolYes;
446
447 if (::strstr (response_cstr, ";s"))
448 m_supports_vCont_s = eLazyBoolYes;
449
450 if (::strstr (response_cstr, ";S"))
451 m_supports_vCont_S = eLazyBoolYes;
452
453 if (m_supports_vCont_c == eLazyBoolYes &&
454 m_supports_vCont_C == eLazyBoolYes &&
455 m_supports_vCont_s == eLazyBoolYes &&
456 m_supports_vCont_S == eLazyBoolYes)
457 {
458 m_supports_vCont_all = eLazyBoolYes;
459 }
460
461 if (m_supports_vCont_c == eLazyBoolYes ||
462 m_supports_vCont_C == eLazyBoolYes ||
463 m_supports_vCont_s == eLazyBoolYes ||
464 m_supports_vCont_S == eLazyBoolYes)
465 {
466 m_supports_vCont_any = eLazyBoolYes;
467 }
468 }
469 }
470
471 switch (flavor)
472 {
473 case 'a': return m_supports_vCont_any;
474 case 'A': return m_supports_vCont_all;
475 case 'c': return m_supports_vCont_c;
476 case 'C': return m_supports_vCont_C;
477 case 's': return m_supports_vCont_s;
478 case 'S': return m_supports_vCont_S;
479 default: break;
480 }
481 return false;
482}
483
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000484// Check if the target supports 'p' packet. It sends out a 'p'
485// packet and checks the response. A normal packet will tell us
486// that support is available.
Sean Callananb1de1142013-09-04 23:24:15 +0000487//
488// Takes a valid thread ID because p needs to apply to a thread.
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000489bool
Sean Callananb1de1142013-09-04 23:24:15 +0000490GDBRemoteCommunicationClient::GetpPacketSupported (lldb::tid_t tid)
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000491{
492 if (m_supports_p == eLazyBoolCalculate)
493 {
494 StringExtractorGDBRemote response;
495 m_supports_p = eLazyBoolNo;
Sean Callananb1de1142013-09-04 23:24:15 +0000496 char packet[256];
497 if (GetThreadSuffixSupported())
498 snprintf(packet, sizeof(packet), "p0;thread:%" PRIx64 ";", tid);
499 else
500 snprintf(packet, sizeof(packet), "p0");
501
Greg Clayton3dedae12013-12-06 21:45:27 +0000502 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
Hafiz Abid Qadeer9a78cdf2013-08-29 09:09:45 +0000503 {
504 if (response.IsNormalResponse())
505 m_supports_p = eLazyBoolYes;
506 }
507 }
508 return m_supports_p;
509}
Greg Clayton576d8832011-03-22 04:00:09 +0000510
Jason Molendabdc4f122014-05-06 02:59:39 +0000511bool
Jason Molenda705b1802014-06-13 02:37:02 +0000512GDBRemoteCommunicationClient::GetThreadExtendedInfoSupported ()
513{
514 if (m_supports_jThreadExtendedInfo == eLazyBoolCalculate)
515 {
516 StringExtractorGDBRemote response;
517 m_supports_jThreadExtendedInfo = eLazyBoolNo;
518 if (SendPacketAndWaitForResponse("jThreadExtendedInfo:", response, false) == PacketResult::Success)
519 {
520 if (response.IsOKResponse())
521 {
522 m_supports_jThreadExtendedInfo = eLazyBoolYes;
523 }
524 }
525 }
526 return m_supports_jThreadExtendedInfo;
527}
528
529bool
Jason Molendabdc4f122014-05-06 02:59:39 +0000530GDBRemoteCommunicationClient::GetxPacketSupported ()
531{
532 if (m_supports_x == eLazyBoolCalculate)
533 {
534 StringExtractorGDBRemote response;
535 m_supports_x = eLazyBoolNo;
536 char packet[256];
537 snprintf (packet, sizeof (packet), "x0,0");
538 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
539 {
540 if (response.IsOKResponse())
541 m_supports_x = eLazyBoolYes;
542 }
543 }
544 return m_supports_x;
545}
546
Greg Clayton3dedae12013-12-06 21:45:27 +0000547GDBRemoteCommunicationClient::PacketResult
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000548GDBRemoteCommunicationClient::SendPacketsAndConcatenateResponses
549(
550 const char *payload_prefix,
551 std::string &response_string
552)
553{
554 Mutex::Locker locker;
555 if (!GetSequenceMutex(locker,
556 "ProcessGDBRemote::SendPacketsAndConcatenateResponses() failed due to not getting the sequence mutex"))
557 {
558 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
559 if (log)
560 log->Printf("error: failed to get packet sequence mutex, not sending packets with prefix '%s'",
561 payload_prefix);
562 return PacketResult::ErrorNoSequenceLock;
563 }
564
565 response_string = "";
566 std::string payload_prefix_str(payload_prefix);
567 unsigned int response_size = 0x1000;
568 if (response_size > GetRemoteMaxPacketSize()) { // May send qSupported packet
569 response_size = GetRemoteMaxPacketSize();
570 }
571
572 for (unsigned int offset = 0; true; offset += response_size)
573 {
574 StringExtractorGDBRemote this_response;
575 // Construct payload
576 char sizeDescriptor[128];
577 snprintf(sizeDescriptor, sizeof(sizeDescriptor), "%x,%x", offset, response_size);
578 PacketResult result = SendPacketAndWaitForResponse((payload_prefix_str + sizeDescriptor).c_str(),
579 this_response,
580 /*send_async=*/false);
581 if (result != PacketResult::Success)
582 return result;
583
584 const std::string &this_string = this_response.GetStringRef();
585
586 // Check for m or l as first character; l seems to mean this is the last chunk
587 char first_char = *this_string.c_str();
588 if (first_char != 'm' && first_char != 'l')
589 {
590 return PacketResult::ErrorReplyInvalid;
591 }
Steve Pucci03904ac2014-03-04 23:18:46 +0000592 // Concatenate the result so far (skipping 'm' or 'l')
593 response_string.append(this_string, 1, std::string::npos);
Steve Pucci5ae54ae2014-01-25 05:46:51 +0000594 if (first_char == 'l')
595 // We're done
596 return PacketResult::Success;
597 }
598}
599
600GDBRemoteCommunicationClient::PacketResult
Greg Clayton576d8832011-03-22 04:00:09 +0000601GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
602(
603 const char *payload,
604 StringExtractorGDBRemote &response,
605 bool send_async
606)
607{
608 return SendPacketAndWaitForResponse (payload,
609 ::strlen (payload),
610 response,
611 send_async);
612}
613
Greg Clayton3dedae12013-12-06 21:45:27 +0000614GDBRemoteCommunicationClient::PacketResult
615GDBRemoteCommunicationClient::SendPacketAndWaitForResponseNoLock (const char *payload,
616 size_t payload_length,
617 StringExtractorGDBRemote &response)
618{
619 PacketResult packet_result = SendPacketNoLock (payload, payload_length);
620 if (packet_result == PacketResult::Success)
621 packet_result = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ());
622 return packet_result;
623}
624
625GDBRemoteCommunicationClient::PacketResult
Greg Clayton576d8832011-03-22 04:00:09 +0000626GDBRemoteCommunicationClient::SendPacketAndWaitForResponse
627(
628 const char *payload,
629 size_t payload_length,
630 StringExtractorGDBRemote &response,
631 bool send_async
632)
633{
Greg Clayton3dedae12013-12-06 21:45:27 +0000634 PacketResult packet_result = PacketResult::ErrorSendFailed;
Greg Clayton576d8832011-03-22 04:00:09 +0000635 Mutex::Locker locker;
Greg Clayton5160ce52013-03-27 23:08:40 +0000636 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000637 if (GetSequenceMutex (locker))
Greg Clayton576d8832011-03-22 04:00:09 +0000638 {
Greg Clayton3dedae12013-12-06 21:45:27 +0000639 packet_result = SendPacketAndWaitForResponseNoLock (payload, payload_length, response);
Greg Clayton576d8832011-03-22 04:00:09 +0000640 }
641 else
642 {
643 if (send_async)
644 {
Greg Claytond3544052012-05-31 21:24:20 +0000645 if (IsRunning())
Greg Clayton576d8832011-03-22 04:00:09 +0000646 {
Greg Claytond3544052012-05-31 21:24:20 +0000647 Mutex::Locker async_locker (m_async_mutex);
648 m_async_packet.assign(payload, payload_length);
649 m_async_packet_predicate.SetValue (true, eBroadcastNever);
650
651 if (log)
652 log->Printf ("async: async packet = %s", m_async_packet.c_str());
653
654 bool timed_out = false;
655 if (SendInterrupt(locker, 2, timed_out))
Greg Clayton576d8832011-03-22 04:00:09 +0000656 {
Greg Claytond3544052012-05-31 21:24:20 +0000657 if (m_interrupt_sent)
Greg Clayton576d8832011-03-22 04:00:09 +0000658 {
Jim Inghambabfc382012-06-06 00:32:39 +0000659 m_interrupt_sent = false;
Greg Claytond3544052012-05-31 21:24:20 +0000660 TimeValue timeout_time;
661 timeout_time = TimeValue::Now();
662 timeout_time.OffsetWithSeconds (m_packet_timeout);
663
Greg Clayton576d8832011-03-22 04:00:09 +0000664 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000665 log->Printf ("async: sent interrupt");
Greg Clayton644247c2011-07-07 01:59:51 +0000666
Greg Claytond3544052012-05-31 21:24:20 +0000667 if (m_async_packet_predicate.WaitForValueEqualTo (false, &timeout_time, &timed_out))
Greg Claytone889ad62011-10-27 22:04:16 +0000668 {
Greg Claytond3544052012-05-31 21:24:20 +0000669 if (log)
670 log->Printf ("async: got response");
671
672 // Swap the response buffer to avoid malloc and string copy
673 response.GetStringRef().swap (m_async_response.GetStringRef());
Jim Inghama6195b72013-12-18 01:24:33 +0000674 packet_result = m_async_result;
Greg Claytond3544052012-05-31 21:24:20 +0000675 }
676 else
677 {
678 if (log)
679 log->Printf ("async: timed out waiting for response");
680 }
681
682 // Make sure we wait until the continue packet has been sent again...
683 if (m_private_is_running.WaitForValueEqualTo (true, &timeout_time, &timed_out))
684 {
685 if (log)
686 {
687 if (timed_out)
688 log->Printf ("async: timed out waiting for process to resume, but process was resumed");
689 else
690 log->Printf ("async: async packet sent");
691 }
692 }
693 else
694 {
695 if (log)
696 log->Printf ("async: timed out waiting for process to resume");
Greg Claytone889ad62011-10-27 22:04:16 +0000697 }
698 }
699 else
700 {
Greg Claytond3544052012-05-31 21:24:20 +0000701 // We had a racy condition where we went to send the interrupt
702 // yet we were able to get the lock, so the process must have
703 // just stopped?
Greg Clayton576d8832011-03-22 04:00:09 +0000704 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000705 log->Printf ("async: got lock without sending interrupt");
706 // Send the packet normally since we got the lock
Greg Clayton3dedae12013-12-06 21:45:27 +0000707 packet_result = SendPacketAndWaitForResponseNoLock (payload, payload_length, response);
Greg Clayton576d8832011-03-22 04:00:09 +0000708 }
709 }
710 else
711 {
Greg Clayton644247c2011-07-07 01:59:51 +0000712 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000713 log->Printf ("async: failed to interrupt");
Greg Clayton576d8832011-03-22 04:00:09 +0000714 }
715 }
716 else
717 {
718 if (log)
Greg Claytond3544052012-05-31 21:24:20 +0000719 log->Printf ("async: not running, async is ignored");
Greg Clayton576d8832011-03-22 04:00:09 +0000720 }
721 }
722 else
723 {
724 if (log)
Greg Claytonc3c0b0e2012-04-12 19:04:34 +0000725 log->Printf("error: failed to get packet sequence mutex, not sending packet '%*s'", (int) payload_length, payload);
Greg Clayton576d8832011-03-22 04:00:09 +0000726 }
727 }
Greg Clayton3dedae12013-12-06 21:45:27 +0000728 return packet_result;
Greg Clayton576d8832011-03-22 04:00:09 +0000729}
730
Han Ming Ong4b6459f2013-01-18 23:11:53 +0000731static const char *end_delimiter = "--end--;";
732static const int end_delimiter_len = 8;
733
734std::string
735GDBRemoteCommunicationClient::HarmonizeThreadIdsForProfileData
736( ProcessGDBRemote *process,
737 StringExtractorGDBRemote& profileDataExtractor
738)
739{
740 std::map<uint64_t, uint32_t> new_thread_id_to_used_usec_map;
741 std::stringstream final_output;
742 std::string name, value;
743
744 // Going to assuming thread_used_usec comes first, else bail out.
745 while (profileDataExtractor.GetNameColonValue(name, value))
746 {
747 if (name.compare("thread_used_id") == 0)
748 {
749 StringExtractor threadIDHexExtractor(value.c_str());
750 uint64_t thread_id = threadIDHexExtractor.GetHexMaxU64(false, 0);
751
752 bool has_used_usec = false;
753 uint32_t curr_used_usec = 0;
754 std::string usec_name, usec_value;
755 uint32_t input_file_pos = profileDataExtractor.GetFilePos();
756 if (profileDataExtractor.GetNameColonValue(usec_name, usec_value))
757 {
758 if (usec_name.compare("thread_used_usec") == 0)
759 {
760 has_used_usec = true;
761 curr_used_usec = strtoull(usec_value.c_str(), NULL, 0);
762 }
763 else
764 {
765 // We didn't find what we want, it is probably
766 // an older version. Bail out.
767 profileDataExtractor.SetFilePos(input_file_pos);
768 }
769 }
770
771 if (has_used_usec)
772 {
773 uint32_t prev_used_usec = 0;
774 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_used_usec_map.find(thread_id);
775 if (iterator != m_thread_id_to_used_usec_map.end())
776 {
777 prev_used_usec = m_thread_id_to_used_usec_map[thread_id];
778 }
779
780 uint32_t real_used_usec = curr_used_usec - prev_used_usec;
781 // A good first time record is one that runs for at least 0.25 sec
782 bool good_first_time = (prev_used_usec == 0) && (real_used_usec > 250000);
783 bool good_subsequent_time = (prev_used_usec > 0) &&
784 ((real_used_usec > 0) || (process->HasAssignedIndexIDToThread(thread_id)));
785
786 if (good_first_time || good_subsequent_time)
787 {
788 // We try to avoid doing too many index id reservation,
789 // resulting in fast increase of index ids.
790
791 final_output << name << ":";
792 int32_t index_id = process->AssignIndexIDToThread(thread_id);
793 final_output << index_id << ";";
794
795 final_output << usec_name << ":" << usec_value << ";";
796 }
797 else
798 {
799 // Skip past 'thread_used_name'.
800 std::string local_name, local_value;
801 profileDataExtractor.GetNameColonValue(local_name, local_value);
802 }
803
804 // Store current time as previous time so that they can be compared later.
805 new_thread_id_to_used_usec_map[thread_id] = curr_used_usec;
806 }
807 else
808 {
809 // Bail out and use old string.
810 final_output << name << ":" << value << ";";
811 }
812 }
813 else
814 {
815 final_output << name << ":" << value << ";";
816 }
817 }
818 final_output << end_delimiter;
819 m_thread_id_to_used_usec_map = new_thread_id_to_used_usec_map;
820
821 return final_output.str();
822}
823
Greg Clayton576d8832011-03-22 04:00:09 +0000824StateType
825GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse
826(
827 ProcessGDBRemote *process,
828 const char *payload,
829 size_t packet_length,
830 StringExtractorGDBRemote &response
831)
832{
Greg Clayton1f5181a2012-07-02 22:05:25 +0000833 m_curr_tid = LLDB_INVALID_THREAD_ID;
Greg Clayton5160ce52013-03-27 23:08:40 +0000834 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
Greg Clayton576d8832011-03-22 04:00:09 +0000835 if (log)
836 log->Printf ("GDBRemoteCommunicationClient::%s ()", __FUNCTION__);
837
838 Mutex::Locker locker(m_sequence_mutex);
839 StateType state = eStateRunning;
840
841 BroadcastEvent(eBroadcastBitRunPacketSent, NULL);
842 m_public_is_running.SetValue (true, eBroadcastNever);
843 // Set the starting continue packet into "continue_packet". This packet
Jim Inghambabfc382012-06-06 00:32:39 +0000844 // may change if we are interrupted and we continue after an async packet...
Greg Clayton576d8832011-03-22 04:00:09 +0000845 std::string continue_packet(payload, packet_length);
846
Greg Clayton3f875c52013-02-22 22:23:55 +0000847 bool got_async_packet = false;
Greg Claytonaf247d72011-05-19 03:54:16 +0000848
Greg Clayton576d8832011-03-22 04:00:09 +0000849 while (state == eStateRunning)
850 {
Greg Clayton3f875c52013-02-22 22:23:55 +0000851 if (!got_async_packet)
Greg Claytonaf247d72011-05-19 03:54:16 +0000852 {
853 if (log)
854 log->Printf ("GDBRemoteCommunicationClient::%s () sending continue packet: %s", __FUNCTION__, continue_packet.c_str());
Greg Clayton3dedae12013-12-06 21:45:27 +0000855 if (SendPacketNoLock(continue_packet.c_str(), continue_packet.size()) != PacketResult::Success)
Greg Claytonaf247d72011-05-19 03:54:16 +0000856 state = eStateInvalid;
Jim Inghamb8cd5752014-04-16 02:24:17 +0000857 else
858 m_interrupt_sent = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000859
Greg Claytone889ad62011-10-27 22:04:16 +0000860 m_private_is_running.SetValue (true, eBroadcastAlways);
Greg Claytonaf247d72011-05-19 03:54:16 +0000861 }
862
Greg Clayton3f875c52013-02-22 22:23:55 +0000863 got_async_packet = false;
Greg Clayton576d8832011-03-22 04:00:09 +0000864
865 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000866 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(%s)", __FUNCTION__, continue_packet.c_str());
Greg Clayton576d8832011-03-22 04:00:09 +0000867
Greg Clayton3dedae12013-12-06 21:45:27 +0000868 if (WaitForPacketWithTimeoutMicroSecondsNoLock(response, UINT32_MAX) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +0000869 {
870 if (response.Empty())
871 state = eStateInvalid;
872 else
873 {
874 const char stop_type = response.GetChar();
875 if (log)
876 log->Printf ("GDBRemoteCommunicationClient::%s () got packet: %s", __FUNCTION__, response.GetStringRef().c_str());
877 switch (stop_type)
878 {
879 case 'T':
880 case 'S':
Greg Clayton576d8832011-03-22 04:00:09 +0000881 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000882 if (process->GetStopID() == 0)
Greg Clayton576d8832011-03-22 04:00:09 +0000883 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000884 if (process->GetID() == LLDB_INVALID_PROCESS_ID)
885 {
886 lldb::pid_t pid = GetCurrentProcessID ();
887 if (pid != LLDB_INVALID_PROCESS_ID)
888 process->SetID (pid);
889 }
890 process->BuildDynamicRegisterInfo (true);
Greg Clayton576d8832011-03-22 04:00:09 +0000891 }
Greg Clayton2687cd12012-03-29 01:55:41 +0000892
893 // Privately notify any internal threads that we have stopped
894 // in case we wanted to interrupt our process, yet we might
895 // send a packet and continue without returning control to the
896 // user.
897 m_private_is_running.SetValue (false, eBroadcastAlways);
898
899 const uint8_t signo = response.GetHexU8 (UINT8_MAX);
900
Jim Inghambabfc382012-06-06 00:32:39 +0000901 bool continue_after_async = m_async_signal != -1 || m_async_packet_predicate.GetValue();
902 if (continue_after_async || m_interrupt_sent)
Greg Clayton2687cd12012-03-29 01:55:41 +0000903 {
Greg Clayton2687cd12012-03-29 01:55:41 +0000904 // We sent an interrupt packet to stop the inferior process
905 // for an async signal or to send an async packet while running
906 // but we might have been single stepping and received the
907 // stop packet for the step instead of for the interrupt packet.
908 // Typically when an interrupt is sent a SIGINT or SIGSTOP
909 // is used, so if we get anything else, we need to try and
910 // get another stop reply packet that may have been sent
911 // due to sending the interrupt when the target is stopped
912 // which will just re-send a copy of the last stop reply
913 // packet. If we don't do this, then the reply for our
914 // async packet will be the repeat stop reply packet and cause
915 // a lot of trouble for us!
916 if (signo != SIGINT && signo != SIGSTOP)
917 {
Greg Claytonfb72fde2012-05-15 02:50:49 +0000918 continue_after_async = false;
Greg Clayton2687cd12012-03-29 01:55:41 +0000919
920 // We didn't get a a SIGINT or SIGSTOP, so try for a
921 // very brief time (1 ms) to get another stop reply
922 // packet to make sure it doesn't get in the way
923 StringExtractorGDBRemote extra_stop_reply_packet;
924 uint32_t timeout_usec = 1000;
Greg Clayton3dedae12013-12-06 21:45:27 +0000925 if (WaitForPacketWithTimeoutMicroSecondsNoLock (extra_stop_reply_packet, timeout_usec) == PacketResult::Success)
Greg Clayton2687cd12012-03-29 01:55:41 +0000926 {
927 switch (extra_stop_reply_packet.GetChar())
928 {
929 case 'T':
930 case 'S':
931 // We did get an extra stop reply, which means
932 // our interrupt didn't stop the target so we
933 // shouldn't continue after the async signal
934 // or packet is sent...
Greg Claytonfb72fde2012-05-15 02:50:49 +0000935 continue_after_async = false;
Greg Clayton2687cd12012-03-29 01:55:41 +0000936 break;
937 }
938 }
939 }
940 }
941
942 if (m_async_signal != -1)
943 {
944 if (log)
945 log->Printf ("async: send signo = %s", Host::GetSignalAsCString (m_async_signal));
946
947 // Save off the async signal we are supposed to send
948 const int async_signal = m_async_signal;
949 // Clear the async signal member so we don't end up
950 // sending the signal multiple times...
951 m_async_signal = -1;
952 // Check which signal we stopped with
953 if (signo == async_signal)
954 {
955 if (log)
956 log->Printf ("async: stopped with signal %s, we are done running", Host::GetSignalAsCString (signo));
957
958 // We already stopped with a signal that we wanted
959 // to stop with, so we are done
960 }
961 else
962 {
963 // We stopped with a different signal that the one
964 // we wanted to stop with, so now we must resume
965 // with the signal we want
966 char signal_packet[32];
967 int signal_packet_len = 0;
968 signal_packet_len = ::snprintf (signal_packet,
969 sizeof (signal_packet),
970 "C%2.2x",
971 async_signal);
972
973 if (log)
974 log->Printf ("async: stopped with signal %s, resume with %s",
975 Host::GetSignalAsCString (signo),
976 Host::GetSignalAsCString (async_signal));
977
978 // Set the continue packet to resume even if the
Greg Claytonfb72fde2012-05-15 02:50:49 +0000979 // interrupt didn't cause our stop (ignore continue_after_async)
Greg Clayton2687cd12012-03-29 01:55:41 +0000980 continue_packet.assign(signal_packet, signal_packet_len);
981 continue;
982 }
983 }
984 else if (m_async_packet_predicate.GetValue())
985 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000986 Log * packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Greg Clayton2687cd12012-03-29 01:55:41 +0000987
988 // We are supposed to send an asynchronous packet while
Jim Inghama6195b72013-12-18 01:24:33 +0000989 // we are running.
Greg Clayton2687cd12012-03-29 01:55:41 +0000990 m_async_response.Clear();
991 if (m_async_packet.empty())
992 {
Jim Inghama6195b72013-12-18 01:24:33 +0000993 m_async_result = PacketResult::ErrorSendFailed;
994 if (packet_log)
Greg Clayton2687cd12012-03-29 01:55:41 +0000995 packet_log->Printf ("async: error: empty async packet");
996
997 }
998 else
999 {
1000 if (packet_log)
1001 packet_log->Printf ("async: sending packet");
1002
Jim Inghama6195b72013-12-18 01:24:33 +00001003 m_async_result = SendPacketAndWaitForResponse (&m_async_packet[0],
1004 m_async_packet.size(),
1005 m_async_response,
1006 false);
Greg Clayton2687cd12012-03-29 01:55:41 +00001007 }
1008 // Let the other thread that was trying to send the async
1009 // packet know that the packet has been sent and response is
1010 // ready...
1011 m_async_packet_predicate.SetValue(false, eBroadcastAlways);
1012
1013 if (packet_log)
Greg Claytonfb72fde2012-05-15 02:50:49 +00001014 packet_log->Printf ("async: sent packet, continue_after_async = %i", continue_after_async);
Greg Clayton2687cd12012-03-29 01:55:41 +00001015
1016 // Set the continue packet to resume if our interrupt
1017 // for the async packet did cause the stop
Greg Claytonfb72fde2012-05-15 02:50:49 +00001018 if (continue_after_async)
Greg Clayton2687cd12012-03-29 01:55:41 +00001019 {
Greg Claytonf1186de2012-05-24 23:42:14 +00001020 // Reverting this for now as it is causing deadlocks
1021 // in programs (<rdar://problem/11529853>). In the future
1022 // we should check our thread list and "do the right thing"
1023 // for new threads that show up while we stop and run async
1024 // packets. Setting the packet to 'c' to continue all threads
1025 // is the right thing to do 99.99% of the time because if a
1026 // thread was single stepping, and we sent an interrupt, we
1027 // will notice above that we didn't stop due to an interrupt
1028 // but stopped due to stepping and we would _not_ continue.
1029 continue_packet.assign (1, 'c');
Greg Clayton2687cd12012-03-29 01:55:41 +00001030 continue;
1031 }
1032 }
1033 // Stop with signal and thread info
1034 state = eStateStopped;
Greg Clayton576d8832011-03-22 04:00:09 +00001035 }
Greg Clayton576d8832011-03-22 04:00:09 +00001036 break;
1037
1038 case 'W':
1039 case 'X':
1040 // process exited
1041 state = eStateExited;
1042 break;
1043
1044 case 'O':
1045 // STDOUT
1046 {
Greg Clayton3f875c52013-02-22 22:23:55 +00001047 got_async_packet = true;
Greg Clayton576d8832011-03-22 04:00:09 +00001048 std::string inferior_stdout;
1049 inferior_stdout.reserve(response.GetBytesLeft () / 2);
1050 char ch;
1051 while ((ch = response.GetHexU8()) != '\0')
1052 inferior_stdout.append(1, ch);
1053 process->AppendSTDOUT (inferior_stdout.c_str(), inferior_stdout.size());
1054 }
1055 break;
1056
Han Ming Ongab3b8b22012-11-17 00:21:04 +00001057 case 'A':
1058 // Async miscellaneous reply. Right now, only profile data is coming through this channel.
1059 {
Greg Clayton3f875c52013-02-22 22:23:55 +00001060 got_async_packet = true;
Han Ming Ong4b6459f2013-01-18 23:11:53 +00001061 std::string input = response.GetStringRef().substr(1); // '1' to move beyond 'A'
1062 if (m_partial_profile_data.length() > 0)
1063 {
1064 m_partial_profile_data.append(input);
1065 input = m_partial_profile_data;
1066 m_partial_profile_data.clear();
1067 }
1068
1069 size_t found, pos = 0, len = input.length();
1070 while ((found = input.find(end_delimiter, pos)) != std::string::npos)
1071 {
1072 StringExtractorGDBRemote profileDataExtractor(input.substr(pos, found).c_str());
Han Ming Ong91ed6b82013-06-24 18:15:05 +00001073 std::string profile_data = HarmonizeThreadIdsForProfileData(process, profileDataExtractor);
1074 process->BroadcastAsyncProfileData (profile_data);
Han Ming Ong4b6459f2013-01-18 23:11:53 +00001075
1076 pos = found + end_delimiter_len;
1077 }
1078
1079 if (pos < len)
1080 {
1081 // Last incomplete chunk.
1082 m_partial_profile_data = input.substr(pos);
1083 }
Han Ming Ongab3b8b22012-11-17 00:21:04 +00001084 }
1085 break;
1086
Greg Clayton576d8832011-03-22 04:00:09 +00001087 case 'E':
1088 // ERROR
1089 state = eStateInvalid;
1090 break;
1091
1092 default:
1093 if (log)
1094 log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__);
1095 state = eStateInvalid;
1096 break;
1097 }
1098 }
1099 }
1100 else
1101 {
1102 if (log)
1103 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__);
1104 state = eStateInvalid;
1105 }
1106 }
1107 if (log)
1108 log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state));
1109 response.SetFilePos(0);
1110 m_private_is_running.SetValue (false, eBroadcastAlways);
1111 m_public_is_running.SetValue (false, eBroadcastAlways);
1112 return state;
1113}
1114
1115bool
1116GDBRemoteCommunicationClient::SendAsyncSignal (int signo)
1117{
Greg Clayton2687cd12012-03-29 01:55:41 +00001118 Mutex::Locker async_locker (m_async_mutex);
Greg Clayton576d8832011-03-22 04:00:09 +00001119 m_async_signal = signo;
1120 bool timed_out = false;
Greg Clayton576d8832011-03-22 04:00:09 +00001121 Mutex::Locker locker;
Greg Clayton2687cd12012-03-29 01:55:41 +00001122 if (SendInterrupt (locker, 1, timed_out))
Greg Clayton576d8832011-03-22 04:00:09 +00001123 return true;
1124 m_async_signal = -1;
1125 return false;
1126}
1127
Greg Clayton37a0a242012-04-11 00:24:49 +00001128// This function takes a mutex locker as a parameter in case the GetSequenceMutex
Greg Clayton576d8832011-03-22 04:00:09 +00001129// actually succeeds. If it doesn't succeed in acquiring the sequence mutex
1130// (the expected result), then it will send the halt packet. If it does succeed
1131// then the caller that requested the interrupt will want to keep the sequence
1132// locked down so that no one else can send packets while the caller has control.
1133// This function usually gets called when we are running and need to stop the
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001134// target. It can also be used when we are running and we need to do something
Greg Clayton576d8832011-03-22 04:00:09 +00001135// else (like read/write memory), so we need to interrupt the running process
1136// (gdb remote protocol requires this), and do what we need to do, then resume.
1137
1138bool
Greg Clayton2687cd12012-03-29 01:55:41 +00001139GDBRemoteCommunicationClient::SendInterrupt
Greg Clayton576d8832011-03-22 04:00:09 +00001140(
1141 Mutex::Locker& locker,
1142 uint32_t seconds_to_wait_for_stop,
Greg Clayton576d8832011-03-22 04:00:09 +00001143 bool &timed_out
1144)
1145{
Greg Clayton576d8832011-03-22 04:00:09 +00001146 timed_out = false;
Greg Clayton5160ce52013-03-27 23:08:40 +00001147 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Clayton576d8832011-03-22 04:00:09 +00001148
1149 if (IsRunning())
1150 {
1151 // Only send an interrupt if our debugserver is running...
Greg Claytonc3c0b0e2012-04-12 19:04:34 +00001152 if (GetSequenceMutex (locker))
Greg Clayton37a0a242012-04-11 00:24:49 +00001153 {
1154 if (log)
1155 log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt");
1156 }
1157 else
Greg Clayton576d8832011-03-22 04:00:09 +00001158 {
1159 // Someone has the mutex locked waiting for a response or for the
1160 // inferior to stop, so send the interrupt on the down low...
1161 char ctrl_c = '\x03';
1162 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton576d8832011-03-22 04:00:09 +00001163 size_t bytes_written = Write (&ctrl_c, 1, status, NULL);
Greg Clayton2687cd12012-03-29 01:55:41 +00001164 if (log)
1165 log->PutCString("send packet: \\x03");
Greg Clayton576d8832011-03-22 04:00:09 +00001166 if (bytes_written > 0)
1167 {
Greg Clayton2687cd12012-03-29 01:55:41 +00001168 m_interrupt_sent = true;
Greg Clayton576d8832011-03-22 04:00:09 +00001169 if (seconds_to_wait_for_stop)
1170 {
Greg Clayton2687cd12012-03-29 01:55:41 +00001171 TimeValue timeout;
1172 if (seconds_to_wait_for_stop)
1173 {
1174 timeout = TimeValue::Now();
1175 timeout.OffsetWithSeconds (seconds_to_wait_for_stop);
1176 }
Greg Clayton576d8832011-03-22 04:00:09 +00001177 if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out))
1178 {
1179 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +00001180 log->PutCString ("SendInterrupt () - sent interrupt, private state stopped");
Greg Clayton576d8832011-03-22 04:00:09 +00001181 return true;
1182 }
1183 else
1184 {
1185 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +00001186 log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume");
Greg Clayton576d8832011-03-22 04:00:09 +00001187 }
1188 }
1189 else
1190 {
1191 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +00001192 log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop...");
Greg Clayton576d8832011-03-22 04:00:09 +00001193 return true;
1194 }
1195 }
1196 else
1197 {
1198 if (log)
Greg Clayton2687cd12012-03-29 01:55:41 +00001199 log->Printf ("SendInterrupt () - failed to write interrupt");
Greg Clayton576d8832011-03-22 04:00:09 +00001200 }
1201 return false;
1202 }
Greg Clayton576d8832011-03-22 04:00:09 +00001203 }
Greg Clayton2687cd12012-03-29 01:55:41 +00001204 else
1205 {
1206 if (log)
1207 log->Printf ("SendInterrupt () - not running");
1208 }
Greg Clayton576d8832011-03-22 04:00:09 +00001209 return true;
1210}
1211
1212lldb::pid_t
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001213GDBRemoteCommunicationClient::GetCurrentProcessID (bool allow_lazy)
Greg Clayton576d8832011-03-22 04:00:09 +00001214{
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001215 if (allow_lazy && m_curr_pid_is_valid == eLazyBoolYes)
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001216 return m_curr_pid;
1217
1218 // First try to retrieve the pid via the qProcessInfo request.
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00001219 GetCurrentProcessInfo (allow_lazy);
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001220 if (m_curr_pid_is_valid == eLazyBoolYes)
Greg Clayton576d8832011-03-22 04:00:09 +00001221 {
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001222 // We really got it.
1223 return m_curr_pid;
Greg Clayton576d8832011-03-22 04:00:09 +00001224 }
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001225 else
1226 {
Todd Fialae24614f2014-05-14 00:15:32 +00001227 // If we don't get a response for qProcessInfo, check if $qC gives us a result.
1228 // $qC only returns a real process id on older debugserver and lldb-platform stubs.
1229 // The gdb remote protocol documents $qC as returning the thread id, which newer
1230 // debugserver and lldb-gdbserver stubs return correctly.
1231 StringExtractorGDBRemote response;
1232 if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false) == PacketResult::Success)
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001233 {
Todd Fialae24614f2014-05-14 00:15:32 +00001234 if (response.GetChar() == 'Q')
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001235 {
Todd Fialae24614f2014-05-14 00:15:32 +00001236 if (response.GetChar() == 'C')
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001237 {
Todd Fialae24614f2014-05-14 00:15:32 +00001238 m_curr_pid = response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID);
1239 if (m_curr_pid != LLDB_INVALID_PROCESS_ID)
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001240 {
Todd Fialae24614f2014-05-14 00:15:32 +00001241 m_curr_pid_is_valid = eLazyBoolYes;
1242 return m_curr_pid;
Todd Fiala9f72b3a2014-05-07 19:28:21 +00001243 }
1244 }
1245 }
1246 }
1247 }
1248
Greg Clayton576d8832011-03-22 04:00:09 +00001249 return LLDB_INVALID_PROCESS_ID;
1250}
1251
1252bool
1253GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str)
1254{
1255 error_str.clear();
1256 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001257 if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001258 {
1259 if (response.IsOKResponse())
1260 return true;
1261 if (response.GetChar() == 'E')
1262 {
1263 // A string the describes what failed when launching...
1264 error_str = response.GetStringRef().substr(1);
1265 }
1266 else
1267 {
1268 error_str.assign ("unknown error occurred launching process");
1269 }
1270 }
1271 else
1272 {
Jim Ingham98d6da52012-06-28 20:30:23 +00001273 error_str.assign ("timed out waiting for app to launch");
Greg Clayton576d8832011-03-22 04:00:09 +00001274 }
1275 return false;
1276}
1277
1278int
Greg Claytonfbb76342013-11-20 21:07:01 +00001279GDBRemoteCommunicationClient::SendArgumentsPacket (const ProcessLaunchInfo &launch_info)
Greg Clayton576d8832011-03-22 04:00:09 +00001280{
Greg Claytonfbb76342013-11-20 21:07:01 +00001281 // Since we don't get the send argv0 separate from the executable path, we need to
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001282 // make sure to use the actual executable path found in the launch_info...
Greg Claytonfbb76342013-11-20 21:07:01 +00001283 std::vector<const char *> argv;
1284 FileSpec exe_file = launch_info.GetExecutableFile();
1285 std::string exe_path;
1286 const char *arg = NULL;
1287 const Args &launch_args = launch_info.GetArguments();
1288 if (exe_file)
1289 exe_path = exe_file.GetPath();
1290 else
1291 {
1292 arg = launch_args.GetArgumentAtIndex(0);
1293 if (arg)
1294 exe_path = arg;
1295 }
1296 if (!exe_path.empty())
1297 {
1298 argv.push_back(exe_path.c_str());
1299 for (uint32_t i=1; (arg = launch_args.GetArgumentAtIndex(i)) != NULL; ++i)
1300 {
1301 if (arg)
1302 argv.push_back(arg);
1303 }
1304 }
1305 if (!argv.empty())
Greg Clayton576d8832011-03-22 04:00:09 +00001306 {
1307 StreamString packet;
1308 packet.PutChar('A');
Greg Claytonfbb76342013-11-20 21:07:01 +00001309 for (size_t i = 0, n = argv.size(); i < n; ++i)
Greg Clayton576d8832011-03-22 04:00:09 +00001310 {
Greg Claytonfbb76342013-11-20 21:07:01 +00001311 arg = argv[i];
Greg Clayton576d8832011-03-22 04:00:09 +00001312 const int arg_len = strlen(arg);
1313 if (i > 0)
1314 packet.PutChar(',');
Greg Claytonfbb76342013-11-20 21:07:01 +00001315 packet.Printf("%i,%i,", arg_len * 2, (int)i);
Greg Clayton576d8832011-03-22 04:00:09 +00001316 packet.PutBytesAsRawHex8 (arg, arg_len);
1317 }
1318
1319 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001320 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001321 {
1322 if (response.IsOKResponse())
1323 return 0;
1324 uint8_t error = response.GetError();
1325 if (error)
1326 return error;
1327 }
1328 }
1329 return -1;
1330}
1331
1332int
1333GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value)
1334{
1335 if (name_equal_value && name_equal_value[0])
1336 {
1337 StreamString packet;
Greg Clayton89600582013-10-10 17:53:50 +00001338 bool send_hex_encoding = false;
1339 for (const char *p = name_equal_value; *p != '\0' && send_hex_encoding == false; ++p)
Greg Clayton576d8832011-03-22 04:00:09 +00001340 {
Greg Clayton89600582013-10-10 17:53:50 +00001341 if (isprint(*p))
1342 {
1343 switch (*p)
1344 {
1345 case '$':
1346 case '#':
1347 send_hex_encoding = true;
1348 break;
1349 default:
1350 break;
1351 }
1352 }
1353 else
1354 {
1355 // We have non printable characters, lets hex encode this...
1356 send_hex_encoding = true;
1357 }
1358 }
1359
1360 StringExtractorGDBRemote response;
1361 if (send_hex_encoding)
1362 {
1363 if (m_supports_QEnvironmentHexEncoded)
1364 {
1365 packet.PutCString("QEnvironmentHexEncoded:");
1366 packet.PutBytesAsRawHex8 (name_equal_value, strlen(name_equal_value));
Greg Clayton3dedae12013-12-06 21:45:27 +00001367 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton89600582013-10-10 17:53:50 +00001368 {
1369 if (response.IsOKResponse())
1370 return 0;
1371 uint8_t error = response.GetError();
1372 if (error)
1373 return error;
1374 if (response.IsUnsupportedResponse())
1375 m_supports_QEnvironmentHexEncoded = false;
1376 }
1377 }
1378
1379 }
1380 else if (m_supports_QEnvironment)
1381 {
1382 packet.Printf("QEnvironment:%s", name_equal_value);
Greg Clayton3dedae12013-12-06 21:45:27 +00001383 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton89600582013-10-10 17:53:50 +00001384 {
1385 if (response.IsOKResponse())
1386 return 0;
1387 uint8_t error = response.GetError();
1388 if (error)
1389 return error;
1390 if (response.IsUnsupportedResponse())
1391 m_supports_QEnvironment = false;
1392 }
Greg Clayton576d8832011-03-22 04:00:09 +00001393 }
1394 }
1395 return -1;
1396}
1397
Greg Claytonc4103b32011-05-08 04:53:50 +00001398int
1399GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch)
1400{
1401 if (arch && arch[0])
1402 {
1403 StreamString packet;
1404 packet.Printf("QLaunchArch:%s", arch);
1405 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001406 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Claytonc4103b32011-05-08 04:53:50 +00001407 {
1408 if (response.IsOKResponse())
1409 return 0;
1410 uint8_t error = response.GetError();
1411 if (error)
1412 return error;
1413 }
1414 }
1415 return -1;
1416}
1417
Jason Molendaa3329782014-03-29 18:54:20 +00001418int
1419GDBRemoteCommunicationClient::SendLaunchEventDataPacket (char const *data, bool *was_supported)
1420{
1421 if (data && *data != '\0')
1422 {
1423 StreamString packet;
1424 packet.Printf("QSetProcessEvent:%s", data);
1425 StringExtractorGDBRemote response;
1426 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
1427 {
1428 if (response.IsOKResponse())
1429 {
1430 if (was_supported)
1431 *was_supported = true;
1432 return 0;
1433 }
1434 else if (response.IsUnsupportedResponse())
1435 {
1436 if (was_supported)
1437 *was_supported = false;
1438 return -1;
1439 }
1440 else
1441 {
1442 uint8_t error = response.GetError();
1443 if (was_supported)
1444 *was_supported = true;
1445 if (error)
1446 return error;
1447 }
1448 }
1449 }
1450 return -1;
1451}
1452
Greg Clayton576d8832011-03-22 04:00:09 +00001453bool
Greg Clayton1cb64962011-03-24 04:28:38 +00001454GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major,
1455 uint32_t &minor,
1456 uint32_t &update)
1457{
1458 if (GetHostInfo ())
1459 {
1460 if (m_os_version_major != UINT32_MAX)
1461 {
1462 major = m_os_version_major;
1463 minor = m_os_version_minor;
1464 update = m_os_version_update;
1465 return true;
1466 }
1467 }
1468 return false;
1469}
1470
1471bool
1472GDBRemoteCommunicationClient::GetOSBuildString (std::string &s)
1473{
1474 if (GetHostInfo ())
1475 {
1476 if (!m_os_build.empty())
1477 {
1478 s = m_os_build;
1479 return true;
1480 }
1481 }
1482 s.clear();
1483 return false;
1484}
1485
1486
1487bool
1488GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s)
1489{
1490 if (GetHostInfo ())
1491 {
1492 if (!m_os_kernel.empty())
1493 {
1494 s = m_os_kernel;
1495 return true;
1496 }
1497 }
1498 s.clear();
1499 return false;
1500}
1501
1502bool
1503GDBRemoteCommunicationClient::GetHostname (std::string &s)
1504{
1505 if (GetHostInfo ())
1506 {
1507 if (!m_hostname.empty())
1508 {
1509 s = m_hostname;
1510 return true;
1511 }
1512 }
1513 s.clear();
1514 return false;
1515}
1516
1517ArchSpec
1518GDBRemoteCommunicationClient::GetSystemArchitecture ()
1519{
1520 if (GetHostInfo ())
1521 return m_host_arch;
1522 return ArchSpec();
1523}
1524
Jason Molendaf17b5ac2012-12-19 02:54:03 +00001525const lldb_private::ArchSpec &
1526GDBRemoteCommunicationClient::GetProcessArchitecture ()
1527{
1528 if (m_qProcessInfo_is_valid == eLazyBoolCalculate)
1529 GetCurrentProcessInfo ();
1530 return m_process_arch;
1531}
1532
Jason Molendaa3329782014-03-29 18:54:20 +00001533bool
1534GDBRemoteCommunicationClient::GetGDBServerVersion()
1535{
1536 if (m_qGDBServerVersion_is_valid == eLazyBoolCalculate)
1537 {
1538 m_gdb_server_name.clear();
1539 m_gdb_server_version = 0;
1540 m_qGDBServerVersion_is_valid = eLazyBoolNo;
1541
1542 StringExtractorGDBRemote response;
1543 if (SendPacketAndWaitForResponse ("qGDBServerVersion", response, false) == PacketResult::Success)
1544 {
1545 if (response.IsNormalResponse())
1546 {
1547 std::string name;
1548 std::string value;
1549 bool success = false;
1550 while (response.GetNameColonValue(name, value))
1551 {
1552 if (name.compare("name") == 0)
1553 {
1554 success = true;
1555 m_gdb_server_name.swap(value);
1556 }
1557 else if (name.compare("version") == 0)
1558 {
1559 size_t dot_pos = value.find('.');
1560 if (dot_pos != std::string::npos)
1561 value[dot_pos] = '\0';
Vince Harron5275aaa2015-01-15 20:08:35 +00001562 const uint32_t version = StringConvert::ToUInt32(value.c_str(), UINT32_MAX, 0);
Jason Molendaa3329782014-03-29 18:54:20 +00001563 if (version != UINT32_MAX)
1564 {
1565 success = true;
1566 m_gdb_server_version = version;
1567 }
1568 }
1569 }
1570 if (success)
1571 m_qGDBServerVersion_is_valid = eLazyBoolYes;
1572 }
1573 }
1574 }
1575 return m_qGDBServerVersion_is_valid == eLazyBoolYes;
1576}
1577
1578const char *
1579GDBRemoteCommunicationClient::GetGDBServerProgramName()
1580{
1581 if (GetGDBServerVersion())
1582 {
1583 if (!m_gdb_server_name.empty())
1584 return m_gdb_server_name.c_str();
1585 }
1586 return NULL;
1587}
1588
1589uint32_t
1590GDBRemoteCommunicationClient::GetGDBServerProgramVersion()
1591{
1592 if (GetGDBServerVersion())
1593 return m_gdb_server_version;
1594 return 0;
1595}
Greg Clayton1cb64962011-03-24 04:28:38 +00001596
1597bool
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00001598GDBRemoteCommunicationClient::GetHostInfo (bool force)
Greg Clayton576d8832011-03-22 04:00:09 +00001599{
Todd Fialaaf245d12014-06-30 21:05:18 +00001600 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS));
1601
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00001602 if (force || m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton576d8832011-03-22 04:00:09 +00001603 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001604 m_qHostInfo_is_valid = eLazyBoolNo;
Greg Clayton576d8832011-03-22 04:00:09 +00001605 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001606 if (SendPacketAndWaitForResponse ("qHostInfo", response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001607 {
Greg Clayton17a0cb62011-05-15 23:46:54 +00001608 if (response.IsNormalResponse())
Greg Claytond314e812011-03-23 00:09:55 +00001609 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001610 std::string name;
1611 std::string value;
1612 uint32_t cpu = LLDB_INVALID_CPUTYPE;
1613 uint32_t sub = 0;
1614 std::string arch_name;
1615 std::string os_name;
1616 std::string vendor_name;
1617 std::string triple;
Todd Fialaa9ddb0e2014-01-18 03:02:39 +00001618 std::string distribution_id;
Greg Clayton32e0a752011-03-30 18:16:51 +00001619 uint32_t pointer_byte_size = 0;
1620 StringExtractor extractor;
1621 ByteOrder byte_order = eByteOrderInvalid;
1622 uint32_t num_keys_decoded = 0;
1623 while (response.GetNameColonValue(name, value))
Greg Claytond314e812011-03-23 00:09:55 +00001624 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001625 if (name.compare("cputype") == 0)
Greg Clayton1cb64962011-03-24 04:28:38 +00001626 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001627 // exception type in big endian hex
Vince Harron5275aaa2015-01-15 20:08:35 +00001628 cpu = StringConvert::ToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0);
Greg Clayton32e0a752011-03-30 18:16:51 +00001629 if (cpu != LLDB_INVALID_CPUTYPE)
1630 ++num_keys_decoded;
1631 }
1632 else if (name.compare("cpusubtype") == 0)
1633 {
1634 // exception count in big endian hex
Vince Harron5275aaa2015-01-15 20:08:35 +00001635 sub = StringConvert::ToUInt32 (value.c_str(), 0, 0);
Greg Clayton32e0a752011-03-30 18:16:51 +00001636 if (sub != 0)
1637 ++num_keys_decoded;
1638 }
1639 else if (name.compare("arch") == 0)
1640 {
1641 arch_name.swap (value);
1642 ++num_keys_decoded;
1643 }
1644 else if (name.compare("triple") == 0)
1645 {
Greg Clayton44272a42014-09-18 00:18:32 +00001646 extractor.GetStringRef ().swap (value);
1647 extractor.SetFilePos(0);
1648 extractor.GetHexByteString (triple);
Greg Clayton32e0a752011-03-30 18:16:51 +00001649 ++num_keys_decoded;
1650 }
Todd Fialaa9ddb0e2014-01-18 03:02:39 +00001651 else if (name.compare ("distribution_id") == 0)
1652 {
1653 extractor.GetStringRef ().swap (value);
1654 extractor.SetFilePos (0);
1655 extractor.GetHexByteString (distribution_id);
1656 ++num_keys_decoded;
1657 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001658 else if (name.compare("os_build") == 0)
1659 {
1660 extractor.GetStringRef().swap(value);
1661 extractor.SetFilePos(0);
1662 extractor.GetHexByteString (m_os_build);
1663 ++num_keys_decoded;
1664 }
1665 else if (name.compare("hostname") == 0)
1666 {
1667 extractor.GetStringRef().swap(value);
1668 extractor.SetFilePos(0);
1669 extractor.GetHexByteString (m_hostname);
1670 ++num_keys_decoded;
1671 }
1672 else if (name.compare("os_kernel") == 0)
1673 {
1674 extractor.GetStringRef().swap(value);
1675 extractor.SetFilePos(0);
1676 extractor.GetHexByteString (m_os_kernel);
1677 ++num_keys_decoded;
1678 }
1679 else if (name.compare("ostype") == 0)
1680 {
1681 os_name.swap (value);
1682 ++num_keys_decoded;
1683 }
1684 else if (name.compare("vendor") == 0)
1685 {
1686 vendor_name.swap(value);
1687 ++num_keys_decoded;
1688 }
1689 else if (name.compare("endian") == 0)
1690 {
1691 ++num_keys_decoded;
1692 if (value.compare("little") == 0)
1693 byte_order = eByteOrderLittle;
1694 else if (value.compare("big") == 0)
1695 byte_order = eByteOrderBig;
1696 else if (value.compare("pdp") == 0)
1697 byte_order = eByteOrderPDP;
1698 else
1699 --num_keys_decoded;
1700 }
1701 else if (name.compare("ptrsize") == 0)
1702 {
Vince Harron5275aaa2015-01-15 20:08:35 +00001703 pointer_byte_size = StringConvert::ToUInt32 (value.c_str(), 0, 0);
Greg Clayton32e0a752011-03-30 18:16:51 +00001704 if (pointer_byte_size != 0)
1705 ++num_keys_decoded;
1706 }
1707 else if (name.compare("os_version") == 0)
1708 {
1709 Args::StringToVersion (value.c_str(),
1710 m_os_version_major,
1711 m_os_version_minor,
1712 m_os_version_update);
1713 if (m_os_version_major != UINT32_MAX)
1714 ++num_keys_decoded;
1715 }
Enrico Granataf04a2192012-07-13 23:18:48 +00001716 else if (name.compare("watchpoint_exceptions_received") == 0)
1717 {
1718 ++num_keys_decoded;
1719 if (strcmp(value.c_str(),"before") == 0)
1720 m_watchpoints_trigger_after_instruction = eLazyBoolNo;
1721 else if (strcmp(value.c_str(),"after") == 0)
1722 m_watchpoints_trigger_after_instruction = eLazyBoolYes;
1723 else
1724 --num_keys_decoded;
1725 }
Greg Clayton9ac6d2d2013-10-25 18:13:17 +00001726 else if (name.compare("default_packet_timeout") == 0)
1727 {
Vince Harron5275aaa2015-01-15 20:08:35 +00001728 m_default_packet_timeout = StringConvert::ToUInt32(value.c_str(), 0);
Greg Clayton9ac6d2d2013-10-25 18:13:17 +00001729 if (m_default_packet_timeout > 0)
1730 {
1731 SetPacketTimeout(m_default_packet_timeout);
1732 ++num_keys_decoded;
1733 }
1734 }
Enrico Granataf04a2192012-07-13 23:18:48 +00001735
Greg Clayton32e0a752011-03-30 18:16:51 +00001736 }
1737
1738 if (num_keys_decoded > 0)
1739 m_qHostInfo_is_valid = eLazyBoolYes;
1740
1741 if (triple.empty())
1742 {
1743 if (arch_name.empty())
1744 {
1745 if (cpu != LLDB_INVALID_CPUTYPE)
1746 {
1747 m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
1748 if (pointer_byte_size)
1749 {
1750 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1751 }
1752 if (byte_order != eByteOrderInvalid)
1753 {
1754 assert (byte_order == m_host_arch.GetByteOrder());
1755 }
Greg Clayton70512312012-05-08 01:45:38 +00001756
1757 if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0)
1758 {
1759 switch (m_host_arch.GetMachine())
1760 {
Todd Fialad8eaa172014-07-23 14:37:35 +00001761 case llvm::Triple::aarch64:
Greg Clayton70512312012-05-08 01:45:38 +00001762 case llvm::Triple::arm:
1763 case llvm::Triple::thumb:
1764 os_name = "ios";
1765 break;
1766 default:
1767 os_name = "macosx";
1768 break;
1769 }
1770 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001771 if (!vendor_name.empty())
1772 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
1773 if (!os_name.empty())
Greg Claytone1dadb82011-09-15 00:21:03 +00001774 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Greg Clayton32e0a752011-03-30 18:16:51 +00001775
1776 }
1777 }
1778 else
1779 {
1780 std::string triple;
1781 triple += arch_name;
Greg Clayton70512312012-05-08 01:45:38 +00001782 if (!vendor_name.empty() || !os_name.empty())
1783 {
1784 triple += '-';
1785 if (vendor_name.empty())
1786 triple += "unknown";
1787 else
1788 triple += vendor_name;
1789 triple += '-';
1790 if (os_name.empty())
1791 triple += "unknown";
1792 else
1793 triple += os_name;
1794 }
1795 m_host_arch.SetTriple (triple.c_str());
1796
1797 llvm::Triple &host_triple = m_host_arch.GetTriple();
1798 if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin)
1799 {
1800 switch (m_host_arch.GetMachine())
1801 {
Todd Fialad8eaa172014-07-23 14:37:35 +00001802 case llvm::Triple::aarch64:
Greg Clayton70512312012-05-08 01:45:38 +00001803 case llvm::Triple::arm:
1804 case llvm::Triple::thumb:
1805 host_triple.setOS(llvm::Triple::IOS);
1806 break;
1807 default:
1808 host_triple.setOS(llvm::Triple::MacOSX);
1809 break;
1810 }
1811 }
Greg Clayton1cb64962011-03-24 04:28:38 +00001812 if (pointer_byte_size)
1813 {
1814 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1815 }
1816 if (byte_order != eByteOrderInvalid)
1817 {
1818 assert (byte_order == m_host_arch.GetByteOrder());
1819 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001820
Greg Clayton1cb64962011-03-24 04:28:38 +00001821 }
1822 }
1823 else
1824 {
Greg Clayton70512312012-05-08 01:45:38 +00001825 m_host_arch.SetTriple (triple.c_str());
Greg Claytond314e812011-03-23 00:09:55 +00001826 if (pointer_byte_size)
1827 {
1828 assert (pointer_byte_size == m_host_arch.GetAddressByteSize());
1829 }
1830 if (byte_order != eByteOrderInvalid)
1831 {
1832 assert (byte_order == m_host_arch.GetByteOrder());
1833 }
Todd Fialaaf245d12014-06-30 21:05:18 +00001834
1835 if (log)
1836 log->Printf ("GDBRemoteCommunicationClient::%s parsed host architecture as %s, triple as %s from triple text %s", __FUNCTION__, m_host_arch.GetArchitectureName () ? m_host_arch.GetArchitectureName () : "<null-arch-name>", m_host_arch.GetTriple ().getTriple ().c_str(), triple.c_str ());
Todd Fialaa9ddb0e2014-01-18 03:02:39 +00001837 }
1838 if (!distribution_id.empty ())
1839 m_host_arch.SetDistributionId (distribution_id.c_str ());
Greg Claytond314e812011-03-23 00:09:55 +00001840 }
Greg Clayton576d8832011-03-22 04:00:09 +00001841 }
1842 }
Greg Clayton32e0a752011-03-30 18:16:51 +00001843 return m_qHostInfo_is_valid == eLazyBoolYes;
Greg Clayton576d8832011-03-22 04:00:09 +00001844}
1845
1846int
1847GDBRemoteCommunicationClient::SendAttach
1848(
1849 lldb::pid_t pid,
1850 StringExtractorGDBRemote& response
1851)
1852{
1853 if (pid != LLDB_INVALID_PROCESS_ID)
1854 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001855 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001856 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, pid);
Andy Gibbsa297a972013-06-19 19:04:53 +00001857 assert (packet_len < (int)sizeof(packet));
Greg Clayton3dedae12013-12-06 21:45:27 +00001858 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00001859 {
1860 if (response.IsErrorResponse())
1861 return response.GetError();
1862 return 0;
1863 }
1864 }
1865 return -1;
1866}
1867
Vince Harrone0be4252015-02-06 18:32:57 +00001868int
1869GDBRemoteCommunicationClient::SendStdinNotification (const char* data, size_t data_len)
1870{
1871 StreamString packet;
1872 packet.PutCString("I");
1873 packet.PutBytesAsRawHex8(data, data_len);
1874 StringExtractorGDBRemote response;
1875 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
1876 {
1877 return 0;
1878 }
1879 return response.GetError();
1880
1881}
1882
Greg Clayton576d8832011-03-22 04:00:09 +00001883const lldb_private::ArchSpec &
1884GDBRemoteCommunicationClient::GetHostArchitecture ()
1885{
Greg Clayton32e0a752011-03-30 18:16:51 +00001886 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
Greg Clayton576d8832011-03-22 04:00:09 +00001887 GetHostInfo ();
Greg Claytond314e812011-03-23 00:09:55 +00001888 return m_host_arch;
Greg Clayton576d8832011-03-22 04:00:09 +00001889}
1890
Greg Clayton9ac6d2d2013-10-25 18:13:17 +00001891uint32_t
1892GDBRemoteCommunicationClient::GetHostDefaultPacketTimeout ()
1893{
1894 if (m_qHostInfo_is_valid == eLazyBoolCalculate)
1895 GetHostInfo ();
1896 return m_default_packet_timeout;
1897}
1898
Greg Clayton576d8832011-03-22 04:00:09 +00001899addr_t
1900GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions)
1901{
Greg Clayton70b57652011-05-15 01:25:55 +00001902 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton576d8832011-03-22 04:00:09 +00001903 {
Greg Clayton70b57652011-05-15 01:25:55 +00001904 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton2a48f522011-05-14 01:50:35 +00001905 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001906 const int packet_len = ::snprintf (packet, sizeof(packet), "_M%" PRIx64 ",%s%s%s",
Greg Clayton43e0af02012-09-18 18:04:04 +00001907 (uint64_t)size,
Greg Clayton2a48f522011-05-14 01:50:35 +00001908 permissions & lldb::ePermissionsReadable ? "r" : "",
1909 permissions & lldb::ePermissionsWritable ? "w" : "",
1910 permissions & lldb::ePermissionsExecutable ? "x" : "");
Andy Gibbsa297a972013-06-19 19:04:53 +00001911 assert (packet_len < (int)sizeof(packet));
Greg Clayton2a48f522011-05-14 01:50:35 +00001912 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001913 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton2a48f522011-05-14 01:50:35 +00001914 {
Todd Fialaf105f582014-06-21 00:48:09 +00001915 if (response.IsUnsupportedResponse())
1916 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1917 else if (!response.IsErrorResponse())
Greg Clayton2a48f522011-05-14 01:50:35 +00001918 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
1919 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00001920 else
1921 {
1922 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1923 }
Greg Clayton576d8832011-03-22 04:00:09 +00001924 }
1925 return LLDB_INVALID_ADDRESS;
1926}
1927
1928bool
1929GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr)
1930{
Greg Clayton70b57652011-05-15 01:25:55 +00001931 if (m_supports_alloc_dealloc_memory != eLazyBoolNo)
Greg Clayton576d8832011-03-22 04:00:09 +00001932 {
Greg Clayton70b57652011-05-15 01:25:55 +00001933 m_supports_alloc_dealloc_memory = eLazyBoolYes;
Greg Clayton2a48f522011-05-14 01:50:35 +00001934 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00001935 const int packet_len = ::snprintf(packet, sizeof(packet), "_m%" PRIx64, (uint64_t)addr);
Andy Gibbsa297a972013-06-19 19:04:53 +00001936 assert (packet_len < (int)sizeof(packet));
Greg Clayton2a48f522011-05-14 01:50:35 +00001937 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001938 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton2a48f522011-05-14 01:50:35 +00001939 {
Todd Fialaf105f582014-06-21 00:48:09 +00001940 if (response.IsUnsupportedResponse())
1941 m_supports_alloc_dealloc_memory = eLazyBoolNo;
1942 else if (response.IsOKResponse())
Greg Clayton2a48f522011-05-14 01:50:35 +00001943 return true;
Greg Clayton17a0cb62011-05-15 23:46:54 +00001944 }
1945 else
1946 {
1947 m_supports_alloc_dealloc_memory = eLazyBoolNo;
Greg Clayton2a48f522011-05-14 01:50:35 +00001948 }
Greg Clayton576d8832011-03-22 04:00:09 +00001949 }
1950 return false;
1951}
1952
Jim Inghamacff8952013-05-02 00:27:30 +00001953Error
1954GDBRemoteCommunicationClient::Detach (bool keep_stopped)
Greg Clayton37a0a242012-04-11 00:24:49 +00001955{
Jim Inghamacff8952013-05-02 00:27:30 +00001956 Error error;
1957
1958 if (keep_stopped)
1959 {
1960 if (m_supports_detach_stay_stopped == eLazyBoolCalculate)
1961 {
1962 char packet[64];
1963 const int packet_len = ::snprintf(packet, sizeof(packet), "qSupportsDetachAndStayStopped:");
Andy Gibbsa297a972013-06-19 19:04:53 +00001964 assert (packet_len < (int)sizeof(packet));
Jim Inghamacff8952013-05-02 00:27:30 +00001965 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00001966 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Jim Inghamacff8952013-05-02 00:27:30 +00001967 {
1968 m_supports_detach_stay_stopped = eLazyBoolYes;
1969 }
1970 else
1971 {
1972 m_supports_detach_stay_stopped = eLazyBoolNo;
1973 }
1974 }
1975
1976 if (m_supports_detach_stay_stopped == eLazyBoolNo)
1977 {
1978 error.SetErrorString("Stays stopped not supported by this target.");
1979 return error;
1980 }
1981 else
1982 {
Jim Ingham6c8824d2014-03-28 20:00:07 +00001983 StringExtractorGDBRemote response;
1984 PacketResult packet_result = SendPacketAndWaitForResponse ("D1", 1, response, false);
Greg Clayton3dedae12013-12-06 21:45:27 +00001985 if (packet_result != PacketResult::Success)
Jim Inghamacff8952013-05-02 00:27:30 +00001986 error.SetErrorString ("Sending extended disconnect packet failed.");
1987 }
1988 }
1989 else
1990 {
Jim Ingham6c8824d2014-03-28 20:00:07 +00001991 StringExtractorGDBRemote response;
1992 PacketResult packet_result = SendPacketAndWaitForResponse ("D", 1, response, false);
Greg Clayton3dedae12013-12-06 21:45:27 +00001993 if (packet_result != PacketResult::Success)
Jim Inghamacff8952013-05-02 00:27:30 +00001994 error.SetErrorString ("Sending disconnect packet failed.");
1995 }
1996 return error;
Greg Clayton37a0a242012-04-11 00:24:49 +00001997}
1998
Greg Clayton46fb5582011-11-18 07:03:08 +00001999Error
2000GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr,
2001 lldb_private::MemoryRegionInfo &region_info)
2002{
2003 Error error;
2004 region_info.Clear();
2005
2006 if (m_supports_memory_region_info != eLazyBoolNo)
2007 {
2008 m_supports_memory_region_info = eLazyBoolYes;
2009 char packet[64];
Daniel Malead01b2952012-11-29 21:49:15 +00002010 const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%" PRIx64, (uint64_t)addr);
Andy Gibbsa297a972013-06-19 19:04:53 +00002011 assert (packet_len < (int)sizeof(packet));
Greg Clayton46fb5582011-11-18 07:03:08 +00002012 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002013 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton46fb5582011-11-18 07:03:08 +00002014 {
2015 std::string name;
2016 std::string value;
2017 addr_t addr_value;
2018 bool success = true;
Jason Molendacb349ee2011-12-13 05:39:38 +00002019 bool saw_permissions = false;
Greg Clayton46fb5582011-11-18 07:03:08 +00002020 while (success && response.GetNameColonValue(name, value))
2021 {
2022 if (name.compare ("start") == 0)
2023 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002024 addr_value = StringConvert::ToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success);
Greg Clayton46fb5582011-11-18 07:03:08 +00002025 if (success)
2026 region_info.GetRange().SetRangeBase(addr_value);
2027 }
2028 else if (name.compare ("size") == 0)
2029 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002030 addr_value = StringConvert::ToUInt64(value.c_str(), 0, 16, &success);
Greg Clayton46fb5582011-11-18 07:03:08 +00002031 if (success)
2032 region_info.GetRange().SetByteSize (addr_value);
2033 }
Jason Molendacb349ee2011-12-13 05:39:38 +00002034 else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid())
Greg Clayton46fb5582011-11-18 07:03:08 +00002035 {
Jason Molendacb349ee2011-12-13 05:39:38 +00002036 saw_permissions = true;
2037 if (region_info.GetRange().Contains (addr))
2038 {
2039 if (value.find('r') != std::string::npos)
2040 region_info.SetReadable (MemoryRegionInfo::eYes);
2041 else
2042 region_info.SetReadable (MemoryRegionInfo::eNo);
2043
2044 if (value.find('w') != std::string::npos)
2045 region_info.SetWritable (MemoryRegionInfo::eYes);
2046 else
2047 region_info.SetWritable (MemoryRegionInfo::eNo);
2048
2049 if (value.find('x') != std::string::npos)
2050 region_info.SetExecutable (MemoryRegionInfo::eYes);
2051 else
2052 region_info.SetExecutable (MemoryRegionInfo::eNo);
2053 }
2054 else
2055 {
2056 // The reported region does not contain this address -- we're looking at an unmapped page
2057 region_info.SetReadable (MemoryRegionInfo::eNo);
2058 region_info.SetWritable (MemoryRegionInfo::eNo);
2059 region_info.SetExecutable (MemoryRegionInfo::eNo);
2060 }
Greg Clayton46fb5582011-11-18 07:03:08 +00002061 }
2062 else if (name.compare ("error") == 0)
2063 {
2064 StringExtractorGDBRemote name_extractor;
2065 // Swap "value" over into "name_extractor"
2066 name_extractor.GetStringRef().swap(value);
2067 // Now convert the HEX bytes into a string value
2068 name_extractor.GetHexByteString (value);
2069 error.SetErrorString(value.c_str());
2070 }
2071 }
Jason Molendacb349ee2011-12-13 05:39:38 +00002072
2073 // We got a valid address range back but no permissions -- which means this is an unmapped page
2074 if (region_info.GetRange().IsValid() && saw_permissions == false)
2075 {
2076 region_info.SetReadable (MemoryRegionInfo::eNo);
2077 region_info.SetWritable (MemoryRegionInfo::eNo);
2078 region_info.SetExecutable (MemoryRegionInfo::eNo);
2079 }
Greg Clayton46fb5582011-11-18 07:03:08 +00002080 }
2081 else
2082 {
2083 m_supports_memory_region_info = eLazyBoolNo;
2084 }
2085 }
2086
2087 if (m_supports_memory_region_info == eLazyBoolNo)
2088 {
2089 error.SetErrorString("qMemoryRegionInfo is not supported");
2090 }
2091 if (error.Fail())
2092 region_info.Clear();
2093 return error;
2094
2095}
2096
Johnny Chen64637202012-05-23 21:09:52 +00002097Error
2098GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num)
2099{
2100 Error error;
2101
2102 if (m_supports_watchpoint_support_info == eLazyBoolYes)
2103 {
2104 num = m_num_supported_hardware_watchpoints;
2105 return error;
2106 }
2107
2108 // Set num to 0 first.
2109 num = 0;
2110 if (m_supports_watchpoint_support_info != eLazyBoolNo)
2111 {
2112 char packet[64];
2113 const int packet_len = ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:");
Andy Gibbsa297a972013-06-19 19:04:53 +00002114 assert (packet_len < (int)sizeof(packet));
Johnny Chen64637202012-05-23 21:09:52 +00002115 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002116 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Johnny Chen64637202012-05-23 21:09:52 +00002117 {
2118 m_supports_watchpoint_support_info = eLazyBoolYes;
2119 std::string name;
2120 std::string value;
2121 while (response.GetNameColonValue(name, value))
2122 {
2123 if (name.compare ("num") == 0)
2124 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002125 num = StringConvert::ToUInt32(value.c_str(), 0, 0);
Johnny Chen64637202012-05-23 21:09:52 +00002126 m_num_supported_hardware_watchpoints = num;
2127 }
2128 }
2129 }
2130 else
2131 {
2132 m_supports_watchpoint_support_info = eLazyBoolNo;
2133 }
2134 }
2135
2136 if (m_supports_watchpoint_support_info == eLazyBoolNo)
2137 {
2138 error.SetErrorString("qWatchpointSupportInfo is not supported");
2139 }
2140 return error;
2141
2142}
Greg Clayton46fb5582011-11-18 07:03:08 +00002143
Enrico Granataf04a2192012-07-13 23:18:48 +00002144lldb_private::Error
2145GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num, bool& after)
2146{
2147 Error error(GetWatchpointSupportInfo(num));
2148 if (error.Success())
2149 error = GetWatchpointsTriggerAfterInstruction(after);
2150 return error;
2151}
2152
2153lldb_private::Error
2154GDBRemoteCommunicationClient::GetWatchpointsTriggerAfterInstruction (bool &after)
2155{
2156 Error error;
2157
2158 // we assume watchpoints will happen after running the relevant opcode
2159 // and we only want to override this behavior if we have explicitly
2160 // received a qHostInfo telling us otherwise
2161 if (m_qHostInfo_is_valid != eLazyBoolYes)
2162 after = true;
2163 else
2164 after = (m_watchpoints_trigger_after_instruction != eLazyBoolNo);
2165 return error;
2166}
2167
Greg Clayton576d8832011-03-22 04:00:09 +00002168int
2169GDBRemoteCommunicationClient::SetSTDIN (char const *path)
2170{
2171 if (path && path[0])
2172 {
2173 StreamString packet;
2174 packet.PutCString("QSetSTDIN:");
2175 packet.PutBytesAsRawHex8(path, strlen(path));
2176
2177 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002178 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002179 {
2180 if (response.IsOKResponse())
2181 return 0;
2182 uint8_t error = response.GetError();
2183 if (error)
2184 return error;
2185 }
2186 }
2187 return -1;
2188}
2189
2190int
2191GDBRemoteCommunicationClient::SetSTDOUT (char const *path)
2192{
2193 if (path && path[0])
2194 {
2195 StreamString packet;
2196 packet.PutCString("QSetSTDOUT:");
2197 packet.PutBytesAsRawHex8(path, strlen(path));
2198
2199 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002200 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002201 {
2202 if (response.IsOKResponse())
2203 return 0;
2204 uint8_t error = response.GetError();
2205 if (error)
2206 return error;
2207 }
2208 }
2209 return -1;
2210}
2211
2212int
2213GDBRemoteCommunicationClient::SetSTDERR (char const *path)
2214{
2215 if (path && path[0])
2216 {
2217 StreamString packet;
2218 packet.PutCString("QSetSTDERR:");
2219 packet.PutBytesAsRawHex8(path, strlen(path));
2220
2221 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002222 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002223 {
2224 if (response.IsOKResponse())
2225 return 0;
2226 uint8_t error = response.GetError();
2227 if (error)
2228 return error;
2229 }
2230 }
2231 return -1;
2232}
2233
Greg Claytonfbb76342013-11-20 21:07:01 +00002234bool
2235GDBRemoteCommunicationClient::GetWorkingDir (std::string &cwd)
2236{
2237 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002238 if (SendPacketAndWaitForResponse ("qGetWorkingDir", response, false) == PacketResult::Success)
Greg Claytonfbb76342013-11-20 21:07:01 +00002239 {
2240 if (response.IsUnsupportedResponse())
2241 return false;
2242 if (response.IsErrorResponse())
2243 return false;
2244 response.GetHexByteString (cwd);
2245 return !cwd.empty();
2246 }
2247 return false;
2248}
2249
Greg Clayton576d8832011-03-22 04:00:09 +00002250int
2251GDBRemoteCommunicationClient::SetWorkingDir (char const *path)
2252{
2253 if (path && path[0])
2254 {
2255 StreamString packet;
2256 packet.PutCString("QSetWorkingDir:");
2257 packet.PutBytesAsRawHex8(path, strlen(path));
2258
2259 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002260 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002261 {
2262 if (response.IsOKResponse())
2263 return 0;
2264 uint8_t error = response.GetError();
2265 if (error)
2266 return error;
2267 }
2268 }
2269 return -1;
2270}
2271
2272int
2273GDBRemoteCommunicationClient::SetDisableASLR (bool enable)
2274{
Greg Clayton32e0a752011-03-30 18:16:51 +00002275 char packet[32];
2276 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0);
Andy Gibbsa297a972013-06-19 19:04:53 +00002277 assert (packet_len < (int)sizeof(packet));
Greg Clayton576d8832011-03-22 04:00:09 +00002278 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002279 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton576d8832011-03-22 04:00:09 +00002280 {
2281 if (response.IsOKResponse())
2282 return 0;
2283 uint8_t error = response.GetError();
2284 if (error)
2285 return error;
2286 }
2287 return -1;
2288}
Greg Clayton32e0a752011-03-30 18:16:51 +00002289
Jim Ingham106d0282014-06-25 02:32:56 +00002290int
2291GDBRemoteCommunicationClient::SetDetachOnError (bool enable)
2292{
2293 char packet[32];
2294 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDetachOnError:%i", enable ? 1 : 0);
2295 assert (packet_len < (int)sizeof(packet));
2296 StringExtractorGDBRemote response;
2297 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
2298 {
2299 if (response.IsOKResponse())
2300 return 0;
2301 uint8_t error = response.GetError();
2302 if (error)
2303 return error;
2304 }
2305 return -1;
2306}
2307
2308
Greg Clayton32e0a752011-03-30 18:16:51 +00002309bool
Greg Clayton8b82f082011-04-12 05:54:46 +00002310GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00002311{
2312 if (response.IsNormalResponse())
2313 {
2314 std::string name;
2315 std::string value;
2316 StringExtractor extractor;
Jason Molenda89c37492014-01-27 22:23:20 +00002317
2318 uint32_t cpu = LLDB_INVALID_CPUTYPE;
2319 uint32_t sub = 0;
2320 std::string vendor;
2321 std::string os_type;
Greg Clayton32e0a752011-03-30 18:16:51 +00002322
2323 while (response.GetNameColonValue(name, value))
2324 {
2325 if (name.compare("pid") == 0)
2326 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002327 process_info.SetProcessID (StringConvert::ToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002328 }
2329 else if (name.compare("ppid") == 0)
2330 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002331 process_info.SetParentProcessID (StringConvert::ToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002332 }
2333 else if (name.compare("uid") == 0)
2334 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002335 process_info.SetUserID (StringConvert::ToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002336 }
2337 else if (name.compare("euid") == 0)
2338 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002339 process_info.SetEffectiveUserID (StringConvert::ToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002340 }
2341 else if (name.compare("gid") == 0)
2342 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002343 process_info.SetGroupID (StringConvert::ToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002344 }
2345 else if (name.compare("egid") == 0)
2346 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002347 process_info.SetEffectiveGroupID (StringConvert::ToUInt32 (value.c_str(), UINT32_MAX, 0));
Greg Clayton32e0a752011-03-30 18:16:51 +00002348 }
2349 else if (name.compare("triple") == 0)
2350 {
Greg Clayton44272a42014-09-18 00:18:32 +00002351 StringExtractor extractor;
2352 extractor.GetStringRef().swap(value);
2353 extractor.SetFilePos(0);
2354 extractor.GetHexByteString (value);
Greg Clayton70512312012-05-08 01:45:38 +00002355 process_info.GetArchitecture ().SetTriple (value.c_str());
Greg Clayton32e0a752011-03-30 18:16:51 +00002356 }
2357 else if (name.compare("name") == 0)
2358 {
2359 StringExtractor extractor;
Filipe Cabecinhasf86cf782012-05-07 09:30:51 +00002360 // The process name from ASCII hex bytes since we can't
Greg Clayton32e0a752011-03-30 18:16:51 +00002361 // control the characters in a process name
2362 extractor.GetStringRef().swap(value);
2363 extractor.SetFilePos(0);
2364 extractor.GetHexByteString (value);
Greg Clayton144f3a92011-11-15 03:53:30 +00002365 process_info.GetExecutableFile().SetFile (value.c_str(), false);
Greg Clayton32e0a752011-03-30 18:16:51 +00002366 }
Jason Molenda89c37492014-01-27 22:23:20 +00002367 else if (name.compare("cputype") == 0)
2368 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002369 cpu = StringConvert::ToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 16);
Jason Molenda89c37492014-01-27 22:23:20 +00002370 }
2371 else if (name.compare("cpusubtype") == 0)
2372 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002373 sub = StringConvert::ToUInt32 (value.c_str(), 0, 16);
Jason Molenda89c37492014-01-27 22:23:20 +00002374 }
2375 else if (name.compare("vendor") == 0)
2376 {
2377 vendor = value;
2378 }
2379 else if (name.compare("ostype") == 0)
2380 {
2381 os_type = value;
2382 }
2383 }
2384
2385 if (cpu != LLDB_INVALID_CPUTYPE && !vendor.empty() && !os_type.empty())
2386 {
2387 if (vendor == "apple")
2388 {
2389 process_info.GetArchitecture().SetArchitecture (eArchTypeMachO, cpu, sub);
2390 process_info.GetArchitecture().GetTriple().setVendorName (llvm::StringRef (vendor));
2391 process_info.GetArchitecture().GetTriple().setOSName (llvm::StringRef (os_type));
2392 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002393 }
2394
2395 if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
2396 return true;
2397 }
2398 return false;
2399}
2400
2401bool
Greg Clayton8b82f082011-04-12 05:54:46 +00002402GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info)
Greg Clayton32e0a752011-03-30 18:16:51 +00002403{
2404 process_info.Clear();
2405
2406 if (m_supports_qProcessInfoPID)
2407 {
2408 char packet[32];
Daniel Malead01b2952012-11-29 21:49:15 +00002409 const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%" PRIu64, pid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002410 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00002411 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002412 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002413 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002414 return DecodeProcessInfoResponse (response, process_info);
2415 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002416 else
2417 {
2418 m_supports_qProcessInfoPID = false;
2419 return false;
2420 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002421 }
2422 return false;
2423}
2424
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002425bool
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00002426GDBRemoteCommunicationClient::GetCurrentProcessInfo (bool allow_lazy)
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002427{
Todd Fiala3daa1762014-09-15 16:01:29 +00002428 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
2429
Oleksiy Vyalov1ef7b2c2015-02-04 23:19:15 +00002430 if (allow_lazy)
2431 {
2432 if (m_qProcessInfo_is_valid == eLazyBoolYes)
2433 return true;
2434 if (m_qProcessInfo_is_valid == eLazyBoolNo)
2435 return false;
2436 }
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002437
2438 GetHostInfo ();
2439
2440 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002441 if (SendPacketAndWaitForResponse ("qProcessInfo", response, false) == PacketResult::Success)
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002442 {
2443 if (response.IsNormalResponse())
2444 {
2445 std::string name;
2446 std::string value;
2447 uint32_t cpu = LLDB_INVALID_CPUTYPE;
2448 uint32_t sub = 0;
2449 std::string arch_name;
2450 std::string os_name;
2451 std::string vendor_name;
2452 std::string triple;
2453 uint32_t pointer_byte_size = 0;
2454 StringExtractor extractor;
Todd Fiala5c9d5bf2014-09-15 15:31:11 +00002455 ByteOrder byte_order = eByteOrderInvalid;
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002456 uint32_t num_keys_decoded = 0;
Todd Fiala9f72b3a2014-05-07 19:28:21 +00002457 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002458 while (response.GetNameColonValue(name, value))
2459 {
2460 if (name.compare("cputype") == 0)
2461 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002462 cpu = StringConvert::ToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 16);
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002463 if (cpu != LLDB_INVALID_CPUTYPE)
2464 ++num_keys_decoded;
2465 }
2466 else if (name.compare("cpusubtype") == 0)
2467 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002468 sub = StringConvert::ToUInt32 (value.c_str(), 0, 16);
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002469 if (sub != 0)
2470 ++num_keys_decoded;
2471 }
Todd Fialac540dd02014-08-26 18:21:02 +00002472 else if (name.compare("triple") == 0)
2473 {
Greg Clayton44272a42014-09-18 00:18:32 +00002474 StringExtractor extractor;
2475 extractor.GetStringRef().swap(value);
2476 extractor.SetFilePos(0);
2477 extractor.GetHexByteString (triple);
Todd Fialac540dd02014-08-26 18:21:02 +00002478 ++num_keys_decoded;
2479 }
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002480 else if (name.compare("ostype") == 0)
2481 {
2482 os_name.swap (value);
2483 ++num_keys_decoded;
2484 }
2485 else if (name.compare("vendor") == 0)
2486 {
2487 vendor_name.swap(value);
2488 ++num_keys_decoded;
2489 }
2490 else if (name.compare("endian") == 0)
2491 {
Todd Fiala5c9d5bf2014-09-15 15:31:11 +00002492 ++num_keys_decoded;
2493 if (value.compare("little") == 0)
2494 byte_order = eByteOrderLittle;
2495 else if (value.compare("big") == 0)
2496 byte_order = eByteOrderBig;
2497 else if (value.compare("pdp") == 0)
2498 byte_order = eByteOrderPDP;
2499 else
2500 --num_keys_decoded;
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002501 }
2502 else if (name.compare("ptrsize") == 0)
2503 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002504 pointer_byte_size = StringConvert::ToUInt32 (value.c_str(), 0, 16);
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002505 if (pointer_byte_size != 0)
2506 ++num_keys_decoded;
2507 }
Todd Fiala9f72b3a2014-05-07 19:28:21 +00002508 else if (name.compare("pid") == 0)
2509 {
Vince Harron5275aaa2015-01-15 20:08:35 +00002510 pid = StringConvert::ToUInt64(value.c_str(), 0, 16);
Todd Fiala9f72b3a2014-05-07 19:28:21 +00002511 if (pid != LLDB_INVALID_PROCESS_ID)
2512 ++num_keys_decoded;
2513 }
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002514 }
2515 if (num_keys_decoded > 0)
2516 m_qProcessInfo_is_valid = eLazyBoolYes;
Todd Fiala9f72b3a2014-05-07 19:28:21 +00002517 if (pid != LLDB_INVALID_PROCESS_ID)
2518 {
2519 m_curr_pid_is_valid = eLazyBoolYes;
2520 m_curr_pid = pid;
2521 }
Todd Fialac540dd02014-08-26 18:21:02 +00002522
2523 // Set the ArchSpec from the triple if we have it.
2524 if (!triple.empty ())
2525 {
2526 m_process_arch.SetTriple (triple.c_str ());
2527 if (pointer_byte_size)
2528 {
2529 assert (pointer_byte_size == m_process_arch.GetAddressByteSize());
2530 }
2531 }
2532 else if (cpu != LLDB_INVALID_CPUTYPE && !os_name.empty() && !vendor_name.empty())
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002533 {
Todd Fiala3daa1762014-09-15 16:01:29 +00002534 llvm::Triple triple(llvm::Twine("-") + vendor_name + "-" + os_name);
2535
2536 assert(triple.getObjectFormat() != llvm::Triple::UnknownObjectFormat);
2537 switch (triple.getObjectFormat()) {
2538 case llvm::Triple::MachO:
2539 m_process_arch.SetArchitecture (eArchTypeMachO, cpu, sub);
2540 break;
2541 case llvm::Triple::ELF:
2542 m_process_arch.SetArchitecture (eArchTypeELF, cpu, sub);
2543 break;
2544 case llvm::Triple::COFF:
2545 m_process_arch.SetArchitecture (eArchTypeCOFF, cpu, sub);
2546 break;
2547 case llvm::Triple::UnknownObjectFormat:
2548 if (log)
2549 log->Printf("error: failed to determine target architecture");
2550 return false;
2551 }
2552
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002553 if (pointer_byte_size)
2554 {
2555 assert (pointer_byte_size == m_process_arch.GetAddressByteSize());
2556 }
Todd Fiala5c9d5bf2014-09-15 15:31:11 +00002557 if (byte_order != eByteOrderInvalid)
2558 {
2559 assert (byte_order == m_process_arch.GetByteOrder());
2560 }
Todd Fiala0cc371c2014-09-05 14:56:13 +00002561 m_process_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
Greg Clayton7ab7f892014-05-29 21:33:45 +00002562 m_process_arch.GetTriple().setOSName(llvm::StringRef (os_name));
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002563 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name));
2564 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name));
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002565 }
Greg Clayton7ab7f892014-05-29 21:33:45 +00002566 return true;
Jason Molendaf17b5ac2012-12-19 02:54:03 +00002567 }
2568 }
2569 else
2570 {
2571 m_qProcessInfo_is_valid = eLazyBoolNo;
2572 }
2573
2574 return false;
2575}
2576
2577
Greg Clayton32e0a752011-03-30 18:16:51 +00002578uint32_t
Greg Clayton8b82f082011-04-12 05:54:46 +00002579GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info,
2580 ProcessInstanceInfoList &process_infos)
Greg Clayton32e0a752011-03-30 18:16:51 +00002581{
2582 process_infos.Clear();
2583
2584 if (m_supports_qfProcessInfo)
2585 {
2586 StreamString packet;
2587 packet.PutCString ("qfProcessInfo");
2588 if (!match_info.MatchAllProcesses())
2589 {
2590 packet.PutChar (':');
2591 const char *name = match_info.GetProcessInfo().GetName();
2592 bool has_name_match = false;
2593 if (name && name[0])
2594 {
2595 has_name_match = true;
2596 NameMatchType name_match_type = match_info.GetNameMatchType();
2597 switch (name_match_type)
2598 {
2599 case eNameMatchIgnore:
2600 has_name_match = false;
2601 break;
2602
2603 case eNameMatchEquals:
2604 packet.PutCString ("name_match:equals;");
2605 break;
2606
2607 case eNameMatchContains:
2608 packet.PutCString ("name_match:contains;");
2609 break;
2610
2611 case eNameMatchStartsWith:
2612 packet.PutCString ("name_match:starts_with;");
2613 break;
2614
2615 case eNameMatchEndsWith:
2616 packet.PutCString ("name_match:ends_with;");
2617 break;
2618
2619 case eNameMatchRegularExpression:
2620 packet.PutCString ("name_match:regex;");
2621 break;
2622 }
2623 if (has_name_match)
2624 {
2625 packet.PutCString ("name:");
2626 packet.PutBytesAsRawHex8(name, ::strlen(name));
2627 packet.PutChar (';');
2628 }
2629 }
2630
2631 if (match_info.GetProcessInfo().ProcessIDIsValid())
Daniel Malead01b2952012-11-29 21:49:15 +00002632 packet.Printf("pid:%" PRIu64 ";",match_info.GetProcessInfo().GetProcessID());
Greg Clayton32e0a752011-03-30 18:16:51 +00002633 if (match_info.GetProcessInfo().ParentProcessIDIsValid())
Daniel Malead01b2952012-11-29 21:49:15 +00002634 packet.Printf("parent_pid:%" PRIu64 ";",match_info.GetProcessInfo().GetParentProcessID());
Greg Clayton8b82f082011-04-12 05:54:46 +00002635 if (match_info.GetProcessInfo().UserIDIsValid())
2636 packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID());
2637 if (match_info.GetProcessInfo().GroupIDIsValid())
2638 packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID());
Greg Clayton32e0a752011-03-30 18:16:51 +00002639 if (match_info.GetProcessInfo().EffectiveUserIDIsValid())
2640 packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID());
2641 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
2642 packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID());
2643 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid())
2644 packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0);
2645 if (match_info.GetProcessInfo().GetArchitecture().IsValid())
2646 {
2647 const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture();
2648 const llvm::Triple &triple = match_arch.GetTriple();
2649 packet.PutCString("triple:");
Matthew Gardinerf39ebbe2014-08-01 05:12:23 +00002650 packet.PutCString(triple.getTriple().c_str());
Greg Clayton32e0a752011-03-30 18:16:51 +00002651 packet.PutChar (';');
2652 }
2653 }
2654 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002655 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002656 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002657 do
2658 {
Greg Clayton8b82f082011-04-12 05:54:46 +00002659 ProcessInstanceInfo process_info;
Greg Clayton32e0a752011-03-30 18:16:51 +00002660 if (!DecodeProcessInfoResponse (response, process_info))
2661 break;
2662 process_infos.Append(process_info);
2663 response.GetStringRef().clear();
2664 response.SetFilePos(0);
Greg Clayton3dedae12013-12-06 21:45:27 +00002665 } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false) == PacketResult::Success);
Greg Clayton32e0a752011-03-30 18:16:51 +00002666 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002667 else
2668 {
2669 m_supports_qfProcessInfo = false;
2670 return 0;
2671 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002672 }
2673 return process_infos.GetSize();
2674
2675}
2676
2677bool
2678GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name)
2679{
2680 if (m_supports_qUserName)
2681 {
2682 char packet[32];
2683 const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002684 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00002685 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002686 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002687 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002688 if (response.IsNormalResponse())
2689 {
2690 // Make sure we parsed the right number of characters. The response is
2691 // the hex encoded user name and should make up the entire packet.
2692 // If there are any non-hex ASCII bytes, the length won't match below..
2693 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
2694 return true;
2695 }
2696 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002697 else
2698 {
2699 m_supports_qUserName = false;
2700 return false;
2701 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002702 }
2703 return false;
2704
2705}
2706
2707bool
2708GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name)
2709{
2710 if (m_supports_qGroupName)
2711 {
2712 char packet[32];
2713 const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002714 assert (packet_len < (int)sizeof(packet));
Greg Clayton32e0a752011-03-30 18:16:51 +00002715 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002716 if (SendPacketAndWaitForResponse (packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton32e0a752011-03-30 18:16:51 +00002717 {
Greg Clayton32e0a752011-03-30 18:16:51 +00002718 if (response.IsNormalResponse())
2719 {
2720 // Make sure we parsed the right number of characters. The response is
2721 // the hex encoded group name and should make up the entire packet.
2722 // If there are any non-hex ASCII bytes, the length won't match below..
2723 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size())
2724 return true;
2725 }
2726 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002727 else
2728 {
2729 m_supports_qGroupName = false;
2730 return false;
2731 }
Greg Clayton32e0a752011-03-30 18:16:51 +00002732 }
2733 return false;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002734}
Greg Clayton32e0a752011-03-30 18:16:51 +00002735
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002736void
2737GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets)
2738{
2739 uint32_t i;
2740 TimeValue start_time, end_time;
2741 uint64_t total_time_nsec;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002742 if (SendSpeedTestPacket (0, 0))
2743 {
Greg Clayton700e5082014-02-21 19:11:28 +00002744 static uint32_t g_send_sizes[] = { 0, 64, 128, 512, 1024 };
2745 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 +00002746 const size_t k_num_send_sizes = llvm::array_lengthof(g_send_sizes);
2747 const size_t k_num_recv_sizes = llvm::array_lengthof(g_recv_sizes);
Greg Clayton700e5082014-02-21 19:11:28 +00002748 const uint64_t k_recv_amount = 4*1024*1024; // Receive 4MB
2749 for (uint32_t send_idx = 0; send_idx < k_num_send_sizes; ++send_idx)
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002750 {
Greg Clayton700e5082014-02-21 19:11:28 +00002751 const uint32_t send_size = g_send_sizes[send_idx];
2752 for (uint32_t recv_idx = 0; recv_idx < k_num_recv_sizes; ++recv_idx)
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002753 {
Greg Clayton700e5082014-02-21 19:11:28 +00002754 const uint32_t recv_size = g_recv_sizes[recv_idx];
2755 StreamString packet;
2756 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size);
2757 uint32_t bytes_left = send_size;
2758 while (bytes_left > 0)
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002759 {
Greg Clayton700e5082014-02-21 19:11:28 +00002760 if (bytes_left >= 26)
2761 {
2762 packet.PutCString("abcdefghijklmnopqrstuvwxyz");
2763 bytes_left -= 26;
2764 }
2765 else
2766 {
2767 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz");
2768 bytes_left = 0;
2769 }
2770 }
2771
2772 start_time = TimeValue::Now();
2773 if (recv_size == 0)
2774 {
2775 for (i=0; i<num_packets; ++i)
2776 {
2777 StringExtractorGDBRemote response;
2778 SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false);
2779 }
2780 }
2781 else
2782 {
2783 uint32_t bytes_read = 0;
2784 while (bytes_read < k_recv_amount)
2785 {
2786 StringExtractorGDBRemote response;
2787 SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false);
2788 bytes_read += recv_size;
2789 }
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002790 }
2791 end_time = TimeValue::Now();
2792 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970();
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002793 if (recv_size == 0)
Greg Clayton700e5082014-02-21 19:11:28 +00002794 {
2795 float packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec;
2796 printf ("%u qSpeedTest(send=%-7u, recv=%-7u) in %" PRIu64 ".%9.9" PRIu64 " sec for %f packets/sec.\n",
2797 num_packets,
2798 send_size,
2799 recv_size,
2800 total_time_nsec / TimeValue::NanoSecPerSec,
2801 total_time_nsec % TimeValue::NanoSecPerSec,
2802 packets_per_second);
2803 }
2804 else
2805 {
2806 float mb_second = ((((float)k_recv_amount)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec) / (1024.0*1024.0);
2807 printf ("%u qSpeedTest(send=%-7u, recv=%-7u) sent 4MB in %" PRIu64 ".%9.9" PRIu64 " sec for %f MB/sec.\n",
2808 num_packets,
2809 send_size,
2810 recv_size,
2811 total_time_nsec / TimeValue::NanoSecPerSec,
2812 total_time_nsec % TimeValue::NanoSecPerSec,
2813 mb_second);
2814 }
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002815 }
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002816 }
2817 }
Greg Clayton9b1e1cd2011-04-04 18:18:57 +00002818}
2819
2820bool
2821GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size)
2822{
2823 StreamString packet;
2824 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size);
2825 uint32_t bytes_left = send_size;
2826 while (bytes_left > 0)
2827 {
2828 if (bytes_left >= 26)
2829 {
2830 packet.PutCString("abcdefghijklmnopqrstuvwxyz");
2831 bytes_left -= 26;
2832 }
2833 else
2834 {
2835 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz");
2836 bytes_left = 0;
2837 }
2838 }
2839
2840 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002841 return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) == PacketResult::Success;
Greg Clayton32e0a752011-03-30 18:16:51 +00002842}
Greg Clayton8b82f082011-04-12 05:54:46 +00002843
2844uint16_t
Greg Claytondbf04572013-12-04 19:40:33 +00002845GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort (lldb::pid_t &pid, const char *remote_accept_hostname)
Greg Clayton8b82f082011-04-12 05:54:46 +00002846{
Daniel Maleae0f8f572013-08-26 23:57:52 +00002847 pid = LLDB_INVALID_PROCESS_ID;
Greg Clayton8b82f082011-04-12 05:54:46 +00002848 StringExtractorGDBRemote response;
Daniel Maleae0f8f572013-08-26 23:57:52 +00002849 StreamString stream;
Greg Clayton29b8fc42013-11-21 01:44:58 +00002850 stream.PutCString("qLaunchGDBServer;");
Daniel Maleae0f8f572013-08-26 23:57:52 +00002851 std::string hostname;
Greg Claytondbf04572013-12-04 19:40:33 +00002852 if (remote_accept_hostname && remote_accept_hostname[0])
2853 hostname = remote_accept_hostname;
Daniel Maleae0f8f572013-08-26 23:57:52 +00002854 else
2855 {
Zachary Turner97a14e62014-08-19 17:18:29 +00002856 if (HostInfo::GetHostname(hostname))
Greg Claytondbf04572013-12-04 19:40:33 +00002857 {
2858 // Make the GDB server we launch only accept connections from this host
2859 stream.Printf("host:%s;", hostname.c_str());
2860 }
2861 else
2862 {
2863 // Make the GDB server we launch accept connections from any host since we can't figure out the hostname
2864 stream.Printf("host:*;");
2865 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00002866 }
2867 const char *packet = stream.GetData();
2868 int packet_len = stream.GetSize();
2869
Vince Harron1b5a74e2015-01-21 22:42:49 +00002870 // give the process a few seconds to startup
Tamas Berghammer912800c2015-02-24 10:23:39 +00002871 GDBRemoteCommunication::ScopedTimeout timeout (*this, 10);
2872
2873 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002874 {
2875 std::string name;
2876 std::string value;
2877 uint16_t port = 0;
Greg Clayton8b82f082011-04-12 05:54:46 +00002878 while (response.GetNameColonValue(name, value))
2879 {
Daniel Maleae0f8f572013-08-26 23:57:52 +00002880 if (name.compare("port") == 0)
Vince Harron5275aaa2015-01-15 20:08:35 +00002881 port = StringConvert::ToUInt32(value.c_str(), 0, 0);
Daniel Maleae0f8f572013-08-26 23:57:52 +00002882 else if (name.compare("pid") == 0)
Vince Harron5275aaa2015-01-15 20:08:35 +00002883 pid = StringConvert::ToUInt64(value.c_str(), LLDB_INVALID_PROCESS_ID, 0);
Greg Clayton8b82f082011-04-12 05:54:46 +00002884 }
2885 return port;
2886 }
2887 return 0;
2888}
2889
2890bool
Daniel Maleae0f8f572013-08-26 23:57:52 +00002891GDBRemoteCommunicationClient::KillSpawnedProcess (lldb::pid_t pid)
2892{
2893 StreamString stream;
2894 stream.Printf ("qKillSpawnedProcess:%" PRId64 , pid);
2895 const char *packet = stream.GetData();
2896 int packet_len = stream.GetSize();
Sylvestre Ledrufd654c42013-10-06 09:51:02 +00002897
Daniel Maleae0f8f572013-08-26 23:57:52 +00002898 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002899 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00002900 {
2901 if (response.IsOKResponse())
2902 return true;
2903 }
2904 return false;
2905}
2906
2907bool
Jason Molendae9ca4af2013-02-23 02:04:45 +00002908GDBRemoteCommunicationClient::SetCurrentThread (uint64_t tid)
Greg Clayton8b82f082011-04-12 05:54:46 +00002909{
2910 if (m_curr_tid == tid)
2911 return true;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002912
Greg Clayton8b82f082011-04-12 05:54:46 +00002913 char packet[32];
2914 int packet_len;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002915 if (tid == UINT64_MAX)
2916 packet_len = ::snprintf (packet, sizeof(packet), "Hg-1");
Greg Clayton8b82f082011-04-12 05:54:46 +00002917 else
Jason Molendae9ca4af2013-02-23 02:04:45 +00002918 packet_len = ::snprintf (packet, sizeof(packet), "Hg%" PRIx64, tid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002919 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002920 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002921 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002922 {
2923 if (response.IsOKResponse())
2924 {
2925 m_curr_tid = tid;
2926 return true;
2927 }
2928 }
2929 return false;
2930}
2931
2932bool
Jason Molendae9ca4af2013-02-23 02:04:45 +00002933GDBRemoteCommunicationClient::SetCurrentThreadForRun (uint64_t tid)
Greg Clayton8b82f082011-04-12 05:54:46 +00002934{
2935 if (m_curr_tid_run == tid)
2936 return true;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002937
Greg Clayton8b82f082011-04-12 05:54:46 +00002938 char packet[32];
2939 int packet_len;
Jason Molendae9ca4af2013-02-23 02:04:45 +00002940 if (tid == UINT64_MAX)
2941 packet_len = ::snprintf (packet, sizeof(packet), "Hc-1");
Greg Clayton8b82f082011-04-12 05:54:46 +00002942 else
Jason Molendae9ca4af2013-02-23 02:04:45 +00002943 packet_len = ::snprintf (packet, sizeof(packet), "Hc%" PRIx64, tid);
2944
Andy Gibbsa297a972013-06-19 19:04:53 +00002945 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00002946 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00002947 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002948 {
2949 if (response.IsOKResponse())
2950 {
2951 m_curr_tid_run = tid;
2952 return true;
2953 }
2954 }
2955 return false;
2956}
2957
2958bool
2959GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response)
2960{
Greg Clayton3dedae12013-12-06 21:45:27 +00002961 if (SendPacketAndWaitForResponse("?", 1, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002962 return response.IsNormalResponse();
2963 return false;
2964}
2965
2966bool
Greg Claytonf402f782012-10-13 02:11:55 +00002967GDBRemoteCommunicationClient::GetThreadStopInfo (lldb::tid_t tid, StringExtractorGDBRemote &response)
Greg Clayton8b82f082011-04-12 05:54:46 +00002968{
2969 if (m_supports_qThreadStopInfo)
2970 {
2971 char packet[256];
Daniel Malead01b2952012-11-29 21:49:15 +00002972 int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%" PRIx64, tid);
Andy Gibbsa297a972013-06-19 19:04:53 +00002973 assert (packet_len < (int)sizeof(packet));
Greg Clayton3dedae12013-12-06 21:45:27 +00002974 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00002975 {
Greg Claytonef8180a2013-10-15 00:14:28 +00002976 if (response.IsUnsupportedResponse())
2977 m_supports_qThreadStopInfo = false;
2978 else if (response.IsNormalResponse())
Greg Clayton8b82f082011-04-12 05:54:46 +00002979 return true;
2980 else
2981 return false;
2982 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00002983 else
2984 {
2985 m_supports_qThreadStopInfo = false;
2986 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002987 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002988 return false;
2989}
2990
2991
2992uint8_t
2993GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length)
2994{
Todd Fiala616b8272014-10-09 00:55:04 +00002995 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
2996 if (log)
2997 log->Printf ("GDBRemoteCommunicationClient::%s() %s at addr = 0x%" PRIx64,
2998 __FUNCTION__, insert ? "add" : "remove", addr);
2999
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003000 // Check if the stub is known not to support this breakpoint type
3001 if (!SupportsGDBStoppointPacket(type))
3002 return UINT8_MAX;
3003 // Construct the breakpoint packet
Greg Clayton8b82f082011-04-12 05:54:46 +00003004 char packet[64];
3005 const int packet_len = ::snprintf (packet,
3006 sizeof(packet),
Daniel Malead01b2952012-11-29 21:49:15 +00003007 "%c%i,%" PRIx64 ",%x",
Greg Clayton8b82f082011-04-12 05:54:46 +00003008 insert ? 'Z' : 'z',
3009 type,
3010 addr,
3011 length);
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003012 // Check we haven't overwritten the end of the packet buffer
Andy Gibbsa297a972013-06-19 19:04:53 +00003013 assert (packet_len + 1 < (int)sizeof(packet));
Greg Clayton8b82f082011-04-12 05:54:46 +00003014 StringExtractorGDBRemote response;
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003015 // Try to send the breakpoint packet, and check that it was correctly sent
Greg Clayton3dedae12013-12-06 21:45:27 +00003016 if (SendPacketAndWaitForResponse(packet, packet_len, response, true) == PacketResult::Success)
Greg Clayton8b82f082011-04-12 05:54:46 +00003017 {
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003018 // Receive and OK packet when the breakpoint successfully placed
Greg Clayton8b82f082011-04-12 05:54:46 +00003019 if (response.IsOKResponse())
3020 return 0;
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003021
3022 // Error while setting breakpoint, send back specific error
3023 if (response.IsErrorResponse())
Greg Clayton8b82f082011-04-12 05:54:46 +00003024 return response.GetError();
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003025
3026 // Empty packet informs us that breakpoint is not supported
3027 if (response.IsUnsupportedResponse())
Greg Clayton17a0cb62011-05-15 23:46:54 +00003028 {
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003029 // Disable this breakpoint type since it is unsupported
3030 switch (type)
3031 {
Greg Clayton17a0cb62011-05-15 23:46:54 +00003032 case eBreakpointSoftware: m_supports_z0 = false; break;
3033 case eBreakpointHardware: m_supports_z1 = false; break;
3034 case eWatchpointWrite: m_supports_z2 = false; break;
3035 case eWatchpointRead: m_supports_z3 = false; break;
3036 case eWatchpointReadWrite: m_supports_z4 = false; break;
Chaoren Lin0be9ebb2015-02-03 01:51:50 +00003037 case eStoppointInvalid: return UINT8_MAX;
Deepak Panickalb98a2bb2014-02-24 11:50:46 +00003038 }
Greg Clayton17a0cb62011-05-15 23:46:54 +00003039 }
3040 }
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003041 // Signal generic failure
Greg Clayton8b82f082011-04-12 05:54:46 +00003042 return UINT8_MAX;
3043}
Greg Claytonadc00cb2011-05-20 23:38:13 +00003044
3045size_t
3046GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids,
3047 bool &sequence_mutex_unavailable)
3048{
3049 Mutex::Locker locker;
3050 thread_ids.clear();
3051
Jim Ingham4ceb9282012-06-08 22:50:40 +00003052 if (GetSequenceMutex (locker, "ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex"))
Greg Claytonadc00cb2011-05-20 23:38:13 +00003053 {
3054 sequence_mutex_unavailable = false;
3055 StringExtractorGDBRemote response;
3056
Greg Clayton3dedae12013-12-06 21:45:27 +00003057 PacketResult packet_result;
3058 for (packet_result = SendPacketAndWaitForResponseNoLock ("qfThreadInfo", strlen("qfThreadInfo"), response);
3059 packet_result == PacketResult::Success && response.IsNormalResponse();
3060 packet_result = SendPacketAndWaitForResponseNoLock ("qsThreadInfo", strlen("qsThreadInfo"), response))
Greg Claytonadc00cb2011-05-20 23:38:13 +00003061 {
3062 char ch = response.GetChar();
3063 if (ch == 'l')
3064 break;
3065 if (ch == 'm')
3066 {
3067 do
3068 {
Jason Molendae9ca4af2013-02-23 02:04:45 +00003069 tid_t tid = response.GetHexMaxU64(false, LLDB_INVALID_THREAD_ID);
Greg Claytonadc00cb2011-05-20 23:38:13 +00003070
3071 if (tid != LLDB_INVALID_THREAD_ID)
3072 {
3073 thread_ids.push_back (tid);
3074 }
3075 ch = response.GetChar(); // Skip the command separator
3076 } while (ch == ','); // Make sure we got a comma separator
3077 }
3078 }
3079 }
3080 else
3081 {
Jim Ingham4ceb9282012-06-08 22:50:40 +00003082#if defined (LLDB_CONFIGURATION_DEBUG)
3083 // assert(!"ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex");
3084#else
Greg Clayton5160ce52013-03-27 23:08:40 +00003085 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS));
Greg Claytonc3c0b0e2012-04-12 19:04:34 +00003086 if (log)
3087 log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'");
Jim Ingham4ceb9282012-06-08 22:50:40 +00003088#endif
Greg Claytonadc00cb2011-05-20 23:38:13 +00003089 sequence_mutex_unavailable = true;
3090 }
3091 return thread_ids.size();
3092}
Greg Clayton37a0a242012-04-11 00:24:49 +00003093
3094lldb::addr_t
3095GDBRemoteCommunicationClient::GetShlibInfoAddr()
3096{
3097 if (!IsRunning())
3098 {
3099 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003100 if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false) == PacketResult::Success)
Greg Clayton37a0a242012-04-11 00:24:49 +00003101 {
3102 if (response.IsNormalResponse())
3103 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS);
3104 }
3105 }
3106 return LLDB_INVALID_ADDRESS;
3107}
3108
Daniel Maleae0f8f572013-08-26 23:57:52 +00003109lldb_private::Error
3110GDBRemoteCommunicationClient::RunShellCommand (const char *command, // Shouldn't be NULL
3111 const char *working_dir, // Pass NULL to use the current working directory
3112 int *status_ptr, // Pass NULL if you don't want the process exit status
3113 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit
3114 std::string *command_output, // Pass NULL if you don't want the command output
3115 uint32_t timeout_sec) // Timeout in seconds to wait for shell program to finish
3116{
3117 lldb_private::StreamString stream;
Greg Claytonfbb76342013-11-20 21:07:01 +00003118 stream.PutCString("qPlatform_shell:");
Daniel Maleae0f8f572013-08-26 23:57:52 +00003119 stream.PutBytesAsRawHex8(command, strlen(command));
3120 stream.PutChar(',');
3121 stream.PutHex32(timeout_sec);
3122 if (working_dir && *working_dir)
3123 {
3124 stream.PutChar(',');
3125 stream.PutBytesAsRawHex8(working_dir, strlen(working_dir));
3126 }
3127 const char *packet = stream.GetData();
3128 int packet_len = stream.GetSize();
3129 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003130 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003131 {
3132 if (response.GetChar() != 'F')
3133 return Error("malformed reply");
3134 if (response.GetChar() != ',')
3135 return Error("malformed reply");
3136 uint32_t exitcode = response.GetHexMaxU32(false, UINT32_MAX);
3137 if (exitcode == UINT32_MAX)
3138 return Error("unable to run remote process");
3139 else if (status_ptr)
3140 *status_ptr = exitcode;
3141 if (response.GetChar() != ',')
3142 return Error("malformed reply");
3143 uint32_t signo = response.GetHexMaxU32(false, UINT32_MAX);
3144 if (signo_ptr)
3145 *signo_ptr = signo;
3146 if (response.GetChar() != ',')
3147 return Error("malformed reply");
3148 std::string output;
3149 response.GetEscapedBinaryData(output);
3150 if (command_output)
3151 command_output->assign(output);
3152 return Error();
3153 }
3154 return Error("unable to send packet");
3155}
3156
Greg Claytonfbb76342013-11-20 21:07:01 +00003157Error
3158GDBRemoteCommunicationClient::MakeDirectory (const char *path,
3159 uint32_t file_permissions)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003160{
3161 lldb_private::StreamString stream;
Greg Claytonfbb76342013-11-20 21:07:01 +00003162 stream.PutCString("qPlatform_mkdir:");
3163 stream.PutHex32(file_permissions);
Daniel Maleae0f8f572013-08-26 23:57:52 +00003164 stream.PutChar(',');
Greg Claytonfbb76342013-11-20 21:07:01 +00003165 stream.PutBytesAsRawHex8(path, strlen(path));
Daniel Maleae0f8f572013-08-26 23:57:52 +00003166 const char *packet = stream.GetData();
3167 int packet_len = stream.GetSize();
3168 StringExtractorGDBRemote response;
Daniel Maleae0f8f572013-08-26 23:57:52 +00003169
Tamas Berghammer0f86b742015-02-23 11:03:08 +00003170 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) != PacketResult::Success)
3171 return Error("failed to send '%s' packet", packet);
3172
3173 if (response.GetChar() != 'F')
3174 return Error("invalid response to '%s' packet", packet);
3175
3176 return Error(response.GetU32(UINT32_MAX), eErrorTypePOSIX);
Daniel Maleae0f8f572013-08-26 23:57:52 +00003177}
3178
Greg Claytonfbb76342013-11-20 21:07:01 +00003179Error
3180GDBRemoteCommunicationClient::SetFilePermissions (const char *path,
3181 uint32_t file_permissions)
3182{
3183 lldb_private::StreamString stream;
3184 stream.PutCString("qPlatform_chmod:");
3185 stream.PutHex32(file_permissions);
3186 stream.PutChar(',');
3187 stream.PutBytesAsRawHex8(path, strlen(path));
3188 const char *packet = stream.GetData();
3189 int packet_len = stream.GetSize();
3190 StringExtractorGDBRemote response;
Tamas Berghammer0f86b742015-02-23 11:03:08 +00003191
3192 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) != PacketResult::Success)
3193 return Error("failed to send '%s' packet", packet);
3194
3195 if (response.GetChar() != 'F')
3196 return Error("invalid response to '%s' packet", packet);
3197
3198 return Error(response.GetU32(false, UINT32_MAX), eErrorTypePOSIX);
Greg Claytonfbb76342013-11-20 21:07:01 +00003199}
3200
Daniel Maleae0f8f572013-08-26 23:57:52 +00003201static uint64_t
3202ParseHostIOPacketResponse (StringExtractorGDBRemote &response,
3203 uint64_t fail_result,
3204 Error &error)
3205{
3206 response.SetFilePos(0);
3207 if (response.GetChar() != 'F')
3208 return fail_result;
3209 int32_t result = response.GetS32 (-2);
3210 if (result == -2)
3211 return fail_result;
3212 if (response.GetChar() == ',')
3213 {
3214 int result_errno = response.GetS32 (-2);
3215 if (result_errno != -2)
3216 error.SetError(result_errno, eErrorTypePOSIX);
3217 else
3218 error.SetError(-1, eErrorTypeGeneric);
3219 }
3220 else
3221 error.Clear();
3222 return result;
3223}
3224lldb::user_id_t
3225GDBRemoteCommunicationClient::OpenFile (const lldb_private::FileSpec& file_spec,
3226 uint32_t flags,
3227 mode_t mode,
3228 Error &error)
3229{
3230 lldb_private::StreamString stream;
3231 stream.PutCString("vFile:open:");
3232 std::string path (file_spec.GetPath());
3233 if (path.empty())
3234 return UINT64_MAX;
3235 stream.PutCStringAsRawHex8(path.c_str());
3236 stream.PutChar(',');
Robert Flackebc56092015-03-18 13:55:48 +00003237 stream.PutHex32(flags);
Daniel Maleae0f8f572013-08-26 23:57:52 +00003238 stream.PutChar(',');
3239 stream.PutHex32(mode);
3240 const char* packet = stream.GetData();
3241 int packet_len = stream.GetSize();
3242 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003243 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003244 {
3245 return ParseHostIOPacketResponse (response, UINT64_MAX, error);
3246 }
3247 return UINT64_MAX;
3248}
3249
3250bool
3251GDBRemoteCommunicationClient::CloseFile (lldb::user_id_t fd,
3252 Error &error)
3253{
3254 lldb_private::StreamString stream;
3255 stream.Printf("vFile:close:%i", (int)fd);
3256 const char* packet = stream.GetData();
3257 int packet_len = stream.GetSize();
3258 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003259 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003260 {
3261 return ParseHostIOPacketResponse (response, -1, error) == 0;
3262 }
Deepak Panickald66b50c2013-10-22 12:27:43 +00003263 return false;
Daniel Maleae0f8f572013-08-26 23:57:52 +00003264}
3265
3266// Extension of host I/O packets to get the file size.
3267lldb::user_id_t
3268GDBRemoteCommunicationClient::GetFileSize (const lldb_private::FileSpec& file_spec)
3269{
3270 lldb_private::StreamString stream;
3271 stream.PutCString("vFile:size:");
3272 std::string path (file_spec.GetPath());
3273 stream.PutCStringAsRawHex8(path.c_str());
3274 const char* packet = stream.GetData();
3275 int packet_len = stream.GetSize();
3276 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003277 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003278 {
3279 if (response.GetChar() != 'F')
3280 return UINT64_MAX;
3281 uint32_t retcode = response.GetHexMaxU64(false, UINT64_MAX);
3282 return retcode;
3283 }
3284 return UINT64_MAX;
3285}
3286
Greg Claytonfbb76342013-11-20 21:07:01 +00003287Error
3288GDBRemoteCommunicationClient::GetFilePermissions(const char *path, uint32_t &file_permissions)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003289{
Greg Claytonfbb76342013-11-20 21:07:01 +00003290 Error error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00003291 lldb_private::StreamString stream;
3292 stream.PutCString("vFile:mode:");
Greg Claytonfbb76342013-11-20 21:07:01 +00003293 stream.PutCStringAsRawHex8(path);
Daniel Maleae0f8f572013-08-26 23:57:52 +00003294 const char* packet = stream.GetData();
3295 int packet_len = stream.GetSize();
3296 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003297 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003298 {
3299 if (response.GetChar() != 'F')
3300 {
3301 error.SetErrorStringWithFormat ("invalid response to '%s' packet", packet);
Daniel Maleae0f8f572013-08-26 23:57:52 +00003302 }
Greg Claytonfbb76342013-11-20 21:07:01 +00003303 else
Daniel Maleae0f8f572013-08-26 23:57:52 +00003304 {
Greg Claytonfbb76342013-11-20 21:07:01 +00003305 const uint32_t mode = response.GetS32(-1);
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00003306 if (static_cast<int32_t>(mode) == -1)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003307 {
Greg Claytonfbb76342013-11-20 21:07:01 +00003308 if (response.GetChar() == ',')
3309 {
3310 int response_errno = response.GetS32(-1);
3311 if (response_errno > 0)
3312 error.SetError(response_errno, lldb::eErrorTypePOSIX);
3313 else
3314 error.SetErrorToGenericError();
3315 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00003316 else
3317 error.SetErrorToGenericError();
3318 }
Greg Claytonfbb76342013-11-20 21:07:01 +00003319 else
3320 {
3321 file_permissions = mode & (S_IRWXU|S_IRWXG|S_IRWXO);
3322 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00003323 }
Daniel Maleae0f8f572013-08-26 23:57:52 +00003324 }
3325 else
3326 {
3327 error.SetErrorStringWithFormat ("failed to send '%s' packet", packet);
3328 }
Greg Claytonfbb76342013-11-20 21:07:01 +00003329 return error;
Daniel Maleae0f8f572013-08-26 23:57:52 +00003330}
3331
3332uint64_t
3333GDBRemoteCommunicationClient::ReadFile (lldb::user_id_t fd,
3334 uint64_t offset,
3335 void *dst,
3336 uint64_t dst_len,
3337 Error &error)
3338{
3339 lldb_private::StreamString stream;
3340 stream.Printf("vFile:pread:%i,%" PRId64 ",%" PRId64, (int)fd, dst_len, offset);
3341 const char* packet = stream.GetData();
3342 int packet_len = stream.GetSize();
3343 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003344 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003345 {
3346 if (response.GetChar() != 'F')
3347 return 0;
3348 uint32_t retcode = response.GetHexMaxU32(false, UINT32_MAX);
3349 if (retcode == UINT32_MAX)
3350 return retcode;
3351 const char next = (response.Peek() ? *response.Peek() : 0);
3352 if (next == ',')
3353 return 0;
3354 if (next == ';')
3355 {
3356 response.GetChar(); // skip the semicolon
3357 std::string buffer;
3358 if (response.GetEscapedBinaryData(buffer))
3359 {
3360 const uint64_t data_to_write = std::min<uint64_t>(dst_len, buffer.size());
3361 if (data_to_write > 0)
3362 memcpy(dst, &buffer[0], data_to_write);
3363 return data_to_write;
3364 }
3365 }
3366 }
3367 return 0;
3368}
3369
3370uint64_t
3371GDBRemoteCommunicationClient::WriteFile (lldb::user_id_t fd,
3372 uint64_t offset,
3373 const void* src,
3374 uint64_t src_len,
3375 Error &error)
3376{
3377 lldb_private::StreamGDBRemote stream;
3378 stream.Printf("vFile:pwrite:%i,%" PRId64 ",", (int)fd, offset);
3379 stream.PutEscapedBytes(src, src_len);
3380 const char* packet = stream.GetData();
3381 int packet_len = stream.GetSize();
3382 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003383 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003384 {
3385 if (response.GetChar() != 'F')
3386 {
3387 error.SetErrorStringWithFormat("write file failed");
3388 return 0;
3389 }
3390 uint64_t bytes_written = response.GetU64(UINT64_MAX);
3391 if (bytes_written == UINT64_MAX)
3392 {
3393 error.SetErrorToGenericError();
3394 if (response.GetChar() == ',')
3395 {
3396 int response_errno = response.GetS32(-1);
3397 if (response_errno > 0)
3398 error.SetError(response_errno, lldb::eErrorTypePOSIX);
3399 }
3400 return 0;
3401 }
3402 return bytes_written;
3403 }
3404 else
3405 {
3406 error.SetErrorString ("failed to send vFile:pwrite packet");
3407 }
3408 return 0;
3409}
3410
Greg Claytonfbb76342013-11-20 21:07:01 +00003411Error
3412GDBRemoteCommunicationClient::CreateSymlink (const char *src, const char *dst)
3413{
3414 Error error;
3415 lldb_private::StreamGDBRemote stream;
3416 stream.PutCString("vFile:symlink:");
3417 // the unix symlink() command reverses its parameters where the dst if first,
3418 // so we follow suit here
3419 stream.PutCStringAsRawHex8(dst);
3420 stream.PutChar(',');
3421 stream.PutCStringAsRawHex8(src);
3422 const char* packet = stream.GetData();
3423 int packet_len = stream.GetSize();
3424 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003425 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Claytonfbb76342013-11-20 21:07:01 +00003426 {
3427 if (response.GetChar() == 'F')
3428 {
3429 uint32_t result = response.GetU32(UINT32_MAX);
3430 if (result != 0)
3431 {
3432 error.SetErrorToGenericError();
3433 if (response.GetChar() == ',')
3434 {
3435 int response_errno = response.GetS32(-1);
3436 if (response_errno > 0)
3437 error.SetError(response_errno, lldb::eErrorTypePOSIX);
3438 }
3439 }
3440 }
3441 else
3442 {
3443 // Should have returned with 'F<result>[,<errno>]'
3444 error.SetErrorStringWithFormat("symlink failed");
3445 }
3446 }
3447 else
3448 {
3449 error.SetErrorString ("failed to send vFile:symlink packet");
3450 }
3451 return error;
3452}
3453
3454Error
3455GDBRemoteCommunicationClient::Unlink (const char *path)
3456{
3457 Error error;
3458 lldb_private::StreamGDBRemote stream;
3459 stream.PutCString("vFile:unlink:");
3460 // the unix symlink() command reverses its parameters where the dst if first,
3461 // so we follow suit here
3462 stream.PutCStringAsRawHex8(path);
3463 const char* packet = stream.GetData();
3464 int packet_len = stream.GetSize();
3465 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003466 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Greg Claytonfbb76342013-11-20 21:07:01 +00003467 {
3468 if (response.GetChar() == 'F')
3469 {
3470 uint32_t result = response.GetU32(UINT32_MAX);
3471 if (result != 0)
3472 {
3473 error.SetErrorToGenericError();
3474 if (response.GetChar() == ',')
3475 {
3476 int response_errno = response.GetS32(-1);
3477 if (response_errno > 0)
3478 error.SetError(response_errno, lldb::eErrorTypePOSIX);
3479 }
3480 }
3481 }
3482 else
3483 {
3484 // Should have returned with 'F<result>[,<errno>]'
3485 error.SetErrorStringWithFormat("unlink failed");
3486 }
3487 }
3488 else
3489 {
3490 error.SetErrorString ("failed to send vFile:unlink packet");
3491 }
3492 return error;
3493}
3494
Daniel Maleae0f8f572013-08-26 23:57:52 +00003495// Extension of host I/O packets to get whether a file exists.
3496bool
3497GDBRemoteCommunicationClient::GetFileExists (const lldb_private::FileSpec& file_spec)
3498{
3499 lldb_private::StreamString stream;
3500 stream.PutCString("vFile:exists:");
3501 std::string path (file_spec.GetPath());
3502 stream.PutCStringAsRawHex8(path.c_str());
3503 const char* packet = stream.GetData();
3504 int packet_len = stream.GetSize();
3505 StringExtractorGDBRemote response;
Greg Clayton3dedae12013-12-06 21:45:27 +00003506 if (SendPacketAndWaitForResponse(packet, packet_len, response, false) == PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003507 {
3508 if (response.GetChar() != 'F')
3509 return false;
3510 if (response.GetChar() != ',')
3511 return false;
3512 bool retcode = (response.GetChar() != '0');
3513 return retcode;
3514 }
3515 return false;
3516}
3517
3518bool
3519GDBRemoteCommunicationClient::CalculateMD5 (const lldb_private::FileSpec& file_spec,
3520 uint64_t &high,
3521 uint64_t &low)
3522{
3523 lldb_private::StreamString stream;
3524 stream.PutCString("vFile:MD5:");
3525 std::string path (file_spec.GetPath());
3526 stream.PutCStringAsRawHex8(path.c_str());
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)
Daniel Maleae0f8f572013-08-26 23:57:52 +00003531 {
3532 if (response.GetChar() != 'F')
3533 return false;
3534 if (response.GetChar() != ',')
3535 return false;
3536 if (response.Peek() && *response.Peek() == 'x')
3537 return false;
3538 low = response.GetHexMaxU64(false, UINT64_MAX);
3539 high = response.GetHexMaxU64(false, UINT64_MAX);
3540 return true;
3541 }
3542 return false;
3543}
Greg Claytonf74cf862013-11-13 23:28:31 +00003544
3545bool
Jason Molendaa3329782014-03-29 18:54:20 +00003546GDBRemoteCommunicationClient::AvoidGPackets (ProcessGDBRemote *process)
3547{
3548 // Some targets have issues with g/G packets and we need to avoid using them
3549 if (m_avoid_g_packets == eLazyBoolCalculate)
3550 {
3551 if (process)
3552 {
3553 m_avoid_g_packets = eLazyBoolNo;
3554 const ArchSpec &arch = process->GetTarget().GetArchitecture();
3555 if (arch.IsValid()
3556 && arch.GetTriple().getVendor() == llvm::Triple::Apple
3557 && arch.GetTriple().getOS() == llvm::Triple::IOS
Todd Fialad8eaa172014-07-23 14:37:35 +00003558 && arch.GetTriple().getArch() == llvm::Triple::aarch64)
Jason Molendaa3329782014-03-29 18:54:20 +00003559 {
3560 m_avoid_g_packets = eLazyBoolYes;
3561 uint32_t gdb_server_version = GetGDBServerProgramVersion();
3562 if (gdb_server_version != 0)
3563 {
3564 const char *gdb_server_name = GetGDBServerProgramName();
3565 if (gdb_server_name && strcmp(gdb_server_name, "debugserver") == 0)
3566 {
3567 if (gdb_server_version >= 310)
3568 m_avoid_g_packets = eLazyBoolNo;
3569 }
3570 }
3571 }
3572 }
3573 }
3574 return m_avoid_g_packets == eLazyBoolYes;
3575}
3576
3577bool
Greg Claytonf74cf862013-11-13 23:28:31 +00003578GDBRemoteCommunicationClient::ReadRegister(lldb::tid_t tid, uint32_t reg, StringExtractorGDBRemote &response)
3579{
3580 Mutex::Locker locker;
3581 if (GetSequenceMutex (locker, "Didn't get sequence mutex for p packet."))
3582 {
3583 const bool thread_suffix_supported = GetThreadSuffixSupported();
3584
3585 if (thread_suffix_supported || SetCurrentThread(tid))
3586 {
3587 char packet[64];
3588 int packet_len = 0;
3589 if (thread_suffix_supported)
3590 packet_len = ::snprintf (packet, sizeof(packet), "p%x;thread:%4.4" PRIx64 ";", reg, tid);
3591 else
3592 packet_len = ::snprintf (packet, sizeof(packet), "p%x", reg);
3593 assert (packet_len < ((int)sizeof(packet) - 1));
Greg Clayton3dedae12013-12-06 21:45:27 +00003594 return SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success;
Greg Claytonf74cf862013-11-13 23:28:31 +00003595 }
3596 }
3597 return false;
3598
3599}
3600
3601
3602bool
3603GDBRemoteCommunicationClient::ReadAllRegisters (lldb::tid_t tid, StringExtractorGDBRemote &response)
3604{
3605 Mutex::Locker locker;
3606 if (GetSequenceMutex (locker, "Didn't get sequence mutex for g packet."))
3607 {
3608 const bool thread_suffix_supported = GetThreadSuffixSupported();
3609
3610 if (thread_suffix_supported || SetCurrentThread(tid))
3611 {
3612 char packet[64];
3613 int packet_len = 0;
3614 // Get all registers in one packet
3615 if (thread_suffix_supported)
3616 packet_len = ::snprintf (packet, sizeof(packet), "g;thread:%4.4" PRIx64 ";", tid);
3617 else
3618 packet_len = ::snprintf (packet, sizeof(packet), "g");
3619 assert (packet_len < ((int)sizeof(packet) - 1));
Greg Clayton3dedae12013-12-06 21:45:27 +00003620 return SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success;
Greg Claytonf74cf862013-11-13 23:28:31 +00003621 }
3622 }
3623 return false;
3624}
3625bool
3626GDBRemoteCommunicationClient::SaveRegisterState (lldb::tid_t tid, uint32_t &save_id)
3627{
3628 save_id = 0; // Set to invalid save ID
3629 if (m_supports_QSaveRegisterState == eLazyBoolNo)
3630 return false;
3631
3632 m_supports_QSaveRegisterState = eLazyBoolYes;
3633 Mutex::Locker locker;
3634 if (GetSequenceMutex (locker, "Didn't get sequence mutex for QSaveRegisterState."))
3635 {
3636 const bool thread_suffix_supported = GetThreadSuffixSupported();
3637 if (thread_suffix_supported || SetCurrentThread(tid))
3638 {
3639 char packet[256];
3640 if (thread_suffix_supported)
3641 ::snprintf (packet, sizeof(packet), "QSaveRegisterState;thread:%4.4" PRIx64 ";", tid);
3642 else
Ilia K686b1fe2015-02-27 19:43:08 +00003643 ::snprintf(packet, sizeof(packet), "QSaveRegisterState");
Greg Claytonf74cf862013-11-13 23:28:31 +00003644
3645 StringExtractorGDBRemote response;
3646
Greg Clayton3dedae12013-12-06 21:45:27 +00003647 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
Greg Claytonf74cf862013-11-13 23:28:31 +00003648 {
3649 if (response.IsUnsupportedResponse())
3650 {
3651 // This packet isn't supported, don't try calling it again
3652 m_supports_QSaveRegisterState = eLazyBoolNo;
3653 }
3654
3655 const uint32_t response_save_id = response.GetU32(0);
3656 if (response_save_id != 0)
3657 {
3658 save_id = response_save_id;
3659 return true;
3660 }
3661 }
3662 }
3663 }
3664 return false;
3665}
3666
3667bool
3668GDBRemoteCommunicationClient::RestoreRegisterState (lldb::tid_t tid, uint32_t save_id)
3669{
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003670 // We use the "m_supports_QSaveRegisterState" variable here because the
Greg Claytonf74cf862013-11-13 23:28:31 +00003671 // QSaveRegisterState and QRestoreRegisterState packets must both be supported in
3672 // order to be useful
3673 if (m_supports_QSaveRegisterState == eLazyBoolNo)
3674 return false;
3675
3676 Mutex::Locker locker;
3677 if (GetSequenceMutex (locker, "Didn't get sequence mutex for QRestoreRegisterState."))
3678 {
3679 const bool thread_suffix_supported = GetThreadSuffixSupported();
3680 if (thread_suffix_supported || SetCurrentThread(tid))
3681 {
3682 char packet[256];
3683 if (thread_suffix_supported)
3684 ::snprintf (packet, sizeof(packet), "QRestoreRegisterState:%u;thread:%4.4" PRIx64 ";", save_id, tid);
3685 else
3686 ::snprintf (packet, sizeof(packet), "QRestoreRegisterState:%u" PRIx64 ";", save_id);
3687
3688 StringExtractorGDBRemote response;
3689
Greg Clayton3dedae12013-12-06 21:45:27 +00003690 if (SendPacketAndWaitForResponse(packet, response, false) == PacketResult::Success)
Greg Claytonf74cf862013-11-13 23:28:31 +00003691 {
3692 if (response.IsOKResponse())
3693 {
3694 return true;
3695 }
3696 else if (response.IsUnsupportedResponse())
3697 {
3698 // This packet isn't supported, don't try calling this packet or
3699 // QSaveRegisterState again...
3700 m_supports_QSaveRegisterState = eLazyBoolNo;
3701 }
3702 }
3703 }
3704 }
3705 return false;
3706}
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00003707
3708bool
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003709GDBRemoteCommunicationClient::GetModuleInfo (const FileSpec& module_file_spec,
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00003710 const lldb_private::ArchSpec& arch_spec,
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003711 ModuleSpec &module_spec)
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00003712{
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003713 std::string module_path = module_file_spec.GetPath ();
3714 if (module_path.empty ())
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00003715 return false;
3716
3717 StreamString packet;
3718 packet.PutCString("qModuleInfo:");
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003719 packet.PutCStringAsRawHex8(module_path.c_str());
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00003720 packet.PutCString(";");
3721 const auto& tripple = arch_spec.GetTriple().getTriple();
3722 packet.PutBytesAsRawHex8(tripple.c_str(), tripple.size());
3723
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00003724 StringExtractorGDBRemote response;
3725 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) != PacketResult::Success)
3726 return false;
3727
3728 if (response.IsErrorResponse ())
3729 return false;
3730
3731 std::string name;
3732 std::string value;
3733 bool success;
3734 StringExtractor extractor;
3735
3736 module_spec.Clear ();
3737 module_spec.GetFileSpec () = module_file_spec;
3738
3739 while (response.GetNameColonValue (name, value))
3740 {
3741 if (name == "uuid" || name == "md5")
3742 {
3743 extractor.GetStringRef ().swap (value);
3744 extractor.SetFilePos (0);
3745 extractor.GetHexByteString (value);
3746 module_spec.GetUUID().SetFromCString (value.c_str(), value.size() / 2);
3747 }
3748 else if (name == "triple")
3749 {
3750 extractor.GetStringRef ().swap (value);
3751 extractor.SetFilePos (0);
3752 extractor.GetHexByteString (value);
3753 module_spec.GetArchitecture().SetTriple (value.c_str ());
3754 }
3755 else if (name == "file_offset")
3756 {
3757 const auto ival = StringConvert::ToUInt64 (value.c_str (), 0, 16, &success);
3758 if (success)
3759 module_spec.SetObjectOffset (ival);
3760 }
3761 else if (name == "file_size")
3762 {
3763 const auto ival = StringConvert::ToUInt64 (value.c_str (), 0, 16, &success);
3764 if (success)
3765 module_spec.SetObjectSize (ival);
3766 }
3767 else if (name == "file_path")
3768 {
3769 extractor.GetStringRef ().swap (value);
3770 extractor.SetFilePos (0);
3771 extractor.GetHexByteString (value);
3772 module_spec.GetFileSpec () = FileSpec (value.c_str(), false);
3773 }
3774 }
3775
3776 return true;
Oleksiy Vyalov6801be32015-02-25 22:15:44 +00003777}