blob: d633b3eaf34c4ec3ee24195095acdbd398894aea [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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Log.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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Host/TimeValue.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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034
35// Project includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "ProcessGDBRemoteLog.h"
37
Todd Fiala015d8182014-07-22 23:41:36 +000038#if defined(__APPLE__)
39# define DEBUGSERVER_BASENAME "debugserver"
40#else
41# define DEBUGSERVER_BASENAME "lldb-gdbserver"
42#endif
Greg Clayton8b82f082011-04-12 05:54:46 +000043
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044using namespace lldb;
45using namespace lldb_private;
46
Greg Claytonc1422c12012-04-09 22:46:21 +000047GDBRemoteCommunication::History::History (uint32_t size) :
48 m_packets(),
49 m_curr_idx (0),
50 m_total_packet_count (0),
51 m_dumped_to_log (false)
52{
53 m_packets.resize(size);
54}
55
56GDBRemoteCommunication::History::~History ()
57{
58}
59
60void
Greg Claytond451c1a2012-04-13 21:24:18 +000061GDBRemoteCommunication::History::AddPacket (char packet_char,
62 PacketType type,
63 uint32_t bytes_transmitted)
64{
65 const size_t size = m_packets.size();
66 if (size > 0)
67 {
68 const uint32_t idx = GetNextIndex();
69 m_packets[idx].packet.assign (1, packet_char);
70 m_packets[idx].type = type;
71 m_packets[idx].bytes_transmitted = bytes_transmitted;
72 m_packets[idx].packet_idx = m_total_packet_count;
73 m_packets[idx].tid = Host::GetCurrentThreadID();
74 }
75}
76
77void
78GDBRemoteCommunication::History::AddPacket (const std::string &src,
79 uint32_t src_len,
80 PacketType type,
81 uint32_t bytes_transmitted)
82{
83 const size_t size = m_packets.size();
84 if (size > 0)
85 {
86 const uint32_t idx = GetNextIndex();
87 m_packets[idx].packet.assign (src, 0, src_len);
88 m_packets[idx].type = type;
89 m_packets[idx].bytes_transmitted = bytes_transmitted;
90 m_packets[idx].packet_idx = m_total_packet_count;
91 m_packets[idx].tid = Host::GetCurrentThreadID();
92 }
93}
94
95void
Greg Claytonc1422c12012-04-09 22:46:21 +000096GDBRemoteCommunication::History::Dump (lldb_private::Stream &strm) const
97{
98 const uint32_t size = GetNumPacketsInHistory ();
99 const uint32_t first_idx = GetFirstSavedPacketIndex ();
100 const uint32_t stop_idx = m_curr_idx + size;
101 for (uint32_t i = first_idx; i < stop_idx; ++i)
102 {
103 const uint32_t idx = NormalizeIndex (i);
104 const Entry &entry = m_packets[idx];
105 if (entry.type == ePacketTypeInvalid || entry.packet.empty())
106 break;
Daniel Malead01b2952012-11-29 21:49:15 +0000107 strm.Printf ("history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s\n",
Greg Claytonc1422c12012-04-09 22:46:21 +0000108 entry.packet_idx,
Greg Claytond451c1a2012-04-13 21:24:18 +0000109 entry.tid,
Greg Claytonc1422c12012-04-09 22:46:21 +0000110 entry.bytes_transmitted,
111 (entry.type == ePacketTypeSend) ? "send" : "read",
112 entry.packet.c_str());
113 }
114}
115
116void
117GDBRemoteCommunication::History::Dump (lldb_private::Log *log) const
118{
119 if (log && !m_dumped_to_log)
120 {
121 m_dumped_to_log = true;
122 const uint32_t size = GetNumPacketsInHistory ();
123 const uint32_t first_idx = GetFirstSavedPacketIndex ();
124 const uint32_t stop_idx = m_curr_idx + size;
125 for (uint32_t i = first_idx; i < stop_idx; ++i)
126 {
127 const uint32_t idx = NormalizeIndex (i);
128 const Entry &entry = m_packets[idx];
129 if (entry.type == ePacketTypeInvalid || entry.packet.empty())
130 break;
Daniel Malead01b2952012-11-29 21:49:15 +0000131 log->Printf ("history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s",
Greg Claytonc1422c12012-04-09 22:46:21 +0000132 entry.packet_idx,
Greg Claytond451c1a2012-04-13 21:24:18 +0000133 entry.tid,
Greg Claytonc1422c12012-04-09 22:46:21 +0000134 entry.bytes_transmitted,
135 (entry.type == ePacketTypeSend) ? "send" : "read",
136 entry.packet.c_str());
137 }
138 }
139}
140
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141//----------------------------------------------------------------------
142// GDBRemoteCommunication constructor
143//----------------------------------------------------------------------
Greg Clayton8b82f082011-04-12 05:54:46 +0000144GDBRemoteCommunication::GDBRemoteCommunication(const char *comm_name,
145 const char *listener_name,
146 bool is_platform) :
Greg Clayton576d8832011-03-22 04:00:09 +0000147 Communication(comm_name),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000148#ifdef LLDB_CONFIGURATION_DEBUG
149 m_packet_timeout (1000),
150#else
Greg Claytonf3dd93c2011-06-17 03:31:01 +0000151 m_packet_timeout (1),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000152#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153 m_sequence_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton4dc72282011-01-20 07:53:45 +0000154 m_public_is_running (false),
Greg Clayton1cb64962011-03-24 04:28:38 +0000155 m_private_is_running (false),
Greg Claytonc1422c12012-04-09 22:46:21 +0000156 m_history (512),
Greg Clayton8b82f082011-04-12 05:54:46 +0000157 m_send_acks (true),
Greg Clayton00fe87b2013-12-05 22:58:22 +0000158 m_is_platform (is_platform),
Greg Clayton00fe87b2013-12-05 22:58:22 +0000159 m_listen_url ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161}
162
163//----------------------------------------------------------------------
164// Destructor
165//----------------------------------------------------------------------
166GDBRemoteCommunication::~GDBRemoteCommunication()
167{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168 if (IsConnected())
169 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170 Disconnect();
171 }
172}
173
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174char
175GDBRemoteCommunication::CalculcateChecksum (const char *payload, size_t payload_length)
176{
177 int checksum = 0;
178
Ed Mastea6b4c772013-08-20 14:12:58 +0000179 for (size_t i = 0; i < payload_length; ++i)
180 checksum += payload[i];
181
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182 return checksum & 255;
183}
184
185size_t
Greg Clayton6ed95942011-01-22 07:12:45 +0000186GDBRemoteCommunication::SendAck ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187{
Greg Clayton5160ce52013-03-27 23:08:40 +0000188 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189 ConnectionStatus status = eConnectionStatusSuccess;
Greg Claytonc1422c12012-04-09 22:46:21 +0000190 char ch = '+';
191 const size_t bytes_written = Write (&ch, 1, status, NULL);
192 if (log)
Greg Clayton45989072013-10-23 18:24:30 +0000193 log->Printf ("<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch);
Greg Claytonc1422c12012-04-09 22:46:21 +0000194 m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written);
195 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196}
197
198size_t
Greg Clayton6ed95942011-01-22 07:12:45 +0000199GDBRemoteCommunication::SendNack ()
200{
Greg Clayton5160ce52013-03-27 23:08:40 +0000201 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Greg Clayton6ed95942011-01-22 07:12:45 +0000202 ConnectionStatus status = eConnectionStatusSuccess;
Greg Claytonc1422c12012-04-09 22:46:21 +0000203 char ch = '-';
204 const size_t bytes_written = Write (&ch, 1, status, NULL);
205 if (log)
Greg Clayton45989072013-10-23 18:24:30 +0000206 log->Printf("<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch);
Greg Claytonc1422c12012-04-09 22:46:21 +0000207 m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written);
208 return bytes_written;
Greg Clayton32e0a752011-03-30 18:16:51 +0000209}
210
Greg Clayton3dedae12013-12-06 21:45:27 +0000211GDBRemoteCommunication::PacketResult
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212GDBRemoteCommunication::SendPacket (const char *payload, size_t payload_length)
213{
214 Mutex::Locker locker(m_sequence_mutex);
215 return SendPacketNoLock (payload, payload_length);
216}
217
Greg Clayton3dedae12013-12-06 21:45:27 +0000218GDBRemoteCommunication::PacketResult
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219GDBRemoteCommunication::SendPacketNoLock (const char *payload, size_t payload_length)
220{
221 if (IsConnected())
222 {
223 StreamString packet(0, 4, eByteOrderBig);
224
225 packet.PutChar('$');
226 packet.Write (payload, payload_length);
227 packet.PutChar('#');
228 packet.PutHex8(CalculcateChecksum (payload, payload_length));
229
Greg Clayton5160ce52013-03-27 23:08:40 +0000230 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton7e244322014-09-18 00:17:36 +0000232 const char *packet_data = packet.GetData();
233 const size_t packet_length = packet.GetSize();
234 size_t bytes_written = Write (packet_data, packet_length, status, NULL);
Greg Claytonc1422c12012-04-09 22:46:21 +0000235 if (log)
236 {
Greg Clayton7e244322014-09-18 00:17:36 +0000237 size_t binary_start_offset = 0;
238 if (strncmp(packet_data, "$vFile:pwrite:", strlen("$vFile:pwrite:")) == 0)
239 {
240 const char *first_comma = strchr(packet_data, ',');
241 if (first_comma)
242 {
243 const char *second_comma = strchr(first_comma + 1, ',');
244 if (second_comma)
245 binary_start_offset = second_comma - packet_data + 1;
246 }
247 }
248
Greg Claytonc1422c12012-04-09 22:46:21 +0000249 // If logging was just enabled and we have history, then dump out what
250 // we have to the log so we get the historical context. The Dump() call that
251 // logs all of the packet will set a boolean so that we don't dump this more
252 // than once
253 if (!m_history.DidDumpToLog ())
Greg Clayton5160ce52013-03-27 23:08:40 +0000254 m_history.Dump (log);
Greg Claytonc1422c12012-04-09 22:46:21 +0000255
Greg Clayton7e244322014-09-18 00:17:36 +0000256 if (binary_start_offset)
257 {
258 StreamString strm;
259 // Print non binary data header
260 strm.Printf("<%4" PRIu64 "> send packet: %.*s", (uint64_t)bytes_written, (int)binary_start_offset, packet_data);
261 const uint8_t *p;
262 // Print binary data exactly as sent
263 for (p = (uint8_t*)packet_data + binary_start_offset; *p != '#'; ++p)
264 strm.Printf("\\x%2.2x", *p);
265 // Print the checksum
266 strm.Printf("%*s", (int)3, p);
267 log->PutCString(strm.GetString().c_str());
268 }
269 else
270 log->Printf("<%4" PRIu64 "> send packet: %.*s", (uint64_t)bytes_written, (int)packet_length, packet_data);
Greg Claytonc1422c12012-04-09 22:46:21 +0000271 }
272
Greg Clayton7e244322014-09-18 00:17:36 +0000273 m_history.AddPacket (packet.GetString(), packet_length, History::ePacketTypeSend, bytes_written);
Greg Claytonc1422c12012-04-09 22:46:21 +0000274
275
Greg Clayton7e244322014-09-18 00:17:36 +0000276 if (bytes_written == packet_length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277 {
Greg Clayton71fc2a32011-02-12 06:28:37 +0000278 if (GetSendAcks ())
Greg Clayton3dedae12013-12-06 21:45:27 +0000279 return GetAck ();
280 else
281 return PacketResult::Success;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282 }
Johnny Chend0c40dd2010-09-14 22:10:43 +0000283 else
284 {
Greg Clayton6d093452011-02-05 02:25:06 +0000285 if (log)
Greg Clayton7e244322014-09-18 00:17:36 +0000286 log->Printf ("error: failed to send packet: %.*s", (int)packet_length, packet_data);
Johnny Chend0c40dd2010-09-14 22:10:43 +0000287 }
Greg Claytonf5e56de2010-09-14 23:36:40 +0000288 }
Greg Clayton3dedae12013-12-06 21:45:27 +0000289 return PacketResult::ErrorSendFailed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290}
291
Greg Clayton3dedae12013-12-06 21:45:27 +0000292GDBRemoteCommunication::PacketResult
Greg Claytonc574ede2011-03-10 02:26:48 +0000293GDBRemoteCommunication::GetAck ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294{
Greg Clayton576d8832011-03-22 04:00:09 +0000295 StringExtractorGDBRemote packet;
Greg Clayton3dedae12013-12-06 21:45:27 +0000296 PacketResult result = WaitForPacketWithTimeoutMicroSecondsNoLock (packet, GetPacketTimeoutInMicroSeconds ());
297 if (result == PacketResult::Success)
298 {
299 if (packet.GetResponseType() == StringExtractorGDBRemote::ResponseType::eAck)
300 return PacketResult::Success;
301 else
302 return PacketResult::ErrorSendAck;
303 }
304 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305}
306
307bool
Jim Ingham4ceb9282012-06-08 22:50:40 +0000308GDBRemoteCommunication::GetSequenceMutex (Mutex::Locker& locker, const char *failure_message)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309{
Greg Claytond9896732012-05-31 16:54:51 +0000310 if (IsRunning())
Jim Ingham4ceb9282012-06-08 22:50:40 +0000311 return locker.TryLock (m_sequence_mutex, failure_message);
Greg Claytond9896732012-05-31 16:54:51 +0000312
313 locker.Lock (m_sequence_mutex);
314 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315}
316
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317
Greg Clayton6779606a2011-01-22 23:43:18 +0000318bool
Greg Clayton6779606a2011-01-22 23:43:18 +0000319GDBRemoteCommunication::WaitForNotRunningPrivate (const TimeValue *timeout_ptr)
320{
321 return m_private_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL);
322}
323
Greg Clayton3dedae12013-12-06 21:45:27 +0000324GDBRemoteCommunication::PacketResult
Greg Clayton73bf5db2011-06-17 01:22:15 +0000325GDBRemoteCommunication::WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &packet, uint32_t timeout_usec)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326{
Greg Clayton73bf5db2011-06-17 01:22:15 +0000327 uint8_t buffer[8192];
328 Error error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329
Greg Clayton5160ce52013-03-27 23:08:40 +0000330 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS | GDBR_LOG_VERBOSE));
Greg Clayton644247c2011-07-07 01:59:51 +0000331
Greg Clayton73bf5db2011-06-17 01:22:15 +0000332 // Check for a packet from our cache first without trying any reading...
333 if (CheckForPacket (NULL, 0, packet))
Greg Clayton3dedae12013-12-06 21:45:27 +0000334 return PacketResult::Success;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335
Greg Clayton0c51ac32011-07-02 23:21:06 +0000336 bool timed_out = false;
Greg Clayton3dedae12013-12-06 21:45:27 +0000337 bool disconnected = false;
Greg Clayton0c51ac32011-07-02 23:21:06 +0000338 while (IsConnected() && !timed_out)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339 {
Johnny Chen74549c82011-07-19 01:13:00 +0000340 lldb::ConnectionStatus status = eConnectionStatusNoConnection;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000341 size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error);
Greg Clayton644247c2011-07-07 01:59:51 +0000342
343 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000344 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 +0000345 __PRETTY_FUNCTION__,
346 timeout_usec,
347 Communication::ConnectionStatusAsCString (status),
348 error.AsCString(),
Greg Clayton43e0af02012-09-18 18:04:04 +0000349 (uint64_t)bytes_read);
Greg Clayton644247c2011-07-07 01:59:51 +0000350
Greg Clayton73bf5db2011-06-17 01:22:15 +0000351 if (bytes_read > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000353 if (CheckForPacket (buffer, bytes_read, packet))
Greg Clayton3dedae12013-12-06 21:45:27 +0000354 return PacketResult::Success;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000355 }
356 else
357 {
358 switch (status)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359 {
Greg Clayton197bacf2011-07-02 21:07:54 +0000360 case eConnectionStatusTimedOut:
Greg Claytonf0066ad2014-05-02 00:45:31 +0000361 case eConnectionStatusInterrupted:
Greg Clayton0c51ac32011-07-02 23:21:06 +0000362 timed_out = true;
363 break;
364 case eConnectionStatusSuccess:
365 //printf ("status = success but error = %s\n", error.AsCString("<invalid>"));
Greg Clayton73bf5db2011-06-17 01:22:15 +0000366 break;
367
368 case eConnectionStatusEndOfFile:
369 case eConnectionStatusNoConnection:
370 case eConnectionStatusLostConnection:
371 case eConnectionStatusError:
Greg Clayton3dedae12013-12-06 21:45:27 +0000372 disconnected = true;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000373 Disconnect();
374 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375 }
376 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377 }
Greg Clayton3dedae12013-12-06 21:45:27 +0000378 packet.Clear ();
379 if (disconnected)
380 return PacketResult::ErrorDisconnected;
381 if (timed_out)
382 return PacketResult::ErrorReplyTimeout;
383 else
384 return PacketResult::ErrorReplyFailed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385}
386
Greg Clayton73bf5db2011-06-17 01:22:15 +0000387bool
388GDBRemoteCommunication::CheckForPacket (const uint8_t *src, size_t src_len, StringExtractorGDBRemote &packet)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389{
390 // Put the packet data into the buffer in a thread safe fashion
391 Mutex::Locker locker(m_bytes_mutex);
Greg Clayton197bacf2011-07-02 21:07:54 +0000392
Greg Clayton5160ce52013-03-27 23:08:40 +0000393 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Greg Clayton197bacf2011-07-02 21:07:54 +0000394
Greg Clayton73bf5db2011-06-17 01:22:15 +0000395 if (src && src_len > 0)
Greg Clayton197bacf2011-07-02 21:07:54 +0000396 {
Greg Clayton0c51ac32011-07-02 23:21:06 +0000397 if (log && log->GetVerbose())
Greg Clayton197bacf2011-07-02 21:07:54 +0000398 {
399 StreamString s;
Greg Clayton0c51ac32011-07-02 23:21:06 +0000400 log->Printf ("GDBRemoteCommunication::%s adding %u bytes: %.*s",
401 __FUNCTION__,
402 (uint32_t)src_len,
403 (uint32_t)src_len,
404 src);
Greg Clayton197bacf2011-07-02 21:07:54 +0000405 }
Greg Clayton73bf5db2011-06-17 01:22:15 +0000406 m_bytes.append ((const char *)src, src_len);
Greg Clayton197bacf2011-07-02 21:07:54 +0000407 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408
409 // Parse up the packets into gdb remote packets
Greg Clayton197bacf2011-07-02 21:07:54 +0000410 if (!m_bytes.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411 {
412 // end_idx must be one past the last valid packet byte. Start
413 // it off with an invalid value that is the same as the current
414 // index.
Greg Clayton73bf5db2011-06-17 01:22:15 +0000415 size_t content_start = 0;
Greg Clayton118593a2014-11-03 21:02:54 +0000416 size_t content_length = 0;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000417 size_t total_length = 0;
418 size_t checksum_idx = std::string::npos;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419
Greg Clayton118593a2014-11-03 21:02:54 +0000420 switch (m_bytes[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421 {
Greg Clayton118593a2014-11-03 21:02:54 +0000422 case '+': // Look for ack
423 case '-': // Look for cancel
424 case '\x03': // ^C to halt target
425 content_length = total_length = 1; // The command is one byte long...
426 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427
Greg Clayton118593a2014-11-03 21:02:54 +0000428 case '$':
429 // Look for a standard gdb packet?
430 {
431 size_t hash_pos = m_bytes.find('#');
432 if (hash_pos != std::string::npos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433 {
Greg Clayton118593a2014-11-03 21:02:54 +0000434 if (hash_pos + 2 < m_bytes.size())
Greg Clayton73bf5db2011-06-17 01:22:15 +0000435 {
Greg Clayton118593a2014-11-03 21:02:54 +0000436 checksum_idx = hash_pos + 1;
437 // Skip the dollar sign
438 content_start = 1;
439 // Don't include the # in the content or the $ in the content length
440 content_length = hash_pos - 1;
441
442 total_length = hash_pos + 3; // Skip the # and the two hex checksum bytes
443 }
444 else
445 {
446 // Checksum bytes aren't all here yet
447 content_length = std::string::npos;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000448 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000449 }
Greg Clayton118593a2014-11-03 21:02:54 +0000450 }
451 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452
Greg Clayton118593a2014-11-03 21:02:54 +0000453 default:
454 {
455 // We have an unexpected byte and we need to flush all bad
456 // data that is in m_bytes, so we need to find the first
457 // byte that is a '+' (ACK), '-' (NACK), \x03 (CTRL+C interrupt),
458 // or '$' character (start of packet header) or of course,
459 // the end of the data in m_bytes...
460 const size_t bytes_len = m_bytes.size();
461 bool done = false;
462 uint32_t idx;
463 for (idx = 1; !done && idx < bytes_len; ++idx)
Greg Clayton197bacf2011-07-02 21:07:54 +0000464 {
Greg Clayton118593a2014-11-03 21:02:54 +0000465 switch (m_bytes[idx])
Greg Clayton197bacf2011-07-02 21:07:54 +0000466 {
Greg Clayton118593a2014-11-03 21:02:54 +0000467 case '+':
468 case '-':
469 case '\x03':
470 case '$':
471 done = true;
472 break;
473
474 default:
475 break;
Greg Clayton197bacf2011-07-02 21:07:54 +0000476 }
477 }
Greg Clayton118593a2014-11-03 21:02:54 +0000478 if (log)
479 log->Printf ("GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'",
480 __FUNCTION__, idx - 1, idx - 1, m_bytes.c_str());
481 m_bytes.erase(0, idx - 1);
482 }
483 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484 }
485
Greg Clayton73bf5db2011-06-17 01:22:15 +0000486 if (content_length == std::string::npos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000488 packet.Clear();
489 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490 }
Greg Claytonf3dd93c2011-06-17 03:31:01 +0000491 else if (total_length > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000493
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494 // We have a valid packet...
Greg Clayton73bf5db2011-06-17 01:22:15 +0000495 assert (content_length <= m_bytes.size());
496 assert (total_length <= m_bytes.size());
497 assert (content_length <= total_length);
Greg Clayton06f09b52014-06-20 20:41:07 +0000498 const size_t content_end = content_start + content_length;
499
Greg Clayton73bf5db2011-06-17 01:22:15 +0000500 bool success = true;
501 std::string &packet_str = packet.GetStringRef();
Greg Claytonc1422c12012-04-09 22:46:21 +0000502
503
504 if (log)
505 {
506 // If logging was just enabled and we have history, then dump out what
507 // we have to the log so we get the historical context. The Dump() call that
508 // logs all of the packet will set a boolean so that we don't dump this more
509 // than once
510 if (!m_history.DidDumpToLog ())
Greg Clayton5160ce52013-03-27 23:08:40 +0000511 m_history.Dump (log);
Greg Claytonc1422c12012-04-09 22:46:21 +0000512
Greg Clayton06f09b52014-06-20 20:41:07 +0000513 bool binary = false;
514 // Only detect binary for packets that start with a '$' and have a '#CC' checksum
515 if (m_bytes[0] == '$' && total_length > 4)
516 {
517 for (size_t i=0; !binary && i<total_length; ++i)
518 {
519 if (isprint(m_bytes[i]) == 0)
520 binary = true;
521 }
522 }
523 if (binary)
524 {
525 StreamString strm;
526 // Packet header...
527 strm.Printf("<%4" PRIu64 "> read packet: %c", (uint64_t)total_length, m_bytes[0]);
528 for (size_t i=content_start; i<content_end; ++i)
529 {
530 // Remove binary escaped bytes when displaying the packet...
531 const char ch = m_bytes[i];
532 if (ch == 0x7d)
533 {
534 // 0x7d is the escape character. The next character is to
535 // be XOR'd with 0x20.
536 const char escapee = m_bytes[++i] ^ 0x20;
537 strm.Printf("%2.2x", escapee);
538 }
539 else
540 {
541 strm.Printf("%2.2x", (uint8_t)ch);
542 }
543 }
544 // Packet footer...
545 strm.Printf("%c%c%c", m_bytes[total_length-3], m_bytes[total_length-2], m_bytes[total_length-1]);
546 log->PutCString(strm.GetString().c_str());
547 }
548 else
549 {
550 log->Printf("<%4" PRIu64 "> read packet: %.*s", (uint64_t)total_length, (int)(total_length), m_bytes.c_str());
551 }
Greg Claytonc1422c12012-04-09 22:46:21 +0000552 }
553
554 m_history.AddPacket (m_bytes.c_str(), total_length, History::ePacketTypeRecv, total_length);
555
Hafiz Abid Qadeere5fd5e12013-08-28 15:10:37 +0000556 // Clear packet_str in case there is some existing data in it.
557 packet_str.clear();
Hafiz Abid Qadeerda96ef22013-08-28 10:31:52 +0000558 // Copy the packet from m_bytes to packet_str expanding the
559 // run-length encoding in the process.
560 // Reserve enough byte for the most common case (no RLE used)
561 packet_str.reserve(m_bytes.length());
Greg Clayton06f09b52014-06-20 20:41:07 +0000562 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 +0000563 {
564 if (*c == '*')
565 {
566 // '*' indicates RLE. Next character will give us the
567 // repeat count and previous character is what is to be
568 // repeated.
569 char char_to_repeat = packet_str.back();
570 // Number of time the previous character is repeated
571 int repeat_count = *++c + 3 - ' ';
572 // We have the char_to_repeat and repeat_count. Now push
573 // it in the packet.
574 for (int i = 0; i < repeat_count; ++i)
575 packet_str.push_back(char_to_repeat);
576 }
Steve Pucci3c5d3332014-02-24 19:07:29 +0000577 else if (*c == 0x7d)
578 {
579 // 0x7d is the escape character. The next character is to
580 // be XOR'd with 0x20.
581 char escapee = *++c ^ 0x20;
582 packet_str.push_back(escapee);
583 }
Hafiz Abid Qadeerda96ef22013-08-28 10:31:52 +0000584 else
585 {
586 packet_str.push_back(*c);
587 }
588 }
589
Greg Clayton73bf5db2011-06-17 01:22:15 +0000590 if (m_bytes[0] == '$')
591 {
592 assert (checksum_idx < m_bytes.size());
593 if (::isxdigit (m_bytes[checksum_idx+0]) ||
594 ::isxdigit (m_bytes[checksum_idx+1]))
595 {
596 if (GetSendAcks ())
597 {
598 const char *packet_checksum_cstr = &m_bytes[checksum_idx];
599 char packet_checksum = strtol (packet_checksum_cstr, NULL, 16);
600 char actual_checksum = CalculcateChecksum (packet_str.c_str(), packet_str.size());
601 success = packet_checksum == actual_checksum;
602 if (!success)
603 {
604 if (log)
605 log->Printf ("error: checksum mismatch: %.*s expected 0x%2.2x, got 0x%2.2x",
606 (int)(total_length),
607 m_bytes.c_str(),
608 (uint8_t)packet_checksum,
609 (uint8_t)actual_checksum);
610 }
611 // Send the ack or nack if needed
612 if (!success)
613 SendNack();
614 else
615 SendAck();
616 }
Greg Clayton73bf5db2011-06-17 01:22:15 +0000617 }
618 else
619 {
620 success = false;
621 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000622 log->Printf ("error: invalid checksum in packet: '%s'\n", m_bytes.c_str());
Greg Clayton73bf5db2011-06-17 01:22:15 +0000623 }
624 }
Greg Claytonc1422c12012-04-09 22:46:21 +0000625
Greg Clayton73bf5db2011-06-17 01:22:15 +0000626 m_bytes.erase(0, total_length);
627 packet.SetFilePos(0);
628 return success;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000630 }
Greg Clayton73bf5db2011-06-17 01:22:15 +0000631 packet.Clear();
632 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633}
634
Greg Clayton8b82f082011-04-12 05:54:46 +0000635Error
Greg Claytond6299802013-12-06 17:46:35 +0000636GDBRemoteCommunication::StartListenThread (const char *hostname, uint16_t port)
Greg Clayton00fe87b2013-12-05 22:58:22 +0000637{
638 Error error;
Zachary Turneracee96a2014-09-23 18:32:09 +0000639 if (m_listen_thread.IsJoinable())
Greg Clayton00fe87b2013-12-05 22:58:22 +0000640 {
641 error.SetErrorString("listen thread already running");
642 }
643 else
644 {
645 char listen_url[512];
646 if (hostname && hostname[0])
Jean-Daniel Dupas3c6774a2014-02-08 20:29:40 +0000647 snprintf(listen_url, sizeof(listen_url), "listen://%s:%i", hostname, port);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000648 else
649 snprintf(listen_url, sizeof(listen_url), "listen://%i", port);
650 m_listen_url = listen_url;
651 SetConnection(new ConnectionFileDescriptor());
Zachary Turner39de3112014-09-09 20:54:56 +0000652 m_listen_thread = ThreadLauncher::LaunchThread(listen_url, GDBRemoteCommunication::ListenThread, this, &error);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000653 }
654 return error;
655}
656
657bool
658GDBRemoteCommunication::JoinListenThread ()
659{
Zachary Turneracee96a2014-09-23 18:32:09 +0000660 if (m_listen_thread.IsJoinable())
Zachary Turner39de3112014-09-09 20:54:56 +0000661 m_listen_thread.Join(nullptr);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000662 return true;
663}
664
665lldb::thread_result_t
666GDBRemoteCommunication::ListenThread (lldb::thread_arg_t arg)
667{
668 GDBRemoteCommunication *comm = (GDBRemoteCommunication *)arg;
669 Error error;
670 ConnectionFileDescriptor *connection = (ConnectionFileDescriptor *)comm->GetConnection ();
671
672 if (connection)
673 {
674 // Do the listen on another thread so we can continue on...
675 if (connection->Connect(comm->m_listen_url.c_str(), &error) != eConnectionStatusSuccess)
676 comm->SetConnection(NULL);
677 }
678 return NULL;
679}
680
681Error
Greg Claytonfda4fab2014-01-10 22:24:11 +0000682GDBRemoteCommunication::StartDebugserverProcess (const char *hostname,
683 uint16_t in_port,
Greg Clayton91a9b2472013-12-04 19:19:12 +0000684 lldb_private::ProcessLaunchInfo &launch_info,
Greg Claytonfda4fab2014-01-10 22:24:11 +0000685 uint16_t &out_port)
Greg Clayton8b82f082011-04-12 05:54:46 +0000686{
Todd Fiala015d8182014-07-22 23:41:36 +0000687 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
688 if (log)
689 log->Printf ("GDBRemoteCommunication::%s(hostname=%s, in_port=%" PRIu16 ", out_port=%" PRIu16, __FUNCTION__, hostname ? hostname : "<empty>", in_port, out_port);
690
Greg Claytonfda4fab2014-01-10 22:24:11 +0000691 out_port = in_port;
Greg Clayton8b82f082011-04-12 05:54:46 +0000692 Error error;
693 // If we locate debugserver, keep that located version around
694 static FileSpec g_debugserver_file_spec;
695
Greg Clayton8b82f082011-04-12 05:54:46 +0000696 char debugserver_path[PATH_MAX];
697 FileSpec &debugserver_file_spec = launch_info.GetExecutableFile();
698
699 // Always check to see if we have an environment override for the path
700 // to the debugserver to use and use it if we do.
701 const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH");
702 if (env_debugserver_path)
Todd Fiala015d8182014-07-22 23:41:36 +0000703 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000704 debugserver_file_spec.SetFile (env_debugserver_path, false);
Todd Fiala015d8182014-07-22 23:41:36 +0000705 if (log)
706 log->Printf ("GDBRemoteCommunication::%s() gdb-remote stub exe path set from environment variable: %s", __FUNCTION__, env_debugserver_path);
707 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000708 else
709 debugserver_file_spec = g_debugserver_file_spec;
710 bool debugserver_exists = debugserver_file_spec.Exists();
711 if (!debugserver_exists)
712 {
713 // The debugserver binary is in the LLDB.framework/Resources
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000714 // directory.
715 if (HostInfo::GetLLDBPath(ePathTypeSupportExecutableDir, debugserver_file_spec))
Greg Clayton8b82f082011-04-12 05:54:46 +0000716 {
Jason Molenda6fd86772014-08-21 23:22:33 +0000717 debugserver_file_spec.AppendPathComponent (DEBUGSERVER_BASENAME);
Greg Clayton8b82f082011-04-12 05:54:46 +0000718 debugserver_exists = debugserver_file_spec.Exists();
719 if (debugserver_exists)
720 {
Todd Fiala015d8182014-07-22 23:41:36 +0000721 if (log)
722 log->Printf ("GDBRemoteCommunication::%s() found gdb-remote stub exe '%s'", __FUNCTION__, debugserver_file_spec.GetPath ().c_str ());
723
Greg Clayton8b82f082011-04-12 05:54:46 +0000724 g_debugserver_file_spec = debugserver_file_spec;
725 }
726 else
727 {
Todd Fiala015d8182014-07-22 23:41:36 +0000728 if (log)
729 log->Printf ("GDBRemoteCommunication::%s() could not find gdb-remote stub exe '%s'", __FUNCTION__, debugserver_file_spec.GetPath ().c_str ());
730
Greg Clayton8b82f082011-04-12 05:54:46 +0000731 g_debugserver_file_spec.Clear();
732 debugserver_file_spec.Clear();
733 }
734 }
735 }
736
737 if (debugserver_exists)
738 {
739 debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path));
740
741 Args &debugserver_args = launch_info.GetArguments();
742 debugserver_args.Clear();
743 char arg_cstr[PATH_MAX];
744
745 // Start args with "debugserver /file/path -r --"
746 debugserver_args.AppendArgument(debugserver_path);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000747
748 // If a host and port is supplied then use it
Greg Claytonfda4fab2014-01-10 22:24:11 +0000749 char host_and_port[128];
750 if (hostname)
751 {
752 snprintf (host_and_port, sizeof(host_and_port), "%s:%u", hostname, in_port);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000753 debugserver_args.AppendArgument(host_and_port);
Greg Claytonfda4fab2014-01-10 22:24:11 +0000754 }
755 else
756 {
757 host_and_port[0] = '\0';
758 }
759
Greg Clayton8b82f082011-04-12 05:54:46 +0000760 // use native registers, not the GDB registers
Oleksiy Vyalovf8ce61c2015-01-28 17:36:59 +0000761 debugserver_args.AppendArgument("--native-regs");
762
763 if (launch_info.GetLaunchInSeparateProcessGroup())
764 {
765 debugserver_args.AppendArgument("--setsid");
766 }
Greg Clayton91a9b2472013-12-04 19:19:12 +0000767
Oleksiy Vyalov4536c452015-02-05 16:29:12 +0000768 llvm::SmallString<PATH_MAX> named_pipe_path;
Oleksiy Vyalovd5f8b6a2015-01-13 23:19:40 +0000769 Pipe port_named_pipe;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000770
Greg Claytonfda4fab2014-01-10 22:24:11 +0000771 bool listen = false;
772 if (host_and_port[0])
Greg Clayton91a9b2472013-12-04 19:19:12 +0000773 {
Greg Clayton00fe87b2013-12-05 22:58:22 +0000774 // Create a temporary file to get the stdout/stderr and redirect the
775 // output of the command into this file. We will later read this file
776 // if all goes well and fill the data into "command_output_ptr"
Greg Clayton00fe87b2013-12-05 22:58:22 +0000777
Greg Claytonfda4fab2014-01-10 22:24:11 +0000778 if (in_port == 0)
Greg Clayton00fe87b2013-12-05 22:58:22 +0000779 {
Greg Claytonfda4fab2014-01-10 22:24:11 +0000780 // Binding to port zero, we need to figure out what port it ends up
781 // using using a named pipe...
Oleksiy Vyalov4536c452015-02-05 16:29:12 +0000782 error = port_named_pipe.CreateWithUniqueName("debugserver-named-pipe", false, named_pipe_path);
783 if (error.Fail())
784 return error;
785 debugserver_args.AppendArgument("--named-pipe");
786 debugserver_args.AppendArgument(named_pipe_path.c_str());
Greg Clayton91a9b2472013-12-04 19:19:12 +0000787 }
788 else
Greg Claytonfda4fab2014-01-10 22:24:11 +0000789 {
790 listen = true;
791 }
Greg Clayton91a9b2472013-12-04 19:19:12 +0000792 }
793 else
Greg Clayton00fe87b2013-12-05 22:58:22 +0000794 {
Greg Clayton00fe87b2013-12-05 22:58:22 +0000795 // No host and port given, so lets listen on our end and make the debugserver
796 // connect to us..
Greg Clayton16810922014-02-27 19:38:18 +0000797 error = StartListenThread ("127.0.0.1", 0);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000798 if (error.Fail())
799 return error;
Greg Clayton8b82f082011-04-12 05:54:46 +0000800
Greg Clayton00fe87b2013-12-05 22:58:22 +0000801 ConnectionFileDescriptor *connection = (ConnectionFileDescriptor *)GetConnection ();
Greg Clayton16810922014-02-27 19:38:18 +0000802 // Wait for 10 seconds to resolve the bound port
Zachary Turner98688922014-08-06 18:16:26 +0000803 out_port = connection->GetListeningPort(10);
Greg Clayton16810922014-02-27 19:38:18 +0000804 if (out_port > 0)
805 {
806 char port_cstr[32];
807 snprintf(port_cstr, sizeof(port_cstr), "127.0.0.1:%i", out_port);
808 // Send the host and port down that debugserver and specify an option
809 // so that it connects back to the port we are listening to in this process
810 debugserver_args.AppendArgument("--reverse-connect");
811 debugserver_args.AppendArgument(port_cstr);
812 }
813 else
814 {
815 error.SetErrorString ("failed to bind to port 0 on 127.0.0.1");
816 return error;
817 }
Greg Clayton00fe87b2013-12-05 22:58:22 +0000818 }
Greg Clayton00fe87b2013-12-05 22:58:22 +0000819
Greg Clayton8b82f082011-04-12 05:54:46 +0000820 const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE");
821 if (env_debugserver_log_file)
822 {
823 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file);
824 debugserver_args.AppendArgument(arg_cstr);
825 }
826
827 const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS");
828 if (env_debugserver_log_flags)
829 {
830 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags);
831 debugserver_args.AppendArgument(arg_cstr);
832 }
Todd Fiala34ba4262014-08-29 17:10:31 +0000833
834 // Add additional args, starting with LLDB_DEBUGSERVER_EXTRA_ARG_1 until an env var doesn't come back.
835 uint32_t env_var_index = 1;
836 bool has_env_var;
837 do
838 {
839 char env_var_name[64];
840 snprintf (env_var_name, sizeof (env_var_name), "LLDB_DEBUGSERVER_EXTRA_ARG_%" PRIu32, env_var_index++);
841 const char *extra_arg = getenv(env_var_name);
842 has_env_var = extra_arg != nullptr;
843
844 if (has_env_var)
845 {
846 debugserver_args.AppendArgument (extra_arg);
847 if (log)
848 log->Printf ("GDBRemoteCommunication::%s adding env var %s contents to stub command line (%s)", __FUNCTION__, env_var_name, extra_arg);
849 }
850 } while (has_env_var);
851
Shawn Best629680e2014-11-05 00:58:55 +0000852 // Close STDIN, STDOUT and STDERR.
Greg Clayton91a9b2472013-12-04 19:19:12 +0000853 launch_info.AppendCloseFileAction (STDIN_FILENO);
854 launch_info.AppendCloseFileAction (STDOUT_FILENO);
855 launch_info.AppendCloseFileAction (STDERR_FILENO);
Shawn Best629680e2014-11-05 00:58:55 +0000856
857 // Redirect STDIN, STDOUT and STDERR to "/dev/null".
858 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
859 launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
860 launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
Zachary Turner9b693272014-12-04 22:06:42 +0000861
Greg Clayton8b82f082011-04-12 05:54:46 +0000862 error = Host::LaunchProcess(launch_info);
Zachary Turner9b693272014-12-04 22:06:42 +0000863
Greg Clayton3121fde2014-02-28 20:47:08 +0000864 if (error.Success() && launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
Greg Clayton91a9b2472013-12-04 19:19:12 +0000865 {
Oleksiy Vyalov4536c452015-02-05 16:29:12 +0000866 if (named_pipe_path.size() > 0)
Greg Clayton91a9b2472013-12-04 19:19:12 +0000867 {
Oleksiy Vyalov47718292015-01-14 01:31:27 +0000868 error = port_named_pipe.OpenAsReader(named_pipe_path, false);
Greg Clayton3121fde2014-02-28 20:47:08 +0000869 if (error.Success())
870 {
Zachary Turner9b693272014-12-04 22:06:42 +0000871 char port_cstr[256];
872 port_cstr[0] = '\0';
873 size_t num_bytes = sizeof(port_cstr);
Oleksiy Vyalovd5f8b6a2015-01-13 23:19:40 +0000874 // Read port from pipe with 10 second timeout.
Oleksiy Vyalov47718292015-01-14 01:31:27 +0000875 error = port_named_pipe.ReadWithTimeout(port_cstr, num_bytes, std::chrono::microseconds(10 * 1000000), num_bytes);
Oleksiy Vyalovd5f8b6a2015-01-13 23:19:40 +0000876 if (error.Success())
877 {
878 assert (num_bytes > 0 && port_cstr[num_bytes-1] == '\0');
Vince Harron5275aaa2015-01-15 20:08:35 +0000879 out_port = StringConvert::ToUInt32(port_cstr, 0);
Oleksiy Vyalovd5f8b6a2015-01-13 23:19:40 +0000880 if (log)
881 log->Printf("GDBRemoteCommunication::%s() debugserver listens %u port", __FUNCTION__, out_port);
882 }
883 else
884 {
885 if (log)
Oleksiy Vyalov4536c452015-02-05 16:29:12 +0000886 log->Printf("GDBRemoteCommunication::%s() failed to read a port value from named pipe %s: %s", __FUNCTION__, named_pipe_path.c_str(), error.AsCString());
Oleksiy Vyalovd5f8b6a2015-01-13 23:19:40 +0000887
888 }
889 port_named_pipe.Close();
Greg Clayton3121fde2014-02-28 20:47:08 +0000890 }
Oleksiy Vyalovd5f8b6a2015-01-13 23:19:40 +0000891 else
892 {
893 if (log)
Oleksiy Vyalov4536c452015-02-05 16:29:12 +0000894 log->Printf("GDBRemoteCommunication::%s() failed to open named pipe %s for reading: %s", __FUNCTION__, named_pipe_path.c_str(), error.AsCString());
Oleksiy Vyalovd5f8b6a2015-01-13 23:19:40 +0000895 }
896 const auto err = port_named_pipe.Delete(named_pipe_path);
897 if (err.Fail())
898 {
899 if (log)
Oleksiy Vyalov4536c452015-02-05 16:29:12 +0000900 log->Printf ("GDBRemoteCommunication::%s failed to delete pipe %s: %s", __FUNCTION__, named_pipe_path.c_str(), err.AsCString());
Oleksiy Vyalovd5f8b6a2015-01-13 23:19:40 +0000901 }
Greg Clayton91a9b2472013-12-04 19:19:12 +0000902 }
Greg Clayton3121fde2014-02-28 20:47:08 +0000903 else if (listen)
904 {
905
906 }
907 else
908 {
909 // Make sure we actually connect with the debugserver...
910 JoinListenThread();
911 }
Greg Clayton00fe87b2013-12-05 22:58:22 +0000912 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000913 }
914 else
915 {
Greg Clayton86edbf42011-10-26 00:56:27 +0000916 error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME );
Greg Clayton8b82f082011-04-12 05:54:46 +0000917 }
918 return error;
919}
920
Greg Claytonc1422c12012-04-09 22:46:21 +0000921void
Greg Claytond451c1a2012-04-13 21:24:18 +0000922GDBRemoteCommunication::DumpHistory(Stream &strm)
Greg Claytonc1422c12012-04-09 22:46:21 +0000923{
Greg Claytond451c1a2012-04-13 21:24:18 +0000924 m_history.Dump (strm);
Greg Claytonc1422c12012-04-09 22:46:21 +0000925}