blob: 7379bb3aa09b192e7399fde60dc51b8440449293 [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
15#include <list>
16#include <string>
Ewan Crawfordfab40d32015-06-16 15:50:18 +000017#include <queue>
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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097 //------------------------------------------------------------------
98 // Constructors and Destructors
99 //------------------------------------------------------------------
Greg Clayton8b82f082011-04-12 05:54:46 +0000100 GDBRemoteCommunication(const char *comm_name,
Tamas Berghammere13c2732015-02-11 10:29:30 +0000101 const char *listener_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102
103 virtual
104 ~GDBRemoteCommunication();
105
Greg Clayton3dedae12013-12-06 21:45:27 +0000106 PacketResult
Greg Claytonc574ede2011-03-10 02:26:48 +0000107 GetAck ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108
109 size_t
Greg Clayton6ed95942011-01-22 07:12:45 +0000110 SendAck ();
111
112 size_t
113 SendNack ();
114
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115 char
116 CalculcateChecksum (const char *payload,
117 size_t payload_length);
118
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119 bool
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000120 GetSequenceMutex (Mutex::Locker& locker, const char *failure_message = NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121
Ewan Crawford9aa2da002015-05-27 14:12:34 +0000122 PacketType
Greg Clayton73bf5db2011-06-17 01:22:15 +0000123 CheckForPacket (const uint8_t *src,
124 size_t src_len,
125 StringExtractorGDBRemote &packet);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126 bool
127 IsRunning() const
128 {
Greg Clayton4dc72282011-01-20 07:53:45 +0000129 return m_public_is_running.GetValue();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130 }
Greg Clayton6779606a2011-01-22 23:43:18 +0000131
Greg Clayton1cb64962011-03-24 04:28:38 +0000132 bool
133 GetSendAcks ()
134 {
135 return m_send_acks;
136 }
137
Greg Clayton576d8832011-03-22 04:00:09 +0000138 //------------------------------------------------------------------
139 // Client and server must implement these pure virtual functions
140 //------------------------------------------------------------------
141 virtual bool
142 GetThreadSuffixSupported () = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143
Greg Clayton576d8832011-03-22 04:00:09 +0000144 //------------------------------------------------------------------
145 // Set the global packet timeout.
146 //
147 // For clients, this is the timeout that gets used when sending
148 // packets and waiting for responses. For servers, this might not
149 // get used, and if it doesn't this should be moved to the
150 // GDBRemoteCommunicationClient.
151 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000152 uint32_t
153 SetPacketTimeout (uint32_t packet_timeout)
154 {
155 const uint32_t old_packet_timeout = m_packet_timeout;
156 m_packet_timeout = packet_timeout;
157 return old_packet_timeout;
158 }
Greg Clayton71fc2a32011-02-12 06:28:37 +0000159
Greg Clayton73bf5db2011-06-17 01:22:15 +0000160 uint32_t
161 GetPacketTimeoutInMicroSeconds () const
162 {
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000163 return m_packet_timeout * TimeValue::MicroSecPerSec;
Greg Clayton73bf5db2011-06-17 01:22:15 +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
Greg Claytonfda4fab2014-01-10 22:24:11 +0000170 StartDebugserverProcess (const char *hostname,
171 uint16_t in_port, // If set to zero, then out_port will contain the bound port on exit
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000172 ProcessLaunchInfo &launch_info,
Greg Claytonfda4fab2014-01-10 22:24:11 +0000173 uint16_t &out_port);
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
180 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);
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
244protected:
245 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
278
279 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
Greg Clayton3dedae12013-12-06 21:45:27 +0000285 PacketResult
Greg Clayton37a0a242012-04-11 00:24:49 +0000286 SendPacket (const char *payload,
287 size_t payload_length);
288
Greg Clayton3dedae12013-12-06 21:45:27 +0000289 PacketResult
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290 SendPacketNoLock (const char *payload,
291 size_t payload_length);
292
Greg Clayton3dedae12013-12-06 21:45:27 +0000293 PacketResult
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000294 ReadPacket (StringExtractorGDBRemote &response, uint32_t timeout_usec, bool sync_on_timeout);
295
296 // Pop a packet from the queue in a thread safe manner
297 PacketResult
298 PopPacketFromQueue (StringExtractorGDBRemote &response, uint32_t timeout_usec);
299
300 PacketResult
Greg Clayton73bf5db2011-06-17 01:22:15 +0000301 WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &response,
Greg Claytonb30c50c2015-05-29 00:01:55 +0000302 uint32_t timeout_usec,
303 bool sync_on_timeout);
Greg Clayton6779606a2011-01-22 23:43:18 +0000304
305 bool
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000306 WaitForNotRunningPrivate (const TimeValue *timeout_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307
Jason Molenda91ffe0a2015-06-18 21:46:06 +0000308 bool
309 CompressionIsEnabled ()
310 {
311 return m_compression_type != CompressionType::None;
312 }
313
314 // If compression is enabled, decompress the packet in m_bytes and update
315 // m_bytes with the uncompressed version.
316 // Returns 'true' packet was decompressed and m_bytes is the now-decompressed text.
317 // Returns 'false' if unable to decompress or if the checksum was invalid.
318 //
319 // NB: Once the packet has been decompressed, checksum cannot be computed based
320 // on m_bytes. The checksum was for the compressed packet.
321 bool
322 DecompressPacket ();
323
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000324 //------------------------------------------------------------------
325 // Classes that inherit from GDBRemoteCommunication can see and modify these
326 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000327 uint32_t m_packet_timeout;
Greg Claytonb30c50c2015-05-29 00:01:55 +0000328 uint32_t m_echo_number;
329 LazyBool m_supports_qEcho;
Greg Claytonb09c5382013-12-13 17:20:18 +0000330#ifdef ENABLE_MUTEX_ERROR_CHECKING
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000331 TrackingMutex m_sequence_mutex;
Jim Ingham4ceb9282012-06-08 22:50:40 +0000332#else
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000333 Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time
Jim Ingham4ceb9282012-06-08 22:50:40 +0000334#endif
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000335 Predicate<bool> m_public_is_running;
336 Predicate<bool> m_private_is_running;
Greg Claytonc1422c12012-04-09 22:46:21 +0000337 History m_history;
Greg Clayton1cb64962011-03-24 04:28:38 +0000338 bool m_send_acks;
Greg Clayton8b82f082011-04-12 05:54:46 +0000339 bool m_is_platform; // Set to true if this class represents a platform,
340 // false if this class represents a debug session for
341 // a single process
Greg Clayton1cb64962011-03-24 04:28:38 +0000342
Jason Molenda91ffe0a2015-06-18 21:46:06 +0000343 CompressionType m_compression_type;
Greg Clayton1cb64962011-03-24 04:28:38 +0000344
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000345 Error
346 StartListenThread (const char *hostname = "127.0.0.1", uint16_t port = 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347
Greg Clayton00fe87b2013-12-05 22:58:22 +0000348 bool
349 JoinListenThread ();
350
351 static lldb::thread_result_t
352 ListenThread (lldb::thread_arg_t arg);
Greg Clayton8b82f082011-04-12 05:54:46 +0000353
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000354 // GDB-Remote read thread
355 // . this thread constantly tries to read from the communication
356 // class and stores all packets received in a queue. The usual
357 // threads read requests simply pop packets off the queue in the
358 // usual order.
359 // This setup allows us to intercept and handle async packets, such
360 // as the notify packet.
361
362 // This method is defined as part of communication.h
363 // when the read thread gets any bytes it will pass them on to this function
364 virtual void AppendBytesToCache (const uint8_t * bytes, size_t len, bool broadcast, lldb::ConnectionStatus status);
365
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366private:
Ewan Crawfordfab40d32015-06-16 15:50:18 +0000367
368 std::queue<StringExtractorGDBRemote> m_packet_queue; // The packet queue
369 lldb_private::Mutex m_packet_queue_mutex; // Mutex for accessing queue
370 Condition m_condition_queue_not_empty; // Condition variable to wait for packets
371
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000372 HostThread m_listen_thread;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000373 std::string m_listen_url;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000374
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375 //------------------------------------------------------------------
376 // For GDBRemoteCommunication only
377 //------------------------------------------------------------------
378 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
379};
380
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000381} // namespace process_gdb_remote
382} // namespace lldb_private
383
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384#endif // liblldb_GDBRemoteCommunication_h_