blob: 0434d8fb736655716d884efcb83abc018c7090e9 [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,
84 lldb_private::TimeValue* timeout);
85
86 char
87 GetAck (uint32_t timeout_seconds);
88
89 size_t
90 SendAck (char ack_char);
91
92 char
93 CalculcateChecksum (const char *payload,
94 size_t payload_length);
95
96 void
97 SetAckMode (bool enabled)
98 {
99 m_send_acks = enabled;
100 }
101
102 bool
103 SendAsyncSignal (int signo);
104
105 bool
Greg Clayton1a679462010-09-03 19:15:43 +0000106 SendInterrupt (lldb_private::Mutex::Locker &locker,
107 uint32_t seconds_to_wait_for_stop,
108 bool *timed_out = NULL);
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
121 GetCurrentProcessID (uint32_t timeout_seconds);
122
123 bool
124 GetLaunchSuccess (uint32_t timeout_seconds, std::string &error_str);
125
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 ///
134 /// @param[in] timeout_seconds
135 /// The number of seconds to wait for a response from the remote
136 /// server.
137 ///
138 /// @return
139 /// Zero if the response was "OK", a positive value if the
140 /// the response was "Exx" where xx are two hex digits, or
141 /// -1 if the call is unsupported or any other unexpected
142 /// response was received.
143 //------------------------------------------------------------------
144 int
145 SendArgumentsPacket (char const *argv[], uint32_t timeout_seconds);
146
147 //------------------------------------------------------------------
148 /// Sends a "QEnvironment:NAME=VALUE" packet that will build up the
149 /// environment that will get used when launching an application
150 /// in conjunction with the 'A' packet. This function can be called
151 /// multiple times in a row in order to pass on the desired
152 /// environment that the inferior should be launched with.
153 ///
154 /// @param[in] name_equal_value
155 /// A NULL terminated C string that contains a single enironment
156 /// in the format "NAME=VALUE".
157 ///
158 /// @param[in] timeout_seconds
159 /// The number of seconds to wait for a response from the remote
160 /// server.
161 ///
162 /// @return
163 /// Zero if the response was "OK", a positive value if the
164 /// the response was "Exx" where xx are two hex digits, or
165 /// -1 if the call is unsupported or any other unexpected
166 /// response was received.
167 //------------------------------------------------------------------
168 int
169 SendEnvironmentPacket (char const *name_equal_value,
170 uint32_t timeout_seconds);
171
172 //------------------------------------------------------------------
173 /// Sends a "vAttach:PID" where PID is in hex.
174 ///
175 /// @param[in] pid
176 /// A process ID for the remote gdb server to attach to.
177 ///
178 /// @param[in] timeout_seconds
179 /// The number of seconds to wait for a response from the remote
180 /// server.
181 ///
182 /// @param[out] response
183 /// The response received from the gdb server. If the return
184 /// value is zero, \a response will contain a stop reply
185 /// packet.
186 ///
187 /// @return
188 /// Zero if the attach was successful, or an error indicating
189 /// an error code.
190 //------------------------------------------------------------------
191 int
192 SendAttach (lldb::pid_t pid,
193 uint32_t timeout_seconds,
194 StringExtractorGDBRemote& response);
195
196
197 lldb::addr_t
198 AllocateMemory (size_t size, uint32_t permissions, uint32_t timeout_seconds);
199
200 bool
201 DeallocateMemory (lldb::addr_t addr, uint32_t timeout_seconds);
202
203 bool
204 IsRunning() const
205 {
206 return m_is_running.GetValue();
207 }
208
209 bool
Chris Lattner24943d22010-06-08 16:52:24 +0000210 GetHostInfo (uint32_t timeout_seconds);
211
212 bool
213 HostInfoIsValid () const
214 {
215 return m_pointer_byte_size != 0;
216 }
217
218 const lldb_private::ArchSpec &
219 GetHostArchitecture ();
220
221 const lldb_private::ConstString &
222 GetOSString ();
223
224 const lldb_private::ConstString &
225 GetVendorString();
226
227 lldb::ByteOrder
228 GetByteOrder ();
229
230 uint32_t
231 GetAddressByteSize ();
232
233protected:
234 typedef std::list<std::string> packet_collection;
235
236 size_t
237 SendPacketNoLock (const char *payload,
238 size_t payload_length);
239
240 size_t
241 WaitForPacketNoLock (StringExtractorGDBRemote &response,
242 lldb_private::TimeValue* timeout_time_ptr);
243
244 //------------------------------------------------------------------
245 // Classes that inherit from GDBRemoteCommunication can see and modify these
246 //------------------------------------------------------------------
247 bool m_send_acks;
248 lldb_private::Listener m_rx_packet_listener;
249 lldb_private::Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time
250 lldb_private::Predicate<bool> m_is_running;
251
252 // If we need to send a packet while the target is running, the m_async_XXX
253 // member variables take care of making this happen.
254 lldb_private::Mutex m_async_mutex;
255 lldb_private::Predicate<bool> m_async_packet_predicate;
256 std::string m_async_packet;
257 StringExtractorGDBRemote m_async_response;
258 uint32_t m_async_timeout;
259 int m_async_signal; // We were asked to deliver a signal to the inferior process.
260
261 lldb_private::ArchSpec m_arch; // Results from the qHostInfo call
262 uint32_t m_cpusubtype; // Results from the qHostInfo call
263 lldb_private::ConstString m_os; // Results from the qHostInfo call
264 lldb_private::ConstString m_vendor; // Results from the qHostInfo call
265 lldb::ByteOrder m_byte_order; // Results from the qHostInfo call
266 uint32_t m_pointer_byte_size; // Results from the qHostInfo call
267
268
269private:
270 //------------------------------------------------------------------
271 // For GDBRemoteCommunication only
272 //------------------------------------------------------------------
273 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunication);
274};
275
276#endif // liblldb_GDBRemoteCommunication_h_