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 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 10 | #include "lldb/Core/Communication.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 11 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Connection.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Event.h" |
Jim Ingham | 583bbb1 | 2016-03-07 21:50:25 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Listener.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 15 | #include "lldb/Host/HostThread.h" |
| 16 | #include "lldb/Host/ThreadLauncher.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 17 | #include "lldb/Utility/ConstString.h" // for ConstString |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/Log.h" |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 19 | #include "lldb/Utility/Logging.h" // for LogIfAnyCategoriesSet, LIBLLDB... |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 20 | #include "lldb/Utility/Status.h" // for Status |
Zachary Turner | 2f3df61 | 2017-04-06 21:28:29 +0000 | [diff] [blame] | 21 | |
| 22 | #include "llvm/ADT/None.h" // for None |
| 23 | #include "llvm/ADT/Optional.h" // for Optional |
| 24 | #include "llvm/Support/Compiler.h" // for LLVM_FALLTHROUGH |
| 25 | |
| 26 | #include <algorithm> // for min |
| 27 | #include <chrono> // for duration, seconds |
| 28 | #include <cstring> |
| 29 | #include <memory> // for shared_ptr |
| 30 | |
| 31 | #include <errno.h> // for EIO |
| 32 | #include <inttypes.h> // for PRIu64 |
| 33 | #include <stdio.h> // for snprintf |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace lldb; |
| 36 | using namespace lldb_private; |
| 37 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 38 | ConstString &Communication::GetStaticBroadcasterClass() { |
| 39 | static ConstString class_name("lldb.communication"); |
| 40 | return class_name; |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 43 | Communication::Communication(const char *name) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 44 | : Broadcaster(nullptr, name), m_connection_sp(), |
| 45 | m_read_thread_enabled(false), m_read_thread_did_exit(false), m_bytes(), |
| 46 | m_bytes_mutex(), m_write_mutex(), m_synchronize_mutex(), |
| 47 | m_callback(nullptr), m_callback_baton(nullptr), m_close_on_eof(true) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | |
| 49 | { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | lldb_private::LogIfAnyCategoriesSet( |
| 51 | LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_COMMUNICATION, |
| 52 | "%p Communication::Communication (name = %s)", this, name); |
Greg Clayton | 95bf0fd | 2011-04-01 00:29:43 +0000 | [diff] [blame] | 53 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | SetEventName(eBroadcastBitDisconnected, "disconnected"); |
| 55 | SetEventName(eBroadcastBitReadThreadGotBytes, "got bytes"); |
| 56 | SetEventName(eBroadcastBitReadThreadDidExit, "read thread did exit"); |
| 57 | SetEventName(eBroadcastBitReadThreadShouldExit, "read thread should exit"); |
| 58 | SetEventName(eBroadcastBitPacketAvailable, "packet available"); |
| 59 | SetEventName(eBroadcastBitNoMorePendingInput, "no more pending input"); |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 60 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | CheckInWithManager(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 64 | Communication::~Communication() { |
| 65 | lldb_private::LogIfAnyCategoriesSet( |
| 66 | LIBLLDB_LOG_OBJECT | LIBLLDB_LOG_COMMUNICATION, |
| 67 | "%p Communication::~Communication (name = %s)", this, |
| 68 | GetBroadcasterName().AsCString()); |
| 69 | Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 72 | void Communication::Clear() { |
| 73 | SetReadThreadBytesReceivedCallback(nullptr, nullptr); |
| 74 | Disconnect(nullptr); |
| 75 | StopReadThread(nullptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 78 | ConnectionStatus Communication::Connect(const char *url, Status *error_ptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 79 | Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 80 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 81 | lldb_private::LogIfAnyCategoriesSet(LIBLLDB_LOG_COMMUNICATION, |
| 82 | "%p Communication::Connect (url = %s)", |
| 83 | this, url); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 85 | lldb::ConnectionSP connection_sp(m_connection_sp); |
| 86 | if (connection_sp) |
| 87 | return connection_sp->Connect(url, error_ptr); |
| 88 | if (error_ptr) |
| 89 | error_ptr->SetErrorString("Invalid connection."); |
| 90 | return eConnectionStatusNoConnection; |
| 91 | } |
| 92 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 93 | ConnectionStatus Communication::Disconnect(Status *error_ptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 94 | lldb_private::LogIfAnyCategoriesSet(LIBLLDB_LOG_COMMUNICATION, |
| 95 | "%p Communication::Disconnect ()", this); |
| 96 | |
| 97 | lldb::ConnectionSP connection_sp(m_connection_sp); |
| 98 | if (connection_sp) { |
| 99 | ConnectionStatus status = connection_sp->Disconnect(error_ptr); |
| 100 | // We currently don't protect connection_sp with any mutex for |
| 101 | // multi-threaded environments. So lets not nuke our connection class |
| 102 | // without putting some multi-threaded protections in. We also probably |
| 103 | // don't want to pay for the overhead it might cause if every time we |
| 104 | // access the connection we have to take a lock. |
| 105 | // |
| 106 | // This unique pointer will cleanup after itself when this object goes away, |
| 107 | // so there is no need to currently have it destroy itself immediately |
| 108 | // upon disconnnect. |
| 109 | // connection_sp.reset(); |
| 110 | return status; |
| 111 | } |
| 112 | return eConnectionStatusNoConnection; |
| 113 | } |
| 114 | |
| 115 | bool Communication::IsConnected() const { |
| 116 | lldb::ConnectionSP connection_sp(m_connection_sp); |
| 117 | return (connection_sp ? connection_sp->IsConnected() : false); |
| 118 | } |
| 119 | |
| 120 | bool Communication::HasConnection() const { |
| 121 | return m_connection_sp.get() != nullptr; |
| 122 | } |
| 123 | |
Pavel Labath | c4063ee | 2016-11-25 11:58:44 +0000 | [diff] [blame] | 124 | size_t Communication::Read(void *dst, size_t dst_len, |
| 125 | const Timeout<std::micro> &timeout, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 126 | ConnectionStatus &status, Status *error_ptr) { |
Pavel Labath | d02b1c8 | 2017-02-10 11:49:33 +0000 | [diff] [blame] | 127 | Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_COMMUNICATION); |
| 128 | LLDB_LOG( |
| 129 | log, |
| 130 | "this = {0}, dst = {1}, dst_len = {2}, timeout = {3}, connection = {4}", |
| 131 | this, dst, dst_len, timeout, m_connection_sp.get()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | |
| 133 | if (m_read_thread_enabled) { |
| 134 | // We have a dedicated read thread that is getting data for us |
| 135 | size_t cached_bytes = GetCachedBytes(dst, dst_len); |
Pavel Labath | c4063ee | 2016-11-25 11:58:44 +0000 | [diff] [blame] | 136 | if (cached_bytes > 0 || (timeout && timeout->count() == 0)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 137 | status = eConnectionStatusSuccess; |
| 138 | return cached_bytes; |
| 139 | } |
| 140 | |
| 141 | if (!m_connection_sp) { |
| 142 | if (error_ptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 143 | error_ptr->SetErrorString("Invalid connection."); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 144 | status = eConnectionStatusNoConnection; |
| 145 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 148 | ListenerSP listener_sp(Listener::MakeListener("Communication::Read")); |
| 149 | listener_sp->StartListeningForEvents( |
| 150 | this, eBroadcastBitReadThreadGotBytes | eBroadcastBitReadThreadDidExit); |
Pavel Labath | 3f5df53 | 2015-03-12 10:12:41 +0000 | [diff] [blame] | 151 | EventSP event_sp; |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 152 | while (listener_sp->GetEvent(event_sp, timeout)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 153 | const uint32_t event_type = event_sp->GetType(); |
| 154 | if (event_type & eBroadcastBitReadThreadGotBytes) { |
| 155 | return GetCachedBytes(dst, dst_len); |
| 156 | } |
| 157 | |
| 158 | if (event_type & eBroadcastBitReadThreadDidExit) { |
| 159 | if (GetCloseOnEOF()) |
| 160 | Disconnect(nullptr); |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | // We aren't using a read thread, just read the data synchronously in this |
| 168 | // thread. |
Pavel Labath | c4063ee | 2016-11-25 11:58:44 +0000 | [diff] [blame] | 169 | return ReadFromConnection(dst, dst_len, timeout, status, error_ptr); |
Pavel Labath | 3f5df53 | 2015-03-12 10:12:41 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 172 | size_t Communication::Write(const void *src, size_t src_len, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 173 | ConnectionStatus &status, Status *error_ptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 174 | lldb::ConnectionSP connection_sp(m_connection_sp); |
| 175 | |
| 176 | std::lock_guard<std::mutex> guard(m_write_mutex); |
| 177 | lldb_private::LogIfAnyCategoriesSet( |
| 178 | LIBLLDB_LOG_COMMUNICATION, |
| 179 | "%p Communication::Write (src = %p, src_len = %" PRIu64 |
| 180 | ") connection = %p", |
| 181 | this, src, (uint64_t)src_len, connection_sp.get()); |
| 182 | |
| 183 | if (connection_sp) |
| 184 | return connection_sp->Write(src, src_len, status, error_ptr); |
| 185 | |
| 186 | if (error_ptr) |
| 187 | error_ptr->SetErrorString("Invalid connection."); |
| 188 | status = eConnectionStatusNoConnection; |
| 189 | return 0; |
| 190 | } |
| 191 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 192 | bool Communication::StartReadThread(Status *error_ptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 193 | if (error_ptr) |
| 194 | error_ptr->Clear(); |
| 195 | |
| 196 | if (m_read_thread.IsJoinable()) |
| 197 | return true; |
| 198 | |
| 199 | lldb_private::LogIfAnyCategoriesSet( |
| 200 | LIBLLDB_LOG_COMMUNICATION, "%p Communication::StartReadThread ()", this); |
| 201 | |
| 202 | char thread_name[1024]; |
| 203 | snprintf(thread_name, sizeof(thread_name), "<lldb.comm.%s>", |
| 204 | GetBroadcasterName().AsCString()); |
| 205 | |
| 206 | m_read_thread_enabled = true; |
| 207 | m_read_thread_did_exit = false; |
| 208 | m_read_thread = ThreadLauncher::LaunchThread( |
| 209 | thread_name, Communication::ReadThread, this, error_ptr); |
| 210 | if (!m_read_thread.IsJoinable()) |
| 211 | m_read_thread_enabled = false; |
| 212 | return m_read_thread_enabled; |
| 213 | } |
| 214 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 215 | bool Communication::StopReadThread(Status *error_ptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 216 | if (!m_read_thread.IsJoinable()) |
| 217 | return true; |
| 218 | |
| 219 | lldb_private::LogIfAnyCategoriesSet( |
| 220 | LIBLLDB_LOG_COMMUNICATION, "%p Communication::StopReadThread ()", this); |
| 221 | |
| 222 | m_read_thread_enabled = false; |
| 223 | |
| 224 | BroadcastEvent(eBroadcastBitReadThreadShouldExit, nullptr); |
| 225 | |
| 226 | // error = m_read_thread.Cancel(); |
| 227 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 228 | Status error = m_read_thread.Join(nullptr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 229 | return error.Success(); |
| 230 | } |
| 231 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 232 | bool Communication::JoinReadThread(Status *error_ptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 233 | if (!m_read_thread.IsJoinable()) |
| 234 | return true; |
| 235 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 236 | Status error = m_read_thread.Join(nullptr); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 237 | return error.Success(); |
| 238 | } |
| 239 | |
| 240 | size_t Communication::GetCachedBytes(void *dst, size_t dst_len) { |
| 241 | std::lock_guard<std::recursive_mutex> guard(m_bytes_mutex); |
| 242 | if (!m_bytes.empty()) { |
| 243 | // If DST is nullptr and we have a thread, then return the number |
| 244 | // of bytes that are available so the caller can call again |
| 245 | if (dst == nullptr) |
| 246 | return m_bytes.size(); |
| 247 | |
| 248 | const size_t len = std::min<size_t>(dst_len, m_bytes.size()); |
| 249 | |
| 250 | ::memcpy(dst, m_bytes.c_str(), len); |
| 251 | m_bytes.erase(m_bytes.begin(), m_bytes.begin() + len); |
| 252 | |
| 253 | return len; |
| 254 | } |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | void Communication::AppendBytesToCache(const uint8_t *bytes, size_t len, |
| 259 | bool broadcast, |
| 260 | ConnectionStatus status) { |
| 261 | lldb_private::LogIfAnyCategoriesSet( |
| 262 | LIBLLDB_LOG_COMMUNICATION, |
| 263 | "%p Communication::AppendBytesToCache (src = %p, src_len = %" PRIu64 |
| 264 | ", broadcast = %i)", |
| 265 | this, bytes, (uint64_t)len, broadcast); |
| 266 | if ((bytes == nullptr || len == 0) && |
| 267 | (status != lldb::eConnectionStatusEndOfFile)) |
| 268 | return; |
| 269 | if (m_callback) { |
| 270 | // If the user registered a callback, then call it and do not broadcast |
| 271 | m_callback(m_callback_baton, bytes, len); |
| 272 | } else if (bytes != nullptr && len > 0) { |
| 273 | std::lock_guard<std::recursive_mutex> guard(m_bytes_mutex); |
| 274 | m_bytes.append((const char *)bytes, len); |
| 275 | if (broadcast) |
| 276 | BroadcastEventIfUnique(eBroadcastBitReadThreadGotBytes); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | size_t Communication::ReadFromConnection(void *dst, size_t dst_len, |
Pavel Labath | c4063ee | 2016-11-25 11:58:44 +0000 | [diff] [blame] | 281 | const Timeout<std::micro> &timeout, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 282 | ConnectionStatus &status, |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 283 | Status *error_ptr) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 284 | lldb::ConnectionSP connection_sp(m_connection_sp); |
Pavel Labath | 2f159a5 | 2016-11-25 12:22:32 +0000 | [diff] [blame] | 285 | if (connection_sp) |
| 286 | return connection_sp->Read(dst, dst_len, timeout, status, error_ptr); |
Pavel Labath | c4063ee | 2016-11-25 11:58:44 +0000 | [diff] [blame] | 287 | |
| 288 | if (error_ptr) |
| 289 | error_ptr->SetErrorString("Invalid connection."); |
| 290 | status = eConnectionStatusNoConnection; |
| 291 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | bool Communication::ReadThreadIsRunning() { return m_read_thread_enabled; } |
| 295 | |
| 296 | lldb::thread_result_t Communication::ReadThread(lldb::thread_arg_t p) { |
| 297 | Communication *comm = (Communication *)p; |
| 298 | |
| 299 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_COMMUNICATION)); |
| 300 | |
| 301 | if (log) |
| 302 | log->Printf("%p Communication::ReadThread () thread starting...", p); |
| 303 | |
| 304 | uint8_t buf[1024]; |
| 305 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 306 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 307 | ConnectionStatus status = eConnectionStatusSuccess; |
| 308 | bool done = false; |
| 309 | while (!done && comm->m_read_thread_enabled) { |
| 310 | size_t bytes_read = comm->ReadFromConnection( |
Pavel Labath | c4063ee | 2016-11-25 11:58:44 +0000 | [diff] [blame] | 311 | buf, sizeof(buf), std::chrono::seconds(5), status, &error); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 312 | if (bytes_read > 0) |
| 313 | comm->AppendBytesToCache(buf, bytes_read, true, status); |
| 314 | else if ((bytes_read == 0) && status == eConnectionStatusEndOfFile) { |
| 315 | if (comm->GetCloseOnEOF()) |
| 316 | comm->Disconnect(); |
| 317 | comm->AppendBytesToCache(buf, bytes_read, true, status); |
| 318 | } |
| 319 | |
| 320 | switch (status) { |
| 321 | case eConnectionStatusSuccess: |
| 322 | break; |
| 323 | |
| 324 | case eConnectionStatusEndOfFile: |
| 325 | done = true; |
| 326 | break; |
| 327 | case eConnectionStatusError: // Check GetError() for details |
| 328 | if (error.GetType() == eErrorTypePOSIX && error.GetError() == EIO) { |
| 329 | // EIO on a pipe is usually caused by remote shutdown |
| 330 | comm->Disconnect(); |
| 331 | done = true; |
| 332 | } |
Zachary Turner | 33aba3c | 2017-02-06 18:31:44 +0000 | [diff] [blame] | 333 | if (error.Fail()) |
| 334 | LLDB_LOG(log, "error: {0}, status = {1}", error, |
| 335 | Communication::ConnectionStatusAsCString(status)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 336 | break; |
| 337 | case eConnectionStatusInterrupted: // Synchronization signal from |
| 338 | // SynchronizeWithReadThread() |
| 339 | // The connection returns eConnectionStatusInterrupted only when there is |
| 340 | // no |
| 341 | // input pending to be read, so we can signal that. |
| 342 | comm->BroadcastEvent(eBroadcastBitNoMorePendingInput); |
| 343 | break; |
| 344 | case eConnectionStatusNoConnection: // No connection |
| 345 | case eConnectionStatusLostConnection: // Lost connection while connected to |
| 346 | // a valid connection |
| 347 | done = true; |
| 348 | LLVM_FALLTHROUGH; |
| 349 | case eConnectionStatusTimedOut: // Request timed out |
Zachary Turner | 33aba3c | 2017-02-06 18:31:44 +0000 | [diff] [blame] | 350 | if (error.Fail()) |
| 351 | LLDB_LOG(log, "error: {0}, status = {1}", error, |
| 352 | Communication::ConnectionStatusAsCString(status)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 353 | break; |
| 354 | } |
| 355 | } |
| 356 | log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_COMMUNICATION); |
| 357 | if (log) |
| 358 | log->Printf("%p Communication::ReadThread () thread exiting...", p); |
| 359 | |
| 360 | comm->m_read_thread_did_exit = true; |
| 361 | // Let clients know that this thread is exiting |
| 362 | comm->BroadcastEvent(eBroadcastBitNoMorePendingInput); |
| 363 | comm->BroadcastEvent(eBroadcastBitReadThreadDidExit); |
| 364 | return NULL; |
| 365 | } |
| 366 | |
| 367 | void Communication::SetReadThreadBytesReceivedCallback( |
| 368 | ReadThreadBytesReceived callback, void *callback_baton) { |
| 369 | m_callback = callback; |
| 370 | m_callback_baton = callback_baton; |
| 371 | } |
| 372 | |
| 373 | void Communication::SynchronizeWithReadThread() { |
| 374 | // Only one thread can do the synchronization dance at a time. |
| 375 | std::lock_guard<std::mutex> guard(m_synchronize_mutex); |
| 376 | |
| 377 | // First start listening for the synchronization event. |
| 378 | ListenerSP listener_sp( |
| 379 | Listener::MakeListener("Communication::SyncronizeWithReadThread")); |
| 380 | listener_sp->StartListeningForEvents(this, eBroadcastBitNoMorePendingInput); |
| 381 | |
| 382 | // If the thread is not running, there is no point in synchronizing. |
| 383 | if (!m_read_thread_enabled || m_read_thread_did_exit) |
| 384 | return; |
| 385 | |
| 386 | // Notify the read thread. |
| 387 | m_connection_sp->InterruptRead(); |
| 388 | |
| 389 | // Wait for the synchronization event. |
| 390 | EventSP event_sp; |
Pavel Labath | d35031e1 | 2016-11-30 10:41:42 +0000 | [diff] [blame] | 391 | listener_sp->GetEvent(event_sp, llvm::None); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void Communication::SetConnection(Connection *connection) { |
| 395 | Disconnect(nullptr); |
| 396 | StopReadThread(nullptr); |
| 397 | m_connection_sp.reset(connection); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 398 | } |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 399 | |
| 400 | const char * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 401 | Communication::ConnectionStatusAsCString(lldb::ConnectionStatus status) { |
| 402 | switch (status) { |
| 403 | case eConnectionStatusSuccess: |
| 404 | return "success"; |
| 405 | case eConnectionStatusError: |
| 406 | return "error"; |
| 407 | case eConnectionStatusTimedOut: |
| 408 | return "timed out"; |
| 409 | case eConnectionStatusNoConnection: |
| 410 | return "no connection"; |
| 411 | case eConnectionStatusLostConnection: |
| 412 | return "lost connection"; |
| 413 | case eConnectionStatusEndOfFile: |
| 414 | return "end of file"; |
| 415 | case eConnectionStatusInterrupted: |
| 416 | return "interrupted"; |
| 417 | } |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 418 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 419 | static char unknown_state_string[64]; |
| 420 | snprintf(unknown_state_string, sizeof(unknown_state_string), |
| 421 | "ConnectionStatus = %i", status); |
| 422 | return unknown_state_string; |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 423 | } |