blob: 8c5b6d5f2ba54fb45dd45b3ae04c450ab2dfd157 [file] [log] [blame]
Greg Clayton269f91e2011-07-15 18:02:58 +00001//===-- CommunicationKDP.h --------------------------------------*- C++ -*-===//
Greg Clayton363be3f2011-07-15 03:27:12 +00002//
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_CommunicationKDP_h_
11#define liblldb_CommunicationKDP_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/Communication.h"
22#include "lldb/Core/Listener.h"
Greg Claytond52d00f2011-07-16 03:19:08 +000023#include "lldb/Core/StreamBuffer.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000024#include "lldb/Host/Mutex.h"
25#include "lldb/Host/Predicate.h"
26#include "lldb/Host/TimeValue.h"
27
Greg Clayton363be3f2011-07-15 03:27:12 +000028class CommunicationKDP : public lldb_private::Communication
29{
30public:
31 enum
32 {
33 eBroadcastBitRunPacketSent = kLoUserBroadcastBit
34 };
Greg Clayton1e5b0212011-07-15 16:31:38 +000035
36 const static uint32_t kMaxPacketSize = 1200;
37 const static uint32_t kMaxDataSize = 1024;
Greg Claytond52d00f2011-07-16 03:19:08 +000038 typedef lldb_private::StreamBuffer<1024> PacketStreamType;
Greg Clayton1e5b0212011-07-15 16:31:38 +000039 typedef enum
40 {
Greg Claytond52d00f2011-07-16 03:19:08 +000041 eCommandTypeConnect = 0u,
42 eCommandTypeDisconnect,
43 eCommandTypeHostInfo,
44 eCommandTypeVersion,
45 eCommandTypeMaxBytes,
46 eCommandTypeReadMemory,
47 eCommandTypeWriteMemory,
48 eCommandTypeReadRegisters,
49 eCommandTypeWriteRegisters,
50 eCommandTypeLoad,
51 eCommandTypeImagePath,
52 eCommandTypeSuspend,
53 eCommandTypeResume,
54 eCommandTypeException,
55 eCommandTypeTermination,
56 eCommandTypeBreakpointSet,
57 eCommandTypeBreakpointRemove,
58 eCommandTypeRegions,
59 eCommandTypeReattach,
60 eCommandTypeHostReboot,
61 eCommandTypeReadMemory64,
62 eCommandTypeWriteMemory64,
63 eCommandTypeBreakpointSet64,
64 eCommandTypeBreakpointRemove64,
65 eCommandTypeKernelVersion
66 } CommandType;
Greg Clayton1e5b0212011-07-15 16:31:38 +000067
Greg Claytond52d00f2011-07-16 03:19:08 +000068 typedef enum
Greg Clayton1e5b0212011-07-15 16:31:38 +000069 {
Greg Claytond52d00f2011-07-16 03:19:08 +000070 KDP_PROTERR_SUCCESS = 0,
71 KDP_PROTERR_ALREADY_CONNECTED,
72 KDP_PROTERR_BAD_NBYTES,
73 KDP_PROTERR_BADFLAVOR
74 } KDPError;
Greg Clayton1e5b0212011-07-15 16:31:38 +000075
76 typedef enum
77 {
Greg Claytond52d00f2011-07-16 03:19:08 +000078 ePacketTypeRequest = 0x00u,
79 ePacketTypeReply = 0x80u,
80 ePacketTypeMask = 0x80u,
81 eCommandTypeMask = 0x7fu
Greg Clayton1e5b0212011-07-15 16:31:38 +000082 } PacketType;
Greg Clayton363be3f2011-07-15 03:27:12 +000083 //------------------------------------------------------------------
84 // Constructors and Destructors
85 //------------------------------------------------------------------
86 CommunicationKDP (const char *comm_name);
87
88 virtual
89 ~CommunicationKDP();
90
Greg Clayton1e5b0212011-07-15 16:31:38 +000091 bool
Greg Claytond52d00f2011-07-16 03:19:08 +000092 SendRequestPacket (const PacketStreamType &request_packet);
Greg Clayton363be3f2011-07-15 03:27:12 +000093
94 // Wait for a packet within 'nsec' seconds
95 size_t
Greg Claytond52d00f2011-07-16 03:19:08 +000096 WaitForPacketWithTimeoutMicroSeconds (lldb_private::DataExtractor &response,
Greg Clayton363be3f2011-07-15 03:27:12 +000097 uint32_t usec);
98
Greg Clayton363be3f2011-07-15 03:27:12 +000099 bool
100 GetSequenceMutex(lldb_private::Mutex::Locker& locker);
101
102 bool
103 CheckForPacket (const uint8_t *src,
104 size_t src_len,
Greg Claytond52d00f2011-07-16 03:19:08 +0000105 lldb_private::DataExtractor &packet);
Greg Clayton363be3f2011-07-15 03:27:12 +0000106 bool
107 IsRunning() const
108 {
109 return m_public_is_running.GetValue();
110 }
111
Greg Clayton363be3f2011-07-15 03:27:12 +0000112 //------------------------------------------------------------------
113 // Set the global packet timeout.
114 //
115 // For clients, this is the timeout that gets used when sending
116 // packets and waiting for responses. For servers, this might not
117 // get used, and if it doesn't this should be moved to the
118 // CommunicationKDPClient.
119 //------------------------------------------------------------------
120 uint32_t
121 SetPacketTimeout (uint32_t packet_timeout)
122 {
123 const uint32_t old_packet_timeout = m_packet_timeout;
124 m_packet_timeout = packet_timeout;
125 return old_packet_timeout;
126 }
127
128 uint32_t
129 GetPacketTimeoutInMicroSeconds () const
130 {
131 return m_packet_timeout * lldb_private::TimeValue::MicroSecPerSec;
132 }
133 //------------------------------------------------------------------
134 // Start a debugserver instance on the current host using the
135 // supplied connection URL.
136 //------------------------------------------------------------------
137 lldb_private::Error
138 StartDebugserverProcess (const char *connect_url,
139 const char *unix_socket_name,
140 lldb_private::ProcessLaunchInfo &launch_info);
141
142
Greg Clayton0fa51242011-07-19 03:57:15 +0000143 //------------------------------------------------------------------
144 // Public Request Packets
145 //------------------------------------------------------------------
Greg Claytond52d00f2011-07-16 03:19:08 +0000146 bool
Greg Clayton8d2ea282011-07-17 20:36:25 +0000147 SendRequestConnect (uint16_t reply_port,
148 uint16_t exc_port,
149 const char *greeting);
Greg Clayton1e5b0212011-07-15 16:31:38 +0000150
Greg Claytond52d00f2011-07-16 03:19:08 +0000151 bool
Greg Clayton8d2ea282011-07-17 20:36:25 +0000152 SendRequestReattach (uint16_t reply_port);
Greg Claytond52d00f2011-07-16 03:19:08 +0000153
154 bool
Greg Clayton8d2ea282011-07-17 20:36:25 +0000155 SendRequestDisconnect ();
Greg Claytond52d00f2011-07-16 03:19:08 +0000156
157 uint32_t
Greg Clayton0fa51242011-07-19 03:57:15 +0000158 SendRequestReadMemory (lldb::addr_t addr,
Greg Claytonec15f502011-07-20 01:32:50 +0000159 void *dst,
160 uint32_t dst_size,
Greg Clayton0fa51242011-07-19 03:57:15 +0000161 lldb_private::Error &error);
162
163 uint32_t
Greg Claytonec15f502011-07-20 01:32:50 +0000164 SendRequestWriteMemory (lldb::addr_t addr,
165 const void *src,
166 uint32_t src_len,
167 lldb_private::Error &error);
168
169 uint32_t
Greg Clayton0fa51242011-07-19 03:57:15 +0000170 SendRequestReadRegisters (uint32_t cpu,
171 uint32_t flavor,
172 void *dst,
173 uint32_t dst_size,
174 lldb_private::Error &error);
Greg Clayton0fa51242011-07-19 03:57:15 +0000175
176 uint32_t
Greg Claytond52d00f2011-07-16 03:19:08 +0000177 GetVersion ();
178
179 uint32_t
180 GetFeatureFlags ();
181
182 uint32_t
183 GetCPUMask ();
184
185 uint32_t
186 GetCPUType ();
187
188 uint32_t
189 GetCPUSubtype ();
Greg Clayton1e5b0212011-07-15 16:31:38 +0000190
Greg Clayton363be3f2011-07-15 03:27:12 +0000191protected:
192 typedef std::list<std::string> packet_collection;
193
Greg Clayton1e5b0212011-07-15 16:31:38 +0000194 bool
Greg Claytond52d00f2011-07-16 03:19:08 +0000195 SendRequestPacketNoLock (const PacketStreamType &request_packet);
Greg Clayton363be3f2011-07-15 03:27:12 +0000196
197 size_t
Greg Claytond52d00f2011-07-16 03:19:08 +0000198 WaitForPacketWithTimeoutMicroSecondsNoLock (lldb_private::DataExtractor &response,
Greg Clayton363be3f2011-07-15 03:27:12 +0000199 uint32_t timeout_usec);
200
201 bool
202 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
203
Greg Clayton1e5b0212011-07-15 16:31:38 +0000204 void
Greg Claytond52d00f2011-07-16 03:19:08 +0000205 MakeRequestPacketHeader (CommandType request_type,
206 PacketStreamType &request_packet,
207 uint16_t request_length);
Greg Clayton1e5b0212011-07-15 16:31:38 +0000208
Greg Clayton0fa51242011-07-19 03:57:15 +0000209 //------------------------------------------------------------------
210 // Protected Request Packets (use public accessors which will cache
211 // results.
212 //------------------------------------------------------------------
Greg Claytond52d00f2011-07-16 03:19:08 +0000213 bool
214 SendRequestVersion ();
215
Greg Clayton0fa51242011-07-19 03:57:15 +0000216 bool
217 SendRequestHostInfo ();
218
219
220 void
221 DumpPacket (lldb_private::Stream &s,
222 const void *data,
223 uint32_t data_len);
224
225 void
226 DumpPacket (lldb_private::Stream &s,
227 const lldb_private::DataExtractor& extractor);
Greg Claytond52d00f2011-07-16 03:19:08 +0000228
229 bool
230 VersionIsValid() const
231 {
232 return m_kdp_version_version != 0;
233 }
234
235 bool
236 HostInfoIsValid() const
237 {
238 return m_kdp_hostinfo_cpu_type != 0;
239 }
240
241 bool
Greg Clayton0fa51242011-07-19 03:57:15 +0000242 ExtractIsReply (uint8_t first_packet_byte) const
243 {
244 // TODO: handle big endian...
245 return (first_packet_byte & ePacketTypeMask) != 0;
246 }
247
248 CommandType
249 ExtractCommand (uint8_t first_packet_byte) const
250 {
251 // TODO: handle big endian...
252 return (CommandType)(first_packet_byte & eCommandTypeMask);
253 }
254
255 static const char *
256 GetCommandAsCString (uint8_t command);
Greg Claytond52d00f2011-07-16 03:19:08 +0000257
258 void
259 ClearKDPSettings ();
260
261 bool
262 SendRequestAndGetReply (const CommandType command,
263 const uint8_t request_sequence_id,
264 const PacketStreamType &request_packet,
265 lldb_private::DataExtractor &reply_packet);
Greg Clayton363be3f2011-07-15 03:27:12 +0000266 //------------------------------------------------------------------
267 // Classes that inherit from CommunicationKDP can see and modify these
268 //------------------------------------------------------------------
Greg Clayton0fa51242011-07-19 03:57:15 +0000269 uint32_t m_addr_byte_size;
Greg Claytond52d00f2011-07-16 03:19:08 +0000270 lldb::ByteOrder m_byte_order;
Greg Clayton363be3f2011-07-15 03:27:12 +0000271 uint32_t m_packet_timeout;
272 lldb_private::Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time
273 lldb_private::Predicate<bool> m_public_is_running;
274 lldb_private::Predicate<bool> m_private_is_running;
Greg Clayton1e5b0212011-07-15 16:31:38 +0000275 uint32_t m_session_key;
276 uint8_t m_request_sequence_id;
277 uint8_t m_exception_sequence_id;
Greg Claytond52d00f2011-07-16 03:19:08 +0000278 uint32_t m_kdp_version_version;
279 uint32_t m_kdp_version_feature;
280 uint32_t m_kdp_hostinfo_cpu_mask;
281 uint32_t m_kdp_hostinfo_cpu_type;
282 uint32_t m_kdp_hostinfo_cpu_subtype;
Greg Clayton363be3f2011-07-15 03:27:12 +0000283private:
284 //------------------------------------------------------------------
285 // For CommunicationKDP only
286 //------------------------------------------------------------------
287 DISALLOW_COPY_AND_ASSIGN (CommunicationKDP);
288};
289
290#endif // liblldb_CommunicationKDP_h_