blob: c26345452fedca1e9b2b375d6ab252968ee8afdd [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- GDBRemoteCommunication.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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010#include "GDBRemoteCommunication.h"
11
12// C Includes
Johnny Chena5663552011-05-13 20:07:25 +000013#include <limits.h>
Stephen Wilsona78867b2011-03-25 18:16:28 +000014#include <string.h>
Greg Clayton91a9b2472013-12-04 19:19:12 +000015#include <sys/stat.h>
Stephen Wilsona78867b2011-03-25 18:16:28 +000016
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017// C++ Includes
18// Other libraries and framework includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/Log.h"
Greg Claytonb30c50c2015-05-29 00:01:55 +000020#include "lldb/Core/RegularExpression.h"
Greg Claytonc1422c12012-04-09 22:46:21 +000021#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/StreamString.h"
Zachary Turner93a66fc2014-10-06 21:22:36 +000023#include "lldb/Host/ConnectionFileDescriptor.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000024#include "lldb/Host/FileSpec.h"
25#include "lldb/Host/Host.h"
Zachary Turner42ff0ad2014-08-21 17:29:12 +000026#include "lldb/Host/HostInfo.h"
Oleksiy Vyalovd5f8b6a2015-01-13 23:19:40 +000027#include "lldb/Host/Pipe.h"
Zachary Turner98688922014-08-06 18:16:26 +000028#include "lldb/Host/Socket.h"
Vince Harron5275aaa2015-01-15 20:08:35 +000029#include "lldb/Host/StringConvert.h"
Zachary Turner39de3112014-09-09 20:54:56 +000030#include "lldb/Host/ThreadLauncher.h"
Greg Clayton6988abc2015-10-19 20:44:01 +000031#include "lldb/Target/Platform.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000032#include "lldb/Target/Process.h"
Oleksiy Vyalov4536c452015-02-05 16:29:12 +000033#include "llvm/ADT/SmallString.h"
Pavel Labath1eb0d422016-08-08 12:54:36 +000034#include "llvm/Support/ScopedPrinter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
36// Project includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "ProcessGDBRemoteLog.h"
38
Todd Fiala015d8182014-07-22 23:41:36 +000039#if defined(__APPLE__)
Kate Stoneb9c1b512016-09-06 20:57:50 +000040#define DEBUGSERVER_BASENAME "debugserver"
Todd Fiala015d8182014-07-22 23:41:36 +000041#else
Kate Stoneb9c1b512016-09-06 20:57:50 +000042#define DEBUGSERVER_BASENAME "lldb-server"
Todd Fiala015d8182014-07-22 23:41:36 +000043#endif
Greg Clayton8b82f082011-04-12 05:54:46 +000044
Kate Stoneb9c1b512016-09-06 20:57:50 +000045#if defined(HAVE_LIBCOMPRESSION)
Jason Molenda91ffe0a2015-06-18 21:46:06 +000046#include <compression.h>
47#endif
48
Kate Stoneb9c1b512016-09-06 20:57:50 +000049#if defined(HAVE_LIBZ)
Jason Molenda91ffe0a2015-06-18 21:46:06 +000050#include <zlib.h>
51#endif
52
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053using namespace lldb;
54using namespace lldb_private;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000055using namespace lldb_private::process_gdb_remote;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056
Kate Stoneb9c1b512016-09-06 20:57:50 +000057GDBRemoteCommunication::History::History(uint32_t size)
58 : m_packets(), m_curr_idx(0), m_total_packet_count(0),
59 m_dumped_to_log(false) {
60 m_packets.resize(size);
Greg Claytonc1422c12012-04-09 22:46:21 +000061}
62
Kate Stoneb9c1b512016-09-06 20:57:50 +000063GDBRemoteCommunication::History::~History() {}
64
65void GDBRemoteCommunication::History::AddPacket(char packet_char,
66 PacketType type,
67 uint32_t bytes_transmitted) {
68 const size_t size = m_packets.size();
69 if (size > 0) {
70 const uint32_t idx = GetNextIndex();
71 m_packets[idx].packet.assign(1, packet_char);
72 m_packets[idx].type = type;
73 m_packets[idx].bytes_transmitted = bytes_transmitted;
74 m_packets[idx].packet_idx = m_total_packet_count;
75 m_packets[idx].tid = Host::GetCurrentThreadID();
76 }
Greg Claytonc1422c12012-04-09 22:46:21 +000077}
78
Kate Stoneb9c1b512016-09-06 20:57:50 +000079void GDBRemoteCommunication::History::AddPacket(const std::string &src,
80 uint32_t src_len,
81 PacketType type,
82 uint32_t bytes_transmitted) {
83 const size_t size = m_packets.size();
84 if (size > 0) {
85 const uint32_t idx = GetNextIndex();
86 m_packets[idx].packet.assign(src, 0, src_len);
87 m_packets[idx].type = type;
88 m_packets[idx].bytes_transmitted = bytes_transmitted;
89 m_packets[idx].packet_idx = m_total_packet_count;
90 m_packets[idx].tid = Host::GetCurrentThreadID();
91 }
Greg Claytond451c1a2012-04-13 21:24:18 +000092}
93
Kate Stoneb9c1b512016-09-06 20:57:50 +000094void GDBRemoteCommunication::History::Dump(Stream &strm) const {
95 const uint32_t size = GetNumPacketsInHistory();
96 const uint32_t first_idx = GetFirstSavedPacketIndex();
97 const uint32_t stop_idx = m_curr_idx + size;
98 for (uint32_t i = first_idx; i < stop_idx; ++i) {
99 const uint32_t idx = NormalizeIndex(i);
100 const Entry &entry = m_packets[idx];
101 if (entry.type == ePacketTypeInvalid || entry.packet.empty())
102 break;
103 strm.Printf("history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s\n",
104 entry.packet_idx, entry.tid, entry.bytes_transmitted,
105 (entry.type == ePacketTypeSend) ? "send" : "read",
106 entry.packet.c_str());
107 }
Greg Claytond451c1a2012-04-13 21:24:18 +0000108}
109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110void GDBRemoteCommunication::History::Dump(Log *log) const {
111 if (log && !m_dumped_to_log) {
112 m_dumped_to_log = true;
113 const uint32_t size = GetNumPacketsInHistory();
114 const uint32_t first_idx = GetFirstSavedPacketIndex();
Greg Claytonc1422c12012-04-09 22:46:21 +0000115 const uint32_t stop_idx = m_curr_idx + size;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 for (uint32_t i = first_idx; i < stop_idx; ++i) {
117 const uint32_t idx = NormalizeIndex(i);
118 const Entry &entry = m_packets[idx];
119 if (entry.type == ePacketTypeInvalid || entry.packet.empty())
120 break;
121 log->Printf("history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s",
122 entry.packet_idx, entry.tid, entry.bytes_transmitted,
123 (entry.type == ePacketTypeSend) ? "send" : "read",
124 entry.packet.c_str());
Greg Claytonc1422c12012-04-09 22:46:21 +0000125 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 }
Greg Claytonc1422c12012-04-09 22:46:21 +0000127}
128
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129//----------------------------------------------------------------------
130// GDBRemoteCommunication constructor
131//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132GDBRemoteCommunication::GDBRemoteCommunication(const char *comm_name,
133 const char *listener_name)
Saleem Abdulrasool2d6a9ec2016-07-28 17:32:20 +0000134 : Communication(comm_name),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000135#ifdef LLDB_CONFIGURATION_DEBUG
Saleem Abdulrasool2d6a9ec2016-07-28 17:32:20 +0000136 m_packet_timeout(1000),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000137#else
Saleem Abdulrasool2d6a9ec2016-07-28 17:32:20 +0000138 m_packet_timeout(1),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000139#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 m_echo_number(0), m_supports_qEcho(eLazyBoolCalculate), m_history(512),
141 m_send_acks(true), m_compression_type(CompressionType::None),
142 m_listen_url() {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143}
144
145//----------------------------------------------------------------------
146// Destructor
147//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148GDBRemoteCommunication::~GDBRemoteCommunication() {
149 if (IsConnected()) {
150 Disconnect();
151 }
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 // Stop the communications read thread which is used to parse all
154 // incoming packets. This function will block until the read
155 // thread returns.
156 if (m_read_thread_enabled)
157 StopReadThread();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158}
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160char GDBRemoteCommunication::CalculcateChecksum(llvm::StringRef payload) {
161 int checksum = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 for (char c : payload)
164 checksum += c;
Ed Mastea6b4c772013-08-20 14:12:58 +0000165
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166 return checksum & 255;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167}
168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169size_t GDBRemoteCommunication::SendAck() {
170 Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PACKETS));
171 ConnectionStatus status = eConnectionStatusSuccess;
172 char ch = '+';
173 const size_t bytes_written = Write(&ch, 1, status, NULL);
174 if (log)
175 log->Printf("<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch);
176 m_history.AddPacket(ch, History::ePacketTypeSend, bytes_written);
177 return bytes_written;
178}
179
180size_t GDBRemoteCommunication::SendNack() {
181 Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PACKETS));
182 ConnectionStatus status = eConnectionStatusSuccess;
183 char ch = '-';
184 const size_t bytes_written = Write(&ch, 1, status, NULL);
185 if (log)
186 log->Printf("<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch);
187 m_history.AddPacket(ch, History::ePacketTypeSend, bytes_written);
188 return bytes_written;
189}
190
191GDBRemoteCommunication::PacketResult
192GDBRemoteCommunication::SendPacketNoLock(llvm::StringRef payload) {
193 if (IsConnected()) {
194 StreamString packet(0, 4, eByteOrderBig);
195
196 packet.PutChar('$');
197 packet.Write(payload.data(), payload.size());
198 packet.PutChar('#');
199 packet.PutHex8(CalculcateChecksum(payload));
200
201 Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PACKETS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202 ConnectionStatus status = eConnectionStatusSuccess;
Zachary Turnerc1564272016-11-16 21:15:24 +0000203 // TODO: Don't shimmy through a std::string, just use StringRef.
204 std::string packet_str = packet.GetString();
205 const char *packet_data = packet_str.c_str();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 const size_t packet_length = packet.GetSize();
207 size_t bytes_written = Write(packet_data, packet_length, status, NULL);
208 if (log) {
209 size_t binary_start_offset = 0;
210 if (strncmp(packet_data, "$vFile:pwrite:", strlen("$vFile:pwrite:")) ==
211 0) {
212 const char *first_comma = strchr(packet_data, ',');
213 if (first_comma) {
214 const char *second_comma = strchr(first_comma + 1, ',');
215 if (second_comma)
216 binary_start_offset = second_comma - packet_data + 1;
Greg Claytonc1422c12012-04-09 22:46:21 +0000217 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 }
Greg Claytonc1422c12012-04-09 22:46:21 +0000219
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220 // If logging was just enabled and we have history, then dump out what
221 // we have to the log so we get the historical context. The Dump() call
222 // that
223 // logs all of the packet will set a boolean so that we don't dump this
224 // more
225 // than once
226 if (!m_history.DidDumpToLog())
227 m_history.Dump(log);
Greg Claytonc1422c12012-04-09 22:46:21 +0000228
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 if (binary_start_offset) {
230 StreamString strm;
231 // Print non binary data header
232 strm.Printf("<%4" PRIu64 "> send packet: %.*s", (uint64_t)bytes_written,
233 (int)binary_start_offset, packet_data);
234 const uint8_t *p;
235 // Print binary data exactly as sent
236 for (p = (const uint8_t *)packet_data + binary_start_offset; *p != '#';
237 ++p)
238 strm.Printf("\\x%2.2x", *p);
239 // Print the checksum
240 strm.Printf("%*s", (int)3, p);
Zachary Turnerc1564272016-11-16 21:15:24 +0000241 log->PutString(strm.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 } else
243 log->Printf("<%4" PRIu64 "> send packet: %.*s", (uint64_t)bytes_written,
244 (int)packet_length, packet_data);
Greg Claytonf5e56de2010-09-14 23:36:40 +0000245 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247 m_history.AddPacket(packet.GetString(), packet_length,
248 History::ePacketTypeSend, bytes_written);
249
250 if (bytes_written == packet_length) {
251 if (GetSendAcks())
252 return GetAck();
253 else
254 return PacketResult::Success;
255 } else {
256 if (log)
257 log->Printf("error: failed to send packet: %.*s", (int)packet_length,
258 packet_data);
Greg Clayton3dedae12013-12-06 21:45:27 +0000259 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 }
261 return PacketResult::ErrorSendFailed;
262}
263
264GDBRemoteCommunication::PacketResult GDBRemoteCommunication::GetAck() {
265 StringExtractorGDBRemote packet;
Pavel Labath1eff73c2016-11-24 10:54:49 +0000266 PacketResult result = ReadPacket(packet, GetPacketTimeout(), false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 if (result == PacketResult::Success) {
268 if (packet.GetResponseType() ==
269 StringExtractorGDBRemote::ResponseType::eAck)
270 return PacketResult::Success;
271 else
272 return PacketResult::ErrorSendAck;
273 }
274 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275}
276
Greg Clayton3dedae12013-12-06 21:45:27 +0000277GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000278GDBRemoteCommunication::ReadPacket(StringExtractorGDBRemote &response,
Pavel Labath1eff73c2016-11-24 10:54:49 +0000279 Timeout<std::micro> timeout,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000280 bool sync_on_timeout) {
281 if (m_read_thread_enabled)
Pavel Labath1eff73c2016-11-24 10:54:49 +0000282 return PopPacketFromQueue(response, timeout);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283 else
Pavel Labath1eff73c2016-11-24 10:54:49 +0000284 return WaitForPacketNoLock(response, timeout, sync_on_timeout);
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000285}
286
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000287// This function is called when a packet is requested.
288// A whole packet is popped from the packet queue and returned to the caller.
289// Packets are placed into this queue from the communication read thread.
290// See GDBRemoteCommunication::AppendBytesToCache.
291GDBRemoteCommunication::PacketResult
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292GDBRemoteCommunication::PopPacketFromQueue(StringExtractorGDBRemote &response,
Pavel Labath1eff73c2016-11-24 10:54:49 +0000293 Timeout<std::micro> timeout) {
294 auto pred = [&] { return !m_packet_queue.empty() && IsConnected(); };
295 // lock down the packet queue
296 std::unique_lock<std::mutex> lock(m_packet_queue_mutex);
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000297
Pavel Labath1eff73c2016-11-24 10:54:49 +0000298 if (!timeout)
299 m_condition_queue_not_empty.wait(lock, pred);
300 else {
301 if (!m_condition_queue_not_empty.wait_for(lock, *timeout, pred))
302 return PacketResult::ErrorReplyTimeout;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000303 if (!IsConnected())
304 return PacketResult::ErrorDisconnected;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000305 }
306
Pavel Labath1eff73c2016-11-24 10:54:49 +0000307 // get the front element of the queue
308 response = m_packet_queue.front();
309
310 // remove the front element
311 m_packet_queue.pop();
312
313 // we got a packet
314 return PacketResult::Success;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000315}
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000316
317GDBRemoteCommunication::PacketResult
Pavel Labath1eff73c2016-11-24 10:54:49 +0000318GDBRemoteCommunication::WaitForPacketNoLock(StringExtractorGDBRemote &packet,
319 Timeout<std::micro> timeout,
320 bool sync_on_timeout) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000321 uint8_t buffer[8192];
322 Error error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323
Kate Stoneb9c1b512016-09-06 20:57:50 +0000324 Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PACKETS |
325 GDBR_LOG_VERBOSE));
Greg Clayton644247c2011-07-07 01:59:51 +0000326
Kate Stoneb9c1b512016-09-06 20:57:50 +0000327 // Check for a packet from our cache first without trying any reading...
328 if (CheckForPacket(NULL, 0, packet) != PacketType::Invalid)
329 return PacketResult::Success;
330
331 bool timed_out = false;
332 bool disconnected = false;
333 while (IsConnected() && !timed_out) {
334 lldb::ConnectionStatus status = eConnectionStatusNoConnection;
335 size_t bytes_read =
Pavel Labath1eff73c2016-11-24 10:54:49 +0000336 Read(buffer, sizeof(buffer), timeout ? timeout->count() : UINT32_MAX,
337 status, &error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338
339 if (log)
Pavel Labath1eff73c2016-11-24 10:54:49 +0000340 log->Printf("%s: Read (buffer, (sizeof(buffer), timeout = %ld us, "
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 "status = %s, error = %s) => bytes_read = %" PRIu64,
Pavel Labath1eff73c2016-11-24 10:54:49 +0000342 LLVM_PRETTY_FUNCTION, long(timeout ? timeout->count() : -1),
Kate Stoneb9c1b512016-09-06 20:57:50 +0000343 Communication::ConnectionStatusAsCString(status),
344 error.AsCString(), (uint64_t)bytes_read);
345
346 if (bytes_read > 0) {
347 if (CheckForPacket(buffer, bytes_read, packet) != PacketType::Invalid)
Greg Clayton3dedae12013-12-06 21:45:27 +0000348 return PacketResult::Success;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000349 } else {
350 switch (status) {
351 case eConnectionStatusTimedOut:
352 case eConnectionStatusInterrupted:
353 if (sync_on_timeout) {
354 //------------------------------------------------------------------
355 /// Sync the remote GDB server and make sure we get a response that
356 /// corresponds to what we send.
357 ///
358 /// Sends a "qEcho" packet and makes sure it gets the exact packet
359 /// echoed back. If the qEcho packet isn't supported, we send a qC
360 /// packet and make sure we get a valid thread ID back. We use the
361 /// "qC" packet since its response if very unique: is responds with
362 /// "QC%x" where %x is the thread ID of the current thread. This
363 /// makes the response unique enough from other packet responses to
364 /// ensure we are back on track.
365 ///
366 /// This packet is needed after we time out sending a packet so we
367 /// can ensure that we are getting the response for the packet we
368 /// are sending. There are no sequence IDs in the GDB remote
369 /// protocol (there used to be, but they are not supported anymore)
370 /// so if you timeout sending packet "abc", you might then send
371 /// packet "cde" and get the response for the previous "abc" packet.
372 /// Many responses are "OK" or "" (unsupported) or "EXX" (error) so
373 /// many responses for packets can look like responses for other
374 /// packets. So if we timeout, we need to ensure that we can get
375 /// back on track. If we can't get back on track, we must
376 /// disconnect.
377 //------------------------------------------------------------------
378 bool sync_success = false;
379 bool got_actual_response = false;
380 // We timed out, we need to sync back up with the
381 char echo_packet[32];
382 int echo_packet_len = 0;
383 RegularExpression response_regex;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384
Kate Stoneb9c1b512016-09-06 20:57:50 +0000385 if (m_supports_qEcho == eLazyBoolYes) {
386 echo_packet_len = ::snprintf(echo_packet, sizeof(echo_packet),
387 "qEcho:%u", ++m_echo_number);
388 std::string regex_str = "^";
389 regex_str += echo_packet;
390 regex_str += "$";
Zachary Turner95eae422016-09-21 16:01:28 +0000391 response_regex.Compile(regex_str);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000392 } else {
393 echo_packet_len =
394 ::snprintf(echo_packet, sizeof(echo_packet), "qC");
Zachary Turner95eae422016-09-21 16:01:28 +0000395 response_regex.Compile(llvm::StringRef("^QC[0-9A-Fa-f]+$"));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000396 }
Greg Clayton644247c2011-07-07 01:59:51 +0000397
Kate Stoneb9c1b512016-09-06 20:57:50 +0000398 PacketResult echo_packet_result =
399 SendPacketNoLock(llvm::StringRef(echo_packet, echo_packet_len));
400 if (echo_packet_result == PacketResult::Success) {
401 const uint32_t max_retries = 3;
402 uint32_t successful_responses = 0;
403 for (uint32_t i = 0; i < max_retries; ++i) {
404 StringExtractorGDBRemote echo_response;
Pavel Labath1eff73c2016-11-24 10:54:49 +0000405 echo_packet_result =
406 WaitForPacketNoLock(echo_response, timeout, false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000407 if (echo_packet_result == PacketResult::Success) {
408 ++successful_responses;
Zachary Turner95eae422016-09-21 16:01:28 +0000409 if (response_regex.Execute(echo_response.GetStringRef())) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000410 sync_success = true;
411 break;
412 } else if (successful_responses == 1) {
413 // We got something else back as the first successful
414 // response, it probably is
415 // the response to the packet we actually wanted, so copy it
416 // over if this
417 // is the first success and continue to try to get the qEcho
418 // response
419 packet = echo_response;
420 got_actual_response = true;
Greg Claytonb30c50c2015-05-29 00:01:55 +0000421 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000422 } else if (echo_packet_result == PacketResult::ErrorReplyTimeout)
423 continue; // Packet timed out, continue waiting for a response
424 else
425 break; // Something else went wrong getting the packet back, we
426 // failed and are done trying
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000428 }
429
430 // We weren't able to sync back up with the server, we must abort
431 // otherwise
432 // all responses might not be from the right packets...
433 if (sync_success) {
434 // We timed out, but were able to recover
435 if (got_actual_response) {
436 // We initially timed out, but we did get a response that came in
437 // before the successful
438 // reply to our qEcho packet, so lets say everything is fine...
439 return PacketResult::Success;
440 }
441 } else {
442 disconnected = true;
443 Disconnect();
444 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000446 timed_out = true;
447 break;
448 case eConnectionStatusSuccess:
449 // printf ("status = success but error = %s\n",
450 // error.AsCString("<invalid>"));
451 break;
452
453 case eConnectionStatusEndOfFile:
454 case eConnectionStatusNoConnection:
455 case eConnectionStatusLostConnection:
456 case eConnectionStatusError:
457 disconnected = true;
458 Disconnect();
459 break;
460 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000462 }
463 packet.Clear();
464 if (disconnected)
465 return PacketResult::ErrorDisconnected;
466 if (timed_out)
467 return PacketResult::ErrorReplyTimeout;
468 else
469 return PacketResult::ErrorReplyFailed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470}
471
Kate Stoneb9c1b512016-09-06 20:57:50 +0000472bool GDBRemoteCommunication::DecompressPacket() {
473 Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PACKETS));
Jason Molenda91ffe0a2015-06-18 21:46:06 +0000474
Kate Stoneb9c1b512016-09-06 20:57:50 +0000475 if (!CompressionIsEnabled())
Jason Molenda91ffe0a2015-06-18 21:46:06 +0000476 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000477
478 size_t pkt_size = m_bytes.size();
479
480 // Smallest possible compressed packet is $N#00 - an uncompressed empty reply,
481 // most commonly indicating
482 // an unsupported packet. Anything less than 5 characters, it's definitely
483 // not a compressed packet.
484 if (pkt_size < 5)
485 return true;
486
487 if (m_bytes[0] != '$' && m_bytes[0] != '%')
488 return true;
489 if (m_bytes[1] != 'C' && m_bytes[1] != 'N')
490 return true;
491
492 size_t hash_mark_idx = m_bytes.find('#');
493 if (hash_mark_idx == std::string::npos)
494 return true;
495 if (hash_mark_idx + 2 >= m_bytes.size())
496 return true;
497
498 if (!::isxdigit(m_bytes[hash_mark_idx + 1]) ||
499 !::isxdigit(m_bytes[hash_mark_idx + 2]))
500 return true;
501
502 size_t content_length =
503 pkt_size -
504 5; // not counting '$', 'C' | 'N', '#', & the two hex checksum chars
505 size_t content_start = 2; // The first character of the
506 // compressed/not-compressed text of the packet
507 size_t checksum_idx =
508 hash_mark_idx +
509 1; // The first character of the two hex checksum characters
510
511 // Normally size_of_first_packet == m_bytes.size() but m_bytes may contain
512 // multiple packets.
513 // size_of_first_packet is the size of the initial packet which we'll replace
514 // with the decompressed
515 // version of, leaving the rest of m_bytes unmodified.
516 size_t size_of_first_packet = hash_mark_idx + 3;
517
518 // Compressed packets ("$C") start with a base10 number which is the size of
519 // the uncompressed payload,
520 // then a : and then the compressed data. e.g. $C1024:<binary>#00
521 // Update content_start and content_length to only include the <binary> part
522 // of the packet.
523
524 uint64_t decompressed_bufsize = ULONG_MAX;
525 if (m_bytes[1] == 'C') {
526 size_t i = content_start;
527 while (i < hash_mark_idx && isdigit(m_bytes[i]))
528 i++;
529 if (i < hash_mark_idx && m_bytes[i] == ':') {
530 i++;
531 content_start = i;
532 content_length = hash_mark_idx - content_start;
533 std::string bufsize_str(m_bytes.data() + 2, i - 2 - 1);
534 errno = 0;
535 decompressed_bufsize = ::strtoul(bufsize_str.c_str(), NULL, 10);
536 if (errno != 0 || decompressed_bufsize == ULONG_MAX) {
537 m_bytes.erase(0, size_of_first_packet);
538 return false;
539 }
540 }
541 }
542
543 if (GetSendAcks()) {
544 char packet_checksum_cstr[3];
545 packet_checksum_cstr[0] = m_bytes[checksum_idx];
546 packet_checksum_cstr[1] = m_bytes[checksum_idx + 1];
547 packet_checksum_cstr[2] = '\0';
548 long packet_checksum = strtol(packet_checksum_cstr, NULL, 16);
549
550 long actual_checksum = CalculcateChecksum(
551 llvm::StringRef(m_bytes).substr(1, hash_mark_idx - 1));
552 bool success = packet_checksum == actual_checksum;
553 if (!success) {
554 if (log)
555 log->Printf(
556 "error: checksum mismatch: %.*s expected 0x%2.2x, got 0x%2.2x",
557 (int)(pkt_size), m_bytes.c_str(), (uint8_t)packet_checksum,
558 (uint8_t)actual_checksum);
559 }
560 // Send the ack or nack if needed
561 if (!success) {
562 SendNack();
563 m_bytes.erase(0, size_of_first_packet);
564 return false;
565 } else {
566 SendAck();
567 }
568 }
569
570 if (m_bytes[1] == 'N') {
571 // This packet was not compressed -- delete the 'N' character at the
572 // start and the packet may be processed as-is.
573 m_bytes.erase(1, 1);
574 return true;
575 }
576
577 // Reverse the gdb-remote binary escaping that was done to the compressed text
578 // to
579 // guard characters like '$', '#', '}', etc.
580 std::vector<uint8_t> unescaped_content;
581 unescaped_content.reserve(content_length);
582 size_t i = content_start;
583 while (i < hash_mark_idx) {
584 if (m_bytes[i] == '}') {
585 i++;
586 unescaped_content.push_back(m_bytes[i] ^ 0x20);
587 } else {
588 unescaped_content.push_back(m_bytes[i]);
589 }
590 i++;
591 }
592
593 uint8_t *decompressed_buffer = nullptr;
594 size_t decompressed_bytes = 0;
595
596 if (decompressed_bufsize != ULONG_MAX) {
597 decompressed_buffer = (uint8_t *)malloc(decompressed_bufsize + 1);
598 if (decompressed_buffer == nullptr) {
599 m_bytes.erase(0, size_of_first_packet);
600 return false;
601 }
602 }
603
604#if defined(HAVE_LIBCOMPRESSION)
605 // libcompression is weak linked so check that compression_decode_buffer() is
606 // available
607 if (compression_decode_buffer != NULL &&
608 (m_compression_type == CompressionType::ZlibDeflate ||
609 m_compression_type == CompressionType::LZFSE ||
610 m_compression_type == CompressionType::LZ4)) {
611 compression_algorithm compression_type;
612 if (m_compression_type == CompressionType::ZlibDeflate)
613 compression_type = COMPRESSION_ZLIB;
614 else if (m_compression_type == CompressionType::LZFSE)
615 compression_type = COMPRESSION_LZFSE;
616 else if (m_compression_type == CompressionType::LZ4)
617 compression_type = COMPRESSION_LZ4_RAW;
618 else if (m_compression_type == CompressionType::LZMA)
619 compression_type = COMPRESSION_LZMA;
620
621 // If we have the expected size of the decompressed payload, we can allocate
622 // the right-sized buffer and do it. If we don't have that information,
623 // we'll
624 // need to try decoding into a big buffer and if the buffer wasn't big
625 // enough,
626 // increase it and try again.
627
628 if (decompressed_bufsize != ULONG_MAX && decompressed_buffer != nullptr) {
629 decompressed_bytes = compression_decode_buffer(
630 decompressed_buffer, decompressed_bufsize + 10,
631 (uint8_t *)unescaped_content.data(), unescaped_content.size(), NULL,
632 compression_type);
633 }
634 }
635#endif
636
637#if defined(HAVE_LIBZ)
638 if (decompressed_bytes == 0 && decompressed_bufsize != ULONG_MAX &&
639 decompressed_buffer != nullptr &&
640 m_compression_type == CompressionType::ZlibDeflate) {
641 z_stream stream;
642 memset(&stream, 0, sizeof(z_stream));
643 stream.next_in = (Bytef *)unescaped_content.data();
644 stream.avail_in = (uInt)unescaped_content.size();
645 stream.total_in = 0;
646 stream.next_out = (Bytef *)decompressed_buffer;
647 stream.avail_out = decompressed_bufsize;
648 stream.total_out = 0;
649 stream.zalloc = Z_NULL;
650 stream.zfree = Z_NULL;
651 stream.opaque = Z_NULL;
652
653 if (inflateInit2(&stream, -15) == Z_OK) {
654 int status = inflate(&stream, Z_NO_FLUSH);
655 inflateEnd(&stream);
656 if (status == Z_STREAM_END) {
657 decompressed_bytes = stream.total_out;
658 }
659 }
660 }
661#endif
662
663 if (decompressed_bytes == 0 || decompressed_buffer == nullptr) {
664 if (decompressed_buffer)
665 free(decompressed_buffer);
666 m_bytes.erase(0, size_of_first_packet);
667 return false;
668 }
669
670 std::string new_packet;
671 new_packet.reserve(decompressed_bytes + 6);
672 new_packet.push_back(m_bytes[0]);
673 new_packet.append((const char *)decompressed_buffer, decompressed_bytes);
674 new_packet.push_back('#');
675 if (GetSendAcks()) {
676 uint8_t decompressed_checksum = CalculcateChecksum(
677 llvm::StringRef((const char *)decompressed_buffer, decompressed_bytes));
678 char decompressed_checksum_str[3];
679 snprintf(decompressed_checksum_str, 3, "%02x", decompressed_checksum);
680 new_packet.append(decompressed_checksum_str);
681 } else {
682 new_packet.push_back('0');
683 new_packet.push_back('0');
684 }
685
686 m_bytes.replace(0, size_of_first_packet, new_packet.data(),
687 new_packet.size());
688
689 free(decompressed_buffer);
690 return true;
Jason Molenda91ffe0a2015-06-18 21:46:06 +0000691}
692
Ewan Crawford9aa2da002015-05-27 14:12:34 +0000693GDBRemoteCommunication::PacketType
Kate Stoneb9c1b512016-09-06 20:57:50 +0000694GDBRemoteCommunication::CheckForPacket(const uint8_t *src, size_t src_len,
695 StringExtractorGDBRemote &packet) {
696 // Put the packet data into the buffer in a thread safe fashion
697 std::lock_guard<std::recursive_mutex> guard(m_bytes_mutex);
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000698
Kate Stoneb9c1b512016-09-06 20:57:50 +0000699 Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PACKETS));
Greg Clayton197bacf2011-07-02 21:07:54 +0000700
Kate Stoneb9c1b512016-09-06 20:57:50 +0000701 if (src && src_len > 0) {
702 if (log && log->GetVerbose()) {
703 StreamString s;
704 log->Printf("GDBRemoteCommunication::%s adding %u bytes: %.*s",
705 __FUNCTION__, (uint32_t)src_len, (uint32_t)src_len, src);
706 }
707 m_bytes.append((const char *)src, src_len);
708 }
709
710 bool isNotifyPacket = false;
711
712 // Parse up the packets into gdb remote packets
713 if (!m_bytes.empty()) {
714 // end_idx must be one past the last valid packet byte. Start
715 // it off with an invalid value that is the same as the current
716 // index.
717 size_t content_start = 0;
718 size_t content_length = 0;
719 size_t total_length = 0;
720 size_t checksum_idx = std::string::npos;
721
722 // Size of packet before it is decompressed, for logging purposes
723 size_t original_packet_size = m_bytes.size();
724 if (CompressionIsEnabled()) {
725 if (DecompressPacket() == false) {
726 packet.Clear();
727 return GDBRemoteCommunication::PacketType::Standard;
728 }
Greg Clayton197bacf2011-07-02 21:07:54 +0000729 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000730
Kate Stoneb9c1b512016-09-06 20:57:50 +0000731 switch (m_bytes[0]) {
732 case '+': // Look for ack
733 case '-': // Look for cancel
734 case '\x03': // ^C to halt target
735 content_length = total_length = 1; // The command is one byte long...
736 break;
Ewan Crawford9aa2da002015-05-27 14:12:34 +0000737
Kate Stoneb9c1b512016-09-06 20:57:50 +0000738 case '%': // Async notify packet
739 isNotifyPacket = true;
740 LLVM_FALLTHROUGH;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741
Kate Stoneb9c1b512016-09-06 20:57:50 +0000742 case '$':
743 // Look for a standard gdb packet?
744 {
745 size_t hash_pos = m_bytes.find('#');
746 if (hash_pos != std::string::npos) {
747 if (hash_pos + 2 < m_bytes.size()) {
748 checksum_idx = hash_pos + 1;
749 // Skip the dollar sign
750 content_start = 1;
751 // Don't include the # in the content or the $ in the content length
752 content_length = hash_pos - 1;
753
754 total_length =
755 hash_pos + 3; // Skip the # and the two hex checksum bytes
756 } else {
757 // Checksum bytes aren't all here yet
758 content_length = std::string::npos;
759 }
Jason Molenda91ffe0a2015-06-18 21:46:06 +0000760 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761 }
762 break;
Jason Molenda91ffe0a2015-06-18 21:46:06 +0000763
Kate Stoneb9c1b512016-09-06 20:57:50 +0000764 default: {
765 // We have an unexpected byte and we need to flush all bad
766 // data that is in m_bytes, so we need to find the first
767 // byte that is a '+' (ACK), '-' (NACK), \x03 (CTRL+C interrupt),
768 // or '$' character (start of packet header) or of course,
769 // the end of the data in m_bytes...
770 const size_t bytes_len = m_bytes.size();
771 bool done = false;
772 uint32_t idx;
773 for (idx = 1; !done && idx < bytes_len; ++idx) {
774 switch (m_bytes[idx]) {
775 case '+':
776 case '-':
777 case '\x03':
778 case '%':
779 case '$':
780 done = true;
781 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000782
Kate Stoneb9c1b512016-09-06 20:57:50 +0000783 default:
784 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000785 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000786 }
787 if (log)
788 log->Printf("GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'",
789 __FUNCTION__, idx - 1, idx - 1, m_bytes.c_str());
790 m_bytes.erase(0, idx - 1);
791 } break;
792 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000793
Kate Stoneb9c1b512016-09-06 20:57:50 +0000794 if (content_length == std::string::npos) {
795 packet.Clear();
796 return GDBRemoteCommunication::PacketType::Invalid;
797 } else if (total_length > 0) {
798
799 // We have a valid packet...
800 assert(content_length <= m_bytes.size());
801 assert(total_length <= m_bytes.size());
802 assert(content_length <= total_length);
803 size_t content_end = content_start + content_length;
804
805 bool success = true;
806 std::string &packet_str = packet.GetStringRef();
807 if (log) {
808 // If logging was just enabled and we have history, then dump out what
809 // we have to the log so we get the historical context. The Dump() call
810 // that
811 // logs all of the packet will set a boolean so that we don't dump this
812 // more
813 // than once
814 if (!m_history.DidDumpToLog())
815 m_history.Dump(log);
816
817 bool binary = false;
818 // Only detect binary for packets that start with a '$' and have a '#CC'
819 // checksum
820 if (m_bytes[0] == '$' && total_length > 4) {
821 for (size_t i = 0; !binary && i < total_length; ++i) {
822 if (isprint(m_bytes[i]) == 0 && isspace(m_bytes[i]) == 0) {
823 binary = true;
824 }
825 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000826 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000827 if (binary) {
828 StreamString strm;
829 // Packet header...
830 if (CompressionIsEnabled())
831 strm.Printf("<%4" PRIu64 ":%" PRIu64 "> read packet: %c",
832 (uint64_t)original_packet_size, (uint64_t)total_length,
833 m_bytes[0]);
834 else
835 strm.Printf("<%4" PRIu64 "> read packet: %c",
836 (uint64_t)total_length, m_bytes[0]);
837 for (size_t i = content_start; i < content_end; ++i) {
838 // Remove binary escaped bytes when displaying the packet...
839 const char ch = m_bytes[i];
840 if (ch == 0x7d) {
841 // 0x7d is the escape character. The next character is to
842 // be XOR'd with 0x20.
843 const char escapee = m_bytes[++i] ^ 0x20;
844 strm.Printf("%2.2x", escapee);
845 } else {
846 strm.Printf("%2.2x", (uint8_t)ch);
Greg Claytonc1422c12012-04-09 22:46:21 +0000847 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000848 }
849 // Packet footer...
850 strm.Printf("%c%c%c", m_bytes[total_length - 3],
851 m_bytes[total_length - 2], m_bytes[total_length - 1]);
Zachary Turnerc1564272016-11-16 21:15:24 +0000852 log->PutString(strm.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000853 } else {
854 if (CompressionIsEnabled())
855 log->Printf("<%4" PRIu64 ":%" PRIu64 "> read packet: %.*s",
856 (uint64_t)original_packet_size, (uint64_t)total_length,
857 (int)(total_length), m_bytes.c_str());
858 else
859 log->Printf("<%4" PRIu64 "> read packet: %.*s",
860 (uint64_t)total_length, (int)(total_length),
861 m_bytes.c_str());
862 }
863 }
Greg Claytonc1422c12012-04-09 22:46:21 +0000864
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000865 m_history.AddPacket(m_bytes, total_length, History::ePacketTypeRecv,
866 total_length);
Greg Claytonc1422c12012-04-09 22:46:21 +0000867
Kate Stoneb9c1b512016-09-06 20:57:50 +0000868 // Clear packet_str in case there is some existing data in it.
869 packet_str.clear();
870 // Copy the packet from m_bytes to packet_str expanding the
871 // run-length encoding in the process.
872 // Reserve enough byte for the most common case (no RLE used)
873 packet_str.reserve(m_bytes.length());
874 for (std::string::const_iterator c = m_bytes.begin() + content_start;
875 c != m_bytes.begin() + content_end; ++c) {
876 if (*c == '*') {
877 // '*' indicates RLE. Next character will give us the
878 // repeat count and previous character is what is to be
879 // repeated.
880 char char_to_repeat = packet_str.back();
881 // Number of time the previous character is repeated
882 int repeat_count = *++c + 3 - ' ';
883 // We have the char_to_repeat and repeat_count. Now push
884 // it in the packet.
885 for (int i = 0; i < repeat_count; ++i)
886 packet_str.push_back(char_to_repeat);
887 } else if (*c == 0x7d) {
888 // 0x7d is the escape character. The next character is to
889 // be XOR'd with 0x20.
890 char escapee = *++c ^ 0x20;
891 packet_str.push_back(escapee);
892 } else {
893 packet_str.push_back(*c);
894 }
895 }
896
897 if (m_bytes[0] == '$' || m_bytes[0] == '%') {
898 assert(checksum_idx < m_bytes.size());
899 if (::isxdigit(m_bytes[checksum_idx + 0]) ||
900 ::isxdigit(m_bytes[checksum_idx + 1])) {
901 if (GetSendAcks()) {
902 const char *packet_checksum_cstr = &m_bytes[checksum_idx];
903 char packet_checksum = strtol(packet_checksum_cstr, NULL, 16);
904 char actual_checksum = CalculcateChecksum(packet_str);
905 success = packet_checksum == actual_checksum;
906 if (!success) {
907 if (log)
908 log->Printf("error: checksum mismatch: %.*s expected 0x%2.2x, "
909 "got 0x%2.2x",
910 (int)(total_length), m_bytes.c_str(),
911 (uint8_t)packet_checksum, (uint8_t)actual_checksum);
Hafiz Abid Qadeerda96ef22013-08-28 10:31:52 +0000912 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000913 // Send the ack or nack if needed
914 if (!success)
915 SendNack();
Ewan Crawford9aa2da002015-05-27 14:12:34 +0000916 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000917 SendAck();
918 }
919 } else {
920 success = false;
921 if (log)
922 log->Printf("error: invalid checksum in packet: '%s'\n",
923 m_bytes.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000924 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000925 }
926
927 m_bytes.erase(0, total_length);
928 packet.SetFilePos(0);
929
930 if (isNotifyPacket)
931 return GDBRemoteCommunication::PacketType::Notify;
932 else
933 return GDBRemoteCommunication::PacketType::Standard;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000934 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000935 }
936 packet.Clear();
937 return GDBRemoteCommunication::PacketType::Invalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000938}
939
Kate Stoneb9c1b512016-09-06 20:57:50 +0000940Error GDBRemoteCommunication::StartListenThread(const char *hostname,
941 uint16_t port) {
942 Error error;
943 if (m_listen_thread.IsJoinable()) {
944 error.SetErrorString("listen thread already running");
945 } else {
946 char listen_url[512];
947 if (hostname && hostname[0])
948 snprintf(listen_url, sizeof(listen_url), "listen://%s:%i", hostname,
949 port);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000950 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000951 snprintf(listen_url, sizeof(listen_url), "listen://%i", port);
952 m_listen_url = listen_url;
953 SetConnection(new ConnectionFileDescriptor());
954 m_listen_thread = ThreadLauncher::LaunchThread(
955 listen_url, GDBRemoteCommunication::ListenThread, this, &error);
956 }
957 return error;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000958}
959
Kate Stoneb9c1b512016-09-06 20:57:50 +0000960bool GDBRemoteCommunication::JoinListenThread() {
961 if (m_listen_thread.IsJoinable())
962 m_listen_thread.Join(nullptr);
963 return true;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000964}
965
966lldb::thread_result_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000967GDBRemoteCommunication::ListenThread(lldb::thread_arg_t arg) {
968 GDBRemoteCommunication *comm = (GDBRemoteCommunication *)arg;
969 Error error;
970 ConnectionFileDescriptor *connection =
971 (ConnectionFileDescriptor *)comm->GetConnection();
972
973 if (connection) {
974 // Do the listen on another thread so we can continue on...
975 if (connection->Connect(comm->m_listen_url.c_str(), &error) !=
976 eConnectionStatusSuccess)
977 comm->SetConnection(NULL);
978 }
979 return NULL;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000980}
981
Kate Stoneb9c1b512016-09-06 20:57:50 +0000982Error GDBRemoteCommunication::StartDebugserverProcess(
983 const char *url, Platform *platform, ProcessLaunchInfo &launch_info,
984 uint16_t *port, const Args *inferior_args, int pass_comm_fd) {
985 Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
986 if (log)
987 log->Printf("GDBRemoteCommunication::%s(url=%s, port=%" PRIu16 ")",
988 __FUNCTION__, url ? url : "<empty>",
989 port ? *port : uint16_t(0));
990
991 Error error;
992 // If we locate debugserver, keep that located version around
993 static FileSpec g_debugserver_file_spec;
994
995 char debugserver_path[PATH_MAX];
996 FileSpec &debugserver_file_spec = launch_info.GetExecutableFile();
997
998 // Always check to see if we have an environment override for the path
999 // to the debugserver to use and use it if we do.
1000 const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH");
1001 if (env_debugserver_path) {
1002 debugserver_file_spec.SetFile(env_debugserver_path, false);
Todd Fiala015d8182014-07-22 23:41:36 +00001003 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001004 log->Printf("GDBRemoteCommunication::%s() gdb-remote stub exe path set "
1005 "from environment variable: %s",
1006 __FUNCTION__, env_debugserver_path);
1007 } else
1008 debugserver_file_spec = g_debugserver_file_spec;
1009 bool debugserver_exists = debugserver_file_spec.Exists();
1010 if (!debugserver_exists) {
1011 // The debugserver binary is in the LLDB.framework/Resources
1012 // directory.
1013 if (HostInfo::GetLLDBPath(ePathTypeSupportExecutableDir,
1014 debugserver_file_spec)) {
1015 debugserver_file_spec.AppendPathComponent(DEBUGSERVER_BASENAME);
1016 debugserver_exists = debugserver_file_spec.Exists();
1017 if (debugserver_exists) {
Todd Fiala015d8182014-07-22 23:41:36 +00001018 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001019 log->Printf(
1020 "GDBRemoteCommunication::%s() found gdb-remote stub exe '%s'",
1021 __FUNCTION__, debugserver_file_spec.GetPath().c_str());
Todd Fiala015d8182014-07-22 23:41:36 +00001022
Kate Stoneb9c1b512016-09-06 20:57:50 +00001023 g_debugserver_file_spec = debugserver_file_spec;
1024 } else {
1025 debugserver_file_spec =
1026 platform->LocateExecutable(DEBUGSERVER_BASENAME);
1027 if (debugserver_file_spec) {
1028 // Platform::LocateExecutable() wouldn't return a path if it doesn't
1029 // exist
1030 debugserver_exists = true;
1031 } else {
1032 if (log)
1033 log->Printf("GDBRemoteCommunication::%s() could not find "
1034 "gdb-remote stub exe '%s'",
1035 __FUNCTION__, debugserver_file_spec.GetPath().c_str());
Greg Clayton8b82f082011-04-12 05:54:46 +00001036 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001037 // Don't cache the platform specific GDB server binary as it could
1038 // change
1039 // from platform to platform
1040 g_debugserver_file_spec.Clear();
1041 }
Greg Clayton8b82f082011-04-12 05:54:46 +00001042 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001043 }
Greg Clayton8b82f082011-04-12 05:54:46 +00001044
Kate Stoneb9c1b512016-09-06 20:57:50 +00001045 if (debugserver_exists) {
1046 debugserver_file_spec.GetPath(debugserver_path, sizeof(debugserver_path));
Tamas Berghammerc2c3d712015-02-18 15:39:41 +00001047
Kate Stoneb9c1b512016-09-06 20:57:50 +00001048 Args &debugserver_args = launch_info.GetArguments();
1049 debugserver_args.Clear();
1050 char arg_cstr[PATH_MAX];
1051
1052 // Start args with "debugserver /file/path -r --"
Zachary Turnerecbb0bb2016-09-19 17:54:06 +00001053 debugserver_args.AppendArgument(llvm::StringRef(debugserver_path));
Greg Clayton00fe87b2013-12-05 22:58:22 +00001054
Tamas Berghammerc2c3d712015-02-18 15:39:41 +00001055#if !defined(__APPLE__)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001056 // First argument to lldb-server must be mode in which to run.
Zachary Turnerecbb0bb2016-09-19 17:54:06 +00001057 debugserver_args.AppendArgument(llvm::StringRef("gdbserver"));
Tamas Berghammerc2c3d712015-02-18 15:39:41 +00001058#endif
1059
Kate Stoneb9c1b512016-09-06 20:57:50 +00001060 // If a url is supplied then use it
1061 if (url)
Zachary Turnerecbb0bb2016-09-19 17:54:06 +00001062 debugserver_args.AppendArgument(llvm::StringRef(url));
Greg Claytonfda4fab2014-01-10 22:24:11 +00001063
Kate Stoneb9c1b512016-09-06 20:57:50 +00001064 if (pass_comm_fd >= 0) {
1065 StreamString fd_arg;
1066 fd_arg.Printf("--fd=%i", pass_comm_fd);
Zachary Turnerecbb0bb2016-09-19 17:54:06 +00001067 debugserver_args.AppendArgument(fd_arg.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001068 // Send "pass_comm_fd" down to the inferior so it can use it to
1069 // communicate back with this process
1070 launch_info.AppendDuplicateFileAction(pass_comm_fd, pass_comm_fd);
1071 }
Greg Claytonc6c420f2016-08-12 16:46:18 +00001072
Kate Stoneb9c1b512016-09-06 20:57:50 +00001073 // use native registers, not the GDB registers
Zachary Turnerecbb0bb2016-09-19 17:54:06 +00001074 debugserver_args.AppendArgument(llvm::StringRef("--native-regs"));
Oleksiy Vyalovf8ce61c2015-01-28 17:36:59 +00001075
Kate Stoneb9c1b512016-09-06 20:57:50 +00001076 if (launch_info.GetLaunchInSeparateProcessGroup()) {
Zachary Turnerecbb0bb2016-09-19 17:54:06 +00001077 debugserver_args.AppendArgument(llvm::StringRef("--setsid"));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001078 }
Greg Clayton91a9b2472013-12-04 19:19:12 +00001079
Kate Stoneb9c1b512016-09-06 20:57:50 +00001080 llvm::SmallString<PATH_MAX> named_pipe_path;
1081 // socket_pipe is used by debug server to communicate back either
1082 // TCP port or domain socket name which it listens on.
1083 // The second purpose of the pipe to serve as a synchronization point -
1084 // once data is written to the pipe, debug server is up and running.
1085 Pipe socket_pipe;
Greg Clayton00fe87b2013-12-05 22:58:22 +00001086
Kate Stoneb9c1b512016-09-06 20:57:50 +00001087 // port is null when debug server should listen on domain socket -
1088 // we're not interested in port value but rather waiting for debug server
1089 // to become available.
1090 if (pass_comm_fd == -1 &&
1091 ((port != nullptr && *port == 0) || port == nullptr)) {
1092 if (url) {
1093// Create a temporary file to get the stdout/stderr and redirect the
1094// output of the command into this file. We will later read this file
1095// if all goes well and fill the data into "command_output_ptr"
Chaoren Lin46951b52015-07-30 17:48:44 +00001096#if defined(__APPLE__)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001097 // Binding to port zero, we need to figure out what port it ends up
1098 // using using a named pipe...
1099 error = socket_pipe.CreateWithUniqueName("debugserver-named-pipe",
1100 false, named_pipe_path);
1101 if (error.Fail()) {
1102 if (log)
1103 log->Printf("GDBRemoteCommunication::%s() "
1104 "named pipe creation failed: %s",
1105 __FUNCTION__, error.AsCString());
1106 return error;
1107 }
Sean Callanan1355f472016-09-19 22:06:12 +00001108 debugserver_args.AppendArgument(llvm::StringRef("--named-pipe"));
Zachary Turner9a4e3012016-09-21 16:01:43 +00001109 debugserver_args.AppendArgument(named_pipe_path);
Chaoren Lin46951b52015-07-30 17:48:44 +00001110#else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001111 // Binding to port zero, we need to figure out what port it ends up
1112 // using using an unnamed pipe...
1113 error = socket_pipe.CreateNew(true);
1114 if (error.Fail()) {
1115 if (log)
1116 log->Printf("GDBRemoteCommunication::%s() "
1117 "unnamed pipe creation failed: %s",
1118 __FUNCTION__, error.AsCString());
1119 return error;
1120 }
1121 int write_fd = socket_pipe.GetWriteFileDescriptor();
Zachary Turnerecbb0bb2016-09-19 17:54:06 +00001122 debugserver_args.AppendArgument(llvm::StringRef("--pipe"));
1123 debugserver_args.AppendArgument(llvm::to_string(write_fd));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001124 launch_info.AppendCloseFileAction(socket_pipe.GetReadFileDescriptor());
Chaoren Lin46951b52015-07-30 17:48:44 +00001125#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +00001126 } else {
1127 // No host and port given, so lets listen on our end and make the
1128 // debugserver
1129 // connect to us..
1130 error = StartListenThread("127.0.0.1", 0);
1131 if (error.Fail()) {
1132 if (log)
1133 log->Printf("GDBRemoteCommunication::%s() unable to start listen "
1134 "thread: %s",
1135 __FUNCTION__, error.AsCString());
1136 return error;
Greg Clayton00fe87b2013-12-05 22:58:22 +00001137 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001138
1139 ConnectionFileDescriptor *connection =
1140 (ConnectionFileDescriptor *)GetConnection();
1141 // Wait for 10 seconds to resolve the bound port
1142 uint16_t port_ = connection->GetListeningPort(10);
1143 if (port_ > 0) {
1144 char port_cstr[32];
1145 snprintf(port_cstr, sizeof(port_cstr), "127.0.0.1:%i", port_);
1146 // Send the host and port down that debugserver and specify an option
1147 // so that it connects back to the port we are listening to in this
1148 // process
Zachary Turnerecbb0bb2016-09-19 17:54:06 +00001149 debugserver_args.AppendArgument(llvm::StringRef("--reverse-connect"));
1150 debugserver_args.AppendArgument(llvm::StringRef(port_cstr));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001151 if (port)
1152 *port = port_;
1153 } else {
1154 error.SetErrorString("failed to bind to port 0 on 127.0.0.1");
1155 if (log)
1156 log->Printf("GDBRemoteCommunication::%s() failed: %s", __FUNCTION__,
1157 error.AsCString());
1158 return error;
Greg Clayton8b82f082011-04-12 05:54:46 +00001159 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001160 }
1161 }
1162
1163 const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE");
1164 if (env_debugserver_log_file) {
1165 ::snprintf(arg_cstr, sizeof(arg_cstr), "--log-file=%s",
1166 env_debugserver_log_file);
Zachary Turnerecbb0bb2016-09-19 17:54:06 +00001167 debugserver_args.AppendArgument(llvm::StringRef(arg_cstr));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001168 }
1169
Vince Harron9753dd92015-05-10 15:22:09 +00001170#if defined(__APPLE__)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001171 const char *env_debugserver_log_flags =
1172 getenv("LLDB_DEBUGSERVER_LOG_FLAGS");
1173 if (env_debugserver_log_flags) {
1174 ::snprintf(arg_cstr, sizeof(arg_cstr), "--log-flags=%s",
1175 env_debugserver_log_flags);
Sean Callanan1355f472016-09-19 22:06:12 +00001176 debugserver_args.AppendArgument(llvm::StringRef(arg_cstr));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001177 }
Vince Harron9753dd92015-05-10 15:22:09 +00001178#else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001179 const char *env_debugserver_log_channels =
1180 getenv("LLDB_SERVER_LOG_CHANNELS");
1181 if (env_debugserver_log_channels) {
1182 ::snprintf(arg_cstr, sizeof(arg_cstr), "--log-channels=%s",
1183 env_debugserver_log_channels);
Zachary Turnerecbb0bb2016-09-19 17:54:06 +00001184 debugserver_args.AppendArgument(llvm::StringRef(arg_cstr));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001185 }
Vince Harron9753dd92015-05-10 15:22:09 +00001186#endif
Todd Fiala34ba4262014-08-29 17:10:31 +00001187
Kate Stoneb9c1b512016-09-06 20:57:50 +00001188 // Add additional args, starting with LLDB_DEBUGSERVER_EXTRA_ARG_1 until an
1189 // env var doesn't come back.
1190 uint32_t env_var_index = 1;
1191 bool has_env_var;
1192 do {
1193 char env_var_name[64];
1194 snprintf(env_var_name, sizeof(env_var_name),
1195 "LLDB_DEBUGSERVER_EXTRA_ARG_%" PRIu32, env_var_index++);
1196 const char *extra_arg = getenv(env_var_name);
1197 has_env_var = extra_arg != nullptr;
Todd Fiala34ba4262014-08-29 17:10:31 +00001198
Kate Stoneb9c1b512016-09-06 20:57:50 +00001199 if (has_env_var) {
Zachary Turnerecbb0bb2016-09-19 17:54:06 +00001200 debugserver_args.AppendArgument(llvm::StringRef(extra_arg));
Todd Fiala7aa4d972016-05-31 18:32:20 +00001201 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001202 log->Printf("GDBRemoteCommunication::%s adding env var %s contents "
1203 "to stub command line (%s)",
1204 __FUNCTION__, env_var_name, extra_arg);
1205 }
1206 } while (has_env_var);
1207
1208 if (inferior_args && inferior_args->GetArgumentCount() > 0) {
Zachary Turnerecbb0bb2016-09-19 17:54:06 +00001209 debugserver_args.AppendArgument(llvm::StringRef("--"));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001210 debugserver_args.AppendArguments(*inferior_args);
1211 }
1212
1213 // Copy the current environment to the gdbserver/debugserver instance
1214 StringList env;
1215 if (Host::GetEnvironment(env)) {
1216 for (size_t i = 0; i < env.GetSize(); ++i)
Zachary Turnerecbb0bb2016-09-19 17:54:06 +00001217 launch_info.GetEnvironmentEntries().AppendArgument(env[i]);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001218 }
1219
1220 // Close STDIN, STDOUT and STDERR.
1221 launch_info.AppendCloseFileAction(STDIN_FILENO);
1222 launch_info.AppendCloseFileAction(STDOUT_FILENO);
1223 launch_info.AppendCloseFileAction(STDERR_FILENO);
1224
1225 // Redirect STDIN, STDOUT and STDERR to "/dev/null".
1226 launch_info.AppendSuppressFileAction(STDIN_FILENO, true, false);
1227 launch_info.AppendSuppressFileAction(STDOUT_FILENO, false, true);
1228 launch_info.AppendSuppressFileAction(STDERR_FILENO, false, true);
1229
1230 if (log) {
1231 StreamString string_stream;
1232 Platform *const platform = nullptr;
1233 launch_info.Dump(string_stream, platform);
1234 log->Printf("launch info for gdb-remote stub:\n%s",
Zachary Turnerc1564272016-11-16 21:15:24 +00001235 string_stream.GetData());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001236 }
1237 error = Host::LaunchProcess(launch_info);
1238
1239 if (error.Success() &&
1240 (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) &&
1241 pass_comm_fd == -1) {
1242 if (named_pipe_path.size() > 0) {
1243 error = socket_pipe.OpenAsReader(named_pipe_path, false);
1244 if (error.Fail())
1245 if (log)
1246 log->Printf("GDBRemoteCommunication::%s() "
1247 "failed to open named pipe %s for reading: %s",
1248 __FUNCTION__, named_pipe_path.c_str(),
1249 error.AsCString());
1250 }
1251
1252 if (socket_pipe.CanWrite())
1253 socket_pipe.CloseWriteFileDescriptor();
1254 if (socket_pipe.CanRead()) {
1255 char port_cstr[PATH_MAX] = {0};
1256 port_cstr[0] = '\0';
1257 size_t num_bytes = sizeof(port_cstr);
1258 // Read port from pipe with 10 second timeout.
1259 error = socket_pipe.ReadWithTimeout(
1260 port_cstr, num_bytes, std::chrono::seconds{10}, num_bytes);
1261 if (error.Success() && (port != nullptr)) {
1262 assert(num_bytes > 0 && port_cstr[num_bytes - 1] == '\0');
1263 *port = StringConvert::ToUInt32(port_cstr, 0);
1264 if (log)
1265 log->Printf("GDBRemoteCommunication::%s() "
1266 "debugserver listens %u port",
1267 __FUNCTION__, *port);
1268 } else {
1269 if (log)
1270 log->Printf("GDBRemoteCommunication::%s() "
1271 "failed to read a port value from pipe %s: %s",
1272 __FUNCTION__, named_pipe_path.c_str(),
1273 error.AsCString());
Todd Fiala7aa4d972016-05-31 18:32:20 +00001274 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001275 socket_pipe.Close();
1276 }
Chaoren Lin368c9f62015-04-27 23:20:30 +00001277
Kate Stoneb9c1b512016-09-06 20:57:50 +00001278 if (named_pipe_path.size() > 0) {
1279 const auto err = socket_pipe.Delete(named_pipe_path);
1280 if (err.Fail()) {
1281 if (log)
1282 log->Printf(
1283 "GDBRemoteCommunication::%s failed to delete pipe %s: %s",
1284 __FUNCTION__, named_pipe_path.c_str(), err.AsCString());
Greg Clayton00fe87b2013-12-05 22:58:22 +00001285 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001286 }
1287
1288 // Make sure we actually connect with the debugserver...
1289 JoinListenThread();
Greg Clayton8b82f082011-04-12 05:54:46 +00001290 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001291 } else {
1292 error.SetErrorStringWithFormat("unable to locate " DEBUGSERVER_BASENAME);
1293 }
Vince Harron8b335672015-05-12 01:10:56 +00001294
Kate Stoneb9c1b512016-09-06 20:57:50 +00001295 if (error.Fail()) {
1296 if (log)
1297 log->Printf("GDBRemoteCommunication::%s() failed: %s", __FUNCTION__,
1298 error.AsCString());
1299 }
Vince Harron8b335672015-05-12 01:10:56 +00001300
Kate Stoneb9c1b512016-09-06 20:57:50 +00001301 return error;
Greg Clayton8b82f082011-04-12 05:54:46 +00001302}
1303
Kate Stoneb9c1b512016-09-06 20:57:50 +00001304void GDBRemoteCommunication::DumpHistory(Stream &strm) { m_history.Dump(strm); }
1305
1306GDBRemoteCommunication::ScopedTimeout::ScopedTimeout(
Pavel Labath3aa04912016-10-31 17:19:42 +00001307 GDBRemoteCommunication &gdb_comm, std::chrono::seconds timeout)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001308 : m_gdb_comm(gdb_comm) {
1309 m_saved_timeout = m_gdb_comm.SetPacketTimeout(timeout);
Greg Claytonc1422c12012-04-09 22:46:21 +00001310}
Tamas Berghammer912800c2015-02-24 10:23:39 +00001311
Kate Stoneb9c1b512016-09-06 20:57:50 +00001312GDBRemoteCommunication::ScopedTimeout::~ScopedTimeout() {
1313 m_gdb_comm.SetPacketTimeout(m_saved_timeout);
Tamas Berghammer912800c2015-02-24 10:23:39 +00001314}
1315
Kate Stoneb9c1b512016-09-06 20:57:50 +00001316// This function is called via the Communications class read thread when bytes
1317// become available
1318// for this connection. This function will consume all incoming bytes and try to
1319// parse whole
1320// packets as they become available. Full packets are placed in a queue, so that
1321// all packet
1322// requests can simply pop from this queue. Async notification packets will be
1323// dispatched
Ewan Crawfordfab40d32015-06-16 15:50:18 +00001324// immediately to the ProcessGDBRemote Async thread via an event.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001325void GDBRemoteCommunication::AppendBytesToCache(const uint8_t *bytes,
1326 size_t len, bool broadcast,
1327 lldb::ConnectionStatus status) {
1328 StringExtractorGDBRemote packet;
Ewan Crawfordfab40d32015-06-16 15:50:18 +00001329
Kate Stoneb9c1b512016-09-06 20:57:50 +00001330 while (true) {
1331 PacketType type = CheckForPacket(bytes, len, packet);
Ewan Crawfordfab40d32015-06-16 15:50:18 +00001332
Kate Stoneb9c1b512016-09-06 20:57:50 +00001333 // scrub the data so we do not pass it back to CheckForPacket
1334 // on future passes of the loop
1335 bytes = nullptr;
1336 len = 0;
Ewan Crawfordfab40d32015-06-16 15:50:18 +00001337
Kate Stoneb9c1b512016-09-06 20:57:50 +00001338 // we may have received no packet so lets bail out
1339 if (type == PacketType::Invalid)
1340 break;
Ewan Crawfordfab40d32015-06-16 15:50:18 +00001341
Kate Stoneb9c1b512016-09-06 20:57:50 +00001342 if (type == PacketType::Standard) {
1343 // scope for the mutex
1344 {
1345 // lock down the packet queue
1346 std::lock_guard<std::mutex> guard(m_packet_queue_mutex);
1347 // push a new packet into the queue
1348 m_packet_queue.push(packet);
1349 // Signal condition variable that we have a packet
1350 m_condition_queue_not_empty.notify_one();
1351 }
Ewan Crawfordfab40d32015-06-16 15:50:18 +00001352 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001353
1354 if (type == PacketType::Notify) {
1355 // put this packet into an event
1356 const char *pdata = packet.GetStringRef().c_str();
1357
1358 // as the communication class, we are a broadcaster and the
1359 // async thread is tuned to listen to us
1360 BroadcastEvent(eBroadcastBitGdbReadThreadGotNotify,
1361 new EventDataBytes(pdata));
1362 }
1363 }
Ewan Crawfordfab40d32015-06-16 15:50:18 +00001364}