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); |
| 45 | } |
| 46 | |
| 47 | //---------------------------------------------------------------------- |
| 48 | // Destructor |
| 49 | //---------------------------------------------------------------------- |
| 50 | Communication::~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 | |
| 58 | void |
| 59 | Communication::Clear() |
| 60 | { |
| 61 | StopReadThread (NULL); |
| 62 | Disconnect (NULL); |
| 63 | } |
| 64 | |
| 65 | ConnectionStatus |
| 66 | Communication::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 Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 70 | lldb::ConnectionSP connection_sp (m_connection_sp); |
| 71 | if (connection_sp.get()) |
| 72 | return connection_sp->BytesAvailable (timeout_usec, error_ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | if (error_ptr) |
| 74 | error_ptr->SetErrorString("Invalid connection."); |
| 75 | return eConnectionStatusNoConnection; |
| 76 | } |
| 77 | |
| 78 | ConnectionStatus |
| 79 | Communication::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 Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 85 | lldb::ConnectionSP connection_sp (m_connection_sp); |
| 86 | if (connection_sp.get()) |
| 87 | return connection_sp->Connect (url, error_ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 88 | if (error_ptr) |
| 89 | error_ptr->SetErrorString("Invalid connection."); |
| 90 | return eConnectionStatusNoConnection; |
| 91 | } |
| 92 | |
| 93 | ConnectionStatus |
| 94 | Communication::Disconnect (Error *error_ptr) |
| 95 | { |
| 96 | lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::Disconnect ()", this); |
| 97 | |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 98 | lldb::ConnectionSP connection_sp (m_connection_sp); |
| 99 | if (connection_sp.get()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | { |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 101 | ConnectionStatus status = connection_sp->Disconnect (error_ptr); |
| 102 | // We currently don't protect connection_sp with any mutex for |
Greg Clayton | bfae66a | 2010-12-12 21:50:57 +0000 | [diff] [blame] | 103 | // 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 Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 111 | //connection_sp.reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 112 | return status; |
| 113 | } |
| 114 | return eConnectionStatusNoConnection; |
| 115 | } |
| 116 | |
| 117 | bool |
| 118 | Communication::IsConnected () const |
| 119 | { |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 120 | lldb::ConnectionSP connection_sp (m_connection_sp); |
| 121 | if (connection_sp.get()) |
| 122 | return connection_sp->IsConnected (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 123 | return false; |
| 124 | } |
| 125 | |
| 126 | bool |
| 127 | Communication::HasConnection () const |
| 128 | { |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 129 | return m_connection_sp.get() != NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | size_t |
| 133 | Communication::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 Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 136 | "%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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | |
Stephen Wilson | a08cfb1 | 2011-01-12 04:22:54 +0000 | [diff] [blame] | 143 | if (m_read_thread_enabled) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 144 | { |
| 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 Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 153 | if (m_connection_sp.get() == NULL) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 154 | { |
| 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 Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 190 | lldb::ConnectionSP connection_sp (m_connection_sp); |
| 191 | if (connection_sp.get()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 192 | { |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 193 | status = connection_sp->BytesAvailable (timeout_usec, error_ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 194 | if (status == eConnectionStatusSuccess) |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 195 | return connection_sp->Read (dst, dst_len, status, error_ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | if (error_ptr) |
| 199 | error_ptr->SetErrorString("Invalid connection."); |
| 200 | status = eConnectionStatusNoConnection; |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | size_t |
| 206 | Communication::Write (const void *src, size_t src_len, ConnectionStatus &status, Error *error_ptr) |
| 207 | { |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 208 | lldb::ConnectionSP connection_sp (m_connection_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 209 | |
Greg Clayton | 7ec3d40 | 2011-01-27 09:02:32 +0000 | [diff] [blame] | 210 | Mutex::Locker (m_write_mutex); |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 211 | 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 220 | |
| 221 | if (error_ptr) |
| 222 | error_ptr->SetErrorString("Invalid connection."); |
| 223 | status = eConnectionStatusNoConnection; |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | bool |
| 229 | Communication::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 Clayton | 86c3f34 | 2010-09-15 05:19:45 +0000 | [diff] [blame] | 241 | m_read_thread_enabled = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 242 | m_read_thread = Host::ThreadCreate (thread_name, Communication::ReadThread, this, error_ptr); |
Greg Clayton | 86c3f34 | 2010-09-15 05:19:45 +0000 | [diff] [blame] | 243 | if (m_read_thread == LLDB_INVALID_HOST_THREAD) |
| 244 | m_read_thread_enabled = false; |
Greg Clayton | 26661bc | 2010-07-23 15:43:25 +0000 | [diff] [blame] | 245 | return m_read_thread_enabled; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | bool |
| 249 | Communication::StopReadThread (Error *error_ptr) |
| 250 | { |
Eli Friedman | 8878f87 | 2010-07-09 22:53:18 +0000 | [diff] [blame] | 251 | if (m_read_thread == LLDB_INVALID_HOST_THREAD) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 252 | 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 Wilson | a08cfb1 | 2011-01-12 04:22:54 +0000 | [diff] [blame] | 263 | bool status = Host::ThreadJoin (m_read_thread, NULL, error_ptr); |
Greg Clayton | 26661bc | 2010-07-23 15:43:25 +0000 | [diff] [blame] | 264 | m_read_thread = LLDB_INVALID_HOST_THREAD; |
Stephen Wilson | a08cfb1 | 2011-01-12 04:22:54 +0000 | [diff] [blame] | 265 | return status; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | |
| 269 | size_t |
| 270 | Communication::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 Clayton | 471b31c | 2010-07-20 22:52:08 +0000 | [diff] [blame] | 282 | ::memcpy (dst, m_bytes.c_str(), len); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 283 | m_bytes.erase(m_bytes.begin(), m_bytes.begin() + len); |
| 284 | |
| 285 | return len; |
| 286 | } |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | void |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 291 | 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] | 292 | { |
| 293 | lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, |
| 294 | "%p Communication::AppendBytesToCache (src = %p, src_len = %zu, broadcast = %i)", |
| 295 | this, bytes, len, broadcast); |
Caroline Tice | 9fd5850 | 2011-02-03 20:02:43 +0000 | [diff] [blame^] | 296 | if ((bytes == NULL || len == 0) |
| 297 | && (status != lldb::eConnectionStatusEndOfFile)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 298 | 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 | |
| 313 | size_t |
| 314 | Communication::ReadFromConnection (void *dst, size_t dst_len, ConnectionStatus &status, Error *error_ptr) |
| 315 | { |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 316 | lldb::ConnectionSP connection_sp (m_connection_sp); |
| 317 | if (connection_sp.get()) |
| 318 | return connection_sp->Read (dst, dst_len, status, error_ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 319 | return 0; |
| 320 | } |
| 321 | |
Caroline Tice | 82305fc | 2010-12-02 18:31:56 +0000 | [diff] [blame] | 322 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 323 | Communication::ReadThreadIsRunning () |
| 324 | { |
Stephen Wilson | a08cfb1 | 2011-01-12 04:22:54 +0000 | [diff] [blame] | 325 | return m_read_thread_enabled; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | void * |
| 329 | Communication::ReadThread (void *p) |
| 330 | { |
| 331 | Communication *comm = (Communication *)p; |
| 332 | |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 333 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMUNICATION)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 334 | |
| 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 Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 351 | comm->AppendBytesToCache (buf, bytes_read, true, status); |
Caroline Tice | 9fd5850 | 2011-02-03 20:02:43 +0000 | [diff] [blame^] | 352 | 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 Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | switch (status) |
| 362 | { |
| 363 | case eConnectionStatusSuccess: |
| 364 | break; |
| 365 | |
Greg Clayton | 7788e5f | 2010-12-04 02:22:36 +0000 | [diff] [blame] | 366 | case eConnectionStatusEndOfFile: |
Caroline Tice | 9fd5850 | 2011-02-03 20:02:43 +0000 | [diff] [blame^] | 367 | if (comm->GetCloseOnEOF()) |
| 368 | done = true; |
| 369 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 370 | 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 Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 377 | if (log) |
| 378 | error.LogIfError(log.get(), "%p Communication::BytesAvailable () => status = %i", p, status); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 379 | break; |
| 380 | } |
| 381 | } |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 382 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMUNICATION); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 383 | if (log) |
| 384 | log->Printf ("%p Communication::ReadThread () thread exiting...", p); |
| 385 | |
| 386 | // Let clients know that this thread is exiting |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 387 | comm->BroadcastEvent (eBroadcastBitReadThreadDidExit); |
Stephen Wilson | a08cfb1 | 2011-01-12 04:22:54 +0000 | [diff] [blame] | 388 | comm->m_read_thread_enabled = false; |
Greg Clayton | bfae66a | 2010-12-12 21:50:57 +0000 | [diff] [blame] | 389 | comm->Disconnect(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 390 | return NULL; |
| 391 | } |
| 392 | |
| 393 | void |
| 394 | Communication::SetReadThreadBytesReceivedCallback |
| 395 | ( |
| 396 | ReadThreadBytesReceived callback, |
| 397 | void *callback_baton |
| 398 | ) |
| 399 | { |
| 400 | m_callback = callback; |
| 401 | m_callback_baton = callback_baton; |
| 402 | } |
| 403 | |
| 404 | void |
| 405 | Communication::SetConnection (Connection *connection) |
| 406 | { |
| 407 | StopReadThread(NULL); |
| 408 | Disconnect (NULL); |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 409 | m_connection_sp.reset(connection); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 410 | } |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 411 | |
| 412 | const char * |
| 413 | Communication::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 | } |