blob: f392895405ac009b897dc88111b6fd86a2cf2491 [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
Kate Stoneb9c1b512016-09-06 20:57:50 +000028 //
29 /// Processes async structured data.
30 ///
31 /// @return
32 /// true if the data was handled; otherwise, false.
33 //
34 virtual bool
35 HandleAsyncStructuredData(const StructuredData::ObjectSP &object_sp) = 0;
36 };
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000037
Kate Stoneb9c1b512016-09-06 20:57:50 +000038 GDBRemoteClientBase(const char *comm_name, const char *listener_name);
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000039
Kate Stoneb9c1b512016-09-06 20:57:50 +000040 bool SendAsyncSignal(int signo);
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000041
Kate Stoneb9c1b512016-09-06 20:57:50 +000042 bool Interrupt();
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000043
Kate Stoneb9c1b512016-09-06 20:57:50 +000044 lldb::StateType SendContinuePacketAndWaitForResponse(
45 ContinueDelegate &delegate, const UnixSignals &signals,
46 llvm::StringRef payload, StringExtractorGDBRemote &response);
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000047
Kate Stoneb9c1b512016-09-06 20:57:50 +000048 PacketResult SendPacketAndWaitForResponse(const char *payload, size_t len,
49 StringExtractorGDBRemote &response,
50 bool send_async) {
51 return SendPacketAndWaitForResponse(llvm::StringRef(payload, len), response,
52 send_async);
53 }
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000054
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 PacketResult SendPacketAndWaitForResponse(llvm::StringRef payload,
56 StringExtractorGDBRemote &response,
57 bool send_async);
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 bool SendvContPacket(llvm::StringRef payload,
60 StringExtractorGDBRemote &response);
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 class Lock {
63 public:
64 Lock(GDBRemoteClientBase &comm, bool interrupt);
65 ~Lock();
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000066
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 explicit operator bool() { return m_acquired; }
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000068
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 // Whether we had to interrupt the continue thread to acquire the
70 // connection.
71 bool DidInterrupt() const { return m_did_interrupt; }
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000072
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 private:
74 std::unique_lock<std::recursive_mutex> m_async_lock;
75 GDBRemoteClientBase &m_comm;
76 bool m_acquired;
77 bool m_did_interrupt;
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000078
Kate Stoneb9c1b512016-09-06 20:57:50 +000079 void SyncWithContinueThread(bool interrupt);
80 };
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000081
82protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 PacketResult
84 SendPacketAndWaitForResponseNoLock(llvm::StringRef payload,
85 StringExtractorGDBRemote &response);
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000086
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 virtual void OnRunPacketSent(bool first);
Pavel Labath8c1b6bd2016-08-09 12:04:46 +000088
89private:
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 // Variables handling synchronization between the Continue thread and any
91 // other threads
92 // wishing to send packets over the connection. Either the continue thread has
93 // control over
94 // the connection (m_is_running == true) or the connection is free for an
95 // arbitrary number of
96 // other senders to take which indicate their interest by incrementing
97 // m_async_count.
98 // Semantics of individual states:
99 // - m_continue_packet == false, m_async_count == 0: connection is free
100 // - m_continue_packet == true, m_async_count == 0: only continue thread is
101 // present
102 // - m_continue_packet == true, m_async_count > 0: continue thread has
103 // control, async threads
104 // should interrupt it and wait for it to set m_continue_packet to false
105 // - m_continue_packet == false, m_async_count > 0: async threads have
106 // control, continue
107 // thread needs to wait for them to finish (m_async_count goes down to 0).
108 std::mutex m_mutex;
109 std::condition_variable m_cv;
110 // Packet with which to resume after an async interrupt. Can be changed by an
111 // async thread
112 // e.g. to inject a signal.
113 std::string m_continue_packet;
114 // When was the interrupt packet sent. Used to make sure we time out if the
115 // stub does not
116 // respond to interrupt requests.
117 std::chrono::time_point<std::chrono::steady_clock> m_interrupt_time;
118 uint32_t m_async_count;
119 bool m_is_running;
120 bool m_should_stop; // Whether we should resume after a stop.
121 // end of continue thread synchronization block
Pavel Labath8c1b6bd2016-08-09 12:04:46 +0000122
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 // This handles the synchronization between individual async threads. For now
124 // they just use a
125 // simple mutex.
126 std::recursive_mutex m_async_mutex;
Pavel Labath8c1b6bd2016-08-09 12:04:46 +0000127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 bool ShouldStop(const UnixSignals &signals,
129 StringExtractorGDBRemote &response);
Pavel Labath8c1b6bd2016-08-09 12:04:46 +0000130
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 class ContinueLock {
132 public:
133 enum class LockResult { Success, Cancelled, Failed };
Pavel Labath8c1b6bd2016-08-09 12:04:46 +0000134
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 explicit ContinueLock(GDBRemoteClientBase &comm);
136 ~ContinueLock();
137 explicit operator bool() { return m_acquired; }
Pavel Labath8c1b6bd2016-08-09 12:04:46 +0000138
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 LockResult lock();
Pavel Labath8c1b6bd2016-08-09 12:04:46 +0000140
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 void unlock();
Pavel Labath8c1b6bd2016-08-09 12:04:46 +0000142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 private:
144 GDBRemoteClientBase &m_comm;
145 bool m_acquired;
146 };
Pavel Labath8c1b6bd2016-08-09 12:04:46 +0000147};
148
149} // namespace process_gdb_remote
150} // namespace lldb_private
151
152#endif // liblldb_GDBRemoteCommunicationClient_h_