blob: f53b190522f3888bc942c971c827922ec1ec491c [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 Molendaca2ffa72013-05-09 23:52:21 +0000236 bool
237 RemoteIsDarwinKernel ();
238
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000239 lldb::addr_t
240 GetLoadAddress ();
241
Greg Clayton07e66e32011-07-20 03:41:06 +0000242 bool
Greg Clayton97d5cf02012-09-25 02:40:06 +0000243 SendRequestResume ();
Greg Clayton07e66e32011-07-20 03:41:06 +0000244
245 bool
246 SendRequestSuspend ();
247
248 bool
249 SendRequestBreakpoint (bool set, lldb::addr_t addr);
250
Greg Claytonf9765ac2011-07-15 03:27:12 +0000251protected:
Greg Claytonf9765ac2011-07-15 03:27:12 +0000252
Greg Clayton57508022011-07-15 16:31:38 +0000253 bool
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000254 SendRequestPacketNoLock (const PacketStreamType &request_packet);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000255
256 size_t
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000257 WaitForPacketWithTimeoutMicroSecondsNoLock (lldb_private::DataExtractor &response,
Greg Claytonf9765ac2011-07-15 03:27:12 +0000258 uint32_t timeout_usec);
259
260 bool
261 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
262
Greg Clayton57508022011-07-15 16:31:38 +0000263 void
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000264 MakeRequestPacketHeader (CommandType request_type,
265 PacketStreamType &request_packet,
266 uint16_t request_length);
Greg Clayton57508022011-07-15 16:31:38 +0000267
Greg Claytona63d08c2011-07-19 03:57:15 +0000268 //------------------------------------------------------------------
269 // Protected Request Packets (use public accessors which will cache
270 // results.
271 //------------------------------------------------------------------
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000272 bool
273 SendRequestVersion ();
274
Greg Claytona63d08c2011-07-19 03:57:15 +0000275 bool
276 SendRequestHostInfo ();
277
Greg Clayton07e66e32011-07-20 03:41:06 +0000278 bool
279 SendRequestKernelVersion ();
Greg Claytona63d08c2011-07-19 03:57:15 +0000280
Greg Clayton5b882162011-07-21 01:12:01 +0000281 // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
282 //bool
283 //SendRequestImagePath ();
284
Greg Claytona63d08c2011-07-19 03:57:15 +0000285 void
286 DumpPacket (lldb_private::Stream &s,
287 const void *data,
288 uint32_t data_len);
289
290 void
291 DumpPacket (lldb_private::Stream &s,
292 const lldb_private::DataExtractor& extractor);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000293
294 bool
295 VersionIsValid() const
296 {
297 return m_kdp_version_version != 0;
298 }
299
300 bool
301 HostInfoIsValid() const
302 {
303 return m_kdp_hostinfo_cpu_type != 0;
304 }
305
306 bool
Greg Claytona63d08c2011-07-19 03:57:15 +0000307 ExtractIsReply (uint8_t first_packet_byte) const
308 {
309 // TODO: handle big endian...
310 return (first_packet_byte & ePacketTypeMask) != 0;
311 }
312
313 CommandType
314 ExtractCommand (uint8_t first_packet_byte) const
315 {
316 // TODO: handle big endian...
317 return (CommandType)(first_packet_byte & eCommandTypeMask);
318 }
319
320 static const char *
321 GetCommandAsCString (uint8_t command);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000322
323 void
324 ClearKDPSettings ();
325
326 bool
327 SendRequestAndGetReply (const CommandType command,
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000328 const PacketStreamType &request_packet,
329 lldb_private::DataExtractor &reply_packet);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000330 //------------------------------------------------------------------
331 // Classes that inherit from CommunicationKDP can see and modify these
332 //------------------------------------------------------------------
Greg Claytona63d08c2011-07-19 03:57:15 +0000333 uint32_t m_addr_byte_size;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000334 lldb::ByteOrder m_byte_order;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000335 uint32_t m_packet_timeout;
336 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 +0000337 lldb_private::Predicate<bool> m_is_running;
Greg Clayton57508022011-07-15 16:31:38 +0000338 uint32_t m_session_key;
339 uint8_t m_request_sequence_id;
340 uint8_t m_exception_sequence_id;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000341 uint32_t m_kdp_version_version;
342 uint32_t m_kdp_version_feature;
343 uint32_t m_kdp_hostinfo_cpu_mask;
344 uint32_t m_kdp_hostinfo_cpu_type;
345 uint32_t m_kdp_hostinfo_cpu_subtype;
Greg Clayton07e66e32011-07-20 03:41:06 +0000346 std::string m_kernel_version;
Greg Clayton5b882162011-07-21 01:12:01 +0000347 //std::string m_image_path; // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
Greg Clayton07e66e32011-07-20 03:41:06 +0000348 lldb::addr_t m_last_read_memory_addr; // Last memory read address for logging
Greg Claytonf9765ac2011-07-15 03:27:12 +0000349private:
350 //------------------------------------------------------------------
351 // For CommunicationKDP only
352 //------------------------------------------------------------------
353 DISALLOW_COPY_AND_ASSIGN (CommunicationKDP);
354};
355
356#endif // liblldb_CommunicationKDP_h_