blob: 77f60ea76aae5473e95b07cf87771a4cfa887d2c [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 };
Greg Clayton3dedae12013-12-06 21:45:27 +000052
53 enum class PacketResult
54 {
55 Success = 0, // Success
56 ErrorSendFailed, // Error sending the packet
57 ErrorSendAck, // Didn't get an ack back after sending a packet
58 ErrorReplyFailed, // Error getting the reply
59 ErrorReplyTimeout, // Timed out waiting for reply
60 ErrorReplyInvalid, // Got a reply but it wasn't valid for the packet that was sent
61 ErrorReplyAck, // Sending reply ack failed
Steve Pucci5ae54ae2014-01-25 05:46:51 +000062 ErrorDisconnected, // We were disconnected
63 ErrorNoSequenceLock // We couldn't get the sequence lock for a multi-packet request
Greg Clayton3dedae12013-12-06 21:45:27 +000064 };
Tamas Berghammer912800c2015-02-24 10:23:39 +000065
66 // Class to change the timeout for a given scope and restore it to the original value when the
67 // created ScopedTimeout object got out of scope
68 class ScopedTimeout
69 {
70 public:
71 ScopedTimeout (GDBRemoteCommunication& gdb_comm, uint32_t timeout);
72 ~ScopedTimeout ();
73
74 private:
75 GDBRemoteCommunication& m_gdb_comm;
76 uint32_t m_saved_timeout;
77 };
78
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079 //------------------------------------------------------------------
80 // Constructors and Destructors
81 //------------------------------------------------------------------
Greg Clayton8b82f082011-04-12 05:54:46 +000082 GDBRemoteCommunication(const char *comm_name,
Tamas Berghammere13c2732015-02-11 10:29:30 +000083 const char *listener_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084
85 virtual
86 ~GDBRemoteCommunication();
87
Greg Clayton3dedae12013-12-06 21:45:27 +000088 PacketResult
Greg Claytonc574ede2011-03-10 02:26:48 +000089 GetAck ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000090
91 size_t
Greg Clayton6ed95942011-01-22 07:12:45 +000092 SendAck ();
93
94 size_t
95 SendNack ();
96
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097 char
98 CalculcateChecksum (const char *payload,
99 size_t payload_length);
100
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101 bool
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000102 GetSequenceMutex (Mutex::Locker& locker, const char *failure_message = NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103
Greg Clayton73bf5db2011-06-17 01:22:15 +0000104 bool
105 CheckForPacket (const uint8_t *src,
106 size_t src_len,
107 StringExtractorGDBRemote &packet);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108 bool
109 IsRunning() const
110 {
Greg Clayton4dc72282011-01-20 07:53:45 +0000111 return m_public_is_running.GetValue();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112 }
Greg Clayton6779606a2011-01-22 23:43:18 +0000113
Greg Clayton1cb64962011-03-24 04:28:38 +0000114 bool
115 GetSendAcks ()
116 {
117 return m_send_acks;
118 }
119
Greg Clayton576d8832011-03-22 04:00:09 +0000120 //------------------------------------------------------------------
121 // Client and server must implement these pure virtual functions
122 //------------------------------------------------------------------
123 virtual bool
124 GetThreadSuffixSupported () = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125
Greg Clayton576d8832011-03-22 04:00:09 +0000126 //------------------------------------------------------------------
127 // Set the global packet timeout.
128 //
129 // For clients, this is the timeout that gets used when sending
130 // packets and waiting for responses. For servers, this might not
131 // get used, and if it doesn't this should be moved to the
132 // GDBRemoteCommunicationClient.
133 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000134 uint32_t
135 SetPacketTimeout (uint32_t packet_timeout)
136 {
137 const uint32_t old_packet_timeout = m_packet_timeout;
138 m_packet_timeout = packet_timeout;
139 return old_packet_timeout;
140 }
Greg Clayton71fc2a32011-02-12 06:28:37 +0000141
Greg Clayton73bf5db2011-06-17 01:22:15 +0000142 uint32_t
143 GetPacketTimeoutInMicroSeconds () const
144 {
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000145 return m_packet_timeout * TimeValue::MicroSecPerSec;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000146 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000147 //------------------------------------------------------------------
148 // Start a debugserver instance on the current host using the
149 // supplied connection URL.
150 //------------------------------------------------------------------
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000151 Error
Greg Claytonfda4fab2014-01-10 22:24:11 +0000152 StartDebugserverProcess (const char *hostname,
153 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 +0000154 ProcessLaunchInfo &launch_info,
Greg Claytonfda4fab2014-01-10 22:24:11 +0000155 uint16_t &out_port);
Greg Clayton8b82f082011-04-12 05:54:46 +0000156
Greg Claytonc1422c12012-04-09 22:46:21 +0000157 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000158 DumpHistory(Stream &strm);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000159
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160protected:
Greg Claytonc1422c12012-04-09 22:46:21 +0000161
162 class History
163 {
164 public:
165 enum PacketType
166 {
167 ePacketTypeInvalid = 0,
168 ePacketTypeSend,
169 ePacketTypeRecv
170 };
171
172 struct Entry
173 {
174 Entry() :
175 packet(),
176 type (ePacketTypeInvalid),
177 bytes_transmitted (0),
Greg Claytond451c1a2012-04-13 21:24:18 +0000178 packet_idx (0),
179 tid (LLDB_INVALID_THREAD_ID)
Greg Claytonc1422c12012-04-09 22:46:21 +0000180 {
181 }
182
183 void
184 Clear ()
185 {
186 packet.clear();
187 type = ePacketTypeInvalid;
188 bytes_transmitted = 0;
189 packet_idx = 0;
Greg Claytond451c1a2012-04-13 21:24:18 +0000190 tid = LLDB_INVALID_THREAD_ID;
Greg Claytonc1422c12012-04-09 22:46:21 +0000191 }
192 std::string packet;
193 PacketType type;
194 uint32_t bytes_transmitted;
195 uint32_t packet_idx;
Greg Claytond451c1a2012-04-13 21:24:18 +0000196 lldb::tid_t tid;
Greg Claytonc1422c12012-04-09 22:46:21 +0000197 };
198
199 History (uint32_t size);
200
201 ~History ();
202
203 // For single char packets for ack, nack and /x03
204 void
205 AddPacket (char packet_char,
206 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000207 uint32_t bytes_transmitted);
Greg Claytonc1422c12012-04-09 22:46:21 +0000208 void
209 AddPacket (const std::string &src,
210 uint32_t src_len,
211 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000212 uint32_t bytes_transmitted);
Greg Claytonc1422c12012-04-09 22:46:21 +0000213
214 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000215 Dump (Stream &strm) const;
Greg Claytonc1422c12012-04-09 22:46:21 +0000216
217 void
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000218 Dump (Log *log) const;
Greg Claytonc1422c12012-04-09 22:46:21 +0000219
220 bool
221 DidDumpToLog () const
222 {
223 return m_dumped_to_log;
224 }
225
226protected:
227 uint32_t
228 GetFirstSavedPacketIndex () const
229 {
230 if (m_total_packet_count < m_packets.size())
231 return 0;
232 else
233 return m_curr_idx + 1;
234 }
235
236 uint32_t
237 GetNumPacketsInHistory () const
238 {
239 if (m_total_packet_count < m_packets.size())
240 return m_total_packet_count;
241 else
242 return (uint32_t)m_packets.size();
243 }
244
245 uint32_t
246 GetNextIndex()
247 {
248 ++m_total_packet_count;
249 const uint32_t idx = m_curr_idx;
250 m_curr_idx = NormalizeIndex(idx + 1);
251 return idx;
252 }
253
254 uint32_t
255 NormalizeIndex (uint32_t i) const
256 {
257 return i % m_packets.size();
258 }
259
260
261 std::vector<Entry> m_packets;
262 uint32_t m_curr_idx;
263 uint32_t m_total_packet_count;
264 mutable bool m_dumped_to_log;
265 };
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000266
Greg Clayton3dedae12013-12-06 21:45:27 +0000267 PacketResult
Greg Clayton37a0a242012-04-11 00:24:49 +0000268 SendPacket (const char *payload,
269 size_t payload_length);
270
Greg Clayton3dedae12013-12-06 21:45:27 +0000271 PacketResult
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272 SendPacketNoLock (const char *payload,
273 size_t payload_length);
274
Greg Clayton3dedae12013-12-06 21:45:27 +0000275 PacketResult
Greg Clayton73bf5db2011-06-17 01:22:15 +0000276 WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &response,
277 uint32_t timeout_usec);
Greg Clayton6779606a2011-01-22 23:43:18 +0000278
279 bool
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000280 WaitForNotRunningPrivate (const TimeValue *timeout_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281
282 //------------------------------------------------------------------
283 // Classes that inherit from GDBRemoteCommunication can see and modify these
284 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000285 uint32_t m_packet_timeout;
Greg Claytonb09c5382013-12-13 17:20:18 +0000286#ifdef ENABLE_MUTEX_ERROR_CHECKING
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000287 TrackingMutex m_sequence_mutex;
Jim Ingham4ceb9282012-06-08 22:50:40 +0000288#else
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000289 Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time
Jim Ingham4ceb9282012-06-08 22:50:40 +0000290#endif
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000291 Predicate<bool> m_public_is_running;
292 Predicate<bool> m_private_is_running;
Greg Claytonc1422c12012-04-09 22:46:21 +0000293 History m_history;
Greg Clayton1cb64962011-03-24 04:28:38 +0000294 bool m_send_acks;
Greg Clayton8b82f082011-04-12 05:54:46 +0000295 bool m_is_platform; // Set to true if this class represents a platform,
296 // false if this class represents a debug session for
297 // a single process
Greg Clayton1cb64962011-03-24 04:28:38 +0000298
299
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000300 Error
301 StartListenThread (const char *hostname = "127.0.0.1", uint16_t port = 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302
Greg Clayton00fe87b2013-12-05 22:58:22 +0000303 bool
304 JoinListenThread ();
305
306 static lldb::thread_result_t
307 ListenThread (lldb::thread_arg_t arg);
Greg Clayton8b82f082011-04-12 05:54:46 +0000308
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000309private:
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000310 HostThread m_listen_thread;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000311 std::string m_listen_url;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000312
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313 //------------------------------------------------------------------
314 // For GDBRemoteCommunication only
315 //------------------------------------------------------------------
316 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
317};
318
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000319} // namespace process_gdb_remote
320} // namespace lldb_private
321
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322#endif // liblldb_GDBRemoteCommunication_h_