blob: 1535fd41260bb83219a8ef1d537a1dc0e1f624c2 [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
49GDBRemoteCommunication::History::Dump (lldb_private::Stream &strm) const
50{
51 const uint32_t size = GetNumPacketsInHistory ();
52 const uint32_t first_idx = GetFirstSavedPacketIndex ();
53 const uint32_t stop_idx = m_curr_idx + size;
54 for (uint32_t i = first_idx; i < stop_idx; ++i)
55 {
56 const uint32_t idx = NormalizeIndex (i);
57 const Entry &entry = m_packets[idx];
58 if (entry.type == ePacketTypeInvalid || entry.packet.empty())
59 break;
60 strm.Printf ("history[%u] <%4u> %s packet: %s\n",
61 entry.packet_idx,
62 entry.bytes_transmitted,
63 (entry.type == ePacketTypeSend) ? "send" : "read",
64 entry.packet.c_str());
65 }
66}
67
68void
69GDBRemoteCommunication::History::Dump (lldb_private::Log *log) const
70{
71 if (log && !m_dumped_to_log)
72 {
73 m_dumped_to_log = true;
74 const uint32_t size = GetNumPacketsInHistory ();
75 const uint32_t first_idx = GetFirstSavedPacketIndex ();
76 const uint32_t stop_idx = m_curr_idx + size;
77 for (uint32_t i = first_idx; i < stop_idx; ++i)
78 {
79 const uint32_t idx = NormalizeIndex (i);
80 const Entry &entry = m_packets[idx];
81 if (entry.type == ePacketTypeInvalid || entry.packet.empty())
82 break;
83 log->Printf ("history[%u] <%4u> %s packet: %s",
84 entry.packet_idx,
85 entry.bytes_transmitted,
86 (entry.type == ePacketTypeSend) ? "send" : "read",
87 entry.packet.c_str());
88 }
89 }
90}
91
Chris Lattner24943d22010-06-08 16:52:24 +000092//----------------------------------------------------------------------
93// GDBRemoteCommunication constructor
94//----------------------------------------------------------------------
Greg Claytonb72d0f02011-04-12 05:54:46 +000095GDBRemoteCommunication::GDBRemoteCommunication(const char *comm_name,
96 const char *listener_name,
97 bool is_platform) :
Greg Clayton61d043b2011-03-22 04:00:09 +000098 Communication(comm_name),
Greg Clayton604f0d32011-06-17 03:31:01 +000099 m_packet_timeout (1),
Chris Lattner24943d22010-06-08 16:52:24 +0000100 m_sequence_mutex (Mutex::eMutexTypeRecursive),
Greg Claytoncecf3482011-01-20 07:53:45 +0000101 m_public_is_running (false),
Greg Clayton58e26e02011-03-24 04:28:38 +0000102 m_private_is_running (false),
Greg Clayton451fa822012-04-09 22:46:21 +0000103 m_history (512),
Greg Claytonb72d0f02011-04-12 05:54:46 +0000104 m_send_acks (true),
105 m_is_platform (is_platform)
Chris Lattner24943d22010-06-08 16:52:24 +0000106{
Chris Lattner24943d22010-06-08 16:52:24 +0000107}
108
109//----------------------------------------------------------------------
110// Destructor
111//----------------------------------------------------------------------
112GDBRemoteCommunication::~GDBRemoteCommunication()
113{
Chris Lattner24943d22010-06-08 16:52:24 +0000114 if (IsConnected())
115 {
Chris Lattner24943d22010-06-08 16:52:24 +0000116 Disconnect();
117 }
118}
119
Chris Lattner24943d22010-06-08 16:52:24 +0000120char
121GDBRemoteCommunication::CalculcateChecksum (const char *payload, size_t payload_length)
122{
123 int checksum = 0;
124
125 // We only need to compute the checksum if we are sending acks
Greg Claytonc1f45872011-02-12 06:28:37 +0000126 if (GetSendAcks ())
Chris Lattner24943d22010-06-08 16:52:24 +0000127 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000128 for (size_t i = 0; i < payload_length; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000129 checksum += payload[i];
130 }
131 return checksum & 255;
132}
133
134size_t
Greg Claytona4881d02011-01-22 07:12:45 +0000135GDBRemoteCommunication::SendAck ()
Chris Lattner24943d22010-06-08 16:52:24 +0000136{
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000137 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Chris Lattner24943d22010-06-08 16:52:24 +0000138 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton451fa822012-04-09 22:46:21 +0000139 char ch = '+';
140 const size_t bytes_written = Write (&ch, 1, status, NULL);
141 if (log)
142 log->Printf ("<%4zu> send packet: %c", bytes_written, ch);
143 m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written);
144 return bytes_written;
Chris Lattner24943d22010-06-08 16:52:24 +0000145}
146
147size_t
Greg Claytona4881d02011-01-22 07:12:45 +0000148GDBRemoteCommunication::SendNack ()
149{
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000150 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Greg Claytona4881d02011-01-22 07:12:45 +0000151 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton451fa822012-04-09 22:46:21 +0000152 char ch = '-';
153 const size_t bytes_written = Write (&ch, 1, status, NULL);
154 if (log)
155 log->Printf ("<%4zu> send packet: %c", bytes_written, ch);
156 m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written);
157 return bytes_written;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000158}
159
160size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000161GDBRemoteCommunication::SendPacket (const char *payload, size_t payload_length)
162{
163 Mutex::Locker locker(m_sequence_mutex);
164 return SendPacketNoLock (payload, payload_length);
165}
166
167size_t
168GDBRemoteCommunication::SendPacketNoLock (const char *payload, size_t payload_length)
169{
170 if (IsConnected())
171 {
172 StreamString packet(0, 4, eByteOrderBig);
173
174 packet.PutChar('$');
175 packet.Write (payload, payload_length);
176 packet.PutChar('#');
177 packet.PutHex8(CalculcateChecksum (payload, payload_length));
178
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000179 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Chris Lattner24943d22010-06-08 16:52:24 +0000180 ConnectionStatus status = eConnectionStatusSuccess;
181 size_t bytes_written = Write (packet.GetData(), packet.GetSize(), status, NULL);
Greg Clayton451fa822012-04-09 22:46:21 +0000182 if (log)
183 {
184 // If logging was just enabled and we have history, then dump out what
185 // we have to the log so we get the historical context. The Dump() call that
186 // logs all of the packet will set a boolean so that we don't dump this more
187 // than once
188 if (!m_history.DidDumpToLog ())
Greg Clayton451fa822012-04-09 22:46:21 +0000189 m_history.Dump (log.get());
Greg Clayton451fa822012-04-09 22:46:21 +0000190
191 log->Printf ("<%4zu> send packet: %.*s", bytes_written, (int)packet.GetSize(), packet.GetData());
192 }
193
194 m_history.AddPacket (packet.GetString(), packet.GetSize(), History::ePacketTypeSend, bytes_written);
195
196
Chris Lattner24943d22010-06-08 16:52:24 +0000197 if (bytes_written == packet.GetSize())
198 {
Greg Claytonc1f45872011-02-12 06:28:37 +0000199 if (GetSendAcks ())
Greg Clayton54e7afa2010-07-09 20:39:50 +0000200 {
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000201 if (GetAck () != '+')
Greg Clayton58e26e02011-03-24 04:28:38 +0000202 {
203 printf("get ack failed...");
Greg Clayton54e7afa2010-07-09 20:39:50 +0000204 return 0;
Greg Clayton58e26e02011-03-24 04:28:38 +0000205 }
Greg Clayton54e7afa2010-07-09 20:39:50 +0000206 }
Chris Lattner24943d22010-06-08 16:52:24 +0000207 }
Johnny Chen515ea542010-09-14 22:10:43 +0000208 else
209 {
Greg Clayton0bfda0b2011-02-05 02:25:06 +0000210 if (log)
Greg Clayton139da722011-05-20 03:15:54 +0000211 log->Printf ("error: failed to send packet: %.*s", (int)packet.GetSize(), packet.GetData());
Johnny Chen515ea542010-09-14 22:10:43 +0000212 }
Chris Lattner24943d22010-06-08 16:52:24 +0000213 return bytes_written;
Greg Claytoneea26402010-09-14 23:36:40 +0000214 }
Chris Lattner24943d22010-06-08 16:52:24 +0000215 return 0;
216}
217
218char
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000219GDBRemoteCommunication::GetAck ()
Chris Lattner24943d22010-06-08 16:52:24 +0000220{
Greg Clayton61d043b2011-03-22 04:00:09 +0000221 StringExtractorGDBRemote packet;
Greg Clayton516f0842012-04-11 00:24:49 +0000222 if (WaitForPacketWithTimeoutMicroSecondsNoLock (packet, GetPacketTimeoutInMicroSeconds ()) == 1)
Greg Clayton61d043b2011-03-22 04:00:09 +0000223 return packet.GetChar();
Chris Lattner24943d22010-06-08 16:52:24 +0000224 return 0;
225}
226
227bool
Greg Clayton516f0842012-04-11 00:24:49 +0000228GDBRemoteCommunication::GetSequenceMutex (Mutex::Locker& locker, uint32_t usec_timeout)
Chris Lattner24943d22010-06-08 16:52:24 +0000229{
Greg Clayton516f0842012-04-11 00:24:49 +0000230 if (usec_timeout == 0)
231 return locker.TryLock (m_sequence_mutex.GetMutex());
232
233 // Wait for the lock
234 locker.Lock (m_sequence_mutex.GetMutex());
235 return true;
Chris Lattner24943d22010-06-08 16:52:24 +0000236}
237
Chris Lattner24943d22010-06-08 16:52:24 +0000238
Greg Clayton72e1c782011-01-22 23:43:18 +0000239bool
Greg Clayton72e1c782011-01-22 23:43:18 +0000240GDBRemoteCommunication::WaitForNotRunningPrivate (const TimeValue *timeout_ptr)
241{
242 return m_private_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL);
243}
244
Chris Lattner24943d22010-06-08 16:52:24 +0000245size_t
Greg Clayton63afdb02011-06-17 01:22:15 +0000246GDBRemoteCommunication::WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &packet, uint32_t timeout_usec)
Chris Lattner24943d22010-06-08 16:52:24 +0000247{
Greg Clayton63afdb02011-06-17 01:22:15 +0000248 uint8_t buffer[8192];
249 Error error;
Chris Lattner24943d22010-06-08 16:52:24 +0000250
Greg Clayton801417e2011-07-07 01:59:51 +0000251 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS | GDBR_LOG_VERBOSE));
252
Greg Clayton63afdb02011-06-17 01:22:15 +0000253 // Check for a packet from our cache first without trying any reading...
254 if (CheckForPacket (NULL, 0, packet))
255 return packet.GetStringRef().size();
Chris Lattner24943d22010-06-08 16:52:24 +0000256
Greg Claytond0691fe2011-07-02 23:21:06 +0000257 bool timed_out = false;
258 while (IsConnected() && !timed_out)
Chris Lattner24943d22010-06-08 16:52:24 +0000259 {
Johnny Chen72fa64b2011-07-19 01:13:00 +0000260 lldb::ConnectionStatus status = eConnectionStatusNoConnection;
Greg Clayton63afdb02011-06-17 01:22:15 +0000261 size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error);
Greg Clayton801417e2011-07-07 01:59:51 +0000262
263 if (log)
264 log->Printf ("%s: Read (buffer, (sizeof(buffer), timeout_usec = 0x%x, status = %s, error = %s) => bytes_read = %zu",
265 __PRETTY_FUNCTION__,
266 timeout_usec,
267 Communication::ConnectionStatusAsCString (status),
268 error.AsCString(),
269 bytes_read);
270
Greg Clayton63afdb02011-06-17 01:22:15 +0000271 if (bytes_read > 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000272 {
Greg Clayton63afdb02011-06-17 01:22:15 +0000273 if (CheckForPacket (buffer, bytes_read, packet))
274 return packet.GetStringRef().size();
275 }
276 else
277 {
278 switch (status)
Chris Lattner24943d22010-06-08 16:52:24 +0000279 {
Greg Claytona9eb8272011-07-02 21:07:54 +0000280 case eConnectionStatusTimedOut:
Greg Claytond0691fe2011-07-02 23:21:06 +0000281 timed_out = true;
282 break;
283 case eConnectionStatusSuccess:
284 //printf ("status = success but error = %s\n", error.AsCString("<invalid>"));
Greg Clayton63afdb02011-06-17 01:22:15 +0000285 break;
286
287 case eConnectionStatusEndOfFile:
288 case eConnectionStatusNoConnection:
289 case eConnectionStatusLostConnection:
290 case eConnectionStatusError:
291 Disconnect();
292 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000293 }
294 }
Chris Lattner24943d22010-06-08 16:52:24 +0000295 }
Greg Clayton63afdb02011-06-17 01:22:15 +0000296 packet.Clear ();
Chris Lattner24943d22010-06-08 16:52:24 +0000297 return 0;
298}
299
Greg Clayton63afdb02011-06-17 01:22:15 +0000300bool
301GDBRemoteCommunication::CheckForPacket (const uint8_t *src, size_t src_len, StringExtractorGDBRemote &packet)
Chris Lattner24943d22010-06-08 16:52:24 +0000302{
303 // Put the packet data into the buffer in a thread safe fashion
304 Mutex::Locker locker(m_bytes_mutex);
Greg Claytona9eb8272011-07-02 21:07:54 +0000305
306 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
307
Greg Clayton63afdb02011-06-17 01:22:15 +0000308 if (src && src_len > 0)
Greg Claytona9eb8272011-07-02 21:07:54 +0000309 {
Greg Claytond0691fe2011-07-02 23:21:06 +0000310 if (log && log->GetVerbose())
Greg Claytona9eb8272011-07-02 21:07:54 +0000311 {
312 StreamString s;
Greg Claytond0691fe2011-07-02 23:21:06 +0000313 log->Printf ("GDBRemoteCommunication::%s adding %u bytes: %.*s",
314 __FUNCTION__,
315 (uint32_t)src_len,
316 (uint32_t)src_len,
317 src);
Greg Claytona9eb8272011-07-02 21:07:54 +0000318 }
Greg Clayton63afdb02011-06-17 01:22:15 +0000319 m_bytes.append ((const char *)src, src_len);
Greg Claytona9eb8272011-07-02 21:07:54 +0000320 }
Chris Lattner24943d22010-06-08 16:52:24 +0000321
322 // Parse up the packets into gdb remote packets
Greg Claytona9eb8272011-07-02 21:07:54 +0000323 if (!m_bytes.empty())
Chris Lattner24943d22010-06-08 16:52:24 +0000324 {
325 // end_idx must be one past the last valid packet byte. Start
326 // it off with an invalid value that is the same as the current
327 // index.
Greg Clayton63afdb02011-06-17 01:22:15 +0000328 size_t content_start = 0;
329 size_t content_length = 0;
330 size_t total_length = 0;
331 size_t checksum_idx = std::string::npos;
Chris Lattner24943d22010-06-08 16:52:24 +0000332
333 switch (m_bytes[0])
334 {
335 case '+': // Look for ack
336 case '-': // Look for cancel
337 case '\x03': // ^C to halt target
Greg Clayton63afdb02011-06-17 01:22:15 +0000338 content_length = total_length = 1; // The command is one byte long...
Chris Lattner24943d22010-06-08 16:52:24 +0000339 break;
340
341 case '$':
342 // Look for a standard gdb packet?
Chris Lattner24943d22010-06-08 16:52:24 +0000343 {
Greg Clayton63afdb02011-06-17 01:22:15 +0000344 size_t hash_pos = m_bytes.find('#');
345 if (hash_pos != std::string::npos)
Chris Lattner24943d22010-06-08 16:52:24 +0000346 {
Greg Clayton63afdb02011-06-17 01:22:15 +0000347 if (hash_pos + 2 < m_bytes.size())
348 {
349 checksum_idx = hash_pos + 1;
350 // Skip the dollar sign
351 content_start = 1;
352 // Don't include the # in the content or the $ in the content length
353 content_length = hash_pos - 1;
354
355 total_length = hash_pos + 3; // Skip the # and the two hex checksum bytes
356 }
357 else
358 {
359 // Checksum bytes aren't all here yet
360 content_length = std::string::npos;
361 }
Chris Lattner24943d22010-06-08 16:52:24 +0000362 }
363 }
364 break;
365
366 default:
Greg Clayton604f0d32011-06-17 03:31:01 +0000367 {
Greg Claytona9eb8272011-07-02 21:07:54 +0000368 // We have an unexpected byte and we need to flush all bad
369 // data that is in m_bytes, so we need to find the first
370 // byte that is a '+' (ACK), '-' (NACK), \x03 (CTRL+C interrupt),
371 // or '$' character (start of packet header) or of course,
372 // the end of the data in m_bytes...
373 const size_t bytes_len = m_bytes.size();
374 bool done = false;
375 uint32_t idx;
376 for (idx = 1; !done && idx < bytes_len; ++idx)
377 {
378 switch (m_bytes[idx])
379 {
380 case '+':
381 case '-':
382 case '\x03':
383 case '$':
384 done = true;
385 break;
386
387 default:
388 break;
389 }
390 }
Greg Clayton604f0d32011-06-17 03:31:01 +0000391 if (log)
Greg Claytona9eb8272011-07-02 21:07:54 +0000392 log->Printf ("GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'",
393 __FUNCTION__, idx, idx, m_bytes.c_str());
394 m_bytes.erase(0, idx);
Greg Clayton604f0d32011-06-17 03:31:01 +0000395 }
Chris Lattner24943d22010-06-08 16:52:24 +0000396 break;
397 }
398
Greg Clayton63afdb02011-06-17 01:22:15 +0000399 if (content_length == std::string::npos)
Chris Lattner24943d22010-06-08 16:52:24 +0000400 {
Greg Clayton63afdb02011-06-17 01:22:15 +0000401 packet.Clear();
402 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000403 }
Greg Clayton604f0d32011-06-17 03:31:01 +0000404 else if (total_length > 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000405 {
Greg Clayton63afdb02011-06-17 01:22:15 +0000406
Chris Lattner24943d22010-06-08 16:52:24 +0000407 // We have a valid packet...
Greg Clayton63afdb02011-06-17 01:22:15 +0000408 assert (content_length <= m_bytes.size());
409 assert (total_length <= m_bytes.size());
410 assert (content_length <= total_length);
411
412 bool success = true;
413 std::string &packet_str = packet.GetStringRef();
Greg Clayton451fa822012-04-09 22:46:21 +0000414
415
416 if (log)
417 {
418 // If logging was just enabled and we have history, then dump out what
419 // we have to the log so we get the historical context. The Dump() call that
420 // logs all of the packet will set a boolean so that we don't dump this more
421 // than once
422 if (!m_history.DidDumpToLog ())
423 m_history.Dump (log.get());
424
425 log->Printf ("<%4zu> read packet: %.*s", total_length, (int)(total_length), m_bytes.c_str());
426 }
427
428 m_history.AddPacket (m_bytes.c_str(), total_length, History::ePacketTypeRecv, total_length);
429
Greg Clayton63afdb02011-06-17 01:22:15 +0000430 packet_str.assign (m_bytes, content_start, content_length);
Greg Clayton451fa822012-04-09 22:46:21 +0000431
Greg Clayton63afdb02011-06-17 01:22:15 +0000432 if (m_bytes[0] == '$')
433 {
434 assert (checksum_idx < m_bytes.size());
435 if (::isxdigit (m_bytes[checksum_idx+0]) ||
436 ::isxdigit (m_bytes[checksum_idx+1]))
437 {
438 if (GetSendAcks ())
439 {
440 const char *packet_checksum_cstr = &m_bytes[checksum_idx];
441 char packet_checksum = strtol (packet_checksum_cstr, NULL, 16);
442 char actual_checksum = CalculcateChecksum (packet_str.c_str(), packet_str.size());
443 success = packet_checksum == actual_checksum;
444 if (!success)
445 {
446 if (log)
447 log->Printf ("error: checksum mismatch: %.*s expected 0x%2.2x, got 0x%2.2x",
448 (int)(total_length),
449 m_bytes.c_str(),
450 (uint8_t)packet_checksum,
451 (uint8_t)actual_checksum);
452 }
453 // Send the ack or nack if needed
454 if (!success)
455 SendNack();
456 else
457 SendAck();
458 }
Greg Clayton63afdb02011-06-17 01:22:15 +0000459 }
460 else
461 {
462 success = false;
463 if (log)
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000464 log->Printf ("error: invalid checksum in packet: '%s'\n", m_bytes.c_str());
Greg Clayton63afdb02011-06-17 01:22:15 +0000465 }
466 }
Greg Clayton451fa822012-04-09 22:46:21 +0000467
Greg Clayton63afdb02011-06-17 01:22:15 +0000468 m_bytes.erase(0, total_length);
469 packet.SetFilePos(0);
470 return success;
Chris Lattner24943d22010-06-08 16:52:24 +0000471 }
Chris Lattner24943d22010-06-08 16:52:24 +0000472 }
Greg Clayton63afdb02011-06-17 01:22:15 +0000473 packet.Clear();
474 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000475}
476
Greg Claytonb72d0f02011-04-12 05:54:46 +0000477Error
478GDBRemoteCommunication::StartDebugserverProcess (const char *debugserver_url,
479 const char *unix_socket_name, // For handshaking
480 lldb_private::ProcessLaunchInfo &launch_info)
481{
482 Error error;
483 // If we locate debugserver, keep that located version around
484 static FileSpec g_debugserver_file_spec;
485
486 // This function will fill in the launch information for the debugserver
487 // instance that gets launched.
488 launch_info.Clear();
489
490 char debugserver_path[PATH_MAX];
491 FileSpec &debugserver_file_spec = launch_info.GetExecutableFile();
492
493 // Always check to see if we have an environment override for the path
494 // to the debugserver to use and use it if we do.
495 const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH");
496 if (env_debugserver_path)
497 debugserver_file_spec.SetFile (env_debugserver_path, false);
498 else
499 debugserver_file_spec = g_debugserver_file_spec;
500 bool debugserver_exists = debugserver_file_spec.Exists();
501 if (!debugserver_exists)
502 {
503 // The debugserver binary is in the LLDB.framework/Resources
504 // directory.
505 if (Host::GetLLDBPath (ePathTypeSupportExecutableDir, debugserver_file_spec))
506 {
507 debugserver_file_spec.GetFilename().SetCString(DEBUGSERVER_BASENAME);
508 debugserver_exists = debugserver_file_spec.Exists();
509 if (debugserver_exists)
510 {
511 g_debugserver_file_spec = debugserver_file_spec;
512 }
513 else
514 {
515 g_debugserver_file_spec.Clear();
516 debugserver_file_spec.Clear();
517 }
518 }
519 }
520
521 if (debugserver_exists)
522 {
523 debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path));
524
525 Args &debugserver_args = launch_info.GetArguments();
526 debugserver_args.Clear();
527 char arg_cstr[PATH_MAX];
528
529 // Start args with "debugserver /file/path -r --"
530 debugserver_args.AppendArgument(debugserver_path);
531 debugserver_args.AppendArgument(debugserver_url);
532 // use native registers, not the GDB registers
533 debugserver_args.AppendArgument("--native-regs");
534 // make debugserver run in its own session so signals generated by
535 // special terminal key sequences (^C) don't affect debugserver
536 debugserver_args.AppendArgument("--setsid");
537
538 if (unix_socket_name && unix_socket_name[0])
539 {
540 debugserver_args.AppendArgument("--unix-socket");
541 debugserver_args.AppendArgument(unix_socket_name);
542 }
543
544 const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE");
545 if (env_debugserver_log_file)
546 {
547 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file);
548 debugserver_args.AppendArgument(arg_cstr);
549 }
550
551 const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS");
552 if (env_debugserver_log_flags)
553 {
554 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags);
555 debugserver_args.AppendArgument(arg_cstr);
556 }
557 // debugserver_args.AppendArgument("--log-file=/tmp/debugserver.txt");
558 // debugserver_args.AppendArgument("--log-flags=0x802e0e");
559
560 // We currently send down all arguments, attach pids, or attach
561 // process names in dedicated GDB server packets, so we don't need
562 // to pass them as arguments. This is currently because of all the
563 // things we need to setup prior to launching: the environment,
564 // current working dir, file actions, etc.
565#if 0
566 // Now append the program arguments
567 if (inferior_argv)
568 {
569 // Terminate the debugserver args so we can now append the inferior args
570 debugserver_args.AppendArgument("--");
571
572 for (int i = 0; inferior_argv[i] != NULL; ++i)
573 debugserver_args.AppendArgument (inferior_argv[i]);
574 }
575 else if (attach_pid != LLDB_INVALID_PROCESS_ID)
576 {
577 ::snprintf (arg_cstr, sizeof(arg_cstr), "--attach=%u", attach_pid);
578 debugserver_args.AppendArgument (arg_cstr);
579 }
580 else if (attach_name && attach_name[0])
581 {
582 if (wait_for_launch)
583 debugserver_args.AppendArgument ("--waitfor");
584 else
585 debugserver_args.AppendArgument ("--attach");
586 debugserver_args.AppendArgument (attach_name);
587 }
588#endif
589
590 // Close STDIN, STDOUT and STDERR. We might need to redirect them
591 // to "/dev/null" if we run into any problems.
592// launch_info.AppendCloseFileAction (STDIN_FILENO);
593// launch_info.AppendCloseFileAction (STDOUT_FILENO);
594// launch_info.AppendCloseFileAction (STDERR_FILENO);
595
596 error = Host::LaunchProcess(launch_info);
597 }
598 else
599 {
Greg Clayton9c236732011-10-26 00:56:27 +0000600 error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME );
Greg Claytonb72d0f02011-04-12 05:54:46 +0000601 }
602 return error;
603}
604
Greg Clayton451fa822012-04-09 22:46:21 +0000605void
606GDBRemoteCommunication::DumpHistory(const char *path)
607{
608 StreamFile strm;
609 Error error (strm.GetFile().Open(path, File::eOpenOptionWrite | File::eOpenOptionCanCreate));
610 if (error.Success())
611 m_history.Dump (strm);
612 else
613 fprintf (stderr, "error: unable to open '%s' -- %s\n", path, error.AsCString());
614}