blob: 8d24f2ad4423519643097833b604cba99460fbaa [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
Greg Claytona2f74232011-02-24 22:24:29 +0000199 //------------------------------------------------------------------
200 /// Sets the path to use for stdin/out/err for a process
201 /// that will be launched with the 'A' packet.
202 ///
203 /// @param[in] path
204 /// The path to use for stdin/out/err
205 ///
206 /// @return
207 /// Zero if the for success, or an error code for failure.
208 //------------------------------------------------------------------
209 int
210 SetSTDIN (char const *path);
211 int
212 SetSTDOUT (char const *path);
213 int
214 SetSTDERR (char const *path);
215
216 //------------------------------------------------------------------
217 /// Sets the disable ASLR flag to \a enable for a process that will
218 /// be launched with the 'A' packet.
219 ///
220 /// @param[in] enable
221 /// A boolean value indicating wether to disable ASLR or not.
222 ///
223 /// @return
224 /// Zero if the for success, or an error code for failure.
225 //------------------------------------------------------------------
226 int
227 SetDisableASLR (bool enable);
228
229 //------------------------------------------------------------------
230 /// Sets the working directory to \a path for a process that will
231 /// be launched with the 'A' packet.
232 ///
233 /// @param[in] path
234 /// The path to a directory to use when launching our processs
235 ///
236 /// @return
237 /// Zero if the for success, or an error code for failure.
238 //------------------------------------------------------------------
239 int
240 SetWorkingDir (char const *path);
241
Chris Lattner24943d22010-06-08 16:52:24 +0000242 lldb::addr_t
243 AllocateMemory (size_t size, uint32_t permissions, uint32_t timeout_seconds);
244
245 bool
246 DeallocateMemory (lldb::addr_t addr, uint32_t timeout_seconds);
247
248 bool
249 IsRunning() const
250 {
Greg Claytoncecf3482011-01-20 07:53:45 +0000251 return m_public_is_running.GetValue();
Chris Lattner24943d22010-06-08 16:52:24 +0000252 }
Greg Clayton72e1c782011-01-22 23:43:18 +0000253
254 bool
255 WaitForNotRunning (const lldb_private::TimeValue *timeout_ptr);
256
Chris Lattner24943d22010-06-08 16:52:24 +0000257 bool
Chris Lattner24943d22010-06-08 16:52:24 +0000258 GetHostInfo (uint32_t timeout_seconds);
259
Chris Lattner24943d22010-06-08 16:52:24 +0000260 const lldb_private::ArchSpec &
261 GetHostArchitecture ();
262
263 const lldb_private::ConstString &
264 GetOSString ();
265
266 const lldb_private::ConstString &
267 GetVendorString();
268
269 lldb::ByteOrder
270 GetByteOrder ();
271
272 uint32_t
273 GetAddressByteSize ();
274
Greg Claytonc1f45872011-02-12 06:28:37 +0000275 bool
276 GetVContSupported (char flavor);
277
278 void
279 ResetDiscoverableSettings();
280
281 bool
282 GetHostInfo ();
283
284 bool
285 GetSendAcks ();
286
287 bool
288 GetSupportsThreadSuffix ();
289
290 bool
291 HasFullVContSupport ()
292 {
293 return GetVContSupported ('A');
294 }
295
296 bool
297 HasAnyVContSupport ()
298 {
299 return GetVContSupported ('a');
300 }
301
Chris Lattner24943d22010-06-08 16:52:24 +0000302protected:
303 typedef std::list<std::string> packet_collection;
304
305 size_t
306 SendPacketNoLock (const char *payload,
307 size_t payload_length);
308
309 size_t
310 WaitForPacketNoLock (StringExtractorGDBRemote &response,
Greg Clayton72e1c782011-01-22 23:43:18 +0000311 const lldb_private::TimeValue* timeout_ptr);
312
313 bool
314 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000315
Greg Claytonc1f45872011-02-12 06:28:37 +0000316 bool
317 HostInfoIsValid () const
318 {
319 return m_supports_qHostInfo != lldb::eLazyBoolCalculate;
320 }
321
Chris Lattner24943d22010-06-08 16:52:24 +0000322 //------------------------------------------------------------------
323 // Classes that inherit from GDBRemoteCommunication can see and modify these
324 //------------------------------------------------------------------
Greg Claytonc1f45872011-02-12 06:28:37 +0000325 lldb::LazyBool m_supports_not_sending_acks;
326 lldb::LazyBool m_supports_thread_suffix;
327 lldb::LazyBool m_supports_qHostInfo;
328 lldb::LazyBool m_supports_vCont_all;
329 lldb::LazyBool m_supports_vCont_any;
330 lldb::LazyBool m_supports_vCont_c;
331 lldb::LazyBool m_supports_vCont_C;
332 lldb::LazyBool m_supports_vCont_s;
333 lldb::LazyBool m_supports_vCont_S;
Chris Lattner24943d22010-06-08 16:52:24 +0000334 lldb_private::Listener m_rx_packet_listener;
335 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 +0000336 lldb_private::Predicate<bool> m_public_is_running;
337 lldb_private::Predicate<bool> m_private_is_running;
Chris Lattner24943d22010-06-08 16:52:24 +0000338
339 // If we need to send a packet while the target is running, the m_async_XXX
340 // member variables take care of making this happen.
341 lldb_private::Mutex m_async_mutex;
342 lldb_private::Predicate<bool> m_async_packet_predicate;
343 std::string m_async_packet;
344 StringExtractorGDBRemote m_async_response;
345 uint32_t m_async_timeout;
346 int m_async_signal; // We were asked to deliver a signal to the inferior process.
347
348 lldb_private::ArchSpec m_arch; // Results from the qHostInfo call
349 uint32_t m_cpusubtype; // Results from the qHostInfo call
350 lldb_private::ConstString m_os; // Results from the qHostInfo call
351 lldb_private::ConstString m_vendor; // Results from the qHostInfo call
352 lldb::ByteOrder m_byte_order; // Results from the qHostInfo call
353 uint32_t m_pointer_byte_size; // Results from the qHostInfo call
354
355
356private:
357 //------------------------------------------------------------------
358 // For GDBRemoteCommunication only
359 //------------------------------------------------------------------
360 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
361};
362
363#endif // liblldb_GDBRemoteCommunication_h_