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 | { |
Greg Clayton | 756f8ae | 2011-08-19 23:28:37 +0000 | [diff] [blame] | 67 | SetReadThreadBytesReceivedCallback (NULL, NULL); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 68 | Disconnect (NULL); |
Greg Clayton | 74d4193 | 2012-01-31 04:56:17 +0000 | [diff] [blame^] | 69 | StopReadThread (NULL); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | ConnectionStatus |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | Communication::Connect (const char *url, Error *error_ptr) |
| 74 | { |
| 75 | Clear(); |
| 76 | |
| 77 | lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::Connect (url = %s)", this, url); |
| 78 | |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 79 | lldb::ConnectionSP connection_sp (m_connection_sp); |
| 80 | if (connection_sp.get()) |
| 81 | return connection_sp->Connect (url, error_ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | if (error_ptr) |
| 83 | error_ptr->SetErrorString("Invalid connection."); |
| 84 | return eConnectionStatusNoConnection; |
| 85 | } |
| 86 | |
| 87 | ConnectionStatus |
| 88 | Communication::Disconnect (Error *error_ptr) |
| 89 | { |
| 90 | lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, "%p Communication::Disconnect ()", this); |
| 91 | |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 92 | lldb::ConnectionSP connection_sp (m_connection_sp); |
| 93 | if (connection_sp.get()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | { |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 95 | ConnectionStatus status = connection_sp->Disconnect (error_ptr); |
| 96 | // We currently don't protect connection_sp with any mutex for |
Greg Clayton | bfae66a | 2010-12-12 21:50:57 +0000 | [diff] [blame] | 97 | // multi-threaded environments. So lets not nuke our connection class |
| 98 | // without putting some multi-threaded protections in. We also probably |
| 99 | // don't want to pay for the overhead it might cause if every time we |
| 100 | // access the connection we have to take a lock. |
| 101 | // |
| 102 | // This auto_ptr will cleanup after itself when this object goes away, |
| 103 | // so there is no need to currently have it destroy itself immediately |
| 104 | // upon disconnnect. |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 105 | //connection_sp.reset(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | return status; |
| 107 | } |
| 108 | return eConnectionStatusNoConnection; |
| 109 | } |
| 110 | |
| 111 | bool |
| 112 | Communication::IsConnected () const |
| 113 | { |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 114 | lldb::ConnectionSP connection_sp (m_connection_sp); |
| 115 | if (connection_sp.get()) |
| 116 | return connection_sp->IsConnected (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 117 | return false; |
| 118 | } |
| 119 | |
| 120 | bool |
| 121 | Communication::HasConnection () const |
| 122 | { |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 123 | return m_connection_sp.get() != NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | size_t |
| 127 | Communication::Read (void *dst, size_t dst_len, uint32_t timeout_usec, ConnectionStatus &status, Error *error_ptr) |
| 128 | { |
| 129 | lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 130 | "%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] | 131 | this, |
| 132 | dst, |
| 133 | dst_len, |
| 134 | timeout_usec, |
| 135 | m_connection_sp.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 136 | |
Stephen Wilson | a08cfb1 | 2011-01-12 04:22:54 +0000 | [diff] [blame] | 137 | if (m_read_thread_enabled) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 138 | { |
| 139 | // We have a dedicated read thread that is getting data for us |
| 140 | size_t cached_bytes = GetCachedBytes (dst, dst_len); |
| 141 | if (cached_bytes > 0 || timeout_usec == 0) |
| 142 | { |
| 143 | status = eConnectionStatusSuccess; |
| 144 | return cached_bytes; |
| 145 | } |
| 146 | |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 147 | if (m_connection_sp.get() == NULL) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 148 | { |
| 149 | if (error_ptr) |
| 150 | error_ptr->SetErrorString("Invalid connection."); |
| 151 | status = eConnectionStatusNoConnection; |
| 152 | return 0; |
| 153 | } |
| 154 | // Set the timeout appropriately |
| 155 | TimeValue timeout_time; |
| 156 | if (timeout_usec != UINT32_MAX) |
| 157 | { |
| 158 | timeout_time = TimeValue::Now(); |
| 159 | timeout_time.OffsetWithMicroSeconds (timeout_usec); |
| 160 | } |
| 161 | |
| 162 | Listener listener ("Communication::Read"); |
| 163 | listener.StartListeningForEvents (this, eBroadcastBitReadThreadGotBytes | eBroadcastBitReadThreadDidExit); |
| 164 | EventSP event_sp; |
| 165 | while (listener.WaitForEvent (timeout_time.IsValid() ? &timeout_time : NULL, event_sp)) |
| 166 | { |
| 167 | const uint32_t event_type = event_sp->GetType(); |
| 168 | if (event_type & eBroadcastBitReadThreadGotBytes) |
| 169 | { |
| 170 | return GetCachedBytes (dst, dst_len); |
| 171 | } |
| 172 | |
| 173 | if (event_type & eBroadcastBitReadThreadDidExit) |
| 174 | { |
| 175 | Disconnect (NULL); |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | // We aren't using a read thread, just read the data synchronously in this |
| 183 | // thread. |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 184 | lldb::ConnectionSP connection_sp (m_connection_sp); |
| 185 | if (connection_sp.get()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 186 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 187 | return connection_sp->Read (dst, dst_len, timeout_usec, status, error_ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | if (error_ptr) |
| 191 | error_ptr->SetErrorString("Invalid connection."); |
| 192 | status = eConnectionStatusNoConnection; |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | size_t |
| 198 | Communication::Write (const void *src, size_t src_len, ConnectionStatus &status, Error *error_ptr) |
| 199 | { |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 200 | lldb::ConnectionSP connection_sp (m_connection_sp); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 201 | |
Greg Clayton | 7ec3d40 | 2011-01-27 09:02:32 +0000 | [diff] [blame] | 202 | Mutex::Locker (m_write_mutex); |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 203 | lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, |
| 204 | "%p Communication::Write (src = %p, src_len = %zu) connection = %p", |
| 205 | this, |
| 206 | src, |
| 207 | src_len, |
| 208 | connection_sp.get()); |
| 209 | |
| 210 | if (connection_sp.get()) |
| 211 | return connection_sp->Write (src, src_len, status, error_ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 212 | |
| 213 | if (error_ptr) |
| 214 | error_ptr->SetErrorString("Invalid connection."); |
| 215 | status = eConnectionStatusNoConnection; |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | |
| 220 | bool |
| 221 | Communication::StartReadThread (Error *error_ptr) |
| 222 | { |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 223 | if (error_ptr) |
| 224 | error_ptr->Clear(); |
| 225 | |
Greg Clayton | 2da6d49 | 2011-02-08 01:34:25 +0000 | [diff] [blame] | 226 | if (IS_VALID_LLDB_HOST_THREAD(m_read_thread)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 227 | return true; |
| 228 | |
| 229 | lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, |
| 230 | "%p Communication::StartReadThread ()", this); |
| 231 | |
| 232 | |
| 233 | char thread_name[1024]; |
| 234 | snprintf(thread_name, sizeof(thread_name), "<lldb.comm.%s>", m_broadcaster_name.AsCString()); |
| 235 | |
Greg Clayton | 86c3f34 | 2010-09-15 05:19:45 +0000 | [diff] [blame] | 236 | m_read_thread_enabled = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 237 | m_read_thread = Host::ThreadCreate (thread_name, Communication::ReadThread, this, error_ptr); |
Greg Clayton | 2da6d49 | 2011-02-08 01:34:25 +0000 | [diff] [blame] | 238 | if (!IS_VALID_LLDB_HOST_THREAD(m_read_thread)) |
Greg Clayton | 86c3f34 | 2010-09-15 05:19:45 +0000 | [diff] [blame] | 239 | m_read_thread_enabled = false; |
Greg Clayton | 26661bc | 2010-07-23 15:43:25 +0000 | [diff] [blame] | 240 | return m_read_thread_enabled; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | bool |
| 244 | Communication::StopReadThread (Error *error_ptr) |
| 245 | { |
Greg Clayton | 2da6d49 | 2011-02-08 01:34:25 +0000 | [diff] [blame] | 246 | if (!IS_VALID_LLDB_HOST_THREAD(m_read_thread)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 247 | return true; |
| 248 | |
| 249 | lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, |
| 250 | "%p Communication::StopReadThread ()", this); |
| 251 | |
| 252 | m_read_thread_enabled = false; |
| 253 | |
| 254 | BroadcastEvent (eBroadcastBitReadThreadShouldExit, NULL); |
| 255 | |
Greg Clayton | 74d4193 | 2012-01-31 04:56:17 +0000 | [diff] [blame^] | 256 | //Host::ThreadCancel (m_read_thread, error_ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 257 | |
Stephen Wilson | a08cfb1 | 2011-01-12 04:22:54 +0000 | [diff] [blame] | 258 | bool status = Host::ThreadJoin (m_read_thread, NULL, error_ptr); |
Greg Clayton | 26661bc | 2010-07-23 15:43:25 +0000 | [diff] [blame] | 259 | m_read_thread = LLDB_INVALID_HOST_THREAD; |
Stephen Wilson | a08cfb1 | 2011-01-12 04:22:54 +0000 | [diff] [blame] | 260 | return status; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | |
| 264 | size_t |
| 265 | Communication::GetCachedBytes (void *dst, size_t dst_len) |
| 266 | { |
| 267 | Mutex::Locker locker(m_bytes_mutex); |
| 268 | if (m_bytes.size() > 0) |
| 269 | { |
| 270 | // If DST is NULL and we have a thread, then return the number |
| 271 | // of bytes that are available so the caller can call again |
| 272 | if (dst == NULL) |
| 273 | return m_bytes.size(); |
| 274 | |
| 275 | const size_t len = std::min<size_t>(dst_len, m_bytes.size()); |
| 276 | |
Greg Clayton | 471b31c | 2010-07-20 22:52:08 +0000 | [diff] [blame] | 277 | ::memcpy (dst, m_bytes.c_str(), len); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 278 | m_bytes.erase(m_bytes.begin(), m_bytes.begin() + len); |
| 279 | |
| 280 | return len; |
| 281 | } |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | void |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 286 | 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] | 287 | { |
| 288 | lldb_private::LogIfAnyCategoriesSet (LIBLLDB_LOG_COMMUNICATION, |
| 289 | "%p Communication::AppendBytesToCache (src = %p, src_len = %zu, broadcast = %i)", |
| 290 | this, bytes, len, broadcast); |
Caroline Tice | 9fd5850 | 2011-02-03 20:02:43 +0000 | [diff] [blame] | 291 | if ((bytes == NULL || len == 0) |
| 292 | && (status != lldb::eConnectionStatusEndOfFile)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 293 | return; |
| 294 | if (m_callback) |
| 295 | { |
| 296 | // If the user registered a callback, then call it and do not broadcast |
| 297 | m_callback (m_callback_baton, bytes, len); |
| 298 | } |
Greg Clayton | 756f8ae | 2011-08-19 23:28:37 +0000 | [diff] [blame] | 299 | else if (bytes != NULL && len > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 300 | { |
| 301 | Mutex::Locker locker(m_bytes_mutex); |
| 302 | m_bytes.append ((const char *)bytes, len); |
| 303 | if (broadcast) |
| 304 | BroadcastEventIfUnique (eBroadcastBitReadThreadGotBytes); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | size_t |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 309 | Communication::ReadFromConnection (void *dst, |
| 310 | size_t dst_len, |
| 311 | uint32_t timeout_usec, |
| 312 | ConnectionStatus &status, |
| 313 | Error *error_ptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 314 | { |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 315 | lldb::ConnectionSP connection_sp (m_connection_sp); |
| 316 | if (connection_sp.get()) |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 317 | return connection_sp->Read (dst, dst_len, timeout_usec, status, error_ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 318 | return 0; |
| 319 | } |
| 320 | |
Caroline Tice | 82305fc | 2010-12-02 18:31:56 +0000 | [diff] [blame] | 321 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 322 | Communication::ReadThreadIsRunning () |
| 323 | { |
Stephen Wilson | a08cfb1 | 2011-01-12 04:22:54 +0000 | [diff] [blame] | 324 | return m_read_thread_enabled; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | void * |
| 328 | Communication::ReadThread (void *p) |
| 329 | { |
| 330 | Communication *comm = (Communication *)p; |
| 331 | |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 332 | LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMUNICATION)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 333 | |
| 334 | if (log) |
| 335 | log->Printf ("%p Communication::ReadThread () thread starting...", p); |
| 336 | |
| 337 | uint8_t buf[1024]; |
| 338 | |
| 339 | Error error; |
| 340 | ConnectionStatus status = eConnectionStatusSuccess; |
| 341 | bool done = false; |
| 342 | while (!done && comm->m_read_thread_enabled) |
| 343 | { |
Peter Collingbourne | ba23ca0 | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 344 | 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] | 345 | if (bytes_read > 0) |
| 346 | comm->AppendBytesToCache (buf, bytes_read, true, status); |
| 347 | else if ((bytes_read == 0) |
| 348 | && status == eConnectionStatusEndOfFile) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 349 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 350 | if (comm->GetCloseOnEOF ()) |
| 351 | comm->Disconnect (); |
| 352 | comm->AppendBytesToCache (buf, bytes_read, true, status); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | switch (status) |
| 356 | { |
| 357 | case eConnectionStatusSuccess: |
| 358 | break; |
| 359 | |
Greg Clayton | 7788e5f | 2010-12-04 02:22:36 +0000 | [diff] [blame] | 360 | case eConnectionStatusEndOfFile: |
Caroline Tice | 9fd5850 | 2011-02-03 20:02:43 +0000 | [diff] [blame] | 361 | if (comm->GetCloseOnEOF()) |
| 362 | done = true; |
| 363 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 364 | case eConnectionStatusNoConnection: // No connection |
| 365 | case eConnectionStatusLostConnection: // Lost connection while connected to a valid connection |
| 366 | done = true; |
| 367 | // Fall through... |
| 368 | default: |
| 369 | case eConnectionStatusError: // Check GetError() for details |
| 370 | case eConnectionStatusTimedOut: // Request timed out |
Greg Clayton | 2d4edfb | 2010-11-06 01:53:30 +0000 | [diff] [blame] | 371 | if (log) |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 372 | error.LogIfError(log.get(), |
| 373 | "%p Communication::ReadFromConnection () => status = %s", |
| 374 | p, |
| 375 | Communication::ConnectionStatusAsCString (status)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 376 | break; |
| 377 | } |
| 378 | } |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 379 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMUNICATION); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 380 | if (log) |
| 381 | log->Printf ("%p Communication::ReadThread () thread exiting...", p); |
| 382 | |
| 383 | // Let clients know that this thread is exiting |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 384 | comm->BroadcastEvent (eBroadcastBitReadThreadDidExit); |
Stephen Wilson | a08cfb1 | 2011-01-12 04:22:54 +0000 | [diff] [blame] | 385 | comm->m_read_thread_enabled = false; |
Greg Clayton | 74d4193 | 2012-01-31 04:56:17 +0000 | [diff] [blame^] | 386 | comm->m_read_thread = LLDB_INVALID_HOST_THREAD; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 387 | return NULL; |
| 388 | } |
| 389 | |
| 390 | void |
| 391 | Communication::SetReadThreadBytesReceivedCallback |
| 392 | ( |
| 393 | ReadThreadBytesReceived callback, |
| 394 | void *callback_baton |
| 395 | ) |
| 396 | { |
| 397 | m_callback = callback; |
| 398 | m_callback_baton = callback_baton; |
| 399 | } |
| 400 | |
| 401 | void |
| 402 | Communication::SetConnection (Connection *connection) |
| 403 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 404 | Disconnect (NULL); |
Greg Clayton | 74d4193 | 2012-01-31 04:56:17 +0000 | [diff] [blame^] | 405 | StopReadThread(NULL); |
Greg Clayton | 8b2fe6d | 2010-12-14 02:59:59 +0000 | [diff] [blame] | 406 | m_connection_sp.reset(connection); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 407 | } |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 408 | |
| 409 | const char * |
| 410 | Communication::ConnectionStatusAsCString (lldb::ConnectionStatus status) |
| 411 | { |
| 412 | switch (status) |
| 413 | { |
| 414 | case eConnectionStatusSuccess: return "success"; |
| 415 | case eConnectionStatusError: return "error"; |
| 416 | case eConnectionStatusTimedOut: return "timed out"; |
| 417 | case eConnectionStatusNoConnection: return "no connection"; |
| 418 | case eConnectionStatusLostConnection: return "lost connection"; |
Greg Clayton | 7a5388b | 2011-03-20 04:57:14 +0000 | [diff] [blame] | 419 | case eConnectionStatusEndOfFile: return "end of file"; |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static char unknown_state_string[64]; |
| 423 | snprintf(unknown_state_string, sizeof (unknown_state_string), "ConnectionStatus = %i", status); |
| 424 | return unknown_state_string; |
| 425 | } |