blob: e4e92c82072876c70b6ba98b1c2f3927a3ffd707 [file] [log] [blame]
Greg Clayton59ec5122011-07-15 18:02:58 +00001//===-- CommunicationKDP.h --------------------------------------*- C++ -*-===//
Greg Claytonf9765ac2011-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 Clayton4df8ddf2011-07-16 03:19:08 +000023#include "lldb/Core/StreamBuffer.h"
Greg Claytonf9765ac2011-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 Claytonf9765ac2011-07-15 03:27:12 +000028class CommunicationKDP : public lldb_private::Communication
29{
30public:
31 enum
32 {
33 eBroadcastBitRunPacketSent = kLoUserBroadcastBit
34 };
Greg Clayton57508022011-07-15 16:31:38 +000035
36 const static uint32_t kMaxPacketSize = 1200;
37 const static uint32_t kMaxDataSize = 1024;
Greg Clayton4df8ddf2011-07-16 03:19:08 +000038 typedef lldb_private::StreamBuffer<1024> PacketStreamType;
Greg Clayton57508022011-07-15 16:31:38 +000039 typedef enum
40 {
Greg Clayton4df8ddf2011-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 Clayton57508022011-07-15 16:31:38 +000067
Greg Clayton07e66e32011-07-20 03:41:06 +000068 enum
69 {
70 eFeatureLocalBreakpointsSupported = (1u << 0),
71 };
72
Greg Clayton4df8ddf2011-07-16 03:19:08 +000073 typedef enum
Greg Clayton57508022011-07-15 16:31:38 +000074 {
Greg Clayton4df8ddf2011-07-16 03:19:08 +000075 KDP_PROTERR_SUCCESS = 0,
76 KDP_PROTERR_ALREADY_CONNECTED,
77 KDP_PROTERR_BAD_NBYTES,
78 KDP_PROTERR_BADFLAVOR
79 } KDPError;
Greg Clayton57508022011-07-15 16:31:38 +000080
81 typedef enum
82 {
Greg Clayton4df8ddf2011-07-16 03:19:08 +000083 ePacketTypeRequest = 0x00u,
84 ePacketTypeReply = 0x80u,
85 ePacketTypeMask = 0x80u,
86 eCommandTypeMask = 0x7fu
Greg Clayton57508022011-07-15 16:31:38 +000087 } PacketType;
Greg Claytonf9765ac2011-07-15 03:27:12 +000088 //------------------------------------------------------------------
89 // Constructors and Destructors
90 //------------------------------------------------------------------
91 CommunicationKDP (const char *comm_name);
92
93 virtual
94 ~CommunicationKDP();
95
Greg Clayton57508022011-07-15 16:31:38 +000096 bool
Greg Clayton4df8ddf2011-07-16 03:19:08 +000097 SendRequestPacket (const PacketStreamType &request_packet);
Greg Claytonf9765ac2011-07-15 03:27:12 +000098
99 // Wait for a packet within 'nsec' seconds
100 size_t
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000101 WaitForPacketWithTimeoutMicroSeconds (lldb_private::DataExtractor &response,
Greg Claytonf9765ac2011-07-15 03:27:12 +0000102 uint32_t usec);
103
Greg Claytonf9765ac2011-07-15 03:27:12 +0000104 bool
105 GetSequenceMutex(lldb_private::Mutex::Locker& locker);
106
107 bool
108 CheckForPacket (const uint8_t *src,
109 size_t src_len,
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000110 lldb_private::DataExtractor &packet);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000111 bool
112 IsRunning() const
113 {
114 return m_public_is_running.GetValue();
115 }
116
Greg Claytonf9765ac2011-07-15 03:27:12 +0000117 //------------------------------------------------------------------
118 // Set the global packet timeout.
119 //
120 // For clients, this is the timeout that gets used when sending
121 // packets and waiting for responses. For servers, this might not
122 // get used, and if it doesn't this should be moved to the
123 // CommunicationKDPClient.
124 //------------------------------------------------------------------
125 uint32_t
126 SetPacketTimeout (uint32_t packet_timeout)
127 {
128 const uint32_t old_packet_timeout = m_packet_timeout;
129 m_packet_timeout = packet_timeout;
130 return old_packet_timeout;
131 }
132
133 uint32_t
134 GetPacketTimeoutInMicroSeconds () const
135 {
136 return m_packet_timeout * lldb_private::TimeValue::MicroSecPerSec;
137 }
138 //------------------------------------------------------------------
139 // Start a debugserver instance on the current host using the
140 // supplied connection URL.
141 //------------------------------------------------------------------
142 lldb_private::Error
143 StartDebugserverProcess (const char *connect_url,
144 const char *unix_socket_name,
145 lldb_private::ProcessLaunchInfo &launch_info);
146
147
Greg Claytona63d08c2011-07-19 03:57:15 +0000148 //------------------------------------------------------------------
149 // Public Request Packets
150 //------------------------------------------------------------------
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000151 bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000152 SendRequestConnect (uint16_t reply_port,
153 uint16_t exc_port,
154 const char *greeting);
Greg Clayton57508022011-07-15 16:31:38 +0000155
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000156 bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000157 SendRequestReattach (uint16_t reply_port);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000158
159 bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000160 SendRequestDisconnect ();
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000161
162 uint32_t
Greg Claytona63d08c2011-07-19 03:57:15 +0000163 SendRequestReadMemory (lldb::addr_t addr,
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000164 void *dst,
165 uint32_t dst_size,
Greg Claytona63d08c2011-07-19 03:57:15 +0000166 lldb_private::Error &error);
167
168 uint32_t
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000169 SendRequestWriteMemory (lldb::addr_t addr,
170 const void *src,
171 uint32_t src_len,
172 lldb_private::Error &error);
173
174 uint32_t
Greg Claytona63d08c2011-07-19 03:57:15 +0000175 SendRequestReadRegisters (uint32_t cpu,
176 uint32_t flavor,
177 void *dst,
178 uint32_t dst_size,
179 lldb_private::Error &error);
Greg Claytona63d08c2011-07-19 03:57:15 +0000180
Greg Clayton07e66e32011-07-20 03:41:06 +0000181 const char *
182 GetKernelVersion ();
183
Greg Claytona63d08c2011-07-19 03:57:15 +0000184 uint32_t
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000185 GetVersion ();
186
187 uint32_t
188 GetFeatureFlags ();
189
Greg Clayton07e66e32011-07-20 03:41:06 +0000190 bool
191 LocalBreakpointsAreSupported ()
192 {
193 return (GetFeatureFlags() & eFeatureLocalBreakpointsSupported) != 0;
194 }
195
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000196 uint32_t
197 GetCPUMask ();
198
199 uint32_t
200 GetCPUType ();
201
202 uint32_t
203 GetCPUSubtype ();
Greg Clayton57508022011-07-15 16:31:38 +0000204
Greg Clayton07e66e32011-07-20 03:41:06 +0000205 // If cpu_mask is zero, then we will resume all CPUs
206 bool
207 SendRequestResume (uint32_t cpu_mask = 0);
208
209 bool
210 SendRequestSuspend ();
211
212 bool
213 SendRequestBreakpoint (bool set, lldb::addr_t addr);
214
Greg Claytonf9765ac2011-07-15 03:27:12 +0000215protected:
216 typedef std::list<std::string> packet_collection;
217
Greg Clayton57508022011-07-15 16:31:38 +0000218 bool
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000219 SendRequestPacketNoLock (const PacketStreamType &request_packet);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000220
221 size_t
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000222 WaitForPacketWithTimeoutMicroSecondsNoLock (lldb_private::DataExtractor &response,
Greg Claytonf9765ac2011-07-15 03:27:12 +0000223 uint32_t timeout_usec);
224
225 bool
226 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
227
Greg Clayton57508022011-07-15 16:31:38 +0000228 void
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000229 MakeRequestPacketHeader (CommandType request_type,
230 PacketStreamType &request_packet,
231 uint16_t request_length);
Greg Clayton57508022011-07-15 16:31:38 +0000232
Greg Claytona63d08c2011-07-19 03:57:15 +0000233 //------------------------------------------------------------------
234 // Protected Request Packets (use public accessors which will cache
235 // results.
236 //------------------------------------------------------------------
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000237 bool
238 SendRequestVersion ();
239
Greg Claytona63d08c2011-07-19 03:57:15 +0000240 bool
241 SendRequestHostInfo ();
242
Greg Clayton07e66e32011-07-20 03:41:06 +0000243 bool
244 SendRequestKernelVersion ();
Greg Claytona63d08c2011-07-19 03:57:15 +0000245
246 void
247 DumpPacket (lldb_private::Stream &s,
248 const void *data,
249 uint32_t data_len);
250
251 void
252 DumpPacket (lldb_private::Stream &s,
253 const lldb_private::DataExtractor& extractor);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000254
255 bool
256 VersionIsValid() const
257 {
258 return m_kdp_version_version != 0;
259 }
260
261 bool
262 HostInfoIsValid() const
263 {
264 return m_kdp_hostinfo_cpu_type != 0;
265 }
266
267 bool
Greg Claytona63d08c2011-07-19 03:57:15 +0000268 ExtractIsReply (uint8_t first_packet_byte) const
269 {
270 // TODO: handle big endian...
271 return (first_packet_byte & ePacketTypeMask) != 0;
272 }
273
274 CommandType
275 ExtractCommand (uint8_t first_packet_byte) const
276 {
277 // TODO: handle big endian...
278 return (CommandType)(first_packet_byte & eCommandTypeMask);
279 }
280
281 static const char *
282 GetCommandAsCString (uint8_t command);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000283
284 void
285 ClearKDPSettings ();
286
287 bool
288 SendRequestAndGetReply (const CommandType command,
289 const uint8_t request_sequence_id,
290 const PacketStreamType &request_packet,
291 lldb_private::DataExtractor &reply_packet);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000292 //------------------------------------------------------------------
293 // Classes that inherit from CommunicationKDP can see and modify these
294 //------------------------------------------------------------------
Greg Claytona63d08c2011-07-19 03:57:15 +0000295 uint32_t m_addr_byte_size;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000296 lldb::ByteOrder m_byte_order;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000297 uint32_t m_packet_timeout;
298 lldb_private::Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time
299 lldb_private::Predicate<bool> m_public_is_running;
300 lldb_private::Predicate<bool> m_private_is_running;
Greg Clayton57508022011-07-15 16:31:38 +0000301 uint32_t m_session_key;
302 uint8_t m_request_sequence_id;
303 uint8_t m_exception_sequence_id;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000304 uint32_t m_kdp_version_version;
305 uint32_t m_kdp_version_feature;
306 uint32_t m_kdp_hostinfo_cpu_mask;
307 uint32_t m_kdp_hostinfo_cpu_type;
308 uint32_t m_kdp_hostinfo_cpu_subtype;
Greg Clayton07e66e32011-07-20 03:41:06 +0000309 std::string m_kernel_version;
310 lldb::addr_t m_last_read_memory_addr; // Last memory read address for logging
Greg Claytonf9765ac2011-07-15 03:27:12 +0000311private:
312 //------------------------------------------------------------------
313 // For CommunicationKDP only
314 //------------------------------------------------------------------
315 DISALLOW_COPY_AND_ASSIGN (CommunicationKDP);
316};
317
318#endif // liblldb_CommunicationKDP_h_