blob: 23568cd4b368a0b1bb350506c413c2aa3d61a5c3 [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
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117 bool
Saleem Abdulrasool2d6a9ec2016-07-28 17:32:20 +0000118 GetSequenceMutex(std::unique_lock<std::recursive_mutex> &lock, const char *failure_message = nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119
Ewan Crawford9aa2da002015-05-27 14:12:34 +0000120 PacketType
Greg Clayton73bf5db2011-06-17 01:22:15 +0000121 CheckForPacket (const uint8_t *src,
122 size_t src_len,
123 StringExtractorGDBRemote &packet);
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000124
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125 bool
126 IsRunning() const
127 {
Greg Clayton4dc72282011-01-20 07:53:45 +0000128 return m_public_is_running.GetValue();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 }
Greg Clayton6779606a2011-01-22 23:43:18 +0000130
Greg Clayton1cb64962011-03-24 04:28:38 +0000131 bool
132 GetSendAcks ()
133 {
134 return m_send_acks;
135 }
136
Greg Clayton576d8832011-03-22 04:00:09 +0000137 //------------------------------------------------------------------
138 // Client and server must implement these pure virtual functions
139 //------------------------------------------------------------------
140 virtual bool
141 GetThreadSuffixSupported () = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142
Greg Clayton576d8832011-03-22 04:00:09 +0000143 //------------------------------------------------------------------
144 // Set the global packet timeout.
145 //
146 // For clients, this is the timeout that gets used when sending
147 // packets and waiting for responses. For servers, this might not
148 // get used, and if it doesn't this should be moved to the
149 // GDBRemoteCommunicationClient.
150 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000151 uint32_t
152 SetPacketTimeout (uint32_t packet_timeout)
153 {
154 const uint32_t old_packet_timeout = m_packet_timeout;
155 m_packet_timeout = packet_timeout;
156 return old_packet_timeout;
157 }
Greg Clayton71fc2a32011-02-12 06:28:37 +0000158
Greg Clayton73bf5db2011-06-17 01:22:15 +0000159 uint32_t
160 GetPacketTimeoutInMicroSeconds () const
161 {
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000162 return m_packet_timeout * TimeValue::MicroSecPerSec;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000163 }
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000164
Greg Clayton8b82f082011-04-12 05:54:46 +0000165 //------------------------------------------------------------------
166 // Start a debugserver instance on the current host using the
167 // supplied connection URL.
168 //------------------------------------------------------------------
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000169 Error
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000170 StartDebugserverProcess(const char *url,
171 Platform *platform, // If non nullptr, then check with the platform for the GDB server binary if it can't be located
172 ProcessLaunchInfo &launch_info,
Tamas Berghammerccd6cff2015-12-08 14:08:19 +0000173 uint16_t *port,
174 const Args& inferior_args = Args());
Greg Clayton8b82f082011-04-12 05:54:46 +0000175
Greg Claytonc1422c12012-04-09 22:46:21 +0000176 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000177 DumpHistory(Stream &strm);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000178
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179protected:
Greg Claytonc1422c12012-04-09 22:46:21 +0000180 class History
181 {
182 public:
183 enum PacketType
184 {
185 ePacketTypeInvalid = 0,
186 ePacketTypeSend,
187 ePacketTypeRecv
188 };
189
190 struct Entry
191 {
192 Entry() :
193 packet(),
194 type (ePacketTypeInvalid),
195 bytes_transmitted (0),
Greg Claytond451c1a2012-04-13 21:24:18 +0000196 packet_idx (0),
197 tid (LLDB_INVALID_THREAD_ID)
Greg Claytonc1422c12012-04-09 22:46:21 +0000198 {
199 }
200
201 void
202 Clear ()
203 {
204 packet.clear();
205 type = ePacketTypeInvalid;
206 bytes_transmitted = 0;
207 packet_idx = 0;
Greg Claytond451c1a2012-04-13 21:24:18 +0000208 tid = LLDB_INVALID_THREAD_ID;
Greg Claytonc1422c12012-04-09 22:46:21 +0000209 }
210 std::string packet;
211 PacketType type;
212 uint32_t bytes_transmitted;
213 uint32_t packet_idx;
Greg Claytond451c1a2012-04-13 21:24:18 +0000214 lldb::tid_t tid;
Greg Claytonc1422c12012-04-09 22:46:21 +0000215 };
216
217 History (uint32_t size);
218
219 ~History ();
220
221 // For single char packets for ack, nack and /x03
222 void
223 AddPacket (char packet_char,
224 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000225 uint32_t bytes_transmitted);
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000226
Greg Claytonc1422c12012-04-09 22:46:21 +0000227 void
228 AddPacket (const std::string &src,
229 uint32_t src_len,
230 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000231 uint32_t bytes_transmitted);
Greg Claytonc1422c12012-04-09 22:46:21 +0000232
233 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000234 Dump (Stream &strm) const;
Greg Claytonc1422c12012-04-09 22:46:21 +0000235
236 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000237 Dump (Log *log) const;
Greg Claytonc1422c12012-04-09 22:46:21 +0000238
239 bool
240 DidDumpToLog () const
241 {
242 return m_dumped_to_log;
243 }
244
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000245 protected:
Greg Claytonc1422c12012-04-09 22:46:21 +0000246 uint32_t
247 GetFirstSavedPacketIndex () const
248 {
249 if (m_total_packet_count < m_packets.size())
250 return 0;
251 else
252 return m_curr_idx + 1;
253 }
254
255 uint32_t
256 GetNumPacketsInHistory () const
257 {
258 if (m_total_packet_count < m_packets.size())
259 return m_total_packet_count;
260 else
261 return (uint32_t)m_packets.size();
262 }
263
264 uint32_t
265 GetNextIndex()
266 {
267 ++m_total_packet_count;
268 const uint32_t idx = m_curr_idx;
269 m_curr_idx = NormalizeIndex(idx + 1);
270 return idx;
271 }
272
273 uint32_t
274 NormalizeIndex (uint32_t i) const
275 {
276 return i % m_packets.size();
277 }
278
Greg Claytonc1422c12012-04-09 22:46:21 +0000279 std::vector<Entry> m_packets;
280 uint32_t m_curr_idx;
281 uint32_t m_total_packet_count;
282 mutable bool m_dumped_to_log;
283 };
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000285 uint32_t m_packet_timeout;
286 uint32_t m_echo_number;
287 LazyBool m_supports_qEcho;
288#ifdef ENABLE_MUTEX_ERROR_CHECKING
Saleem Abdulrasool2d6a9ec2016-07-28 17:32:20 +0000289#error TrackingMutex is no longer supported
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000290#else
Saleem Abdulrasool2d6a9ec2016-07-28 17:32:20 +0000291 std::recursive_mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000292#endif
293 Predicate<bool> m_public_is_running;
294 Predicate<bool> m_private_is_running;
295 History m_history;
296 bool m_send_acks;
297 bool m_is_platform; // Set to true if this class represents a platform,
298 // false if this class represents a debug session for
299 // a single process
300
301 CompressionType m_compression_type;
302
Greg Clayton3dedae12013-12-06 21:45:27 +0000303 PacketResult
Greg Clayton37a0a242012-04-11 00:24:49 +0000304 SendPacket (const char *payload,
305 size_t payload_length);
306
Greg Clayton3dedae12013-12-06 21:45:27 +0000307 PacketResult
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000308 SendPacketNoLock (const char *payload,
309 size_t payload_length);
310
Greg Clayton3dedae12013-12-06 21:45:27 +0000311 PacketResult
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000312 ReadPacket (StringExtractorGDBRemote &response, uint32_t timeout_usec, bool sync_on_timeout);
313
314 // Pop a packet from the queue in a thread safe manner
315 PacketResult
316 PopPacketFromQueue (StringExtractorGDBRemote &response, uint32_t timeout_usec);
317
318 PacketResult
Greg Clayton73bf5db2011-06-17 01:22:15 +0000319 WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &response,
Greg Claytonb30c50c2015-05-29 00:01:55 +0000320 uint32_t timeout_usec,
321 bool sync_on_timeout);
Greg Clayton6779606a2011-01-22 23:43:18 +0000322
323 bool
Saleem Abdulrasool2d6a9ec2016-07-28 17:32:20 +0000324 WaitForNotRunningPrivate(const std::chrono::microseconds &timeout);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325
Jason Molenda91ffe0a2015-06-18 21:46:06 +0000326 bool
327 CompressionIsEnabled ()
328 {
329 return m_compression_type != CompressionType::None;
330 }
331
332 // If compression is enabled, decompress the packet in m_bytes and update
333 // m_bytes with the uncompressed version.
334 // Returns 'true' packet was decompressed and m_bytes is the now-decompressed text.
335 // Returns 'false' if unable to decompress or if the checksum was invalid.
336 //
337 // NB: Once the packet has been decompressed, checksum cannot be computed based
338 // on m_bytes. The checksum was for the compressed packet.
339 bool
340 DecompressPacket ();
341
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000342 Error
343 StartListenThread (const char *hostname = "127.0.0.1", uint16_t port = 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344
Greg Clayton00fe87b2013-12-05 22:58:22 +0000345 bool
346 JoinListenThread ();
347
348 static lldb::thread_result_t
349 ListenThread (lldb::thread_arg_t arg);
Greg Clayton8b82f082011-04-12 05:54:46 +0000350
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000351 // GDB-Remote read thread
352 // . this thread constantly tries to read from the communication
353 // class and stores all packets received in a queue. The usual
354 // threads read requests simply pop packets off the queue in the
355 // usual order.
356 // This setup allows us to intercept and handle async packets, such
357 // as the notify packet.
358
359 // This method is defined as part of communication.h
360 // when the read thread gets any bytes it will pass them on to this function
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000361 void AppendBytesToCache(const uint8_t * bytes,
362 size_t len,
363 bool broadcast,
364 lldb::ConnectionStatus status) override;
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000365
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366private:
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000367 std::queue<StringExtractorGDBRemote> m_packet_queue; // The packet queue
Saleem Abdulrasool2d6a9ec2016-07-28 17:32:20 +0000368 std::mutex m_packet_queue_mutex; // Mutex for accessing queue
369 std::condition_variable m_condition_queue_not_empty; // Condition variable to wait for packets
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000370
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000371 HostThread m_listen_thread;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000372 std::string m_listen_url;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000373
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
375};
376
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000377} // namespace process_gdb_remote
378} // namespace lldb_private
379
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000380#endif // liblldb_GDBRemoteCommunication_h_