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 #include "GDBRemoteCommunicationHistory.h" 14 15 #include <condition_variable> 16 #include <mutex> 17 #include <queue> 18 #include <string> 19 #include <vector> 20 21 #include "lldb/Core/Communication.h" 22 #include "lldb/Host/HostThread.h" 23 #include "lldb/Utility/Args.h" 24 #include "lldb/Utility/Listener.h" 25 #include "lldb/Utility/Predicate.h" 26 #include "lldb/Utility/StringExtractorGDBRemote.h" 27 #include "lldb/lldb-public.h" 28 29 namespace lldb_private { 30 namespace process_gdb_remote { 31 32 typedef enum { 33 eStoppointInvalid = -1, 34 eBreakpointSoftware = 0, 35 eBreakpointHardware, 36 eWatchpointWrite, 37 eWatchpointRead, 38 eWatchpointReadWrite 39 } GDBStoppointType; 40 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 49 }; 50 51 class ProcessGDBRemote; 52 53 class GDBRemoteCommunication : public Communication { 54 public: 55 enum { 56 eBroadcastBitRunPacketSent = kLoUserBroadcastBit, 57 eBroadcastBitGdbReadThreadGotNotify = 58 kLoUserBroadcastBit << 1 // Sent when we received a notify packet. 59 }; 60 61 enum class PacketType { Invalid = 0, Standard, Notify }; 62 63 enum class PacketResult { 64 Success = 0, // Success 65 ErrorSendFailed, // Status sending the packet 66 ErrorSendAck, // Didn't get an ack back after sending a packet 67 ErrorReplyFailed, // Status getting the reply 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 }; 76 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: 82 ScopedTimeout(GDBRemoteCommunication &gdb_comm, 83 std::chrono::seconds timeout); 84 ~ScopedTimeout(); 85 86 private: 87 GDBRemoteCommunication &m_gdb_comm; 88 std::chrono::seconds m_saved_timeout; 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; 93 }; 94 95 GDBRemoteCommunication(const char *comm_name, const char *listener_name); 96 97 ~GDBRemoteCommunication() override; 98 99 PacketResult GetAck(); 100 101 size_t SendAck(); 102 103 size_t SendNack(); 104 105 char CalculcateChecksum(llvm::StringRef payload); 106 107 PacketType CheckForPacket(const uint8_t *src, size_t src_len, 108 StringExtractorGDBRemote &packet); 109 GetSendAcks()110 bool GetSendAcks() { return m_send_acks; } 111 112 //------------------------------------------------------------------ 113 // Set the global packet timeout. 114 // 115 // For clients, this is the timeout that gets used when sending 116 // packets and waiting for responses. For servers, this is used when waiting 117 // for ACKs. 118 //------------------------------------------------------------------ SetPacketTimeout(std::chrono::seconds packet_timeout)119 std::chrono::seconds SetPacketTimeout(std::chrono::seconds packet_timeout) { 120 const auto old_packet_timeout = m_packet_timeout; 121 m_packet_timeout = packet_timeout; 122 return old_packet_timeout; 123 } 124 GetPacketTimeout()125 std::chrono::seconds GetPacketTimeout() const { return m_packet_timeout; } 126 127 //------------------------------------------------------------------ 128 // Start a debugserver instance on the current host using the 129 // supplied connection URL. 130 //------------------------------------------------------------------ 131 Status StartDebugserverProcess( 132 const char *url, 133 Platform *platform, // If non nullptr, then check with the platform for 134 // the GDB server binary if it can't be located 135 ProcessLaunchInfo &launch_info, uint16_t *port, const Args *inferior_args, 136 int pass_comm_fd); // Communication file descriptor to pass during 137 // fork/exec to avoid having to connect/accept 138 139 void DumpHistory(Stream &strm); 140 void SetHistoryStream(llvm::raw_ostream *strm); 141 142 static llvm::Error ConnectLocally(GDBRemoteCommunication &client, 143 GDBRemoteCommunication &server); 144 145 protected: 146 std::chrono::seconds m_packet_timeout; 147 uint32_t m_echo_number; 148 LazyBool m_supports_qEcho; 149 GDBRemoteCommunicationHistory m_history; 150 bool m_send_acks; 151 bool m_is_platform; // Set to true if this class represents a platform, 152 // false if this class represents a debug session for 153 // a single process 154 155 CompressionType m_compression_type; 156 157 PacketResult SendPacketNoLock(llvm::StringRef payload); 158 PacketResult SendRawPacketNoLock(llvm::StringRef payload, 159 bool skip_ack = false); 160 161 PacketResult ReadPacket(StringExtractorGDBRemote &response, 162 Timeout<std::micro> timeout, bool sync_on_timeout); 163 164 PacketResult ReadPacketWithOutputSupport( 165 StringExtractorGDBRemote &response, Timeout<std::micro> timeout, 166 bool sync_on_timeout, 167 llvm::function_ref<void(llvm::StringRef)> output_callback); 168 169 // Pop a packet from the queue in a thread safe manner 170 PacketResult PopPacketFromQueue(StringExtractorGDBRemote &response, 171 Timeout<std::micro> timeout); 172 173 PacketResult WaitForPacketNoLock(StringExtractorGDBRemote &response, 174 Timeout<std::micro> timeout, 175 bool sync_on_timeout); 176 CompressionIsEnabled()177 bool CompressionIsEnabled() { 178 return m_compression_type != CompressionType::None; 179 } 180 181 // If compression is enabled, decompress the packet in m_bytes and update 182 // m_bytes with the uncompressed version. 183 // Returns 'true' packet was decompressed and m_bytes is the now-decompressed 184 // text. 185 // Returns 'false' if unable to decompress or if the checksum was invalid. 186 // 187 // NB: Once the packet has been decompressed, checksum cannot be computed 188 // based 189 // on m_bytes. The checksum was for the compressed packet. 190 bool DecompressPacket(); 191 192 Status StartListenThread(const char *hostname = "127.0.0.1", 193 uint16_t port = 0); 194 195 bool JoinListenThread(); 196 197 static lldb::thread_result_t ListenThread(lldb::thread_arg_t arg); 198 199 // GDB-Remote read thread 200 // . this thread constantly tries to read from the communication 201 // class and stores all packets received in a queue. The usual 202 // threads read requests simply pop packets off the queue in the 203 // usual order. 204 // This setup allows us to intercept and handle async packets, such 205 // as the notify packet. 206 207 // This method is defined as part of communication.h 208 // when the read thread gets any bytes it will pass them on to this function 209 void AppendBytesToCache(const uint8_t *bytes, size_t len, bool broadcast, 210 lldb::ConnectionStatus status) override; 211 212 private: 213 std::queue<StringExtractorGDBRemote> m_packet_queue; // The packet queue 214 std::mutex m_packet_queue_mutex; // Mutex for accessing queue 215 std::condition_variable 216 m_condition_queue_not_empty; // Condition variable to wait for packets 217 218 HostThread m_listen_thread; 219 std::string m_listen_url; 220 221 CompressionType m_decompression_scratch_type; 222 void *m_decompression_scratch; 223 224 DISALLOW_COPY_AND_ASSIGN(GDBRemoteCommunication); 225 }; 226 227 } // namespace process_gdb_remote 228 } // namespace lldb_private 229 230 namespace llvm { 231 template <> 232 struct format_provider< 233 lldb_private::process_gdb_remote::GDBRemoteCommunication::PacketResult> { 234 static void format(const lldb_private::process_gdb_remote:: 235 GDBRemoteCommunication::PacketResult &state, 236 raw_ostream &Stream, StringRef Style); 237 }; 238 } // namespace llvm 239 240 #endif // liblldb_GDBRemoteCommunication_h_ 241