blob: ac203a62788a10b9d1306e899ffa3cf8040a592c [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
30class ProcessGDBRemote;
31
Greg Clayton576d8832011-03-22 04:00:09 +000032class GDBRemoteCommunication : public lldb_private::Communication
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033{
34public:
Greg Claytone5219662010-12-03 06:02:24 +000035 enum
36 {
37 eBroadcastBitRunPacketSent = kLoUserBroadcastBit
38 };
Greg Clayton3dedae12013-12-06 21:45:27 +000039
40 enum class PacketResult
41 {
42 Success = 0, // Success
43 ErrorSendFailed, // Error sending the packet
44 ErrorSendAck, // Didn't get an ack back after sending a packet
45 ErrorReplyFailed, // Error getting the reply
46 ErrorReplyTimeout, // Timed out waiting for reply
47 ErrorReplyInvalid, // Got a reply but it wasn't valid for the packet that was sent
48 ErrorReplyAck, // Sending reply ack failed
Steve Pucci5ae54ae2014-01-25 05:46:51 +000049 ErrorDisconnected, // We were disconnected
50 ErrorNoSequenceLock // We couldn't get the sequence lock for a multi-packet request
Greg Clayton3dedae12013-12-06 21:45:27 +000051 };
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052 //------------------------------------------------------------------
53 // Constructors and Destructors
54 //------------------------------------------------------------------
Greg Clayton8b82f082011-04-12 05:54:46 +000055 GDBRemoteCommunication(const char *comm_name,
56 const char *listener_name,
57 bool is_platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058
59 virtual
60 ~GDBRemoteCommunication();
61
Greg Clayton3dedae12013-12-06 21:45:27 +000062 PacketResult
Greg Claytonc574ede2011-03-10 02:26:48 +000063 GetAck ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064
65 size_t
Greg Clayton6ed95942011-01-22 07:12:45 +000066 SendAck ();
67
68 size_t
69 SendNack ();
70
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071 char
72 CalculcateChecksum (const char *payload,
73 size_t payload_length);
74
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075 bool
Jim Ingham4ceb9282012-06-08 22:50:40 +000076 GetSequenceMutex (lldb_private::Mutex::Locker& locker, const char *failure_message = NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077
Greg Clayton73bf5db2011-06-17 01:22:15 +000078 bool
79 CheckForPacket (const uint8_t *src,
80 size_t src_len,
81 StringExtractorGDBRemote &packet);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082 bool
83 IsRunning() const
84 {
Greg Clayton4dc72282011-01-20 07:53:45 +000085 return m_public_is_running.GetValue();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086 }
Greg Clayton6779606a2011-01-22 23:43:18 +000087
Greg Clayton1cb64962011-03-24 04:28:38 +000088 bool
89 GetSendAcks ()
90 {
91 return m_send_acks;
92 }
93
Greg Clayton576d8832011-03-22 04:00:09 +000094 //------------------------------------------------------------------
95 // Client and server must implement these pure virtual functions
96 //------------------------------------------------------------------
97 virtual bool
98 GetThreadSuffixSupported () = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099
Greg Clayton576d8832011-03-22 04:00:09 +0000100 //------------------------------------------------------------------
101 // Set the global packet timeout.
102 //
103 // For clients, this is the timeout that gets used when sending
104 // packets and waiting for responses. For servers, this might not
105 // get used, and if it doesn't this should be moved to the
106 // GDBRemoteCommunicationClient.
107 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000108 uint32_t
109 SetPacketTimeout (uint32_t packet_timeout)
110 {
111 const uint32_t old_packet_timeout = m_packet_timeout;
112 m_packet_timeout = packet_timeout;
113 return old_packet_timeout;
114 }
Greg Clayton71fc2a32011-02-12 06:28:37 +0000115
Greg Clayton73bf5db2011-06-17 01:22:15 +0000116 uint32_t
117 GetPacketTimeoutInMicroSeconds () const
118 {
Peter Collingbourneba23ca02011-06-18 23:52:14 +0000119 return m_packet_timeout * lldb_private::TimeValue::MicroSecPerSec;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000120 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000121 //------------------------------------------------------------------
122 // Start a debugserver instance on the current host using the
123 // supplied connection URL.
124 //------------------------------------------------------------------
Greg Clayton00fe87b2013-12-05 22:58:22 +0000125 lldb_private::Error
Greg Claytonfda4fab2014-01-10 22:24:11 +0000126 StartDebugserverProcess (const char *hostname,
127 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 +0000128 lldb_private::ProcessLaunchInfo &launch_info,
Greg Claytonfda4fab2014-01-10 22:24:11 +0000129 uint16_t &out_port);
Greg Clayton8b82f082011-04-12 05:54:46 +0000130
Greg Claytonc1422c12012-04-09 22:46:21 +0000131 void
Greg Claytond451c1a2012-04-13 21:24:18 +0000132 DumpHistory(lldb_private::Stream &strm);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000133
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134protected:
Greg Claytonc1422c12012-04-09 22:46:21 +0000135
136 class History
137 {
138 public:
139 enum PacketType
140 {
141 ePacketTypeInvalid = 0,
142 ePacketTypeSend,
143 ePacketTypeRecv
144 };
145
146 struct Entry
147 {
148 Entry() :
149 packet(),
150 type (ePacketTypeInvalid),
151 bytes_transmitted (0),
Greg Claytond451c1a2012-04-13 21:24:18 +0000152 packet_idx (0),
153 tid (LLDB_INVALID_THREAD_ID)
Greg Claytonc1422c12012-04-09 22:46:21 +0000154 {
155 }
156
157 void
158 Clear ()
159 {
160 packet.clear();
161 type = ePacketTypeInvalid;
162 bytes_transmitted = 0;
163 packet_idx = 0;
Greg Claytond451c1a2012-04-13 21:24:18 +0000164 tid = LLDB_INVALID_THREAD_ID;
Greg Claytonc1422c12012-04-09 22:46:21 +0000165 }
166 std::string packet;
167 PacketType type;
168 uint32_t bytes_transmitted;
169 uint32_t packet_idx;
Greg Claytond451c1a2012-04-13 21:24:18 +0000170 lldb::tid_t tid;
Greg Claytonc1422c12012-04-09 22:46:21 +0000171 };
172
173 History (uint32_t size);
174
175 ~History ();
176
177 // For single char packets for ack, nack and /x03
178 void
179 AddPacket (char packet_char,
180 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000181 uint32_t bytes_transmitted);
Greg Claytonc1422c12012-04-09 22:46:21 +0000182 void
183 AddPacket (const std::string &src,
184 uint32_t src_len,
185 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000186 uint32_t bytes_transmitted);
Greg Claytonc1422c12012-04-09 22:46:21 +0000187
188 void
189 Dump (lldb_private::Stream &strm) const;
190
191 void
192 Dump (lldb_private::Log *log) const;
193
194 bool
195 DidDumpToLog () const
196 {
197 return m_dumped_to_log;
198 }
199
200protected:
201 uint32_t
202 GetFirstSavedPacketIndex () const
203 {
204 if (m_total_packet_count < m_packets.size())
205 return 0;
206 else
207 return m_curr_idx + 1;
208 }
209
210 uint32_t
211 GetNumPacketsInHistory () const
212 {
213 if (m_total_packet_count < m_packets.size())
214 return m_total_packet_count;
215 else
216 return (uint32_t)m_packets.size();
217 }
218
219 uint32_t
220 GetNextIndex()
221 {
222 ++m_total_packet_count;
223 const uint32_t idx = m_curr_idx;
224 m_curr_idx = NormalizeIndex(idx + 1);
225 return idx;
226 }
227
228 uint32_t
229 NormalizeIndex (uint32_t i) const
230 {
231 return i % m_packets.size();
232 }
233
234
235 std::vector<Entry> m_packets;
236 uint32_t m_curr_idx;
237 uint32_t m_total_packet_count;
238 mutable bool m_dumped_to_log;
239 };
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240
Greg Clayton3dedae12013-12-06 21:45:27 +0000241 PacketResult
Greg Clayton37a0a242012-04-11 00:24:49 +0000242 SendPacket (const char *payload,
243 size_t payload_length);
244
Greg Clayton3dedae12013-12-06 21:45:27 +0000245 PacketResult
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246 SendPacketNoLock (const char *payload,
247 size_t payload_length);
248
Greg Clayton3dedae12013-12-06 21:45:27 +0000249 PacketResult
Greg Clayton73bf5db2011-06-17 01:22:15 +0000250 WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &response,
251 uint32_t timeout_usec);
Greg Clayton6779606a2011-01-22 23:43:18 +0000252
253 bool
254 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255
256 //------------------------------------------------------------------
257 // Classes that inherit from GDBRemoteCommunication can see and modify these
258 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000259 uint32_t m_packet_timeout;
Greg Claytonb09c5382013-12-13 17:20:18 +0000260#ifdef ENABLE_MUTEX_ERROR_CHECKING
Jim Ingham4ceb9282012-06-08 22:50:40 +0000261 lldb_private::TrackingMutex m_sequence_mutex;
262#else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263 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 +0000264#endif
Greg Clayton4dc72282011-01-20 07:53:45 +0000265 lldb_private::Predicate<bool> m_public_is_running;
266 lldb_private::Predicate<bool> m_private_is_running;
Greg Claytonc1422c12012-04-09 22:46:21 +0000267 History m_history;
Greg Clayton1cb64962011-03-24 04:28:38 +0000268 bool m_send_acks;
Greg Clayton8b82f082011-04-12 05:54:46 +0000269 bool m_is_platform; // Set to true if this class represents a platform,
270 // false if this class represents a debug session for
271 // a single process
Greg Clayton1cb64962011-03-24 04:28:38 +0000272
273
Greg Clayton00fe87b2013-12-05 22:58:22 +0000274 lldb_private::Error
Greg Clayton16810922014-02-27 19:38:18 +0000275 StartListenThread (const char *hostname = "127.0.0.1",
Greg Claytond6299802013-12-06 17:46:35 +0000276 uint16_t port = 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277
Greg Clayton00fe87b2013-12-05 22:58:22 +0000278 bool
279 JoinListenThread ();
280
281 static lldb::thread_result_t
282 ListenThread (lldb::thread_arg_t arg);
Greg Clayton8b82f082011-04-12 05:54:46 +0000283
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000284private:
Zachary Turner39de3112014-09-09 20:54:56 +0000285 lldb_private::HostThread m_listen_thread;
Greg Clayton00fe87b2013-12-05 22:58:22 +0000286 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_