blob: 3d84ce0ebe1887eb61c7ee34f56c698a6934b9c2 [file] [log] [blame]
Pavel Labath8c1b6bd2016-08-09 12:04:46 +00001//===-- GDBRemoteClientBase.h -----------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_GDBRemoteClientBase_h_
11#define liblldb_GDBRemoteClientBase_h_
12
13#include "GDBRemoteCommunication.h"
14
15#include <condition_variable>
16
Kate Stoneb9c1b512016-09-06 20:57:50 +000017namespace lldb_private {
18namespace process_gdb_remote {
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000019
Kate Stoneb9c1b512016-09-06 20:57:50 +000020class GDBRemoteClientBase : public GDBRemoteCommunication {
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000021public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000022 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;
Todd Fiala75930012016-08-19 04:21:48 +000027
Todd Fialafcdb1af2016-09-10 00:06:29 +000028 // =========================================================================
29 /// Process asynchronously-received structured data.
Kate Stoneb9c1b512016-09-06 20:57:50 +000030 ///
Todd Fialafcdb1af2016-09-10 00:06:29 +000031 /// @param[in] data
32 /// The complete data packet, expected to start with JSON-async.
33 // =========================================================================
34 virtual void HandleAsyncStructuredDataPacket(llvm::StringRef data) = 0;
Kate Stoneb9c1b512016-09-06 20:57:50 +000035 };
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000036
Kate Stoneb9c1b512016-09-06 20:57:50 +000037 GDBRemoteClientBase(const char *comm_name, const char *listener_name);
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000038
Kate Stoneb9c1b512016-09-06 20:57:50 +000039 bool SendAsyncSignal(int signo);
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000040
Kate Stoneb9c1b512016-09-06 20:57:50 +000041 bool Interrupt();
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000042
Kate Stoneb9c1b512016-09-06 20:57:50 +000043 lldb::StateType SendContinuePacketAndWaitForResponse(
44 ContinueDelegate &delegate, const UnixSignals &signals,
45 llvm::StringRef payload, StringExtractorGDBRemote &response);
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000046
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 PacketResult SendPacketAndWaitForResponse(llvm::StringRef payload,
48 StringExtractorGDBRemote &response,
49 bool send_async);
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000050
Pavel Labath7da84752018-01-10 14:39:08 +000051 PacketResult SendPacketAndReceiveResponseWithOutputSupport(
52 llvm::StringRef payload, StringExtractorGDBRemote &response,
53 bool send_async,
54 llvm::function_ref<void(llvm::StringRef)> output_callback);
55
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 bool SendvContPacket(llvm::StringRef payload,
57 StringExtractorGDBRemote &response);
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 class Lock {
60 public:
61 Lock(GDBRemoteClientBase &comm, bool interrupt);
62 ~Lock();
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 explicit operator bool() { return m_acquired; }
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000065
Kate Stoneb9c1b512016-09-06 20:57:50 +000066 // Whether we had to interrupt the continue thread to acquire the
67 // connection.
68 bool DidInterrupt() const { return m_did_interrupt; }
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000069
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 private:
71 std::unique_lock<std::recursive_mutex> m_async_lock;
72 GDBRemoteClientBase &m_comm;
73 bool m_acquired;
74 bool m_did_interrupt;
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000075
Kate Stoneb9c1b512016-09-06 20:57:50 +000076 void SyncWithContinueThread(bool interrupt);
77 };
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000078
79protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 PacketResult
81 SendPacketAndWaitForResponseNoLock(llvm::StringRef payload,
82 StringExtractorGDBRemote &response);
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 virtual void OnRunPacketSent(bool first);
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000085
86private:
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 // Variables handling synchronization between the Continue thread and any
88 // other threads
89 // wishing to send packets over the connection. Either the continue thread has
90 // control over
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
94 // m_async_count.
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
98 // present
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
103 // control, continue
104 // thread needs to wait for them to finish (m_async_count goes down to 0).
105 std::mutex m_mutex;
106 std::condition_variable m_cv;
107 // Packet with which to resume after an async interrupt. Can be changed by an
108 // async thread
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
112 // stub does not
113 // respond to interrupt requests.
114 std::chrono::time_point<std::chrono::steady_clock> m_interrupt_time;
115 uint32_t m_async_count;
116 bool m_is_running;
117 bool m_should_stop; // Whether we should resume after a stop.
118 // end of continue thread synchronization block
Pavel Labath8c1b6bd2016-08-09 12:04:46 +0000119
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 // This handles the synchronization between individual async threads. For now
121 // they just use a
122 // simple mutex.
123 std::recursive_mutex m_async_mutex;
Pavel Labath8c1b6bd2016-08-09 12:04:46 +0000124
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 bool ShouldStop(const UnixSignals &signals,
126 StringExtractorGDBRemote &response);
Pavel Labath8c1b6bd2016-08-09 12:04:46 +0000127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 class ContinueLock {
129 public:
130 enum class LockResult { Success, Cancelled, Failed };
Pavel Labath8c1b6bd2016-08-09 12:04:46 +0000131
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 explicit ContinueLock(GDBRemoteClientBase &comm);
133 ~ContinueLock();
134 explicit operator bool() { return m_acquired; }
Pavel Labath8c1b6bd2016-08-09 12:04:46 +0000135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 LockResult lock();
Pavel Labath8c1b6bd2016-08-09 12:04:46 +0000137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 void unlock();
Pavel Labath8c1b6bd2016-08-09 12:04:46 +0000139
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 private:
141 GDBRemoteClientBase &m_comm;
142 bool m_acquired;
143 };
Pavel Labath8c1b6bd2016-08-09 12:04:46 +0000144};
145
146} // namespace process_gdb_remote
147} // namespace lldb_private
148
149#endif // liblldb_GDBRemoteCommunicationClient_h_