xref: /llvm-project/lldb/source/Plugins/Process/gdb-remote/GDBRemoteClientBase.h (revision b9c1b51e45b845debb76d8658edabca70ca56079)
1 //===-- 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 
17 namespace lldb_private {
18 namespace process_gdb_remote {
19 
20 class GDBRemoteClientBase : public GDBRemoteCommunication {
21 public:
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;
27 
28     //
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   };
37 
38   GDBRemoteClientBase(const char *comm_name, const char *listener_name);
39 
40   bool SendAsyncSignal(int signo);
41 
42   bool Interrupt();
43 
44   lldb::StateType SendContinuePacketAndWaitForResponse(
45       ContinueDelegate &delegate, const UnixSignals &signals,
46       llvm::StringRef payload, StringExtractorGDBRemote &response);
47 
48   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   }
54 
55   PacketResult SendPacketAndWaitForResponse(llvm::StringRef payload,
56                                             StringExtractorGDBRemote &response,
57                                             bool send_async);
58 
59   bool SendvContPacket(llvm::StringRef payload,
60                        StringExtractorGDBRemote &response);
61 
62   class Lock {
63   public:
64     Lock(GDBRemoteClientBase &comm, bool interrupt);
65     ~Lock();
66 
67     explicit operator bool() { return m_acquired; }
68 
69     // Whether we had to interrupt the continue thread to acquire the
70     // connection.
71     bool DidInterrupt() const { return m_did_interrupt; }
72 
73   private:
74     std::unique_lock<std::recursive_mutex> m_async_lock;
75     GDBRemoteClientBase &m_comm;
76     bool m_acquired;
77     bool m_did_interrupt;
78 
79     void SyncWithContinueThread(bool interrupt);
80   };
81 
82 protected:
83   PacketResult
84   SendPacketAndWaitForResponseNoLock(llvm::StringRef payload,
85                                      StringExtractorGDBRemote &response);
86 
87   virtual void OnRunPacketSent(bool first);
88 
89 private:
90   // 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
122 
123   // 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;
127 
128   bool ShouldStop(const UnixSignals &signals,
129                   StringExtractorGDBRemote &response);
130 
131   class ContinueLock {
132   public:
133     enum class LockResult { Success, Cancelled, Failed };
134 
135     explicit ContinueLock(GDBRemoteClientBase &comm);
136     ~ContinueLock();
137     explicit operator bool() { return m_acquired; }
138 
139     LockResult lock();
140 
141     void unlock();
142 
143   private:
144     GDBRemoteClientBase &m_comm;
145     bool m_acquired;
146   };
147 };
148 
149 } // namespace process_gdb_remote
150 } // namespace lldb_private
151 
152 #endif // liblldb_GDBRemoteCommunicationClient_h_
153