blob: 6fd1b854d8a3a98e1eae085127fe5064932be8ca [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>
17
18// Other libraries and framework includes
19// Project includes
Greg Claytonc1422c12012-04-09 22:46:21 +000020#include "lldb/lldb-public.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Core/Communication.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/Listener.h"
Zachary Turner39de3112014-09-09 20:54:56 +000023#include "lldb/Host/HostThread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Host/Mutex.h"
25#include "lldb/Host/Predicate.h"
Peter Collingbourneba23ca02011-06-18 23:52:14 +000026#include "lldb/Host/TimeValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Greg Claytonc982c762010-07-09 20:39:50 +000028#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
Tamas Berghammerdb264a62015-03-31 09:52:22 +000030namespace lldb_private {
31namespace process_gdb_remote {
32
Chaoren Lin18fe6402015-02-03 01:51:47 +000033typedef enum
34{
35 eStoppointInvalid = -1,
36 eBreakpointSoftware = 0,
37 eBreakpointHardware,
38 eWatchpointWrite,
39 eWatchpointRead,
40 eWatchpointReadWrite
41} GDBStoppointType;
42
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043class ProcessGDBRemote;
44
Tamas Berghammerdb264a62015-03-31 09:52:22 +000045class GDBRemoteCommunication : public Communication
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046{
47public:
Greg Claytone5219662010-12-03 06:02:24 +000048 enum
49 {
50 eBroadcastBitRunPacketSent = kLoUserBroadcastBit
51 };
Ewan Crawford9aa2da002015-05-27 14:12:34 +000052
53 enum class PacketType
54 {
55 Invalid = 0,
56 Standard,
57 Notify
58 };
59
Greg Clayton3dedae12013-12-06 21:45:27 +000060 enum class PacketResult
61 {
62 Success = 0, // Success
63 ErrorSendFailed, // Error sending the packet
64 ErrorSendAck, // Didn't get an ack back after sending a packet
65 ErrorReplyFailed, // Error getting the reply
66 ErrorReplyTimeout, // Timed out waiting for reply
67 ErrorReplyInvalid, // Got a reply but it wasn't valid for the packet that was sent
68 ErrorReplyAck, // Sending reply ack failed
Steve Pucci5ae54ae2014-01-25 05:46:51 +000069 ErrorDisconnected, // We were disconnected
70 ErrorNoSequenceLock // We couldn't get the sequence lock for a multi-packet request
Greg Clayton3dedae12013-12-06 21:45:27 +000071 };
Tamas Berghammer912800c2015-02-24 10:23:39 +000072
73 // Class to change the timeout for a given scope and restore it to the original value when the
74 // created ScopedTimeout object got out of scope
75 class ScopedTimeout
76 {
77 public:
78 ScopedTimeout (GDBRemoteCommunication& gdb_comm, uint32_t timeout);
79 ~ScopedTimeout ();
80
81 private:
82 GDBRemoteCommunication& m_gdb_comm;
83 uint32_t m_saved_timeout;
84 };
85
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086 //------------------------------------------------------------------
87 // Constructors and Destructors
88 //------------------------------------------------------------------
Greg Clayton8b82f082011-04-12 05:54:46 +000089 GDBRemoteCommunication(const char *comm_name,
Tamas Berghammere13c2732015-02-11 10:29:30 +000090 const char *listener_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091
92 virtual
93 ~GDBRemoteCommunication();
94
Greg Clayton3dedae12013-12-06 21:45:27 +000095 PacketResult
Greg Claytonc574ede2011-03-10 02:26:48 +000096 GetAck ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097
98 size_t
Greg Clayton6ed95942011-01-22 07:12:45 +000099 SendAck ();
100
101 size_t
102 SendNack ();
103
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104 char
105 CalculcateChecksum (const char *payload,
106 size_t payload_length);
107
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108 bool
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000109 GetSequenceMutex (Mutex::Locker& locker, const char *failure_message = NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110
Ewan Crawford9aa2da002015-05-27 14:12:34 +0000111 PacketType
Greg Clayton73bf5db2011-06-17 01:22:15 +0000112 CheckForPacket (const uint8_t *src,
113 size_t src_len,
114 StringExtractorGDBRemote &packet);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115 bool
116 IsRunning() const
117 {
Greg Clayton4dc72282011-01-20 07:53:45 +0000118 return m_public_is_running.GetValue();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119 }
Greg Clayton6779606a2011-01-22 23:43:18 +0000120
Greg Clayton1cb64962011-03-24 04:28:38 +0000121 bool
122 GetSendAcks ()
123 {
124 return m_send_acks;
125 }
126
Greg Clayton576d8832011-03-22 04:00:09 +0000127 //------------------------------------------------------------------
128 // Client and server must implement these pure virtual functions
129 //------------------------------------------------------------------
130 virtual bool
131 GetThreadSuffixSupported () = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132
Greg Clayton576d8832011-03-22 04:00:09 +0000133 //------------------------------------------------------------------
134 // Set the global packet timeout.
135 //
136 // For clients, this is the timeout that gets used when sending
137 // packets and waiting for responses. For servers, this might not
138 // get used, and if it doesn't this should be moved to the
139 // GDBRemoteCommunicationClient.
140 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000141 uint32_t
142 SetPacketTimeout (uint32_t packet_timeout)
143 {
144 const uint32_t old_packet_timeout = m_packet_timeout;
145 m_packet_timeout = packet_timeout;
146 return old_packet_timeout;
147 }
Greg Clayton71fc2a32011-02-12 06:28:37 +0000148
Greg Clayton73bf5db2011-06-17 01:22:15 +0000149 uint32_t
150 GetPacketTimeoutInMicroSeconds () const
151 {
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000152 return m_packet_timeout * TimeValue::MicroSecPerSec;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000153 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000154 //------------------------------------------------------------------
155 // Start a debugserver instance on the current host using the
156 // supplied connection URL.
157 //------------------------------------------------------------------
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000158 Error
Greg Claytonfda4fab2014-01-10 22:24:11 +0000159 StartDebugserverProcess (const char *hostname,
160 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 +0000161 ProcessLaunchInfo &launch_info,
Greg Claytonfda4fab2014-01-10 22:24:11 +0000162 uint16_t &out_port);
Greg Clayton8b82f082011-04-12 05:54:46 +0000163
Greg Claytonc1422c12012-04-09 22:46:21 +0000164 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000165 DumpHistory(Stream &strm);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000166
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167protected:
Greg Claytonc1422c12012-04-09 22:46:21 +0000168
169 class History
170 {
171 public:
172 enum PacketType
173 {
174 ePacketTypeInvalid = 0,
175 ePacketTypeSend,
176 ePacketTypeRecv
177 };
178
179 struct Entry
180 {
181 Entry() :
182 packet(),
183 type (ePacketTypeInvalid),
184 bytes_transmitted (0),
Greg Claytond451c1a2012-04-13 21:24:18 +0000185 packet_idx (0),
186 tid (LLDB_INVALID_THREAD_ID)
Greg Claytonc1422c12012-04-09 22:46:21 +0000187 {
188 }
189
190 void
191 Clear ()
192 {
193 packet.clear();
194 type = ePacketTypeInvalid;
195 bytes_transmitted = 0;
196 packet_idx = 0;
Greg Claytond451c1a2012-04-13 21:24:18 +0000197 tid = LLDB_INVALID_THREAD_ID;
Greg Claytonc1422c12012-04-09 22:46:21 +0000198 }
199 std::string packet;
200 PacketType type;
201 uint32_t bytes_transmitted;
202 uint32_t packet_idx;
Greg Claytond451c1a2012-04-13 21:24:18 +0000203 lldb::tid_t tid;
Greg Claytonc1422c12012-04-09 22:46:21 +0000204 };
205
206 History (uint32_t size);
207
208 ~History ();
209
210 // For single char packets for ack, nack and /x03
211 void
212 AddPacket (char packet_char,
213 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000214 uint32_t bytes_transmitted);
Greg Claytonc1422c12012-04-09 22:46:21 +0000215 void
216 AddPacket (const std::string &src,
217 uint32_t src_len,
218 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000219 uint32_t bytes_transmitted);
Greg Claytonc1422c12012-04-09 22:46:21 +0000220
221 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000222 Dump (Stream &strm) const;
Greg Claytonc1422c12012-04-09 22:46:21 +0000223
224 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000225 Dump (Log *log) const;
Greg Claytonc1422c12012-04-09 22:46:21 +0000226
227 bool
228 DidDumpToLog () const
229 {
230 return m_dumped_to_log;
231 }
232
233protected:
234 uint32_t
235 GetFirstSavedPacketIndex () const
236 {
237 if (m_total_packet_count < m_packets.size())
238 return 0;
239 else
240 return m_curr_idx + 1;
241 }
242
243 uint32_t
244 GetNumPacketsInHistory () const
245 {
246 if (m_total_packet_count < m_packets.size())
247 return m_total_packet_count;
248 else
249 return (uint32_t)m_packets.size();
250 }
251
252 uint32_t
253 GetNextIndex()
254 {
255 ++m_total_packet_count;
256 const uint32_t idx = m_curr_idx;
257 m_curr_idx = NormalizeIndex(idx + 1);
258 return idx;
259 }
260
261 uint32_t
262 NormalizeIndex (uint32_t i) const
263 {
264 return i % m_packets.size();
265 }
266
267
268 std::vector<Entry> m_packets;
269 uint32_t m_curr_idx;
270 uint32_t m_total_packet_count;
271 mutable bool m_dumped_to_log;
272 };
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273
Greg Clayton3dedae12013-12-06 21:45:27 +0000274 PacketResult
Greg Clayton37a0a242012-04-11 00:24:49 +0000275 SendPacket (const char *payload,
276 size_t payload_length);
277
Greg Clayton3dedae12013-12-06 21:45:27 +0000278 PacketResult
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279 SendPacketNoLock (const char *payload,
280 size_t payload_length);
281
Greg Clayton3dedae12013-12-06 21:45:27 +0000282 PacketResult
Greg Clayton73bf5db2011-06-17 01:22:15 +0000283 WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &response,
284 uint32_t timeout_usec);
Greg Clayton6779606a2011-01-22 23:43:18 +0000285
286 bool
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000287 WaitForNotRunningPrivate (const TimeValue *timeout_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288
289 //------------------------------------------------------------------
290 // Classes that inherit from GDBRemoteCommunication can see and modify these
291 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000292 uint32_t m_packet_timeout;
Greg Claytonb09c5382013-12-13 17:20:18 +0000293#ifdef ENABLE_MUTEX_ERROR_CHECKING
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000294 TrackingMutex m_sequence_mutex;
Jim Ingham4ceb9282012-06-08 22:50:40 +0000295#else
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000296 Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time
Jim Ingham4ceb9282012-06-08 22:50:40 +0000297#endif
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000298 Predicate<bool> m_public_is_running;
299 Predicate<bool> m_private_is_running;
Greg Claytonc1422c12012-04-09 22:46:21 +0000300 History m_history;
Greg Clayton1cb64962011-03-24 04:28:38 +0000301 bool m_send_acks;
Greg Clayton8b82f082011-04-12 05:54:46 +0000302 bool m_is_platform; // Set to true if this class represents a platform,
303 // false if this class represents a debug session for
304 // a single process
Greg Clayton1cb64962011-03-24 04:28:38 +0000305
306
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000307 Error
308 StartListenThread (const char *hostname = "127.0.0.1", uint16_t port = 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309
Greg Clayton00fe87b2013-12-05 22:58:22 +0000310 bool
311 JoinListenThread ();
312
313 static lldb::thread_result_t
314 ListenThread (lldb::thread_arg_t arg);
Greg Clayton8b82f082011-04-12 05:54:46 +0000315
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000316private:
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000317 HostThread m_listen_thread;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000318 std::string m_listen_url;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000319
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320 //------------------------------------------------------------------
321 // For GDBRemoteCommunication only
322 //------------------------------------------------------------------
323 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
324};
325
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000326} // namespace process_gdb_remote
327} // namespace lldb_private
328
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329#endif // liblldb_GDBRemoteCommunication_h_