blob: ccc8ef1b247483e2aac06d289360d471ec9d12a9 [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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028
Greg Claytonc982c762010-07-09 20:39:50 +000029#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
Tamas Berghammerdb264a62015-03-31 09:52:22 +000031namespace lldb_private {
32namespace process_gdb_remote {
33
Chaoren Lin18fe6402015-02-03 01:51:47 +000034typedef enum
35{
36 eStoppointInvalid = -1,
37 eBreakpointSoftware = 0,
38 eBreakpointHardware,
39 eWatchpointWrite,
40 eWatchpointRead,
41 eWatchpointReadWrite
42} GDBStoppointType;
43
Jason Molenda91ffe0a2015-06-18 21:46:06 +000044enum class CompressionType
45{
46 None = 0, // no compression
47 ZlibDeflate, // zlib's deflate compression scheme, requires zlib or Apple's libcompression
48 LZFSE, // an Apple compression scheme, requires Apple's libcompression
49 LZ4, // lz compression - called "lz4 raw" in libcompression terms, compat with https://code.google.com/p/lz4/
50 LZMA, // Lempel–Ziv–Markov chain algorithm
51};
52
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053class ProcessGDBRemote;
54
Tamas Berghammerdb264a62015-03-31 09:52:22 +000055class GDBRemoteCommunication : public Communication
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056{
57public:
Greg Claytone5219662010-12-03 06:02:24 +000058 enum
59 {
Ewan Crawfordfab40d32015-06-16 15:50:18 +000060 eBroadcastBitRunPacketSent = kLoUserBroadcastBit,
61 eBroadcastBitGdbReadThreadGotNotify = kLoUserBroadcastBit << 1 // Sent when we received a notify packet.
Greg Claytone5219662010-12-03 06:02:24 +000062 };
Ewan Crawford9aa2da002015-05-27 14:12:34 +000063
64 enum class PacketType
65 {
66 Invalid = 0,
67 Standard,
68 Notify
69 };
70
Greg Clayton3dedae12013-12-06 21:45:27 +000071 enum class PacketResult
72 {
73 Success = 0, // Success
74 ErrorSendFailed, // Error sending the packet
75 ErrorSendAck, // Didn't get an ack back after sending a packet
76 ErrorReplyFailed, // Error getting the reply
77 ErrorReplyTimeout, // Timed out waiting for reply
78 ErrorReplyInvalid, // Got a reply but it wasn't valid for the packet that was sent
79 ErrorReplyAck, // Sending reply ack failed
Steve Pucci5ae54ae2014-01-25 05:46:51 +000080 ErrorDisconnected, // We were disconnected
81 ErrorNoSequenceLock // We couldn't get the sequence lock for a multi-packet request
Greg Clayton3dedae12013-12-06 21:45:27 +000082 };
Tamas Berghammer912800c2015-02-24 10:23:39 +000083
84 // Class to change the timeout for a given scope and restore it to the original value when the
85 // created ScopedTimeout object got out of scope
86 class ScopedTimeout
87 {
88 public:
89 ScopedTimeout (GDBRemoteCommunication& gdb_comm, uint32_t timeout);
90 ~ScopedTimeout ();
91
92 private:
93 GDBRemoteCommunication& m_gdb_comm;
94 uint32_t m_saved_timeout;
95 };
96
Greg Clayton8b82f082011-04-12 05:54:46 +000097 GDBRemoteCommunication(const char *comm_name,
Tamas Berghammere13c2732015-02-11 10:29:30 +000098 const char *listener_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000100 ~GDBRemoteCommunication() override;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101
Greg Clayton3dedae12013-12-06 21:45:27 +0000102 PacketResult
Greg Claytonc574ede2011-03-10 02:26:48 +0000103 GetAck ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104
105 size_t
Greg Clayton6ed95942011-01-22 07:12:45 +0000106 SendAck ();
107
108 size_t
109 SendNack ();
110
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111 char
112 CalculcateChecksum (const char *payload,
113 size_t payload_length);
114
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115 bool
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000116 GetSequenceMutex(Mutex::Locker& locker, const char *failure_message = nullptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117
Ewan Crawford9aa2da002015-05-27 14:12:34 +0000118 PacketType
Greg Clayton73bf5db2011-06-17 01:22:15 +0000119 CheckForPacket (const uint8_t *src,
120 size_t src_len,
121 StringExtractorGDBRemote &packet);
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000122
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123 bool
124 IsRunning() const
125 {
Greg Clayton4dc72282011-01-20 07:53:45 +0000126 return m_public_is_running.GetValue();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127 }
Greg Clayton6779606a2011-01-22 23:43:18 +0000128
Greg Clayton1cb64962011-03-24 04:28:38 +0000129 bool
130 GetSendAcks ()
131 {
132 return m_send_acks;
133 }
134
Greg Clayton576d8832011-03-22 04:00:09 +0000135 //------------------------------------------------------------------
136 // Client and server must implement these pure virtual functions
137 //------------------------------------------------------------------
138 virtual bool
139 GetThreadSuffixSupported () = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140
Greg Clayton576d8832011-03-22 04:00:09 +0000141 //------------------------------------------------------------------
142 // Set the global packet timeout.
143 //
144 // For clients, this is the timeout that gets used when sending
145 // packets and waiting for responses. For servers, this might not
146 // get used, and if it doesn't this should be moved to the
147 // GDBRemoteCommunicationClient.
148 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000149 uint32_t
150 SetPacketTimeout (uint32_t packet_timeout)
151 {
152 const uint32_t old_packet_timeout = m_packet_timeout;
153 m_packet_timeout = packet_timeout;
154 return old_packet_timeout;
155 }
Greg Clayton71fc2a32011-02-12 06:28:37 +0000156
Greg Clayton73bf5db2011-06-17 01:22:15 +0000157 uint32_t
158 GetPacketTimeoutInMicroSeconds () const
159 {
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000160 return m_packet_timeout * TimeValue::MicroSecPerSec;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000161 }
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000162
Greg Clayton8b82f082011-04-12 05:54:46 +0000163 //------------------------------------------------------------------
164 // Start a debugserver instance on the current host using the
165 // supplied connection URL.
166 //------------------------------------------------------------------
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000167 Error
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000168 StartDebugserverProcess(const char *url,
169 Platform *platform, // If non nullptr, then check with the platform for the GDB server binary if it can't be located
170 ProcessLaunchInfo &launch_info,
171 uint16_t *port);
Greg Clayton8b82f082011-04-12 05:54:46 +0000172
Greg Claytonc1422c12012-04-09 22:46:21 +0000173 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000174 DumpHistory(Stream &strm);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000175
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000176protected:
Greg Claytonc1422c12012-04-09 22:46:21 +0000177 class History
178 {
179 public:
180 enum PacketType
181 {
182 ePacketTypeInvalid = 0,
183 ePacketTypeSend,
184 ePacketTypeRecv
185 };
186
187 struct Entry
188 {
189 Entry() :
190 packet(),
191 type (ePacketTypeInvalid),
192 bytes_transmitted (0),
Greg Claytond451c1a2012-04-13 21:24:18 +0000193 packet_idx (0),
194 tid (LLDB_INVALID_THREAD_ID)
Greg Claytonc1422c12012-04-09 22:46:21 +0000195 {
196 }
197
198 void
199 Clear ()
200 {
201 packet.clear();
202 type = ePacketTypeInvalid;
203 bytes_transmitted = 0;
204 packet_idx = 0;
Greg Claytond451c1a2012-04-13 21:24:18 +0000205 tid = LLDB_INVALID_THREAD_ID;
Greg Claytonc1422c12012-04-09 22:46:21 +0000206 }
207 std::string packet;
208 PacketType type;
209 uint32_t bytes_transmitted;
210 uint32_t packet_idx;
Greg Claytond451c1a2012-04-13 21:24:18 +0000211 lldb::tid_t tid;
Greg Claytonc1422c12012-04-09 22:46:21 +0000212 };
213
214 History (uint32_t size);
215
216 ~History ();
217
218 // For single char packets for ack, nack and /x03
219 void
220 AddPacket (char packet_char,
221 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000222 uint32_t bytes_transmitted);
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000223
Greg Claytonc1422c12012-04-09 22:46:21 +0000224 void
225 AddPacket (const std::string &src,
226 uint32_t src_len,
227 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000228 uint32_t bytes_transmitted);
Greg Claytonc1422c12012-04-09 22:46:21 +0000229
230 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000231 Dump (Stream &strm) const;
Greg Claytonc1422c12012-04-09 22:46:21 +0000232
233 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000234 Dump (Log *log) const;
Greg Claytonc1422c12012-04-09 22:46:21 +0000235
236 bool
237 DidDumpToLog () const
238 {
239 return m_dumped_to_log;
240 }
241
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000242 protected:
Greg Claytonc1422c12012-04-09 22:46:21 +0000243 uint32_t
244 GetFirstSavedPacketIndex () const
245 {
246 if (m_total_packet_count < m_packets.size())
247 return 0;
248 else
249 return m_curr_idx + 1;
250 }
251
252 uint32_t
253 GetNumPacketsInHistory () const
254 {
255 if (m_total_packet_count < m_packets.size())
256 return m_total_packet_count;
257 else
258 return (uint32_t)m_packets.size();
259 }
260
261 uint32_t
262 GetNextIndex()
263 {
264 ++m_total_packet_count;
265 const uint32_t idx = m_curr_idx;
266 m_curr_idx = NormalizeIndex(idx + 1);
267 return idx;
268 }
269
270 uint32_t
271 NormalizeIndex (uint32_t i) const
272 {
273 return i % m_packets.size();
274 }
275
Greg Claytonc1422c12012-04-09 22:46:21 +0000276 std::vector<Entry> m_packets;
277 uint32_t m_curr_idx;
278 uint32_t m_total_packet_count;
279 mutable bool m_dumped_to_log;
280 };
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000282 uint32_t m_packet_timeout;
283 uint32_t m_echo_number;
284 LazyBool m_supports_qEcho;
285#ifdef ENABLE_MUTEX_ERROR_CHECKING
286 TrackingMutex m_sequence_mutex;
287#else
288 Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time
289#endif
290 Predicate<bool> m_public_is_running;
291 Predicate<bool> m_private_is_running;
292 History m_history;
293 bool m_send_acks;
294 bool m_is_platform; // Set to true if this class represents a platform,
295 // false if this class represents a debug session for
296 // a single process
297
298 CompressionType m_compression_type;
299
Greg Clayton3dedae12013-12-06 21:45:27 +0000300 PacketResult
Greg Clayton37a0a242012-04-11 00:24:49 +0000301 SendPacket (const char *payload,
302 size_t payload_length);
303
Greg Clayton3dedae12013-12-06 21:45:27 +0000304 PacketResult
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305 SendPacketNoLock (const char *payload,
306 size_t payload_length);
307
Greg Clayton3dedae12013-12-06 21:45:27 +0000308 PacketResult
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000309 ReadPacket (StringExtractorGDBRemote &response, uint32_t timeout_usec, bool sync_on_timeout);
310
311 // Pop a packet from the queue in a thread safe manner
312 PacketResult
313 PopPacketFromQueue (StringExtractorGDBRemote &response, uint32_t timeout_usec);
314
315 PacketResult
Greg Clayton73bf5db2011-06-17 01:22:15 +0000316 WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &response,
Greg Claytonb30c50c2015-05-29 00:01:55 +0000317 uint32_t timeout_usec,
318 bool sync_on_timeout);
Greg Clayton6779606a2011-01-22 23:43:18 +0000319
320 bool
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000321 WaitForNotRunningPrivate (const TimeValue *timeout_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322
Jason Molenda91ffe0a2015-06-18 21:46:06 +0000323 bool
324 CompressionIsEnabled ()
325 {
326 return m_compression_type != CompressionType::None;
327 }
328
329 // If compression is enabled, decompress the packet in m_bytes and update
330 // m_bytes with the uncompressed version.
331 // Returns 'true' packet was decompressed and m_bytes is the now-decompressed text.
332 // Returns 'false' if unable to decompress or if the checksum was invalid.
333 //
334 // NB: Once the packet has been decompressed, checksum cannot be computed based
335 // on m_bytes. The checksum was for the compressed packet.
336 bool
337 DecompressPacket ();
338
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000339 Error
340 StartListenThread (const char *hostname = "127.0.0.1", uint16_t port = 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341
Greg Clayton00fe87b2013-12-05 22:58:22 +0000342 bool
343 JoinListenThread ();
344
345 static lldb::thread_result_t
346 ListenThread (lldb::thread_arg_t arg);
Greg Clayton8b82f082011-04-12 05:54:46 +0000347
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000348 // GDB-Remote read thread
349 // . this thread constantly tries to read from the communication
350 // class and stores all packets received in a queue. The usual
351 // threads read requests simply pop packets off the queue in the
352 // usual order.
353 // This setup allows us to intercept and handle async packets, such
354 // as the notify packet.
355
356 // This method is defined as part of communication.h
357 // when the read thread gets any bytes it will pass them on to this function
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000358 void AppendBytesToCache(const uint8_t * bytes,
359 size_t len,
360 bool broadcast,
361 lldb::ConnectionStatus status) override;
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000362
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363private:
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000364 std::queue<StringExtractorGDBRemote> m_packet_queue; // The packet queue
365 lldb_private::Mutex m_packet_queue_mutex; // Mutex for accessing queue
366 Condition m_condition_queue_not_empty; // Condition variable to wait for packets
367
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000368 HostThread m_listen_thread;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000369 std::string m_listen_url;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000370
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
372};
373
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000374} // namespace process_gdb_remote
375} // namespace lldb_private
376
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000377#endif // liblldb_GDBRemoteCommunication_h_