blob: ab2b293f8c3ae61debbe2b4ee81b88633e0888c7 [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
Eugene Zelenko896ddd02016-03-02 01:09:03 +000012#include <cstring>
13
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014// Other libraries and framework includes
15// Project includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Core/Communication.h"
17#include "lldb/Core/Connection.h"
18#include "lldb/Core/Log.h"
19#include "lldb/Core/Timer.h"
20#include "lldb/Core/Event.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000021#include "lldb/Host/Host.h"
Zachary Turner39de3112014-09-09 20:54:56 +000022#include "lldb/Host/HostThread.h"
23#include "lldb/Host/ThreadLauncher.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
Greg Claytond46c87a2010-12-04 02:39:47 +000035Communication::Communication(const char *name) :
Eugene Zelenko896ddd02016-03-02 01:09:03 +000036 Broadcaster(nullptr, name),
37 m_connection_sp(),
38 m_read_thread_enabled(false),
39 m_read_thread_did_exit(false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040 m_bytes(),
Eugene Zelenko896ddd02016-03-02 01:09:03 +000041 m_bytes_mutex(Mutex::eMutexTypeRecursive),
42 m_write_mutex(Mutex::eMutexTypeNormal),
43 m_synchronize_mutex(Mutex::eMutexTypeNormal),
44 m_callback(nullptr),
45 m_callback_baton(nullptr),
46 m_close_on_eof(true)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047
48{
49 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_COMMUNICATION,
50 "%p Communication::Communication (name = %s)",
51 this, name);
Greg Clayton95bf0fd2011-04-01 00:29:43 +000052
53 SetEventName (eBroadcastBitDisconnected, "disconnected");
54 SetEventName (eBroadcastBitReadThreadGotBytes, "got bytes");
55 SetEventName (eBroadcastBitReadThreadDidExit, "read thread did exit");
56 SetEventName (eBroadcastBitReadThreadShouldExit, "read thread should exit");
57 SetEventName (eBroadcastBitPacketAvailable, "packet available");
Pavel Labath3f5df532015-03-12 10:12:41 +000058 SetEventName (eBroadcastBitNoMorePendingInput, "no more pending input");
Jim Ingham4bddaeb2012-02-16 06:50:00 +000059
60 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061}
62
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063Communication::~Communication()
64{
65 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_COMMUNICATION,
66 "%p Communication::~Communication (name = %s)",
67 this, m_broadcaster_name.AsCString(""));
68 Clear();
69}
70
71void
72Communication::Clear()
73{
Eugene Zelenko896ddd02016-03-02 01:09:03 +000074 SetReadThreadBytesReceivedCallback(nullptr, nullptr);
75 Disconnect(nullptr);
76 StopReadThread(nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077}
78
79ConnectionStatus
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080Communication::Connect (const char *url, Error *error_ptr)
81{
82 Clear();
83
84 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::Connect (url = %s)", this, url);
85
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000086 lldb::ConnectionSP connection_sp (m_connection_sp);
Eugene Zelenko896ddd02016-03-02 01:09:03 +000087 if (connection_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000088 return connection_sp->Connect (url, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000089 if (error_ptr)
90 error_ptr->SetErrorString("Invalid connection.");
91 return eConnectionStatusNoConnection;
92}
93
94ConnectionStatus
95Communication::Disconnect (Error *error_ptr)
96{
97 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::Disconnect ()", this);
98
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000099 lldb::ConnectionSP connection_sp (m_connection_sp);
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000100 if (connection_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000102 ConnectionStatus status = connection_sp->Disconnect (error_ptr);
103 // We currently don't protect connection_sp with any mutex for
Greg Claytonbfae66a2010-12-12 21:50:57 +0000104 // multi-threaded environments. So lets not nuke our connection class
105 // without putting some multi-threaded protections in. We also probably
106 // don't want to pay for the overhead it might cause if every time we
107 // access the connection we have to take a lock.
108 //
Greg Claytone01e07b2013-04-18 18:10:51 +0000109 // This unique pointer will cleanup after itself when this object goes away,
Greg Claytonbfae66a2010-12-12 21:50:57 +0000110 // so there is no need to currently have it destroy itself immediately
111 // upon disconnnect.
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000112 //connection_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113 return status;
114 }
115 return eConnectionStatusNoConnection;
116}
117
118bool
119Communication::IsConnected () const
120{
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000121 lldb::ConnectionSP connection_sp(m_connection_sp);
122 return (connection_sp ? connection_sp->IsConnected() : false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123}
124
125bool
126Communication::HasConnection () const
127{
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000128 return m_connection_sp.get() != nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129}
130
131size_t
132Communication::Read (void *dst, size_t dst_len, uint32_t timeout_usec, ConnectionStatus &status, Error *error_ptr)
133{
134 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
Daniel Malead01b2952012-11-29 21:49:15 +0000135 "%p Communication::Read (dst = %p, dst_len = %" PRIu64 ", timeout = %u usec) connection = %p",
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000136 this,
137 dst,
Greg Clayton43e0af02012-09-18 18:04:04 +0000138 (uint64_t)dst_len,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000139 timeout_usec,
140 m_connection_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141
Stephen Wilsona08cfb12011-01-12 04:22:54 +0000142 if (m_read_thread_enabled)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143 {
144 // We have a dedicated read thread that is getting data for us
145 size_t cached_bytes = GetCachedBytes (dst, dst_len);
146 if (cached_bytes > 0 || timeout_usec == 0)
147 {
148 status = eConnectionStatusSuccess;
149 return cached_bytes;
150 }
151
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000152 if (!m_connection_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153 {
154 if (error_ptr)
155 error_ptr->SetErrorString("Invalid connection.");
156 status = eConnectionStatusNoConnection;
157 return 0;
158 }
159 // Set the timeout appropriately
160 TimeValue timeout_time;
161 if (timeout_usec != UINT32_MAX)
162 {
163 timeout_time = TimeValue::Now();
164 timeout_time.OffsetWithMicroSeconds (timeout_usec);
165 }
166
167 Listener listener ("Communication::Read");
168 listener.StartListeningForEvents (this, eBroadcastBitReadThreadGotBytes | eBroadcastBitReadThreadDidExit);
169 EventSP event_sp;
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000170 while (listener.WaitForEvent((timeout_time.IsValid() ? &timeout_time : nullptr), event_sp))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171 {
172 const uint32_t event_type = event_sp->GetType();
173 if (event_type & eBroadcastBitReadThreadGotBytes)
174 {
175 return GetCachedBytes (dst, dst_len);
176 }
177
178 if (event_type & eBroadcastBitReadThreadDidExit)
179 {
Tamas Berghammer35856692015-04-20 09:52:47 +0000180 if (GetCloseOnEOF ())
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000181 Disconnect(nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182 break;
183 }
184 }
185 return 0;
186 }
187
188 // We aren't using a read thread, just read the data synchronously in this
189 // thread.
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000190 lldb::ConnectionSP connection_sp (m_connection_sp);
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000191 if (connection_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000193 return connection_sp->Read (dst, dst_len, timeout_usec, status, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194 }
195
196 if (error_ptr)
197 error_ptr->SetErrorString("Invalid connection.");
198 status = eConnectionStatusNoConnection;
199 return 0;
200}
201
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202size_t
203Communication::Write (const void *src, size_t src_len, ConnectionStatus &status, Error *error_ptr)
204{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000205 lldb::ConnectionSP connection_sp (m_connection_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206
Jason Molendaccd41e52012-10-04 22:47:07 +0000207 Mutex::Locker locker(m_write_mutex);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000208 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
Daniel Malead01b2952012-11-29 21:49:15 +0000209 "%p Communication::Write (src = %p, src_len = %" PRIu64 ") connection = %p",
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000210 this,
211 src,
Greg Clayton43e0af02012-09-18 18:04:04 +0000212 (uint64_t)src_len,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000213 connection_sp.get());
214
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000215 if (connection_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000216 return connection_sp->Write (src, src_len, status, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217
218 if (error_ptr)
219 error_ptr->SetErrorString("Invalid connection.");
220 status = eConnectionStatusNoConnection;
221 return 0;
222}
223
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224bool
225Communication::StartReadThread (Error *error_ptr)
226{
Greg Clayton1cb64962011-03-24 04:28:38 +0000227 if (error_ptr)
228 error_ptr->Clear();
229
Zachary Turneracee96a2014-09-23 18:32:09 +0000230 if (m_read_thread.IsJoinable())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231 return true;
232
233 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
234 "%p Communication::StartReadThread ()", this);
235
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236 char thread_name[1024];
237 snprintf(thread_name, sizeof(thread_name), "<lldb.comm.%s>", m_broadcaster_name.AsCString());
238
Greg Clayton86c3f342010-09-15 05:19:45 +0000239 m_read_thread_enabled = true;
Pavel Labath3f5df532015-03-12 10:12:41 +0000240 m_read_thread_did_exit = false;
Zachary Turner39de3112014-09-09 20:54:56 +0000241 m_read_thread = ThreadLauncher::LaunchThread(thread_name, Communication::ReadThread, this, error_ptr);
Zachary Turneracee96a2014-09-23 18:32:09 +0000242 if (!m_read_thread.IsJoinable())
Greg Clayton86c3f342010-09-15 05:19:45 +0000243 m_read_thread_enabled = false;
Greg Clayton26661bc2010-07-23 15:43:25 +0000244 return m_read_thread_enabled;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245}
246
247bool
248Communication::StopReadThread (Error *error_ptr)
249{
Zachary Turneracee96a2014-09-23 18:32:09 +0000250 if (!m_read_thread.IsJoinable())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251 return true;
252
253 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
254 "%p Communication::StopReadThread ()", this);
255
256 m_read_thread_enabled = false;
257
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000258 BroadcastEvent(eBroadcastBitReadThreadShouldExit, nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259
Zachary Turner39de3112014-09-09 20:54:56 +0000260 // error = m_read_thread.Cancel();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261
Zachary Turner39de3112014-09-09 20:54:56 +0000262 Error error = m_read_thread.Join(nullptr);
Zachary Turner39de3112014-09-09 20:54:56 +0000263 return error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264}
265
Greg Clayton59042602014-01-30 18:52:57 +0000266bool
267Communication::JoinReadThread (Error *error_ptr)
268{
Zachary Turneracee96a2014-09-23 18:32:09 +0000269 if (!m_read_thread.IsJoinable())
Greg Clayton59042602014-01-30 18:52:57 +0000270 return true;
271
Zachary Turner39de3112014-09-09 20:54:56 +0000272 Error error = m_read_thread.Join(nullptr);
Zachary Turner39de3112014-09-09 20:54:56 +0000273 return error.Success();
Greg Clayton59042602014-01-30 18:52:57 +0000274}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275
276size_t
277Communication::GetCachedBytes (void *dst, size_t dst_len)
278{
279 Mutex::Locker locker(m_bytes_mutex);
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000280 if (!m_bytes.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281 {
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000282 // If DST is nullptr and we have a thread, then return the number
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283 // of bytes that are available so the caller can call again
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000284 if (dst == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285 return m_bytes.size();
286
287 const size_t len = std::min<size_t>(dst_len, m_bytes.size());
288
Greg Clayton471b31c2010-07-20 22:52:08 +0000289 ::memcpy (dst, m_bytes.c_str(), len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290 m_bytes.erase(m_bytes.begin(), m_bytes.begin() + len);
291
292 return len;
293 }
294 return 0;
295}
296
297void
Caroline Ticeefed6132010-11-19 20:47:54 +0000298Communication::AppendBytesToCache (const uint8_t * bytes, size_t len, bool broadcast, ConnectionStatus status)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000299{
300 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
Daniel Malead01b2952012-11-29 21:49:15 +0000301 "%p Communication::AppendBytesToCache (src = %p, src_len = %" PRIu64 ", broadcast = %i)",
Greg Clayton43e0af02012-09-18 18:04:04 +0000302 this, bytes, (uint64_t)len, broadcast);
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000303 if ((bytes == nullptr || len == 0)
Caroline Tice9fd58502011-02-03 20:02:43 +0000304 && (status != lldb::eConnectionStatusEndOfFile))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305 return;
306 if (m_callback)
307 {
308 // If the user registered a callback, then call it and do not broadcast
309 m_callback (m_callback_baton, bytes, len);
310 }
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000311 else if (bytes != nullptr && len > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312 {
313 Mutex::Locker locker(m_bytes_mutex);
314 m_bytes.append ((const char *)bytes, len);
315 if (broadcast)
316 BroadcastEventIfUnique (eBroadcastBitReadThreadGotBytes);
317 }
318}
319
320size_t
Greg Clayton73bf5db2011-06-17 01:22:15 +0000321Communication::ReadFromConnection (void *dst,
322 size_t dst_len,
323 uint32_t timeout_usec,
324 ConnectionStatus &status,
325 Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326{
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000327 lldb::ConnectionSP connection_sp(m_connection_sp);
328 return (connection_sp ? connection_sp->Read(dst, dst_len, timeout_usec, status, error_ptr) : 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329}
330
Caroline Tice82305fc2010-12-02 18:31:56 +0000331bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332Communication::ReadThreadIsRunning ()
333{
Stephen Wilsona08cfb12011-01-12 04:22:54 +0000334 return m_read_thread_enabled;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335}
336
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000337lldb::thread_result_t
338Communication::ReadThread (lldb::thread_arg_t p)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339{
340 Communication *comm = (Communication *)p;
341
Greg Clayton5160ce52013-03-27 23:08:40 +0000342 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMUNICATION));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343
344 if (log)
345 log->Printf ("%p Communication::ReadThread () thread starting...", p);
346
347 uint8_t buf[1024];
348
349 Error error;
350 ConnectionStatus status = eConnectionStatusSuccess;
351 bool done = false;
352 while (!done && comm->m_read_thread_enabled)
353 {
Peter Collingbourneba23ca02011-06-18 23:52:14 +0000354 size_t bytes_read = comm->ReadFromConnection (buf, sizeof(buf), 5 * TimeValue::MicroSecPerSec, status, &error);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000355 if (bytes_read > 0)
356 comm->AppendBytesToCache (buf, bytes_read, true, status);
357 else if ((bytes_read == 0)
358 && status == eConnectionStatusEndOfFile)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000360 if (comm->GetCloseOnEOF ())
361 comm->Disconnect ();
362 comm->AppendBytesToCache (buf, bytes_read, true, status);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363 }
364
365 switch (status)
366 {
367 case eConnectionStatusSuccess:
368 break;
369
Greg Clayton7788e5f2010-12-04 02:22:36 +0000370 case eConnectionStatusEndOfFile:
Tamas Berghammer35856692015-04-20 09:52:47 +0000371 done = true;
372 break;
Todd Fiala42628282014-08-21 17:16:26 +0000373 case eConnectionStatusError: // Check GetError() for details
374 if (error.GetType() == eErrorTypePOSIX && error.GetError() == EIO)
375 {
376 // EIO on a pipe is usually caused by remote shutdown
377 comm->Disconnect ();
378 done = true;
379 }
380 if (log)
381 error.LogIfError (log,
382 "%p Communication::ReadFromConnection () => status = %s",
383 p,
384 Communication::ConnectionStatusAsCString (status));
385 break;
Pavel Labath3f5df532015-03-12 10:12:41 +0000386 case eConnectionStatusInterrupted: // Synchronization signal from SynchronizeWithReadThread()
387 // The connection returns eConnectionStatusInterrupted only when there is no
388 // input pending to be read, so we can signal that.
389 comm->BroadcastEvent (eBroadcastBitNoMorePendingInput);
390 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391 case eConnectionStatusNoConnection: // No connection
392 case eConnectionStatusLostConnection: // Lost connection while connected to a valid connection
393 done = true;
Jason Molenda62e06812016-02-16 04:14:33 +0000394 LLVM_FALLTHROUGH;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395 case eConnectionStatusTimedOut: // Request timed out
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000396 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +0000397 error.LogIfError (log,
398 "%p Communication::ReadFromConnection () => status = %s",
399 p,
400 Communication::ConnectionStatusAsCString (status));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000401 break;
402 }
403 }
Caroline Tice20ad3c42010-10-29 21:48:37 +0000404 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMUNICATION);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405 if (log)
406 log->Printf ("%p Communication::ReadThread () thread exiting...", p);
407
Pavel Labath3f5df532015-03-12 10:12:41 +0000408 comm->m_read_thread_did_exit = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409 // Let clients know that this thread is exiting
Pavel Labath3f5df532015-03-12 10:12:41 +0000410 comm->BroadcastEvent (eBroadcastBitNoMorePendingInput);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411 comm->BroadcastEvent (eBroadcastBitReadThreadDidExit);
412 return NULL;
413}
414
415void
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000416Communication::SetReadThreadBytesReceivedCallback(ReadThreadBytesReceived callback,
417 void *callback_baton)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418{
419 m_callback = callback;
420 m_callback_baton = callback_baton;
421}
422
423void
Pavel Labath3f5df532015-03-12 10:12:41 +0000424Communication::SynchronizeWithReadThread ()
425{
426 // Only one thread can do the synchronization dance at a time.
427 Mutex::Locker locker(m_synchronize_mutex);
428
429 // First start listening for the synchronization event.
430 Listener listener("Communication::SyncronizeWithReadThread");
431 listener.StartListeningForEvents(this, eBroadcastBitNoMorePendingInput);
432
433 // If the thread is not running, there is no point in synchronizing.
434 if (!m_read_thread_enabled || m_read_thread_did_exit)
435 return;
436
437 // Notify the read thread.
438 m_connection_sp->InterruptRead();
439
440 // Wait for the synchronization event.
441 EventSP event_sp;
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000442 listener.WaitForEvent(nullptr, event_sp);
Pavel Labath3f5df532015-03-12 10:12:41 +0000443}
444
445void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446Communication::SetConnection (Connection *connection)
447{
Eugene Zelenko896ddd02016-03-02 01:09:03 +0000448 Disconnect(nullptr);
449 StopReadThread(nullptr);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000450 m_connection_sp.reset(connection);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451}
Caroline Ticeceb6b132010-10-26 03:11:13 +0000452
453const char *
454Communication::ConnectionStatusAsCString (lldb::ConnectionStatus status)
455{
456 switch (status)
457 {
458 case eConnectionStatusSuccess: return "success";
459 case eConnectionStatusError: return "error";
460 case eConnectionStatusTimedOut: return "timed out";
461 case eConnectionStatusNoConnection: return "no connection";
462 case eConnectionStatusLostConnection: return "lost connection";
Greg Clayton7a5388b2011-03-20 04:57:14 +0000463 case eConnectionStatusEndOfFile: return "end of file";
Greg Claytonf0066ad2014-05-02 00:45:31 +0000464 case eConnectionStatusInterrupted: return "interrupted";
Caroline Ticeceb6b132010-10-26 03:11:13 +0000465 }
466
467 static char unknown_state_string[64];
468 snprintf(unknown_state_string, sizeof (unknown_state_string), "ConnectionStatus = %i", status);
469 return unknown_state_string;
470}