blob: 1499ba90443a607b44394a8f682a2bc19ec6b133 [file] [log] [blame]
Chris Lattner24943d22010-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
20#include "lldb/lldb-private.h"
21#include "lldb/Core/ArchSpec.h"
22#include "lldb/Core/Communication.h"
23#include "lldb/Core/ConstString.h"
24#include "lldb/Core/Error.h"
25#include "lldb/Core/Listener.h"
26#include "lldb/Host/Mutex.h"
27#include "lldb/Host/Predicate.h"
28
Greg Clayton54e7afa2010-07-09 20:39:50 +000029#include "Utility/StringExtractorGDBRemote.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030
31class ProcessGDBRemote;
32
33class GDBRemoteCommunication :
34 public lldb_private::Communication
35{
36public:
Greg Claytonb749a262010-12-03 06:02:24 +000037 enum
38 {
39 eBroadcastBitRunPacketSent = kLoUserBroadcastBit
40 };
Chris Lattner24943d22010-06-08 16:52:24 +000041 //------------------------------------------------------------------
42 // Constructors and Destructors
43 //------------------------------------------------------------------
44 GDBRemoteCommunication();
45
46 virtual
47 ~GDBRemoteCommunication();
48
49 size_t
50 SendPacket (const char *payload);
51
52 size_t
53 SendPacket (const char *payload,
54 size_t payload_length);
55
56 size_t
57 SendPacketAndWaitForResponse (const char *send_payload,
58 StringExtractorGDBRemote &response,
59 uint32_t timeout_seconds,
60 bool send_async);
61
62 size_t
63 SendPacketAndWaitForResponse (const char *send_payload,
64 size_t send_length,
65 StringExtractorGDBRemote &response,
66 uint32_t timeout_seconds,
67 bool send_async);
68
69 lldb::StateType
70 SendContinuePacketAndWaitForResponse (ProcessGDBRemote *process,
71 const char *packet_payload,
72 size_t packet_length,
73 StringExtractorGDBRemote &response);
74
75 // Wait for a packet within 'nsec' seconds
76 size_t
77 WaitForPacket (StringExtractorGDBRemote &response,
78 uint32_t nsec);
79
80 // Wait for a packet with an absolute timeout time. If 'timeout' is NULL
81 // wait indefinitely.
82 size_t
83 WaitForPacket (StringExtractorGDBRemote &response,
Greg Clayton72e1c782011-01-22 23:43:18 +000084 const lldb_private::TimeValue* timeout);
Chris Lattner24943d22010-06-08 16:52:24 +000085
86 char
87 GetAck (uint32_t timeout_seconds);
88
89 size_t
Greg Claytona4881d02011-01-22 07:12:45 +000090 SendAck ();
91
92 size_t
93 SendNack ();
94
Chris Lattner24943d22010-06-08 16:52:24 +000095
96 char
97 CalculcateChecksum (const char *payload,
98 size_t payload_length);
99
Chris Lattner24943d22010-06-08 16:52:24 +0000100 bool
Greg Claytonc1f45872011-02-12 06:28:37 +0000101 GetThreadSuffixSupported ();
Greg Claytonc71899e2011-01-18 19:36:39 +0000102
103 bool
Chris Lattner24943d22010-06-08 16:52:24 +0000104 SendAsyncSignal (int signo);
105
106 bool
Greg Clayton1a679462010-09-03 19:15:43 +0000107 SendInterrupt (lldb_private::Mutex::Locker &locker,
108 uint32_t seconds_to_wait_for_stop,
Greg Claytona4881d02011-01-22 07:12:45 +0000109 bool &sent_interrupt,
110 bool &timed_out);
Chris Lattner24943d22010-06-08 16:52:24 +0000111
112 bool
113 GetSequenceMutex(lldb_private::Mutex::Locker& locker);
114
115 //------------------------------------------------------------------
116 // Communication overrides
117 //------------------------------------------------------------------
118 virtual void
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000119 AppendBytesToCache (const uint8_t *src, size_t src_len, bool broadcast, lldb::ConnectionStatus status);
Chris Lattner24943d22010-06-08 16:52:24 +0000120
121
122 lldb::pid_t
123 GetCurrentProcessID (uint32_t timeout_seconds);
124
125 bool
126 GetLaunchSuccess (uint32_t timeout_seconds, std::string &error_str);
127
128 //------------------------------------------------------------------
129 /// Sends a GDB remote protocol 'A' packet that delivers program
130 /// arguments to the remote server.
131 ///
132 /// @param[in] argv
133 /// A NULL terminated array of const C strings to use as the
134 /// arguments.
135 ///
136 /// @param[in] timeout_seconds
137 /// The number of seconds to wait for a response from the remote
138 /// server.
139 ///
140 /// @return
141 /// Zero if the response was "OK", a positive value if the
142 /// the response was "Exx" where xx are two hex digits, or
143 /// -1 if the call is unsupported or any other unexpected
144 /// response was received.
145 //------------------------------------------------------------------
146 int
147 SendArgumentsPacket (char const *argv[], uint32_t timeout_seconds);
148
149 //------------------------------------------------------------------
150 /// Sends a "QEnvironment:NAME=VALUE" packet that will build up the
151 /// environment that will get used when launching an application
152 /// in conjunction with the 'A' packet. This function can be called
153 /// multiple times in a row in order to pass on the desired
154 /// environment that the inferior should be launched with.
155 ///
156 /// @param[in] name_equal_value
Greg Clayton5d187e52011-01-08 20:28:42 +0000157 /// A NULL terminated C string that contains a single environment
Chris Lattner24943d22010-06-08 16:52:24 +0000158 /// in the format "NAME=VALUE".
159 ///
160 /// @param[in] timeout_seconds
161 /// The number of seconds to wait for a response from the remote
162 /// server.
163 ///
164 /// @return
165 /// Zero if the response was "OK", a positive value if the
166 /// the response was "Exx" where xx are two hex digits, or
167 /// -1 if the call is unsupported or any other unexpected
168 /// response was received.
169 //------------------------------------------------------------------
170 int
171 SendEnvironmentPacket (char const *name_equal_value,
172 uint32_t timeout_seconds);
173
174 //------------------------------------------------------------------
175 /// Sends a "vAttach:PID" where PID is in hex.
176 ///
177 /// @param[in] pid
178 /// A process ID for the remote gdb server to attach to.
179 ///
180 /// @param[in] timeout_seconds
181 /// The number of seconds to wait for a response from the remote
182 /// server.
183 ///
184 /// @param[out] response
185 /// The response received from the gdb server. If the return
186 /// value is zero, \a response will contain a stop reply
187 /// packet.
188 ///
189 /// @return
190 /// Zero if the attach was successful, or an error indicating
191 /// an error code.
192 //------------------------------------------------------------------
193 int
194 SendAttach (lldb::pid_t pid,
195 uint32_t timeout_seconds,
196 StringExtractorGDBRemote& response);
197
198
199 lldb::addr_t
200 AllocateMemory (size_t size, uint32_t permissions, uint32_t timeout_seconds);
201
202 bool
203 DeallocateMemory (lldb::addr_t addr, uint32_t timeout_seconds);
204
205 bool
206 IsRunning() const
207 {
Greg Claytoncecf3482011-01-20 07:53:45 +0000208 return m_public_is_running.GetValue();
Chris Lattner24943d22010-06-08 16:52:24 +0000209 }
Greg Clayton72e1c782011-01-22 23:43:18 +0000210
211 bool
212 WaitForNotRunning (const lldb_private::TimeValue *timeout_ptr);
213
Chris Lattner24943d22010-06-08 16:52:24 +0000214 bool
Chris Lattner24943d22010-06-08 16:52:24 +0000215 GetHostInfo (uint32_t timeout_seconds);
216
Chris Lattner24943d22010-06-08 16:52:24 +0000217 const lldb_private::ArchSpec &
218 GetHostArchitecture ();
219
220 const lldb_private::ConstString &
221 GetOSString ();
222
223 const lldb_private::ConstString &
224 GetVendorString();
225
226 lldb::ByteOrder
227 GetByteOrder ();
228
229 uint32_t
230 GetAddressByteSize ();
231
Greg Claytonc1f45872011-02-12 06:28:37 +0000232 bool
233 GetVContSupported (char flavor);
234
235 void
236 ResetDiscoverableSettings();
237
238 bool
239 GetHostInfo ();
240
241 bool
242 GetSendAcks ();
243
244 bool
245 GetSupportsThreadSuffix ();
246
247 bool
248 HasFullVContSupport ()
249 {
250 return GetVContSupported ('A');
251 }
252
253 bool
254 HasAnyVContSupport ()
255 {
256 return GetVContSupported ('a');
257 }
258
Chris Lattner24943d22010-06-08 16:52:24 +0000259protected:
260 typedef std::list<std::string> packet_collection;
261
262 size_t
263 SendPacketNoLock (const char *payload,
264 size_t payload_length);
265
266 size_t
267 WaitForPacketNoLock (StringExtractorGDBRemote &response,
Greg Clayton72e1c782011-01-22 23:43:18 +0000268 const lldb_private::TimeValue* timeout_ptr);
269
270 bool
271 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000272
Greg Claytonc1f45872011-02-12 06:28:37 +0000273 bool
274 HostInfoIsValid () const
275 {
276 return m_supports_qHostInfo != lldb::eLazyBoolCalculate;
277 }
278
Chris Lattner24943d22010-06-08 16:52:24 +0000279 //------------------------------------------------------------------
280 // Classes that inherit from GDBRemoteCommunication can see and modify these
281 //------------------------------------------------------------------
Greg Claytonc1f45872011-02-12 06:28:37 +0000282 lldb::LazyBool m_supports_not_sending_acks;
283 lldb::LazyBool m_supports_thread_suffix;
284 lldb::LazyBool m_supports_qHostInfo;
285 lldb::LazyBool m_supports_vCont_all;
286 lldb::LazyBool m_supports_vCont_any;
287 lldb::LazyBool m_supports_vCont_c;
288 lldb::LazyBool m_supports_vCont_C;
289 lldb::LazyBool m_supports_vCont_s;
290 lldb::LazyBool m_supports_vCont_S;
Chris Lattner24943d22010-06-08 16:52:24 +0000291 lldb_private::Listener m_rx_packet_listener;
292 lldb_private::Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time
Greg Claytoncecf3482011-01-20 07:53:45 +0000293 lldb_private::Predicate<bool> m_public_is_running;
294 lldb_private::Predicate<bool> m_private_is_running;
Chris Lattner24943d22010-06-08 16:52:24 +0000295
296 // If we need to send a packet while the target is running, the m_async_XXX
297 // member variables take care of making this happen.
298 lldb_private::Mutex m_async_mutex;
299 lldb_private::Predicate<bool> m_async_packet_predicate;
300 std::string m_async_packet;
301 StringExtractorGDBRemote m_async_response;
302 uint32_t m_async_timeout;
303 int m_async_signal; // We were asked to deliver a signal to the inferior process.
304
305 lldb_private::ArchSpec m_arch; // Results from the qHostInfo call
306 uint32_t m_cpusubtype; // Results from the qHostInfo call
307 lldb_private::ConstString m_os; // Results from the qHostInfo call
308 lldb_private::ConstString m_vendor; // Results from the qHostInfo call
309 lldb::ByteOrder m_byte_order; // Results from the qHostInfo call
310 uint32_t m_pointer_byte_size; // Results from the qHostInfo call
311
312
313private:
314 //------------------------------------------------------------------
315 // For GDBRemoteCommunication only
316 //------------------------------------------------------------------
317 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
318};
319
320#endif // liblldb_GDBRemoteCommunication_h_