blob: b704df3b72f804f49f7a42a1078db84cdaa2998b [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 };
Tamas Berghammer912800c2015-02-24 10:23:39 +000062
63 // Class to change the timeout for a given scope and restore it to the original value when the
64 // created ScopedTimeout object got out of scope
65 class ScopedTimeout
66 {
67 public:
68 ScopedTimeout (GDBRemoteCommunication& gdb_comm, uint32_t timeout);
69 ~ScopedTimeout ();
70
71 private:
72 GDBRemoteCommunication& m_gdb_comm;
73 uint32_t m_saved_timeout;
74 };
75
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076 //------------------------------------------------------------------
77 // Constructors and Destructors
78 //------------------------------------------------------------------
Greg Clayton8b82f082011-04-12 05:54:46 +000079 GDBRemoteCommunication(const char *comm_name,
Tamas Berghammere13c2732015-02-11 10:29:30 +000080 const char *listener_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081
82 virtual
83 ~GDBRemoteCommunication();
84
Greg Clayton3dedae12013-12-06 21:45:27 +000085 PacketResult
Greg Claytonc574ede2011-03-10 02:26:48 +000086 GetAck ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087
88 size_t
Greg Clayton6ed95942011-01-22 07:12:45 +000089 SendAck ();
90
91 size_t
92 SendNack ();
93
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094 char
95 CalculcateChecksum (const char *payload,
96 size_t payload_length);
97
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098 bool
Jim Ingham4ceb9282012-06-08 22:50:40 +000099 GetSequenceMutex (lldb_private::Mutex::Locker& locker, const char *failure_message = NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000100
Greg Clayton73bf5db2011-06-17 01:22:15 +0000101 bool
102 CheckForPacket (const uint8_t *src,
103 size_t src_len,
104 StringExtractorGDBRemote &packet);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105 bool
106 IsRunning() const
107 {
Greg Clayton4dc72282011-01-20 07:53:45 +0000108 return m_public_is_running.GetValue();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109 }
Greg Clayton6779606a2011-01-22 23:43:18 +0000110
Greg Clayton1cb64962011-03-24 04:28:38 +0000111 bool
112 GetSendAcks ()
113 {
114 return m_send_acks;
115 }
116
Greg Clayton576d8832011-03-22 04:00:09 +0000117 //------------------------------------------------------------------
118 // Client and server must implement these pure virtual functions
119 //------------------------------------------------------------------
120 virtual bool
121 GetThreadSuffixSupported () = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000122
Greg Clayton576d8832011-03-22 04:00:09 +0000123 //------------------------------------------------------------------
124 // Set the global packet timeout.
125 //
126 // For clients, this is the timeout that gets used when sending
127 // packets and waiting for responses. For servers, this might not
128 // get used, and if it doesn't this should be moved to the
129 // GDBRemoteCommunicationClient.
130 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000131 uint32_t
132 SetPacketTimeout (uint32_t packet_timeout)
133 {
134 const uint32_t old_packet_timeout = m_packet_timeout;
135 m_packet_timeout = packet_timeout;
136 return old_packet_timeout;
137 }
Greg Clayton71fc2a32011-02-12 06:28:37 +0000138
Greg Clayton73bf5db2011-06-17 01:22:15 +0000139 uint32_t
140 GetPacketTimeoutInMicroSeconds () const
141 {
Peter Collingbourneba23ca02011-06-18 23:52:14 +0000142 return m_packet_timeout * lldb_private::TimeValue::MicroSecPerSec;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000143 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000144 //------------------------------------------------------------------
145 // Start a debugserver instance on the current host using the
146 // supplied connection URL.
147 //------------------------------------------------------------------
Greg Clayton00fe87b2013-12-05 22:58:22 +0000148 lldb_private::Error
Greg Claytonfda4fab2014-01-10 22:24:11 +0000149 StartDebugserverProcess (const char *hostname,
150 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 +0000151 lldb_private::ProcessLaunchInfo &launch_info,
Greg Claytonfda4fab2014-01-10 22:24:11 +0000152 uint16_t &out_port);
Greg Clayton8b82f082011-04-12 05:54:46 +0000153
Greg Claytonc1422c12012-04-09 22:46:21 +0000154 void
Greg Claytond451c1a2012-04-13 21:24:18 +0000155 DumpHistory(lldb_private::Stream &strm);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000156
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157protected:
Greg Claytonc1422c12012-04-09 22:46:21 +0000158
159 class History
160 {
161 public:
162 enum PacketType
163 {
164 ePacketTypeInvalid = 0,
165 ePacketTypeSend,
166 ePacketTypeRecv
167 };
168
169 struct Entry
170 {
171 Entry() :
172 packet(),
173 type (ePacketTypeInvalid),
174 bytes_transmitted (0),
Greg Claytond451c1a2012-04-13 21:24:18 +0000175 packet_idx (0),
176 tid (LLDB_INVALID_THREAD_ID)
Greg Claytonc1422c12012-04-09 22:46:21 +0000177 {
178 }
179
180 void
181 Clear ()
182 {
183 packet.clear();
184 type = ePacketTypeInvalid;
185 bytes_transmitted = 0;
186 packet_idx = 0;
Greg Claytond451c1a2012-04-13 21:24:18 +0000187 tid = LLDB_INVALID_THREAD_ID;
Greg Claytonc1422c12012-04-09 22:46:21 +0000188 }
189 std::string packet;
190 PacketType type;
191 uint32_t bytes_transmitted;
192 uint32_t packet_idx;
Greg Claytond451c1a2012-04-13 21:24:18 +0000193 lldb::tid_t tid;
Greg Claytonc1422c12012-04-09 22:46:21 +0000194 };
195
196 History (uint32_t size);
197
198 ~History ();
199
200 // For single char packets for ack, nack and /x03
201 void
202 AddPacket (char packet_char,
203 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000204 uint32_t bytes_transmitted);
Greg Claytonc1422c12012-04-09 22:46:21 +0000205 void
206 AddPacket (const std::string &src,
207 uint32_t src_len,
208 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000209 uint32_t bytes_transmitted);
Greg Claytonc1422c12012-04-09 22:46:21 +0000210
211 void
212 Dump (lldb_private::Stream &strm) const;
213
214 void
215 Dump (lldb_private::Log *log) const;
216
217 bool
218 DidDumpToLog () const
219 {
220 return m_dumped_to_log;
221 }
222
223protected:
224 uint32_t
225 GetFirstSavedPacketIndex () const
226 {
227 if (m_total_packet_count < m_packets.size())
228 return 0;
229 else
230 return m_curr_idx + 1;
231 }
232
233 uint32_t
234 GetNumPacketsInHistory () const
235 {
236 if (m_total_packet_count < m_packets.size())
237 return m_total_packet_count;
238 else
239 return (uint32_t)m_packets.size();
240 }
241
242 uint32_t
243 GetNextIndex()
244 {
245 ++m_total_packet_count;
246 const uint32_t idx = m_curr_idx;
247 m_curr_idx = NormalizeIndex(idx + 1);
248 return idx;
249 }
250
251 uint32_t
252 NormalizeIndex (uint32_t i) const
253 {
254 return i % m_packets.size();
255 }
256
257
258 std::vector<Entry> m_packets;
259 uint32_t m_curr_idx;
260 uint32_t m_total_packet_count;
261 mutable bool m_dumped_to_log;
262 };
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263
Greg Clayton3dedae12013-12-06 21:45:27 +0000264 PacketResult
Greg Clayton37a0a242012-04-11 00:24:49 +0000265 SendPacket (const char *payload,
266 size_t payload_length);
267
Greg Clayton3dedae12013-12-06 21:45:27 +0000268 PacketResult
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269 SendPacketNoLock (const char *payload,
270 size_t payload_length);
271
Greg Clayton3dedae12013-12-06 21:45:27 +0000272 PacketResult
Greg Clayton73bf5db2011-06-17 01:22:15 +0000273 WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &response,
274 uint32_t timeout_usec);
Greg Clayton6779606a2011-01-22 23:43:18 +0000275
276 bool
277 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278
279 //------------------------------------------------------------------
280 // Classes that inherit from GDBRemoteCommunication can see and modify these
281 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000282 uint32_t m_packet_timeout;
Greg Claytonb09c5382013-12-13 17:20:18 +0000283#ifdef ENABLE_MUTEX_ERROR_CHECKING
Jim Ingham4ceb9282012-06-08 22:50:40 +0000284 lldb_private::TrackingMutex m_sequence_mutex;
285#else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286 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 +0000287#endif
Greg Clayton4dc72282011-01-20 07:53:45 +0000288 lldb_private::Predicate<bool> m_public_is_running;
289 lldb_private::Predicate<bool> m_private_is_running;
Greg Claytonc1422c12012-04-09 22:46:21 +0000290 History m_history;
Greg Clayton1cb64962011-03-24 04:28:38 +0000291 bool m_send_acks;
Greg Clayton8b82f082011-04-12 05:54:46 +0000292 bool m_is_platform; // Set to true if this class represents a platform,
293 // false if this class represents a debug session for
294 // a single process
Greg Clayton1cb64962011-03-24 04:28:38 +0000295
296
Greg Clayton00fe87b2013-12-05 22:58:22 +0000297 lldb_private::Error
Greg Clayton16810922014-02-27 19:38:18 +0000298 StartListenThread (const char *hostname = "127.0.0.1",
Greg Claytond6299802013-12-06 17:46:35 +0000299 uint16_t port = 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300
Greg Clayton00fe87b2013-12-05 22:58:22 +0000301 bool
302 JoinListenThread ();
303
304 static lldb::thread_result_t
305 ListenThread (lldb::thread_arg_t arg);
Greg Clayton8b82f082011-04-12 05:54:46 +0000306
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307private:
Vince Harron1b5a74e2015-01-21 22:42:49 +0000308 lldb_private::HostThread m_listen_thread;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000309 std::string m_listen_url;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000310
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311 //------------------------------------------------------------------
312 // For GDBRemoteCommunication only
313 //------------------------------------------------------------------
314 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
315};
316
317#endif // liblldb_GDBRemoteCommunication_h_