1 //===-- GDBRemoteClientBase.h -----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_GDBRemoteClientBase_h_
11 #define liblldb_GDBRemoteClientBase_h_
13 #include "GDBRemoteCommunication.h"
15 #include <condition_variable>
17 namespace lldb_private {
18 namespace process_gdb_remote {
20 class GDBRemoteClientBase : public GDBRemoteCommunication {
22 struct ContinueDelegate {
23 virtual ~ContinueDelegate();
24 virtual void HandleAsyncStdout(llvm::StringRef out) = 0;
25 virtual void HandleAsyncMisc(llvm::StringRef data) = 0;
26 virtual void HandleStopReply() = 0;
28 // =========================================================================
29 /// Process asynchronously-received structured data.
32 /// The complete data packet, expected to start with JSON-async.
33 // =========================================================================
34 virtual void HandleAsyncStructuredDataPacket(llvm::StringRef data) = 0;
37 GDBRemoteClientBase(const char *comm_name, const char *listener_name);
39 bool SendAsyncSignal(int signo);
43 lldb::StateType SendContinuePacketAndWaitForResponse(
44 ContinueDelegate &delegate, const UnixSignals &signals,
45 llvm::StringRef payload, StringExtractorGDBRemote &response);
47 PacketResult SendPacketAndWaitForResponse(llvm::StringRef payload,
48 StringExtractorGDBRemote &response,
51 PacketResult SendPacketAndReceiveResponseWithOutputSupport(
52 llvm::StringRef payload, StringExtractorGDBRemote &response,
54 llvm::function_ref<void(llvm::StringRef)> output_callback);
56 bool SendvContPacket(llvm::StringRef payload,
57 StringExtractorGDBRemote &response);
61 Lock(GDBRemoteClientBase &comm, bool interrupt);
64 explicit operator bool() { return m_acquired; }
66 // Whether we had to interrupt the continue thread to acquire the
68 bool DidInterrupt() const { return m_did_interrupt; }
71 std::unique_lock<std::recursive_mutex> m_async_lock;
72 GDBRemoteClientBase &m_comm;
76 void SyncWithContinueThread(bool interrupt);
81 SendPacketAndWaitForResponseNoLock(llvm::StringRef payload,
82 StringExtractorGDBRemote &response);
84 virtual void OnRunPacketSent(bool first);
87 // Variables handling synchronization between the Continue thread and any
89 // wishing to send packets over the connection. Either the continue thread has
91 // the connection (m_is_running == true) or the connection is free for an
92 // arbitrary number of
93 // other senders to take which indicate their interest by incrementing
95 // Semantics of individual states:
96 // - m_continue_packet == false, m_async_count == 0: connection is free
97 // - m_continue_packet == true, m_async_count == 0: only continue thread is
99 // - m_continue_packet == true, m_async_count > 0: continue thread has
100 // control, async threads
101 // should interrupt it and wait for it to set m_continue_packet to false
102 // - m_continue_packet == false, m_async_count > 0: async threads have
104 // thread needs to wait for them to finish (m_async_count goes down to 0).
106 std::condition_variable m_cv;
107 // Packet with which to resume after an async interrupt. Can be changed by an
109 // e.g. to inject a signal.
110 std::string m_continue_packet;
111 // When was the interrupt packet sent. Used to make sure we time out if the
113 // respond to interrupt requests.
114 std::chrono::time_point<std::chrono::steady_clock> m_interrupt_time;
115 uint32_t m_async_count;
117 bool m_should_stop; // Whether we should resume after a stop.
118 // end of continue thread synchronization block
120 // This handles the synchronization between individual async threads. For now
123 std::recursive_mutex m_async_mutex;
125 bool ShouldStop(const UnixSignals &signals,
126 StringExtractorGDBRemote &response);
130 enum class LockResult { Success, Cancelled, Failed };
132 explicit ContinueLock(GDBRemoteClientBase &comm);
134 explicit operator bool() { return m_acquired; }
141 GDBRemoteClientBase &m_comm;
146 } // namespace process_gdb_remote
147 } // namespace lldb_private
149 #endif // liblldb_GDBRemoteCommunicationClient_h_