Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- GDBRemoteCommunication.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 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef liblldb_GDBRemoteCommunication_h_ |
| 10 | #define liblldb_GDBRemoteCommunication_h_ |
| 11 | |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 12 | #include "GDBRemoteCommunicationHistory.h" |
| 13 | |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 14 | #include <condition_variable> |
| 15 | #include <mutex> |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 16 | #include <queue> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 17 | #include <string> |
Eugene Zelenko | edb35d9 | 2015-10-24 01:08:35 +0000 | [diff] [blame] | 18 | #include <vector> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Communication.h" |
Raphael Isemann | 46508f6 | 2019-01-25 08:21:47 +0000 | [diff] [blame] | 21 | #include "lldb/Host/Config.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 22 | #include "lldb/Host/HostThread.h" |
Pavel Labath | 145d95c | 2018-04-17 18:53:35 +0000 | [diff] [blame] | 23 | #include "lldb/Utility/Args.h" |
Pavel Labath | 181b823 | 2018-12-14 15:59:49 +0000 | [diff] [blame] | 24 | #include "lldb/Utility/Listener.h" |
Raphael Isemann | 7fae493 | 2018-08-30 17:51:10 +0000 | [diff] [blame] | 25 | #include "lldb/Utility/Predicate.h" |
Pavel Labath | 9af71b3 | 2018-03-20 16:14:00 +0000 | [diff] [blame] | 26 | #include "lldb/Utility/StringExtractorGDBRemote.h" |
Pavel Labath | 181b823 | 2018-12-14 15:59:49 +0000 | [diff] [blame] | 27 | #include "lldb/lldb-public.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 29 | namespace lldb_private { |
| 30 | namespace process_gdb_remote { |
| 31 | |
Fangrui Song | efe8e7e | 2019-05-14 08:55:50 +0000 | [diff] [blame] | 32 | enum GDBStoppointType { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 33 | eStoppointInvalid = -1, |
| 34 | eBreakpointSoftware = 0, |
| 35 | eBreakpointHardware, |
| 36 | eWatchpointWrite, |
| 37 | eWatchpointRead, |
| 38 | eWatchpointReadWrite |
Fangrui Song | efe8e7e | 2019-05-14 08:55:50 +0000 | [diff] [blame] | 39 | }; |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 40 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 41 | enum class CompressionType { |
| 42 | None = 0, // no compression |
| 43 | ZlibDeflate, // zlib's deflate compression scheme, requires zlib or Apple's |
| 44 | // libcompression |
| 45 | LZFSE, // an Apple compression scheme, requires Apple's libcompression |
| 46 | LZ4, // lz compression - called "lz4 raw" in libcompression terms, compat with |
| 47 | // https://code.google.com/p/lz4/ |
| 48 | LZMA, // Lempel–Ziv–Markov chain algorithm |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | class ProcessGDBRemote; |
| 52 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 53 | class GDBRemoteCommunication : public Communication { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | enum { |
| 56 | eBroadcastBitRunPacketSent = kLoUserBroadcastBit, |
| 57 | eBroadcastBitGdbReadThreadGotNotify = |
| 58 | kLoUserBroadcastBit << 1 // Sent when we received a notify packet. |
| 59 | }; |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 60 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | enum class PacketType { Invalid = 0, Standard, Notify }; |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 62 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | enum class PacketResult { |
| 64 | Success = 0, // Success |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 65 | ErrorSendFailed, // Status sending the packet |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 66 | ErrorSendAck, // Didn't get an ack back after sending a packet |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 67 | ErrorReplyFailed, // Status getting the reply |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 68 | ErrorReplyTimeout, // Timed out waiting for reply |
| 69 | ErrorReplyInvalid, // Got a reply but it wasn't valid for the packet that |
| 70 | // was sent |
| 71 | ErrorReplyAck, // Sending reply ack failed |
| 72 | ErrorDisconnected, // We were disconnected |
| 73 | ErrorNoSequenceLock // We couldn't get the sequence lock for a multi-packet |
| 74 | // request |
| 75 | }; |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 76 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 77 | // Class to change the timeout for a given scope and restore it to the |
| 78 | // original value when the |
| 79 | // created ScopedTimeout object got out of scope |
| 80 | class ScopedTimeout { |
| 81 | public: |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 82 | ScopedTimeout(GDBRemoteCommunication &gdb_comm, |
| 83 | std::chrono::seconds timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | ~ScopedTimeout(); |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 85 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | private: |
| 87 | GDBRemoteCommunication &m_gdb_comm; |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 88 | std::chrono::seconds m_saved_timeout; |
Greg Clayton | 8457709 | 2017-04-17 16:20:22 +0000 | [diff] [blame] | 89 | // Don't ever reduce the timeout for a packet, only increase it. If the |
| 90 | // requested timeout if less than the current timeout, we don't set it |
| 91 | // and won't need to restore it. |
| 92 | bool m_timeout_modified; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | }; |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 94 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 95 | GDBRemoteCommunication(const char *comm_name, const char *listener_name); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 96 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 97 | ~GDBRemoteCommunication() override; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | PacketResult GetAck(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 101 | size_t SendAck(); |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 102 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 103 | size_t SendNack(); |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 104 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 105 | char CalculcateChecksum(llvm::StringRef payload); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | PacketType CheckForPacket(const uint8_t *src, size_t src_len, |
| 108 | StringExtractorGDBRemote &packet); |
Eugene Zelenko | edb35d9 | 2015-10-24 01:08:35 +0000 | [diff] [blame] | 109 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 110 | bool GetSendAcks() { return m_send_acks; } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 111 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | // Set the global packet timeout. |
| 113 | // |
| 114 | // For clients, this is the timeout that gets used when sending |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 115 | // packets and waiting for responses. For servers, this is used when waiting |
| 116 | // for ACKs. |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 117 | std::chrono::seconds SetPacketTimeout(std::chrono::seconds packet_timeout) { |
| 118 | const auto old_packet_timeout = m_packet_timeout; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | m_packet_timeout = packet_timeout; |
| 120 | return old_packet_timeout; |
| 121 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 122 | |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 123 | std::chrono::seconds GetPacketTimeout() const { return m_packet_timeout; } |
Eugene Zelenko | edb35d9 | 2015-10-24 01:08:35 +0000 | [diff] [blame] | 124 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 125 | // Start a debugserver instance on the current host using the |
| 126 | // supplied connection URL. |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 127 | Status StartDebugserverProcess( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 128 | const char *url, |
| 129 | Platform *platform, // If non nullptr, then check with the platform for |
| 130 | // the GDB server binary if it can't be located |
| 131 | ProcessLaunchInfo &launch_info, uint16_t *port, const Args *inferior_args, |
| 132 | int pass_comm_fd); // Communication file descriptor to pass during |
| 133 | // fork/exec to avoid having to connect/accept |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 134 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 135 | void DumpHistory(Stream &strm); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 136 | void SetHistoryStream(llvm::raw_ostream *strm); |
| 137 | |
| 138 | static llvm::Error ConnectLocally(GDBRemoteCommunication &client, |
| 139 | GDBRemoteCommunication &server); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 141 | protected: |
Pavel Labath | 3aa0491 | 2016-10-31 17:19:42 +0000 | [diff] [blame] | 142 | std::chrono::seconds m_packet_timeout; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 143 | uint32_t m_echo_number; |
| 144 | LazyBool m_supports_qEcho; |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 145 | GDBRemoteCommunicationHistory m_history; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 146 | bool m_send_acks; |
| 147 | bool m_is_platform; // Set to true if this class represents a platform, |
| 148 | // false if this class represents a debug session for |
| 149 | // a single process |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 150 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 151 | CompressionType m_compression_type; |
| 152 | |
| 153 | PacketResult SendPacketNoLock(llvm::StringRef payload); |
Jonas Devlieghere | 9e046f0 | 2018-11-13 19:18:16 +0000 | [diff] [blame] | 154 | PacketResult SendRawPacketNoLock(llvm::StringRef payload, |
| 155 | bool skip_ack = false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 156 | |
| 157 | PacketResult ReadPacket(StringExtractorGDBRemote &response, |
Pavel Labath | 1eff73c | 2016-11-24 10:54:49 +0000 | [diff] [blame] | 158 | Timeout<std::micro> timeout, bool sync_on_timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 159 | |
Pavel Labath | 7da8475 | 2018-01-10 14:39:08 +0000 | [diff] [blame] | 160 | PacketResult ReadPacketWithOutputSupport( |
| 161 | StringExtractorGDBRemote &response, Timeout<std::micro> timeout, |
| 162 | bool sync_on_timeout, |
| 163 | llvm::function_ref<void(llvm::StringRef)> output_callback); |
| 164 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 165 | // Pop a packet from the queue in a thread safe manner |
| 166 | PacketResult PopPacketFromQueue(StringExtractorGDBRemote &response, |
Pavel Labath | 1eff73c | 2016-11-24 10:54:49 +0000 | [diff] [blame] | 167 | Timeout<std::micro> timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 168 | |
Pavel Labath | 1eff73c | 2016-11-24 10:54:49 +0000 | [diff] [blame] | 169 | PacketResult WaitForPacketNoLock(StringExtractorGDBRemote &response, |
| 170 | Timeout<std::micro> timeout, |
| 171 | bool sync_on_timeout); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 172 | |
| 173 | bool CompressionIsEnabled() { |
| 174 | return m_compression_type != CompressionType::None; |
| 175 | } |
| 176 | |
| 177 | // If compression is enabled, decompress the packet in m_bytes and update |
| 178 | // m_bytes with the uncompressed version. |
| 179 | // Returns 'true' packet was decompressed and m_bytes is the now-decompressed |
| 180 | // text. |
| 181 | // Returns 'false' if unable to decompress or if the checksum was invalid. |
| 182 | // |
| 183 | // NB: Once the packet has been decompressed, checksum cannot be computed |
| 184 | // based |
| 185 | // on m_bytes. The checksum was for the compressed packet. |
| 186 | bool DecompressPacket(); |
| 187 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 188 | Status StartListenThread(const char *hostname = "127.0.0.1", |
| 189 | uint16_t port = 0); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 190 | |
| 191 | bool JoinListenThread(); |
| 192 | |
| 193 | static lldb::thread_result_t ListenThread(lldb::thread_arg_t arg); |
| 194 | |
| 195 | // GDB-Remote read thread |
| 196 | // . this thread constantly tries to read from the communication |
| 197 | // class and stores all packets received in a queue. The usual |
| 198 | // threads read requests simply pop packets off the queue in the |
| 199 | // usual order. |
| 200 | // This setup allows us to intercept and handle async packets, such |
| 201 | // as the notify packet. |
| 202 | |
| 203 | // This method is defined as part of communication.h |
| 204 | // when the read thread gets any bytes it will pass them on to this function |
| 205 | void AppendBytesToCache(const uint8_t *bytes, size_t len, bool broadcast, |
| 206 | lldb::ConnectionStatus status) override; |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 208 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | std::queue<StringExtractorGDBRemote> m_packet_queue; // The packet queue |
| 210 | std::mutex m_packet_queue_mutex; // Mutex for accessing queue |
| 211 | std::condition_variable |
| 212 | m_condition_queue_not_empty; // Condition variable to wait for packets |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 213 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 214 | HostThread m_listen_thread; |
| 215 | std::string m_listen_url; |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 216 | |
Raphael Isemann | 46508f6 | 2019-01-25 08:21:47 +0000 | [diff] [blame] | 217 | #if defined(HAVE_LIBCOMPRESSION) |
| 218 | CompressionType m_decompression_scratch_type = CompressionType::None; |
| 219 | void *m_decompression_scratch = nullptr; |
| 220 | #endif |
Jason Molenda | 4a793c8 | 2018-12-18 23:02:50 +0000 | [diff] [blame] | 221 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 222 | DISALLOW_COPY_AND_ASSIGN(GDBRemoteCommunication); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 225 | } // namespace process_gdb_remote |
| 226 | } // namespace lldb_private |
| 227 | |
Pavel Labath | 1ebc85f | 2017-11-09 15:45:09 +0000 | [diff] [blame] | 228 | namespace llvm { |
| 229 | template <> |
| 230 | struct format_provider< |
| 231 | lldb_private::process_gdb_remote::GDBRemoteCommunication::PacketResult> { |
| 232 | static void format(const lldb_private::process_gdb_remote:: |
| 233 | GDBRemoteCommunication::PacketResult &state, |
| 234 | raw_ostream &Stream, StringRef Style); |
| 235 | }; |
| 236 | } // namespace llvm |
| 237 | |
Eugene Zelenko | edb35d9 | 2015-10-24 01:08:35 +0000 | [diff] [blame] | 238 | #endif // liblldb_GDBRemoteCommunication_h_ |