Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 Clayton | 7fb56d0 | 2011-02-01 01:31:41 +0000 | [diff] [blame] | 20 | #include "lldb/Host/Host.h" |
Eli Friedman | 8896697 | 2010-06-09 08:50:27 +0000 | [diff] [blame] | 21 | #include <string.h> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace lldb; |
| 24 | using namespace lldb_private; |
| 25 | |
| 26 | //---------------------------------------------------------------------- |
| 27 | // Constructor |
| 28 | //---------------------------------------------------------------------- |
Greg Clayton | d46c87a | 2010-12-04 02:39:47 +0000 | [diff] [blame] | 29 | Communication::Communication(const char *name) : |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | Broadcaster (name), |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 31 | m_connection_sp (), |
Eli Friedman | 8878f87 | 2010-07-09 22:53:18 +0000 | [diff] [blame] | 32 | m_read_thread (LLDB_INVALID_HOST_THREAD), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 33 | m_read_thread_enabled (false), |
| 34 | m_bytes(), |
| 35 | m_bytes_mutex (Mutex::eMutexTypeRecursive), |
Greg Clayton | 7ec3d40 | 2011-01-27 09:02:32 +0000 | [diff] [blame] | 36 | m_write_mutex (Mutex::eMutexTypeNormal), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | m_callback (NULL), |
Caroline Tice | 82305fc | 2010-12-02 18:31:56 +0000 | [diff] [blame] | 38 | m_callback_baton (NULL), |
Greg Clayton | d46c87a | 2010-12-04 02:39:47 +0000 | [diff] [blame] | 39 | m_close_on_eof (true) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | |
| 41 | { |
| 42 | lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_COMMUNICATION, |
| 43 | "%p Communication::Communication (name = %s)", |
| 44 | this, name); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 45 | |
| 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | //---------------------------------------------------------------------- |
| 54 | // Destructor |
| 55 | //---------------------------------------------------------------------- |
| 56 | Communication::~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 | |
| 64 | void |
| 65 | Communication::Clear() |
| 66 | { |
| 67 | StopReadThread (NULL); |
| 68 | Disconnect (NULL); |
| 69 | } |
| 70 | |
| 71 | ConnectionStatus |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 72 | Communication::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 Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 78 | lldb::ConnectionSP connection_sp (m_connection_sp); |
| 79 | if (connection_sp.get()) |
| 80 | return connection_sp->Connect (url, error_ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | if (error_ptr) |
| 82 | error_ptr->SetErrorString("Invalid connection."); |
| 83 | return eConnectionStatusNoConnection; |
| 84 | } |
| 85 | |
| 86 | ConnectionStatus |
| 87 | Communication::Disconnect (Error *error_ptr) |
| 88 | { |
| 89 | lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::Disconnect ()", this); |
| 90 | |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 91 | lldb::ConnectionSP connection_sp (m_connection_sp); |
| 92 | if (connection_sp.get()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 93 | { |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 94 | ConnectionStatus status = connection_sp->Disconnect (error_ptr); |
| 95 | // We currently don't protect connection_sp with any mutex for |
Greg Clayton | bfae66a | 2010-12-12 21:50:57 +0000 | [diff] [blame] | 96 | // 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 Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 104 | //connection_sp.reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 105 | return status; |
| 106 | } |
| 107 | return eConnectionStatusNoConnection; |
| 108 | } |
| 109 | |
| 110 | bool |
| 111 | Communication::IsConnected () const |
| 112 | { |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 113 | lldb::ConnectionSP connection_sp (m_connection_sp); |
| 114 | if (connection_sp.get()) |
| 115 | return connection_sp->IsConnected (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 116 | return false; |
| 117 | } |
| 118 | |
| 119 | bool |
| 120 | Communication::HasConnection () const |
| 121 | { |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 122 | return m_connection_sp.get() != NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | size_t |
| 126 | Communication::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 Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 129 | "%p Communication::Read (dst = %p, dst_len = %zu, timeout = %u usec) connection = %p", |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 130 | this, |
| 131 | dst, |
| 132 | dst_len, |
| 133 | timeout_usec, |
| 134 | m_connection_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 135 | |
Stephen Wilson | a08cfb1 | 2011-01-12 04:22:54 +0000 | [diff] [blame] | 136 | if (m_read_thread_enabled) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 137 | { |
| 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 Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 146 | if (m_connection_sp.get() == NULL) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 147 | { |
| 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 Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 183 | lldb::ConnectionSP connection_sp (m_connection_sp); |
| 184 | if (connection_sp.get()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 185 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 186 | return connection_sp->Read (dst, dst_len, timeout_usec, status, error_ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | if (error_ptr) |
| 190 | error_ptr->SetErrorString("Invalid connection."); |
| 191 | status = eConnectionStatusNoConnection; |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | size_t |
| 197 | Communication::Write (const void *src, size_t src_len, ConnectionStatus &status, Error *error_ptr) |
| 198 | { |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 199 | lldb::ConnectionSP connection_sp (m_connection_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 200 | |
Greg Clayton | 7ec3d40 | 2011-01-27 09:02:32 +0000 | [diff] [blame] | 201 | Mutex::Locker (m_write_mutex); |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 202 | 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 211 | |
| 212 | if (error_ptr) |
| 213 | error_ptr->SetErrorString("Invalid connection."); |
| 214 | status = eConnectionStatusNoConnection; |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | bool |
| 220 | Communication::StartReadThread (Error *error_ptr) |
| 221 | { |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 222 | if (error_ptr) |
| 223 | error_ptr->Clear(); |
| 224 | |
Greg Clayton | 2da6d49 | 2011-02-08 01:34:25 +0000 | [diff] [blame] | 225 | if (IS_VALID_LLDB_HOST_THREAD(m_read_thread)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 226 | 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 Clayton | 86c3f34 | 2010-09-15 05:19:45 +0000 | [diff] [blame] | 235 | m_read_thread_enabled = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 236 | m_read_thread = Host::ThreadCreate (thread_name, Communication::ReadThread, this, error_ptr); |
Greg Clayton | 2da6d49 | 2011-02-08 01:34:25 +0000 | [diff] [blame] | 237 | if (!IS_VALID_LLDB_HOST_THREAD(m_read_thread)) |
Greg Clayton | 86c3f34 | 2010-09-15 05:19:45 +0000 | [diff] [blame] | 238 | m_read_thread_enabled = false; |
Greg Clayton | 26661bc | 2010-07-23 15:43:25 +0000 | [diff] [blame] | 239 | return m_read_thread_enabled; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | bool |
| 243 | Communication::StopReadThread (Error *error_ptr) |
| 244 | { |
Greg Clayton | 2da6d49 | 2011-02-08 01:34:25 +0000 | [diff] [blame] | 245 | if (!IS_VALID_LLDB_HOST_THREAD(m_read_thread)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 246 | 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 Wilson | a08cfb1 | 2011-01-12 04:22:54 +0000 | [diff] [blame] | 257 | bool status = Host::ThreadJoin (m_read_thread, NULL, error_ptr); |
Greg Clayton | 26661bc | 2010-07-23 15:43:25 +0000 | [diff] [blame] | 258 | m_read_thread = LLDB_INVALID_HOST_THREAD; |
Stephen Wilson | a08cfb1 | 2011-01-12 04:22:54 +0000 | [diff] [blame] | 259 | return status; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
| 263 | size_t |
| 264 | Communication::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 Clayton | 471b31c | 2010-07-20 22:52:08 +0000 | [diff] [blame] | 276 | ::memcpy (dst, m_bytes.c_str(), len); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 277 | m_bytes.erase(m_bytes.begin(), m_bytes.begin() + len); |
| 278 | |
| 279 | return len; |
| 280 | } |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | void |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 285 | Communication::AppendBytesToCache (const uint8_t * bytes, size_t len, bool broadcast, ConnectionStatus status) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 286 | { |
| 287 | lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, |
| 288 | "%p Communication::AppendBytesToCache (src = %p, src_len = %zu, broadcast = %i)", |
| 289 | this, bytes, len, broadcast); |
Caroline Tice | 9fd5850 | 2011-02-03 20:02:43 +0000 | [diff] [blame] | 290 | if ((bytes == NULL || len == 0) |
| 291 | && (status != lldb::eConnectionStatusEndOfFile)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 292 | 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 | |
| 307 | size_t |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 308 | Communication::ReadFromConnection (void *dst, |
| 309 | size_t dst_len, |
| 310 | uint32_t timeout_usec, |
| 311 | ConnectionStatus &status, |
| 312 | Error *error_ptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 313 | { |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 314 | lldb::ConnectionSP connection_sp (m_connection_sp); |
| 315 | if (connection_sp.get()) |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 316 | return connection_sp->Read (dst, dst_len, timeout_usec, status, error_ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | |
Caroline Tice | 82305fc | 2010-12-02 18:31:56 +0000 | [diff] [blame] | 320 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 321 | Communication::ReadThreadIsRunning () |
| 322 | { |
Stephen Wilson | a08cfb1 | 2011-01-12 04:22:54 +0000 | [diff] [blame] | 323 | return m_read_thread_enabled; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | void * |
| 327 | Communication::ReadThread (void *p) |
| 328 | { |
| 329 | Communication *comm = (Communication *)p; |
| 330 | |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 331 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMUNICATION)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 332 | |
| 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 Collingbourne | ba23ca0 | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 343 | size_t bytes_read = comm->ReadFromConnection (buf, sizeof(buf), 5 * TimeValue::MicroSecPerSec, status, &error); |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 344 | if (bytes_read > 0) |
| 345 | comm->AppendBytesToCache (buf, bytes_read, true, status); |
| 346 | else if ((bytes_read == 0) |
| 347 | && status == eConnectionStatusEndOfFile) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 348 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 349 | if (comm->GetCloseOnEOF ()) |
| 350 | comm->Disconnect (); |
| 351 | comm->AppendBytesToCache (buf, bytes_read, true, status); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | switch (status) |
| 355 | { |
| 356 | case eConnectionStatusSuccess: |
| 357 | break; |
| 358 | |
Greg Clayton | 7788e5f | 2010-12-04 02:22:36 +0000 | [diff] [blame] | 359 | case eConnectionStatusEndOfFile: |
Caroline Tice | 9fd5850 | 2011-02-03 20:02:43 +0000 | [diff] [blame] | 360 | if (comm->GetCloseOnEOF()) |
| 361 | done = true; |
| 362 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 363 | 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 Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 370 | if (log) |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame^] | 371 | error.LogIfError(log.get(), |
| 372 | "%p Communication::ReadFromConnection () => status = %s", |
| 373 | p, |
| 374 | Communication::ConnectionStatusAsCString (status)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 375 | break; |
| 376 | } |
| 377 | } |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 378 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMUNICATION); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 379 | if (log) |
| 380 | log->Printf ("%p Communication::ReadThread () thread exiting...", p); |
| 381 | |
| 382 | // Let clients know that this thread is exiting |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 383 | comm->BroadcastEvent (eBroadcastBitReadThreadDidExit); |
Stephen Wilson | a08cfb1 | 2011-01-12 04:22:54 +0000 | [diff] [blame] | 384 | comm->m_read_thread_enabled = false; |
Greg Clayton | bfae66a | 2010-12-12 21:50:57 +0000 | [diff] [blame] | 385 | comm->Disconnect(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 386 | return NULL; |
| 387 | } |
| 388 | |
| 389 | void |
| 390 | Communication::SetReadThreadBytesReceivedCallback |
| 391 | ( |
| 392 | ReadThreadBytesReceived callback, |
| 393 | void *callback_baton |
| 394 | ) |
| 395 | { |
| 396 | m_callback = callback; |
| 397 | m_callback_baton = callback_baton; |
| 398 | } |
| 399 | |
| 400 | void |
| 401 | Communication::SetConnection (Connection *connection) |
| 402 | { |
| 403 | StopReadThread(NULL); |
| 404 | Disconnect (NULL); |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 405 | m_connection_sp.reset(connection); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 406 | } |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 407 | |
| 408 | const char * |
| 409 | Communication::ConnectionStatusAsCString (lldb::ConnectionStatus status) |
| 410 | { |
| 411 | switch (status) |
| 412 | { |
| 413 | case eConnectionStatusSuccess: return "success"; |
| 414 | case eConnectionStatusError: return "error"; |
| 415 | case eConnectionStatusTimedOut: return "timed out"; |
| 416 | case eConnectionStatusNoConnection: return "no connection"; |
| 417 | case eConnectionStatusLostConnection: return "lost connection"; |
Greg Clayton | 7a5388b | 2011-03-20 04:57:14 +0000 | [diff] [blame] | 418 | case eConnectionStatusEndOfFile: return "end of file"; |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | static char unknown_state_string[64]; |
| 422 | snprintf(unknown_state_string, sizeof (unknown_state_string), "ConnectionStatus = %i", status); |
| 423 | return unknown_state_string; |
| 424 | } |