blob: 6b8a6fe530ba7cac097acc5f99d28d144eb740d5 [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"
Eli Friedman88966972010-06-09 08:50:27 +000021#include <string.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022
23using namespace lldb;
24using namespace lldb_private;
25
26//----------------------------------------------------------------------
27// Constructor
28//----------------------------------------------------------------------
Greg Claytond46c87a2010-12-04 02:39:47 +000029Communication::Communication(const char *name) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030 Broadcaster (name),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000031 m_connection_sp (),
Eli Friedman8878f872010-07-09 22:53:18 +000032 m_read_thread (LLDB_INVALID_HOST_THREAD),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033 m_read_thread_enabled (false),
34 m_bytes(),
35 m_bytes_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton7ec3d402011-01-27 09:02:32 +000036 m_write_mutex (Mutex::eMutexTypeNormal),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037 m_callback (NULL),
Caroline Tice82305fc2010-12-02 18:31:56 +000038 m_callback_baton (NULL),
Greg Claytond46c87a2010-12-04 02:39:47 +000039 m_close_on_eof (true)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040
41{
42 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_COMMUNICATION,
43 "%p Communication::Communication (name = %s)",
44 this, name);
45}
46
47//----------------------------------------------------------------------
48// Destructor
49//----------------------------------------------------------------------
50Communication::~Communication()
51{
52 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_COMMUNICATION,
53 "%p Communication::~Communication (name = %s)",
54 this, m_broadcaster_name.AsCString(""));
55 Clear();
56}
57
58void
59Communication::Clear()
60{
61 StopReadThread (NULL);
62 Disconnect (NULL);
63}
64
65ConnectionStatus
66Communication::BytesAvailable (uint32_t timeout_usec, Error *error_ptr)
67{
68 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::BytesAvailable (timeout_usec = %u)", this, timeout_usec);
69
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000070 lldb::ConnectionSP connection_sp (m_connection_sp);
71 if (connection_sp.get())
72 return connection_sp->BytesAvailable (timeout_usec, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073 if (error_ptr)
74 error_ptr->SetErrorString("Invalid connection.");
75 return eConnectionStatusNoConnection;
76}
77
78ConnectionStatus
79Communication::Connect (const char *url, Error *error_ptr)
80{
81 Clear();
82
83 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::Connect (url = %s)", this, url);
84
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000085 lldb::ConnectionSP connection_sp (m_connection_sp);
86 if (connection_sp.get())
87 return connection_sp->Connect (url, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088 if (error_ptr)
89 error_ptr->SetErrorString("Invalid connection.");
90 return eConnectionStatusNoConnection;
91}
92
93ConnectionStatus
94Communication::Disconnect (Error *error_ptr)
95{
96 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::Disconnect ()", this);
97
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000098 lldb::ConnectionSP connection_sp (m_connection_sp);
99 if (connection_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000101 ConnectionStatus status = connection_sp->Disconnect (error_ptr);
102 // We currently don't protect connection_sp with any mutex for
Greg Claytonbfae66a2010-12-12 21:50:57 +0000103 // multi-threaded environments. So lets not nuke our connection class
104 // without putting some multi-threaded protections in. We also probably
105 // don't want to pay for the overhead it might cause if every time we
106 // access the connection we have to take a lock.
107 //
108 // This auto_ptr will cleanup after itself when this object goes away,
109 // so there is no need to currently have it destroy itself immediately
110 // upon disconnnect.
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000111 //connection_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112 return status;
113 }
114 return eConnectionStatusNoConnection;
115}
116
117bool
118Communication::IsConnected () const
119{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000120 lldb::ConnectionSP connection_sp (m_connection_sp);
121 if (connection_sp.get())
122 return connection_sp->IsConnected ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123 return false;
124}
125
126bool
127Communication::HasConnection () const
128{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000129 return m_connection_sp.get() != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130}
131
132size_t
133Communication::Read (void *dst, size_t dst_len, uint32_t timeout_usec, ConnectionStatus &status, Error *error_ptr)
134{
135 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000136 "%p Communication::Read (dst = %p, dst_len = %zu, timeout_usec = %u) connection = %p",
137 this,
138 dst,
139 dst_len,
140 timeout_usec,
141 m_connection_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142
Stephen Wilsona08cfb12011-01-12 04:22:54 +0000143 if (m_read_thread_enabled)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144 {
145 // We have a dedicated read thread that is getting data for us
146 size_t cached_bytes = GetCachedBytes (dst, dst_len);
147 if (cached_bytes > 0 || timeout_usec == 0)
148 {
149 status = eConnectionStatusSuccess;
150 return cached_bytes;
151 }
152
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000153 if (m_connection_sp.get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154 {
155 if (error_ptr)
156 error_ptr->SetErrorString("Invalid connection.");
157 status = eConnectionStatusNoConnection;
158 return 0;
159 }
160 // Set the timeout appropriately
161 TimeValue timeout_time;
162 if (timeout_usec != UINT32_MAX)
163 {
164 timeout_time = TimeValue::Now();
165 timeout_time.OffsetWithMicroSeconds (timeout_usec);
166 }
167
168 Listener listener ("Communication::Read");
169 listener.StartListeningForEvents (this, eBroadcastBitReadThreadGotBytes | eBroadcastBitReadThreadDidExit);
170 EventSP event_sp;
171 while (listener.WaitForEvent (timeout_time.IsValid() ? &timeout_time : NULL, event_sp))
172 {
173 const uint32_t event_type = event_sp->GetType();
174 if (event_type & eBroadcastBitReadThreadGotBytes)
175 {
176 return GetCachedBytes (dst, dst_len);
177 }
178
179 if (event_type & eBroadcastBitReadThreadDidExit)
180 {
181 Disconnect (NULL);
182 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);
191 if (connection_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000193 status = connection_sp->BytesAvailable (timeout_usec, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194 if (status == eConnectionStatusSuccess)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000195 return connection_sp->Read (dst, dst_len, status, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196 }
197
198 if (error_ptr)
199 error_ptr->SetErrorString("Invalid connection.");
200 status = eConnectionStatusNoConnection;
201 return 0;
202}
203
204
205size_t
206Communication::Write (const void *src, size_t src_len, ConnectionStatus &status, Error *error_ptr)
207{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000208 lldb::ConnectionSP connection_sp (m_connection_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000209
Greg Clayton7ec3d402011-01-27 09:02:32 +0000210 Mutex::Locker (m_write_mutex);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000211 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
212 "%p Communication::Write (src = %p, src_len = %zu) connection = %p",
213 this,
214 src,
215 src_len,
216 connection_sp.get());
217
218 if (connection_sp.get())
219 return connection_sp->Write (src, src_len, status, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000220
221 if (error_ptr)
222 error_ptr->SetErrorString("Invalid connection.");
223 status = eConnectionStatusNoConnection;
224 return 0;
225}
226
227
228bool
229Communication::StartReadThread (Error *error_ptr)
230{
231 if (m_read_thread != LLDB_INVALID_HOST_THREAD)
232 return true;
233
234 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
235 "%p Communication::StartReadThread ()", this);
236
237
238 char thread_name[1024];
239 snprintf(thread_name, sizeof(thread_name), "<lldb.comm.%s>", m_broadcaster_name.AsCString());
240
Greg Clayton86c3f342010-09-15 05:19:45 +0000241 m_read_thread_enabled = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242 m_read_thread = Host::ThreadCreate (thread_name, Communication::ReadThread, this, error_ptr);
Greg Clayton86c3f342010-09-15 05:19:45 +0000243 if (m_read_thread == LLDB_INVALID_HOST_THREAD)
244 m_read_thread_enabled = false;
Greg Clayton26661bc2010-07-23 15:43:25 +0000245 return m_read_thread_enabled;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246}
247
248bool
249Communication::StopReadThread (Error *error_ptr)
250{
Eli Friedman8878f872010-07-09 22:53:18 +0000251 if (m_read_thread == LLDB_INVALID_HOST_THREAD)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252 return true;
253
254 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
255 "%p Communication::StopReadThread ()", this);
256
257 m_read_thread_enabled = false;
258
259 BroadcastEvent (eBroadcastBitReadThreadShouldExit, NULL);
260
261 Host::ThreadCancel (m_read_thread, error_ptr);
262
Stephen Wilsona08cfb12011-01-12 04:22:54 +0000263 bool status = Host::ThreadJoin (m_read_thread, NULL, error_ptr);
Greg Clayton26661bc2010-07-23 15:43:25 +0000264 m_read_thread = LLDB_INVALID_HOST_THREAD;
Stephen Wilsona08cfb12011-01-12 04:22:54 +0000265 return status;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000266}
267
268
269size_t
270Communication::GetCachedBytes (void *dst, size_t dst_len)
271{
272 Mutex::Locker locker(m_bytes_mutex);
273 if (m_bytes.size() > 0)
274 {
275 // If DST is NULL and we have a thread, then return the number
276 // of bytes that are available so the caller can call again
277 if (dst == NULL)
278 return m_bytes.size();
279
280 const size_t len = std::min<size_t>(dst_len, m_bytes.size());
281
Greg Clayton471b31c2010-07-20 22:52:08 +0000282 ::memcpy (dst, m_bytes.c_str(), len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283 m_bytes.erase(m_bytes.begin(), m_bytes.begin() + len);
284
285 return len;
286 }
287 return 0;
288}
289
290void
Caroline Ticeefed6132010-11-19 20:47:54 +0000291Communication::AppendBytesToCache (const uint8_t * bytes, size_t len, bool broadcast, ConnectionStatus status)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292{
293 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
294 "%p Communication::AppendBytesToCache (src = %p, src_len = %zu, broadcast = %i)",
295 this, bytes, len, broadcast);
Caroline Tice9fd58502011-02-03 20:02:43 +0000296 if ((bytes == NULL || len == 0)
297 && (status != lldb::eConnectionStatusEndOfFile))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298 return;
299 if (m_callback)
300 {
301 // If the user registered a callback, then call it and do not broadcast
302 m_callback (m_callback_baton, bytes, len);
303 }
304 else
305 {
306 Mutex::Locker locker(m_bytes_mutex);
307 m_bytes.append ((const char *)bytes, len);
308 if (broadcast)
309 BroadcastEventIfUnique (eBroadcastBitReadThreadGotBytes);
310 }
311}
312
313size_t
314Communication::ReadFromConnection (void *dst, size_t dst_len, ConnectionStatus &status, Error *error_ptr)
315{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000316 lldb::ConnectionSP connection_sp (m_connection_sp);
317 if (connection_sp.get())
318 return connection_sp->Read (dst, dst_len, status, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319 return 0;
320}
321
Caroline Tice82305fc2010-12-02 18:31:56 +0000322bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323Communication::ReadThreadIsRunning ()
324{
Stephen Wilsona08cfb12011-01-12 04:22:54 +0000325 return m_read_thread_enabled;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326}
327
328void *
329Communication::ReadThread (void *p)
330{
331 Communication *comm = (Communication *)p;
332
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000333 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMUNICATION));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334
335 if (log)
336 log->Printf ("%p Communication::ReadThread () thread starting...", p);
337
338 uint8_t buf[1024];
339
340 Error error;
341 ConnectionStatus status = eConnectionStatusSuccess;
342 bool done = false;
343 while (!done && comm->m_read_thread_enabled)
344 {
345 status = comm->BytesAvailable (UINT32_MAX, &error);
346
347 if (status == eConnectionStatusSuccess)
348 {
349 size_t bytes_read = comm->ReadFromConnection (buf, sizeof(buf), status, &error);
350 if (bytes_read > 0)
Caroline Ticeefed6132010-11-19 20:47:54 +0000351 comm->AppendBytesToCache (buf, bytes_read, true, status);
Caroline Tice9fd58502011-02-03 20:02:43 +0000352 else if ((bytes_read == 0)
353 && status == eConnectionStatusEndOfFile)
354 {
355 if (comm->GetCloseOnEOF ())
356 comm->Disconnect ();
357 comm->AppendBytesToCache (buf, bytes_read, true, status);
358 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359 }
360
361 switch (status)
362 {
363 case eConnectionStatusSuccess:
364 break;
365
Greg Clayton7788e5f2010-12-04 02:22:36 +0000366 case eConnectionStatusEndOfFile:
Caroline Tice9fd58502011-02-03 20:02:43 +0000367 if (comm->GetCloseOnEOF())
368 done = true;
369 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370 case eConnectionStatusNoConnection: // No connection
371 case eConnectionStatusLostConnection: // Lost connection while connected to a valid connection
372 done = true;
373 // Fall through...
374 default:
375 case eConnectionStatusError: // Check GetError() for details
376 case eConnectionStatusTimedOut: // Request timed out
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000377 if (log)
378 error.LogIfError(log.get(), "%p Communication::BytesAvailable () => status = %i", p, status);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379 break;
380 }
381 }
Caroline Tice20ad3c42010-10-29 21:48:37 +0000382 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMUNICATION);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383 if (log)
384 log->Printf ("%p Communication::ReadThread () thread exiting...", p);
385
386 // Let clients know that this thread is exiting
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000387 comm->BroadcastEvent (eBroadcastBitReadThreadDidExit);
Stephen Wilsona08cfb12011-01-12 04:22:54 +0000388 comm->m_read_thread_enabled = false;
Greg Claytonbfae66a2010-12-12 21:50:57 +0000389 comm->Disconnect();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390 return NULL;
391}
392
393void
394Communication::SetReadThreadBytesReceivedCallback
395(
396 ReadThreadBytesReceived callback,
397 void *callback_baton
398)
399{
400 m_callback = callback;
401 m_callback_baton = callback_baton;
402}
403
404void
405Communication::SetConnection (Connection *connection)
406{
407 StopReadThread(NULL);
408 Disconnect (NULL);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000409 m_connection_sp.reset(connection);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410}
Caroline Ticeceb6b132010-10-26 03:11:13 +0000411
412const char *
413Communication::ConnectionStatusAsCString (lldb::ConnectionStatus status)
414{
415 switch (status)
416 {
417 case eConnectionStatusSuccess: return "success";
418 case eConnectionStatusError: return "error";
419 case eConnectionStatusTimedOut: return "timed out";
420 case eConnectionStatusNoConnection: return "no connection";
421 case eConnectionStatusLostConnection: return "lost connection";
422 }
423
424 static char unknown_state_string[64];
425 snprintf(unknown_state_string, sizeof (unknown_state_string), "ConnectionStatus = %i", status);
426 return unknown_state_string;
427}