blob: b11d38563207c246f914a3820b289592674c3739 [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"
23#include "lldb/Host/Mutex.h"
24#include "lldb/Host/Predicate.h"
Peter Collingbourneba23ca02011-06-18 23:52:14 +000025#include "lldb/Host/TimeValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Greg Claytonc982c762010-07-09 20:39:50 +000027#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028
29class ProcessGDBRemote;
30
Greg Clayton576d8832011-03-22 04:00:09 +000031class GDBRemoteCommunication : public lldb_private::Communication
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032{
33public:
Greg Claytone5219662010-12-03 06:02:24 +000034 enum
35 {
36 eBroadcastBitRunPacketSent = kLoUserBroadcastBit
37 };
Greg Clayton3dedae12013-12-06 21:45:27 +000038
39 enum class PacketResult
40 {
41 Success = 0, // Success
42 ErrorSendFailed, // Error sending the packet
43 ErrorSendAck, // Didn't get an ack back after sending a packet
44 ErrorReplyFailed, // Error getting the reply
45 ErrorReplyTimeout, // Timed out waiting for reply
46 ErrorReplyInvalid, // Got a reply but it wasn't valid for the packet that was sent
47 ErrorReplyAck, // Sending reply ack failed
Steve Pucci5ae54ae2014-01-25 05:46:51 +000048 ErrorDisconnected, // We were disconnected
49 ErrorNoSequenceLock // We couldn't get the sequence lock for a multi-packet request
Greg Clayton3dedae12013-12-06 21:45:27 +000050 };
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051 //------------------------------------------------------------------
52 // Constructors and Destructors
53 //------------------------------------------------------------------
Greg Clayton8b82f082011-04-12 05:54:46 +000054 GDBRemoteCommunication(const char *comm_name,
55 const char *listener_name,
56 bool is_platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057
58 virtual
59 ~GDBRemoteCommunication();
60
Greg Clayton3dedae12013-12-06 21:45:27 +000061 PacketResult
Greg Claytonc574ede2011-03-10 02:26:48 +000062 GetAck ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063
64 size_t
Greg Clayton6ed95942011-01-22 07:12:45 +000065 SendAck ();
66
67 size_t
68 SendNack ();
69
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070 char
71 CalculcateChecksum (const char *payload,
72 size_t payload_length);
73
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074 bool
Jim Ingham4ceb9282012-06-08 22:50:40 +000075 GetSequenceMutex (lldb_private::Mutex::Locker& locker, const char *failure_message = NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000076
Greg Clayton73bf5db2011-06-17 01:22:15 +000077 bool
78 CheckForPacket (const uint8_t *src,
79 size_t src_len,
80 StringExtractorGDBRemote &packet);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081 bool
82 IsRunning() const
83 {
Greg Clayton4dc72282011-01-20 07:53:45 +000084 return m_public_is_running.GetValue();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085 }
Greg Clayton6779606a2011-01-22 23:43:18 +000086
Greg Clayton1cb64962011-03-24 04:28:38 +000087 bool
88 GetSendAcks ()
89 {
90 return m_send_acks;
91 }
92
Greg Clayton576d8832011-03-22 04:00:09 +000093 //------------------------------------------------------------------
94 // Client and server must implement these pure virtual functions
95 //------------------------------------------------------------------
96 virtual bool
97 GetThreadSuffixSupported () = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098
Greg Clayton576d8832011-03-22 04:00:09 +000099 //------------------------------------------------------------------
100 // Set the global packet timeout.
101 //
102 // For clients, this is the timeout that gets used when sending
103 // packets and waiting for responses. For servers, this might not
104 // get used, and if it doesn't this should be moved to the
105 // GDBRemoteCommunicationClient.
106 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000107 uint32_t
108 SetPacketTimeout (uint32_t packet_timeout)
109 {
110 const uint32_t old_packet_timeout = m_packet_timeout;
111 m_packet_timeout = packet_timeout;
112 return old_packet_timeout;
113 }
Greg Clayton71fc2a32011-02-12 06:28:37 +0000114
Greg Clayton73bf5db2011-06-17 01:22:15 +0000115 uint32_t
116 GetPacketTimeoutInMicroSeconds () const
117 {
Peter Collingbourneba23ca02011-06-18 23:52:14 +0000118 return m_packet_timeout * lldb_private::TimeValue::MicroSecPerSec;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000119 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000120 //------------------------------------------------------------------
121 // Start a debugserver instance on the current host using the
122 // supplied connection URL.
123 //------------------------------------------------------------------
Greg Clayton00fe87b2013-12-05 22:58:22 +0000124 lldb_private::Error
Greg Claytonfda4fab2014-01-10 22:24:11 +0000125 StartDebugserverProcess (const char *hostname,
126 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 +0000127 lldb_private::ProcessLaunchInfo &launch_info,
Greg Claytonfda4fab2014-01-10 22:24:11 +0000128 uint16_t &out_port);
Greg Clayton8b82f082011-04-12 05:54:46 +0000129
Greg Claytonc1422c12012-04-09 22:46:21 +0000130 void
Greg Claytond451c1a2012-04-13 21:24:18 +0000131 DumpHistory(lldb_private::Stream &strm);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000132
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133protected:
Greg Claytonc1422c12012-04-09 22:46:21 +0000134
135 class History
136 {
137 public:
138 enum PacketType
139 {
140 ePacketTypeInvalid = 0,
141 ePacketTypeSend,
142 ePacketTypeRecv
143 };
144
145 struct Entry
146 {
147 Entry() :
148 packet(),
149 type (ePacketTypeInvalid),
150 bytes_transmitted (0),
Greg Claytond451c1a2012-04-13 21:24:18 +0000151 packet_idx (0),
152 tid (LLDB_INVALID_THREAD_ID)
Greg Claytonc1422c12012-04-09 22:46:21 +0000153 {
154 }
155
156 void
157 Clear ()
158 {
159 packet.clear();
160 type = ePacketTypeInvalid;
161 bytes_transmitted = 0;
162 packet_idx = 0;
Greg Claytond451c1a2012-04-13 21:24:18 +0000163 tid = LLDB_INVALID_THREAD_ID;
Greg Claytonc1422c12012-04-09 22:46:21 +0000164 }
165 std::string packet;
166 PacketType type;
167 uint32_t bytes_transmitted;
168 uint32_t packet_idx;
Greg Claytond451c1a2012-04-13 21:24:18 +0000169 lldb::tid_t tid;
Greg Claytonc1422c12012-04-09 22:46:21 +0000170 };
171
172 History (uint32_t size);
173
174 ~History ();
175
176 // For single char packets for ack, nack and /x03
177 void
178 AddPacket (char packet_char,
179 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000180 uint32_t bytes_transmitted);
Greg Claytonc1422c12012-04-09 22:46:21 +0000181 void
182 AddPacket (const std::string &src,
183 uint32_t src_len,
184 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000185 uint32_t bytes_transmitted);
Greg Claytonc1422c12012-04-09 22:46:21 +0000186
187 void
188 Dump (lldb_private::Stream &strm) const;
189
190 void
191 Dump (lldb_private::Log *log) const;
192
193 bool
194 DidDumpToLog () const
195 {
196 return m_dumped_to_log;
197 }
198
199protected:
200 uint32_t
201 GetFirstSavedPacketIndex () const
202 {
203 if (m_total_packet_count < m_packets.size())
204 return 0;
205 else
206 return m_curr_idx + 1;
207 }
208
209 uint32_t
210 GetNumPacketsInHistory () const
211 {
212 if (m_total_packet_count < m_packets.size())
213 return m_total_packet_count;
214 else
215 return (uint32_t)m_packets.size();
216 }
217
218 uint32_t
219 GetNextIndex()
220 {
221 ++m_total_packet_count;
222 const uint32_t idx = m_curr_idx;
223 m_curr_idx = NormalizeIndex(idx + 1);
224 return idx;
225 }
226
227 uint32_t
228 NormalizeIndex (uint32_t i) const
229 {
230 return i % m_packets.size();
231 }
232
233
234 std::vector<Entry> m_packets;
235 uint32_t m_curr_idx;
236 uint32_t m_total_packet_count;
237 mutable bool m_dumped_to_log;
238 };
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239
Greg Clayton3dedae12013-12-06 21:45:27 +0000240 PacketResult
Greg Clayton37a0a242012-04-11 00:24:49 +0000241 SendPacket (const char *payload,
242 size_t payload_length);
243
Greg Clayton3dedae12013-12-06 21:45:27 +0000244 PacketResult
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245 SendPacketNoLock (const char *payload,
246 size_t payload_length);
247
Greg Clayton3dedae12013-12-06 21:45:27 +0000248 PacketResult
Greg Clayton73bf5db2011-06-17 01:22:15 +0000249 WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &response,
250 uint32_t timeout_usec);
Greg Clayton6779606a2011-01-22 23:43:18 +0000251
252 bool
253 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254
255 //------------------------------------------------------------------
256 // Classes that inherit from GDBRemoteCommunication can see and modify these
257 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000258 uint32_t m_packet_timeout;
Greg Claytonb09c5382013-12-13 17:20:18 +0000259#ifdef ENABLE_MUTEX_ERROR_CHECKING
Jim Ingham4ceb9282012-06-08 22:50:40 +0000260 lldb_private::TrackingMutex m_sequence_mutex;
261#else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262 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 +0000263#endif
Greg Clayton4dc72282011-01-20 07:53:45 +0000264 lldb_private::Predicate<bool> m_public_is_running;
265 lldb_private::Predicate<bool> m_private_is_running;
Greg Claytonc1422c12012-04-09 22:46:21 +0000266 History m_history;
Greg Clayton1cb64962011-03-24 04:28:38 +0000267 bool m_send_acks;
Greg Clayton8b82f082011-04-12 05:54:46 +0000268 bool m_is_platform; // Set to true if this class represents a platform,
269 // false if this class represents a debug session for
270 // a single process
Greg Clayton1cb64962011-03-24 04:28:38 +0000271
272
Greg Clayton00fe87b2013-12-05 22:58:22 +0000273 lldb_private::Error
Greg Clayton16810922014-02-27 19:38:18 +0000274 StartListenThread (const char *hostname = "127.0.0.1",
Greg Claytond6299802013-12-06 17:46:35 +0000275 uint16_t port = 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276
Greg Clayton00fe87b2013-12-05 22:58:22 +0000277 bool
278 JoinListenThread ();
279
280 static lldb::thread_result_t
281 ListenThread (lldb::thread_arg_t arg);
Greg Clayton8b82f082011-04-12 05:54:46 +0000282
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283private:
Greg Clayton00fe87b2013-12-05 22:58:22 +0000284
285 lldb::thread_t m_listen_thread;
286 std::string m_listen_url;
287
288
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289 //------------------------------------------------------------------
290 // For GDBRemoteCommunication only
291 //------------------------------------------------------------------
292 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
293};
294
295#endif // liblldb_GDBRemoteCommunication_h_