1 //===-- ThreadedCommunication.cpp -----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Core/ThreadedCommunication.h"
11 #include "lldb/Host/ThreadLauncher.h"
12 #include "lldb/Utility/Connection.h"
13 #include "lldb/Utility/ConstString.h"
14 #include "lldb/Utility/Event.h"
15 #include "lldb/Utility/LLDBLog.h"
16 #include "lldb/Utility/Listener.h"
17 #include "lldb/Utility/Log.h"
18 #include "lldb/Utility/Status.h"
20 #include "llvm/Support/Compiler.h"
26 #include <shared_mutex>
33 using namespace lldb_private
;
35 ConstString
&ThreadedCommunication::GetStaticBroadcasterClass() {
36 static ConstString
class_name("lldb.communication");
40 ThreadedCommunication::ThreadedCommunication(const char *name
)
41 : Communication(), Broadcaster(nullptr, name
), m_read_thread_enabled(false),
42 m_read_thread_did_exit(false), m_bytes(), m_bytes_mutex(),
43 m_synchronize_mutex(), m_callback(nullptr), m_callback_baton(nullptr) {
44 LLDB_LOG(GetLog(LLDBLog::Object
| LLDBLog::Communication
),
45 "{0} ThreadedCommunication::ThreadedCommunication (name = {1})",
48 SetEventName(eBroadcastBitDisconnected
, "disconnected");
49 SetEventName(eBroadcastBitReadThreadGotBytes
, "got bytes");
50 SetEventName(eBroadcastBitReadThreadDidExit
, "read thread did exit");
51 SetEventName(eBroadcastBitReadThreadShouldExit
, "read thread should exit");
52 SetEventName(eBroadcastBitPacketAvailable
, "packet available");
53 SetEventName(eBroadcastBitNoMorePendingInput
, "no more pending input");
58 ThreadedCommunication::~ThreadedCommunication() {
59 LLDB_LOG(GetLog(LLDBLog::Object
| LLDBLog::Communication
),
60 "{0} ThreadedCommunication::~ThreadedCommunication (name = {1})",
61 this, GetBroadcasterName());
64 void ThreadedCommunication::Clear() {
65 SetReadThreadBytesReceivedCallback(nullptr, nullptr);
66 StopReadThread(nullptr);
67 Communication::Clear();
70 ConnectionStatus
ThreadedCommunication::Disconnect(Status
*error_ptr
) {
71 assert((!m_read_thread_enabled
|| m_read_thread_did_exit
) &&
72 "Disconnecting while the read thread is running is racy!");
73 return Communication::Disconnect(error_ptr
);
76 size_t ThreadedCommunication::Read(void *dst
, size_t dst_len
,
77 const Timeout
<std::micro
> &timeout
,
78 ConnectionStatus
&status
,
80 Log
*log
= GetLog(LLDBLog::Communication
);
83 "this = {0}, dst = {1}, dst_len = {2}, timeout = {3}, connection = {4}",
84 this, dst
, dst_len
, timeout
, m_connection_sp
.get());
86 if (m_read_thread_enabled
) {
87 // We have a dedicated read thread that is getting data for us
88 size_t cached_bytes
= GetCachedBytes(dst
, dst_len
);
89 if (cached_bytes
> 0) {
90 status
= eConnectionStatusSuccess
;
93 if (timeout
&& timeout
->count() == 0) {
95 error_ptr
->SetErrorString("Timed out.");
96 status
= eConnectionStatusTimedOut
;
100 if (!m_connection_sp
) {
102 error_ptr
->SetErrorString("Invalid connection.");
103 status
= eConnectionStatusNoConnection
;
107 // No data yet, we have to start listening.
108 ListenerSP
listener_sp(
109 Listener::MakeListener("ThreadedCommunication::Read"));
110 listener_sp
->StartListeningForEvents(
111 this, eBroadcastBitReadThreadGotBytes
| eBroadcastBitReadThreadDidExit
);
113 // Re-check for data, as it might have arrived while we were setting up our
115 cached_bytes
= GetCachedBytes(dst
, dst_len
);
116 if (cached_bytes
> 0) {
117 status
= eConnectionStatusSuccess
;
122 // Explicitly check for the thread exit, for the same reason.
123 if (m_read_thread_did_exit
) {
124 // We've missed the event, lets just conjure one up.
125 event_sp
= std::make_shared
<Event
>(eBroadcastBitReadThreadDidExit
);
127 if (!listener_sp
->GetEvent(event_sp
, timeout
)) {
129 error_ptr
->SetErrorString("Timed out.");
130 status
= eConnectionStatusTimedOut
;
134 const uint32_t event_type
= event_sp
->GetType();
135 if (event_type
& eBroadcastBitReadThreadGotBytes
) {
136 return GetCachedBytes(dst
, dst_len
);
139 if (event_type
& eBroadcastBitReadThreadDidExit
) {
140 // If the thread exited of its own accord, it either means it
141 // hit an end-of-file condition or an error.
142 status
= m_pass_status
;
144 *error_ptr
= std::move(m_pass_error
);
150 llvm_unreachable("Got unexpected event type!");
153 // We aren't using a read thread, just read the data synchronously in this
155 return Communication::Read(dst
, dst_len
, timeout
, status
, error_ptr
);
158 bool ThreadedCommunication::StartReadThread(Status
*error_ptr
) {
159 std::lock_guard
<std::mutex
> lock(m_read_thread_mutex
);
164 if (m_read_thread
.IsJoinable())
167 LLDB_LOG(GetLog(LLDBLog::Communication
),
168 "{0} ThreadedCommunication::StartReadThread ()", this);
170 const std::string thread_name
=
171 llvm::formatv("<lldb.comm.{0}>", GetBroadcasterName());
173 m_read_thread_enabled
= true;
174 m_read_thread_did_exit
= false;
175 auto maybe_thread
= ThreadLauncher::LaunchThread(
176 thread_name
, [this] { return ReadThread(); });
178 m_read_thread
= *maybe_thread
;
181 *error_ptr
= Status(maybe_thread
.takeError());
183 LLDB_LOG_ERROR(GetLog(LLDBLog::Host
), maybe_thread
.takeError(),
184 "failed to launch host thread: {0}");
188 if (!m_read_thread
.IsJoinable())
189 m_read_thread_enabled
= false;
191 return m_read_thread_enabled
;
194 bool ThreadedCommunication::StopReadThread(Status
*error_ptr
) {
195 std::lock_guard
<std::mutex
> lock(m_read_thread_mutex
);
197 if (!m_read_thread
.IsJoinable())
200 LLDB_LOG(GetLog(LLDBLog::Communication
),
201 "{0} ThreadedCommunication::StopReadThread ()", this);
203 m_read_thread_enabled
= false;
205 BroadcastEvent(eBroadcastBitReadThreadShouldExit
, nullptr);
207 Status error
= m_read_thread
.Join(nullptr);
208 return error
.Success();
211 bool ThreadedCommunication::JoinReadThread(Status
*error_ptr
) {
212 std::lock_guard
<std::mutex
> lock(m_read_thread_mutex
);
214 if (!m_read_thread
.IsJoinable())
217 Status error
= m_read_thread
.Join(nullptr);
218 return error
.Success();
221 size_t ThreadedCommunication::GetCachedBytes(void *dst
, size_t dst_len
) {
222 std::lock_guard
<std::recursive_mutex
> guard(m_bytes_mutex
);
223 if (!m_bytes
.empty()) {
224 // If DST is nullptr and we have a thread, then return the number of bytes
225 // that are available so the caller can call again
227 return m_bytes
.size();
229 const size_t len
= std::min
<size_t>(dst_len
, m_bytes
.size());
231 ::memcpy(dst
, m_bytes
.c_str(), len
);
232 m_bytes
.erase(m_bytes
.begin(), m_bytes
.begin() + len
);
239 void ThreadedCommunication::AppendBytesToCache(const uint8_t *bytes
, size_t len
,
241 ConnectionStatus status
) {
242 LLDB_LOG(GetLog(LLDBLog::Communication
),
243 "{0} ThreadedCommunication::AppendBytesToCache (src = {1}, src_len "
246 this, bytes
, (uint64_t)len
, broadcast
);
247 if ((bytes
== nullptr || len
== 0) &&
248 (status
!= lldb::eConnectionStatusEndOfFile
))
251 // If the user registered a callback, then call it and do not broadcast
252 m_callback(m_callback_baton
, bytes
, len
);
253 } else if (bytes
!= nullptr && len
> 0) {
254 std::lock_guard
<std::recursive_mutex
> guard(m_bytes_mutex
);
255 m_bytes
.append((const char *)bytes
, len
);
257 BroadcastEventIfUnique(eBroadcastBitReadThreadGotBytes
);
261 bool ThreadedCommunication::ReadThreadIsRunning() {
262 return m_read_thread_enabled
;
265 lldb::thread_result_t
ThreadedCommunication::ReadThread() {
266 Log
*log
= GetLog(LLDBLog::Communication
);
268 LLDB_LOG(log
, "Communication({0}) thread starting...", this);
273 ConnectionStatus status
= eConnectionStatusSuccess
;
275 bool disconnect
= false;
276 while (!done
&& m_read_thread_enabled
) {
277 size_t bytes_read
= ReadFromConnection(
278 buf
, sizeof(buf
), std::chrono::seconds(5), status
, &error
);
279 if (bytes_read
> 0 || status
== eConnectionStatusEndOfFile
)
280 AppendBytesToCache(buf
, bytes_read
, true, status
);
283 case eConnectionStatusSuccess
:
286 case eConnectionStatusEndOfFile
:
288 disconnect
= GetCloseOnEOF();
290 case eConnectionStatusError
: // Check GetError() for details
291 if (error
.GetType() == eErrorTypePOSIX
&& error
.GetError() == EIO
) {
292 // EIO on a pipe is usually caused by remote shutdown
293 disconnect
= GetCloseOnEOF();
297 LLDB_LOG(log
, "error: {0}, status = {1}", error
,
298 ThreadedCommunication::ConnectionStatusAsString(status
));
300 case eConnectionStatusInterrupted
: // Synchronization signal from
301 // SynchronizeWithReadThread()
302 // The connection returns eConnectionStatusInterrupted only when there is
303 // no input pending to be read, so we can signal that.
304 BroadcastEvent(eBroadcastBitNoMorePendingInput
);
306 case eConnectionStatusNoConnection
: // No connection
307 case eConnectionStatusLostConnection
: // Lost connection while connected to
308 // a valid connection
311 case eConnectionStatusTimedOut
: // Request timed out
313 LLDB_LOG(log
, "error: {0}, status = {1}", error
,
314 ThreadedCommunication::ConnectionStatusAsString(status
));
318 m_pass_status
= status
;
319 m_pass_error
= std::move(error
);
320 LLDB_LOG(log
, "Communication({0}) thread exiting...", this);
322 // Start shutting down. We need to do this in a very specific order to ensure
323 // we don't race with threads wanting to read/synchronize with us.
325 // First, we signal our intent to exit. This ensures no new thread start
326 // waiting on events from us.
327 m_read_thread_did_exit
= true;
329 // Unblock any existing thread waiting for the synchronization signal.
330 BroadcastEvent(eBroadcastBitNoMorePendingInput
);
333 // Wait for the synchronization thread to finish...
334 std::lock_guard
<std::mutex
> guard(m_synchronize_mutex
);
335 // ... and disconnect.
340 // Finally, unblock any readers waiting for us to exit.
341 BroadcastEvent(eBroadcastBitReadThreadDidExit
);
345 void ThreadedCommunication::SetReadThreadBytesReceivedCallback(
346 ReadThreadBytesReceived callback
, void *callback_baton
) {
347 m_callback
= callback
;
348 m_callback_baton
= callback_baton
;
351 void ThreadedCommunication::SynchronizeWithReadThread() {
352 // Only one thread can do the synchronization dance at a time.
353 std::lock_guard
<std::mutex
> guard(m_synchronize_mutex
);
355 // First start listening for the synchronization event.
356 ListenerSP
listener_sp(Listener::MakeListener(
357 "ThreadedCommunication::SyncronizeWithReadThread"));
358 listener_sp
->StartListeningForEvents(this, eBroadcastBitNoMorePendingInput
);
360 // If the thread is not running, there is no point in synchronizing.
361 if (!m_read_thread_enabled
|| m_read_thread_did_exit
)
364 // Notify the read thread.
365 m_connection_sp
->InterruptRead();
367 // Wait for the synchronization event.
369 listener_sp
->GetEvent(event_sp
, std::nullopt
);
372 void ThreadedCommunication::SetConnection(
373 std::unique_ptr
<Connection
> connection
) {
374 StopReadThread(nullptr);
375 Communication::SetConnection(std::move(connection
));