/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/source/Core/ThreadedCommunication.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- ThreadedCommunication.cpp -----------------------------------------===// |
2 | | // |
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 |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | |
9 | | #include "lldb/Core/ThreadedCommunication.h" |
10 | | |
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" |
19 | | |
20 | | #include "llvm/Support/Compiler.h" |
21 | | |
22 | | #include <algorithm> |
23 | | #include <chrono> |
24 | | #include <cstring> |
25 | | #include <memory> |
26 | | #include <shared_mutex> |
27 | | |
28 | | #include <cerrno> |
29 | | #include <cinttypes> |
30 | | #include <cstdio> |
31 | | |
32 | | using namespace lldb; |
33 | | using namespace lldb_private; |
34 | | |
35 | 0 | ConstString &ThreadedCommunication::GetStaticBroadcasterClass() { |
36 | 0 | static ConstString class_name("lldb.communication"); |
37 | 0 | return class_name; |
38 | 0 | } |
39 | | |
40 | | ThreadedCommunication::ThreadedCommunication(const char *name) |
41 | 8.96k | : Communication(), Broadcaster(nullptr, name), m_read_thread_enabled(false), |
42 | 8.96k | m_read_thread_did_exit(false), m_bytes(), m_bytes_mutex(), |
43 | 8.96k | m_synchronize_mutex(), m_callback(nullptr), m_callback_baton(nullptr) { |
44 | 8.96k | LLDB_LOG(GetLog(LLDBLog::Object | LLDBLog::Communication), |
45 | 8.96k | "{0} ThreadedCommunication::ThreadedCommunication (name = {1})", |
46 | 8.96k | this, name); |
47 | | |
48 | 8.96k | SetEventName(eBroadcastBitDisconnected, "disconnected"); |
49 | 8.96k | SetEventName(eBroadcastBitReadThreadGotBytes, "got bytes"); |
50 | 8.96k | SetEventName(eBroadcastBitReadThreadDidExit, "read thread did exit"); |
51 | 8.96k | SetEventName(eBroadcastBitReadThreadShouldExit, "read thread should exit"); |
52 | 8.96k | SetEventName(eBroadcastBitPacketAvailable, "packet available"); |
53 | 8.96k | SetEventName(eBroadcastBitNoMorePendingInput, "no more pending input"); |
54 | | |
55 | 8.96k | CheckInWithManager(); |
56 | 8.96k | } |
57 | | |
58 | 8.86k | ThreadedCommunication::~ThreadedCommunication() { |
59 | 8.86k | LLDB_LOG(GetLog(LLDBLog::Object | LLDBLog::Communication), |
60 | 8.86k | "{0} ThreadedCommunication::~ThreadedCommunication (name = {1})", |
61 | 8.86k | this, GetBroadcasterName()); |
62 | 8.86k | } |
63 | | |
64 | 2.25k | void ThreadedCommunication::Clear() { |
65 | 2.25k | SetReadThreadBytesReceivedCallback(nullptr, nullptr); |
66 | 2.25k | StopReadThread(nullptr); |
67 | 2.25k | Communication::Clear(); |
68 | 2.25k | } |
69 | | |
70 | 13.9k | ConnectionStatus ThreadedCommunication::Disconnect(Status *error_ptr) { |
71 | 13.9k | assert((!m_read_thread_enabled || m_read_thread_did_exit) && |
72 | 13.9k | "Disconnecting while the read thread is running is racy!"); |
73 | 13.9k | return Communication::Disconnect(error_ptr); |
74 | 13.9k | } |
75 | | |
76 | | size_t ThreadedCommunication::Read(void *dst, size_t dst_len, |
77 | | const Timeout<std::micro> &timeout, |
78 | | ConnectionStatus &status, |
79 | 12 | Status *error_ptr) { |
80 | 12 | Log *log = GetLog(LLDBLog::Communication); |
81 | 12 | LLDB_LOG( |
82 | 12 | log, |
83 | 12 | "this = {0}, dst = {1}, dst_len = {2}, timeout = {3}, connection = {4}", |
84 | 12 | this, dst, dst_len, timeout, m_connection_sp.get()); |
85 | | |
86 | 12 | if (m_read_thread_enabled) { |
87 | | // We have a dedicated read thread that is getting data for us |
88 | 6 | size_t cached_bytes = GetCachedBytes(dst, dst_len); |
89 | 6 | if (cached_bytes > 0) { |
90 | 0 | status = eConnectionStatusSuccess; |
91 | 0 | return cached_bytes; |
92 | 0 | } |
93 | 6 | if (timeout && timeout->count() == 0) { |
94 | 1 | if (error_ptr) |
95 | 1 | error_ptr->SetErrorString("Timed out."); |
96 | 1 | status = eConnectionStatusTimedOut; |
97 | 1 | return 0; |
98 | 1 | } |
99 | | |
100 | 5 | if (!m_connection_sp) { |
101 | 1 | if (error_ptr) |
102 | 1 | error_ptr->SetErrorString("Invalid connection."); |
103 | 1 | status = eConnectionStatusNoConnection; |
104 | 1 | return 0; |
105 | 1 | } |
106 | | |
107 | | // No data yet, we have to start listening. |
108 | 4 | ListenerSP listener_sp( |
109 | 4 | Listener::MakeListener("ThreadedCommunication::Read")); |
110 | 4 | listener_sp->StartListeningForEvents( |
111 | 4 | this, eBroadcastBitReadThreadGotBytes | eBroadcastBitReadThreadDidExit); |
112 | | |
113 | | // Re-check for data, as it might have arrived while we were setting up our |
114 | | // listener. |
115 | 4 | cached_bytes = GetCachedBytes(dst, dst_len); |
116 | 4 | if (cached_bytes > 0) { |
117 | 0 | status = eConnectionStatusSuccess; |
118 | 0 | return cached_bytes; |
119 | 0 | } |
120 | | |
121 | 4 | EventSP event_sp; |
122 | | // Explicitly check for the thread exit, for the same reason. |
123 | 4 | if (m_read_thread_did_exit) { |
124 | | // We've missed the event, lets just conjure one up. |
125 | 0 | event_sp = std::make_shared<Event>(eBroadcastBitReadThreadDidExit); |
126 | 4 | } else { |
127 | 4 | if (!listener_sp->GetEvent(event_sp, timeout)) { |
128 | 1 | if (error_ptr) |
129 | 1 | error_ptr->SetErrorString("Timed out."); |
130 | 1 | status = eConnectionStatusTimedOut; |
131 | 1 | return 0; |
132 | 1 | } |
133 | 4 | } |
134 | 3 | const uint32_t event_type = event_sp->GetType(); |
135 | 3 | if (event_type & eBroadcastBitReadThreadGotBytes) { |
136 | 1 | return GetCachedBytes(dst, dst_len); |
137 | 1 | } |
138 | | |
139 | 2 | 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 | 2 | status = m_pass_status; |
143 | 2 | if (error_ptr) |
144 | 2 | *error_ptr = std::move(m_pass_error); |
145 | | |
146 | 2 | if (GetCloseOnEOF()) |
147 | 2 | Disconnect(nullptr); |
148 | 2 | return 0; |
149 | 2 | } |
150 | 0 | llvm_unreachable("Got unexpected event type!"); |
151 | 0 | } |
152 | | |
153 | | // We aren't using a read thread, just read the data synchronously in this |
154 | | // thread. |
155 | 6 | return Communication::Read(dst, dst_len, timeout, status, error_ptr); |
156 | 12 | } |
157 | | |
158 | 2.18k | bool ThreadedCommunication::StartReadThread(Status *error_ptr) { |
159 | 2.18k | std::lock_guard<std::mutex> lock(m_read_thread_mutex); |
160 | | |
161 | 2.18k | if (error_ptr) |
162 | 0 | error_ptr->Clear(); |
163 | | |
164 | 2.18k | if (m_read_thread.IsJoinable()) |
165 | 0 | return true; |
166 | | |
167 | 2.18k | LLDB_LOG(GetLog(LLDBLog::Communication), |
168 | 2.18k | "{0} ThreadedCommunication::StartReadThread ()", this); |
169 | | |
170 | 2.18k | const std::string thread_name = |
171 | 2.18k | llvm::formatv("<lldb.comm.{0}>", GetBroadcasterName()); |
172 | | |
173 | 2.18k | m_read_thread_enabled = true; |
174 | 2.18k | m_read_thread_did_exit = false; |
175 | 2.18k | auto maybe_thread = ThreadLauncher::LaunchThread( |
176 | 2.18k | thread_name, [this] { return ReadThread(); }); |
177 | 2.18k | if (maybe_thread) { |
178 | 2.18k | m_read_thread = *maybe_thread; |
179 | 2.18k | } else { |
180 | 0 | if (error_ptr) |
181 | 0 | *error_ptr = Status(maybe_thread.takeError()); |
182 | 0 | else { |
183 | 0 | LLDB_LOG_ERROR(GetLog(LLDBLog::Host), maybe_thread.takeError(), |
184 | 0 | "failed to launch host thread: {0}"); |
185 | 0 | } |
186 | 0 | } |
187 | | |
188 | 2.18k | if (!m_read_thread.IsJoinable()) |
189 | 0 | m_read_thread_enabled = false; |
190 | | |
191 | 2.18k | return m_read_thread_enabled; |
192 | 2.18k | } |
193 | | |
194 | 11.6k | bool ThreadedCommunication::StopReadThread(Status *error_ptr) { |
195 | 11.6k | std::lock_guard<std::mutex> lock(m_read_thread_mutex); |
196 | | |
197 | 11.6k | if (!m_read_thread.IsJoinable()) |
198 | 9.57k | return true; |
199 | | |
200 | 2.09k | LLDB_LOG(GetLog(LLDBLog::Communication), |
201 | 2.09k | "{0} ThreadedCommunication::StopReadThread ()", this); |
202 | | |
203 | 2.09k | m_read_thread_enabled = false; |
204 | | |
205 | 2.09k | BroadcastEvent(eBroadcastBitReadThreadShouldExit, nullptr); |
206 | | |
207 | 2.09k | Status error = m_read_thread.Join(nullptr); |
208 | 2.09k | return error.Success(); |
209 | 11.6k | } |
210 | | |
211 | 91 | bool ThreadedCommunication::JoinReadThread(Status *error_ptr) { |
212 | 91 | std::lock_guard<std::mutex> lock(m_read_thread_mutex); |
213 | | |
214 | 91 | if (!m_read_thread.IsJoinable()) |
215 | 3 | return true; |
216 | | |
217 | 88 | Status error = m_read_thread.Join(nullptr); |
218 | 88 | return error.Success(); |
219 | 91 | } |
220 | | |
221 | 11 | size_t ThreadedCommunication::GetCachedBytes(void *dst, size_t dst_len) { |
222 | 11 | std::lock_guard<std::recursive_mutex> guard(m_bytes_mutex); |
223 | 11 | 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 |
226 | 1 | if (dst == nullptr) |
227 | 0 | return m_bytes.size(); |
228 | | |
229 | 1 | const size_t len = std::min<size_t>(dst_len, m_bytes.size()); |
230 | | |
231 | 1 | ::memcpy(dst, m_bytes.c_str(), len); |
232 | 1 | m_bytes.erase(m_bytes.begin(), m_bytes.begin() + len); |
233 | | |
234 | 1 | return len; |
235 | 1 | } |
236 | 10 | return 0; |
237 | 11 | } |
238 | | |
239 | | void ThreadedCommunication::AppendBytesToCache(const uint8_t *bytes, size_t len, |
240 | | bool broadcast, |
241 | 4.01k | ConnectionStatus status) { |
242 | 4.01k | LLDB_LOG(GetLog(LLDBLog::Communication), |
243 | 4.01k | "{0} ThreadedCommunication::AppendBytesToCache (src = {1}, src_len " |
244 | 4.01k | "= {2}, " |
245 | 4.01k | "broadcast = {3})", |
246 | 4.01k | this, bytes, (uint64_t)len, broadcast); |
247 | 4.01k | if ((bytes == nullptr || len == 0) && |
248 | 4.01k | (status != lldb::eConnectionStatusEndOfFile)2.18k ) |
249 | 0 | return; |
250 | 4.01k | if (m_callback) { |
251 | | // If the user registered a callback, then call it and do not broadcast |
252 | 4.00k | m_callback(m_callback_baton, bytes, len); |
253 | 4.00k | } else if (3 bytes != nullptr3 && len > 03 ) { |
254 | 1 | std::lock_guard<std::recursive_mutex> guard(m_bytes_mutex); |
255 | 1 | m_bytes.append((const char *)bytes, len); |
256 | 1 | if (broadcast) |
257 | 1 | BroadcastEventIfUnique(eBroadcastBitReadThreadGotBytes); |
258 | 1 | } |
259 | 4.01k | } |
260 | | |
261 | 0 | bool ThreadedCommunication::ReadThreadIsRunning() { |
262 | 0 | return m_read_thread_enabled; |
263 | 0 | } |
264 | | |
265 | 2.18k | lldb::thread_result_t ThreadedCommunication::ReadThread() { |
266 | 2.18k | Log *log = GetLog(LLDBLog::Communication); |
267 | | |
268 | 2.18k | LLDB_LOG(log, "Communication({0}) thread starting...", this); |
269 | | |
270 | 2.18k | uint8_t buf[1024]; |
271 | | |
272 | 2.18k | Status error; |
273 | 2.18k | ConnectionStatus status = eConnectionStatusSuccess; |
274 | 2.18k | bool done = false; |
275 | 2.18k | bool disconnect = false; |
276 | 21.7k | while (!done && m_read_thread_enabled19.6k ) { |
277 | 19.5k | size_t bytes_read = ReadFromConnection( |
278 | 19.5k | buf, sizeof(buf), std::chrono::seconds(5), status, &error); |
279 | 19.5k | if (bytes_read > 0 || status == eConnectionStatusEndOfFile17.7k ) |
280 | 4.01k | AppendBytesToCache(buf, bytes_read, true, status); |
281 | | |
282 | 19.5k | switch (status) { |
283 | 1.82k | case eConnectionStatusSuccess: |
284 | 1.82k | break; |
285 | | |
286 | 2.18k | case eConnectionStatusEndOfFile: |
287 | 2.18k | done = true; |
288 | 2.18k | disconnect = GetCloseOnEOF(); |
289 | 2.18k | break; |
290 | 0 | case eConnectionStatusError: // Check GetError() for details |
291 | 0 | if (error.GetType() == eErrorTypePOSIX && error.GetError() == EIO) { |
292 | | // EIO on a pipe is usually caused by remote shutdown |
293 | 0 | disconnect = GetCloseOnEOF(); |
294 | 0 | done = true; |
295 | 0 | } |
296 | 0 | if (error.Fail()) |
297 | 0 | LLDB_LOG(log, "error: {0}, status = {1}", error, |
298 | 0 | ThreadedCommunication::ConnectionStatusAsString(status)); |
299 | 0 | break; |
300 | 15.4k | 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 | 15.4k | BroadcastEvent(eBroadcastBitNoMorePendingInput); |
305 | 15.4k | break; |
306 | 1 | case eConnectionStatusNoConnection: // No connection |
307 | 2 | case eConnectionStatusLostConnection: // Lost connection while connected to |
308 | | // a valid connection |
309 | 2 | done = true; |
310 | 2 | [[fallthrough]]; |
311 | 188 | case eConnectionStatusTimedOut: // Request timed out |
312 | 188 | if (error.Fail()) |
313 | 188 | LLDB_LOG(log, "error: {0}, status = {1}", error, |
314 | 188 | ThreadedCommunication::ConnectionStatusAsString(status)); |
315 | 188 | break; |
316 | 19.5k | } |
317 | 19.5k | } |
318 | 2.18k | m_pass_status = status; |
319 | 2.18k | m_pass_error = std::move(error); |
320 | 2.18k | LLDB_LOG(log, "Communication({0}) thread exiting...", this); |
321 | | |
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. |
324 | | |
325 | | // First, we signal our intent to exit. This ensures no new thread start |
326 | | // waiting on events from us. |
327 | 2.18k | m_read_thread_did_exit = true; |
328 | | |
329 | | // Unblock any existing thread waiting for the synchronization signal. |
330 | 2.18k | BroadcastEvent(eBroadcastBitNoMorePendingInput); |
331 | | |
332 | 2.18k | { |
333 | | // Wait for the synchronization thread to finish... |
334 | 2.18k | std::lock_guard<std::mutex> guard(m_synchronize_mutex); |
335 | | // ... and disconnect. |
336 | 2.18k | if (disconnect) |
337 | 2.18k | Disconnect(); |
338 | 2.18k | } |
339 | | |
340 | | // Finally, unblock any readers waiting for us to exit. |
341 | 2.18k | BroadcastEvent(eBroadcastBitReadThreadDidExit); |
342 | 2.18k | return {}; |
343 | 2.18k | } |
344 | | |
345 | | void ThreadedCommunication::SetReadThreadBytesReceivedCallback( |
346 | 4.44k | ReadThreadBytesReceived callback, void *callback_baton) { |
347 | 4.44k | m_callback = callback; |
348 | 4.44k | m_callback_baton = callback_baton; |
349 | 4.44k | } |
350 | | |
351 | 18.0k | void ThreadedCommunication::SynchronizeWithReadThread() { |
352 | | // Only one thread can do the synchronization dance at a time. |
353 | 18.0k | std::lock_guard<std::mutex> guard(m_synchronize_mutex); |
354 | | |
355 | | // First start listening for the synchronization event. |
356 | 18.0k | ListenerSP listener_sp(Listener::MakeListener( |
357 | 18.0k | "ThreadedCommunication::SyncronizeWithReadThread")); |
358 | 18.0k | listener_sp->StartListeningForEvents(this, eBroadcastBitNoMorePendingInput); |
359 | | |
360 | | // If the thread is not running, there is no point in synchronizing. |
361 | 18.0k | if (!m_read_thread_enabled || m_read_thread_did_exit17.4k ) |
362 | 2.62k | return; |
363 | | |
364 | | // Notify the read thread. |
365 | 15.4k | m_connection_sp->InterruptRead(); |
366 | | |
367 | | // Wait for the synchronization event. |
368 | 15.4k | EventSP event_sp; |
369 | 15.4k | listener_sp->GetEvent(event_sp, std::nullopt); |
370 | 15.4k | } |
371 | | |
372 | | void ThreadedCommunication::SetConnection( |
373 | 2.18k | std::unique_ptr<Connection> connection) { |
374 | 2.18k | StopReadThread(nullptr); |
375 | 2.18k | Communication::SetConnection(std::move(connection)); |
376 | 2.18k | } |