blob: e7ef38ab1e0f6ea1d6e8afe7be501e503c1c979b [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:
37 //------------------------------------------------------------------
38 // Constructors and Destructors
39 //------------------------------------------------------------------
40 GDBRemoteCommunication();
41
42 virtual
43 ~GDBRemoteCommunication();
44
45 size_t
46 SendPacket (const char *payload);
47
48 size_t
49 SendPacket (const char *payload,
50 size_t payload_length);
51
52 size_t
53 SendPacketAndWaitForResponse (const char *send_payload,
54 StringExtractorGDBRemote &response,
55 uint32_t timeout_seconds,
56 bool send_async);
57
58 size_t
59 SendPacketAndWaitForResponse (const char *send_payload,
60 size_t send_length,
61 StringExtractorGDBRemote &response,
62 uint32_t timeout_seconds,
63 bool send_async);
64
65 lldb::StateType
66 SendContinuePacketAndWaitForResponse (ProcessGDBRemote *process,
67 const char *packet_payload,
68 size_t packet_length,
69 StringExtractorGDBRemote &response);
70
71 // Wait for a packet within 'nsec' seconds
72 size_t
73 WaitForPacket (StringExtractorGDBRemote &response,
74 uint32_t nsec);
75
76 // Wait for a packet with an absolute timeout time. If 'timeout' is NULL
77 // wait indefinitely.
78 size_t
79 WaitForPacket (StringExtractorGDBRemote &response,
80 lldb_private::TimeValue* timeout);
81
82 char
83 GetAck (uint32_t timeout_seconds);
84
85 size_t
86 SendAck (char ack_char);
87
88 char
89 CalculcateChecksum (const char *payload,
90 size_t payload_length);
91
92 void
93 SetAckMode (bool enabled)
94 {
95 m_send_acks = enabled;
96 }
97
98 bool
99 SendAsyncSignal (int signo);
100
101 bool
Greg Clayton1a679462010-09-03 19:15:43 +0000102 SendInterrupt (lldb_private::Mutex::Locker &locker,
103 uint32_t seconds_to_wait_for_stop,
104 bool *timed_out = NULL);
Chris Lattner24943d22010-06-08 16:52:24 +0000105
106 bool
107 GetSequenceMutex(lldb_private::Mutex::Locker& locker);
108
109 //------------------------------------------------------------------
110 // Communication overrides
111 //------------------------------------------------------------------
112 virtual void
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000113 AppendBytesToCache (const uint8_t *src, size_t src_len, bool broadcast, lldb::ConnectionStatus status);
Chris Lattner24943d22010-06-08 16:52:24 +0000114
115
116 lldb::pid_t
117 GetCurrentProcessID (uint32_t timeout_seconds);
118
119 bool
120 GetLaunchSuccess (uint32_t timeout_seconds, std::string &error_str);
121
122 //------------------------------------------------------------------
123 /// Sends a GDB remote protocol 'A' packet that delivers program
124 /// arguments to the remote server.
125 ///
126 /// @param[in] argv
127 /// A NULL terminated array of const C strings to use as the
128 /// arguments.
129 ///
130 /// @param[in] timeout_seconds
131 /// The number of seconds to wait for a response from the remote
132 /// server.
133 ///
134 /// @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
141 SendArgumentsPacket (char const *argv[], uint32_t timeout_seconds);
142
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
151 /// A NULL terminated C string that contains a single enironment
152 /// in the format "NAME=VALUE".
153 ///
154 /// @param[in] timeout_seconds
155 /// The number of seconds to wait for a response from the remote
156 /// server.
157 ///
158 /// @return
159 /// Zero if the response was "OK", a positive value if the
160 /// the response was "Exx" where xx are two hex digits, or
161 /// -1 if the call is unsupported or any other unexpected
162 /// response was received.
163 //------------------------------------------------------------------
164 int
165 SendEnvironmentPacket (char const *name_equal_value,
166 uint32_t timeout_seconds);
167
168 //------------------------------------------------------------------
169 /// Sends a "vAttach:PID" where PID is in hex.
170 ///
171 /// @param[in] pid
172 /// A process ID for the remote gdb server to attach to.
173 ///
174 /// @param[in] timeout_seconds
175 /// The number of seconds to wait for a response from the remote
176 /// server.
177 ///
178 /// @param[out] response
179 /// The response received from the gdb server. If the return
180 /// value is zero, \a response will contain a stop reply
181 /// packet.
182 ///
183 /// @return
184 /// Zero if the attach was successful, or an error indicating
185 /// an error code.
186 //------------------------------------------------------------------
187 int
188 SendAttach (lldb::pid_t pid,
189 uint32_t timeout_seconds,
190 StringExtractorGDBRemote& response);
191
192
193 lldb::addr_t
194 AllocateMemory (size_t size, uint32_t permissions, uint32_t timeout_seconds);
195
196 bool
197 DeallocateMemory (lldb::addr_t addr, uint32_t timeout_seconds);
198
199 bool
200 IsRunning() const
201 {
202 return m_is_running.GetValue();
203 }
204
205 bool
Jim Ingham3ae449a2010-11-17 02:32:00 +0000206 WaitForIsRunning (uint32_t timeout_sec);
207
208 bool
Chris Lattner24943d22010-06-08 16:52:24 +0000209 GetHostInfo (uint32_t timeout_seconds);
210
211 bool
212 HostInfoIsValid () const
213 {
214 return m_pointer_byte_size != 0;
215 }
216
217 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
232protected:
233 typedef std::list<std::string> packet_collection;
234
235 size_t
236 SendPacketNoLock (const char *payload,
237 size_t payload_length);
238
239 size_t
240 WaitForPacketNoLock (StringExtractorGDBRemote &response,
241 lldb_private::TimeValue* timeout_time_ptr);
242
243 //------------------------------------------------------------------
244 // Classes that inherit from GDBRemoteCommunication can see and modify these
245 //------------------------------------------------------------------
246 bool m_send_acks;
247 lldb_private::Listener m_rx_packet_listener;
248 lldb_private::Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time
249 lldb_private::Predicate<bool> m_is_running;
250
251 // If we need to send a packet while the target is running, the m_async_XXX
252 // member variables take care of making this happen.
253 lldb_private::Mutex m_async_mutex;
254 lldb_private::Predicate<bool> m_async_packet_predicate;
255 std::string m_async_packet;
256 StringExtractorGDBRemote m_async_response;
257 uint32_t m_async_timeout;
258 int m_async_signal; // We were asked to deliver a signal to the inferior process.
259
260 lldb_private::ArchSpec m_arch; // Results from the qHostInfo call
261 uint32_t m_cpusubtype; // Results from the qHostInfo call
262 lldb_private::ConstString m_os; // Results from the qHostInfo call
263 lldb_private::ConstString m_vendor; // Results from the qHostInfo call
264 lldb::ByteOrder m_byte_order; // Results from the qHostInfo call
265 uint32_t m_pointer_byte_size; // Results from the qHostInfo call
266
267
268private:
269 //------------------------------------------------------------------
270 // For GDBRemoteCommunication only
271 //------------------------------------------------------------------
272 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
273};
274
275#endif // liblldb_GDBRemoteCommunication_h_