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