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