blob: 6b61f4422dde2cd7b8ac5b3fa26445654777045a [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 Clayton5b882162011-07-21 01:12:01 +000041 KDP_CONNECT = 0u,
42 KDP_DISCONNECT,
43 KDP_HOSTINFO,
44 KDP_VERSION,
45 KDP_MAXBYTES,
46 KDP_READMEM,
47 KDP_WRITEMEM,
48 KDP_READREGS,
49 KDP_WRITEREGS,
50 KDP_LOAD,
51 KDP_IMAGEPATH,
52 KDP_SUSPEND,
53 KDP_RESUMECPUS,
54 KDP_EXCEPTION,
55 KDP_TERMINATION,
56 KDP_BREAKPOINT_SET,
57 KDP_BREAKPOINT_REMOVE,
58 KDP_REGIONS,
59 KDP_REATTACH,
60 KDP_HOSTREBOOT,
61 KDP_READMEM64,
62 KDP_WRITEMEM64,
63 KDP_BREAKPOINT_SET64,
64 KDP_BREAKPOINT_REMOVE64,
Jason Molendace62fd72013-03-01 00:43:19 +000065 KDP_KERNELVERSION,
66 KDP_READPHYSMEM64,
67 KDP_WRITEPHYSMEM64,
68 KDP_READIOPORT,
69 KDP_WRITEIOPORT,
70 KDP_READMSR64,
71 KDP_WRITEMSR64,
72 KDP_DUMPINFO
Greg Clayton4df8ddf2011-07-16 03:19:08 +000073 } CommandType;
Greg Clayton57508022011-07-15 16:31:38 +000074
Greg Clayton07e66e32011-07-20 03:41:06 +000075 enum
76 {
Enrico Granata02b66762011-08-19 01:14:49 +000077 KDP_FEATURE_BP = (1u << 0)
Greg Clayton07e66e32011-07-20 03:41:06 +000078 };
79
Greg Clayton4df8ddf2011-07-16 03:19:08 +000080 typedef enum
Greg Clayton57508022011-07-15 16:31:38 +000081 {
Greg Clayton4df8ddf2011-07-16 03:19:08 +000082 KDP_PROTERR_SUCCESS = 0,
83 KDP_PROTERR_ALREADY_CONNECTED,
84 KDP_PROTERR_BAD_NBYTES,
85 KDP_PROTERR_BADFLAVOR
86 } KDPError;
Greg Clayton57508022011-07-15 16:31:38 +000087
88 typedef enum
89 {
Greg Clayton4df8ddf2011-07-16 03:19:08 +000090 ePacketTypeRequest = 0x00u,
91 ePacketTypeReply = 0x80u,
92 ePacketTypeMask = 0x80u,
93 eCommandTypeMask = 0x7fu
Greg Clayton57508022011-07-15 16:31:38 +000094 } PacketType;
Greg Claytonf9765ac2011-07-15 03:27:12 +000095 //------------------------------------------------------------------
96 // Constructors and Destructors
97 //------------------------------------------------------------------
98 CommunicationKDP (const char *comm_name);
99
100 virtual
101 ~CommunicationKDP();
102
Greg Clayton57508022011-07-15 16:31:38 +0000103 bool
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000104 SendRequestPacket (const PacketStreamType &request_packet);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000105
106 // Wait for a packet within 'nsec' seconds
107 size_t
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000108 WaitForPacketWithTimeoutMicroSeconds (lldb_private::DataExtractor &response,
Greg Claytonf9765ac2011-07-15 03:27:12 +0000109 uint32_t usec);
110
Greg Claytonf9765ac2011-07-15 03:27:12 +0000111 bool
Greg Clayton37a0a242012-04-11 00:24:49 +0000112 GetSequenceMutex(lldb_private::Mutex::Locker& locker);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000113
114 bool
115 CheckForPacket (const uint8_t *src,
116 size_t src_len,
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000117 lldb_private::DataExtractor &packet);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000118 bool
119 IsRunning() const
120 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000121 return m_is_running.GetValue();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000122 }
123
Greg Claytonf9765ac2011-07-15 03:27:12 +0000124 //------------------------------------------------------------------
125 // Set the global packet timeout.
126 //
127 // For clients, this is the timeout that gets used when sending
128 // packets and waiting for responses. For servers, this might not
129 // get used, and if it doesn't this should be moved to the
130 // CommunicationKDPClient.
131 //------------------------------------------------------------------
132 uint32_t
133 SetPacketTimeout (uint32_t packet_timeout)
134 {
135 const uint32_t old_packet_timeout = m_packet_timeout;
136 m_packet_timeout = packet_timeout;
137 return old_packet_timeout;
138 }
139
140 uint32_t
141 GetPacketTimeoutInMicroSeconds () const
142 {
143 return m_packet_timeout * lldb_private::TimeValue::MicroSecPerSec;
144 }
145 //------------------------------------------------------------------
146 // Start a debugserver instance on the current host using the
147 // supplied connection URL.
148 //------------------------------------------------------------------
149 lldb_private::Error
150 StartDebugserverProcess (const char *connect_url,
151 const char *unix_socket_name,
152 lldb_private::ProcessLaunchInfo &launch_info);
153
154
Greg Claytona63d08c2011-07-19 03:57:15 +0000155 //------------------------------------------------------------------
156 // Public Request Packets
157 //------------------------------------------------------------------
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000158 bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000159 SendRequestConnect (uint16_t reply_port,
160 uint16_t exc_port,
161 const char *greeting);
Greg Clayton57508022011-07-15 16:31:38 +0000162
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000163 bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000164 SendRequestReattach (uint16_t reply_port);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000165
166 bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000167 SendRequestDisconnect ();
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000168
169 uint32_t
Greg Claytona63d08c2011-07-19 03:57:15 +0000170 SendRequestReadMemory (lldb::addr_t addr,
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000171 void *dst,
172 uint32_t dst_size,
Greg Claytona63d08c2011-07-19 03:57:15 +0000173 lldb_private::Error &error);
174
175 uint32_t
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000176 SendRequestWriteMemory (lldb::addr_t addr,
177 const void *src,
178 uint32_t src_len,
179 lldb_private::Error &error);
180
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000181 bool
182 SendRawRequest (uint8_t command_byte,
183 const void *src,
184 uint32_t src_len,
185 lldb_private::DataExtractor &reply,
186 lldb_private::Error &error);
187
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000188 uint32_t
Greg Claytona63d08c2011-07-19 03:57:15 +0000189 SendRequestReadRegisters (uint32_t cpu,
190 uint32_t flavor,
191 void *dst,
192 uint32_t dst_size,
193 lldb_private::Error &error);
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000194
195 uint32_t
196 SendRequestWriteRegisters (uint32_t cpu,
197 uint32_t flavor,
198 const void *src,
199 uint32_t src_size,
200 lldb_private::Error &error);
201
Greg Clayton07e66e32011-07-20 03:41:06 +0000202 const char *
203 GetKernelVersion ();
204
Greg Clayton5b882162011-07-21 01:12:01 +0000205 // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
206 // const char *
207 // GetImagePath ();
208
Greg Claytona63d08c2011-07-19 03:57:15 +0000209 uint32_t
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000210 GetVersion ();
211
212 uint32_t
213 GetFeatureFlags ();
214
Greg Clayton07e66e32011-07-20 03:41:06 +0000215 bool
216 LocalBreakpointsAreSupported ()
217 {
Greg Clayton5b882162011-07-21 01:12:01 +0000218 return (GetFeatureFlags() & KDP_FEATURE_BP) != 0;
Greg Clayton07e66e32011-07-20 03:41:06 +0000219 }
220
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000221 uint32_t
222 GetCPUMask ();
223
224 uint32_t
225 GetCPUType ();
226
227 uint32_t
228 GetCPUSubtype ();
Greg Clayton57508022011-07-15 16:31:38 +0000229
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000230 lldb_private::UUID
231 GetUUID ();
232
Jason Molenda840f12c2012-10-25 00:25:13 +0000233 bool
234 RemoteIsEFI ();
235
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000236 lldb::addr_t
237 GetLoadAddress ();
238
Greg Clayton07e66e32011-07-20 03:41:06 +0000239 bool
Greg Clayton97d5cf02012-09-25 02:40:06 +0000240 SendRequestResume ();
Greg Clayton07e66e32011-07-20 03:41:06 +0000241
242 bool
243 SendRequestSuspend ();
244
245 bool
246 SendRequestBreakpoint (bool set, lldb::addr_t addr);
247
Greg Claytonf9765ac2011-07-15 03:27:12 +0000248protected:
Greg Claytonf9765ac2011-07-15 03:27:12 +0000249
Greg Clayton57508022011-07-15 16:31:38 +0000250 bool
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000251 SendRequestPacketNoLock (const PacketStreamType &request_packet);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000252
253 size_t
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000254 WaitForPacketWithTimeoutMicroSecondsNoLock (lldb_private::DataExtractor &response,
Greg Claytonf9765ac2011-07-15 03:27:12 +0000255 uint32_t timeout_usec);
256
257 bool
258 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
259
Greg Clayton57508022011-07-15 16:31:38 +0000260 void
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000261 MakeRequestPacketHeader (CommandType request_type,
262 PacketStreamType &request_packet,
263 uint16_t request_length);
Greg Clayton57508022011-07-15 16:31:38 +0000264
Greg Claytona63d08c2011-07-19 03:57:15 +0000265 //------------------------------------------------------------------
266 // Protected Request Packets (use public accessors which will cache
267 // results.
268 //------------------------------------------------------------------
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000269 bool
270 SendRequestVersion ();
271
Greg Claytona63d08c2011-07-19 03:57:15 +0000272 bool
273 SendRequestHostInfo ();
274
Greg Clayton07e66e32011-07-20 03:41:06 +0000275 bool
276 SendRequestKernelVersion ();
Greg Claytona63d08c2011-07-19 03:57:15 +0000277
Greg Clayton5b882162011-07-21 01:12:01 +0000278 // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
279 //bool
280 //SendRequestImagePath ();
281
Greg Claytona63d08c2011-07-19 03:57:15 +0000282 void
283 DumpPacket (lldb_private::Stream &s,
284 const void *data,
285 uint32_t data_len);
286
287 void
288 DumpPacket (lldb_private::Stream &s,
289 const lldb_private::DataExtractor& extractor);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000290
291 bool
292 VersionIsValid() const
293 {
294 return m_kdp_version_version != 0;
295 }
296
297 bool
298 HostInfoIsValid() const
299 {
300 return m_kdp_hostinfo_cpu_type != 0;
301 }
302
303 bool
Greg Claytona63d08c2011-07-19 03:57:15 +0000304 ExtractIsReply (uint8_t first_packet_byte) const
305 {
306 // TODO: handle big endian...
307 return (first_packet_byte & ePacketTypeMask) != 0;
308 }
309
310 CommandType
311 ExtractCommand (uint8_t first_packet_byte) const
312 {
313 // TODO: handle big endian...
314 return (CommandType)(first_packet_byte & eCommandTypeMask);
315 }
316
317 static const char *
318 GetCommandAsCString (uint8_t command);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000319
320 void
321 ClearKDPSettings ();
322
323 bool
324 SendRequestAndGetReply (const CommandType command,
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000325 const PacketStreamType &request_packet,
326 lldb_private::DataExtractor &reply_packet);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000327 //------------------------------------------------------------------
328 // Classes that inherit from CommunicationKDP can see and modify these
329 //------------------------------------------------------------------
Greg Claytona63d08c2011-07-19 03:57:15 +0000330 uint32_t m_addr_byte_size;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000331 lldb::ByteOrder m_byte_order;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000332 uint32_t m_packet_timeout;
333 lldb_private::Mutex m_sequence_mutex; // Restrict access to sending/receiving packets to a single thread at a time
Greg Clayton97d5cf02012-09-25 02:40:06 +0000334 lldb_private::Predicate<bool> m_is_running;
Greg Clayton57508022011-07-15 16:31:38 +0000335 uint32_t m_session_key;
336 uint8_t m_request_sequence_id;
337 uint8_t m_exception_sequence_id;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000338 uint32_t m_kdp_version_version;
339 uint32_t m_kdp_version_feature;
340 uint32_t m_kdp_hostinfo_cpu_mask;
341 uint32_t m_kdp_hostinfo_cpu_type;
342 uint32_t m_kdp_hostinfo_cpu_subtype;
Greg Clayton07e66e32011-07-20 03:41:06 +0000343 std::string m_kernel_version;
Greg Clayton5b882162011-07-21 01:12:01 +0000344 //std::string m_image_path; // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
Greg Clayton07e66e32011-07-20 03:41:06 +0000345 lldb::addr_t m_last_read_memory_addr; // Last memory read address for logging
Greg Claytonf9765ac2011-07-15 03:27:12 +0000346private:
347 //------------------------------------------------------------------
348 // For CommunicationKDP only
349 //------------------------------------------------------------------
350 DISALLOW_COPY_AND_ASSIGN (CommunicationKDP);
351};
352
353#endif // liblldb_CommunicationKDP_h_