blob: ee2f3276513d7a8169ad32c60c07943c14ceee0c [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
Chaoren Lin18fe6402015-02-03 01:51:47 +000030typedef enum
31{
32 eStoppointInvalid = -1,
33 eBreakpointSoftware = 0,
34 eBreakpointHardware,
35 eWatchpointWrite,
36 eWatchpointRead,
37 eWatchpointReadWrite
38} GDBStoppointType;
39
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040class ProcessGDBRemote;
41
Greg Clayton576d8832011-03-22 04:00:09 +000042class GDBRemoteCommunication : public lldb_private::Communication
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043{
44public:
Greg Claytone5219662010-12-03 06:02:24 +000045 enum
46 {
47 eBroadcastBitRunPacketSent = kLoUserBroadcastBit
48 };
Greg Clayton3dedae12013-12-06 21:45:27 +000049
50 enum class PacketResult
51 {
52 Success = 0, // Success
53 ErrorSendFailed, // Error sending the packet
54 ErrorSendAck, // Didn't get an ack back after sending a packet
55 ErrorReplyFailed, // Error getting the reply
56 ErrorReplyTimeout, // Timed out waiting for reply
57 ErrorReplyInvalid, // Got a reply but it wasn't valid for the packet that was sent
58 ErrorReplyAck, // Sending reply ack failed
Steve Pucci5ae54ae2014-01-25 05:46:51 +000059 ErrorDisconnected, // We were disconnected
60 ErrorNoSequenceLock // We couldn't get the sequence lock for a multi-packet request
Greg Clayton3dedae12013-12-06 21:45:27 +000061 };
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062 //------------------------------------------------------------------
63 // Constructors and Destructors
64 //------------------------------------------------------------------
Greg Clayton8b82f082011-04-12 05:54:46 +000065 GDBRemoteCommunication(const char *comm_name,
66 const char *listener_name,
67 bool is_platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068
69 virtual
70 ~GDBRemoteCommunication();
71
Greg Clayton3dedae12013-12-06 21:45:27 +000072 PacketResult
Greg Claytonc574ede2011-03-10 02:26:48 +000073 GetAck ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074
75 size_t
Greg Clayton6ed95942011-01-22 07:12:45 +000076 SendAck ();
77
78 size_t
79 SendNack ();
80
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081 char
82 CalculcateChecksum (const char *payload,
83 size_t payload_length);
84
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085 bool
Jim Ingham4ceb9282012-06-08 22:50:40 +000086 GetSequenceMutex (lldb_private::Mutex::Locker& locker, const char *failure_message = NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087
Greg Clayton73bf5db2011-06-17 01:22:15 +000088 bool
89 CheckForPacket (const uint8_t *src,
90 size_t src_len,
91 StringExtractorGDBRemote &packet);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092 bool
93 IsRunning() const
94 {
Greg Clayton4dc72282011-01-20 07:53:45 +000095 return m_public_is_running.GetValue();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000096 }
Greg Clayton6779606a2011-01-22 23:43:18 +000097
Greg Clayton1cb64962011-03-24 04:28:38 +000098 bool
99 GetSendAcks ()
100 {
101 return m_send_acks;
102 }
103
Greg Clayton576d8832011-03-22 04:00:09 +0000104 //------------------------------------------------------------------
105 // Client and server must implement these pure virtual functions
106 //------------------------------------------------------------------
107 virtual bool
108 GetThreadSuffixSupported () = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109
Greg Clayton576d8832011-03-22 04:00:09 +0000110 //------------------------------------------------------------------
111 // Set the global packet timeout.
112 //
113 // For clients, this is the timeout that gets used when sending
114 // packets and waiting for responses. For servers, this might not
115 // get used, and if it doesn't this should be moved to the
116 // GDBRemoteCommunicationClient.
117 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000118 uint32_t
119 SetPacketTimeout (uint32_t packet_timeout)
120 {
121 const uint32_t old_packet_timeout = m_packet_timeout;
122 m_packet_timeout = packet_timeout;
123 return old_packet_timeout;
124 }
Greg Clayton71fc2a32011-02-12 06:28:37 +0000125
Greg Clayton73bf5db2011-06-17 01:22:15 +0000126 uint32_t
127 GetPacketTimeoutInMicroSeconds () const
128 {
Peter Collingbourneba23ca02011-06-18 23:52:14 +0000129 return m_packet_timeout * lldb_private::TimeValue::MicroSecPerSec;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000130 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000131 //------------------------------------------------------------------
132 // Start a debugserver instance on the current host using the
133 // supplied connection URL.
134 //------------------------------------------------------------------
Greg Clayton00fe87b2013-12-05 22:58:22 +0000135 lldb_private::Error
Greg Claytonfda4fab2014-01-10 22:24:11 +0000136 StartDebugserverProcess (const char *hostname,
137 uint16_t in_port, // If set to zero, then out_port will contain the bound port on exit
Greg Clayton91a9b2472013-12-04 19:19:12 +0000138 lldb_private::ProcessLaunchInfo &launch_info,
Greg Claytonfda4fab2014-01-10 22:24:11 +0000139 uint16_t &out_port);
Greg Clayton8b82f082011-04-12 05:54:46 +0000140
Greg Claytonc1422c12012-04-09 22:46:21 +0000141 void
Greg Claytond451c1a2012-04-13 21:24:18 +0000142 DumpHistory(lldb_private::Stream &strm);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000143
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144protected:
Greg Claytonc1422c12012-04-09 22:46:21 +0000145
146 class History
147 {
148 public:
149 enum PacketType
150 {
151 ePacketTypeInvalid = 0,
152 ePacketTypeSend,
153 ePacketTypeRecv
154 };
155
156 struct Entry
157 {
158 Entry() :
159 packet(),
160 type (ePacketTypeInvalid),
161 bytes_transmitted (0),
Greg Claytond451c1a2012-04-13 21:24:18 +0000162 packet_idx (0),
163 tid (LLDB_INVALID_THREAD_ID)
Greg Claytonc1422c12012-04-09 22:46:21 +0000164 {
165 }
166
167 void
168 Clear ()
169 {
170 packet.clear();
171 type = ePacketTypeInvalid;
172 bytes_transmitted = 0;
173 packet_idx = 0;
Greg Claytond451c1a2012-04-13 21:24:18 +0000174 tid = LLDB_INVALID_THREAD_ID;
Greg Claytonc1422c12012-04-09 22:46:21 +0000175 }
176 std::string packet;
177 PacketType type;
178 uint32_t bytes_transmitted;
179 uint32_t packet_idx;
Greg Claytond451c1a2012-04-13 21:24:18 +0000180 lldb::tid_t tid;
Greg Claytonc1422c12012-04-09 22:46:21 +0000181 };
182
183 History (uint32_t size);
184
185 ~History ();
186
187 // For single char packets for ack, nack and /x03
188 void
189 AddPacket (char packet_char,
190 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000191 uint32_t bytes_transmitted);
Greg Claytonc1422c12012-04-09 22:46:21 +0000192 void
193 AddPacket (const std::string &src,
194 uint32_t src_len,
195 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000196 uint32_t bytes_transmitted);
Greg Claytonc1422c12012-04-09 22:46:21 +0000197
198 void
199 Dump (lldb_private::Stream &strm) const;
200
201 void
202 Dump (lldb_private::Log *log) const;
203
204 bool
205 DidDumpToLog () const
206 {
207 return m_dumped_to_log;
208 }
209
210protected:
211 uint32_t
212 GetFirstSavedPacketIndex () const
213 {
214 if (m_total_packet_count < m_packets.size())
215 return 0;
216 else
217 return m_curr_idx + 1;
218 }
219
220 uint32_t
221 GetNumPacketsInHistory () const
222 {
223 if (m_total_packet_count < m_packets.size())
224 return m_total_packet_count;
225 else
226 return (uint32_t)m_packets.size();
227 }
228
229 uint32_t
230 GetNextIndex()
231 {
232 ++m_total_packet_count;
233 const uint32_t idx = m_curr_idx;
234 m_curr_idx = NormalizeIndex(idx + 1);
235 return idx;
236 }
237
238 uint32_t
239 NormalizeIndex (uint32_t i) const
240 {
241 return i % m_packets.size();
242 }
243
244
245 std::vector<Entry> m_packets;
246 uint32_t m_curr_idx;
247 uint32_t m_total_packet_count;
248 mutable bool m_dumped_to_log;
249 };
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250
Greg Clayton3dedae12013-12-06 21:45:27 +0000251 PacketResult
Greg Clayton37a0a242012-04-11 00:24:49 +0000252 SendPacket (const char *payload,
253 size_t payload_length);
254
Greg Clayton3dedae12013-12-06 21:45:27 +0000255 PacketResult
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256 SendPacketNoLock (const char *payload,
257 size_t payload_length);
258
Greg Clayton3dedae12013-12-06 21:45:27 +0000259 PacketResult
Greg Clayton73bf5db2011-06-17 01:22:15 +0000260 WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &response,
261 uint32_t timeout_usec);
Greg Clayton6779606a2011-01-22 23:43:18 +0000262
263 bool
264 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265
266 //------------------------------------------------------------------
267 // Classes that inherit from GDBRemoteCommunication can see and modify these
268 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000269 uint32_t m_packet_timeout;
Greg Claytonb09c5382013-12-13 17:20:18 +0000270#ifdef ENABLE_MUTEX_ERROR_CHECKING
Jim Ingham4ceb9282012-06-08 22:50:40 +0000271 lldb_private::TrackingMutex m_sequence_mutex;
272#else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273 lldb_private::Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time
Jim Ingham4ceb9282012-06-08 22:50:40 +0000274#endif
Greg Clayton4dc72282011-01-20 07:53:45 +0000275 lldb_private::Predicate<bool> m_public_is_running;
276 lldb_private::Predicate<bool> m_private_is_running;
Greg Claytonc1422c12012-04-09 22:46:21 +0000277 History m_history;
Greg Clayton1cb64962011-03-24 04:28:38 +0000278 bool m_send_acks;
Greg Clayton8b82f082011-04-12 05:54:46 +0000279 bool m_is_platform; // Set to true if this class represents a platform,
280 // false if this class represents a debug session for
281 // a single process
Greg Clayton1cb64962011-03-24 04:28:38 +0000282
283
Greg Clayton00fe87b2013-12-05 22:58:22 +0000284 lldb_private::Error
Greg Clayton16810922014-02-27 19:38:18 +0000285 StartListenThread (const char *hostname = "127.0.0.1",
Greg Claytond6299802013-12-06 17:46:35 +0000286 uint16_t port = 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287
Greg Clayton00fe87b2013-12-05 22:58:22 +0000288 bool
289 JoinListenThread ();
290
291 static lldb::thread_result_t
292 ListenThread (lldb::thread_arg_t arg);
Greg Clayton8b82f082011-04-12 05:54:46 +0000293
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294private:
Vince Harron1b5a74e2015-01-21 22:42:49 +0000295 lldb_private::HostThread m_listen_thread;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000296 std::string m_listen_url;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000297
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298 //------------------------------------------------------------------
299 // For GDBRemoteCommunication only
300 //------------------------------------------------------------------
301 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
302};
303
304#endif // liblldb_GDBRemoteCommunication_h_