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 /// Process asynchronously-received structured data. 30 /// 31 /// @param[in] data 32 /// The complete data packet, expected to start with JSON-async. 33 // ========================================================================= 34 virtual void HandleAsyncStructuredDataPacket(llvm::StringRef data) = 0; 35 }; 36 37 GDBRemoteClientBase(const char *comm_name, const char *listener_name); 38 39 bool SendAsyncSignal(int signo); 40 41 bool Interrupt(); 42 43 lldb::StateType SendContinuePacketAndWaitForResponse( 44 ContinueDelegate &delegate, const UnixSignals &signals, 45 llvm::StringRef payload, StringExtractorGDBRemote &response); 46 47 PacketResult SendPacketAndWaitForResponse(const char *payload, size_t len, 48 StringExtractorGDBRemote &response, 49 bool send_async) { 50 return SendPacketAndWaitForResponse(llvm::StringRef(payload, len), response, 51 send_async); 52 } 53 54 PacketResult SendPacketAndWaitForResponse(llvm::StringRef payload, 55 StringExtractorGDBRemote &response, 56 bool send_async); 57 58 bool SendvContPacket(llvm::StringRef payload, 59 StringExtractorGDBRemote &response); 60 61 class Lock { 62 public: 63 Lock(GDBRemoteClientBase &comm, bool interrupt); 64 ~Lock(); 65 66 explicit operator bool() { return m_acquired; } 67 68 // Whether we had to interrupt the continue thread to acquire the 69 // connection. 70 bool DidInterrupt() const { return m_did_interrupt; } 71 72 private: 73 std::unique_lock<std::recursive_mutex> m_async_lock; 74 GDBRemoteClientBase &m_comm; 75 bool m_acquired; 76 bool m_did_interrupt; 77 78 void SyncWithContinueThread(bool interrupt); 79 }; 80 81 protected: 82 PacketResult 83 SendPacketAndWaitForResponseNoLock(llvm::StringRef payload, 84 StringExtractorGDBRemote &response); 85 86 virtual void OnRunPacketSent(bool first); 87 88 private: 89 // Variables handling synchronization between the Continue thread and any 90 // other threads 91 // wishing to send packets over the connection. Either the continue thread has 92 // control over 93 // the connection (m_is_running == true) or the connection is free for an 94 // arbitrary number of 95 // other senders to take which indicate their interest by incrementing 96 // m_async_count. 97 // Semantics of individual states: 98 // - m_continue_packet == false, m_async_count == 0: connection is free 99 // - m_continue_packet == true, m_async_count == 0: only continue thread is 100 // present 101 // - m_continue_packet == true, m_async_count > 0: continue thread has 102 // control, async threads 103 // should interrupt it and wait for it to set m_continue_packet to false 104 // - m_continue_packet == false, m_async_count > 0: async threads have 105 // control, continue 106 // thread needs to wait for them to finish (m_async_count goes down to 0). 107 std::mutex m_mutex; 108 std::condition_variable m_cv; 109 // Packet with which to resume after an async interrupt. Can be changed by an 110 // async thread 111 // e.g. to inject a signal. 112 std::string m_continue_packet; 113 // When was the interrupt packet sent. Used to make sure we time out if the 114 // stub does not 115 // respond to interrupt requests. 116 std::chrono::time_point<std::chrono::steady_clock> m_interrupt_time; 117 uint32_t m_async_count; 118 bool m_is_running; 119 bool m_should_stop; // Whether we should resume after a stop. 120 // end of continue thread synchronization block 121 122 // This handles the synchronization between individual async threads. For now 123 // they just use a 124 // simple mutex. 125 std::recursive_mutex m_async_mutex; 126 127 bool ShouldStop(const UnixSignals &signals, 128 StringExtractorGDBRemote &response); 129 130 class ContinueLock { 131 public: 132 enum class LockResult { Success, Cancelled, Failed }; 133 134 explicit ContinueLock(GDBRemoteClientBase &comm); 135 ~ContinueLock(); 136 explicit operator bool() { return m_acquired; } 137 138 LockResult lock(); 139 140 void unlock(); 141 142 private: 143 GDBRemoteClientBase &m_comm; 144 bool m_acquired; 145 }; 146 }; 147 148 } // namespace process_gdb_remote 149 } // namespace lldb_private 150 151 #endif // liblldb_GDBRemoteCommunicationClient_h_ 152