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