blob: 13325410e4884c73c7ac4fef8f33a503b9043e0a [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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 Abdulrasool2d6a9ec2016-07-28 17:32:20 +000015#include <condition_variable>
16#include <mutex>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include <string>
Ewan Crawfordfab40d32015-06-16 15:50:18 +000018#include <queue>
Eugene Zelenkoedb35d92015-10-24 01:08:35 +000019#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
21// Other libraries and framework includes
22// Project includes
Greg Claytonc1422c12012-04-09 22:46:21 +000023#include "lldb/lldb-public.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Core/Communication.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Listener.h"
Zachary Turner39de3112014-09-09 20:54:56 +000026#include "lldb/Host/HostThread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Host/Predicate.h"
Peter Collingbourneba23ca02011-06-18 23:52:14 +000028#include "lldb/Host/TimeValue.h"
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000029#include "lldb/Interpreter/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
Greg Claytonc982c762010-07-09 20:39:50 +000031#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
Tamas Berghammerdb264a62015-03-31 09:52:22 +000033namespace lldb_private {
34namespace process_gdb_remote {
35
Chaoren Lin18fe6402015-02-03 01:51:47 +000036typedef enum
37{
38 eStoppointInvalid = -1,
39 eBreakpointSoftware = 0,
40 eBreakpointHardware,
41 eWatchpointWrite,
42 eWatchpointRead,
43 eWatchpointReadWrite
44} GDBStoppointType;
45
Jason Molenda91ffe0a2015-06-18 21:46:06 +000046enum class CompressionType
47{
48 None = 0, // no compression
49 ZlibDeflate, // zlib's deflate compression scheme, requires zlib or Apple's libcompression
50 LZFSE, // an Apple compression scheme, requires Apple's libcompression
51 LZ4, // lz compression - called "lz4 raw" in libcompression terms, compat with https://code.google.com/p/lz4/
52 LZMA, // Lempel–Ziv–Markov chain algorithm
53};
54
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055class ProcessGDBRemote;
56
Tamas Berghammerdb264a62015-03-31 09:52:22 +000057class GDBRemoteCommunication : public Communication
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058{
59public:
Greg Claytone5219662010-12-03 06:02:24 +000060 enum
61 {
Ewan Crawfordfab40d32015-06-16 15:50:18 +000062 eBroadcastBitRunPacketSent = kLoUserBroadcastBit,
63 eBroadcastBitGdbReadThreadGotNotify = kLoUserBroadcastBit << 1 // Sent when we received a notify packet.
Greg Claytone5219662010-12-03 06:02:24 +000064 };
Ewan Crawford9aa2da002015-05-27 14:12:34 +000065
66 enum class PacketType
67 {
68 Invalid = 0,
69 Standard,
70 Notify
71 };
72
Greg Clayton3dedae12013-12-06 21:45:27 +000073 enum class PacketResult
74 {
75 Success = 0, // Success
76 ErrorSendFailed, // Error sending the packet
77 ErrorSendAck, // Didn't get an ack back after sending a packet
78 ErrorReplyFailed, // Error getting the reply
79 ErrorReplyTimeout, // Timed out waiting for reply
80 ErrorReplyInvalid, // Got a reply but it wasn't valid for the packet that was sent
81 ErrorReplyAck, // Sending reply ack failed
Steve Pucci5ae54ae2014-01-25 05:46:51 +000082 ErrorDisconnected, // We were disconnected
83 ErrorNoSequenceLock // We couldn't get the sequence lock for a multi-packet request
Greg Clayton3dedae12013-12-06 21:45:27 +000084 };
Tamas Berghammer912800c2015-02-24 10:23:39 +000085
86 // Class to change the timeout for a given scope and restore it to the original value when the
87 // created ScopedTimeout object got out of scope
88 class ScopedTimeout
89 {
90 public:
91 ScopedTimeout (GDBRemoteCommunication& gdb_comm, uint32_t timeout);
92 ~ScopedTimeout ();
93
94 private:
95 GDBRemoteCommunication& m_gdb_comm;
96 uint32_t m_saved_timeout;
97 };
98
Greg Clayton8b82f082011-04-12 05:54:46 +000099 GDBRemoteCommunication(const char *comm_name,
Tamas Berghammere13c2732015-02-11 10:29:30 +0000100 const char *listener_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000102 ~GDBRemoteCommunication() override;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103
Greg Clayton3dedae12013-12-06 21:45:27 +0000104 PacketResult
Greg Claytonc574ede2011-03-10 02:26:48 +0000105 GetAck ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106
107 size_t
Greg Clayton6ed95942011-01-22 07:12:45 +0000108 SendAck ();
109
110 size_t
111 SendNack ();
112
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113 char
114 CalculcateChecksum (const char *payload,
115 size_t payload_length);
116
Ewan Crawford9aa2da002015-05-27 14:12:34 +0000117 PacketType
Greg Clayton73bf5db2011-06-17 01:22:15 +0000118 CheckForPacket (const uint8_t *src,
119 size_t src_len,
120 StringExtractorGDBRemote &packet);
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000121
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122 bool
Greg Clayton1cb64962011-03-24 04:28:38 +0000123 GetSendAcks ()
124 {
125 return m_send_acks;
126 }
127
Greg Clayton576d8832011-03-22 04:00:09 +0000128 //------------------------------------------------------------------
129 // Client and server must implement these pure virtual functions
130 //------------------------------------------------------------------
131 virtual bool
132 GetThreadSuffixSupported () = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133
Greg Clayton576d8832011-03-22 04:00:09 +0000134 //------------------------------------------------------------------
135 // Set the global packet timeout.
136 //
137 // For clients, this is the timeout that gets used when sending
138 // packets and waiting for responses. For servers, this might not
139 // get used, and if it doesn't this should be moved to the
140 // GDBRemoteCommunicationClient.
141 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000142 uint32_t
143 SetPacketTimeout (uint32_t packet_timeout)
144 {
145 const uint32_t old_packet_timeout = m_packet_timeout;
146 m_packet_timeout = packet_timeout;
147 return old_packet_timeout;
148 }
Greg Clayton71fc2a32011-02-12 06:28:37 +0000149
Greg Clayton73bf5db2011-06-17 01:22:15 +0000150 uint32_t
151 GetPacketTimeoutInMicroSeconds () const
152 {
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000153 return m_packet_timeout * TimeValue::MicroSecPerSec;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000154 }
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000155
Greg Clayton8b82f082011-04-12 05:54:46 +0000156 //------------------------------------------------------------------
157 // Start a debugserver instance on the current host using the
158 // supplied connection URL.
159 //------------------------------------------------------------------
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000160 Error
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000161 StartDebugserverProcess(const char *url,
162 Platform *platform, // If non nullptr, then check with the platform for the GDB server binary if it can't be located
163 ProcessLaunchInfo &launch_info,
Tamas Berghammerccd6cff2015-12-08 14:08:19 +0000164 uint16_t *port,
165 const Args& inferior_args = Args());
Greg Clayton8b82f082011-04-12 05:54:46 +0000166
Greg Claytonc1422c12012-04-09 22:46:21 +0000167 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000168 DumpHistory(Stream &strm);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000169
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170protected:
Greg Claytonc1422c12012-04-09 22:46:21 +0000171 class History
172 {
173 public:
174 enum PacketType
175 {
176 ePacketTypeInvalid = 0,
177 ePacketTypeSend,
178 ePacketTypeRecv
179 };
180
181 struct Entry
182 {
183 Entry() :
184 packet(),
185 type (ePacketTypeInvalid),
186 bytes_transmitted (0),
Greg Claytond451c1a2012-04-13 21:24:18 +0000187 packet_idx (0),
188 tid (LLDB_INVALID_THREAD_ID)
Greg Claytonc1422c12012-04-09 22:46:21 +0000189 {
190 }
191
192 void
193 Clear ()
194 {
195 packet.clear();
196 type = ePacketTypeInvalid;
197 bytes_transmitted = 0;
198 packet_idx = 0;
Greg Claytond451c1a2012-04-13 21:24:18 +0000199 tid = LLDB_INVALID_THREAD_ID;
Greg Claytonc1422c12012-04-09 22:46:21 +0000200 }
201 std::string packet;
202 PacketType type;
203 uint32_t bytes_transmitted;
204 uint32_t packet_idx;
Greg Claytond451c1a2012-04-13 21:24:18 +0000205 lldb::tid_t tid;
Greg Claytonc1422c12012-04-09 22:46:21 +0000206 };
207
208 History (uint32_t size);
209
210 ~History ();
211
212 // For single char packets for ack, nack and /x03
213 void
214 AddPacket (char packet_char,
215 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000216 uint32_t bytes_transmitted);
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000217
Greg Claytonc1422c12012-04-09 22:46:21 +0000218 void
219 AddPacket (const std::string &src,
220 uint32_t src_len,
221 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000222 uint32_t bytes_transmitted);
Greg Claytonc1422c12012-04-09 22:46:21 +0000223
224 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000225 Dump (Stream &strm) const;
Greg Claytonc1422c12012-04-09 22:46:21 +0000226
227 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000228 Dump (Log *log) const;
Greg Claytonc1422c12012-04-09 22:46:21 +0000229
230 bool
231 DidDumpToLog () const
232 {
233 return m_dumped_to_log;
234 }
235
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000236 protected:
Greg Claytonc1422c12012-04-09 22:46:21 +0000237 uint32_t
238 GetFirstSavedPacketIndex () const
239 {
240 if (m_total_packet_count < m_packets.size())
241 return 0;
242 else
243 return m_curr_idx + 1;
244 }
245
246 uint32_t
247 GetNumPacketsInHistory () const
248 {
249 if (m_total_packet_count < m_packets.size())
250 return m_total_packet_count;
251 else
252 return (uint32_t)m_packets.size();
253 }
254
255 uint32_t
256 GetNextIndex()
257 {
258 ++m_total_packet_count;
259 const uint32_t idx = m_curr_idx;
260 m_curr_idx = NormalizeIndex(idx + 1);
261 return idx;
262 }
263
264 uint32_t
265 NormalizeIndex (uint32_t i) const
266 {
267 return i % m_packets.size();
268 }
269
Greg Claytonc1422c12012-04-09 22:46:21 +0000270 std::vector<Entry> m_packets;
271 uint32_t m_curr_idx;
272 uint32_t m_total_packet_count;
273 mutable bool m_dumped_to_log;
274 };
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000276 uint32_t m_packet_timeout;
277 uint32_t m_echo_number;
278 LazyBool m_supports_qEcho;
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000279 History m_history;
280 bool m_send_acks;
281 bool m_is_platform; // Set to true if this class represents a platform,
282 // false if this class represents a debug session for
283 // a single process
284
285 CompressionType m_compression_type;
286
Greg Clayton3dedae12013-12-06 21:45:27 +0000287 PacketResult
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288 SendPacketNoLock (const char *payload,
289 size_t payload_length);
290
Greg Clayton3dedae12013-12-06 21:45:27 +0000291 PacketResult
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000292 ReadPacket (StringExtractorGDBRemote &response, uint32_t timeout_usec, bool sync_on_timeout);
293
294 // Pop a packet from the queue in a thread safe manner
295 PacketResult
296 PopPacketFromQueue (StringExtractorGDBRemote &response, uint32_t timeout_usec);
297
298 PacketResult
Greg Clayton73bf5db2011-06-17 01:22:15 +0000299 WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &response,
Greg Claytonb30c50c2015-05-29 00:01:55 +0000300 uint32_t timeout_usec,
301 bool sync_on_timeout);
Greg Clayton6779606a2011-01-22 23:43:18 +0000302
303 bool
Jason Molenda91ffe0a2015-06-18 21:46:06 +0000304 CompressionIsEnabled ()
305 {
306 return m_compression_type != CompressionType::None;
307 }
308
309 // If compression is enabled, decompress the packet in m_bytes and update
310 // m_bytes with the uncompressed version.
311 // Returns 'true' packet was decompressed and m_bytes is the now-decompressed text.
312 // Returns 'false' if unable to decompress or if the checksum was invalid.
313 //
314 // NB: Once the packet has been decompressed, checksum cannot be computed based
315 // on m_bytes. The checksum was for the compressed packet.
316 bool
317 DecompressPacket ();
318
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000319 Error
320 StartListenThread (const char *hostname = "127.0.0.1", uint16_t port = 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321
Greg Clayton00fe87b2013-12-05 22:58:22 +0000322 bool
323 JoinListenThread ();
324
325 static lldb::thread_result_t
326 ListenThread (lldb::thread_arg_t arg);
Greg Clayton8b82f082011-04-12 05:54:46 +0000327
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000328 // GDB-Remote read thread
329 // . this thread constantly tries to read from the communication
330 // class and stores all packets received in a queue. The usual
331 // threads read requests simply pop packets off the queue in the
332 // usual order.
333 // This setup allows us to intercept and handle async packets, such
334 // as the notify packet.
335
336 // This method is defined as part of communication.h
337 // when the read thread gets any bytes it will pass them on to this function
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000338 void AppendBytesToCache(const uint8_t * bytes,
339 size_t len,
340 bool broadcast,
341 lldb::ConnectionStatus status) override;
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000342
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343private:
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000344 std::queue<StringExtractorGDBRemote> m_packet_queue; // The packet queue
Saleem Abdulrasool2d6a9ec2016-07-28 17:32:20 +0000345 std::mutex m_packet_queue_mutex; // Mutex for accessing queue
346 std::condition_variable m_condition_queue_not_empty; // Condition variable to wait for packets
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000347
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000348 HostThread m_listen_thread;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000349 std::string m_listen_url;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000350
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
352};
353
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000354} // namespace process_gdb_remote
355} // namespace lldb_private
356
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000357#endif // liblldb_GDBRemoteCommunication_h_