blob: 094cc94b0c1dd64bf761b0e4b5e7807efbdf55b7 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- GDBRemoteCommunication.h --------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef liblldb_GDBRemoteCommunication_h_
10#define liblldb_GDBRemoteCommunication_h_
11
Jonas Devlieghere9e046f02018-11-13 19:18:16 +000012#include "GDBRemoteCommunicationHistory.h"
13
Saleem Abdulrasool2d6a9ec2016-07-28 17:32:20 +000014#include <condition_variable>
15#include <mutex>
Ewan Crawfordfab40d32015-06-16 15:50:18 +000016#include <queue>
Kate Stoneb9c1b512016-09-06 20:57:50 +000017#include <string>
Eugene Zelenkoedb35d92015-10-24 01:08:35 +000018#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Core/Communication.h"
Zachary Turner39de3112014-09-09 20:54:56 +000021#include "lldb/Host/HostThread.h"
Pavel Labath145d95c2018-04-17 18:53:35 +000022#include "lldb/Utility/Args.h"
Pavel Labath181b8232018-12-14 15:59:49 +000023#include "lldb/Utility/Listener.h"
Raphael Isemann7fae4932018-08-30 17:51:10 +000024#include "lldb/Utility/Predicate.h"
Pavel Labath9af71b32018-03-20 16:14:00 +000025#include "lldb/Utility/StringExtractorGDBRemote.h"
Pavel Labath181b8232018-12-14 15:59:49 +000026#include "lldb/lldb-public.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Tamas Berghammerdb264a62015-03-31 09:52:22 +000028namespace lldb_private {
29namespace process_gdb_remote {
30
Kate Stoneb9c1b512016-09-06 20:57:50 +000031typedef enum {
32 eStoppointInvalid = -1,
33 eBreakpointSoftware = 0,
34 eBreakpointHardware,
35 eWatchpointWrite,
36 eWatchpointRead,
37 eWatchpointReadWrite
Chaoren Lin18fe6402015-02-03 01:51:47 +000038} GDBStoppointType;
39
Kate Stoneb9c1b512016-09-06 20:57:50 +000040enum class CompressionType {
41 None = 0, // no compression
42 ZlibDeflate, // zlib's deflate compression scheme, requires zlib or Apple's
43 // libcompression
44 LZFSE, // an Apple compression scheme, requires Apple's libcompression
45 LZ4, // lz compression - called "lz4 raw" in libcompression terms, compat with
46 // https://code.google.com/p/lz4/
47 LZMA, // Lempel–Ziv–Markov chain algorithm
Jason Molenda91ffe0a2015-06-18 21:46:06 +000048};
49
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050class ProcessGDBRemote;
51
Kate Stoneb9c1b512016-09-06 20:57:50 +000052class GDBRemoteCommunication : public Communication {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 enum {
55 eBroadcastBitRunPacketSent = kLoUserBroadcastBit,
56 eBroadcastBitGdbReadThreadGotNotify =
57 kLoUserBroadcastBit << 1 // Sent when we received a notify packet.
58 };
Ewan Crawford9aa2da002015-05-27 14:12:34 +000059
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 enum class PacketType { Invalid = 0, Standard, Notify };
Ewan Crawford9aa2da002015-05-27 14:12:34 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 enum class PacketResult {
63 Success = 0, // Success
Zachary Turner97206d52017-05-12 04:51:55 +000064 ErrorSendFailed, // Status sending the packet
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 ErrorSendAck, // Didn't get an ack back after sending a packet
Zachary Turner97206d52017-05-12 04:51:55 +000066 ErrorReplyFailed, // Status getting the reply
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 ErrorReplyTimeout, // Timed out waiting for reply
68 ErrorReplyInvalid, // Got a reply but it wasn't valid for the packet that
69 // was sent
70 ErrorReplyAck, // Sending reply ack failed
71 ErrorDisconnected, // We were disconnected
72 ErrorNoSequenceLock // We couldn't get the sequence lock for a multi-packet
73 // request
74 };
Tamas Berghammer912800c2015-02-24 10:23:39 +000075
Kate Stoneb9c1b512016-09-06 20:57:50 +000076 // Class to change the timeout for a given scope and restore it to the
77 // original value when the
78 // created ScopedTimeout object got out of scope
79 class ScopedTimeout {
80 public:
Pavel Labath3aa04912016-10-31 17:19:42 +000081 ScopedTimeout(GDBRemoteCommunication &gdb_comm,
82 std::chrono::seconds timeout);
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 ~ScopedTimeout();
Tamas Berghammer912800c2015-02-24 10:23:39 +000084
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 private:
86 GDBRemoteCommunication &m_gdb_comm;
Pavel Labath3aa04912016-10-31 17:19:42 +000087 std::chrono::seconds m_saved_timeout;
Greg Clayton84577092017-04-17 16:20:22 +000088 // Don't ever reduce the timeout for a packet, only increase it. If the
89 // requested timeout if less than the current timeout, we don't set it
90 // and won't need to restore it.
91 bool m_timeout_modified;
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 };
Tamas Berghammer912800c2015-02-24 10:23:39 +000093
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 GDBRemoteCommunication(const char *comm_name, const char *listener_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095
Kate Stoneb9c1b512016-09-06 20:57:50 +000096 ~GDBRemoteCommunication() override;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 PacketResult GetAck();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 size_t SendAck();
Greg Clayton6ed95942011-01-22 07:12:45 +0000101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 size_t SendNack();
Greg Clayton6ed95942011-01-22 07:12:45 +0000103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 char CalculcateChecksum(llvm::StringRef payload);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 PacketType CheckForPacket(const uint8_t *src, size_t src_len,
107 StringExtractorGDBRemote &packet);
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 bool GetSendAcks() { return m_send_acks; }
Greg Clayton1cb64962011-03-24 04:28:38 +0000110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 //------------------------------------------------------------------
112 // Set the global packet timeout.
113 //
114 // For clients, this is the timeout that gets used when sending
Pavel Labath3aa04912016-10-31 17:19:42 +0000115 // packets and waiting for responses. For servers, this is used when waiting
116 // for ACKs.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 //------------------------------------------------------------------
Pavel Labath3aa04912016-10-31 17:19:42 +0000118 std::chrono::seconds SetPacketTimeout(std::chrono::seconds packet_timeout) {
119 const auto old_packet_timeout = m_packet_timeout;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 m_packet_timeout = packet_timeout;
121 return old_packet_timeout;
122 }
Greg Clayton71fc2a32011-02-12 06:28:37 +0000123
Pavel Labath3aa04912016-10-31 17:19:42 +0000124 std::chrono::seconds GetPacketTimeout() const { return m_packet_timeout; }
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000125
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 //------------------------------------------------------------------
127 // Start a debugserver instance on the current host using the
128 // supplied connection URL.
129 //------------------------------------------------------------------
Zachary Turner97206d52017-05-12 04:51:55 +0000130 Status StartDebugserverProcess(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000131 const char *url,
132 Platform *platform, // If non nullptr, then check with the platform for
133 // the GDB server binary if it can't be located
134 ProcessLaunchInfo &launch_info, uint16_t *port, const Args *inferior_args,
135 int pass_comm_fd); // Communication file descriptor to pass during
136 // fork/exec to avoid having to connect/accept
Greg Clayton8b82f082011-04-12 05:54:46 +0000137
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 void DumpHistory(Stream &strm);
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000139 void SetHistoryStream(llvm::raw_ostream *strm);
140
141 static llvm::Error ConnectLocally(GDBRemoteCommunication &client,
142 GDBRemoteCommunication &server);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144protected:
Pavel Labath3aa04912016-10-31 17:19:42 +0000145 std::chrono::seconds m_packet_timeout;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 uint32_t m_echo_number;
147 LazyBool m_supports_qEcho;
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000148 GDBRemoteCommunicationHistory m_history;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 bool m_send_acks;
150 bool m_is_platform; // Set to true if this class represents a platform,
151 // false if this class represents a debug session for
152 // a single process
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 CompressionType m_compression_type;
155
156 PacketResult SendPacketNoLock(llvm::StringRef payload);
Jonas Devlieghere9e046f02018-11-13 19:18:16 +0000157 PacketResult SendRawPacketNoLock(llvm::StringRef payload,
158 bool skip_ack = false);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159
160 PacketResult ReadPacket(StringExtractorGDBRemote &response,
Pavel Labath1eff73c2016-11-24 10:54:49 +0000161 Timeout<std::micro> timeout, bool sync_on_timeout);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000162
Pavel Labath7da84752018-01-10 14:39:08 +0000163 PacketResult ReadPacketWithOutputSupport(
164 StringExtractorGDBRemote &response, Timeout<std::micro> timeout,
165 bool sync_on_timeout,
166 llvm::function_ref<void(llvm::StringRef)> output_callback);
167
Kate Stoneb9c1b512016-09-06 20:57:50 +0000168 // Pop a packet from the queue in a thread safe manner
169 PacketResult PopPacketFromQueue(StringExtractorGDBRemote &response,
Pavel Labath1eff73c2016-11-24 10:54:49 +0000170 Timeout<std::micro> timeout);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171
Pavel Labath1eff73c2016-11-24 10:54:49 +0000172 PacketResult WaitForPacketNoLock(StringExtractorGDBRemote &response,
173 Timeout<std::micro> timeout,
174 bool sync_on_timeout);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000175
176 bool CompressionIsEnabled() {
177 return m_compression_type != CompressionType::None;
178 }
179
180 // If compression is enabled, decompress the packet in m_bytes and update
181 // m_bytes with the uncompressed version.
182 // Returns 'true' packet was decompressed and m_bytes is the now-decompressed
183 // text.
184 // Returns 'false' if unable to decompress or if the checksum was invalid.
185 //
186 // NB: Once the packet has been decompressed, checksum cannot be computed
187 // based
188 // on m_bytes. The checksum was for the compressed packet.
189 bool DecompressPacket();
190
Zachary Turner97206d52017-05-12 04:51:55 +0000191 Status StartListenThread(const char *hostname = "127.0.0.1",
192 uint16_t port = 0);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193
194 bool JoinListenThread();
195
196 static lldb::thread_result_t ListenThread(lldb::thread_arg_t arg);
197
198 // GDB-Remote read thread
199 // . this thread constantly tries to read from the communication
200 // class and stores all packets received in a queue. The usual
201 // threads read requests simply pop packets off the queue in the
202 // usual order.
203 // This setup allows us to intercept and handle async packets, such
204 // as the notify packet.
205
206 // This method is defined as part of communication.h
207 // when the read thread gets any bytes it will pass them on to this function
208 void AppendBytesToCache(const uint8_t *bytes, size_t len, bool broadcast,
209 lldb::ConnectionStatus status) override;
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000210
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000212 std::queue<StringExtractorGDBRemote> m_packet_queue; // The packet queue
213 std::mutex m_packet_queue_mutex; // Mutex for accessing queue
214 std::condition_variable
215 m_condition_queue_not_empty; // Condition variable to wait for packets
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 HostThread m_listen_thread;
218 std::string m_listen_url;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000219
Jason Molenda4a793c82018-12-18 23:02:50 +0000220 CompressionType m_decompression_scratch_type;
221 void *m_decompression_scratch;
222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223 DISALLOW_COPY_AND_ASSIGN(GDBRemoteCommunication);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224};
225
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000226} // namespace process_gdb_remote
227} // namespace lldb_private
228
Pavel Labath1ebc85f2017-11-09 15:45:09 +0000229namespace llvm {
230template <>
231struct format_provider<
232 lldb_private::process_gdb_remote::GDBRemoteCommunication::PacketResult> {
233 static void format(const lldb_private::process_gdb_remote::
234 GDBRemoteCommunication::PacketResult &state,
235 raw_ostream &Stream, StringRef Style);
236};
237} // namespace llvm
238
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000239#endif // liblldb_GDBRemoteCommunication_h_