blob: 3ebabeb7085dd9bbcf5bfd7ddc9285d15b1e324d [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
100 void
101 SetAckMode (bool enabled)
102 {
103 m_send_acks = enabled;
104 }
105
106 bool
Greg Claytonc71899e2011-01-18 19:36:39 +0000107 GetThreadSuffixSupported () const
108 {
109 return m_thread_suffix_supported;
110 }
111
112 void
113 SetThreadSuffixSupported (bool enabled)
114 {
115 m_thread_suffix_supported = enabled;
116 }
117
118 bool
Chris Lattner24943d22010-06-08 16:52:24 +0000119 SendAsyncSignal (int signo);
120
121 bool
Greg Clayton1a679462010-09-03 19:15:43 +0000122 SendInterrupt (lldb_private::Mutex::Locker &locker,
123 uint32_t seconds_to_wait_for_stop,
Greg Claytona4881d02011-01-22 07:12:45 +0000124 bool &sent_interrupt,
125 bool &timed_out);
Chris Lattner24943d22010-06-08 16:52:24 +0000126
127 bool
128 GetSequenceMutex(lldb_private::Mutex::Locker& locker);
129
130 //------------------------------------------------------------------
131 // Communication overrides
132 //------------------------------------------------------------------
133 virtual void
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000134 AppendBytesToCache (const uint8_t *src, size_t src_len, bool broadcast, lldb::ConnectionStatus status);
Chris Lattner24943d22010-06-08 16:52:24 +0000135
136
137 lldb::pid_t
138 GetCurrentProcessID (uint32_t timeout_seconds);
139
140 bool
141 GetLaunchSuccess (uint32_t timeout_seconds, std::string &error_str);
142
143 //------------------------------------------------------------------
144 /// Sends a GDB remote protocol 'A' packet that delivers program
145 /// arguments to the remote server.
146 ///
147 /// @param[in] argv
148 /// A NULL terminated array of const C strings to use as the
149 /// arguments.
150 ///
151 /// @param[in] timeout_seconds
152 /// The number of seconds to wait for a response from the remote
153 /// server.
154 ///
155 /// @return
156 /// Zero if the response was "OK", a positive value if the
157 /// the response was "Exx" where xx are two hex digits, or
158 /// -1 if the call is unsupported or any other unexpected
159 /// response was received.
160 //------------------------------------------------------------------
161 int
162 SendArgumentsPacket (char const *argv[], uint32_t timeout_seconds);
163
164 //------------------------------------------------------------------
165 /// Sends a "QEnvironment:NAME=VALUE" packet that will build up the
166 /// environment that will get used when launching an application
167 /// in conjunction with the 'A' packet. This function can be called
168 /// multiple times in a row in order to pass on the desired
169 /// environment that the inferior should be launched with.
170 ///
171 /// @param[in] name_equal_value
Greg Clayton5d187e52011-01-08 20:28:42 +0000172 /// A NULL terminated C string that contains a single environment
Chris Lattner24943d22010-06-08 16:52:24 +0000173 /// in the format "NAME=VALUE".
174 ///
175 /// @param[in] timeout_seconds
176 /// The number of seconds to wait for a response from the remote
177 /// server.
178 ///
179 /// @return
180 /// Zero if the response was "OK", a positive value if the
181 /// the response was "Exx" where xx are two hex digits, or
182 /// -1 if the call is unsupported or any other unexpected
183 /// response was received.
184 //------------------------------------------------------------------
185 int
186 SendEnvironmentPacket (char const *name_equal_value,
187 uint32_t timeout_seconds);
188
189 //------------------------------------------------------------------
190 /// Sends a "vAttach:PID" where PID is in hex.
191 ///
192 /// @param[in] pid
193 /// A process ID for the remote gdb server to attach to.
194 ///
195 /// @param[in] timeout_seconds
196 /// The number of seconds to wait for a response from the remote
197 /// server.
198 ///
199 /// @param[out] response
200 /// The response received from the gdb server. If the return
201 /// value is zero, \a response will contain a stop reply
202 /// packet.
203 ///
204 /// @return
205 /// Zero if the attach was successful, or an error indicating
206 /// an error code.
207 //------------------------------------------------------------------
208 int
209 SendAttach (lldb::pid_t pid,
210 uint32_t timeout_seconds,
211 StringExtractorGDBRemote& response);
212
213
214 lldb::addr_t
215 AllocateMemory (size_t size, uint32_t permissions, uint32_t timeout_seconds);
216
217 bool
218 DeallocateMemory (lldb::addr_t addr, uint32_t timeout_seconds);
219
220 bool
221 IsRunning() const
222 {
Greg Claytoncecf3482011-01-20 07:53:45 +0000223 return m_public_is_running.GetValue();
Chris Lattner24943d22010-06-08 16:52:24 +0000224 }
Greg Clayton72e1c782011-01-22 23:43:18 +0000225
226 bool
227 WaitForNotRunning (const lldb_private::TimeValue *timeout_ptr);
228
Chris Lattner24943d22010-06-08 16:52:24 +0000229 bool
Chris Lattner24943d22010-06-08 16:52:24 +0000230 GetHostInfo (uint32_t timeout_seconds);
231
232 bool
233 HostInfoIsValid () const
234 {
235 return m_pointer_byte_size != 0;
236 }
237
238 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
253protected:
254 typedef std::list<std::string> packet_collection;
255
256 size_t
257 SendPacketNoLock (const char *payload,
258 size_t payload_length);
259
260 size_t
261 WaitForPacketNoLock (StringExtractorGDBRemote &response,
Greg Clayton72e1c782011-01-22 23:43:18 +0000262 const lldb_private::TimeValue* timeout_ptr);
263
264 bool
265 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +0000266
267 //------------------------------------------------------------------
268 // Classes that inherit from GDBRemoteCommunication can see and modify these
269 //------------------------------------------------------------------
Greg Claytonc71899e2011-01-18 19:36:39 +0000270 bool m_send_acks:1,
271 m_thread_suffix_supported:1;
Chris Lattner24943d22010-06-08 16:52:24 +0000272 lldb_private::Listener m_rx_packet_listener;
273 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 +0000274 lldb_private::Predicate<bool> m_public_is_running;
275 lldb_private::Predicate<bool> m_private_is_running;
Chris Lattner24943d22010-06-08 16:52:24 +0000276
277 // If we need to send a packet while the target is running, the m_async_XXX
278 // member variables take care of making this happen.
279 lldb_private::Mutex m_async_mutex;
280 lldb_private::Predicate<bool> m_async_packet_predicate;
281 std::string m_async_packet;
282 StringExtractorGDBRemote m_async_response;
283 uint32_t m_async_timeout;
284 int m_async_signal; // We were asked to deliver a signal to the inferior process.
285
286 lldb_private::ArchSpec m_arch; // Results from the qHostInfo call
287 uint32_t m_cpusubtype; // Results from the qHostInfo call
288 lldb_private::ConstString m_os; // Results from the qHostInfo call
289 lldb_private::ConstString m_vendor; // Results from the qHostInfo call
290 lldb::ByteOrder m_byte_order; // Results from the qHostInfo call
291 uint32_t m_pointer_byte_size; // Results from the qHostInfo call
292
293
294private:
295 //------------------------------------------------------------------
296 // For GDBRemoteCommunication only
297 //------------------------------------------------------------------
298 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
299};
300
301#endif // liblldb_GDBRemoteCommunication_h_