1 //===-- GDBRemoteClientBase.h -----------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECLIENTBASE_H 10 #define LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECLIENTBASE_H 11 12 #include "GDBRemoteCommunication.h" 13 14 #include <condition_variable> 15 16 namespace lldb_private { 17 namespace process_gdb_remote { 18 19 class GDBRemoteClientBase : public GDBRemoteCommunication { 20 public: 21 struct ContinueDelegate { 22 virtual ~ContinueDelegate(); 23 virtual void HandleAsyncStdout(llvm::StringRef out) = 0; 24 virtual void HandleAsyncMisc(llvm::StringRef data) = 0; 25 virtual void HandleStopReply() = 0; 26 27 /// Process asynchronously-received structured data. 28 /// 29 /// \param[in] data 30 /// The complete data packet, expected to start with JSON-async. 31 virtual void HandleAsyncStructuredDataPacket(llvm::StringRef data) = 0; 32 }; 33 34 GDBRemoteClientBase(const char *comm_name, const char *listener_name); 35 36 bool SendAsyncSignal(int signo, std::chrono::seconds interrupt_timeout); 37 38 bool Interrupt(std::chrono::seconds interrupt_timeout); 39 40 lldb::StateType SendContinuePacketAndWaitForResponse( 41 ContinueDelegate &delegate, const UnixSignals &signals, 42 llvm::StringRef payload, std::chrono::seconds interrupt_timeout, 43 StringExtractorGDBRemote &response); 44 45 // If interrupt_timeout == 0 seconds, don't interrupt the target. 46 // Only send the packet if the target is stopped. 47 // If you want to use this mode, use the fact that the timeout is defaulted 48 // so it's clear from the call-site that you are using no-interrupt. 49 // If it is non-zero, interrupt the target if it is running, and 50 // send the packet. 51 // It the target doesn't respond within the given timeout, it returns 52 // ErrorReplyTimeout. 53 PacketResult SendPacketAndWaitForResponse( 54 llvm::StringRef payload, StringExtractorGDBRemote &response, 55 std::chrono::seconds interrupt_timeout = std::chrono::seconds(0)); 56 57 PacketResult SendPacketAndReceiveResponseWithOutputSupport( 58 llvm::StringRef payload, StringExtractorGDBRemote &response, 59 std::chrono::seconds interrupt_timeout, 60 llvm::function_ref<void(llvm::StringRef)> output_callback); 61 62 class Lock { 63 public: 64 // If interrupt_timeout == 0 seconds, only take the lock if the target is 65 // not running. If using this option, use the fact that the 66 // interrupt_timeout is defaulted so it will be obvious at the call site 67 // that you are choosing this mode. If it is non-zero, interrupt the target 68 // if it is running, waiting for the given timeout for the interrupt to 69 // succeed. 70 Lock(GDBRemoteClientBase &comm, 71 std::chrono::seconds interrupt_timeout = std::chrono::seconds(0)); 72 ~Lock(); 73 74 explicit operator bool() { return m_acquired; } 75 76 // Whether we had to interrupt the continue thread to acquire the 77 // connection. 78 bool DidInterrupt() const { return m_did_interrupt; } 79 80 private: 81 std::unique_lock<std::recursive_mutex> m_async_lock; 82 GDBRemoteClientBase &m_comm; 83 std::chrono::seconds m_interrupt_timeout; 84 bool m_acquired; 85 bool m_did_interrupt; 86 87 void SyncWithContinueThread(); 88 }; 89 90 protected: 91 PacketResult 92 SendPacketAndWaitForResponseNoLock(llvm::StringRef payload, 93 StringExtractorGDBRemote &response); 94 95 virtual void OnRunPacketSent(bool first); 96 97 private: 98 /// Variables handling synchronization between the Continue thread and any 99 /// other threads wishing to send packets over the connection. Either the 100 /// continue thread has control over the connection (m_is_running == true) or 101 /// the connection is free for an arbitrary number of other senders to take 102 /// which indicate their interest by incrementing m_async_count. 103 /// 104 /// Semantics of individual states: 105 /// 106 /// - m_continue_packet == false, m_async_count == 0: 107 /// connection is free 108 /// - m_continue_packet == true, m_async_count == 0: 109 /// only continue thread is present 110 /// - m_continue_packet == true, m_async_count > 0: 111 /// continue thread has control, async threads should interrupt it and wait 112 /// for it to set m_continue_packet to false 113 /// - m_continue_packet == false, m_async_count > 0: 114 /// async threads have control, continue thread needs to wait for them to 115 /// finish (m_async_count goes down to 0). 116 /// @{ 117 std::mutex m_mutex; 118 std::condition_variable m_cv; 119 120 /// Packet with which to resume after an async interrupt. Can be changed by 121 /// an async thread e.g. to inject a signal. 122 std::string m_continue_packet; 123 124 /// When was the interrupt packet sent. Used to make sure we time out if the 125 /// stub does not respond to interrupt requests. 126 std::chrono::time_point<std::chrono::steady_clock> m_interrupt_endpoint; 127 128 /// Number of threads interested in sending. 129 uint32_t m_async_count; 130 131 /// Whether the continue thread has control. 132 bool m_is_running; 133 134 /// Whether we should resume after a stop. 135 bool m_should_stop; 136 /// @} 137 138 /// This handles the synchronization between individual async threads. For 139 /// now they just use a simple mutex. 140 std::recursive_mutex m_async_mutex; 141 142 bool ShouldStop(const UnixSignals &signals, 143 StringExtractorGDBRemote &response); 144 145 class ContinueLock { 146 public: 147 enum class LockResult { Success, Cancelled, Failed }; 148 149 explicit ContinueLock(GDBRemoteClientBase &comm); 150 ~ContinueLock(); 151 explicit operator bool() { return m_acquired; } 152 153 LockResult lock(); 154 155 void unlock(); 156 157 private: 158 GDBRemoteClientBase &m_comm; 159 bool m_acquired; 160 }; 161 }; 162 163 } // namespace process_gdb_remote 164 } // namespace lldb_private 165 166 #endif // LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECLIENTBASE_H 167