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