blob: 65140353eb8ba0ce7dc9a307edc5cf5142616179 [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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Core/Communication.h"
15#include "lldb/Core/Connection.h"
16#include "lldb/Core/Log.h"
17#include "lldb/Core/Timer.h"
18#include "lldb/Core/Event.h"
Greg Clayton7fb56d02011-02-01 01:31:41 +000019#include "lldb/Host/Host.h"
Zachary Turner39de3112014-09-09 20:54:56 +000020#include "lldb/Host/HostThread.h"
21#include "lldb/Host/ThreadLauncher.h"
Eli Friedman88966972010-06-09 08:50:27 +000022#include <string.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023
24using namespace lldb;
25using namespace lldb_private;
26
Jim Ingham4bddaeb2012-02-16 06:50:00 +000027ConstString &
28Communication::GetStaticBroadcasterClass ()
29{
30 static ConstString class_name ("lldb.communication");
31 return class_name;
32}
33
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034//----------------------------------------------------------------------
35// Constructor
36//----------------------------------------------------------------------
Greg Claytond46c87a2010-12-04 02:39:47 +000037Communication::Communication(const char *name) :
Jim Ingham4bddaeb2012-02-16 06:50:00 +000038 Broadcaster (NULL, name),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000039 m_connection_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040 m_read_thread_enabled (false),
Pavel Labath3f5df532015-03-12 10:12:41 +000041 m_read_thread_did_exit (false),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042 m_bytes(),
43 m_bytes_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton7ec3d402011-01-27 09:02:32 +000044 m_write_mutex (Mutex::eMutexTypeNormal),
Pavel Labath3f5df532015-03-12 10:12:41 +000045 m_synchronize_mutex (Mutex::eMutexTypeNormal),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046 m_callback (NULL),
Caroline Tice82305fc2010-12-02 18:31:56 +000047 m_callback_baton (NULL),
Greg Claytond46c87a2010-12-04 02:39:47 +000048 m_close_on_eof (true)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049
50{
51 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_COMMUNICATION,
52 "%p Communication::Communication (name = %s)",
53 this, name);
Greg Clayton95bf0fd2011-04-01 00:29:43 +000054
55 SetEventName (eBroadcastBitDisconnected, "disconnected");
56 SetEventName (eBroadcastBitReadThreadGotBytes, "got bytes");
57 SetEventName (eBroadcastBitReadThreadDidExit, "read thread did exit");
58 SetEventName (eBroadcastBitReadThreadShouldExit, "read thread should exit");
59 SetEventName (eBroadcastBitPacketAvailable, "packet available");
Pavel Labath3f5df532015-03-12 10:12:41 +000060 SetEventName (eBroadcastBitNoMorePendingInput, "no more pending input");
Jim Ingham4bddaeb2012-02-16 06:50:00 +000061
62 CheckInWithManager();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063}
64
65//----------------------------------------------------------------------
66// Destructor
67//----------------------------------------------------------------------
68Communication::~Communication()
69{
70 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_COMMUNICATION,
71 "%p Communication::~Communication (name = %s)",
72 this, m_broadcaster_name.AsCString(""));
73 Clear();
74}
75
76void
77Communication::Clear()
78{
Greg Clayton756f8ae2011-08-19 23:28:37 +000079 SetReadThreadBytesReceivedCallback (NULL, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080 Disconnect (NULL);
Greg Clayton74d41932012-01-31 04:56:17 +000081 StopReadThread (NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082}
83
84ConnectionStatus
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085Communication::Connect (const char *url, Error *error_ptr)
86{
87 Clear();
88
89 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::Connect (url = %s)", this, url);
90
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000091 lldb::ConnectionSP connection_sp (m_connection_sp);
92 if (connection_sp.get())
93 return connection_sp->Connect (url, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094 if (error_ptr)
95 error_ptr->SetErrorString("Invalid connection.");
96 return eConnectionStatusNoConnection;
97}
98
99ConnectionStatus
100Communication::Disconnect (Error *error_ptr)
101{
102 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::Disconnect ()", this);
103
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000104 lldb::ConnectionSP connection_sp (m_connection_sp);
105 if (connection_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000107 ConnectionStatus status = connection_sp->Disconnect (error_ptr);
108 // We currently don't protect connection_sp with any mutex for
Greg Claytonbfae66a2010-12-12 21:50:57 +0000109 // multi-threaded environments. So lets not nuke our connection class
110 // without putting some multi-threaded protections in. We also probably
111 // don't want to pay for the overhead it might cause if every time we
112 // access the connection we have to take a lock.
113 //
Greg Claytone01e07b2013-04-18 18:10:51 +0000114 // This unique pointer will cleanup after itself when this object goes away,
Greg Claytonbfae66a2010-12-12 21:50:57 +0000115 // so there is no need to currently have it destroy itself immediately
116 // upon disconnnect.
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000117 //connection_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118 return status;
119 }
120 return eConnectionStatusNoConnection;
121}
122
123bool
124Communication::IsConnected () const
125{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000126 lldb::ConnectionSP connection_sp (m_connection_sp);
127 if (connection_sp.get())
128 return connection_sp->IsConnected ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 return false;
130}
131
132bool
133Communication::HasConnection () const
134{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000135 return m_connection_sp.get() != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000136}
137
138size_t
139Communication::Read (void *dst, size_t dst_len, uint32_t timeout_usec, ConnectionStatus &status, Error *error_ptr)
140{
141 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
Daniel Malead01b2952012-11-29 21:49:15 +0000142 "%p Communication::Read (dst = %p, dst_len = %" PRIu64 ", timeout = %u usec) connection = %p",
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000143 this,
144 dst,
Greg Clayton43e0af02012-09-18 18:04:04 +0000145 (uint64_t)dst_len,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000146 timeout_usec,
147 m_connection_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148
Stephen Wilsona08cfb12011-01-12 04:22:54 +0000149 if (m_read_thread_enabled)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150 {
151 // We have a dedicated read thread that is getting data for us
152 size_t cached_bytes = GetCachedBytes (dst, dst_len);
153 if (cached_bytes > 0 || timeout_usec == 0)
154 {
155 status = eConnectionStatusSuccess;
156 return cached_bytes;
157 }
158
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000159 if (m_connection_sp.get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160 {
161 if (error_ptr)
162 error_ptr->SetErrorString("Invalid connection.");
163 status = eConnectionStatusNoConnection;
164 return 0;
165 }
166 // Set the timeout appropriately
167 TimeValue timeout_time;
168 if (timeout_usec != UINT32_MAX)
169 {
170 timeout_time = TimeValue::Now();
171 timeout_time.OffsetWithMicroSeconds (timeout_usec);
172 }
173
174 Listener listener ("Communication::Read");
175 listener.StartListeningForEvents (this, eBroadcastBitReadThreadGotBytes | eBroadcastBitReadThreadDidExit);
176 EventSP event_sp;
177 while (listener.WaitForEvent (timeout_time.IsValid() ? &timeout_time : NULL, event_sp))
178 {
179 const uint32_t event_type = event_sp->GetType();
180 if (event_type & eBroadcastBitReadThreadGotBytes)
181 {
182 return GetCachedBytes (dst, dst_len);
183 }
184
185 if (event_type & eBroadcastBitReadThreadDidExit)
186 {
Tamas Berghammer35856692015-04-20 09:52:47 +0000187 if (GetCloseOnEOF ())
188 Disconnect (NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189 break;
190 }
191 }
192 return 0;
193 }
194
195 // We aren't using a read thread, just read the data synchronously in this
196 // thread.
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000197 lldb::ConnectionSP connection_sp (m_connection_sp);
198 if (connection_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000200 return connection_sp->Read (dst, dst_len, timeout_usec, status, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201 }
202
203 if (error_ptr)
204 error_ptr->SetErrorString("Invalid connection.");
205 status = eConnectionStatusNoConnection;
206 return 0;
207}
208
209
210size_t
211Communication::Write (const void *src, size_t src_len, ConnectionStatus &status, Error *error_ptr)
212{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000213 lldb::ConnectionSP connection_sp (m_connection_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214
Jason Molendaccd41e52012-10-04 22:47:07 +0000215 Mutex::Locker locker(m_write_mutex);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000216 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
Daniel Malead01b2952012-11-29 21:49:15 +0000217 "%p Communication::Write (src = %p, src_len = %" PRIu64 ") connection = %p",
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000218 this,
219 src,
Greg Clayton43e0af02012-09-18 18:04:04 +0000220 (uint64_t)src_len,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000221 connection_sp.get());
222
223 if (connection_sp.get())
224 return connection_sp->Write (src, src_len, status, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225
226 if (error_ptr)
227 error_ptr->SetErrorString("Invalid connection.");
228 status = eConnectionStatusNoConnection;
229 return 0;
230}
231
232
233bool
234Communication::StartReadThread (Error *error_ptr)
235{
Greg Clayton1cb64962011-03-24 04:28:38 +0000236 if (error_ptr)
237 error_ptr->Clear();
238
Zachary Turneracee96a2014-09-23 18:32:09 +0000239 if (m_read_thread.IsJoinable())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240 return true;
241
242 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
243 "%p Communication::StartReadThread ()", this);
244
245
246 char thread_name[1024];
247 snprintf(thread_name, sizeof(thread_name), "<lldb.comm.%s>", m_broadcaster_name.AsCString());
248
Greg Clayton86c3f342010-09-15 05:19:45 +0000249 m_read_thread_enabled = true;
Pavel Labath3f5df532015-03-12 10:12:41 +0000250 m_read_thread_did_exit = false;
Zachary Turner39de3112014-09-09 20:54:56 +0000251 m_read_thread = ThreadLauncher::LaunchThread(thread_name, Communication::ReadThread, this, error_ptr);
Zachary Turneracee96a2014-09-23 18:32:09 +0000252 if (!m_read_thread.IsJoinable())
Greg Clayton86c3f342010-09-15 05:19:45 +0000253 m_read_thread_enabled = false;
Greg Clayton26661bc2010-07-23 15:43:25 +0000254 return m_read_thread_enabled;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255}
256
257bool
258Communication::StopReadThread (Error *error_ptr)
259{
Zachary Turneracee96a2014-09-23 18:32:09 +0000260 if (!m_read_thread.IsJoinable())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261 return true;
262
263 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
264 "%p Communication::StopReadThread ()", this);
265
266 m_read_thread_enabled = false;
267
268 BroadcastEvent (eBroadcastBitReadThreadShouldExit, NULL);
269
Zachary Turner39de3112014-09-09 20:54:56 +0000270 // error = m_read_thread.Cancel();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000271
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();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000274}
275
Greg Clayton59042602014-01-30 18:52:57 +0000276bool
277Communication::JoinReadThread (Error *error_ptr)
278{
Zachary Turneracee96a2014-09-23 18:32:09 +0000279 if (!m_read_thread.IsJoinable())
Greg Clayton59042602014-01-30 18:52:57 +0000280 return true;
281
Zachary Turner39de3112014-09-09 20:54:56 +0000282 Error error = m_read_thread.Join(nullptr);
Zachary Turner39de3112014-09-09 20:54:56 +0000283 return error.Success();
Greg Clayton59042602014-01-30 18:52:57 +0000284}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285
286size_t
287Communication::GetCachedBytes (void *dst, size_t dst_len)
288{
289 Mutex::Locker locker(m_bytes_mutex);
290 if (m_bytes.size() > 0)
291 {
292 // If DST is NULL and we have a thread, then return the number
293 // of bytes that are available so the caller can call again
294 if (dst == NULL)
295 return m_bytes.size();
296
297 const size_t len = std::min<size_t>(dst_len, m_bytes.size());
298
Greg Clayton471b31c2010-07-20 22:52:08 +0000299 ::memcpy (dst, m_bytes.c_str(), len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300 m_bytes.erase(m_bytes.begin(), m_bytes.begin() + len);
301
302 return len;
303 }
304 return 0;
305}
306
307void
Caroline Ticeefed6132010-11-19 20:47:54 +0000308Communication::AppendBytesToCache (const uint8_t * bytes, size_t len, bool broadcast, ConnectionStatus status)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309{
310 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
Daniel Malead01b2952012-11-29 21:49:15 +0000311 "%p Communication::AppendBytesToCache (src = %p, src_len = %" PRIu64 ", broadcast = %i)",
Greg Clayton43e0af02012-09-18 18:04:04 +0000312 this, bytes, (uint64_t)len, broadcast);
Caroline Tice9fd58502011-02-03 20:02:43 +0000313 if ((bytes == NULL || len == 0)
314 && (status != lldb::eConnectionStatusEndOfFile))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000315 return;
316 if (m_callback)
317 {
318 // If the user registered a callback, then call it and do not broadcast
319 m_callback (m_callback_baton, bytes, len);
320 }
Greg Clayton756f8ae2011-08-19 23:28:37 +0000321 else if (bytes != NULL && len > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322 {
323 Mutex::Locker locker(m_bytes_mutex);
324 m_bytes.append ((const char *)bytes, len);
325 if (broadcast)
326 BroadcastEventIfUnique (eBroadcastBitReadThreadGotBytes);
327 }
328}
329
330size_t
Greg Clayton73bf5db2011-06-17 01:22:15 +0000331Communication::ReadFromConnection (void *dst,
332 size_t dst_len,
333 uint32_t timeout_usec,
334 ConnectionStatus &status,
335 Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000337 lldb::ConnectionSP connection_sp (m_connection_sp);
338 if (connection_sp.get())
Greg Clayton73bf5db2011-06-17 01:22:15 +0000339 return connection_sp->Read (dst, dst_len, timeout_usec, status, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340 return 0;
341}
342
Caroline Tice82305fc2010-12-02 18:31:56 +0000343bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344Communication::ReadThreadIsRunning ()
345{
Stephen Wilsona08cfb12011-01-12 04:22:54 +0000346 return m_read_thread_enabled;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347}
348
Virgile Bellob2f1fb22013-08-23 12:44:05 +0000349lldb::thread_result_t
350Communication::ReadThread (lldb::thread_arg_t p)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351{
352 Communication *comm = (Communication *)p;
353
Greg Clayton5160ce52013-03-27 23:08:40 +0000354 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMUNICATION));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355
356 if (log)
357 log->Printf ("%p Communication::ReadThread () thread starting...", p);
358
359 uint8_t buf[1024];
360
361 Error error;
362 ConnectionStatus status = eConnectionStatusSuccess;
363 bool done = false;
364 while (!done && comm->m_read_thread_enabled)
365 {
Peter Collingbourneba23ca02011-06-18 23:52:14 +0000366 size_t bytes_read = comm->ReadFromConnection (buf, sizeof(buf), 5 * TimeValue::MicroSecPerSec, status, &error);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000367 if (bytes_read > 0)
368 comm->AppendBytesToCache (buf, bytes_read, true, status);
369 else if ((bytes_read == 0)
370 && status == eConnectionStatusEndOfFile)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000372 if (comm->GetCloseOnEOF ())
373 comm->Disconnect ();
374 comm->AppendBytesToCache (buf, bytes_read, true, status);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375 }
376
377 switch (status)
378 {
379 case eConnectionStatusSuccess:
380 break;
381
Greg Clayton7788e5f2010-12-04 02:22:36 +0000382 case eConnectionStatusEndOfFile:
Tamas Berghammer35856692015-04-20 09:52:47 +0000383 done = true;
384 break;
Todd Fiala42628282014-08-21 17:16:26 +0000385 case eConnectionStatusError: // Check GetError() for details
386 if (error.GetType() == eErrorTypePOSIX && error.GetError() == EIO)
387 {
388 // EIO on a pipe is usually caused by remote shutdown
389 comm->Disconnect ();
390 done = true;
391 }
392 if (log)
393 error.LogIfError (log,
394 "%p Communication::ReadFromConnection () => status = %s",
395 p,
396 Communication::ConnectionStatusAsCString (status));
397 break;
Pavel Labath3f5df532015-03-12 10:12:41 +0000398 case eConnectionStatusInterrupted: // Synchronization signal from SynchronizeWithReadThread()
399 // The connection returns eConnectionStatusInterrupted only when there is no
400 // input pending to be read, so we can signal that.
401 comm->BroadcastEvent (eBroadcastBitNoMorePendingInput);
402 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403 case eConnectionStatusNoConnection: // No connection
404 case eConnectionStatusLostConnection: // Lost connection while connected to a valid connection
405 done = true;
Jason Molenda62e06812016-02-16 04:14:33 +0000406 LLVM_FALLTHROUGH;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407 case eConnectionStatusTimedOut: // Request timed out
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000408 if (log)
Greg Clayton5160ce52013-03-27 23:08:40 +0000409 error.LogIfError (log,
410 "%p Communication::ReadFromConnection () => status = %s",
411 p,
412 Communication::ConnectionStatusAsCString (status));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413 break;
414 }
415 }
Caroline Tice20ad3c42010-10-29 21:48:37 +0000416 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMUNICATION);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417 if (log)
418 log->Printf ("%p Communication::ReadThread () thread exiting...", p);
419
Pavel Labath3f5df532015-03-12 10:12:41 +0000420 comm->m_read_thread_did_exit = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421 // Let clients know that this thread is exiting
Pavel Labath3f5df532015-03-12 10:12:41 +0000422 comm->BroadcastEvent (eBroadcastBitNoMorePendingInput);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423 comm->BroadcastEvent (eBroadcastBitReadThreadDidExit);
424 return NULL;
425}
426
427void
428Communication::SetReadThreadBytesReceivedCallback
429(
430 ReadThreadBytesReceived callback,
431 void *callback_baton
432)
433{
434 m_callback = callback;
435 m_callback_baton = callback_baton;
436}
437
438void
Pavel Labath3f5df532015-03-12 10:12:41 +0000439Communication::SynchronizeWithReadThread ()
440{
441 // Only one thread can do the synchronization dance at a time.
442 Mutex::Locker locker(m_synchronize_mutex);
443
444 // First start listening for the synchronization event.
445 Listener listener("Communication::SyncronizeWithReadThread");
446 listener.StartListeningForEvents(this, eBroadcastBitNoMorePendingInput);
447
448 // If the thread is not running, there is no point in synchronizing.
449 if (!m_read_thread_enabled || m_read_thread_did_exit)
450 return;
451
452 // Notify the read thread.
453 m_connection_sp->InterruptRead();
454
455 // Wait for the synchronization event.
456 EventSP event_sp;
457 listener.WaitForEvent(NULL, event_sp);
458}
459
460void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461Communication::SetConnection (Connection *connection)
462{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463 Disconnect (NULL);
Greg Clayton74d41932012-01-31 04:56:17 +0000464 StopReadThread(NULL);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000465 m_connection_sp.reset(connection);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466}
Caroline Ticeceb6b132010-10-26 03:11:13 +0000467
468const char *
469Communication::ConnectionStatusAsCString (lldb::ConnectionStatus status)
470{
471 switch (status)
472 {
473 case eConnectionStatusSuccess: return "success";
474 case eConnectionStatusError: return "error";
475 case eConnectionStatusTimedOut: return "timed out";
476 case eConnectionStatusNoConnection: return "no connection";
477 case eConnectionStatusLostConnection: return "lost connection";
Greg Clayton7a5388b2011-03-20 04:57:14 +0000478 case eConnectionStatusEndOfFile: return "end of file";
Greg Claytonf0066ad2014-05-02 00:45:31 +0000479 case eConnectionStatusInterrupted: return "interrupted";
Caroline Ticeceb6b132010-10-26 03:11:13 +0000480 }
481
482 static char unknown_state_string[64];
483 snprintf(unknown_state_string, sizeof (unknown_state_string), "ConnectionStatus = %i", status);
484 return unknown_state_string;
485}