blob: 4f8628a1f377c5c3b0f90f245f645a10455d99ea [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Communication.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// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
14#include "lldb/lldb-private-log.h"
15#include "lldb/Core/Communication.h"
16#include "lldb/Core/Connection.h"
17#include "lldb/Core/Log.h"
18#include "lldb/Core/Timer.h"
19#include "lldb/Core/Event.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000020#include "lldb/Host/Host.h"
Zachary Turner39de3112014-09-09 20:54:56 +000021#include "lldb/Host/HostThread.h"
22#include "lldb/Host/ThreadLauncher.h"
Eli Friedman88966972010-06-09 08:50:27 +000023#include <string.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024
25using namespace lldb;
26using namespace lldb_private;
27
Jim Ingham4bddaeb2012-02-16 06:50:00 +000028ConstString &
29Communication::GetStaticBroadcasterClass ()
30{
31 static ConstString class_name ("lldb.communication");
32 return class_name;
33}
34
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035//----------------------------------------------------------------------
36// Constructor
37//----------------------------------------------------------------------
Greg Claytond46c87a2010-12-04 02:39:47 +000038Communication::Communication(const char *name) :
Jim Ingham4bddaeb2012-02-16 06:50:00 +000039 Broadcaster (NULL, name),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000040 m_connection_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041 m_read_thread_enabled (false),
42 m_bytes(),
43 m_bytes_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton7ec3d402011-01-27 09:02:32 +000044 m_write_mutex (Mutex::eMutexTypeNormal),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045 m_callback (NULL),
Caroline Tice82305fc2010-12-02 18:31:56 +000046 m_callback_baton (NULL),
Greg Claytond46c87a2010-12-04 02:39:47 +000047 m_close_on_eof (true)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048
49{
50 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_COMMUNICATION,
51 "%p Communication::Communication (name = %s)",
52 this, name);
Greg Clayton95bf0fd2011-04-01 00:29:43 +000053
54 SetEventName (eBroadcastBitDisconnected, "disconnected");
55 SetEventName (eBroadcastBitReadThreadGotBytes, "got bytes");
56 SetEventName (eBroadcastBitReadThreadDidExit, "read thread did exit");
57 SetEventName (eBroadcastBitReadThreadShouldExit, "read thread should exit");
58 SetEventName (eBroadcastBitPacketAvailable, "packet available");
Jim Ingham4bddaeb2012-02-16 06:50:00 +000059
60 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061}
62
63//----------------------------------------------------------------------
64// Destructor
65//----------------------------------------------------------------------
66Communication::~Communication()
67{
68 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_COMMUNICATION,
69 "%p Communication::~Communication (name = %s)",
70 this, m_broadcaster_name.AsCString(""));
71 Clear();
72}
73
74void
75Communication::Clear()
76{
Greg Clayton756f8ae2011-08-19 23:28:37 +000077 SetReadThreadBytesReceivedCallback (NULL, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078 Disconnect (NULL);
Greg Clayton74d41932012-01-31 04:56:17 +000079 StopReadThread (NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080}
81
82ConnectionStatus
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083Communication::Connect (const char *url, Error *error_ptr)
84{
85 Clear();
86
87 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::Connect (url = %s)", this, url);
88
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000089 lldb::ConnectionSP connection_sp (m_connection_sp);
90 if (connection_sp.get())
91 return connection_sp->Connect (url, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092 if (error_ptr)
93 error_ptr->SetErrorString("Invalid connection.");
94 return eConnectionStatusNoConnection;
95}
96
97ConnectionStatus
98Communication::Disconnect (Error *error_ptr)
99{
100 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::Disconnect ()", this);
101
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000102 lldb::ConnectionSP connection_sp (m_connection_sp);
103 if (connection_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000105 ConnectionStatus status = connection_sp->Disconnect (error_ptr);
106 // We currently don't protect connection_sp with any mutex for
Greg Claytonbfae66a2010-12-12 21:50:57 +0000107 // multi-threaded environments. So lets not nuke our connection class
108 // without putting some multi-threaded protections in. We also probably
109 // don't want to pay for the overhead it might cause if every time we
110 // access the connection we have to take a lock.
111 //
Greg Claytone01e07b2013-04-18 18:10:51 +0000112 // This unique pointer will cleanup after itself when this object goes away,
Greg Claytonbfae66a2010-12-12 21:50:57 +0000113 // so there is no need to currently have it destroy itself immediately
114 // upon disconnnect.
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000115 //connection_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116 return status;
117 }
118 return eConnectionStatusNoConnection;
119}
120
121bool
122Communication::IsConnected () const
123{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000124 lldb::ConnectionSP connection_sp (m_connection_sp);
125 if (connection_sp.get())
126 return connection_sp->IsConnected ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127 return false;
128}
129
130bool
131Communication::HasConnection () const
132{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000133 return m_connection_sp.get() != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134}
135
136size_t
137Communication::Read (void *dst, size_t dst_len, uint32_t timeout_usec, ConnectionStatus &status, Error *error_ptr)
138{
139 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
Daniel Malead01b2952012-11-29 21:49:15 +0000140 "%p Communication::Read (dst = %p, dst_len = %" PRIu64 ", timeout = %u usec) connection = %p",
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000141 this,
142 dst,
Greg Clayton43e0af02012-09-18 18:04:04 +0000143 (uint64_t)dst_len,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000144 timeout_usec,
145 m_connection_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146
Stephen Wilsona08cfb12011-01-12 04:22:54 +0000147 if (m_read_thread_enabled)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148 {
149 // We have a dedicated read thread that is getting data for us
150 size_t cached_bytes = GetCachedBytes (dst, dst_len);
151 if (cached_bytes > 0 || timeout_usec == 0)
152 {
153 status = eConnectionStatusSuccess;
154 return cached_bytes;
155 }
156
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000157 if (m_connection_sp.get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158 {
159 if (error_ptr)
160 error_ptr->SetErrorString("Invalid connection.");
161 status = eConnectionStatusNoConnection;
162 return 0;
163 }
164 // Set the timeout appropriately
165 TimeValue timeout_time;
166 if (timeout_usec != UINT32_MAX)
167 {
168 timeout_time = TimeValue::Now();
169 timeout_time.OffsetWithMicroSeconds (timeout_usec);
170 }
171
172 Listener listener ("Communication::Read");
173 listener.StartListeningForEvents (this, eBroadcastBitReadThreadGotBytes | eBroadcastBitReadThreadDidExit);
174 EventSP event_sp;
175 while (listener.WaitForEvent (timeout_time.IsValid() ? &timeout_time : NULL, event_sp))
176 {
177 const uint32_t event_type = event_sp->GetType();
178 if (event_type & eBroadcastBitReadThreadGotBytes)
179 {
180 return GetCachedBytes (dst, dst_len);
181 }
182
183 if (event_type & eBroadcastBitReadThreadDidExit)
184 {
185 Disconnect (NULL);
186 break;
187 }
188 }
189 return 0;
190 }
191
192 // We aren't using a read thread, just read the data synchronously in this
193 // thread.
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000194 lldb::ConnectionSP connection_sp (m_connection_sp);
195 if (connection_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000197 return connection_sp->Read (dst, dst_len, timeout_usec, status, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198 }
199
200 if (error_ptr)
201 error_ptr->SetErrorString("Invalid connection.");
202 status = eConnectionStatusNoConnection;
203 return 0;
204}
205
206
207size_t
208Communication::Write (const void *src, size_t src_len, ConnectionStatus &status, Error *error_ptr)
209{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000210 lldb::ConnectionSP connection_sp (m_connection_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211
Jason Molendaccd41e52012-10-04 22:47:07 +0000212 Mutex::Locker locker(m_write_mutex);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000213 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
Daniel Malead01b2952012-11-29 21:49:15 +0000214 "%p Communication::Write (src = %p, src_len = %" PRIu64 ") connection = %p",
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000215 this,
216 src,
Greg Clayton43e0af02012-09-18 18:04:04 +0000217 (uint64_t)src_len,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000218 connection_sp.get());
219
220 if (connection_sp.get())
221 return connection_sp->Write (src, src_len, status, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000222
223 if (error_ptr)
224 error_ptr->SetErrorString("Invalid connection.");
225 status = eConnectionStatusNoConnection;
226 return 0;
227}
228
229
230bool
231Communication::StartReadThread (Error *error_ptr)
232{
Greg Clayton1cb64962011-03-24 04:28:38 +0000233 if (error_ptr)
234 error_ptr->Clear();
235
Zachary Turner39de3112014-09-09 20:54:56 +0000236 if (m_read_thread.GetState() == eThreadStateRunning)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237 return true;
238
239 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
240 "%p Communication::StartReadThread ()", this);
241
242
243 char thread_name[1024];
244 snprintf(thread_name, sizeof(thread_name), "<lldb.comm.%s>", m_broadcaster_name.AsCString());
245
Greg Clayton86c3f342010-09-15 05:19:45 +0000246 m_read_thread_enabled = true;
Zachary Turner39de3112014-09-09 20:54:56 +0000247 m_read_thread = ThreadLauncher::LaunchThread(thread_name, Communication::ReadThread, this, error_ptr);
248 if (m_read_thread.GetState() != eThreadStateRunning)
Greg Clayton86c3f342010-09-15 05:19:45 +0000249 m_read_thread_enabled = false;
Greg Clayton26661bc2010-07-23 15:43:25 +0000250 return m_read_thread_enabled;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251}
252
253bool
254Communication::StopReadThread (Error *error_ptr)
255{
Zachary Turner39de3112014-09-09 20:54:56 +0000256 if (m_read_thread.GetState() != eThreadStateRunning)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257 return true;
258
259 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
260 "%p Communication::StopReadThread ()", this);
261
262 m_read_thread_enabled = false;
263
264 BroadcastEvent (eBroadcastBitReadThreadShouldExit, NULL);
265
Zachary Turner39de3112014-09-09 20:54:56 +0000266 // error = m_read_thread.Cancel();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267
Zachary Turner39de3112014-09-09 20:54:56 +0000268 Error error = m_read_thread.Join(nullptr);
269 m_read_thread.Reset();
270 return error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271}
272
Greg Clayton59042602014-01-30 18:52:57 +0000273bool
274Communication::JoinReadThread (Error *error_ptr)
275{
Zachary Turner39de3112014-09-09 20:54:56 +0000276 if (m_read_thread.GetState() != eThreadStateRunning)
Greg Clayton59042602014-01-30 18:52:57 +0000277 return true;
278
Zachary Turner39de3112014-09-09 20:54:56 +0000279 Error error = m_read_thread.Join(nullptr);
280 m_read_thread.Reset();
281 return error.Success();
Greg Clayton59042602014-01-30 18:52:57 +0000282}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283
284size_t
285Communication::GetCachedBytes (void *dst, size_t dst_len)
286{
287 Mutex::Locker locker(m_bytes_mutex);
288 if (m_bytes.size() > 0)
289 {
290 // If DST is NULL and we have a thread, then return the number
291 // of bytes that are available so the caller can call again
292 if (dst == NULL)
293 return m_bytes.size();
294
295 const size_t len = std::min<size_t>(dst_len, m_bytes.size());
296
Greg Clayton471b31c2010-07-20 22:52:08 +0000297 ::memcpy (dst, m_bytes.c_str(), len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298 m_bytes.erase(m_bytes.begin(), m_bytes.begin() + len);
299
300 return len;
301 }
302 return 0;
303}
304
305void
Caroline Ticeefed6132010-11-19 20:47:54 +0000306Communication::AppendBytesToCache (const uint8_t * bytes, size_t len, bool broadcast, ConnectionStatus status)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307{
308 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
Daniel Malead01b2952012-11-29 21:49:15 +0000309 "%p Communication::AppendBytesToCache (src = %p, src_len = %" PRIu64 ", broadcast = %i)",
Greg Clayton43e0af02012-09-18 18:04:04 +0000310 this, bytes, (uint64_t)len, broadcast);
Caroline Tice9fd58502011-02-03 20:02:43 +0000311 if ((bytes == NULL || len == 0)
312 && (status != lldb::eConnectionStatusEndOfFile))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313 return;
314 if (m_callback)
315 {
316 // If the user registered a callback, then call it and do not broadcast
317 m_callback (m_callback_baton, bytes, len);
318 }
Greg Clayton756f8ae2011-08-19 23:28:37 +0000319 else if (bytes != NULL && len > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320 {
321 Mutex::Locker locker(m_bytes_mutex);
322 m_bytes.append ((const char *)bytes, len);
323 if (broadcast)
324 BroadcastEventIfUnique (eBroadcastBitReadThreadGotBytes);
325 }
326}
327
328size_t
Greg Clayton73bf5db2011-06-17 01:22:15 +0000329Communication::ReadFromConnection (void *dst,
330 size_t dst_len,
331 uint32_t timeout_usec,
332 ConnectionStatus &status,
333 Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000335 lldb::ConnectionSP connection_sp (m_connection_sp);
336 if (connection_sp.get())
Greg Clayton73bf5db2011-06-17 01:22:15 +0000337 return connection_sp->Read (dst, dst_len, timeout_usec, status, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000338 return 0;
339}
340
Caroline Tice82305fc2010-12-02 18:31:56 +0000341bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342Communication::ReadThreadIsRunning ()
343{
Stephen Wilsona08cfb12011-01-12 04:22:54 +0000344 return m_read_thread_enabled;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345}
346
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000347lldb::thread_result_t
348Communication::ReadThread (lldb::thread_arg_t p)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349{
350 Communication *comm = (Communication *)p;
351
Greg Clayton5160ce52013-03-27 23:08:40 +0000352 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMUNICATION));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353
354 if (log)
355 log->Printf ("%p Communication::ReadThread () thread starting...", p);
356
357 uint8_t buf[1024];
358
359 Error error;
360 ConnectionStatus status = eConnectionStatusSuccess;
361 bool done = false;
362 while (!done && comm->m_read_thread_enabled)
363 {
Peter Collingbourneba23ca02011-06-18 23:52:14 +0000364 size_t bytes_read = comm->ReadFromConnection (buf, sizeof(buf), 5 * TimeValue::MicroSecPerSec, status, &error);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000365 if (bytes_read > 0)
366 comm->AppendBytesToCache (buf, bytes_read, true, status);
367 else if ((bytes_read == 0)
368 && status == eConnectionStatusEndOfFile)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000370 if (comm->GetCloseOnEOF ())
371 comm->Disconnect ();
372 comm->AppendBytesToCache (buf, bytes_read, true, status);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373 }
374
375 switch (status)
376 {
377 case eConnectionStatusSuccess:
378 break;
379
Greg Clayton7788e5f2010-12-04 02:22:36 +0000380 case eConnectionStatusEndOfFile:
Caroline Tice9fd58502011-02-03 20:02:43 +0000381 if (comm->GetCloseOnEOF())
382 done = true;
383 break;
Todd Fiala42628282014-08-21 17:16:26 +0000384 case eConnectionStatusError: // Check GetError() for details
385 if (error.GetType() == eErrorTypePOSIX && error.GetError() == EIO)
386 {
387 // EIO on a pipe is usually caused by remote shutdown
388 comm->Disconnect ();
389 done = true;
390 }
391 if (log)
392 error.LogIfError (log,
393 "%p Communication::ReadFromConnection () => status = %s",
394 p,
395 Communication::ConnectionStatusAsCString (status));
396 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397 case eConnectionStatusNoConnection: // No connection
398 case eConnectionStatusLostConnection: // Lost connection while connected to a valid connection
Greg Claytonf0066ad2014-05-02 00:45:31 +0000399 case eConnectionStatusInterrupted: // Interrupted
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400 done = true;
401 // Fall through...
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402 case eConnectionStatusTimedOut: // Request timed out
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000403 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +0000404 error.LogIfError (log,
405 "%p Communication::ReadFromConnection () => status = %s",
406 p,
407 Communication::ConnectionStatusAsCString (status));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408 break;
409 }
410 }
Caroline Tice20ad3c42010-10-29 21:48:37 +0000411 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMUNICATION);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412 if (log)
413 log->Printf ("%p Communication::ReadThread () thread exiting...", p);
414
415 // Let clients know that this thread is exiting
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416 comm->BroadcastEvent (eBroadcastBitReadThreadDidExit);
417 return NULL;
418}
419
420void
421Communication::SetReadThreadBytesReceivedCallback
422(
423 ReadThreadBytesReceived callback,
424 void *callback_baton
425)
426{
427 m_callback = callback;
428 m_callback_baton = callback_baton;
429}
430
431void
432Communication::SetConnection (Connection *connection)
433{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434 Disconnect (NULL);
Greg Clayton74d41932012-01-31 04:56:17 +0000435 StopReadThread(NULL);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000436 m_connection_sp.reset(connection);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437}
Caroline Ticeceb6b132010-10-26 03:11:13 +0000438
439const char *
440Communication::ConnectionStatusAsCString (lldb::ConnectionStatus status)
441{
442 switch (status)
443 {
444 case eConnectionStatusSuccess: return "success";
445 case eConnectionStatusError: return "error";
446 case eConnectionStatusTimedOut: return "timed out";
447 case eConnectionStatusNoConnection: return "no connection";
448 case eConnectionStatusLostConnection: return "lost connection";
Greg Clayton7a5388b2011-03-20 04:57:14 +0000449 case eConnectionStatusEndOfFile: return "end of file";
Greg Claytonf0066ad2014-05-02 00:45:31 +0000450 case eConnectionStatusInterrupted: return "interrupted";
Caroline Ticeceb6b132010-10-26 03:11:13 +0000451 }
452
453 static char unknown_state_string[64];
454 snprintf(unknown_state_string, sizeof (unknown_state_string), "ConnectionStatus = %i", status);
455 return unknown_state_string;
456}