blob: c5fc4affe32c4cf59824f04ad4b80415d4189338 [file] [log] [blame]
Chris Lattner24943d22010-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 Chenf3878232011-05-13 20:07:25 +000014#include <limits.h>
Stephen Wilson50daf772011-03-25 18:16:28 +000015#include <string.h>
16
Chris Lattner24943d22010-06-08 16:52:24 +000017// C++ Includes
18// Other libraries and framework includes
Chris Lattner24943d22010-06-08 16:52:24 +000019#include "lldb/Core/Log.h"
Greg Clayton451fa822012-04-09 22:46:21 +000020#include "lldb/Core/StreamFile.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021#include "lldb/Core/StreamString.h"
Greg Claytonb72d0f02011-04-12 05:54:46 +000022#include "lldb/Host/FileSpec.h"
23#include "lldb/Host/Host.h"
Chris Lattner24943d22010-06-08 16:52:24 +000024#include "lldb/Host/TimeValue.h"
Greg Claytonb72d0f02011-04-12 05:54:46 +000025#include "lldb/Target/Process.h"
Chris Lattner24943d22010-06-08 16:52:24 +000026
27// Project includes
Chris Lattner24943d22010-06-08 16:52:24 +000028#include "ProcessGDBRemoteLog.h"
29
Greg Claytonb72d0f02011-04-12 05:54:46 +000030#define DEBUGSERVER_BASENAME "debugserver"
31
Chris Lattner24943d22010-06-08 16:52:24 +000032using namespace lldb;
33using namespace lldb_private;
34
Greg Clayton451fa822012-04-09 22:46:21 +000035GDBRemoteCommunication::History::History (uint32_t size) :
36 m_packets(),
37 m_curr_idx (0),
38 m_total_packet_count (0),
39 m_dumped_to_log (false)
40{
41 m_packets.resize(size);
42}
43
44GDBRemoteCommunication::History::~History ()
45{
46}
47
48void
Greg Clayton33559462012-04-13 21:24:18 +000049GDBRemoteCommunication::History::AddPacket (char packet_char,
50 PacketType type,
51 uint32_t bytes_transmitted)
52{
53 const size_t size = m_packets.size();
54 if (size > 0)
55 {
56 const uint32_t idx = GetNextIndex();
57 m_packets[idx].packet.assign (1, packet_char);
58 m_packets[idx].type = type;
59 m_packets[idx].bytes_transmitted = bytes_transmitted;
60 m_packets[idx].packet_idx = m_total_packet_count;
61 m_packets[idx].tid = Host::GetCurrentThreadID();
62 }
63}
64
65void
66GDBRemoteCommunication::History::AddPacket (const std::string &src,
67 uint32_t src_len,
68 PacketType type,
69 uint32_t bytes_transmitted)
70{
71 const size_t size = m_packets.size();
72 if (size > 0)
73 {
74 const uint32_t idx = GetNextIndex();
75 m_packets[idx].packet.assign (src, 0, src_len);
76 m_packets[idx].type = type;
77 m_packets[idx].bytes_transmitted = bytes_transmitted;
78 m_packets[idx].packet_idx = m_total_packet_count;
79 m_packets[idx].tid = Host::GetCurrentThreadID();
80 }
81}
82
83void
Greg Clayton451fa822012-04-09 22:46:21 +000084GDBRemoteCommunication::History::Dump (lldb_private::Stream &strm) const
85{
86 const uint32_t size = GetNumPacketsInHistory ();
87 const uint32_t first_idx = GetFirstSavedPacketIndex ();
88 const uint32_t stop_idx = m_curr_idx + size;
89 for (uint32_t i = first_idx; i < stop_idx; ++i)
90 {
91 const uint32_t idx = NormalizeIndex (i);
92 const Entry &entry = m_packets[idx];
93 if (entry.type == ePacketTypeInvalid || entry.packet.empty())
94 break;
Greg Clayton33559462012-04-13 21:24:18 +000095 strm.Printf ("history[%u] tid=0x%4.4llx <%4u> %s packet: %s\n",
Greg Clayton451fa822012-04-09 22:46:21 +000096 entry.packet_idx,
Greg Clayton33559462012-04-13 21:24:18 +000097 entry.tid,
Greg Clayton451fa822012-04-09 22:46:21 +000098 entry.bytes_transmitted,
99 (entry.type == ePacketTypeSend) ? "send" : "read",
100 entry.packet.c_str());
101 }
102}
103
104void
105GDBRemoteCommunication::History::Dump (lldb_private::Log *log) const
106{
107 if (log && !m_dumped_to_log)
108 {
109 m_dumped_to_log = true;
110 const uint32_t size = GetNumPacketsInHistory ();
111 const uint32_t first_idx = GetFirstSavedPacketIndex ();
112 const uint32_t stop_idx = m_curr_idx + size;
113 for (uint32_t i = first_idx; i < stop_idx; ++i)
114 {
115 const uint32_t idx = NormalizeIndex (i);
116 const Entry &entry = m_packets[idx];
117 if (entry.type == ePacketTypeInvalid || entry.packet.empty())
118 break;
Greg Clayton33559462012-04-13 21:24:18 +0000119 log->Printf ("history[%u] tid=0x%4.4llx <%4u> %s packet: %s",
Greg Clayton451fa822012-04-09 22:46:21 +0000120 entry.packet_idx,
Greg Clayton33559462012-04-13 21:24:18 +0000121 entry.tid,
Greg Clayton451fa822012-04-09 22:46:21 +0000122 entry.bytes_transmitted,
123 (entry.type == ePacketTypeSend) ? "send" : "read",
124 entry.packet.c_str());
125 }
126 }
127}
128
Chris Lattner24943d22010-06-08 16:52:24 +0000129//----------------------------------------------------------------------
130// GDBRemoteCommunication constructor
131//----------------------------------------------------------------------
Greg Claytonb72d0f02011-04-12 05:54:46 +0000132GDBRemoteCommunication::GDBRemoteCommunication(const char *comm_name,
133 const char *listener_name,
134 bool is_platform) :
Greg Clayton61d043b2011-03-22 04:00:09 +0000135 Communication(comm_name),
Greg Clayton604f0d32011-06-17 03:31:01 +0000136 m_packet_timeout (1),
Chris Lattner24943d22010-06-08 16:52:24 +0000137 m_sequence_mutex (Mutex::eMutexTypeRecursive),
Greg Claytoncecf3482011-01-20 07:53:45 +0000138 m_public_is_running (false),
Greg Clayton58e26e02011-03-24 04:28:38 +0000139 m_private_is_running (false),
Greg Clayton451fa822012-04-09 22:46:21 +0000140 m_history (512),
Greg Claytonb72d0f02011-04-12 05:54:46 +0000141 m_send_acks (true),
142 m_is_platform (is_platform)
Chris Lattner24943d22010-06-08 16:52:24 +0000143{
Chris Lattner24943d22010-06-08 16:52:24 +0000144}
145
146//----------------------------------------------------------------------
147// Destructor
148//----------------------------------------------------------------------
149GDBRemoteCommunication::~GDBRemoteCommunication()
150{
Chris Lattner24943d22010-06-08 16:52:24 +0000151 if (IsConnected())
152 {
Chris Lattner24943d22010-06-08 16:52:24 +0000153 Disconnect();
154 }
155}
156
Chris Lattner24943d22010-06-08 16:52:24 +0000157char
158GDBRemoteCommunication::CalculcateChecksum (const char *payload, size_t payload_length)
159{
160 int checksum = 0;
161
162 // We only need to compute the checksum if we are sending acks
Greg Claytonc1f45872011-02-12 06:28:37 +0000163 if (GetSendAcks ())
Chris Lattner24943d22010-06-08 16:52:24 +0000164 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000165 for (size_t i = 0; i < payload_length; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000166 checksum += payload[i];
167 }
168 return checksum & 255;
169}
170
171size_t
Greg Claytona4881d02011-01-22 07:12:45 +0000172GDBRemoteCommunication::SendAck ()
Chris Lattner24943d22010-06-08 16:52:24 +0000173{
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000174 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Chris Lattner24943d22010-06-08 16:52:24 +0000175 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton451fa822012-04-09 22:46:21 +0000176 char ch = '+';
177 const size_t bytes_written = Write (&ch, 1, status, NULL);
178 if (log)
179 log->Printf ("<%4zu> send packet: %c", bytes_written, ch);
180 m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written);
181 return bytes_written;
Chris Lattner24943d22010-06-08 16:52:24 +0000182}
183
184size_t
Greg Claytona4881d02011-01-22 07:12:45 +0000185GDBRemoteCommunication::SendNack ()
186{
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000187 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Greg Claytona4881d02011-01-22 07:12:45 +0000188 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton451fa822012-04-09 22:46:21 +0000189 char ch = '-';
190 const size_t bytes_written = Write (&ch, 1, status, NULL);
191 if (log)
192 log->Printf ("<%4zu> send packet: %c", bytes_written, ch);
193 m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written);
194 return bytes_written;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000195}
196
197size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000198GDBRemoteCommunication::SendPacket (const char *payload, size_t payload_length)
199{
200 Mutex::Locker locker(m_sequence_mutex);
201 return SendPacketNoLock (payload, payload_length);
202}
203
204size_t
205GDBRemoteCommunication::SendPacketNoLock (const char *payload, size_t payload_length)
206{
207 if (IsConnected())
208 {
209 StreamString packet(0, 4, eByteOrderBig);
210
211 packet.PutChar('$');
212 packet.Write (payload, payload_length);
213 packet.PutChar('#');
214 packet.PutHex8(CalculcateChecksum (payload, payload_length));
215
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000216 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Chris Lattner24943d22010-06-08 16:52:24 +0000217 ConnectionStatus status = eConnectionStatusSuccess;
218 size_t bytes_written = Write (packet.GetData(), packet.GetSize(), status, NULL);
Greg Clayton451fa822012-04-09 22:46:21 +0000219 if (log)
220 {
221 // If logging was just enabled and we have history, then dump out what
222 // we have to the log so we get the historical context. The Dump() call that
223 // logs all of the packet will set a boolean so that we don't dump this more
224 // than once
225 if (!m_history.DidDumpToLog ())
Greg Clayton451fa822012-04-09 22:46:21 +0000226 m_history.Dump (log.get());
Greg Clayton451fa822012-04-09 22:46:21 +0000227
228 log->Printf ("<%4zu> send packet: %.*s", bytes_written, (int)packet.GetSize(), packet.GetData());
229 }
230
231 m_history.AddPacket (packet.GetString(), packet.GetSize(), History::ePacketTypeSend, bytes_written);
232
233
Chris Lattner24943d22010-06-08 16:52:24 +0000234 if (bytes_written == packet.GetSize())
235 {
Greg Claytonc1f45872011-02-12 06:28:37 +0000236 if (GetSendAcks ())
Greg Clayton54e7afa2010-07-09 20:39:50 +0000237 {
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000238 if (GetAck () != '+')
Greg Clayton58e26e02011-03-24 04:28:38 +0000239 {
240 printf("get ack failed...");
Greg Clayton54e7afa2010-07-09 20:39:50 +0000241 return 0;
Greg Clayton58e26e02011-03-24 04:28:38 +0000242 }
Greg Clayton54e7afa2010-07-09 20:39:50 +0000243 }
Chris Lattner24943d22010-06-08 16:52:24 +0000244 }
Johnny Chen515ea542010-09-14 22:10:43 +0000245 else
246 {
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000247 if (log)
Greg Clayton139da722011-05-20 03:15:54 +0000248 log->Printf ("error: failed to send packet: %.*s", (int)packet.GetSize(), packet.GetData());
Johnny Chen515ea542010-09-14 22:10:43 +0000249 }
Chris Lattner24943d22010-06-08 16:52:24 +0000250 return bytes_written;
Greg Claytoneea26402010-09-14 23:36:40 +0000251 }
Chris Lattner24943d22010-06-08 16:52:24 +0000252 return 0;
253}
254
255char
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000256GDBRemoteCommunication::GetAck ()
Chris Lattner24943d22010-06-08 16:52:24 +0000257{
Greg Clayton61d043b2011-03-22 04:00:09 +0000258 StringExtractorGDBRemote packet;
Greg Clayton516f0842012-04-11 00:24:49 +0000259 if (WaitForPacketWithTimeoutMicroSecondsNoLock (packet, GetPacketTimeoutInMicroSeconds ()) == 1)
Greg Clayton61d043b2011-03-22 04:00:09 +0000260 return packet.GetChar();
Chris Lattner24943d22010-06-08 16:52:24 +0000261 return 0;
262}
263
264bool
Greg Claytonc8dd5702012-04-12 19:04:34 +0000265GDBRemoteCommunication::GetSequenceMutex (Mutex::Locker& locker)
Chris Lattner24943d22010-06-08 16:52:24 +0000266{
Greg Clayton775f8532012-05-31 16:54:51 +0000267 if (IsRunning())
268 return locker.TryLock (m_sequence_mutex);
269
270 locker.Lock (m_sequence_mutex);
271 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000272}
273
Chris Lattner24943d22010-06-08 16:52:24 +0000274
Greg Clayton72e1c782011-01-22 23:43:18 +0000275bool
Greg Clayton72e1c782011-01-22 23:43:18 +0000276GDBRemoteCommunication::WaitForNotRunningPrivate (const TimeValue *timeout_ptr)
277{
278 return m_private_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL);
279}
280
Chris Lattner24943d22010-06-08 16:52:24 +0000281size_t
Greg Clayton63afdb02011-06-17 01:22:15 +0000282GDBRemoteCommunication::WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &packet, uint32_t timeout_usec)
Chris Lattner24943d22010-06-08 16:52:24 +0000283{
Greg Clayton63afdb02011-06-17 01:22:15 +0000284 uint8_t buffer[8192];
285 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +0000286
Greg Clayton801417e2011-07-07 01:59:51 +0000287 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS | GDBR_LOG_VERBOSE));
288
Greg Clayton63afdb02011-06-17 01:22:15 +0000289 // Check for a packet from our cache first without trying any reading...
290 if (CheckForPacket (NULL, 0, packet))
291 return packet.GetStringRef().size();
Chris Lattner24943d22010-06-08 16:52:24 +0000292
Greg Claytond0691fe2011-07-02 23:21:06 +0000293 bool timed_out = false;
294 while (IsConnected() && !timed_out)
Chris Lattner24943d22010-06-08 16:52:24 +0000295 {
Johnny Chen72fa64b2011-07-19 01:13:00 +0000296 lldb::ConnectionStatus status = eConnectionStatusNoConnection;
Greg Clayton63afdb02011-06-17 01:22:15 +0000297 size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error);
Greg Clayton801417e2011-07-07 01:59:51 +0000298
299 if (log)
300 log->Printf ("%s: Read (buffer, (sizeof(buffer), timeout_usec = 0x%x, status = %s, error = %s) => bytes_read = %zu",
301 __PRETTY_FUNCTION__,
302 timeout_usec,
303 Communication::ConnectionStatusAsCString (status),
304 error.AsCString(),
305 bytes_read);
306
Greg Clayton63afdb02011-06-17 01:22:15 +0000307 if (bytes_read > 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000308 {
Greg Clayton63afdb02011-06-17 01:22:15 +0000309 if (CheckForPacket (buffer, bytes_read, packet))
310 return packet.GetStringRef().size();
311 }
312 else
313 {
314 switch (status)
Chris Lattner24943d22010-06-08 16:52:24 +0000315 {
Greg Claytona9eb8272011-07-02 21:07:54 +0000316 case eConnectionStatusTimedOut:
Greg Claytond0691fe2011-07-02 23:21:06 +0000317 timed_out = true;
318 break;
319 case eConnectionStatusSuccess:
320 //printf ("status = success but error = %s\n", error.AsCString("<invalid>"));
Greg Clayton63afdb02011-06-17 01:22:15 +0000321 break;
322
323 case eConnectionStatusEndOfFile:
324 case eConnectionStatusNoConnection:
325 case eConnectionStatusLostConnection:
326 case eConnectionStatusError:
327 Disconnect();
328 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000329 }
330 }
Chris Lattner24943d22010-06-08 16:52:24 +0000331 }
Greg Clayton63afdb02011-06-17 01:22:15 +0000332 packet.Clear ();
Chris Lattner24943d22010-06-08 16:52:24 +0000333 return 0;
334}
335
Greg Clayton63afdb02011-06-17 01:22:15 +0000336bool
337GDBRemoteCommunication::CheckForPacket (const uint8_t *src, size_t src_len, StringExtractorGDBRemote &packet)
Chris Lattner24943d22010-06-08 16:52:24 +0000338{
339 // Put the packet data into the buffer in a thread safe fashion
340 Mutex::Locker locker(m_bytes_mutex);
Greg Claytona9eb8272011-07-02 21:07:54 +0000341
342 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
343
Greg Clayton63afdb02011-06-17 01:22:15 +0000344 if (src && src_len > 0)
Greg Claytona9eb8272011-07-02 21:07:54 +0000345 {
Greg Claytond0691fe2011-07-02 23:21:06 +0000346 if (log && log->GetVerbose())
Greg Claytona9eb8272011-07-02 21:07:54 +0000347 {
348 StreamString s;
Greg Claytond0691fe2011-07-02 23:21:06 +0000349 log->Printf ("GDBRemoteCommunication::%s adding %u bytes: %.*s",
350 __FUNCTION__,
351 (uint32_t)src_len,
352 (uint32_t)src_len,
353 src);
Greg Claytona9eb8272011-07-02 21:07:54 +0000354 }
Greg Clayton63afdb02011-06-17 01:22:15 +0000355 m_bytes.append ((const char *)src, src_len);
Greg Claytona9eb8272011-07-02 21:07:54 +0000356 }
Chris Lattner24943d22010-06-08 16:52:24 +0000357
358 // Parse up the packets into gdb remote packets
Greg Claytona9eb8272011-07-02 21:07:54 +0000359 if (!m_bytes.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000360 {
361 // end_idx must be one past the last valid packet byte. Start
362 // it off with an invalid value that is the same as the current
363 // index.
Greg Clayton63afdb02011-06-17 01:22:15 +0000364 size_t content_start = 0;
365 size_t content_length = 0;
366 size_t total_length = 0;
367 size_t checksum_idx = std::string::npos;
Chris Lattner24943d22010-06-08 16:52:24 +0000368
369 switch (m_bytes[0])
370 {
371 case '+': // Look for ack
372 case '-': // Look for cancel
373 case '\x03': // ^C to halt target
Greg Clayton63afdb02011-06-17 01:22:15 +0000374 content_length = total_length = 1; // The command is one byte long...
Chris Lattner24943d22010-06-08 16:52:24 +0000375 break;
376
377 case '$':
378 // Look for a standard gdb packet?
Chris Lattner24943d22010-06-08 16:52:24 +0000379 {
Greg Clayton63afdb02011-06-17 01:22:15 +0000380 size_t hash_pos = m_bytes.find('#');
381 if (hash_pos != std::string::npos)
Chris Lattner24943d22010-06-08 16:52:24 +0000382 {
Greg Clayton63afdb02011-06-17 01:22:15 +0000383 if (hash_pos + 2 < m_bytes.size())
384 {
385 checksum_idx = hash_pos + 1;
386 // Skip the dollar sign
387 content_start = 1;
388 // Don't include the # in the content or the $ in the content length
389 content_length = hash_pos - 1;
390
391 total_length = hash_pos + 3; // Skip the # and the two hex checksum bytes
392 }
393 else
394 {
395 // Checksum bytes aren't all here yet
396 content_length = std::string::npos;
397 }
Chris Lattner24943d22010-06-08 16:52:24 +0000398 }
399 }
400 break;
401
402 default:
Greg Clayton604f0d32011-06-17 03:31:01 +0000403 {
Greg Claytona9eb8272011-07-02 21:07:54 +0000404 // We have an unexpected byte and we need to flush all bad
405 // data that is in m_bytes, so we need to find the first
406 // byte that is a '+' (ACK), '-' (NACK), \x03 (CTRL+C interrupt),
407 // or '$' character (start of packet header) or of course,
408 // the end of the data in m_bytes...
409 const size_t bytes_len = m_bytes.size();
410 bool done = false;
411 uint32_t idx;
412 for (idx = 1; !done && idx < bytes_len; ++idx)
413 {
414 switch (m_bytes[idx])
415 {
416 case '+':
417 case '-':
418 case '\x03':
419 case '$':
420 done = true;
421 break;
422
423 default:
424 break;
425 }
426 }
Greg Clayton604f0d32011-06-17 03:31:01 +0000427 if (log)
Greg Claytona9eb8272011-07-02 21:07:54 +0000428 log->Printf ("GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'",
429 __FUNCTION__, idx, idx, m_bytes.c_str());
430 m_bytes.erase(0, idx);
Greg Clayton604f0d32011-06-17 03:31:01 +0000431 }
Chris Lattner24943d22010-06-08 16:52:24 +0000432 break;
433 }
434
Greg Clayton63afdb02011-06-17 01:22:15 +0000435 if (content_length == std::string::npos)
Chris Lattner24943d22010-06-08 16:52:24 +0000436 {
Greg Clayton63afdb02011-06-17 01:22:15 +0000437 packet.Clear();
438 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000439 }
Greg Clayton604f0d32011-06-17 03:31:01 +0000440 else if (total_length > 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000441 {
Greg Clayton63afdb02011-06-17 01:22:15 +0000442
Chris Lattner24943d22010-06-08 16:52:24 +0000443 // We have a valid packet...
Greg Clayton63afdb02011-06-17 01:22:15 +0000444 assert (content_length <= m_bytes.size());
445 assert (total_length <= m_bytes.size());
446 assert (content_length <= total_length);
447
448 bool success = true;
449 std::string &packet_str = packet.GetStringRef();
Greg Clayton451fa822012-04-09 22:46:21 +0000450
451
452 if (log)
453 {
454 // If logging was just enabled and we have history, then dump out what
455 // we have to the log so we get the historical context. The Dump() call that
456 // logs all of the packet will set a boolean so that we don't dump this more
457 // than once
458 if (!m_history.DidDumpToLog ())
459 m_history.Dump (log.get());
460
461 log->Printf ("<%4zu> read packet: %.*s", total_length, (int)(total_length), m_bytes.c_str());
462 }
463
464 m_history.AddPacket (m_bytes.c_str(), total_length, History::ePacketTypeRecv, total_length);
465
Greg Clayton63afdb02011-06-17 01:22:15 +0000466 packet_str.assign (m_bytes, content_start, content_length);
Greg Clayton451fa822012-04-09 22:46:21 +0000467
Greg Clayton63afdb02011-06-17 01:22:15 +0000468 if (m_bytes[0] == '$')
469 {
470 assert (checksum_idx < m_bytes.size());
471 if (::isxdigit (m_bytes[checksum_idx+0]) ||
472 ::isxdigit (m_bytes[checksum_idx+1]))
473 {
474 if (GetSendAcks ())
475 {
476 const char *packet_checksum_cstr = &m_bytes[checksum_idx];
477 char packet_checksum = strtol (packet_checksum_cstr, NULL, 16);
478 char actual_checksum = CalculcateChecksum (packet_str.c_str(), packet_str.size());
479 success = packet_checksum == actual_checksum;
480 if (!success)
481 {
482 if (log)
483 log->Printf ("error: checksum mismatch: %.*s expected 0x%2.2x, got 0x%2.2x",
484 (int)(total_length),
485 m_bytes.c_str(),
486 (uint8_t)packet_checksum,
487 (uint8_t)actual_checksum);
488 }
489 // Send the ack or nack if needed
490 if (!success)
491 SendNack();
492 else
493 SendAck();
494 }
Greg Clayton63afdb02011-06-17 01:22:15 +0000495 }
496 else
497 {
498 success = false;
499 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000500 log->Printf ("error: invalid checksum in packet: '%s'\n", m_bytes.c_str());
Greg Clayton63afdb02011-06-17 01:22:15 +0000501 }
502 }
Greg Clayton451fa822012-04-09 22:46:21 +0000503
Greg Clayton63afdb02011-06-17 01:22:15 +0000504 m_bytes.erase(0, total_length);
505 packet.SetFilePos(0);
506 return success;
Chris Lattner24943d22010-06-08 16:52:24 +0000507 }
Chris Lattner24943d22010-06-08 16:52:24 +0000508 }
Greg Clayton63afdb02011-06-17 01:22:15 +0000509 packet.Clear();
510 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000511}
512
Greg Claytonb72d0f02011-04-12 05:54:46 +0000513Error
514GDBRemoteCommunication::StartDebugserverProcess (const char *debugserver_url,
515 const char *unix_socket_name, // For handshaking
516 lldb_private::ProcessLaunchInfo &launch_info)
517{
518 Error error;
519 // If we locate debugserver, keep that located version around
520 static FileSpec g_debugserver_file_spec;
521
522 // This function will fill in the launch information for the debugserver
523 // instance that gets launched.
524 launch_info.Clear();
525
526 char debugserver_path[PATH_MAX];
527 FileSpec &debugserver_file_spec = launch_info.GetExecutableFile();
528
529 // Always check to see if we have an environment override for the path
530 // to the debugserver to use and use it if we do.
531 const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH");
532 if (env_debugserver_path)
533 debugserver_file_spec.SetFile (env_debugserver_path, false);
534 else
535 debugserver_file_spec = g_debugserver_file_spec;
536 bool debugserver_exists = debugserver_file_spec.Exists();
537 if (!debugserver_exists)
538 {
539 // The debugserver binary is in the LLDB.framework/Resources
540 // directory.
541 if (Host::GetLLDBPath (ePathTypeSupportExecutableDir, debugserver_file_spec))
542 {
543 debugserver_file_spec.GetFilename().SetCString(DEBUGSERVER_BASENAME);
544 debugserver_exists = debugserver_file_spec.Exists();
545 if (debugserver_exists)
546 {
547 g_debugserver_file_spec = debugserver_file_spec;
548 }
549 else
550 {
551 g_debugserver_file_spec.Clear();
552 debugserver_file_spec.Clear();
553 }
554 }
555 }
556
557 if (debugserver_exists)
558 {
559 debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path));
560
561 Args &debugserver_args = launch_info.GetArguments();
562 debugserver_args.Clear();
563 char arg_cstr[PATH_MAX];
564
565 // Start args with "debugserver /file/path -r --"
566 debugserver_args.AppendArgument(debugserver_path);
567 debugserver_args.AppendArgument(debugserver_url);
568 // use native registers, not the GDB registers
569 debugserver_args.AppendArgument("--native-regs");
570 // make debugserver run in its own session so signals generated by
571 // special terminal key sequences (^C) don't affect debugserver
572 debugserver_args.AppendArgument("--setsid");
573
574 if (unix_socket_name && unix_socket_name[0])
575 {
576 debugserver_args.AppendArgument("--unix-socket");
577 debugserver_args.AppendArgument(unix_socket_name);
578 }
579
580 const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE");
581 if (env_debugserver_log_file)
582 {
583 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file);
584 debugserver_args.AppendArgument(arg_cstr);
585 }
586
587 const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS");
588 if (env_debugserver_log_flags)
589 {
590 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags);
591 debugserver_args.AppendArgument(arg_cstr);
592 }
593 // debugserver_args.AppendArgument("--log-file=/tmp/debugserver.txt");
594 // debugserver_args.AppendArgument("--log-flags=0x802e0e");
595
596 // We currently send down all arguments, attach pids, or attach
597 // process names in dedicated GDB server packets, so we don't need
598 // to pass them as arguments. This is currently because of all the
599 // things we need to setup prior to launching: the environment,
600 // current working dir, file actions, etc.
601#if 0
602 // Now append the program arguments
603 if (inferior_argv)
604 {
605 // Terminate the debugserver args so we can now append the inferior args
606 debugserver_args.AppendArgument("--");
607
608 for (int i = 0; inferior_argv[i] != NULL; ++i)
609 debugserver_args.AppendArgument (inferior_argv[i]);
610 }
611 else if (attach_pid != LLDB_INVALID_PROCESS_ID)
612 {
613 ::snprintf (arg_cstr, sizeof(arg_cstr), "--attach=%u", attach_pid);
614 debugserver_args.AppendArgument (arg_cstr);
615 }
616 else if (attach_name && attach_name[0])
617 {
618 if (wait_for_launch)
619 debugserver_args.AppendArgument ("--waitfor");
620 else
621 debugserver_args.AppendArgument ("--attach");
622 debugserver_args.AppendArgument (attach_name);
623 }
624#endif
625
626 // Close STDIN, STDOUT and STDERR. We might need to redirect them
627 // to "/dev/null" if we run into any problems.
628// launch_info.AppendCloseFileAction (STDIN_FILENO);
629// launch_info.AppendCloseFileAction (STDOUT_FILENO);
630// launch_info.AppendCloseFileAction (STDERR_FILENO);
631
632 error = Host::LaunchProcess(launch_info);
633 }
634 else
635 {
Greg Clayton9c236732011-10-26 00:56:27 +0000636 error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME );
Greg Claytonb72d0f02011-04-12 05:54:46 +0000637 }
638 return error;
639}
640
Greg Clayton451fa822012-04-09 22:46:21 +0000641void
Greg Clayton33559462012-04-13 21:24:18 +0000642GDBRemoteCommunication::DumpHistory(Stream &strm)
Greg Clayton451fa822012-04-09 22:46:21 +0000643{
Greg Clayton33559462012-04-13 21:24:18 +0000644 m_history.Dump (strm);
Greg Clayton451fa822012-04-09 22:46:21 +0000645}