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