blob: ecf581b2fabcf9cd4b8adc8ae807270ecb9e946f [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
Tamas Berghammerc2c3d712015-02-18 15:39:41 +000041# define DEBUGSERVER_BASENAME "lldb-server"
Todd Fiala015d8182014-07-22 23:41:36 +000042#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,
Tamas Berghammere13c2732015-02-11 10:29:30 +0000145 const char *listener_name) :
Greg Clayton576d8832011-03-22 04:00:09 +0000146 Communication(comm_name),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000147#ifdef LLDB_CONFIGURATION_DEBUG
148 m_packet_timeout (1000),
149#else
Greg Claytonf3dd93c2011-06-17 03:31:01 +0000150 m_packet_timeout (1),
Daniel Maleae0f8f572013-08-26 23:57:52 +0000151#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152 m_sequence_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton4dc72282011-01-20 07:53:45 +0000153 m_public_is_running (false),
Greg Clayton1cb64962011-03-24 04:28:38 +0000154 m_private_is_running (false),
Greg Claytonc1422c12012-04-09 22:46:21 +0000155 m_history (512),
Greg Clayton8b82f082011-04-12 05:54:46 +0000156 m_send_acks (true),
Greg Clayton00fe87b2013-12-05 22:58:22 +0000157 m_listen_url ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159}
160
161//----------------------------------------------------------------------
162// Destructor
163//----------------------------------------------------------------------
164GDBRemoteCommunication::~GDBRemoteCommunication()
165{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166 if (IsConnected())
167 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168 Disconnect();
169 }
170}
171
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172char
173GDBRemoteCommunication::CalculcateChecksum (const char *payload, size_t payload_length)
174{
175 int checksum = 0;
176
Ed Mastea6b4c772013-08-20 14:12:58 +0000177 for (size_t i = 0; i < payload_length; ++i)
178 checksum += payload[i];
179
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180 return checksum & 255;
181}
182
183size_t
Greg Clayton6ed95942011-01-22 07:12:45 +0000184GDBRemoteCommunication::SendAck ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185{
Greg Clayton5160ce52013-03-27 23:08:40 +0000186 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187 ConnectionStatus status = eConnectionStatusSuccess;
Greg Claytonc1422c12012-04-09 22:46:21 +0000188 char ch = '+';
189 const size_t bytes_written = Write (&ch, 1, status, NULL);
190 if (log)
Greg Clayton45989072013-10-23 18:24:30 +0000191 log->Printf ("<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch);
Greg Claytonc1422c12012-04-09 22:46:21 +0000192 m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written);
193 return bytes_written;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194}
195
196size_t
Greg Clayton6ed95942011-01-22 07:12:45 +0000197GDBRemoteCommunication::SendNack ()
198{
Greg Clayton5160ce52013-03-27 23:08:40 +0000199 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Greg Clayton6ed95942011-01-22 07:12:45 +0000200 ConnectionStatus status = eConnectionStatusSuccess;
Greg Claytonc1422c12012-04-09 22:46:21 +0000201 char ch = '-';
202 const size_t bytes_written = Write (&ch, 1, status, NULL);
203 if (log)
Greg Clayton45989072013-10-23 18:24:30 +0000204 log->Printf("<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch);
Greg Claytonc1422c12012-04-09 22:46:21 +0000205 m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written);
206 return bytes_written;
Greg Clayton32e0a752011-03-30 18:16:51 +0000207}
208
Greg Clayton3dedae12013-12-06 21:45:27 +0000209GDBRemoteCommunication::PacketResult
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000210GDBRemoteCommunication::SendPacket (const char *payload, size_t payload_length)
211{
212 Mutex::Locker locker(m_sequence_mutex);
213 return SendPacketNoLock (payload, payload_length);
214}
215
Greg Clayton3dedae12013-12-06 21:45:27 +0000216GDBRemoteCommunication::PacketResult
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217GDBRemoteCommunication::SendPacketNoLock (const char *payload, size_t payload_length)
218{
219 if (IsConnected())
220 {
221 StreamString packet(0, 4, eByteOrderBig);
222
223 packet.PutChar('$');
224 packet.Write (payload, payload_length);
225 packet.PutChar('#');
226 packet.PutHex8(CalculcateChecksum (payload, payload_length));
227
Greg Clayton5160ce52013-03-27 23:08:40 +0000228 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton7e244322014-09-18 00:17:36 +0000230 const char *packet_data = packet.GetData();
231 const size_t packet_length = packet.GetSize();
232 size_t bytes_written = Write (packet_data, packet_length, status, NULL);
Greg Claytonc1422c12012-04-09 22:46:21 +0000233 if (log)
234 {
Greg Clayton7e244322014-09-18 00:17:36 +0000235 size_t binary_start_offset = 0;
236 if (strncmp(packet_data, "$vFile:pwrite:", strlen("$vFile:pwrite:")) == 0)
237 {
238 const char *first_comma = strchr(packet_data, ',');
239 if (first_comma)
240 {
241 const char *second_comma = strchr(first_comma + 1, ',');
242 if (second_comma)
243 binary_start_offset = second_comma - packet_data + 1;
244 }
245 }
246
Greg Claytonc1422c12012-04-09 22:46:21 +0000247 // If logging was just enabled and we have history, then dump out what
248 // we have to the log so we get the historical context. The Dump() call that
249 // logs all of the packet will set a boolean so that we don't dump this more
250 // than once
251 if (!m_history.DidDumpToLog ())
Greg Clayton5160ce52013-03-27 23:08:40 +0000252 m_history.Dump (log);
Greg Claytonc1422c12012-04-09 22:46:21 +0000253
Greg Clayton7e244322014-09-18 00:17:36 +0000254 if (binary_start_offset)
255 {
256 StreamString strm;
257 // Print non binary data header
258 strm.Printf("<%4" PRIu64 "> send packet: %.*s", (uint64_t)bytes_written, (int)binary_start_offset, packet_data);
259 const uint8_t *p;
260 // Print binary data exactly as sent
261 for (p = (uint8_t*)packet_data + binary_start_offset; *p != '#'; ++p)
262 strm.Printf("\\x%2.2x", *p);
263 // Print the checksum
264 strm.Printf("%*s", (int)3, p);
265 log->PutCString(strm.GetString().c_str());
266 }
267 else
268 log->Printf("<%4" PRIu64 "> send packet: %.*s", (uint64_t)bytes_written, (int)packet_length, packet_data);
Greg Claytonc1422c12012-04-09 22:46:21 +0000269 }
270
Greg Clayton7e244322014-09-18 00:17:36 +0000271 m_history.AddPacket (packet.GetString(), packet_length, History::ePacketTypeSend, bytes_written);
Greg Claytonc1422c12012-04-09 22:46:21 +0000272
273
Greg Clayton7e244322014-09-18 00:17:36 +0000274 if (bytes_written == packet_length)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275 {
Greg Clayton71fc2a32011-02-12 06:28:37 +0000276 if (GetSendAcks ())
Greg Clayton3dedae12013-12-06 21:45:27 +0000277 return GetAck ();
278 else
279 return PacketResult::Success;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280 }
Johnny Chend0c40dd2010-09-14 22:10:43 +0000281 else
282 {
Greg Clayton6d093452011-02-05 02:25:06 +0000283 if (log)
Greg Clayton7e244322014-09-18 00:17:36 +0000284 log->Printf ("error: failed to send packet: %.*s", (int)packet_length, packet_data);
Johnny Chend0c40dd2010-09-14 22:10:43 +0000285 }
Greg Claytonf5e56de2010-09-14 23:36:40 +0000286 }
Greg Clayton3dedae12013-12-06 21:45:27 +0000287 return PacketResult::ErrorSendFailed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288}
289
Greg Clayton3dedae12013-12-06 21:45:27 +0000290GDBRemoteCommunication::PacketResult
Greg Claytonc574ede2011-03-10 02:26:48 +0000291GDBRemoteCommunication::GetAck ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292{
Greg Clayton576d8832011-03-22 04:00:09 +0000293 StringExtractorGDBRemote packet;
Greg Clayton3dedae12013-12-06 21:45:27 +0000294 PacketResult result = WaitForPacketWithTimeoutMicroSecondsNoLock (packet, GetPacketTimeoutInMicroSeconds ());
295 if (result == PacketResult::Success)
296 {
297 if (packet.GetResponseType() == StringExtractorGDBRemote::ResponseType::eAck)
298 return PacketResult::Success;
299 else
300 return PacketResult::ErrorSendAck;
301 }
302 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303}
304
305bool
Jim Ingham4ceb9282012-06-08 22:50:40 +0000306GDBRemoteCommunication::GetSequenceMutex (Mutex::Locker& locker, const char *failure_message)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307{
Greg Claytond9896732012-05-31 16:54:51 +0000308 if (IsRunning())
Jim Ingham4ceb9282012-06-08 22:50:40 +0000309 return locker.TryLock (m_sequence_mutex, failure_message);
Greg Claytond9896732012-05-31 16:54:51 +0000310
311 locker.Lock (m_sequence_mutex);
312 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313}
314
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315
Greg Clayton6779606a2011-01-22 23:43:18 +0000316bool
Greg Clayton6779606a2011-01-22 23:43:18 +0000317GDBRemoteCommunication::WaitForNotRunningPrivate (const TimeValue *timeout_ptr)
318{
319 return m_private_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL);
320}
321
Greg Clayton3dedae12013-12-06 21:45:27 +0000322GDBRemoteCommunication::PacketResult
Greg Clayton73bf5db2011-06-17 01:22:15 +0000323GDBRemoteCommunication::WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &packet, uint32_t timeout_usec)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324{
Greg Clayton73bf5db2011-06-17 01:22:15 +0000325 uint8_t buffer[8192];
326 Error error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327
Greg Clayton5160ce52013-03-27 23:08:40 +0000328 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS | GDBR_LOG_VERBOSE));
Greg Clayton644247c2011-07-07 01:59:51 +0000329
Greg Clayton73bf5db2011-06-17 01:22:15 +0000330 // Check for a packet from our cache first without trying any reading...
331 if (CheckForPacket (NULL, 0, packet))
Greg Clayton3dedae12013-12-06 21:45:27 +0000332 return PacketResult::Success;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333
Greg Clayton0c51ac32011-07-02 23:21:06 +0000334 bool timed_out = false;
Greg Clayton3dedae12013-12-06 21:45:27 +0000335 bool disconnected = false;
Greg Clayton0c51ac32011-07-02 23:21:06 +0000336 while (IsConnected() && !timed_out)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337 {
Johnny Chen74549c82011-07-19 01:13:00 +0000338 lldb::ConnectionStatus status = eConnectionStatusNoConnection;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000339 size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error);
Greg Clayton644247c2011-07-07 01:59:51 +0000340
341 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000342 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 +0000343 __PRETTY_FUNCTION__,
344 timeout_usec,
345 Communication::ConnectionStatusAsCString (status),
346 error.AsCString(),
Greg Clayton43e0af02012-09-18 18:04:04 +0000347 (uint64_t)bytes_read);
Greg Clayton644247c2011-07-07 01:59:51 +0000348
Greg Clayton73bf5db2011-06-17 01:22:15 +0000349 if (bytes_read > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000351 if (CheckForPacket (buffer, bytes_read, packet))
Greg Clayton3dedae12013-12-06 21:45:27 +0000352 return PacketResult::Success;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000353 }
354 else
355 {
356 switch (status)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000357 {
Greg Clayton197bacf2011-07-02 21:07:54 +0000358 case eConnectionStatusTimedOut:
Greg Claytonf0066ad2014-05-02 00:45:31 +0000359 case eConnectionStatusInterrupted:
Greg Clayton0c51ac32011-07-02 23:21:06 +0000360 timed_out = true;
361 break;
362 case eConnectionStatusSuccess:
363 //printf ("status = success but error = %s\n", error.AsCString("<invalid>"));
Greg Clayton73bf5db2011-06-17 01:22:15 +0000364 break;
365
366 case eConnectionStatusEndOfFile:
367 case eConnectionStatusNoConnection:
368 case eConnectionStatusLostConnection:
369 case eConnectionStatusError:
Greg Clayton3dedae12013-12-06 21:45:27 +0000370 disconnected = true;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000371 Disconnect();
372 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373 }
374 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375 }
Greg Clayton3dedae12013-12-06 21:45:27 +0000376 packet.Clear ();
377 if (disconnected)
378 return PacketResult::ErrorDisconnected;
379 if (timed_out)
380 return PacketResult::ErrorReplyTimeout;
381 else
382 return PacketResult::ErrorReplyFailed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383}
384
Greg Clayton73bf5db2011-06-17 01:22:15 +0000385bool
386GDBRemoteCommunication::CheckForPacket (const uint8_t *src, size_t src_len, StringExtractorGDBRemote &packet)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387{
388 // Put the packet data into the buffer in a thread safe fashion
389 Mutex::Locker locker(m_bytes_mutex);
Greg Clayton197bacf2011-07-02 21:07:54 +0000390
Greg Clayton5160ce52013-03-27 23:08:40 +0000391 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS));
Greg Clayton197bacf2011-07-02 21:07:54 +0000392
Greg Clayton73bf5db2011-06-17 01:22:15 +0000393 if (src && src_len > 0)
Greg Clayton197bacf2011-07-02 21:07:54 +0000394 {
Greg Clayton0c51ac32011-07-02 23:21:06 +0000395 if (log && log->GetVerbose())
Greg Clayton197bacf2011-07-02 21:07:54 +0000396 {
397 StreamString s;
Greg Clayton0c51ac32011-07-02 23:21:06 +0000398 log->Printf ("GDBRemoteCommunication::%s adding %u bytes: %.*s",
399 __FUNCTION__,
400 (uint32_t)src_len,
401 (uint32_t)src_len,
402 src);
Greg Clayton197bacf2011-07-02 21:07:54 +0000403 }
Greg Clayton73bf5db2011-06-17 01:22:15 +0000404 m_bytes.append ((const char *)src, src_len);
Greg Clayton197bacf2011-07-02 21:07:54 +0000405 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406
407 // Parse up the packets into gdb remote packets
Greg Clayton197bacf2011-07-02 21:07:54 +0000408 if (!m_bytes.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409 {
410 // end_idx must be one past the last valid packet byte. Start
411 // it off with an invalid value that is the same as the current
412 // index.
Greg Clayton73bf5db2011-06-17 01:22:15 +0000413 size_t content_start = 0;
Greg Clayton118593a2014-11-03 21:02:54 +0000414 size_t content_length = 0;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000415 size_t total_length = 0;
416 size_t checksum_idx = std::string::npos;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417
Greg Clayton118593a2014-11-03 21:02:54 +0000418 switch (m_bytes[0])
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419 {
Greg Clayton118593a2014-11-03 21:02:54 +0000420 case '+': // Look for ack
421 case '-': // Look for cancel
422 case '\x03': // ^C to halt target
423 content_length = total_length = 1; // The command is one byte long...
424 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425
Greg Clayton118593a2014-11-03 21:02:54 +0000426 case '$':
427 // Look for a standard gdb packet?
428 {
429 size_t hash_pos = m_bytes.find('#');
430 if (hash_pos != std::string::npos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431 {
Greg Clayton118593a2014-11-03 21:02:54 +0000432 if (hash_pos + 2 < m_bytes.size())
Greg Clayton73bf5db2011-06-17 01:22:15 +0000433 {
Greg Clayton118593a2014-11-03 21:02:54 +0000434 checksum_idx = hash_pos + 1;
435 // Skip the dollar sign
436 content_start = 1;
437 // Don't include the # in the content or the $ in the content length
438 content_length = hash_pos - 1;
439
440 total_length = hash_pos + 3; // Skip the # and the two hex checksum bytes
441 }
442 else
443 {
444 // Checksum bytes aren't all here yet
445 content_length = std::string::npos;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000446 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447 }
Greg Clayton118593a2014-11-03 21:02:54 +0000448 }
449 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450
Greg Clayton118593a2014-11-03 21:02:54 +0000451 default:
452 {
453 // We have an unexpected byte and we need to flush all bad
454 // data that is in m_bytes, so we need to find the first
455 // byte that is a '+' (ACK), '-' (NACK), \x03 (CTRL+C interrupt),
456 // or '$' character (start of packet header) or of course,
457 // the end of the data in m_bytes...
458 const size_t bytes_len = m_bytes.size();
459 bool done = false;
460 uint32_t idx;
461 for (idx = 1; !done && idx < bytes_len; ++idx)
Greg Clayton197bacf2011-07-02 21:07:54 +0000462 {
Greg Clayton118593a2014-11-03 21:02:54 +0000463 switch (m_bytes[idx])
Greg Clayton197bacf2011-07-02 21:07:54 +0000464 {
Greg Clayton118593a2014-11-03 21:02:54 +0000465 case '+':
466 case '-':
467 case '\x03':
468 case '$':
469 done = true;
470 break;
471
472 default:
473 break;
Greg Clayton197bacf2011-07-02 21:07:54 +0000474 }
475 }
Greg Clayton118593a2014-11-03 21:02:54 +0000476 if (log)
477 log->Printf ("GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'",
478 __FUNCTION__, idx - 1, idx - 1, m_bytes.c_str());
479 m_bytes.erase(0, idx - 1);
480 }
481 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482 }
483
Greg Clayton73bf5db2011-06-17 01:22:15 +0000484 if (content_length == std::string::npos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000486 packet.Clear();
487 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488 }
Greg Claytonf3dd93c2011-06-17 03:31:01 +0000489 else if (total_length > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000491
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492 // We have a valid packet...
Greg Clayton73bf5db2011-06-17 01:22:15 +0000493 assert (content_length <= m_bytes.size());
494 assert (total_length <= m_bytes.size());
495 assert (content_length <= total_length);
Greg Clayton06f09b52014-06-20 20:41:07 +0000496 const size_t content_end = content_start + content_length;
497
Greg Clayton73bf5db2011-06-17 01:22:15 +0000498 bool success = true;
499 std::string &packet_str = packet.GetStringRef();
Greg Claytonc1422c12012-04-09 22:46:21 +0000500
501
502 if (log)
503 {
504 // If logging was just enabled and we have history, then dump out what
505 // we have to the log so we get the historical context. The Dump() call that
506 // logs all of the packet will set a boolean so that we don't dump this more
507 // than once
508 if (!m_history.DidDumpToLog ())
Greg Clayton5160ce52013-03-27 23:08:40 +0000509 m_history.Dump (log);
Greg Claytonc1422c12012-04-09 22:46:21 +0000510
Greg Clayton06f09b52014-06-20 20:41:07 +0000511 bool binary = false;
512 // Only detect binary for packets that start with a '$' and have a '#CC' checksum
513 if (m_bytes[0] == '$' && total_length > 4)
514 {
515 for (size_t i=0; !binary && i<total_length; ++i)
516 {
517 if (isprint(m_bytes[i]) == 0)
518 binary = true;
519 }
520 }
521 if (binary)
522 {
523 StreamString strm;
524 // Packet header...
525 strm.Printf("<%4" PRIu64 "> read packet: %c", (uint64_t)total_length, m_bytes[0]);
526 for (size_t i=content_start; i<content_end; ++i)
527 {
528 // Remove binary escaped bytes when displaying the packet...
529 const char ch = m_bytes[i];
530 if (ch == 0x7d)
531 {
532 // 0x7d is the escape character. The next character is to
533 // be XOR'd with 0x20.
534 const char escapee = m_bytes[++i] ^ 0x20;
535 strm.Printf("%2.2x", escapee);
536 }
537 else
538 {
539 strm.Printf("%2.2x", (uint8_t)ch);
540 }
541 }
542 // Packet footer...
543 strm.Printf("%c%c%c", m_bytes[total_length-3], m_bytes[total_length-2], m_bytes[total_length-1]);
544 log->PutCString(strm.GetString().c_str());
545 }
546 else
547 {
548 log->Printf("<%4" PRIu64 "> read packet: %.*s", (uint64_t)total_length, (int)(total_length), m_bytes.c_str());
549 }
Greg Claytonc1422c12012-04-09 22:46:21 +0000550 }
551
552 m_history.AddPacket (m_bytes.c_str(), total_length, History::ePacketTypeRecv, total_length);
553
Hafiz Abid Qadeere5fd5e12013-08-28 15:10:37 +0000554 // Clear packet_str in case there is some existing data in it.
555 packet_str.clear();
Hafiz Abid Qadeerda96ef22013-08-28 10:31:52 +0000556 // Copy the packet from m_bytes to packet_str expanding the
557 // run-length encoding in the process.
558 // Reserve enough byte for the most common case (no RLE used)
559 packet_str.reserve(m_bytes.length());
Greg Clayton06f09b52014-06-20 20:41:07 +0000560 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 +0000561 {
562 if (*c == '*')
563 {
564 // '*' indicates RLE. Next character will give us the
565 // repeat count and previous character is what is to be
566 // repeated.
567 char char_to_repeat = packet_str.back();
568 // Number of time the previous character is repeated
569 int repeat_count = *++c + 3 - ' ';
570 // We have the char_to_repeat and repeat_count. Now push
571 // it in the packet.
572 for (int i = 0; i < repeat_count; ++i)
573 packet_str.push_back(char_to_repeat);
574 }
Steve Pucci3c5d3332014-02-24 19:07:29 +0000575 else if (*c == 0x7d)
576 {
577 // 0x7d is the escape character. The next character is to
578 // be XOR'd with 0x20.
579 char escapee = *++c ^ 0x20;
580 packet_str.push_back(escapee);
581 }
Hafiz Abid Qadeerda96ef22013-08-28 10:31:52 +0000582 else
583 {
584 packet_str.push_back(*c);
585 }
586 }
587
Greg Clayton73bf5db2011-06-17 01:22:15 +0000588 if (m_bytes[0] == '$')
589 {
590 assert (checksum_idx < m_bytes.size());
591 if (::isxdigit (m_bytes[checksum_idx+0]) ||
592 ::isxdigit (m_bytes[checksum_idx+1]))
593 {
594 if (GetSendAcks ())
595 {
596 const char *packet_checksum_cstr = &m_bytes[checksum_idx];
597 char packet_checksum = strtol (packet_checksum_cstr, NULL, 16);
598 char actual_checksum = CalculcateChecksum (packet_str.c_str(), packet_str.size());
599 success = packet_checksum == actual_checksum;
600 if (!success)
601 {
602 if (log)
603 log->Printf ("error: checksum mismatch: %.*s expected 0x%2.2x, got 0x%2.2x",
604 (int)(total_length),
605 m_bytes.c_str(),
606 (uint8_t)packet_checksum,
607 (uint8_t)actual_checksum);
608 }
609 // Send the ack or nack if needed
610 if (!success)
611 SendNack();
612 else
613 SendAck();
614 }
Greg Clayton73bf5db2011-06-17 01:22:15 +0000615 }
616 else
617 {
618 success = false;
619 if (log)
Jason Molendafd54b362011-09-20 21:44:10 +0000620 log->Printf ("error: invalid checksum in packet: '%s'\n", m_bytes.c_str());
Greg Clayton73bf5db2011-06-17 01:22:15 +0000621 }
622 }
Greg Claytonc1422c12012-04-09 22:46:21 +0000623
Greg Clayton73bf5db2011-06-17 01:22:15 +0000624 m_bytes.erase(0, total_length);
625 packet.SetFilePos(0);
626 return success;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628 }
Greg Clayton73bf5db2011-06-17 01:22:15 +0000629 packet.Clear();
630 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631}
632
Greg Clayton8b82f082011-04-12 05:54:46 +0000633Error
Greg Claytond6299802013-12-06 17:46:35 +0000634GDBRemoteCommunication::StartListenThread (const char *hostname, uint16_t port)
Greg Clayton00fe87b2013-12-05 22:58:22 +0000635{
636 Error error;
Zachary Turneracee96a2014-09-23 18:32:09 +0000637 if (m_listen_thread.IsJoinable())
Greg Clayton00fe87b2013-12-05 22:58:22 +0000638 {
639 error.SetErrorString("listen thread already running");
640 }
641 else
642 {
643 char listen_url[512];
644 if (hostname && hostname[0])
Jean-Daniel Dupas3c6774a2014-02-08 20:29:40 +0000645 snprintf(listen_url, sizeof(listen_url), "listen://%s:%i", hostname, port);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000646 else
647 snprintf(listen_url, sizeof(listen_url), "listen://%i", port);
648 m_listen_url = listen_url;
649 SetConnection(new ConnectionFileDescriptor());
Zachary Turner39de3112014-09-09 20:54:56 +0000650 m_listen_thread = ThreadLauncher::LaunchThread(listen_url, GDBRemoteCommunication::ListenThread, this, &error);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000651 }
652 return error;
653}
654
655bool
656GDBRemoteCommunication::JoinListenThread ()
657{
Zachary Turneracee96a2014-09-23 18:32:09 +0000658 if (m_listen_thread.IsJoinable())
Zachary Turner39de3112014-09-09 20:54:56 +0000659 m_listen_thread.Join(nullptr);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000660 return true;
661}
662
663lldb::thread_result_t
664GDBRemoteCommunication::ListenThread (lldb::thread_arg_t arg)
665{
666 GDBRemoteCommunication *comm = (GDBRemoteCommunication *)arg;
667 Error error;
668 ConnectionFileDescriptor *connection = (ConnectionFileDescriptor *)comm->GetConnection ();
669
670 if (connection)
671 {
672 // Do the listen on another thread so we can continue on...
673 if (connection->Connect(comm->m_listen_url.c_str(), &error) != eConnectionStatusSuccess)
674 comm->SetConnection(NULL);
675 }
676 return NULL;
677}
678
679Error
Greg Claytonfda4fab2014-01-10 22:24:11 +0000680GDBRemoteCommunication::StartDebugserverProcess (const char *hostname,
681 uint16_t in_port,
Greg Clayton91a9b2472013-12-04 19:19:12 +0000682 lldb_private::ProcessLaunchInfo &launch_info,
Greg Claytonfda4fab2014-01-10 22:24:11 +0000683 uint16_t &out_port)
Greg Clayton8b82f082011-04-12 05:54:46 +0000684{
Todd Fiala015d8182014-07-22 23:41:36 +0000685 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS));
686 if (log)
687 log->Printf ("GDBRemoteCommunication::%s(hostname=%s, in_port=%" PRIu16 ", out_port=%" PRIu16, __FUNCTION__, hostname ? hostname : "<empty>", in_port, out_port);
688
Greg Claytonfda4fab2014-01-10 22:24:11 +0000689 out_port = in_port;
Greg Clayton8b82f082011-04-12 05:54:46 +0000690 Error error;
691 // If we locate debugserver, keep that located version around
692 static FileSpec g_debugserver_file_spec;
693
Greg Clayton8b82f082011-04-12 05:54:46 +0000694 char debugserver_path[PATH_MAX];
695 FileSpec &debugserver_file_spec = launch_info.GetExecutableFile();
696
697 // Always check to see if we have an environment override for the path
698 // to the debugserver to use and use it if we do.
699 const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH");
700 if (env_debugserver_path)
Todd Fiala015d8182014-07-22 23:41:36 +0000701 {
Greg Clayton8b82f082011-04-12 05:54:46 +0000702 debugserver_file_spec.SetFile (env_debugserver_path, false);
Todd Fiala015d8182014-07-22 23:41:36 +0000703 if (log)
704 log->Printf ("GDBRemoteCommunication::%s() gdb-remote stub exe path set from environment variable: %s", __FUNCTION__, env_debugserver_path);
705 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000706 else
707 debugserver_file_spec = g_debugserver_file_spec;
708 bool debugserver_exists = debugserver_file_spec.Exists();
709 if (!debugserver_exists)
710 {
711 // The debugserver binary is in the LLDB.framework/Resources
Zachary Turner42ff0ad2014-08-21 17:29:12 +0000712 // directory.
713 if (HostInfo::GetLLDBPath(ePathTypeSupportExecutableDir, debugserver_file_spec))
Greg Clayton8b82f082011-04-12 05:54:46 +0000714 {
Jason Molenda6fd86772014-08-21 23:22:33 +0000715 debugserver_file_spec.AppendPathComponent (DEBUGSERVER_BASENAME);
Greg Clayton8b82f082011-04-12 05:54:46 +0000716 debugserver_exists = debugserver_file_spec.Exists();
717 if (debugserver_exists)
718 {
Todd Fiala015d8182014-07-22 23:41:36 +0000719 if (log)
720 log->Printf ("GDBRemoteCommunication::%s() found gdb-remote stub exe '%s'", __FUNCTION__, debugserver_file_spec.GetPath ().c_str ());
721
Greg Clayton8b82f082011-04-12 05:54:46 +0000722 g_debugserver_file_spec = debugserver_file_spec;
723 }
724 else
725 {
Todd Fiala015d8182014-07-22 23:41:36 +0000726 if (log)
727 log->Printf ("GDBRemoteCommunication::%s() could not find gdb-remote stub exe '%s'", __FUNCTION__, debugserver_file_spec.GetPath ().c_str ());
728
Greg Clayton8b82f082011-04-12 05:54:46 +0000729 g_debugserver_file_spec.Clear();
730 debugserver_file_spec.Clear();
731 }
732 }
733 }
734
735 if (debugserver_exists)
736 {
737 debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path));
738
739 Args &debugserver_args = launch_info.GetArguments();
740 debugserver_args.Clear();
741 char arg_cstr[PATH_MAX];
Tamas Berghammerc2c3d712015-02-18 15:39:41 +0000742
Greg Clayton8b82f082011-04-12 05:54:46 +0000743 // Start args with "debugserver /file/path -r --"
744 debugserver_args.AppendArgument(debugserver_path);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000745
Tamas Berghammerc2c3d712015-02-18 15:39:41 +0000746#if !defined(__APPLE__)
747 // First argument to lldb-server must be mode in which to run.
748 debugserver_args.AppendArgument("gdbserver");
749#endif
750
Greg Clayton00fe87b2013-12-05 22:58:22 +0000751 // If a host and port is supplied then use it
Greg Claytonfda4fab2014-01-10 22:24:11 +0000752 char host_and_port[128];
753 if (hostname)
754 {
755 snprintf (host_and_port, sizeof(host_and_port), "%s:%u", hostname, in_port);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000756 debugserver_args.AppendArgument(host_and_port);
Greg Claytonfda4fab2014-01-10 22:24:11 +0000757 }
758 else
759 {
760 host_and_port[0] = '\0';
761 }
762
Greg Clayton8b82f082011-04-12 05:54:46 +0000763 // use native registers, not the GDB registers
Oleksiy Vyalovf8ce61c2015-01-28 17:36:59 +0000764 debugserver_args.AppendArgument("--native-regs");
765
766 if (launch_info.GetLaunchInSeparateProcessGroup())
767 {
768 debugserver_args.AppendArgument("--setsid");
769 }
Greg Clayton91a9b2472013-12-04 19:19:12 +0000770
Oleksiy Vyalov4536c452015-02-05 16:29:12 +0000771 llvm::SmallString<PATH_MAX> named_pipe_path;
Oleksiy Vyalovd5f8b6a2015-01-13 23:19:40 +0000772 Pipe port_named_pipe;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000773
Greg Claytonfda4fab2014-01-10 22:24:11 +0000774 bool listen = false;
775 if (host_and_port[0])
Greg Clayton91a9b2472013-12-04 19:19:12 +0000776 {
Greg Clayton00fe87b2013-12-05 22:58:22 +0000777 // Create a temporary file to get the stdout/stderr and redirect the
778 // output of the command into this file. We will later read this file
779 // if all goes well and fill the data into "command_output_ptr"
Greg Clayton00fe87b2013-12-05 22:58:22 +0000780
Greg Claytonfda4fab2014-01-10 22:24:11 +0000781 if (in_port == 0)
Greg Clayton00fe87b2013-12-05 22:58:22 +0000782 {
Greg Claytonfda4fab2014-01-10 22:24:11 +0000783 // Binding to port zero, we need to figure out what port it ends up
784 // using using a named pipe...
Oleksiy Vyalov4536c452015-02-05 16:29:12 +0000785 error = port_named_pipe.CreateWithUniqueName("debugserver-named-pipe", false, named_pipe_path);
786 if (error.Fail())
787 return error;
788 debugserver_args.AppendArgument("--named-pipe");
789 debugserver_args.AppendArgument(named_pipe_path.c_str());
Greg Clayton91a9b2472013-12-04 19:19:12 +0000790 }
791 else
Greg Claytonfda4fab2014-01-10 22:24:11 +0000792 {
793 listen = true;
794 }
Greg Clayton91a9b2472013-12-04 19:19:12 +0000795 }
796 else
Greg Clayton00fe87b2013-12-05 22:58:22 +0000797 {
Greg Clayton00fe87b2013-12-05 22:58:22 +0000798 // No host and port given, so lets listen on our end and make the debugserver
799 // connect to us..
Greg Clayton16810922014-02-27 19:38:18 +0000800 error = StartListenThread ("127.0.0.1", 0);
Greg Clayton00fe87b2013-12-05 22:58:22 +0000801 if (error.Fail())
802 return error;
Greg Clayton8b82f082011-04-12 05:54:46 +0000803
Greg Clayton00fe87b2013-12-05 22:58:22 +0000804 ConnectionFileDescriptor *connection = (ConnectionFileDescriptor *)GetConnection ();
Greg Clayton16810922014-02-27 19:38:18 +0000805 // Wait for 10 seconds to resolve the bound port
Zachary Turner98688922014-08-06 18:16:26 +0000806 out_port = connection->GetListeningPort(10);
Greg Clayton16810922014-02-27 19:38:18 +0000807 if (out_port > 0)
808 {
809 char port_cstr[32];
810 snprintf(port_cstr, sizeof(port_cstr), "127.0.0.1:%i", out_port);
811 // Send the host and port down that debugserver and specify an option
812 // so that it connects back to the port we are listening to in this process
813 debugserver_args.AppendArgument("--reverse-connect");
814 debugserver_args.AppendArgument(port_cstr);
815 }
816 else
817 {
818 error.SetErrorString ("failed to bind to port 0 on 127.0.0.1");
819 return error;
820 }
Greg Clayton00fe87b2013-12-05 22:58:22 +0000821 }
Greg Clayton00fe87b2013-12-05 22:58:22 +0000822
Greg Clayton8b82f082011-04-12 05:54:46 +0000823 const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE");
824 if (env_debugserver_log_file)
825 {
826 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file);
827 debugserver_args.AppendArgument(arg_cstr);
828 }
829
830 const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS");
831 if (env_debugserver_log_flags)
832 {
833 ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags);
834 debugserver_args.AppendArgument(arg_cstr);
835 }
Todd Fiala34ba4262014-08-29 17:10:31 +0000836
837 // Add additional args, starting with LLDB_DEBUGSERVER_EXTRA_ARG_1 until an env var doesn't come back.
838 uint32_t env_var_index = 1;
839 bool has_env_var;
840 do
841 {
842 char env_var_name[64];
843 snprintf (env_var_name, sizeof (env_var_name), "LLDB_DEBUGSERVER_EXTRA_ARG_%" PRIu32, env_var_index++);
844 const char *extra_arg = getenv(env_var_name);
845 has_env_var = extra_arg != nullptr;
846
847 if (has_env_var)
848 {
849 debugserver_args.AppendArgument (extra_arg);
850 if (log)
851 log->Printf ("GDBRemoteCommunication::%s adding env var %s contents to stub command line (%s)", __FUNCTION__, env_var_name, extra_arg);
852 }
853 } while (has_env_var);
854
Shawn Best629680e2014-11-05 00:58:55 +0000855 // Close STDIN, STDOUT and STDERR.
Greg Clayton91a9b2472013-12-04 19:19:12 +0000856 launch_info.AppendCloseFileAction (STDIN_FILENO);
857 launch_info.AppendCloseFileAction (STDOUT_FILENO);
858 launch_info.AppendCloseFileAction (STDERR_FILENO);
Shawn Best629680e2014-11-05 00:58:55 +0000859
860 // Redirect STDIN, STDOUT and STDERR to "/dev/null".
861 launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false);
862 launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true);
863 launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true);
Zachary Turner9b693272014-12-04 22:06:42 +0000864
Greg Clayton8b82f082011-04-12 05:54:46 +0000865 error = Host::LaunchProcess(launch_info);
Zachary Turner9b693272014-12-04 22:06:42 +0000866
Greg Clayton3121fde2014-02-28 20:47:08 +0000867 if (error.Success() && launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID)
Greg Clayton91a9b2472013-12-04 19:19:12 +0000868 {
Oleksiy Vyalov4536c452015-02-05 16:29:12 +0000869 if (named_pipe_path.size() > 0)
Greg Clayton91a9b2472013-12-04 19:19:12 +0000870 {
Oleksiy Vyalov47718292015-01-14 01:31:27 +0000871 error = port_named_pipe.OpenAsReader(named_pipe_path, false);
Greg Clayton3121fde2014-02-28 20:47:08 +0000872 if (error.Success())
873 {
Zachary Turner9b693272014-12-04 22:06:42 +0000874 char port_cstr[256];
875 port_cstr[0] = '\0';
876 size_t num_bytes = sizeof(port_cstr);
Oleksiy Vyalovd5f8b6a2015-01-13 23:19:40 +0000877 // Read port from pipe with 10 second timeout.
Oleksiy Vyalov47718292015-01-14 01:31:27 +0000878 error = port_named_pipe.ReadWithTimeout(port_cstr, num_bytes, std::chrono::microseconds(10 * 1000000), num_bytes);
Oleksiy Vyalovd5f8b6a2015-01-13 23:19:40 +0000879 if (error.Success())
880 {
881 assert (num_bytes > 0 && port_cstr[num_bytes-1] == '\0');
Vince Harron5275aaa2015-01-15 20:08:35 +0000882 out_port = StringConvert::ToUInt32(port_cstr, 0);
Oleksiy Vyalovd5f8b6a2015-01-13 23:19:40 +0000883 if (log)
884 log->Printf("GDBRemoteCommunication::%s() debugserver listens %u port", __FUNCTION__, out_port);
885 }
886 else
887 {
888 if (log)
Oleksiy Vyalov4536c452015-02-05 16:29:12 +0000889 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 +0000890
891 }
892 port_named_pipe.Close();
Greg Clayton3121fde2014-02-28 20:47:08 +0000893 }
Oleksiy Vyalovd5f8b6a2015-01-13 23:19:40 +0000894 else
895 {
896 if (log)
Oleksiy Vyalov4536c452015-02-05 16:29:12 +0000897 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 +0000898 }
899 const auto err = port_named_pipe.Delete(named_pipe_path);
900 if (err.Fail())
901 {
902 if (log)
Oleksiy Vyalov4536c452015-02-05 16:29:12 +0000903 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 +0000904 }
Greg Clayton91a9b2472013-12-04 19:19:12 +0000905 }
Greg Clayton3121fde2014-02-28 20:47:08 +0000906 else if (listen)
907 {
908
909 }
910 else
911 {
912 // Make sure we actually connect with the debugserver...
913 JoinListenThread();
914 }
Greg Clayton00fe87b2013-12-05 22:58:22 +0000915 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000916 }
917 else
918 {
Greg Clayton86edbf42011-10-26 00:56:27 +0000919 error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME );
Greg Clayton8b82f082011-04-12 05:54:46 +0000920 }
921 return error;
922}
923
Greg Claytonc1422c12012-04-09 22:46:21 +0000924void
Greg Claytond451c1a2012-04-13 21:24:18 +0000925GDBRemoteCommunication::DumpHistory(Stream &strm)
Greg Claytonc1422c12012-04-09 22:46:21 +0000926{
Greg Claytond451c1a2012-04-13 21:24:18 +0000927 m_history.Dump (strm);
Greg Claytonc1422c12012-04-09 22:46:21 +0000928}