1 //===-- GDBRemoteClientBase.h -----------------------------------*- C++ -*-===//
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 #ifndef LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECLIENTBASE_H
10 #define LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECLIENTBASE_H
12 #include "GDBRemoteCommunication.h"
14 #include <condition_variable>
16 namespace lldb_private
{
17 namespace process_gdb_remote
{
19 class GDBRemoteClientBase
: public GDBRemoteCommunication
, public Broadcaster
{
22 eBroadcastBitRunPacketSent
= (1u << 0),
25 struct ContinueDelegate
{
26 virtual ~ContinueDelegate();
27 virtual void HandleAsyncStdout(llvm::StringRef out
) = 0;
28 virtual void HandleAsyncMisc(llvm::StringRef data
) = 0;
29 virtual void HandleStopReply() = 0;
31 /// Process asynchronously-received structured data.
34 /// The complete data packet, expected to start with JSON-async.
35 virtual void HandleAsyncStructuredDataPacket(llvm::StringRef data
) = 0;
38 GDBRemoteClientBase(const char *comm_name
);
40 bool SendAsyncSignal(int signo
, std::chrono::seconds interrupt_timeout
);
42 bool Interrupt(std::chrono::seconds interrupt_timeout
);
44 lldb::StateType
SendContinuePacketAndWaitForResponse(
45 ContinueDelegate
&delegate
, const UnixSignals
&signals
,
46 llvm::StringRef payload
, std::chrono::seconds interrupt_timeout
,
47 StringExtractorGDBRemote
&response
);
49 // If interrupt_timeout == 0 seconds, don't interrupt the target.
50 // Only send the packet if the target is stopped.
51 // If you want to use this mode, use the fact that the timeout is defaulted
52 // so it's clear from the call-site that you are using no-interrupt.
53 // If it is non-zero, interrupt the target if it is running, and
55 // It the target doesn't respond within the given timeout, it returns
57 PacketResult
SendPacketAndWaitForResponse(
58 llvm::StringRef payload
, StringExtractorGDBRemote
&response
,
59 std::chrono::seconds interrupt_timeout
= std::chrono::seconds(0));
61 PacketResult
ReadPacketWithOutputSupport(
62 StringExtractorGDBRemote
&response
, Timeout
<std::micro
> timeout
,
64 llvm::function_ref
<void(llvm::StringRef
)> output_callback
);
66 PacketResult
SendPacketAndReceiveResponseWithOutputSupport(
67 llvm::StringRef payload
, StringExtractorGDBRemote
&response
,
68 std::chrono::seconds interrupt_timeout
,
69 llvm::function_ref
<void(llvm::StringRef
)> output_callback
);
73 // If interrupt_timeout == 0 seconds, only take the lock if the target is
74 // not running. If using this option, use the fact that the
75 // interrupt_timeout is defaulted so it will be obvious at the call site
76 // that you are choosing this mode. If it is non-zero, interrupt the target
77 // if it is running, waiting for the given timeout for the interrupt to
79 Lock(GDBRemoteClientBase
&comm
,
80 std::chrono::seconds interrupt_timeout
= std::chrono::seconds(0));
83 explicit operator bool() { return m_acquired
; }
85 // Whether we had to interrupt the continue thread to acquire the
87 bool DidInterrupt() const { return m_did_interrupt
; }
90 std::unique_lock
<std::recursive_mutex
> m_async_lock
;
91 GDBRemoteClientBase
&m_comm
;
92 std::chrono::seconds m_interrupt_timeout
;
96 void SyncWithContinueThread();
101 SendPacketAndWaitForResponseNoLock(llvm::StringRef payload
,
102 StringExtractorGDBRemote
&response
);
104 virtual void OnRunPacketSent(bool first
);
107 /// Variables handling synchronization between the Continue thread and any
108 /// other threads wishing to send packets over the connection. Either the
109 /// continue thread has control over the connection (m_is_running == true) or
110 /// the connection is free for an arbitrary number of other senders to take
111 /// which indicate their interest by incrementing m_async_count.
113 /// Semantics of individual states:
115 /// - m_continue_packet == false, m_async_count == 0:
116 /// connection is free
117 /// - m_continue_packet == true, m_async_count == 0:
118 /// only continue thread is present
119 /// - m_continue_packet == true, m_async_count > 0:
120 /// continue thread has control, async threads should interrupt it and wait
121 /// for it to set m_continue_packet to false
122 /// - m_continue_packet == false, m_async_count > 0:
123 /// async threads have control, continue thread needs to wait for them to
124 /// finish (m_async_count goes down to 0).
127 std::condition_variable m_cv
;
129 /// Packet with which to resume after an async interrupt. Can be changed by
130 /// an async thread e.g. to inject a signal.
131 std::string m_continue_packet
;
133 /// When was the interrupt packet sent. Used to make sure we time out if the
134 /// stub does not respond to interrupt requests.
135 std::chrono::time_point
<std::chrono::steady_clock
> m_interrupt_endpoint
;
137 /// Number of threads interested in sending.
138 uint32_t m_async_count
;
140 /// Whether the continue thread has control.
143 /// Whether we should resume after a stop.
147 /// This handles the synchronization between individual async threads. For
148 /// now they just use a simple mutex.
149 std::recursive_mutex m_async_mutex
;
151 bool ShouldStop(const UnixSignals
&signals
,
152 StringExtractorGDBRemote
&response
);
156 enum class LockResult
{ Success
, Cancelled
, Failed
};
158 explicit ContinueLock(GDBRemoteClientBase
&comm
);
160 explicit operator bool() { return m_acquired
; }
167 GDBRemoteClientBase
&m_comm
;
172 } // namespace process_gdb_remote
173 } // namespace lldb_private
175 #endif // LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECLIENTBASE_H