blob: bd378ce95dd465d1b4bbb34f8b11fff91795d3d5 [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
10
11#include "GDBRemoteCommunication.h"
12
13// C Includes
Johnny Chena5663552011-05-13 20:07:25 +000014#include <limits.h>
Stephen Wilsona78867b2011-03-25 18:16:28 +000015#include <string.h>
Greg Clayton91a9b2472013-12-04 19:19:12 +000016#include <sys/stat.h>
Stephen Wilsona78867b2011-03-25 18:16:28 +000017
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018// C++ Includes
19// Other libraries and framework includes
Greg Clayton00fe87b2013-12-05 22:58:22 +000020#include "lldb/Core/ConnectionFileDescriptor.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/Log.h"
Greg Claytonc1422c12012-04-09 22:46:21 +000022#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/StreamString.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000024#include "lldb/Host/FileSpec.h"
Zachary Turnerc00cf4a2014-08-15 22:04:21 +000025#include "lldb/Host/FileSystem.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000026#include "lldb/Host/Host.h"
Zachary Turner98688922014-08-06 18:16:26 +000027#include "lldb/Host/Socket.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Host/TimeValue.h"
Greg Clayton8b82f082011-04-12 05:54:46 +000029#include "lldb/Target/Process.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
31// Project includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "ProcessGDBRemoteLog.h"
33
Todd Fiala015d8182014-07-22 23:41:36 +000034#if defined(__APPLE__)
35# define DEBUGSERVER_BASENAME "debugserver"
36#else
37# define DEBUGSERVER_BASENAME "lldb-gdbserver"
38#endif
Greg Clayton8b82f082011-04-12 05:54:46 +000039
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040using namespace lldb;
41using namespace lldb_private;
42
Greg Claytonc1422c12012-04-09 22:46:21 +000043GDBRemoteCommunication::History::History (uint32_t size) :
44 m_packets(),
45 m_curr_idx (0),
46 m_total_packet_count (0),
47 m_dumped_to_log (false)
48{
49 m_packets.resize(size);
50}
51
52GDBRemoteCommunication::History::~History ()
53{
54}
55
56void
Greg Claytond451c1a2012-04-13 21:24:18 +000057GDBRemoteCommunication::History::AddPacket (char packet_char,
58 PacketType type,
59 uint32_t bytes_transmitted)
60{
61 const size_t size = m_packets.size();
62 if (size > 0)
63 {
64 const uint32_t idx = GetNextIndex();
65 m_packets[idx].packet.assign (1, packet_char);
66 m_packets[idx].type = type;
67 m_packets[idx].bytes_transmitted = bytes_transmitted;
68 m_packets[idx].packet_idx = m_total_packet_count;
69 m_packets[idx].tid = Host::GetCurrentThreadID();
70 }
71}
72
73void
74GDBRemoteCommunication::History::AddPacket (const std::string &src,
75 uint32_t src_len,
76 PacketType type,
77 uint32_t bytes_transmitted)
78{
79 const size_t size = m_packets.size();
80 if (size > 0)
81 {
82 const uint32_t idx = GetNextIndex();
83 m_packets[idx].packet.assign (src, 0, src_len);
84 m_packets[idx].type = type;
85 m_packets[idx].bytes_transmitted = bytes_transmitted;
86 m_packets[idx].packet_idx = m_total_packet_count;
87 m_packets[idx].tid = Host::GetCurrentThreadID();
88 }
89}
90
91void
Greg Claytonc1422c12012-04-09 22:46:21 +000092GDBRemoteCommunication::History::Dump (lldb_private::Stream &strm) const
93{
94 const uint32_t size = GetNumPacketsInHistory ();
95 const uint32_t first_idx = GetFirstSavedPacketIndex ();
96 const uint32_t stop_idx = m_curr_idx + size;
97 for (uint32_t i = first_idx; i < stop_idx; ++i)
98 {
99 const uint32_t idx = NormalizeIndex (i);
100 const Entry &entry = m_packets[idx];
101 if (entry.type == ePacketTypeInvalid || entry.packet.empty())
102 break;
Daniel Malead01b2952012-11-29 21:49:15 +0000103 strm.Printf ("history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s\n",
Greg Claytonc1422c12012-04-09 22:46:21 +0000104 entry.packet_idx,
Greg Claytond451c1a2012-04-13 21:24:18 +0000105 entry.tid,
Greg Claytonc1422c12012-04-09 22:46:21 +0000106 entry.bytes_transmitted,
107 (entry.type == ePacketTypeSend) ? "send" : "read",
108 entry.packet.c_str());
109 }
110}
111
112void
113GDBRemoteCommunication::History::Dump (lldb_private::Log *log) const
114{
115 if (log && !m_dumped_to_log)
116 {
117 m_dumped_to_log = true;
118 const uint32_t size = GetNumPacketsInHistory ();
119 const uint32_t first_idx = GetFirstSavedPacketIndex ();
120 const uint32_t stop_idx = m_curr_idx + size;
121 for (uint32_t i = first_idx; i < stop_idx; ++i)
122 {
123 const uint32_t idx = NormalizeIndex (i);
124 const Entry &entry = m_packets[idx];
125 if (entry.type == ePacketTypeInvalid || entry.packet.empty())
126 break;
Daniel Malead01b2952012-11-29 21:49:15 +0000127 log->Printf ("history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s",
Greg Claytonc1422c12012-04-09 22:46:21 +0000128 entry.packet_idx,
Greg Claytond451c1a2012-04-13 21:24:18 +0000129 entry.tid,
Greg Claytonc1422c12012-04-09 22:46:21 +0000130 entry.bytes_transmitted,
131 (entry.type == ePacketTypeSend) ? "send" : "read",
132 entry.packet.c_str());
133 }
134 }
135}
136
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137//----------------------------------------------------------------------
138// GDBRemoteCommunication constructor
139//----------------------------------------------------------------------
Greg Clayton8b82f082011-04-12 05:54:46 +0000140GDBRemoteCommunication::GDBRemoteCommunication(const char *comm_name,
141 const char *listener_name,
142 bool is_platform) :
Greg Clayton576d8832011-03-22 04:00:09 +0000143 Communication(comm_name),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000144#ifdef LLDB_CONFIGURATION_DEBUG
145 m_packet_timeout (1000),
146#else
Greg Claytonf3dd93c2011-06-17 03:31:01 +0000147 m_packet_timeout (1),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000148#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149 m_sequence_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton4dc72282011-01-20 07:53:45 +0000150 m_public_is_running (false),
Greg Clayton1cb64962011-03-24 04:28:38 +0000151 m_private_is_running (false),
Greg Claytonc1422c12012-04-09 22:46:21 +0000152 m_history (512),
Greg Clayton8b82f082011-04-12 05:54:46 +0000153 m_send_acks (true),
Greg Clayton00fe87b2013-12-05 22:58:22 +0000154 m_is_platform (is_platform),
155 m_listen_thread (LLDB_INVALID_HOST_THREAD),
156 m_listen_url ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158}
159
160//----------------------------------------------------------------------
161// Destructor
162//----------------------------------------------------------------------
163GDBRemoteCommunication::~GDBRemoteCommunication()
164{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165 if (IsConnected())
166 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167 Disconnect();
168 }
169}
170
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171char
172GDBRemoteCommunication::CalculcateChecksum (const char *payload, size_t payload_length)
173{
174 int checksum = 0;
175
Ed Mastea6b4c772013-08-20 14:12:58 +0000176 for (size_t i = 0; i < payload_length; ++i)
177 checksum += payload[i];
178
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 return checksum & 255;
180}
181
182size_t
Greg Clayton6ed95942011-01-22 07:12:45 +0000183GDBRemoteCommunication::SendAck ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184{
Greg Clayton5160ce52013-03-27 23:08:40 +0000185 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186 ConnectionStatus status = eConnectionStatusSuccess;
Greg Claytonc1422c12012-04-09 22:46:21 +0000187 char ch = '+';
188 const size_t bytes_written = Write (&ch, 1, status, NULL);
189 if (log)
Greg Clayton45989072013-10-23 18:24:30 +0000190 log->Printf ("<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch);
Greg Claytonc1422c12012-04-09 22:46:21 +0000191 m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written);
192 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193}
194
195size_t
Greg Clayton6ed95942011-01-22 07:12:45 +0000196GDBRemoteCommunication::SendNack ()
197{
Greg Clayton5160ce52013-03-27 23:08:40 +0000198 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Greg Clayton6ed95942011-01-22 07:12:45 +0000199 ConnectionStatus status = eConnectionStatusSuccess;
Greg Claytonc1422c12012-04-09 22:46:21 +0000200 char ch = '-';
201 const size_t bytes_written = Write (&ch, 1, status, NULL);
202 if (log)
Greg Clayton45989072013-10-23 18:24:30 +0000203 log->Printf("<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch);
Greg Claytonc1422c12012-04-09 22:46:21 +0000204 m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written);
205 return bytes_written;
Greg Clayton32e0a752011-03-30 18:16:51 +0000206}
207
Greg Clayton3dedae12013-12-06 21:45:27 +0000208GDBRemoteCommunication::PacketResult
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209GDBRemoteCommunication::SendPacket (const char *payload, size_t payload_length)
210{
211 Mutex::Locker locker(m_sequence_mutex);
212 return SendPacketNoLock (payload, payload_length);
213}
214
Greg Clayton3dedae12013-12-06 21:45:27 +0000215GDBRemoteCommunication::PacketResult
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000216GDBRemoteCommunication::SendPacketNoLock (const char *payload, size_t payload_length)
217{
218 if (IsConnected())
219 {
220 StreamString packet(0, 4, eByteOrderBig);
221
222 packet.PutChar('$');
223 packet.Write (payload, payload_length);
224 packet.PutChar('#');
225 packet.PutHex8(CalculcateChecksum (payload, payload_length));
226
Greg Clayton5160ce52013-03-27 23:08:40 +0000227 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228 ConnectionStatus status = eConnectionStatusSuccess;
229 size_t bytes_written = Write (packet.GetData(), packet.GetSize(), status, NULL);
Greg Claytonc1422c12012-04-09 22:46:21 +0000230 if (log)
231 {
232 // If logging was just enabled and we have history, then dump out what
233 // we have to the log so we get the historical context. The Dump() call that
234 // logs all of the packet will set a boolean so that we don't dump this more
235 // than once
236 if (!m_history.DidDumpToLog ())
Greg Clayton5160ce52013-03-27 23:08:40 +0000237 m_history.Dump (log);
Greg Claytonc1422c12012-04-09 22:46:21 +0000238
Greg Clayton45989072013-10-23 18:24:30 +0000239 log->Printf("<%4" PRIu64 "> send packet: %.*s", (uint64_t)bytes_written, (int)packet.GetSize(), packet.GetData());
Greg Claytonc1422c12012-04-09 22:46:21 +0000240 }
241
242 m_history.AddPacket (packet.GetString(), packet.GetSize(), History::ePacketTypeSend, bytes_written);
243
244
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245 if (bytes_written == packet.GetSize())
246 {
Greg Clayton71fc2a32011-02-12 06:28:37 +0000247 if (GetSendAcks ())
Greg Clayton3dedae12013-12-06 21:45:27 +0000248 return GetAck ();
249 else
250 return PacketResult::Success;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251 }
Johnny Chend0c40dd2010-09-14 22:10:43 +0000252 else
253 {
Greg Clayton6d093452011-02-05 02:25:06 +0000254 if (log)
Greg Clayton5fe15d22011-05-20 03:15:54 +0000255 log->Printf ("error: failed to send packet: %.*s", (int)packet.GetSize(), packet.GetData());
Johnny Chend0c40dd2010-09-14 22:10:43 +0000256 }
Greg Claytonf5e56de2010-09-14 23:36:40 +0000257 }
Greg Clayton3dedae12013-12-06 21:45:27 +0000258 return PacketResult::ErrorSendFailed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259}
260
Greg Clayton3dedae12013-12-06 21:45:27 +0000261GDBRemoteCommunication::PacketResult
Greg Claytonc574ede2011-03-10 02:26:48 +0000262GDBRemoteCommunication::GetAck ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263{
Greg Clayton576d8832011-03-22 04:00:09 +0000264 StringExtractorGDBRemote packet;
Greg Clayton3dedae12013-12-06 21:45:27 +0000265 PacketResult result = WaitForPacketWithTimeoutMicroSecondsNoLock (packet, GetPacketTimeoutInMicroSeconds ());
266 if (result == PacketResult::Success)
267 {
268 if (packet.GetResponseType() == StringExtractorGDBRemote::ResponseType::eAck)
269 return PacketResult::Success;
270 else
271 return PacketResult::ErrorSendAck;
272 }
273 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000274}
275
276bool
Jim Ingham4ceb9282012-06-08 22:50:40 +0000277GDBRemoteCommunication::GetSequenceMutex (Mutex::Locker& locker, const char *failure_message)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278{
Greg Claytond9896732012-05-31 16:54:51 +0000279 if (IsRunning())
Jim Ingham4ceb9282012-06-08 22:50:40 +0000280 return locker.TryLock (m_sequence_mutex, failure_message);
Greg Claytond9896732012-05-31 16:54:51 +0000281
282 locker.Lock (m_sequence_mutex);
283 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284}
285
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286
Greg Clayton6779606a2011-01-22 23:43:18 +0000287bool
Greg Clayton6779606a2011-01-22 23:43:18 +0000288GDBRemoteCommunication::WaitForNotRunningPrivate (const TimeValue *timeout_ptr)
289{
290 return m_private_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL);
291}
292
Greg Clayton3dedae12013-12-06 21:45:27 +0000293GDBRemoteCommunication::PacketResult
Greg Clayton73bf5db2011-06-17 01:22:15 +0000294GDBRemoteCommunication::WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &packet, uint32_t timeout_usec)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295{
Greg Clayton73bf5db2011-06-17 01:22:15 +0000296 uint8_t buffer[8192];
297 Error error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298
Greg Clayton5160ce52013-03-27 23:08:40 +0000299 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS | GDBR_LOG_VERBOSE));
Greg Clayton644247c2011-07-07 01:59:51 +0000300
Greg Clayton73bf5db2011-06-17 01:22:15 +0000301 // Check for a packet from our cache first without trying any reading...
302 if (CheckForPacket (NULL, 0, packet))
Greg Clayton3dedae12013-12-06 21:45:27 +0000303 return PacketResult::Success;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304
Greg Clayton0c51ac32011-07-02 23:21:06 +0000305 bool timed_out = false;
Greg Clayton3dedae12013-12-06 21:45:27 +0000306 bool disconnected = false;
Greg Clayton0c51ac32011-07-02 23:21:06 +0000307 while (IsConnected() && !timed_out)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308 {
Johnny Chen74549c82011-07-19 01:13:00 +0000309 lldb::ConnectionStatus status = eConnectionStatusNoConnection;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000310 size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error);
Greg Clayton644247c2011-07-07 01:59:51 +0000311
312 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000313 log->Printf ("%s: Read (buffer, (sizeof(buffer), timeout_usec = 0x%x, status = %s, error = %s) => bytes_read = %" PRIu64,
Greg Clayton644247c2011-07-07 01:59:51 +0000314 __PRETTY_FUNCTION__,
315 timeout_usec,
316 Communication::ConnectionStatusAsCString (status),
317 error.AsCString(),
Greg Clayton43e0af02012-09-18 18:04:04 +0000318 (uint64_t)bytes_read);
Greg Clayton644247c2011-07-07 01:59:51 +0000319
Greg Clayton73bf5db2011-06-17 01:22:15 +0000320 if (bytes_read > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000322 if (CheckForPacket (buffer, bytes_read, packet))
Greg Clayton3dedae12013-12-06 21:45:27 +0000323 return PacketResult::Success;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000324 }
325 else
326 {
327 switch (status)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328 {
Greg Clayton197bacf2011-07-02 21:07:54 +0000329 case eConnectionStatusTimedOut:
Greg Claytonf0066ad2014-05-02 00:45:31 +0000330 case eConnectionStatusInterrupted:
Greg Clayton0c51ac32011-07-02 23:21:06 +0000331 timed_out = true;
332 break;
333 case eConnectionStatusSuccess:
334 //printf ("status = success but error = %s\n", error.AsCString("<invalid>"));
Greg Clayton73bf5db2011-06-17 01:22:15 +0000335 break;
336
337 case eConnectionStatusEndOfFile:
338 case eConnectionStatusNoConnection:
339 case eConnectionStatusLostConnection:
340 case eConnectionStatusError:
Greg Clayton3dedae12013-12-06 21:45:27 +0000341 disconnected = true;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000342 Disconnect();
343 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344 }
345 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346 }
Greg Clayton3dedae12013-12-06 21:45:27 +0000347 packet.Clear ();
348 if (disconnected)
349 return PacketResult::ErrorDisconnected;
350 if (timed_out)
351 return PacketResult::ErrorReplyTimeout;
352 else
353 return PacketResult::ErrorReplyFailed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354}
355
Greg Clayton73bf5db2011-06-17 01:22:15 +0000356bool
357GDBRemoteCommunication::CheckForPacket (const uint8_t *src, size_t src_len, StringExtractorGDBRemote &packet)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358{
359 // Put the packet data into the buffer in a thread safe fashion
360 Mutex::Locker locker(m_bytes_mutex);
Greg Clayton197bacf2011-07-02 21:07:54 +0000361
Greg Clayton5160ce52013-03-27 23:08:40 +0000362 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Greg Clayton197bacf2011-07-02 21:07:54 +0000363
Greg Clayton73bf5db2011-06-17 01:22:15 +0000364 if (src && src_len > 0)
Greg Clayton197bacf2011-07-02 21:07:54 +0000365 {
Greg Clayton0c51ac32011-07-02 23:21:06 +0000366 if (log && log->GetVerbose())
Greg Clayton197bacf2011-07-02 21:07:54 +0000367 {
368 StreamString s;
Greg Clayton0c51ac32011-07-02 23:21:06 +0000369 log->Printf ("GDBRemoteCommunication::%s adding %u bytes: %.*s",
370 __FUNCTION__,
371 (uint32_t)src_len,
372 (uint32_t)src_len,
373 src);
Greg Clayton197bacf2011-07-02 21:07:54 +0000374 }
Greg Clayton73bf5db2011-06-17 01:22:15 +0000375 m_bytes.append ((const char *)src, src_len);
Greg Clayton197bacf2011-07-02 21:07:54 +0000376 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377
378 // Parse up the packets into gdb remote packets
Greg Clayton197bacf2011-07-02 21:07:54 +0000379 if (!m_bytes.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000380 {
381 // end_idx must be one past the last valid packet byte. Start
382 // it off with an invalid value that is the same as the current
383 // index.
Greg Clayton73bf5db2011-06-17 01:22:15 +0000384 size_t content_start = 0;
385 size_t content_length = 0;
386 size_t total_length = 0;
387 size_t checksum_idx = std::string::npos;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388
389 switch (m_bytes[0])
390 {
391 case '+': // Look for ack
392 case '-': // Look for cancel
393 case '\x03': // ^C to halt target
Greg Clayton73bf5db2011-06-17 01:22:15 +0000394 content_length = total_length = 1; // The command is one byte long...
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395 break;
396
397 case '$':
398 // Look for a standard gdb packet?
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000400 size_t hash_pos = m_bytes.find('#');
401 if (hash_pos != std::string::npos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000403 if (hash_pos + 2 < m_bytes.size())
404 {
405 checksum_idx = hash_pos + 1;
406 // Skip the dollar sign
407 content_start = 1;
408 // Don't include the # in the content or the $ in the content length
409 content_length = hash_pos - 1;
410
411 total_length = hash_pos + 3; // Skip the # and the two hex checksum bytes
412 }
413 else
414 {
415 // Checksum bytes aren't all here yet
416 content_length = std::string::npos;
417 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418 }
419 }
420 break;
421
422 default:
Greg Claytonf3dd93c2011-06-17 03:31:01 +0000423 {
Greg Clayton197bacf2011-07-02 21:07:54 +0000424 // We have an unexpected byte and we need to flush all bad
425 // data that is in m_bytes, so we need to find the first
426 // byte that is a '+' (ACK), '-' (NACK), \x03 (CTRL+C interrupt),
427 // or '$' character (start of packet header) or of course,
428 // the end of the data in m_bytes...
429 const size_t bytes_len = m_bytes.size();
430 bool done = false;
431 uint32_t idx;
432 for (idx = 1; !done && idx < bytes_len; ++idx)
433 {
434 switch (m_bytes[idx])
435 {
436 case '+':
437 case '-':
438 case '\x03':
439 case '$':
440 done = true;
441 break;
442
443 default:
444 break;
445 }
446 }
Greg Claytonf3dd93c2011-06-17 03:31:01 +0000447 if (log)
Greg Clayton197bacf2011-07-02 21:07:54 +0000448 log->Printf ("GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'",
449 __FUNCTION__, idx, idx, m_bytes.c_str());
450 m_bytes.erase(0, idx);
Greg Claytonf3dd93c2011-06-17 03:31:01 +0000451 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452 break;
453 }
454
Greg Clayton73bf5db2011-06-17 01:22:15 +0000455 if (content_length == std::string::npos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000457 packet.Clear();
458 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459 }
Greg Claytonf3dd93c2011-06-17 03:31:01 +0000460 else if (total_length > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000462
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463 // We have a valid packet...
Greg Clayton73bf5db2011-06-17 01:22:15 +0000464 assert (content_length <= m_bytes.size());
465 assert (total_length <= m_bytes.size());
466 assert (content_length <= total_length);
Greg Clayton06f09b52014-06-20 20:41:07 +0000467 const size_t content_end = content_start + content_length;
468
Greg Clayton73bf5db2011-06-17 01:22:15 +0000469 bool success = true;
470 std::string &packet_str = packet.GetStringRef();
Greg Claytonc1422c12012-04-09 22:46:21 +0000471
472
473 if (log)
474 {
475 // If logging was just enabled and we have history, then dump out what
476 // we have to the log so we get the historical context. The Dump() call that
477 // logs all of the packet will set a boolean so that we don't dump this more
478 // than once
479 if (!m_history.DidDumpToLog ())
Greg Clayton5160ce52013-03-27 23:08:40 +0000480 m_history.Dump (log);
Greg Claytonc1422c12012-04-09 22:46:21 +0000481
Greg Clayton06f09b52014-06-20 20:41:07 +0000482 bool binary = false;
483 // Only detect binary for packets that start with a '$' and have a '#CC' checksum
484 if (m_bytes[0] == '$' && total_length > 4)
485 {
486 for (size_t i=0; !binary && i<total_length; ++i)
487 {
488 if (isprint(m_bytes[i]) == 0)
489 binary = true;
490 }
491 }
492 if (binary)
493 {
494 StreamString strm;
495 // Packet header...
496 strm.Printf("<%4" PRIu64 "> read packet: %c", (uint64_t)total_length, m_bytes[0]);
497 for (size_t i=content_start; i<content_end; ++i)
498 {
499 // Remove binary escaped bytes when displaying the packet...
500 const char ch = m_bytes[i];
501 if (ch == 0x7d)
502 {
503 // 0x7d is the escape character. The next character is to
504 // be XOR'd with 0x20.
505 const char escapee = m_bytes[++i] ^ 0x20;
506 strm.Printf("%2.2x", escapee);
507 }
508 else
509 {
510 strm.Printf("%2.2x", (uint8_t)ch);
511 }
512 }
513 // Packet footer...
514 strm.Printf("%c%c%c", m_bytes[total_length-3], m_bytes[total_length-2], m_bytes[total_length-1]);
515 log->PutCString(strm.GetString().c_str());
516 }
517 else
518 {
519 log->Printf("<%4" PRIu64 "> read packet: %.*s", (uint64_t)total_length, (int)(total_length), m_bytes.c_str());
520 }
Greg Claytonc1422c12012-04-09 22:46:21 +0000521 }
522
523 m_history.AddPacket (m_bytes.c_str(), total_length, History::ePacketTypeRecv, total_length);
524
Hafiz Abid Qadeere5fd5e12013-08-28 15:10:37 +0000525 // Clear packet_str in case there is some existing data in it.
526 packet_str.clear();
Hafiz Abid Qadeerda96ef22013-08-28 10:31:52 +0000527 // Copy the packet from m_bytes to packet_str expanding the
528 // run-length encoding in the process.
529 // Reserve enough byte for the most common case (no RLE used)
530 packet_str.reserve(m_bytes.length());
Greg Clayton06f09b52014-06-20 20:41:07 +0000531 for (std::string::const_iterator c = m_bytes.begin() + content_start; c != m_bytes.begin() + content_end; ++c)
Hafiz Abid Qadeerda96ef22013-08-28 10:31:52 +0000532 {
533 if (*c == '*')
534 {
535 // '*' indicates RLE. Next character will give us the
536 // repeat count and previous character is what is to be
537 // repeated.
538 char char_to_repeat = packet_str.back();
539 // Number of time the previous character is repeated
540 int repeat_count = *++c + 3 - ' ';
541 // We have the char_to_repeat and repeat_count. Now push
542 // it in the packet.
543 for (int i = 0; i < repeat_count; ++i)
544 packet_str.push_back(char_to_repeat);
545 }
Steve Pucci3c5d3332014-02-24 19:07:29 +0000546 else if (*c == 0x7d)
547 {
548 // 0x7d is the escape character. The next character is to
549 // be XOR'd with 0x20.
550 char escapee = *++c ^ 0x20;
551 packet_str.push_back(escapee);
552 }
Hafiz Abid Qadeerda96ef22013-08-28 10:31:52 +0000553 else
554 {
555 packet_str.push_back(*c);
556 }
557 }
558
Greg Clayton73bf5db2011-06-17 01:22:15 +0000559 if (m_bytes[0] == '$')
560 {
561 assert (checksum_idx < m_bytes.size());
562 if (::isxdigit (m_bytes[checksum_idx+0]) ||
563 ::isxdigit (m_bytes[checksum_idx+1]))
564 {
565 if (GetSendAcks ())
566 {
567 const char *packet_checksum_cstr = &m_bytes[checksum_idx];
568 char packet_checksum = strtol (packet_checksum_cstr, NULL, 16);
569 char actual_checksum = CalculcateChecksum (packet_str.c_str(), packet_str.size());
570 success = packet_checksum == actual_checksum;
571 if (!success)
572 {
573 if (log)
574 log->Printf ("error: checksum mismatch: %.*s expected 0x%2.2x, got 0x%2.2x",
575 (int)(total_length),
576 m_bytes.c_str(),
577 (uint8_t)packet_checksum,
578 (uint8_t)actual_checksum);
579 }
580 // Send the ack or nack if needed
581 if (!success)
582 SendNack();
583 else
584 SendAck();
585 }
Greg Clayton73bf5db2011-06-17 01:22:15 +0000586 }
587 else
588 {
589 success = false;
590 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000591 log->Printf ("error: invalid checksum in packet: '%s'\n", m_bytes.c_str());
Greg Clayton73bf5db2011-06-17 01:22:15 +0000592 }
593 }
Greg Claytonc1422c12012-04-09 22:46:21 +0000594
Greg Clayton73bf5db2011-06-17 01:22:15 +0000595 m_bytes.erase(0, total_length);
596 packet.SetFilePos(0);
597 return success;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599 }
Greg Clayton73bf5db2011-06-17 01:22:15 +0000600 packet.Clear();
601 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602}
603
Greg Clayton8b82f082011-04-12 05:54:46 +0000604Error
Greg Claytond6299802013-12-06 17:46:35 +0000605GDBRemoteCommunication::StartListenThread (const char *hostname, uint16_t port)
Greg Clayton00fe87b2013-12-05 22:58:22 +0000606{
607 Error error;
608 if (IS_VALID_LLDB_HOST_THREAD(m_listen_thread))
609 {
610 error.SetErrorString("listen thread already running");
611 }
612 else
613 {
614 char listen_url[512];
615 if (hostname && hostname[0])
Jean-Daniel Dupas3c6774a2014-02-08 20:29:40 +0000616 snprintf(listen_url, sizeof(listen_url), "listen://%s:%i", hostname, port);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000617 else
618 snprintf(listen_url, sizeof(listen_url), "listen://%i", port);
619 m_listen_url = listen_url;
620 SetConnection(new ConnectionFileDescriptor());
621 m_listen_thread = Host::ThreadCreate (listen_url, GDBRemoteCommunication::ListenThread, this, &error);
622 }
623 return error;
624}
625
626bool
627GDBRemoteCommunication::JoinListenThread ()
628{
629 if (IS_VALID_LLDB_HOST_THREAD(m_listen_thread))
630 {
631 Host::ThreadJoin(m_listen_thread, NULL, NULL);
632 m_listen_thread = LLDB_INVALID_HOST_THREAD;
633 }
634 return true;
635}
636
637lldb::thread_result_t
638GDBRemoteCommunication::ListenThread (lldb::thread_arg_t arg)
639{
640 GDBRemoteCommunication *comm = (GDBRemoteCommunication *)arg;
641 Error error;
642 ConnectionFileDescriptor *connection = (ConnectionFileDescriptor *)comm->GetConnection ();
643
644 if (connection)
645 {
646 // Do the listen on another thread so we can continue on...
647 if (connection->Connect(comm->m_listen_url.c_str(), &error) != eConnectionStatusSuccess)
648 comm->SetConnection(NULL);
649 }
650 return NULL;
651}
652
653Error
Greg Claytonfda4fab2014-01-10 22:24:11 +0000654GDBRemoteCommunication::StartDebugserverProcess (const char *hostname,
655 uint16_t in_port,
Greg Clayton91a9b2472013-12-04 19:19:12 +0000656 lldb_private::ProcessLaunchInfo &launch_info,
Greg Claytonfda4fab2014-01-10 22:24:11 +0000657 uint16_t &out_port)
Greg Clayton8b82f082011-04-12 05:54:46 +0000658{
Todd Fiala015d8182014-07-22 23:41:36 +0000659 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
660 if (log)
661 log->Printf ("GDBRemoteCommunication::%s(hostname=%s, in_port=%" PRIu16 ", out_port=%" PRIu16, __FUNCTION__, hostname ? hostname : "<empty>", in_port, out_port);
662
Greg Claytonfda4fab2014-01-10 22:24:11 +0000663 out_port = in_port;
Greg Clayton8b82f082011-04-12 05:54:46 +0000664 Error error;
665 // If we locate debugserver, keep that located version around
666 static FileSpec g_debugserver_file_spec;
667
Greg Clayton8b82f082011-04-12 05:54:46 +0000668 char debugserver_path[PATH_MAX];
669 FileSpec &debugserver_file_spec = launch_info.GetExecutableFile();
670
671 // Always check to see if we have an environment override for the path
672 // to the debugserver to use and use it if we do.
673 const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH");
674 if (env_debugserver_path)
Todd Fiala015d8182014-07-22 23:41:36 +0000675 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000676 debugserver_file_spec.SetFile (env_debugserver_path, false);
Todd Fiala015d8182014-07-22 23:41:36 +0000677 if (log)
678 log->Printf ("GDBRemoteCommunication::%s() gdb-remote stub exe path set from environment variable: %s", __FUNCTION__, env_debugserver_path);
679 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000680 else
681 debugserver_file_spec = g_debugserver_file_spec;
682 bool debugserver_exists = debugserver_file_spec.Exists();
683 if (!debugserver_exists)
684 {
685 // The debugserver binary is in the LLDB.framework/Resources
686 // directory.
687 if (Host::GetLLDBPath (ePathTypeSupportExecutableDir, debugserver_file_spec))
688 {
689 debugserver_file_spec.GetFilename().SetCString(DEBUGSERVER_BASENAME);
690 debugserver_exists = debugserver_file_spec.Exists();
691 if (debugserver_exists)
692 {
Todd Fiala015d8182014-07-22 23:41:36 +0000693 if (log)
694 log->Printf ("GDBRemoteCommunication::%s() found gdb-remote stub exe '%s'", __FUNCTION__, debugserver_file_spec.GetPath ().c_str ());
695
Greg Clayton8b82f082011-04-12 05:54:46 +0000696 g_debugserver_file_spec = debugserver_file_spec;
697 }
698 else
699 {
Todd Fiala015d8182014-07-22 23:41:36 +0000700 if (log)
701 log->Printf ("GDBRemoteCommunication::%s() could not find gdb-remote stub exe '%s'", __FUNCTION__, debugserver_file_spec.GetPath ().c_str ());
702
Greg Clayton8b82f082011-04-12 05:54:46 +0000703 g_debugserver_file_spec.Clear();
704 debugserver_file_spec.Clear();
705 }
706 }
707 }
708
709 if (debugserver_exists)
710 {
711 debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path));
712
713 Args &debugserver_args = launch_info.GetArguments();
714 debugserver_args.Clear();
715 char arg_cstr[PATH_MAX];
716
717 // Start args with "debugserver /file/path -r --"
718 debugserver_args.AppendArgument(debugserver_path);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000719
720 // If a host and port is supplied then use it
Greg Claytonfda4fab2014-01-10 22:24:11 +0000721 char host_and_port[128];
722 if (hostname)
723 {
724 snprintf (host_and_port, sizeof(host_and_port), "%s:%u", hostname, in_port);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000725 debugserver_args.AppendArgument(host_and_port);
Greg Claytonfda4fab2014-01-10 22:24:11 +0000726 }
727 else
728 {
729 host_and_port[0] = '\0';
730 }
731
Greg Clayton8b82f082011-04-12 05:54:46 +0000732 // use native registers, not the GDB registers
733 debugserver_args.AppendArgument("--native-regs");
734 // make debugserver run in its own session so signals generated by
735 // special terminal key sequences (^C) don't affect debugserver
736 debugserver_args.AppendArgument("--setsid");
Greg Clayton91a9b2472013-12-04 19:19:12 +0000737
Greg Clayton00fe87b2013-12-05 22:58:22 +0000738 char named_pipe_path[PATH_MAX];
Jason Molenda6e205542014-01-25 03:57:13 +0000739 named_pipe_path[0] = '\0';
Greg Clayton00fe87b2013-12-05 22:58:22 +0000740
Greg Claytonfda4fab2014-01-10 22:24:11 +0000741 bool listen = false;
742 if (host_and_port[0])
Greg Clayton91a9b2472013-12-04 19:19:12 +0000743 {
Greg Clayton00fe87b2013-12-05 22:58:22 +0000744 // Create a temporary file to get the stdout/stderr and redirect the
745 // output of the command into this file. We will later read this file
746 // if all goes well and fill the data into "command_output_ptr"
Greg Clayton00fe87b2013-12-05 22:58:22 +0000747
Greg Claytonfda4fab2014-01-10 22:24:11 +0000748 if (in_port == 0)
Greg Clayton00fe87b2013-12-05 22:58:22 +0000749 {
Greg Claytonfda4fab2014-01-10 22:24:11 +0000750 // Binding to port zero, we need to figure out what port it ends up
751 // using using a named pipe...
752 FileSpec tmpdir_file_spec;
753 if (Host::GetLLDBPath (ePathTypeLLDBTempSystemDir, tmpdir_file_spec))
Greg Clayton00fe87b2013-12-05 22:58:22 +0000754 {
Greg Claytonfda4fab2014-01-10 22:24:11 +0000755 tmpdir_file_spec.GetFilename().SetCString("debugserver-named-pipe.XXXXXX");
756 strncpy(named_pipe_path, tmpdir_file_spec.GetPath().c_str(), sizeof(named_pipe_path));
757 }
758 else
759 {
760 strncpy(named_pipe_path, "/tmp/debugserver-named-pipe.XXXXXX", sizeof(named_pipe_path));
761 }
762
763 if (::mktemp (named_pipe_path))
764 {
Hafiz Abid Qadeer6eff1012014-03-12 10:45:23 +0000765#if defined(_WIN32)
Deepak Panickalb36da432014-01-13 14:55:15 +0000766 if ( false )
767#else
Greg Claytonfda4fab2014-01-10 22:24:11 +0000768 if (::mkfifo(named_pipe_path, 0600) == 0)
Deepak Panickalb36da432014-01-13 14:55:15 +0000769#endif
Greg Claytonfda4fab2014-01-10 22:24:11 +0000770 {
771 debugserver_args.AppendArgument("--named-pipe");
772 debugserver_args.AppendArgument(named_pipe_path);
773 }
Greg Clayton00fe87b2013-12-05 22:58:22 +0000774 }
Greg Clayton91a9b2472013-12-04 19:19:12 +0000775 }
776 else
Greg Claytonfda4fab2014-01-10 22:24:11 +0000777 {
778 listen = true;
779 }
Greg Clayton91a9b2472013-12-04 19:19:12 +0000780 }
781 else
Greg Clayton00fe87b2013-12-05 22:58:22 +0000782 {
Greg Clayton00fe87b2013-12-05 22:58:22 +0000783 // No host and port given, so lets listen on our end and make the debugserver
784 // connect to us..
Greg Clayton16810922014-02-27 19:38:18 +0000785 error = StartListenThread ("127.0.0.1", 0);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000786 if (error.Fail())
787 return error;
Greg Clayton8b82f082011-04-12 05:54:46 +0000788
Greg Clayton00fe87b2013-12-05 22:58:22 +0000789 ConnectionFileDescriptor *connection = (ConnectionFileDescriptor *)GetConnection ();
Greg Clayton16810922014-02-27 19:38:18 +0000790 // Wait for 10 seconds to resolve the bound port
Zachary Turner98688922014-08-06 18:16:26 +0000791 out_port = connection->GetListeningPort(10);
Greg Clayton16810922014-02-27 19:38:18 +0000792 if (out_port > 0)
793 {
794 char port_cstr[32];
795 snprintf(port_cstr, sizeof(port_cstr), "127.0.0.1:%i", out_port);
796 // Send the host and port down that debugserver and specify an option
797 // so that it connects back to the port we are listening to in this process
798 debugserver_args.AppendArgument("--reverse-connect");
799 debugserver_args.AppendArgument(port_cstr);
800 }
801 else
802 {
803 error.SetErrorString ("failed to bind to port 0 on 127.0.0.1");
804 return error;
805 }
Greg Clayton00fe87b2013-12-05 22:58:22 +0000806 }
Greg Clayton00fe87b2013-12-05 22:58:22 +0000807
Greg Clayton8b82f082011-04-12 05:54:46 +0000808 const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE");
809 if (env_debugserver_log_file)
810 {
811 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file);
812 debugserver_args.AppendArgument(arg_cstr);
813 }
814
815 const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS");
816 if (env_debugserver_log_flags)
817 {
818 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags);
819 debugserver_args.AppendArgument(arg_cstr);
820 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000821
822 // Close STDIN, STDOUT and STDERR. We might need to redirect them
823 // to "/dev/null" if we run into any problems.
Greg Clayton91a9b2472013-12-04 19:19:12 +0000824 launch_info.AppendCloseFileAction (STDIN_FILENO);
825 launch_info.AppendCloseFileAction (STDOUT_FILENO);
826 launch_info.AppendCloseFileAction (STDERR_FILENO);
Greg Clayton8b82f082011-04-12 05:54:46 +0000827
828 error = Host::LaunchProcess(launch_info);
Greg Clayton91a9b2472013-12-04 19:19:12 +0000829
Greg Clayton3121fde2014-02-28 20:47:08 +0000830 if (error.Success() && launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
Greg Clayton91a9b2472013-12-04 19:19:12 +0000831 {
Greg Clayton3121fde2014-02-28 20:47:08 +0000832 if (named_pipe_path[0])
Greg Clayton91a9b2472013-12-04 19:19:12 +0000833 {
Greg Clayton3121fde2014-02-28 20:47:08 +0000834 File name_pipe_file;
835 error = name_pipe_file.Open(named_pipe_path, File::eOpenOptionRead);
836 if (error.Success())
837 {
838 char port_cstr[256];
839 port_cstr[0] = '\0';
840 size_t num_bytes = sizeof(port_cstr);
841 error = name_pipe_file.Read(port_cstr, num_bytes);
842 assert (error.Success());
843 assert (num_bytes > 0 && port_cstr[num_bytes-1] == '\0');
844 out_port = Args::StringToUInt32(port_cstr, 0);
845 name_pipe_file.Close();
846 }
Zachary Turnerc00cf4a2014-08-15 22:04:21 +0000847 FileSystem::Unlink(named_pipe_path);
Greg Clayton91a9b2472013-12-04 19:19:12 +0000848 }
Greg Clayton3121fde2014-02-28 20:47:08 +0000849 else if (listen)
850 {
851
852 }
853 else
854 {
855 // Make sure we actually connect with the debugserver...
856 JoinListenThread();
857 }
Greg Clayton00fe87b2013-12-05 22:58:22 +0000858 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000859 }
860 else
861 {
Greg Clayton86edbf42011-10-26 00:56:27 +0000862 error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME );
Greg Clayton8b82f082011-04-12 05:54:46 +0000863 }
864 return error;
865}
866
Greg Claytonc1422c12012-04-09 22:46:21 +0000867void
Greg Claytond451c1a2012-04-13 21:24:18 +0000868GDBRemoteCommunication::DumpHistory(Stream &strm)
Greg Claytonc1422c12012-04-09 22:46:21 +0000869{
Greg Claytond451c1a2012-04-13 21:24:18 +0000870 m_history.Dump (strm);
Greg Claytonc1422c12012-04-09 22:46:21 +0000871}