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