xref: /llvm-project/lldb/source/Plugins/Process/gdb-remote/GDBRemoteClientBase.cpp (revision 9558b602b22cb7d681757c5f56d941e39a9d9d19)
1 //===-- GDBRemoteClientBase.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 "GDBRemoteClientBase.h"
10 
11 #include "llvm/ADT/StringExtras.h"
12 
13 #include "lldb/Target/UnixSignals.h"
14 #include "lldb/Utility/LLDBAssert.h"
15 
16 #include "ProcessGDBRemoteLog.h"
17 
18 using namespace lldb;
19 using namespace lldb_private;
20 using namespace lldb_private::process_gdb_remote;
21 using namespace std::chrono;
22 
23 // When we've sent a continue packet and are waiting for the target to stop,
24 // we wake up the wait with this interval to make sure the stub hasn't gone
25 // away while we were waiting.
26 static const seconds kWakeupInterval(5);
27 
28 /////////////////////////
29 // GDBRemoteClientBase //
30 /////////////////////////
31 
32 GDBRemoteClientBase::ContinueDelegate::~ContinueDelegate() = default;
33 
34 GDBRemoteClientBase::GDBRemoteClientBase(const char *comm_name,
35                                          const char *listener_name)
36     : GDBRemoteCommunication(comm_name, listener_name), m_async_count(0),
37       m_is_running(false), m_should_stop(false) {}
38 
39 StateType GDBRemoteClientBase::SendContinuePacketAndWaitForResponse(
40     ContinueDelegate &delegate, const UnixSignals &signals,
41     llvm::StringRef payload, std::chrono::seconds interrupt_timeout,
42     StringExtractorGDBRemote &response) {
43   Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
44   response.Clear();
45 
46   {
47     std::lock_guard<std::mutex> lock(m_mutex);
48     m_continue_packet = std::string(payload);
49     m_should_stop = false;
50   }
51   ContinueLock cont_lock(*this);
52   if (!cont_lock)
53     return eStateInvalid;
54   OnRunPacketSent(true);
55   // The main ReadPacket loop wakes up at computed_timeout intervals, just to
56   // check that the connection hasn't dropped.  When we wake up we also check
57   // whether there is an interrupt request that has reached its endpoint.
58   // If we want a shorter interrupt timeout that kWakeupInterval, we need to
59   // choose the shorter interval for the wake up as well.
60   std::chrono::seconds computed_timeout = std::min(interrupt_timeout,
61                                                    kWakeupInterval);
62   for (;;) {
63     PacketResult read_result = ReadPacket(response, computed_timeout, false);
64     switch (read_result) {
65     case PacketResult::ErrorReplyTimeout: {
66       std::lock_guard<std::mutex> lock(m_mutex);
67       if (m_async_count == 0) {
68         continue;
69       }
70       auto cur_time = steady_clock::now();
71       if (cur_time >= m_interrupt_endpoint)
72         return eStateInvalid;
73       else {
74         // We woke up and found an interrupt is in flight, but we haven't
75         // exceeded the interrupt wait time.  So reset the wait time to the
76         // time left till the interrupt timeout.  But don't wait longer
77         // than our wakeup timeout.
78         auto new_wait = m_interrupt_endpoint - cur_time;
79         computed_timeout = std::min(kWakeupInterval,
80             std::chrono::duration_cast<std::chrono::seconds>(new_wait));
81         continue;
82       }
83       break;
84     }
85     case PacketResult::Success:
86       break;
87     default:
88       LLDB_LOGF(log, "GDBRemoteClientBase::%s () ReadPacket(...) => false",
89                 __FUNCTION__);
90       return eStateInvalid;
91     }
92     if (response.Empty())
93       return eStateInvalid;
94 
95     const char stop_type = response.GetChar();
96     LLDB_LOGF(log, "GDBRemoteClientBase::%s () got packet: %s", __FUNCTION__,
97               response.GetStringRef().data());
98 
99     switch (stop_type) {
100     case 'W':
101     case 'X':
102       return eStateExited;
103     case 'E':
104       // ERROR
105       return eStateInvalid;
106     default:
107       LLDB_LOGF(log, "GDBRemoteClientBase::%s () unrecognized async packet",
108                 __FUNCTION__);
109       return eStateInvalid;
110     case 'O': {
111       std::string inferior_stdout;
112       response.GetHexByteString(inferior_stdout);
113       delegate.HandleAsyncStdout(inferior_stdout);
114       break;
115     }
116     case 'A':
117       delegate.HandleAsyncMisc(
118           llvm::StringRef(response.GetStringRef()).substr(1));
119       break;
120     case 'J':
121       delegate.HandleAsyncStructuredDataPacket(response.GetStringRef());
122       break;
123     case 'T':
124     case 'S':
125       // Do this with the continue lock held.
126       const bool should_stop = ShouldStop(signals, response);
127       response.SetFilePos(0);
128 
129       // The packet we should resume with. In the future we should check our
130       // thread list and "do the right thing" for new threads that show up
131       // while we stop and run async packets. Setting the packet to 'c' to
132       // continue all threads is the right thing to do 99.99% of the time
133       // because if a thread was single stepping, and we sent an interrupt, we
134       // will notice above that we didn't stop due to an interrupt but stopped
135       // due to stepping and we would _not_ continue. This packet may get
136       // modified by the async actions (e.g. to send a signal).
137       m_continue_packet = 'c';
138       cont_lock.unlock();
139 
140       delegate.HandleStopReply();
141       if (should_stop)
142         return eStateStopped;
143 
144       switch (cont_lock.lock()) {
145       case ContinueLock::LockResult::Success:
146         break;
147       case ContinueLock::LockResult::Failed:
148         return eStateInvalid;
149       case ContinueLock::LockResult::Cancelled:
150         return eStateStopped;
151       }
152       OnRunPacketSent(false);
153       break;
154     }
155   }
156 }
157 
158 bool GDBRemoteClientBase::SendAsyncSignal(
159     int signo, std::chrono::seconds interrupt_timeout) {
160   Lock lock(*this, interrupt_timeout);
161   if (!lock || !lock.DidInterrupt())
162     return false;
163 
164   m_continue_packet = 'C';
165   m_continue_packet += llvm::hexdigit((signo / 16) % 16);
166   m_continue_packet += llvm::hexdigit(signo % 16);
167   return true;
168 }
169 
170 bool GDBRemoteClientBase::Interrupt(std::chrono::seconds interrupt_timeout) {
171   Lock lock(*this, interrupt_timeout);
172   if (!lock.DidInterrupt())
173     return false;
174   m_should_stop = true;
175   return true;
176 }
177 
178 GDBRemoteCommunication::PacketResult
179 GDBRemoteClientBase::SendPacketAndWaitForResponse(
180     llvm::StringRef payload, StringExtractorGDBRemote &response,
181     std::chrono::seconds interrupt_timeout) {
182   Lock lock(*this, interrupt_timeout);
183   if (!lock) {
184     if (Log *log =
185             ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS))
186       LLDB_LOGF(log,
187                 "GDBRemoteClientBase::%s failed to get mutex, not sending "
188                 "packet '%.*s'",
189                 __FUNCTION__, int(payload.size()), payload.data());
190     return PacketResult::ErrorSendFailed;
191   }
192 
193   return SendPacketAndWaitForResponseNoLock(payload, response);
194 }
195 
196 GDBRemoteCommunication::PacketResult
197 GDBRemoteClientBase::SendPacketAndReceiveResponseWithOutputSupport(
198     llvm::StringRef payload, StringExtractorGDBRemote &response,
199     std::chrono::seconds interrupt_timeout,
200     llvm::function_ref<void(llvm::StringRef)> output_callback) {
201   Lock lock(*this, interrupt_timeout);
202   if (!lock) {
203     if (Log *log =
204             ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS))
205       LLDB_LOGF(log,
206                 "GDBRemoteClientBase::%s failed to get mutex, not sending "
207                 "packet '%.*s'",
208                 __FUNCTION__, int(payload.size()), payload.data());
209     return PacketResult::ErrorSendFailed;
210   }
211 
212   PacketResult packet_result = SendPacketNoLock(payload);
213   if (packet_result != PacketResult::Success)
214     return packet_result;
215 
216   return ReadPacketWithOutputSupport(response, GetPacketTimeout(), true,
217                                      output_callback);
218 }
219 
220 GDBRemoteCommunication::PacketResult
221 GDBRemoteClientBase::SendPacketAndWaitForResponseNoLock(
222     llvm::StringRef payload, StringExtractorGDBRemote &response) {
223   PacketResult packet_result = SendPacketNoLock(payload);
224   if (packet_result != PacketResult::Success)
225     return packet_result;
226 
227   const size_t max_response_retries = 3;
228   for (size_t i = 0; i < max_response_retries; ++i) {
229     packet_result = ReadPacket(response, GetPacketTimeout(), true);
230     // Make sure we received a response
231     if (packet_result != PacketResult::Success)
232       return packet_result;
233     // Make sure our response is valid for the payload that was sent
234     if (response.ValidateResponse())
235       return packet_result;
236     // Response says it wasn't valid
237     Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PACKETS);
238     LLDB_LOGF(
239         log,
240         "error: packet with payload \"%.*s\" got invalid response \"%s\": %s",
241         int(payload.size()), payload.data(), response.GetStringRef().data(),
242         (i == (max_response_retries - 1))
243             ? "using invalid response and giving up"
244             : "ignoring response and waiting for another");
245   }
246   return packet_result;
247 }
248 
249 bool GDBRemoteClientBase::SendvContPacket(
250     llvm::StringRef payload, std::chrono::seconds interrupt_timeout,
251     StringExtractorGDBRemote &response) {
252   Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
253   LLDB_LOGF(log, "GDBRemoteCommunicationClient::%s ()", __FUNCTION__);
254 
255   // we want to lock down packet sending while we continue
256   Lock lock(*this, interrupt_timeout);
257 
258   LLDB_LOGF(log,
259             "GDBRemoteCommunicationClient::%s () sending vCont packet: %.*s",
260             __FUNCTION__, int(payload.size()), payload.data());
261 
262   if (SendPacketNoLock(payload) != PacketResult::Success)
263     return false;
264 
265   OnRunPacketSent(true);
266 
267   // wait for the response to the vCont
268   if (ReadPacket(response, llvm::None, false) == PacketResult::Success) {
269     if (response.IsOKResponse())
270       return true;
271   }
272 
273   return false;
274 }
275 bool GDBRemoteClientBase::ShouldStop(const UnixSignals &signals,
276                                      StringExtractorGDBRemote &response) {
277   std::lock_guard<std::mutex> lock(m_mutex);
278 
279   if (m_async_count == 0)
280     return true; // We were not interrupted. The process stopped on its own.
281 
282   // Older debugserver stubs (before April 2016) can return two stop-reply
283   // packets in response to a ^C packet. Additionally, all debugservers still
284   // return two stop replies if the inferior stops due to some other reason
285   // before the remote stub manages to interrupt it. We need to wait for this
286   // additional packet to make sure the packet sequence does not get skewed.
287   StringExtractorGDBRemote extra_stop_reply_packet;
288   ReadPacket(extra_stop_reply_packet, milliseconds(100), false);
289 
290   // Interrupting is typically done using SIGSTOP or SIGINT, so if the process
291   // stops with some other signal, we definitely want to stop.
292   const uint8_t signo = response.GetHexU8(UINT8_MAX);
293   if (signo != signals.GetSignalNumberFromName("SIGSTOP") &&
294       signo != signals.GetSignalNumberFromName("SIGINT"))
295     return true;
296 
297   // We probably only stopped to perform some async processing, so continue
298   // after that is done.
299   // TODO: This is not 100% correct, as the process may have been stopped with
300   // SIGINT or SIGSTOP that was not caused by us (e.g. raise(SIGINT)). This will
301   // normally cause a stop, but if it's done concurrently with a async
302   // interrupt, that stop will get eaten (llvm.org/pr20231).
303   return false;
304 }
305 
306 void GDBRemoteClientBase::OnRunPacketSent(bool first) {
307   if (first)
308     BroadcastEvent(eBroadcastBitRunPacketSent, nullptr);
309 }
310 
311 ///////////////////////////////////////
312 // GDBRemoteClientBase::ContinueLock //
313 ///////////////////////////////////////
314 
315 GDBRemoteClientBase::ContinueLock::ContinueLock(GDBRemoteClientBase &comm)
316     : m_comm(comm), m_acquired(false) {
317   lock();
318 }
319 
320 GDBRemoteClientBase::ContinueLock::~ContinueLock() {
321   if (m_acquired)
322     unlock();
323 }
324 
325 void GDBRemoteClientBase::ContinueLock::unlock() {
326   lldbassert(m_acquired);
327   {
328     std::unique_lock<std::mutex> lock(m_comm.m_mutex);
329     m_comm.m_is_running = false;
330   }
331   m_comm.m_cv.notify_all();
332   m_acquired = false;
333 }
334 
335 GDBRemoteClientBase::ContinueLock::LockResult
336 GDBRemoteClientBase::ContinueLock::lock() {
337   Log *log = ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS);
338   LLDB_LOGF(log, "GDBRemoteClientBase::ContinueLock::%s() resuming with %s",
339             __FUNCTION__, m_comm.m_continue_packet.c_str());
340 
341   lldbassert(!m_acquired);
342   std::unique_lock<std::mutex> lock(m_comm.m_mutex);
343   m_comm.m_cv.wait(lock, [this] { return m_comm.m_async_count == 0; });
344   if (m_comm.m_should_stop) {
345     m_comm.m_should_stop = false;
346     LLDB_LOGF(log, "GDBRemoteClientBase::ContinueLock::%s() cancelled",
347               __FUNCTION__);
348     return LockResult::Cancelled;
349   }
350   if (m_comm.SendPacketNoLock(m_comm.m_continue_packet) !=
351       PacketResult::Success)
352     return LockResult::Failed;
353 
354   lldbassert(!m_comm.m_is_running);
355   m_comm.m_is_running = true;
356   m_acquired = true;
357   return LockResult::Success;
358 }
359 
360 ///////////////////////////////
361 // GDBRemoteClientBase::Lock //
362 ///////////////////////////////
363 
364 GDBRemoteClientBase::Lock::Lock(GDBRemoteClientBase &comm,
365                                 std::chrono::seconds interrupt_timeout)
366     : m_async_lock(comm.m_async_mutex, std::defer_lock), m_comm(comm),
367       m_interrupt_timeout(interrupt_timeout), m_acquired(false),
368       m_did_interrupt(false) {
369   SyncWithContinueThread();
370   if (m_acquired)
371     m_async_lock.lock();
372 }
373 
374 void GDBRemoteClientBase::Lock::SyncWithContinueThread() {
375   Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PROCESS));
376   std::unique_lock<std::mutex> lock(m_comm.m_mutex);
377   if (m_comm.m_is_running && m_interrupt_timeout == std::chrono::seconds(0))
378     return; // We were asked to avoid interrupting the sender. Lock is not
379             // acquired.
380 
381   ++m_comm.m_async_count;
382   if (m_comm.m_is_running) {
383     if (m_comm.m_async_count == 1) {
384       // The sender has sent the continue packet and we are the first async
385       // packet. Let's interrupt it.
386       const char ctrl_c = '\x03';
387       ConnectionStatus status = eConnectionStatusSuccess;
388       size_t bytes_written = m_comm.Write(&ctrl_c, 1, status, nullptr);
389       if (bytes_written == 0) {
390         --m_comm.m_async_count;
391         LLDB_LOGF(log, "GDBRemoteClientBase::Lock::Lock failed to send "
392                        "interrupt packet");
393         return;
394       }
395       m_comm.m_interrupt_endpoint = steady_clock::now() + m_interrupt_timeout;
396       if (log)
397         log->PutCString("GDBRemoteClientBase::Lock::Lock sent packet: \\x03");
398     }
399     m_comm.m_cv.wait(lock, [this] { return !m_comm.m_is_running; });
400     m_did_interrupt = true;
401   }
402   m_acquired = true;
403 }
404 
405 GDBRemoteClientBase::Lock::~Lock() {
406   if (!m_acquired)
407     return;
408   {
409     std::unique_lock<std::mutex> lock(m_comm.m_mutex);
410     --m_comm.m_async_count;
411   }
412   m_comm.m_cv.notify_one();
413 }
414