blob: 066b5bce2424dc1f673599a9c7eccff56644da95 [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
48 ErrorDisconnected // We were disconnected
49 };
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050 //------------------------------------------------------------------
51 // Constructors and Destructors
52 //------------------------------------------------------------------
Greg Clayton8b82f082011-04-12 05:54:46 +000053 GDBRemoteCommunication(const char *comm_name,
54 const char *listener_name,
55 bool is_platform);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056
57 virtual
58 ~GDBRemoteCommunication();
59
Greg Clayton3dedae12013-12-06 21:45:27 +000060 PacketResult
Greg Claytonc574ede2011-03-10 02:26:48 +000061 GetAck ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062
63 size_t
Greg Clayton6ed95942011-01-22 07:12:45 +000064 SendAck ();
65
66 size_t
67 SendNack ();
68
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069 char
70 CalculcateChecksum (const char *payload,
71 size_t payload_length);
72
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073 bool
Jim Ingham4ceb9282012-06-08 22:50:40 +000074 GetSequenceMutex (lldb_private::Mutex::Locker& locker, const char *failure_message = NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075
Greg Clayton73bf5db2011-06-17 01:22:15 +000076 bool
77 CheckForPacket (const uint8_t *src,
78 size_t src_len,
79 StringExtractorGDBRemote &packet);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080 bool
81 IsRunning() const
82 {
Greg Clayton4dc72282011-01-20 07:53:45 +000083 return m_public_is_running.GetValue();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084 }
Greg Clayton6779606a2011-01-22 23:43:18 +000085
Greg Clayton1cb64962011-03-24 04:28:38 +000086 bool
87 GetSendAcks ()
88 {
89 return m_send_acks;
90 }
91
Greg Clayton576d8832011-03-22 04:00:09 +000092 //------------------------------------------------------------------
93 // Client and server must implement these pure virtual functions
94 //------------------------------------------------------------------
95 virtual bool
96 GetThreadSuffixSupported () = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097
Greg Clayton576d8832011-03-22 04:00:09 +000098 //------------------------------------------------------------------
99 // Set the global packet timeout.
100 //
101 // For clients, this is the timeout that gets used when sending
102 // packets and waiting for responses. For servers, this might not
103 // get used, and if it doesn't this should be moved to the
104 // GDBRemoteCommunicationClient.
105 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000106 uint32_t
107 SetPacketTimeout (uint32_t packet_timeout)
108 {
109 const uint32_t old_packet_timeout = m_packet_timeout;
110 m_packet_timeout = packet_timeout;
111 return old_packet_timeout;
112 }
Greg Clayton71fc2a32011-02-12 06:28:37 +0000113
Greg Clayton73bf5db2011-06-17 01:22:15 +0000114 uint32_t
115 GetPacketTimeoutInMicroSeconds () const
116 {
Peter Collingbourneba23ca02011-06-18 23:52:14 +0000117 return m_packet_timeout * lldb_private::TimeValue::MicroSecPerSec;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000118 }
Greg Clayton8b82f082011-04-12 05:54:46 +0000119 //------------------------------------------------------------------
120 // Start a debugserver instance on the current host using the
121 // supplied connection URL.
122 //------------------------------------------------------------------
Greg Clayton00fe87b2013-12-05 22:58:22 +0000123 lldb_private::Error
Greg Claytonfda4fab2014-01-10 22:24:11 +0000124 StartDebugserverProcess (const char *hostname,
125 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 +0000126 lldb_private::ProcessLaunchInfo &launch_info,
Greg Claytonfda4fab2014-01-10 22:24:11 +0000127 uint16_t &out_port);
Greg Clayton8b82f082011-04-12 05:54:46 +0000128
Greg Claytonc1422c12012-04-09 22:46:21 +0000129 void
Greg Claytond451c1a2012-04-13 21:24:18 +0000130 DumpHistory(lldb_private::Stream &strm);
Greg Clayton73bf5db2011-06-17 01:22:15 +0000131
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132protected:
Greg Claytonc1422c12012-04-09 22:46:21 +0000133
134 class History
135 {
136 public:
137 enum PacketType
138 {
139 ePacketTypeInvalid = 0,
140 ePacketTypeSend,
141 ePacketTypeRecv
142 };
143
144 struct Entry
145 {
146 Entry() :
147 packet(),
148 type (ePacketTypeInvalid),
149 bytes_transmitted (0),
Greg Claytond451c1a2012-04-13 21:24:18 +0000150 packet_idx (0),
151 tid (LLDB_INVALID_THREAD_ID)
Greg Claytonc1422c12012-04-09 22:46:21 +0000152 {
153 }
154
155 void
156 Clear ()
157 {
158 packet.clear();
159 type = ePacketTypeInvalid;
160 bytes_transmitted = 0;
161 packet_idx = 0;
Greg Claytond451c1a2012-04-13 21:24:18 +0000162 tid = LLDB_INVALID_THREAD_ID;
Greg Claytonc1422c12012-04-09 22:46:21 +0000163 }
164 std::string packet;
165 PacketType type;
166 uint32_t bytes_transmitted;
167 uint32_t packet_idx;
Greg Claytond451c1a2012-04-13 21:24:18 +0000168 lldb::tid_t tid;
Greg Claytonc1422c12012-04-09 22:46:21 +0000169 };
170
171 History (uint32_t size);
172
173 ~History ();
174
175 // For single char packets for ack, nack and /x03
176 void
177 AddPacket (char packet_char,
178 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000179 uint32_t bytes_transmitted);
Greg Claytonc1422c12012-04-09 22:46:21 +0000180 void
181 AddPacket (const std::string &src,
182 uint32_t src_len,
183 PacketType type,
Greg Claytond451c1a2012-04-13 21:24:18 +0000184 uint32_t bytes_transmitted);
Greg Claytonc1422c12012-04-09 22:46:21 +0000185
186 void
187 Dump (lldb_private::Stream &strm) const;
188
189 void
190 Dump (lldb_private::Log *log) const;
191
192 bool
193 DidDumpToLog () const
194 {
195 return m_dumped_to_log;
196 }
197
198protected:
199 uint32_t
200 GetFirstSavedPacketIndex () const
201 {
202 if (m_total_packet_count < m_packets.size())
203 return 0;
204 else
205 return m_curr_idx + 1;
206 }
207
208 uint32_t
209 GetNumPacketsInHistory () const
210 {
211 if (m_total_packet_count < m_packets.size())
212 return m_total_packet_count;
213 else
214 return (uint32_t)m_packets.size();
215 }
216
217 uint32_t
218 GetNextIndex()
219 {
220 ++m_total_packet_count;
221 const uint32_t idx = m_curr_idx;
222 m_curr_idx = NormalizeIndex(idx + 1);
223 return idx;
224 }
225
226 uint32_t
227 NormalizeIndex (uint32_t i) const
228 {
229 return i % m_packets.size();
230 }
231
232
233 std::vector<Entry> m_packets;
234 uint32_t m_curr_idx;
235 uint32_t m_total_packet_count;
236 mutable bool m_dumped_to_log;
237 };
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238
Greg Clayton3dedae12013-12-06 21:45:27 +0000239 PacketResult
Greg Clayton37a0a242012-04-11 00:24:49 +0000240 SendPacket (const char *payload,
241 size_t payload_length);
242
Greg Clayton3dedae12013-12-06 21:45:27 +0000243 PacketResult
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000244 SendPacketNoLock (const char *payload,
245 size_t payload_length);
246
Greg Clayton3dedae12013-12-06 21:45:27 +0000247 PacketResult
Greg Clayton73bf5db2011-06-17 01:22:15 +0000248 WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &response,
249 uint32_t timeout_usec);
Greg Clayton6779606a2011-01-22 23:43:18 +0000250
251 bool
252 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253
254 //------------------------------------------------------------------
255 // Classes that inherit from GDBRemoteCommunication can see and modify these
256 //------------------------------------------------------------------
Greg Claytonc574ede2011-03-10 02:26:48 +0000257 uint32_t m_packet_timeout;
Greg Claytonb09c5382013-12-13 17:20:18 +0000258#ifdef ENABLE_MUTEX_ERROR_CHECKING
Jim Ingham4ceb9282012-06-08 22:50:40 +0000259 lldb_private::TrackingMutex m_sequence_mutex;
260#else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261 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 +0000262#endif
Greg Clayton4dc72282011-01-20 07:53:45 +0000263 lldb_private::Predicate<bool> m_public_is_running;
264 lldb_private::Predicate<bool> m_private_is_running;
Greg Claytonc1422c12012-04-09 22:46:21 +0000265 History m_history;
Greg Clayton1cb64962011-03-24 04:28:38 +0000266 bool m_send_acks;
Greg Clayton8b82f082011-04-12 05:54:46 +0000267 bool m_is_platform; // Set to true if this class represents a platform,
268 // false if this class represents a debug session for
269 // a single process
Greg Clayton1cb64962011-03-24 04:28:38 +0000270
271
Greg Clayton00fe87b2013-12-05 22:58:22 +0000272 lldb_private::Error
273 StartListenThread (const char *hostname = "localhost",
Greg Claytond6299802013-12-06 17:46:35 +0000274 uint16_t port = 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275
Greg Clayton00fe87b2013-12-05 22:58:22 +0000276 bool
277 JoinListenThread ();
278
279 static lldb::thread_result_t
280 ListenThread (lldb::thread_arg_t arg);
Greg Clayton8b82f082011-04-12 05:54:46 +0000281
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282private:
Greg Clayton00fe87b2013-12-05 22:58:22 +0000283
284 lldb::thread_t m_listen_thread;
285 std::string m_listen_url;
286
287
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288 //------------------------------------------------------------------
289 // For GDBRemoteCommunication only
290 //------------------------------------------------------------------
291 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
292};
293
294#endif // liblldb_GDBRemoteCommunication_h_