blob: 03e1856bf504403a32e91680100561752f1dd840 [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,
Enrico Granata02b66762011-08-19 01:14:49 +000065 KDP_KERNELVERSION
Greg Clayton4df8ddf2011-07-16 03:19:08 +000066 } CommandType;
Greg Clayton57508022011-07-15 16:31:38 +000067
Greg Clayton07e66e32011-07-20 03:41:06 +000068 enum
69 {
Enrico Granata02b66762011-08-19 01:14:49 +000070 KDP_FEATURE_BP = (1u << 0)
Greg Clayton07e66e32011-07-20 03:41:06 +000071 };
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
Greg Clayton37a0a242012-04-11 00:24:49 +0000105 GetSequenceMutex(lldb_private::Mutex::Locker& locker);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000106
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 {
Greg Clayton97d5cf02012-09-25 02:40:06 +0000114 return m_is_running.GetValue();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000115 }
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 Clayton4b1b8b32012-09-21 01:55:30 +0000180
181 uint32_t
182 SendRequestWriteRegisters (uint32_t cpu,
183 uint32_t flavor,
184 const void *src,
185 uint32_t src_size,
186 lldb_private::Error &error);
187
Greg Clayton07e66e32011-07-20 03:41:06 +0000188 const char *
189 GetKernelVersion ();
190
Greg Clayton5b882162011-07-21 01:12:01 +0000191 // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
192 // const char *
193 // GetImagePath ();
194
Greg Claytona63d08c2011-07-19 03:57:15 +0000195 uint32_t
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000196 GetVersion ();
197
198 uint32_t
199 GetFeatureFlags ();
200
Greg Clayton07e66e32011-07-20 03:41:06 +0000201 bool
202 LocalBreakpointsAreSupported ()
203 {
Greg Clayton5b882162011-07-21 01:12:01 +0000204 return (GetFeatureFlags() & KDP_FEATURE_BP) != 0;
Greg Clayton07e66e32011-07-20 03:41:06 +0000205 }
206
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000207 uint32_t
208 GetCPUMask ();
209
210 uint32_t
211 GetCPUType ();
212
213 uint32_t
214 GetCPUSubtype ();
Greg Clayton57508022011-07-15 16:31:38 +0000215
Greg Clayton07e66e32011-07-20 03:41:06 +0000216 bool
Greg Clayton97d5cf02012-09-25 02:40:06 +0000217 SendRequestResume ();
Greg Clayton07e66e32011-07-20 03:41:06 +0000218
219 bool
220 SendRequestSuspend ();
221
222 bool
223 SendRequestBreakpoint (bool set, lldb::addr_t addr);
224
Greg Claytonf9765ac2011-07-15 03:27:12 +0000225protected:
Greg Claytonf9765ac2011-07-15 03:27:12 +0000226
Greg Clayton57508022011-07-15 16:31:38 +0000227 bool
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000228 SendRequestPacketNoLock (const PacketStreamType &request_packet);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000229
230 size_t
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000231 WaitForPacketWithTimeoutMicroSecondsNoLock (lldb_private::DataExtractor &response,
Greg Claytonf9765ac2011-07-15 03:27:12 +0000232 uint32_t timeout_usec);
233
234 bool
235 WaitForNotRunningPrivate (const lldb_private::TimeValue *timeout_ptr);
236
Greg Clayton57508022011-07-15 16:31:38 +0000237 void
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000238 MakeRequestPacketHeader (CommandType request_type,
239 PacketStreamType &request_packet,
240 uint16_t request_length);
Greg Clayton57508022011-07-15 16:31:38 +0000241
Greg Claytona63d08c2011-07-19 03:57:15 +0000242 //------------------------------------------------------------------
243 // Protected Request Packets (use public accessors which will cache
244 // results.
245 //------------------------------------------------------------------
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000246 bool
247 SendRequestVersion ();
248
Greg Claytona63d08c2011-07-19 03:57:15 +0000249 bool
250 SendRequestHostInfo ();
251
Greg Clayton07e66e32011-07-20 03:41:06 +0000252 bool
253 SendRequestKernelVersion ();
Greg Claytona63d08c2011-07-19 03:57:15 +0000254
Greg Clayton5b882162011-07-21 01:12:01 +0000255 // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
256 //bool
257 //SendRequestImagePath ();
258
Greg Claytona63d08c2011-07-19 03:57:15 +0000259 void
260 DumpPacket (lldb_private::Stream &s,
261 const void *data,
262 uint32_t data_len);
263
264 void
265 DumpPacket (lldb_private::Stream &s,
266 const lldb_private::DataExtractor& extractor);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000267
268 bool
269 VersionIsValid() const
270 {
271 return m_kdp_version_version != 0;
272 }
273
274 bool
275 HostInfoIsValid() const
276 {
277 return m_kdp_hostinfo_cpu_type != 0;
278 }
279
280 bool
Greg Claytona63d08c2011-07-19 03:57:15 +0000281 ExtractIsReply (uint8_t first_packet_byte) const
282 {
283 // TODO: handle big endian...
284 return (first_packet_byte & ePacketTypeMask) != 0;
285 }
286
287 CommandType
288 ExtractCommand (uint8_t first_packet_byte) const
289 {
290 // TODO: handle big endian...
291 return (CommandType)(first_packet_byte & eCommandTypeMask);
292 }
293
294 static const char *
295 GetCommandAsCString (uint8_t command);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000296
297 void
298 ClearKDPSettings ();
299
300 bool
301 SendRequestAndGetReply (const CommandType command,
302 const uint8_t request_sequence_id,
303 const PacketStreamType &request_packet,
304 lldb_private::DataExtractor &reply_packet);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000305 //------------------------------------------------------------------
306 // Classes that inherit from CommunicationKDP can see and modify these
307 //------------------------------------------------------------------
Greg Claytona63d08c2011-07-19 03:57:15 +0000308 uint32_t m_addr_byte_size;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000309 lldb::ByteOrder m_byte_order;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000310 uint32_t m_packet_timeout;
311 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 +0000312 lldb_private::Predicate<bool> m_is_running;
Greg Clayton57508022011-07-15 16:31:38 +0000313 uint32_t m_session_key;
314 uint8_t m_request_sequence_id;
315 uint8_t m_exception_sequence_id;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000316 uint32_t m_kdp_version_version;
317 uint32_t m_kdp_version_feature;
318 uint32_t m_kdp_hostinfo_cpu_mask;
319 uint32_t m_kdp_hostinfo_cpu_type;
320 uint32_t m_kdp_hostinfo_cpu_subtype;
Greg Clayton07e66e32011-07-20 03:41:06 +0000321 std::string m_kernel_version;
Greg Clayton5b882162011-07-21 01:12:01 +0000322 //std::string m_image_path; // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
Greg Clayton07e66e32011-07-20 03:41:06 +0000323 lldb::addr_t m_last_read_memory_addr; // Last memory read address for logging
Greg Claytonf9765ac2011-07-15 03:27:12 +0000324private:
325 //------------------------------------------------------------------
326 // For CommunicationKDP only
327 //------------------------------------------------------------------
328 DISALLOW_COPY_AND_ASSIGN (CommunicationKDP);
329};
330
331#endif // liblldb_CommunicationKDP_h_