blob: 81c1bce30f2fcaed28d8cf2fff74e6806575014d [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);
Greg Clayton95bf0fd2011-04-01 00:29:43 +000045
46 SetEventName (eBroadcastBitDisconnected, "disconnected");
47 SetEventName (eBroadcastBitReadThreadGotBytes, "got bytes");
48 SetEventName (eBroadcastBitReadThreadDidExit, "read thread did exit");
49 SetEventName (eBroadcastBitReadThreadShouldExit, "read thread should exit");
50 SetEventName (eBroadcastBitPacketAvailable, "packet available");
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051}
52
53//----------------------------------------------------------------------
54// Destructor
55//----------------------------------------------------------------------
56Communication::~Communication()
57{
58 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_COMMUNICATION,
59 "%p Communication::~Communication (name = %s)",
60 this, m_broadcaster_name.AsCString(""));
61 Clear();
62}
63
64void
65Communication::Clear()
66{
67 StopReadThread (NULL);
68 Disconnect (NULL);
69}
70
71ConnectionStatus
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072Communication::Connect (const char *url, Error *error_ptr)
73{
74 Clear();
75
76 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::Connect (url = %s)", this, url);
77
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000078 lldb::ConnectionSP connection_sp (m_connection_sp);
79 if (connection_sp.get())
80 return connection_sp->Connect (url, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081 if (error_ptr)
82 error_ptr->SetErrorString("Invalid connection.");
83 return eConnectionStatusNoConnection;
84}
85
86ConnectionStatus
87Communication::Disconnect (Error *error_ptr)
88{
89 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::Disconnect ()", this);
90
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000091 lldb::ConnectionSP connection_sp (m_connection_sp);
92 if (connection_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000094 ConnectionStatus status = connection_sp->Disconnect (error_ptr);
95 // We currently don't protect connection_sp with any mutex for
Greg Claytonbfae66a2010-12-12 21:50:57 +000096 // multi-threaded environments. So lets not nuke our connection class
97 // without putting some multi-threaded protections in. We also probably
98 // don't want to pay for the overhead it might cause if every time we
99 // access the connection we have to take a lock.
100 //
101 // This auto_ptr will cleanup after itself when this object goes away,
102 // so there is no need to currently have it destroy itself immediately
103 // upon disconnnect.
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000104 //connection_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105 return status;
106 }
107 return eConnectionStatusNoConnection;
108}
109
110bool
111Communication::IsConnected () const
112{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000113 lldb::ConnectionSP connection_sp (m_connection_sp);
114 if (connection_sp.get())
115 return connection_sp->IsConnected ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116 return false;
117}
118
119bool
120Communication::HasConnection () const
121{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000122 return m_connection_sp.get() != NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123}
124
125size_t
126Communication::Read (void *dst, size_t dst_len, uint32_t timeout_usec, ConnectionStatus &status, Error *error_ptr)
127{
128 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
Greg Clayton73bf5db2011-06-17 01:22:15 +0000129 "%p Communication::Read (dst = %p, dst_len = %zu, timeout = %u usec) connection = %p",
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000130 this,
131 dst,
132 dst_len,
133 timeout_usec,
134 m_connection_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135
Stephen Wilsona08cfb12011-01-12 04:22:54 +0000136 if (m_read_thread_enabled)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137 {
138 // We have a dedicated read thread that is getting data for us
139 size_t cached_bytes = GetCachedBytes (dst, dst_len);
140 if (cached_bytes > 0 || timeout_usec == 0)
141 {
142 status = eConnectionStatusSuccess;
143 return cached_bytes;
144 }
145
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000146 if (m_connection_sp.get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147 {
148 if (error_ptr)
149 error_ptr->SetErrorString("Invalid connection.");
150 status = eConnectionStatusNoConnection;
151 return 0;
152 }
153 // Set the timeout appropriately
154 TimeValue timeout_time;
155 if (timeout_usec != UINT32_MAX)
156 {
157 timeout_time = TimeValue::Now();
158 timeout_time.OffsetWithMicroSeconds (timeout_usec);
159 }
160
161 Listener listener ("Communication::Read");
162 listener.StartListeningForEvents (this, eBroadcastBitReadThreadGotBytes | eBroadcastBitReadThreadDidExit);
163 EventSP event_sp;
164 while (listener.WaitForEvent (timeout_time.IsValid() ? &timeout_time : NULL, event_sp))
165 {
166 const uint32_t event_type = event_sp->GetType();
167 if (event_type & eBroadcastBitReadThreadGotBytes)
168 {
169 return GetCachedBytes (dst, dst_len);
170 }
171
172 if (event_type & eBroadcastBitReadThreadDidExit)
173 {
174 Disconnect (NULL);
175 break;
176 }
177 }
178 return 0;
179 }
180
181 // We aren't using a read thread, just read the data synchronously in this
182 // thread.
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000183 lldb::ConnectionSP connection_sp (m_connection_sp);
184 if (connection_sp.get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000186 return connection_sp->Read (dst, dst_len, timeout_usec, status, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187 }
188
189 if (error_ptr)
190 error_ptr->SetErrorString("Invalid connection.");
191 status = eConnectionStatusNoConnection;
192 return 0;
193}
194
195
196size_t
197Communication::Write (const void *src, size_t src_len, ConnectionStatus &status, Error *error_ptr)
198{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000199 lldb::ConnectionSP connection_sp (m_connection_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200
Greg Clayton7ec3d402011-01-27 09:02:32 +0000201 Mutex::Locker (m_write_mutex);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000202 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
203 "%p Communication::Write (src = %p, src_len = %zu) connection = %p",
204 this,
205 src,
206 src_len,
207 connection_sp.get());
208
209 if (connection_sp.get())
210 return connection_sp->Write (src, src_len, status, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211
212 if (error_ptr)
213 error_ptr->SetErrorString("Invalid connection.");
214 status = eConnectionStatusNoConnection;
215 return 0;
216}
217
218
219bool
220Communication::StartReadThread (Error *error_ptr)
221{
Greg Clayton1cb64962011-03-24 04:28:38 +0000222 if (error_ptr)
223 error_ptr->Clear();
224
Greg Clayton2da6d492011-02-08 01:34:25 +0000225 if (IS_VALID_LLDB_HOST_THREAD(m_read_thread))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226 return true;
227
228 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
229 "%p Communication::StartReadThread ()", this);
230
231
232 char thread_name[1024];
233 snprintf(thread_name, sizeof(thread_name), "<lldb.comm.%s>", m_broadcaster_name.AsCString());
234
Greg Clayton86c3f342010-09-15 05:19:45 +0000235 m_read_thread_enabled = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236 m_read_thread = Host::ThreadCreate (thread_name, Communication::ReadThread, this, error_ptr);
Greg Clayton2da6d492011-02-08 01:34:25 +0000237 if (!IS_VALID_LLDB_HOST_THREAD(m_read_thread))
Greg Clayton86c3f342010-09-15 05:19:45 +0000238 m_read_thread_enabled = false;
Greg Clayton26661bc2010-07-23 15:43:25 +0000239 return m_read_thread_enabled;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240}
241
242bool
243Communication::StopReadThread (Error *error_ptr)
244{
Greg Clayton2da6d492011-02-08 01:34:25 +0000245 if (!IS_VALID_LLDB_HOST_THREAD(m_read_thread))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246 return true;
247
248 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
249 "%p Communication::StopReadThread ()", this);
250
251 m_read_thread_enabled = false;
252
253 BroadcastEvent (eBroadcastBitReadThreadShouldExit, NULL);
254
255 Host::ThreadCancel (m_read_thread, error_ptr);
256
Stephen Wilsona08cfb12011-01-12 04:22:54 +0000257 bool status = Host::ThreadJoin (m_read_thread, NULL, error_ptr);
Greg Clayton26661bc2010-07-23 15:43:25 +0000258 m_read_thread = LLDB_INVALID_HOST_THREAD;
Stephen Wilsona08cfb12011-01-12 04:22:54 +0000259 return status;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260}
261
262
263size_t
264Communication::GetCachedBytes (void *dst, size_t dst_len)
265{
266 Mutex::Locker locker(m_bytes_mutex);
267 if (m_bytes.size() > 0)
268 {
269 // If DST is NULL and we have a thread, then return the number
270 // of bytes that are available so the caller can call again
271 if (dst == NULL)
272 return m_bytes.size();
273
274 const size_t len = std::min<size_t>(dst_len, m_bytes.size());
275
Greg Clayton471b31c2010-07-20 22:52:08 +0000276 ::memcpy (dst, m_bytes.c_str(), len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277 m_bytes.erase(m_bytes.begin(), m_bytes.begin() + len);
278
279 return len;
280 }
281 return 0;
282}
283
284void
Caroline Ticeefed6132010-11-19 20:47:54 +0000285Communication::AppendBytesToCache (const uint8_t * bytes, size_t len, bool broadcast, ConnectionStatus status)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286{
287 lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION,
288 "%p Communication::AppendBytesToCache (src = %p, src_len = %zu, broadcast = %i)",
289 this, bytes, len, broadcast);
Caroline Tice9fd58502011-02-03 20:02:43 +0000290 if ((bytes == NULL || len == 0)
291 && (status != lldb::eConnectionStatusEndOfFile))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292 return;
293 if (m_callback)
294 {
295 // If the user registered a callback, then call it and do not broadcast
296 m_callback (m_callback_baton, bytes, len);
297 }
298 else
299 {
300 Mutex::Locker locker(m_bytes_mutex);
301 m_bytes.append ((const char *)bytes, len);
302 if (broadcast)
303 BroadcastEventIfUnique (eBroadcastBitReadThreadGotBytes);
304 }
305}
306
307size_t
Greg Clayton73bf5db2011-06-17 01:22:15 +0000308Communication::ReadFromConnection (void *dst,
309 size_t dst_len,
310 uint32_t timeout_usec,
311 ConnectionStatus &status,
312 Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313{
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000314 lldb::ConnectionSP connection_sp (m_connection_sp);
315 if (connection_sp.get())
Greg Clayton73bf5db2011-06-17 01:22:15 +0000316 return connection_sp->Read (dst, dst_len, timeout_usec, status, error_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317 return 0;
318}
319
Caroline Tice82305fc2010-12-02 18:31:56 +0000320bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321Communication::ReadThreadIsRunning ()
322{
Stephen Wilsona08cfb12011-01-12 04:22:54 +0000323 return m_read_thread_enabled;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324}
325
326void *
327Communication::ReadThread (void *p)
328{
329 Communication *comm = (Communication *)p;
330
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000331 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMUNICATION));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332
333 if (log)
334 log->Printf ("%p Communication::ReadThread () thread starting...", p);
335
336 uint8_t buf[1024];
337
338 Error error;
339 ConnectionStatus status = eConnectionStatusSuccess;
340 bool done = false;
341 while (!done && comm->m_read_thread_enabled)
342 {
Peter Collingbourneba23ca02011-06-18 23:52:14 +0000343 size_t bytes_read = comm->ReadFromConnection (buf, sizeof(buf), 5 * TimeValue::MicroSecPerSec, status, &error);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000344 if (bytes_read > 0)
345 comm->AppendBytesToCache (buf, bytes_read, true, status);
346 else if ((bytes_read == 0)
347 && status == eConnectionStatusEndOfFile)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348 {
Greg Clayton73bf5db2011-06-17 01:22:15 +0000349 if (comm->GetCloseOnEOF ())
350 comm->Disconnect ();
351 comm->AppendBytesToCache (buf, bytes_read, true, status);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352 }
353
354 switch (status)
355 {
356 case eConnectionStatusSuccess:
357 break;
358
Greg Clayton7788e5f2010-12-04 02:22:36 +0000359 case eConnectionStatusEndOfFile:
Caroline Tice9fd58502011-02-03 20:02:43 +0000360 if (comm->GetCloseOnEOF())
361 done = true;
362 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363 case eConnectionStatusNoConnection: // No connection
364 case eConnectionStatusLostConnection: // Lost connection while connected to a valid connection
365 done = true;
366 // Fall through...
367 default:
368 case eConnectionStatusError: // Check GetError() for details
369 case eConnectionStatusTimedOut: // Request timed out
Greg Clayton2d4edfb2010-11-06 01:53:30 +0000370 if (log)
Greg Clayton73bf5db2011-06-17 01:22:15 +0000371 error.LogIfError(log.get(), "%p Communication::ReadFromConnection () => status = %i", p, status);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372 break;
373 }
374 }
Caroline Tice20ad3c42010-10-29 21:48:37 +0000375 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMUNICATION);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376 if (log)
377 log->Printf ("%p Communication::ReadThread () thread exiting...", p);
378
379 // Let clients know that this thread is exiting
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000380 comm->BroadcastEvent (eBroadcastBitReadThreadDidExit);
Stephen Wilsona08cfb12011-01-12 04:22:54 +0000381 comm->m_read_thread_enabled = false;
Greg Claytonbfae66a2010-12-12 21:50:57 +0000382 comm->Disconnect();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383 return NULL;
384}
385
386void
387Communication::SetReadThreadBytesReceivedCallback
388(
389 ReadThreadBytesReceived callback,
390 void *callback_baton
391)
392{
393 m_callback = callback;
394 m_callback_baton = callback_baton;
395}
396
397void
398Communication::SetConnection (Connection *connection)
399{
400 StopReadThread(NULL);
401 Disconnect (NULL);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000402 m_connection_sp.reset(connection);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403}
Caroline Ticeceb6b132010-10-26 03:11:13 +0000404
405const char *
406Communication::ConnectionStatusAsCString (lldb::ConnectionStatus status)
407{
408 switch (status)
409 {
410 case eConnectionStatusSuccess: return "success";
411 case eConnectionStatusError: return "error";
412 case eConnectionStatusTimedOut: return "timed out";
413 case eConnectionStatusNoConnection: return "no connection";
414 case eConnectionStatusLostConnection: return "lost connection";
Greg Clayton7a5388b2011-03-20 04:57:14 +0000415 case eConnectionStatusEndOfFile: return "end of file";
Caroline Ticeceb6b132010-10-26 03:11:13 +0000416 }
417
418 static char unknown_state_string[64];
419 snprintf(unknown_state_string, sizeof (unknown_state_string), "ConnectionStatus = %i", status);
420 return unknown_state_string;
421}