blob: dbd1bd0e32b3c3fd0f94863059da8d9eb4f4bacb [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,
Chris Lattner24943d22010-06-08 16:52:24 +000059 bool send_async);
60
61 size_t
62 SendPacketAndWaitForResponse (const char *send_payload,
63 size_t send_length,
64 StringExtractorGDBRemote &response,
Chris Lattner24943d22010-06-08 16:52:24 +000065 bool send_async);
66
67 lldb::StateType
68 SendContinuePacketAndWaitForResponse (ProcessGDBRemote *process,
69 const char *packet_payload,
70 size_t packet_length,
71 StringExtractorGDBRemote &response);
72
73 // Wait for a packet within 'nsec' seconds
74 size_t
75 WaitForPacket (StringExtractorGDBRemote &response,
Greg Claytonc97bfdb2011-03-10 02:26:48 +000076 uint32_t sec);
Chris Lattner24943d22010-06-08 16:52:24 +000077
78 // Wait for a packet with an absolute timeout time. If 'timeout' is NULL
79 // wait indefinitely.
80 size_t
81 WaitForPacket (StringExtractorGDBRemote &response,
Greg Clayton72e1c782011-01-22 23:43:18 +000082 const lldb_private::TimeValue* timeout);
Chris Lattner24943d22010-06-08 16:52:24 +000083
84 char
Greg Claytonc97bfdb2011-03-10 02:26:48 +000085 GetAck ();
Chris Lattner24943d22010-06-08 16:52:24 +000086
87 size_t
Greg Claytona4881d02011-01-22 07:12:45 +000088 SendAck ();
89
90 size_t
91 SendNack ();
92
Chris Lattner24943d22010-06-08 16:52:24 +000093
94 char
95 CalculcateChecksum (const char *payload,
96 size_t payload_length);
97
Chris Lattner24943d22010-06-08 16:52:24 +000098 bool
Greg Claytonc1f45872011-02-12 06:28:37 +000099 GetThreadSuffixSupported ();
Greg Claytonc71899e2011-01-18 19:36:39 +0000100
101 bool
Chris Lattner24943d22010-06-08 16:52:24 +0000102 SendAsyncSignal (int signo);
103
104 bool
Greg Clayton1a679462010-09-03 19:15:43 +0000105 SendInterrupt (lldb_private::Mutex::Locker &locker,
106 uint32_t seconds_to_wait_for_stop,
Greg Claytona4881d02011-01-22 07:12:45 +0000107 bool &sent_interrupt,
108 bool &timed_out);
Chris Lattner24943d22010-06-08 16:52:24 +0000109
110 bool
111 GetSequenceMutex(lldb_private::Mutex::Locker& locker);
112
113 //------------------------------------------------------------------
114 // Communication overrides
115 //------------------------------------------------------------------
116 virtual void
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000117 AppendBytesToCache (const uint8_t *src, size_t src_len, bool broadcast, lldb::ConnectionStatus status);
Chris Lattner24943d22010-06-08 16:52:24 +0000118
119
120 lldb::pid_t
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000121 GetCurrentProcessID ();
Chris Lattner24943d22010-06-08 16:52:24 +0000122
123 bool
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000124 GetLaunchSuccess (std::string &error_str);
Chris Lattner24943d22010-06-08 16:52:24 +0000125
126 //------------------------------------------------------------------
127 /// Sends a GDB remote protocol 'A' packet that delivers program
128 /// arguments to the remote server.
129 ///
130 /// @param[in] argv
131 /// A NULL terminated array of const C strings to use as the
132 /// arguments.
133 ///
Chris Lattner24943d22010-06-08 16:52:24 +0000134 /// @return
135 /// Zero if the response was "OK", a positive value if the
136 /// the response was "Exx" where xx are two hex digits, or
137 /// -1 if the call is unsupported or any other unexpected
138 /// response was received.
139 //------------------------------------------------------------------
140 int
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000141 SendArgumentsPacket (char const *argv[]);
Chris Lattner24943d22010-06-08 16:52:24 +0000142
143 //------------------------------------------------------------------
144 /// Sends a "QEnvironment:NAME=VALUE" packet that will build up the
145 /// environment that will get used when launching an application
146 /// in conjunction with the 'A' packet. This function can be called
147 /// multiple times in a row in order to pass on the desired
148 /// environment that the inferior should be launched with.
149 ///
150 /// @param[in] name_equal_value
Greg Clayton5d187e52011-01-08 20:28:42 +0000151 /// A NULL terminated C string that contains a single environment
Chris Lattner24943d22010-06-08 16:52:24 +0000152 /// in the format "NAME=VALUE".
153 ///
Chris Lattner24943d22010-06-08 16:52:24 +0000154 /// @return
155 /// Zero if the response was "OK", a positive value if the
156 /// the response was "Exx" where xx are two hex digits, or
157 /// -1 if the call is unsupported or any other unexpected
158 /// response was received.
159 //------------------------------------------------------------------
160 int
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000161 SendEnvironmentPacket (char const *name_equal_value);
Chris Lattner24943d22010-06-08 16:52:24 +0000162
163 //------------------------------------------------------------------
164 /// Sends a "vAttach:PID" where PID is in hex.
165 ///
166 /// @param[in] pid
167 /// A process ID for the remote gdb server to attach to.
168 ///
Chris Lattner24943d22010-06-08 16:52:24 +0000169 /// @param[out] response
170 /// The response received from the gdb server. If the return
171 /// value is zero, \a response will contain a stop reply
172 /// packet.
173 ///
174 /// @return
175 /// Zero if the attach was successful, or an error indicating
176 /// an error code.
177 //------------------------------------------------------------------
178 int
179 SendAttach (lldb::pid_t pid,
Chris Lattner24943d22010-06-08 16:52:24 +0000180 StringExtractorGDBRemote& response);
181
182
Greg Claytona2f74232011-02-24 22:24:29 +0000183 //------------------------------------------------------------------
184 /// Sets the path to use for stdin/out/err for a process
185 /// that will be launched with the 'A' packet.
186 ///
187 /// @param[in] path
188 /// The path to use for stdin/out/err
189 ///
190 /// @return
191 /// Zero if the for success, or an error code for failure.
192 //------------------------------------------------------------------
193 int
194 SetSTDIN (char const *path);
195 int
196 SetSTDOUT (char const *path);
197 int
198 SetSTDERR (char const *path);
199
200 //------------------------------------------------------------------
201 /// Sets the disable ASLR flag to \a enable for a process that will
202 /// be launched with the 'A' packet.
203 ///
204 /// @param[in] enable
205 /// A boolean value indicating wether to disable ASLR or not.
206 ///
207 /// @return
208 /// Zero if the for success, or an error code for failure.
209 //------------------------------------------------------------------
210 int
211 SetDisableASLR (bool enable);
212
213 //------------------------------------------------------------------
214 /// Sets the working directory to \a path for a process that will
215 /// be launched with the 'A' packet.
216 ///
217 /// @param[in] path
218 /// The path to a directory to use when launching our processs
219 ///
220 /// @return
221 /// Zero if the for success, or an error code for failure.
222 //------------------------------------------------------------------
223 int
224 SetWorkingDir (char const *path);
225
Chris Lattner24943d22010-06-08 16:52:24 +0000226 lldb::addr_t
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000227 AllocateMemory (size_t size, uint32_t permissions);
Chris Lattner24943d22010-06-08 16:52:24 +0000228
229 bool
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000230 DeallocateMemory (lldb::addr_t addr);
Chris Lattner24943d22010-06-08 16:52:24 +0000231
232 bool
233 IsRunning() const
234 {
Greg Claytoncecf3482011-01-20 07:53:45 +0000235 return m_public_is_running.GetValue();
Chris Lattner24943d22010-06-08 16:52:24 +0000236 }
Greg Clayton72e1c782011-01-22 23:43:18 +0000237
Chris Lattner24943d22010-06-08 16:52:24 +0000238 const lldb_private::ArchSpec &
239 GetHostArchitecture ();
240
241 const lldb_private::ConstString &
242 GetOSString ();
243
244 const lldb_private::ConstString &
245 GetVendorString();
246
247 lldb::ByteOrder
248 GetByteOrder ();
249
250 uint32_t
251 GetAddressByteSize ();
252
Greg Claytonc1f45872011-02-12 06:28:37 +0000253 bool
254 GetVContSupported (char flavor);
255
256 void
257 ResetDiscoverableSettings();
258
259 bool
260 GetHostInfo ();
261
262 bool
263 GetSendAcks ();
264
265 bool
266 GetSupportsThreadSuffix ();
267
268 bool
269 HasFullVContSupport ()
270 {
271 return GetVContSupported ('A');
272 }
273
274 bool
275 HasAnyVContSupport ()
276 {
277 return GetVContSupported ('a');
278 }
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000279
280 uint32_t
281 SetPacketTimeout (uint32_t packet_timeout)
282 {
283 const uint32_t old_packet_timeout = m_packet_timeout;
284 m_packet_timeout = packet_timeout;
285 return old_packet_timeout;
286 }
Greg Claytonc1f45872011-02-12 06:28:37 +0000287
Chris Lattner24943d22010-06-08 16:52:24 +0000288protected:
289 typedef std::list<std::string> packet_collection;
290
291 size_t
292 SendPacketNoLock (const char *payload,
293 size_t payload_length);
294
295 size_t
296 WaitForPacketNoLock (StringExtractorGDBRemote &response,
Greg Clayton72e1c782011-01-22 23:43:18 +0000297 const lldb_private::TimeValue* timeout_ptr);
298
299 bool
300 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000301
Greg Claytonc1f45872011-02-12 06:28:37 +0000302 bool
303 HostInfoIsValid () const
304 {
305 return m_supports_qHostInfo != lldb::eLazyBoolCalculate;
306 }
307
Chris Lattner24943d22010-06-08 16:52:24 +0000308 //------------------------------------------------------------------
309 // Classes that inherit from GDBRemoteCommunication can see and modify these
310 //------------------------------------------------------------------
Greg Claytonc97bfdb2011-03-10 02:26:48 +0000311 uint32_t m_packet_timeout;
Greg Claytonc1f45872011-02-12 06:28:37 +0000312 lldb::LazyBool m_supports_not_sending_acks;
313 lldb::LazyBool m_supports_thread_suffix;
314 lldb::LazyBool m_supports_qHostInfo;
315 lldb::LazyBool m_supports_vCont_all;
316 lldb::LazyBool m_supports_vCont_any;
317 lldb::LazyBool m_supports_vCont_c;
318 lldb::LazyBool m_supports_vCont_C;
319 lldb::LazyBool m_supports_vCont_s;
320 lldb::LazyBool m_supports_vCont_S;
Chris Lattner24943d22010-06-08 16:52:24 +0000321 lldb_private::Listener m_rx_packet_listener;
322 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 +0000323 lldb_private::Predicate<bool> m_public_is_running;
324 lldb_private::Predicate<bool> m_private_is_running;
Chris Lattner24943d22010-06-08 16:52:24 +0000325
326 // If we need to send a packet while the target is running, the m_async_XXX
327 // member variables take care of making this happen.
328 lldb_private::Mutex m_async_mutex;
329 lldb_private::Predicate<bool> m_async_packet_predicate;
330 std::string m_async_packet;
331 StringExtractorGDBRemote m_async_response;
Chris Lattner24943d22010-06-08 16:52:24 +0000332 int m_async_signal; // We were asked to deliver a signal to the inferior process.
333
334 lldb_private::ArchSpec m_arch; // Results from the qHostInfo call
335 uint32_t m_cpusubtype; // Results from the qHostInfo call
336 lldb_private::ConstString m_os; // Results from the qHostInfo call
337 lldb_private::ConstString m_vendor; // Results from the qHostInfo call
338 lldb::ByteOrder m_byte_order; // Results from the qHostInfo call
339 uint32_t m_pointer_byte_size; // Results from the qHostInfo call
340
341
342private:
343 //------------------------------------------------------------------
344 // For GDBRemoteCommunication only
345 //------------------------------------------------------------------
346 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
347};
348
349#endif // liblldb_GDBRemoteCommunication_h_