blob: 2a01bcec260cae57c0152e9763b8a1a2504f316b [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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include <string>
Ewan Crawfordfab40d32015-06-16 15:50:18 +000016#include <queue>
Eugene Zelenkoedb35d92015-10-24 01:08:35 +000017#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018
19// Other libraries and framework includes
20// Project includes
Greg Claytonc1422c12012-04-09 22:46:21 +000021#include "lldb/lldb-public.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/Communication.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/Listener.h"
Zachary Turner39de3112014-09-09 20:54:56 +000024#include "lldb/Host/HostThread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Host/Mutex.h"
26#include "lldb/Host/Predicate.h"
Peter Collingbourneba23ca02011-06-18 23:52:14 +000027#include "lldb/Host/TimeValue.h"
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000028#include "lldb/Interpreter/Args.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
Greg Claytonc982c762010-07-09 20:39:50 +000030#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
Tamas Berghammerdb264a62015-03-31 09:52:22 +000032namespace lldb_private {
33namespace process_gdb_remote {
34
Chaoren Lin18fe6402015-02-03 01:51:47 +000035typedef enum
36{
37 eStoppointInvalid = -1,
38 eBreakpointSoftware = 0,
39 eBreakpointHardware,
40 eWatchpointWrite,
41 eWatchpointRead,
42 eWatchpointReadWrite
43} GDBStoppointType;
44
Jason Molenda91ffe0a2015-06-18 21:46:06 +000045enum class CompressionType
46{
47 None = 0, // no compression
48 ZlibDeflate, // zlib's deflate compression scheme, requires zlib or Apple's libcompression
49 LZFSE, // an Apple compression scheme, requires Apple's libcompression
50 LZ4, // lz compression - called "lz4 raw" in libcompression terms, compat with https://code.google.com/p/lz4/
51 LZMA, // Lempel–Ziv–Markov chain algorithm
52};
53
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054class ProcessGDBRemote;
55
Tamas Berghammerdb264a62015-03-31 09:52:22 +000056class GDBRemoteCommunication : public Communication
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057{
58public:
Greg Claytone5219662010-12-03 06:02:24 +000059 enum
60 {
Ewan Crawfordfab40d32015-06-16 15:50:18 +000061 eBroadcastBitRunPacketSent = kLoUserBroadcastBit,
62 eBroadcastBitGdbReadThreadGotNotify = kLoUserBroadcastBit << 1 // Sent when we received a notify packet.
Greg Claytone5219662010-12-03 06:02:24 +000063 };
Ewan Crawford9aa2da002015-05-27 14:12:34 +000064
65 enum class PacketType
66 {
67 Invalid = 0,
68 Standard,
69 Notify
70 };
71
Greg Clayton3dedae12013-12-06 21:45:27 +000072 enum class PacketResult
73 {
74 Success = 0, // Success
75 ErrorSendFailed, // Error sending the packet
76 ErrorSendAck, // Didn't get an ack back after sending a packet
77 ErrorReplyFailed, // Error getting the reply
78 ErrorReplyTimeout, // Timed out waiting for reply
79 ErrorReplyInvalid, // Got a reply but it wasn't valid for the packet that was sent
80 ErrorReplyAck, // Sending reply ack failed
Steve Pucci5ae54ae2014-01-25 05:46:51 +000081 ErrorDisconnected, // We were disconnected
82 ErrorNoSequenceLock // We couldn't get the sequence lock for a multi-packet request
Greg Clayton3dedae12013-12-06 21:45:27 +000083 };
Tamas Berghammer912800c2015-02-24 10:23:39 +000084
85 // Class to change the timeout for a given scope and restore it to the original value when the
86 // created ScopedTimeout object got out of scope
87 class ScopedTimeout
88 {
89 public:
90 ScopedTimeout (GDBRemoteCommunication& gdb_comm, uint32_t timeout);
91 ~ScopedTimeout ();
92
93 private:
94 GDBRemoteCommunication& m_gdb_comm;
95 uint32_t m_saved_timeout;
96 };
97
Greg Clayton8b82f082011-04-12 05:54:46 +000098 GDBRemoteCommunication(const char *comm_name,
Tamas Berghammere13c2732015-02-11 10:29:30 +000099 const char *listener_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000101 ~GDBRemoteCommunication() override;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102
Greg Clayton3dedae12013-12-06 21:45:27 +0000103 PacketResult
Greg Claytonc574ede2011-03-10 02:26:48 +0000104 GetAck ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105
106 size_t
Greg Clayton6ed95942011-01-22 07:12:45 +0000107 SendAck ();
108
109 size_t
110 SendNack ();
111
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112 char
113 CalculcateChecksum (const char *payload,
114 size_t payload_length);
115
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116 bool
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000117 GetSequenceMutex(Mutex::Locker& locker, const char *failure_message = nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118
Ewan Crawford9aa2da002015-05-27 14:12:34 +0000119 PacketType
Greg Clayton73bf5db2011-06-17 01:22:15 +0000120 CheckForPacket (const uint8_t *src,
121 size_t src_len,
122 StringExtractorGDBRemote &packet);
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000123
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124 bool
125 IsRunning() const
126 {
Greg Clayton4dc72282011-01-20 07:53:45 +0000127 return m_public_is_running.GetValue();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128 }
Greg Clayton6779606a2011-01-22 23:43:18 +0000129
Greg Clayton1cb64962011-03-24 04:28:38 +0000130 bool
131 GetSendAcks ()
132 {
133 return m_send_acks;
134 }
135
Greg Clayton576d8832011-03-22 04:00:09 +0000136 //------------------------------------------------------------------
137 // Client and server must implement these pure virtual functions
138 //------------------------------------------------------------------
139 virtual bool
140 GetThreadSuffixSupported () = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141
Greg Clayton576d8832011-03-22 04:00:09 +0000142 //------------------------------------------------------------------
143 // Set the global packet timeout.
144 //
145 // For clients, this is the timeout that gets used when sending
146 // packets and waiting for responses. For servers, this might not
147 // get used, and if it doesn't this should be moved to the
148 // GDBRemoteCommunicationClient.
149 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000150 uint32_t
151 SetPacketTimeout (uint32_t packet_timeout)
152 {
153 const uint32_t old_packet_timeout = m_packet_timeout;
154 m_packet_timeout = packet_timeout;
155 return old_packet_timeout;
156 }
Greg Clayton71fc2a32011-02-12 06:28:37 +0000157
Greg Clayton73bf5db2011-06-17 01:22:15 +0000158 uint32_t
159 GetPacketTimeoutInMicroSeconds () const
160 {
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000161 return m_packet_timeout * TimeValue::MicroSecPerSec;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000162 }
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000163
Greg Clayton8b82f082011-04-12 05:54:46 +0000164 //------------------------------------------------------------------
165 // Start a debugserver instance on the current host using the
166 // supplied connection URL.
167 //------------------------------------------------------------------
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000168 Error
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000169 StartDebugserverProcess(const char *url,
170 Platform *platform, // If non nullptr, then check with the platform for the GDB server binary if it can't be located
171 ProcessLaunchInfo &launch_info,
Tamas Berghammerccd6cff2015-12-08 14:08:19 +0000172 uint16_t *port,
173 const Args& inferior_args = Args());
Greg Clayton8b82f082011-04-12 05:54:46 +0000174
Greg Claytonc1422c12012-04-09 22:46:21 +0000175 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000176 DumpHistory(Stream &strm);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000177
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000178protected:
Greg Claytonc1422c12012-04-09 22:46:21 +0000179 class History
180 {
181 public:
182 enum PacketType
183 {
184 ePacketTypeInvalid = 0,
185 ePacketTypeSend,
186 ePacketTypeRecv
187 };
188
189 struct Entry
190 {
191 Entry() :
192 packet(),
193 type (ePacketTypeInvalid),
194 bytes_transmitted (0),
Greg Claytond451c1a2012-04-13 21:24:18 +0000195 packet_idx (0),
196 tid (LLDB_INVALID_THREAD_ID)
Greg Claytonc1422c12012-04-09 22:46:21 +0000197 {
198 }
199
200 void
201 Clear ()
202 {
203 packet.clear();
204 type = ePacketTypeInvalid;
205 bytes_transmitted = 0;
206 packet_idx = 0;
Greg Claytond451c1a2012-04-13 21:24:18 +0000207 tid = LLDB_INVALID_THREAD_ID;
Greg Claytonc1422c12012-04-09 22:46:21 +0000208 }
209 std::string packet;
210 PacketType type;
211 uint32_t bytes_transmitted;
212 uint32_t packet_idx;
Greg Claytond451c1a2012-04-13 21:24:18 +0000213 lldb::tid_t tid;
Greg Claytonc1422c12012-04-09 22:46:21 +0000214 };
215
216 History (uint32_t size);
217
218 ~History ();
219
220 // For single char packets for ack, nack and /x03
221 void
222 AddPacket (char packet_char,
223 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000224 uint32_t bytes_transmitted);
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000225
Greg Claytonc1422c12012-04-09 22:46:21 +0000226 void
227 AddPacket (const std::string &src,
228 uint32_t src_len,
229 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000230 uint32_t bytes_transmitted);
Greg Claytonc1422c12012-04-09 22:46:21 +0000231
232 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000233 Dump (Stream &strm) const;
Greg Claytonc1422c12012-04-09 22:46:21 +0000234
235 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000236 Dump (Log *log) const;
Greg Claytonc1422c12012-04-09 22:46:21 +0000237
238 bool
239 DidDumpToLog () const
240 {
241 return m_dumped_to_log;
242 }
243
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000244 protected:
Greg Claytonc1422c12012-04-09 22:46:21 +0000245 uint32_t
246 GetFirstSavedPacketIndex () const
247 {
248 if (m_total_packet_count < m_packets.size())
249 return 0;
250 else
251 return m_curr_idx + 1;
252 }
253
254 uint32_t
255 GetNumPacketsInHistory () const
256 {
257 if (m_total_packet_count < m_packets.size())
258 return m_total_packet_count;
259 else
260 return (uint32_t)m_packets.size();
261 }
262
263 uint32_t
264 GetNextIndex()
265 {
266 ++m_total_packet_count;
267 const uint32_t idx = m_curr_idx;
268 m_curr_idx = NormalizeIndex(idx + 1);
269 return idx;
270 }
271
272 uint32_t
273 NormalizeIndex (uint32_t i) const
274 {
275 return i % m_packets.size();
276 }
277
Greg Claytonc1422c12012-04-09 22:46:21 +0000278 std::vector<Entry> m_packets;
279 uint32_t m_curr_idx;
280 uint32_t m_total_packet_count;
281 mutable bool m_dumped_to_log;
282 };
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000284 uint32_t m_packet_timeout;
285 uint32_t m_echo_number;
286 LazyBool m_supports_qEcho;
287#ifdef ENABLE_MUTEX_ERROR_CHECKING
288 TrackingMutex m_sequence_mutex;
289#else
290 Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time
291#endif
292 Predicate<bool> m_public_is_running;
293 Predicate<bool> m_private_is_running;
294 History m_history;
295 bool m_send_acks;
296 bool m_is_platform; // Set to true if this class represents a platform,
297 // false if this class represents a debug session for
298 // a single process
299
300 CompressionType m_compression_type;
301
Greg Clayton3dedae12013-12-06 21:45:27 +0000302 PacketResult
Greg Clayton37a0a242012-04-11 00:24:49 +0000303 SendPacket (const char *payload,
304 size_t payload_length);
305
Greg Clayton3dedae12013-12-06 21:45:27 +0000306 PacketResult
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307 SendPacketNoLock (const char *payload,
308 size_t payload_length);
309
Greg Clayton3dedae12013-12-06 21:45:27 +0000310 PacketResult
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000311 ReadPacket (StringExtractorGDBRemote &response, uint32_t timeout_usec, bool sync_on_timeout);
312
313 // Pop a packet from the queue in a thread safe manner
314 PacketResult
315 PopPacketFromQueue (StringExtractorGDBRemote &response, uint32_t timeout_usec);
316
317 PacketResult
Greg Clayton73bf5db2011-06-17 01:22:15 +0000318 WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &response,
Greg Claytonb30c50c2015-05-29 00:01:55 +0000319 uint32_t timeout_usec,
320 bool sync_on_timeout);
Greg Clayton6779606a2011-01-22 23:43:18 +0000321
322 bool
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000323 WaitForNotRunningPrivate (const TimeValue *timeout_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324
Jason Molenda91ffe0a2015-06-18 21:46:06 +0000325 bool
326 CompressionIsEnabled ()
327 {
328 return m_compression_type != CompressionType::None;
329 }
330
331 // If compression is enabled, decompress the packet in m_bytes and update
332 // m_bytes with the uncompressed version.
333 // Returns 'true' packet was decompressed and m_bytes is the now-decompressed text.
334 // Returns 'false' if unable to decompress or if the checksum was invalid.
335 //
336 // NB: Once the packet has been decompressed, checksum cannot be computed based
337 // on m_bytes. The checksum was for the compressed packet.
338 bool
339 DecompressPacket ();
340
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000341 Error
342 StartListenThread (const char *hostname = "127.0.0.1", uint16_t port = 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343
Greg Clayton00fe87b2013-12-05 22:58:22 +0000344 bool
345 JoinListenThread ();
346
347 static lldb::thread_result_t
348 ListenThread (lldb::thread_arg_t arg);
Greg Clayton8b82f082011-04-12 05:54:46 +0000349
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000350 // GDB-Remote read thread
351 // . this thread constantly tries to read from the communication
352 // class and stores all packets received in a queue. The usual
353 // threads read requests simply pop packets off the queue in the
354 // usual order.
355 // This setup allows us to intercept and handle async packets, such
356 // as the notify packet.
357
358 // This method is defined as part of communication.h
359 // when the read thread gets any bytes it will pass them on to this function
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000360 void AppendBytesToCache(const uint8_t * bytes,
361 size_t len,
362 bool broadcast,
363 lldb::ConnectionStatus status) override;
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000364
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365private:
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000366 std::queue<StringExtractorGDBRemote> m_packet_queue; // The packet queue
367 lldb_private::Mutex m_packet_queue_mutex; // Mutex for accessing queue
368 Condition m_condition_queue_not_empty; // Condition variable to wait for packets
369
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000370 HostThread m_listen_thread;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000371 std::string m_listen_url;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000372
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
374};
375
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000376} // namespace process_gdb_remote
377} // namespace lldb_private
378
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000379#endif // liblldb_GDBRemoteCommunication_h_