Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- GDBRemoteCommunication.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_GDBRemoteCommunication_h_ |
| 11 | #define liblldb_GDBRemoteCommunication_h_ |
| 12 | |
| 13 | // C Includes |
| 14 | // C++ Includes |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 15 | #include <condition_variable> |
| 16 | #include <mutex> |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 17 | #include <queue> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 18 | #include <string> |
Eugene Zelenko | edb35d9 | 2015-10-24 01:08:35 +0000 | [diff] [blame] | 19 | #include <vector> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | |
| 21 | // Other libraries and framework includes |
| 22 | // Project includes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #include "lldb/Core/Communication.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | #include "lldb/Core/Listener.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 25 | #include "lldb/Host/HostThread.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | #include "lldb/Host/Predicate.h" |
Peter Collingbourne | ba23ca0 | 2011-06-18 23:52:14 +0000 | [diff] [blame] | 27 | #include "lldb/Host/TimeValue.h" |
Tamas Berghammer | ccd6cff | 2015-12-08 14:08:19 +0000 | [diff] [blame] | 28 | #include "lldb/Interpreter/Args.h" |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 29 | #include "lldb/lldb-public.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 31 | #include "Utility/StringExtractorGDBRemote.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 33 | namespace lldb_private { |
| 34 | namespace process_gdb_remote { |
| 35 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 36 | typedef enum { |
| 37 | eStoppointInvalid = -1, |
| 38 | eBreakpointSoftware = 0, |
| 39 | eBreakpointHardware, |
| 40 | eWatchpointWrite, |
| 41 | eWatchpointRead, |
| 42 | eWatchpointReadWrite |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 43 | } GDBStoppointType; |
| 44 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 45 | enum class CompressionType { |
| 46 | None = 0, // no compression |
| 47 | ZlibDeflate, // zlib's deflate compression scheme, requires zlib or Apple's |
| 48 | // libcompression |
| 49 | LZFSE, // an Apple compression scheme, requires Apple's libcompression |
| 50 | LZ4, // lz compression - called "lz4 raw" in libcompression terms, compat with |
| 51 | // https://code.google.com/p/lz4/ |
| 52 | LZMA, // Lempel–Ziv–Markov chain algorithm |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 55 | class ProcessGDBRemote; |
| 56 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 57 | class GDBRemoteCommunication : public Communication { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 58 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 59 | enum { |
| 60 | eBroadcastBitRunPacketSent = kLoUserBroadcastBit, |
| 61 | eBroadcastBitGdbReadThreadGotNotify = |
| 62 | kLoUserBroadcastBit << 1 // Sent when we received a notify packet. |
| 63 | }; |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 64 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 65 | enum class PacketType { Invalid = 0, Standard, Notify }; |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 66 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 67 | enum class PacketResult { |
| 68 | Success = 0, // Success |
| 69 | ErrorSendFailed, // Error sending the packet |
| 70 | ErrorSendAck, // Didn't get an ack back after sending a packet |
| 71 | ErrorReplyFailed, // Error getting the reply |
| 72 | ErrorReplyTimeout, // Timed out waiting for reply |
| 73 | ErrorReplyInvalid, // Got a reply but it wasn't valid for the packet that |
| 74 | // was sent |
| 75 | ErrorReplyAck, // Sending reply ack failed |
| 76 | ErrorDisconnected, // We were disconnected |
| 77 | ErrorNoSequenceLock // We couldn't get the sequence lock for a multi-packet |
| 78 | // request |
| 79 | }; |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 80 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 81 | // Class to change the timeout for a given scope and restore it to the |
| 82 | // original value when the |
| 83 | // created ScopedTimeout object got out of scope |
| 84 | class ScopedTimeout { |
| 85 | public: |
| 86 | ScopedTimeout(GDBRemoteCommunication &gdb_comm, uint32_t timeout); |
| 87 | ~ScopedTimeout(); |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 88 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 89 | private: |
| 90 | GDBRemoteCommunication &m_gdb_comm; |
| 91 | uint32_t m_saved_timeout; |
| 92 | }; |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 93 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 94 | GDBRemoteCommunication(const char *comm_name, const char *listener_name); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 95 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 96 | ~GDBRemoteCommunication() override; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 97 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 98 | PacketResult GetAck(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 99 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 100 | size_t SendAck(); |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 102 | size_t SendNack(); |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 103 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 104 | char CalculcateChecksum(llvm::StringRef payload); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 105 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 106 | PacketType CheckForPacket(const uint8_t *src, size_t src_len, |
| 107 | StringExtractorGDBRemote &packet); |
Eugene Zelenko | edb35d9 | 2015-10-24 01:08:35 +0000 | [diff] [blame] | 108 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 109 | bool GetSendAcks() { return m_send_acks; } |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 110 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 111 | //------------------------------------------------------------------ |
| 112 | // Set the global packet timeout. |
| 113 | // |
| 114 | // For clients, this is the timeout that gets used when sending |
| 115 | // packets and waiting for responses. For servers, this might not |
| 116 | // get used, and if it doesn't this should be moved to the |
| 117 | // GDBRemoteCommunicationClient. |
| 118 | //------------------------------------------------------------------ |
| 119 | uint32_t SetPacketTimeout(uint32_t packet_timeout) { |
| 120 | const uint32_t old_packet_timeout = m_packet_timeout; |
| 121 | m_packet_timeout = packet_timeout; |
| 122 | return old_packet_timeout; |
| 123 | } |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 124 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 125 | uint32_t GetPacketTimeoutInMicroSeconds() const { |
| 126 | return m_packet_timeout * TimeValue::MicroSecPerSec; |
| 127 | } |
Eugene Zelenko | edb35d9 | 2015-10-24 01:08:35 +0000 | [diff] [blame] | 128 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 129 | //------------------------------------------------------------------ |
| 130 | // Start a debugserver instance on the current host using the |
| 131 | // supplied connection URL. |
| 132 | //------------------------------------------------------------------ |
| 133 | Error StartDebugserverProcess( |
| 134 | const char *url, |
| 135 | Platform *platform, // If non nullptr, then check with the platform for |
| 136 | // the GDB server binary if it can't be located |
| 137 | ProcessLaunchInfo &launch_info, uint16_t *port, const Args *inferior_args, |
| 138 | int pass_comm_fd); // Communication file descriptor to pass during |
| 139 | // fork/exec to avoid having to connect/accept |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 140 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 141 | void DumpHistory(Stream &strm); |
| 142 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 143 | protected: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 144 | class History { |
| 145 | public: |
| 146 | enum PacketType { |
| 147 | ePacketTypeInvalid = 0, |
| 148 | ePacketTypeSend, |
| 149 | ePacketTypeRecv |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 150 | }; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 151 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 152 | struct Entry { |
| 153 | Entry() |
| 154 | : packet(), type(ePacketTypeInvalid), bytes_transmitted(0), |
| 155 | packet_idx(0), tid(LLDB_INVALID_THREAD_ID) {} |
Eugene Zelenko | edb35d9 | 2015-10-24 01:08:35 +0000 | [diff] [blame] | 156 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 157 | void Clear() { |
| 158 | packet.clear(); |
| 159 | type = ePacketTypeInvalid; |
| 160 | bytes_transmitted = 0; |
| 161 | packet_idx = 0; |
| 162 | tid = LLDB_INVALID_THREAD_ID; |
| 163 | } |
| 164 | std::string packet; |
| 165 | PacketType type; |
| 166 | uint32_t bytes_transmitted; |
| 167 | uint32_t packet_idx; |
| 168 | lldb::tid_t tid; |
| 169 | }; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 170 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 171 | History(uint32_t size); |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 172 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 173 | ~History(); |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 174 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 175 | // For single char packets for ack, nack and /x03 |
| 176 | void AddPacket(char packet_char, PacketType type, |
| 177 | uint32_t bytes_transmitted); |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 178 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 179 | void AddPacket(const std::string &src, uint32_t src_len, PacketType type, |
| 180 | uint32_t bytes_transmitted); |
| 181 | |
| 182 | void Dump(Stream &strm) const; |
| 183 | |
| 184 | void Dump(Log *log) const; |
| 185 | |
| 186 | bool DidDumpToLog() const { return m_dumped_to_log; } |
| 187 | |
| 188 | protected: |
| 189 | uint32_t GetFirstSavedPacketIndex() const { |
| 190 | if (m_total_packet_count < m_packets.size()) |
| 191 | return 0; |
| 192 | else |
| 193 | return m_curr_idx + 1; |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 196 | uint32_t GetNumPacketsInHistory() const { |
| 197 | if (m_total_packet_count < m_packets.size()) |
| 198 | return m_total_packet_count; |
| 199 | else |
| 200 | return (uint32_t)m_packets.size(); |
| 201 | } |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 202 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 203 | uint32_t GetNextIndex() { |
| 204 | ++m_total_packet_count; |
| 205 | const uint32_t idx = m_curr_idx; |
| 206 | m_curr_idx = NormalizeIndex(idx + 1); |
| 207 | return idx; |
| 208 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 209 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 210 | uint32_t NormalizeIndex(uint32_t i) const { return i % m_packets.size(); } |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 211 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 212 | std::vector<Entry> m_packets; |
| 213 | uint32_t m_curr_idx; |
| 214 | uint32_t m_total_packet_count; |
| 215 | mutable bool m_dumped_to_log; |
| 216 | }; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 217 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 218 | uint32_t m_packet_timeout; |
| 219 | uint32_t m_echo_number; |
| 220 | LazyBool m_supports_qEcho; |
| 221 | History m_history; |
| 222 | bool m_send_acks; |
| 223 | bool m_is_platform; // Set to true if this class represents a platform, |
| 224 | // false if this class represents a debug session for |
| 225 | // a single process |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 226 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 227 | CompressionType m_compression_type; |
| 228 | |
| 229 | PacketResult SendPacketNoLock(llvm::StringRef payload); |
| 230 | |
| 231 | PacketResult ReadPacket(StringExtractorGDBRemote &response, |
| 232 | uint32_t timeout_usec, bool sync_on_timeout); |
| 233 | |
| 234 | // Pop a packet from the queue in a thread safe manner |
| 235 | PacketResult PopPacketFromQueue(StringExtractorGDBRemote &response, |
| 236 | uint32_t timeout_usec); |
| 237 | |
| 238 | PacketResult |
| 239 | WaitForPacketWithTimeoutMicroSecondsNoLock(StringExtractorGDBRemote &response, |
| 240 | uint32_t timeout_usec, |
| 241 | bool sync_on_timeout); |
| 242 | |
| 243 | bool CompressionIsEnabled() { |
| 244 | return m_compression_type != CompressionType::None; |
| 245 | } |
| 246 | |
| 247 | // If compression is enabled, decompress the packet in m_bytes and update |
| 248 | // m_bytes with the uncompressed version. |
| 249 | // Returns 'true' packet was decompressed and m_bytes is the now-decompressed |
| 250 | // text. |
| 251 | // Returns 'false' if unable to decompress or if the checksum was invalid. |
| 252 | // |
| 253 | // NB: Once the packet has been decompressed, checksum cannot be computed |
| 254 | // based |
| 255 | // on m_bytes. The checksum was for the compressed packet. |
| 256 | bool DecompressPacket(); |
| 257 | |
| 258 | Error StartListenThread(const char *hostname = "127.0.0.1", |
| 259 | uint16_t port = 0); |
| 260 | |
| 261 | bool JoinListenThread(); |
| 262 | |
| 263 | static lldb::thread_result_t ListenThread(lldb::thread_arg_t arg); |
| 264 | |
| 265 | // GDB-Remote read thread |
| 266 | // . this thread constantly tries to read from the communication |
| 267 | // class and stores all packets received in a queue. The usual |
| 268 | // threads read requests simply pop packets off the queue in the |
| 269 | // usual order. |
| 270 | // This setup allows us to intercept and handle async packets, such |
| 271 | // as the notify packet. |
| 272 | |
| 273 | // This method is defined as part of communication.h |
| 274 | // when the read thread gets any bytes it will pass them on to this function |
| 275 | void AppendBytesToCache(const uint8_t *bytes, size_t len, bool broadcast, |
| 276 | lldb::ConnectionStatus status) override; |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 277 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 278 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 279 | std::queue<StringExtractorGDBRemote> m_packet_queue; // The packet queue |
| 280 | std::mutex m_packet_queue_mutex; // Mutex for accessing queue |
| 281 | std::condition_variable |
| 282 | m_condition_queue_not_empty; // Condition variable to wait for packets |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 283 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 284 | HostThread m_listen_thread; |
| 285 | std::string m_listen_url; |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 286 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame^] | 287 | DISALLOW_COPY_AND_ASSIGN(GDBRemoteCommunication); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 288 | }; |
| 289 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 290 | } // namespace process_gdb_remote |
| 291 | } // namespace lldb_private |
| 292 | |
Eugene Zelenko | edb35d9 | 2015-10-24 01:08:35 +0000 | [diff] [blame] | 293 | #endif // liblldb_GDBRemoteCommunication_h_ |