blob: 230dc87b31079af86679f352fe5d086a124aee5e [file] [log] [blame]
Greg Clayton269f91e2011-07-15 18:02:58 +00001//===-- CommunicationKDP.cpp ------------------------------------*- 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
11#include "CommunicationKDP.h"
12
13// C Includes
14#include <limits.h>
15#include <string.h>
16
17// C++ Includes
Greg Clayton0fa51242011-07-19 03:57:15 +000018#include "llvm/Support/MachO.h"
19
Greg Clayton363be3f2011-07-15 03:27:12 +000020// Other libraries and framework includes
Greg Claytond52d00f2011-07-16 03:19:08 +000021#include "lldb/Core/DataBufferHeap.h"
Greg Clayton1e5b0212011-07-15 16:31:38 +000022#include "lldb/Core/DataExtractor.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000023#include "lldb/Core/Log.h"
Greg Clayton0fa51242011-07-19 03:57:15 +000024#include "lldb/Core/State.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000025#include "lldb/Host/FileSpec.h"
26#include "lldb/Host/Host.h"
27#include "lldb/Host/TimeValue.h"
28#include "lldb/Target/Process.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000029
30// Project includes
31#include "ProcessKDPLog.h"
32
33#define DEBUGSERVER_BASENAME "debugserver"
34
35using namespace lldb;
36using namespace lldb_private;
37
38//----------------------------------------------------------------------
39// CommunicationKDP constructor
40//----------------------------------------------------------------------
41CommunicationKDP::CommunicationKDP (const char *comm_name) :
42 Communication(comm_name),
Greg Clayton0fa51242011-07-19 03:57:15 +000043 m_addr_byte_size (4),
Greg Claytond52d00f2011-07-16 03:19:08 +000044 m_byte_order (eByteOrderLittle),
Greg Clayton363be3f2011-07-15 03:27:12 +000045 m_packet_timeout (1),
46 m_sequence_mutex (Mutex::eMutexTypeRecursive),
47 m_public_is_running (false),
48 m_private_is_running (false),
Greg Claytond52d00f2011-07-16 03:19:08 +000049 m_session_key (0u),
50 m_request_sequence_id (0u),
51 m_exception_sequence_id (0u),
52 m_kdp_version_version (0u),
53 m_kdp_version_feature (0u),
54 m_kdp_hostinfo_cpu_mask (0u),
55 m_kdp_hostinfo_cpu_type (0u),
56 m_kdp_hostinfo_cpu_subtype (0u)
Greg Clayton363be3f2011-07-15 03:27:12 +000057{
58}
59
60//----------------------------------------------------------------------
61// Destructor
62//----------------------------------------------------------------------
63CommunicationKDP::~CommunicationKDP()
64{
65 if (IsConnected())
66 {
67 Disconnect();
68 }
69}
70
Greg Clayton1e5b0212011-07-15 16:31:38 +000071bool
Greg Claytond52d00f2011-07-16 03:19:08 +000072CommunicationKDP::SendRequestPacket (const PacketStreamType &request_packet)
Greg Clayton363be3f2011-07-15 03:27:12 +000073{
74 Mutex::Locker locker(m_sequence_mutex);
Greg Clayton1e5b0212011-07-15 16:31:38 +000075 return SendRequestPacketNoLock (request_packet);
Greg Clayton363be3f2011-07-15 03:27:12 +000076}
77
Greg Claytond52d00f2011-07-16 03:19:08 +000078#if 0
79typedef struct {
80 uint8_t request; // Either: CommandType | ePacketTypeRequest, or CommandType | ePacketTypeReply
81 uint8_t sequence;
82 uint16_t length; // Length of entire packet including this header
83 uint32_t key; // Session key
84} kdp_hdr_t;
85#endif
86
Greg Clayton1e5b0212011-07-15 16:31:38 +000087void
Greg Claytond52d00f2011-07-16 03:19:08 +000088CommunicationKDP::MakeRequestPacketHeader (CommandType request_type,
89 PacketStreamType &request_packet,
90 uint16_t request_length)
Greg Clayton363be3f2011-07-15 03:27:12 +000091{
Greg Clayton1e5b0212011-07-15 16:31:38 +000092 request_packet.Clear();
Greg Claytond52d00f2011-07-16 03:19:08 +000093 request_packet.PutHex8 (request_type | ePacketTypeRequest); // Set the request type
94 request_packet.PutHex8 (m_request_sequence_id++); // Sequence number
95 request_packet.PutHex16 (request_length); // Length of the packet including this header
96 request_packet.PutHex32 (m_session_key); // Session key
Greg Clayton363be3f2011-07-15 03:27:12 +000097}
98
Greg Claytond52d00f2011-07-16 03:19:08 +000099bool
100CommunicationKDP::SendRequestAndGetReply (const CommandType command,
101 const uint8_t request_sequence_id,
102 const PacketStreamType &request_packet,
103 DataExtractor &reply_packet)
104{
105
106 Mutex::Locker locker(m_sequence_mutex);
107 if (SendRequestPacketNoLock(request_packet))
108 {
Greg Clayton8d2ea282011-07-17 20:36:25 +0000109 if (WaitForPacketWithTimeoutMicroSecondsNoLock (reply_packet, GetPacketTimeoutInMicroSeconds ()))
Greg Claytond52d00f2011-07-16 03:19:08 +0000110 {
111 uint32_t offset = 0;
112 const uint8_t reply_command = reply_packet.GetU8 (&offset);
113 const uint8_t reply_sequence_id = reply_packet.GetU8 (&offset);
114 if ((reply_command & eCommandTypeMask) == command)
115 {
116 if (request_sequence_id == reply_sequence_id)
117 return true;
118 }
119 }
120 }
121 reply_packet.Clear();
122 return false;
123}
Greg Clayton363be3f2011-07-15 03:27:12 +0000124
Greg Clayton1e5b0212011-07-15 16:31:38 +0000125bool
Greg Claytond52d00f2011-07-16 03:19:08 +0000126CommunicationKDP::SendRequestPacketNoLock (const PacketStreamType &request_packet)
Greg Clayton363be3f2011-07-15 03:27:12 +0000127{
128 if (IsConnected())
129 {
Greg Clayton1e5b0212011-07-15 16:31:38 +0000130 const char *packet_data = request_packet.GetData();
131 const size_t packet_size = request_packet.GetSize();
Greg Clayton363be3f2011-07-15 03:27:12 +0000132
133 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS));
134 if (log)
Greg Clayton1e5b0212011-07-15 16:31:38 +0000135 {
Greg Clayton0fa51242011-07-19 03:57:15 +0000136 PacketStreamType log_strm;
137 DumpPacket (log_strm, packet_data, packet_size);
138 log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData());
Greg Clayton1e5b0212011-07-15 16:31:38 +0000139 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000140 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton363be3f2011-07-15 03:27:12 +0000141
Greg Clayton1e5b0212011-07-15 16:31:38 +0000142 size_t bytes_written = Write (packet_data,
143 packet_size,
144 status,
145 NULL);
146
147 if (bytes_written == packet_size)
148 return true;
149
150 if (log)
151 log->Printf ("error: failed to send packet entire packet %zu of %zu bytes sent", bytes_written, packet_size);
152 }
153 return false;
Greg Clayton363be3f2011-07-15 03:27:12 +0000154}
155
156bool
157CommunicationKDP::GetSequenceMutex (Mutex::Locker& locker)
158{
159 return locker.TryLock (m_sequence_mutex.GetMutex());
160}
161
162
163bool
164CommunicationKDP::WaitForNotRunningPrivate (const TimeValue *timeout_ptr)
165{
166 return m_private_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL);
167}
168
169size_t
Greg Claytond52d00f2011-07-16 03:19:08 +0000170CommunicationKDP::WaitForPacketWithTimeoutMicroSeconds (DataExtractor &packet, uint32_t timeout_usec)
Greg Clayton363be3f2011-07-15 03:27:12 +0000171{
172 Mutex::Locker locker(m_sequence_mutex);
173 return WaitForPacketWithTimeoutMicroSecondsNoLock (packet, timeout_usec);
174}
175
176size_t
Greg Claytond52d00f2011-07-16 03:19:08 +0000177CommunicationKDP::WaitForPacketWithTimeoutMicroSecondsNoLock (DataExtractor &packet, uint32_t timeout_usec)
Greg Clayton363be3f2011-07-15 03:27:12 +0000178{
179 uint8_t buffer[8192];
180 Error error;
181
182 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS | KDP_LOG_VERBOSE));
183
184 // Check for a packet from our cache first without trying any reading...
185 if (CheckForPacket (NULL, 0, packet))
Greg Claytond52d00f2011-07-16 03:19:08 +0000186 return packet.GetByteSize();
Greg Clayton363be3f2011-07-15 03:27:12 +0000187
188 bool timed_out = false;
189 while (IsConnected() && !timed_out)
190 {
191 lldb::ConnectionStatus status;
192 size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error);
193
194 if (log)
195 log->Printf ("%s: Read (buffer, (sizeof(buffer), timeout_usec = 0x%x, status = %s, error = %s) => bytes_read = %zu",
196 __PRETTY_FUNCTION__,
197 timeout_usec,
198 Communication::ConnectionStatusAsCString (status),
199 error.AsCString(),
200 bytes_read);
201
202 if (bytes_read > 0)
203 {
204 if (CheckForPacket (buffer, bytes_read, packet))
Greg Claytond52d00f2011-07-16 03:19:08 +0000205 return packet.GetByteSize();
Greg Clayton363be3f2011-07-15 03:27:12 +0000206 }
207 else
208 {
209 switch (status)
210 {
211 case eConnectionStatusTimedOut:
212 timed_out = true;
213 break;
214 case eConnectionStatusSuccess:
215 //printf ("status = success but error = %s\n", error.AsCString("<invalid>"));
216 break;
217
218 case eConnectionStatusEndOfFile:
219 case eConnectionStatusNoConnection:
220 case eConnectionStatusLostConnection:
221 case eConnectionStatusError:
222 Disconnect();
223 break;
224 }
225 }
226 }
227 packet.Clear ();
228 return 0;
229}
230
231bool
Greg Claytond52d00f2011-07-16 03:19:08 +0000232CommunicationKDP::CheckForPacket (const uint8_t *src, size_t src_len, DataExtractor &packet)
Greg Clayton363be3f2011-07-15 03:27:12 +0000233{
234 // Put the packet data into the buffer in a thread safe fashion
235 Mutex::Locker locker(m_bytes_mutex);
236
237 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS));
238
239 if (src && src_len > 0)
240 {
241 if (log && log->GetVerbose())
242 {
Greg Claytond52d00f2011-07-16 03:19:08 +0000243 PacketStreamType log_strm;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000244 DataExtractor::DumpHexBytes (&log_strm, src, src_len, UINT32_MAX, LLDB_INVALID_ADDRESS);
Greg Claytond52d00f2011-07-16 03:19:08 +0000245 log->Printf ("CommunicationKDP::%s adding %u bytes: %s",
Greg Clayton363be3f2011-07-15 03:27:12 +0000246 __FUNCTION__,
247 (uint32_t)src_len,
Greg Claytond52d00f2011-07-16 03:19:08 +0000248 log_strm.GetData());
Greg Clayton363be3f2011-07-15 03:27:12 +0000249 }
250 m_bytes.append ((const char *)src, src_len);
251 }
252
Greg Claytond52d00f2011-07-16 03:19:08 +0000253 // Make sure we at least have enough bytes for a packet header
254 const size_t bytes_available = m_bytes.size();
255 if (bytes_available >= 8)
Greg Clayton363be3f2011-07-15 03:27:12 +0000256 {
Greg Claytond52d00f2011-07-16 03:19:08 +0000257 packet.SetData (&m_bytes[0], bytes_available, m_byte_order);
258 uint32_t offset = 0;
259 uint8_t reply_command = packet.GetU8(&offset);
260 switch (reply_command)
261 {
262 case ePacketTypeReply | eCommandTypeConnect:
263 case ePacketTypeReply | eCommandTypeDisconnect:
264 case ePacketTypeReply | eCommandTypeHostInfo:
265 case ePacketTypeReply | eCommandTypeVersion:
266 case ePacketTypeReply | eCommandTypeMaxBytes:
267 case ePacketTypeReply | eCommandTypeReadMemory:
268 case ePacketTypeReply | eCommandTypeWriteMemory:
269 case ePacketTypeReply | eCommandTypeReadRegisters:
270 case ePacketTypeReply | eCommandTypeWriteRegisters:
271 case ePacketTypeReply | eCommandTypeLoad:
272 case ePacketTypeReply | eCommandTypeImagePath:
273 case ePacketTypeReply | eCommandTypeSuspend:
274 case ePacketTypeReply | eCommandTypeResume:
275 case ePacketTypeReply | eCommandTypeException:
276 case ePacketTypeReply | eCommandTypeTermination:
277 case ePacketTypeReply | eCommandTypeBreakpointSet:
278 case ePacketTypeReply | eCommandTypeBreakpointRemove:
279 case ePacketTypeReply | eCommandTypeRegions:
280 case ePacketTypeReply | eCommandTypeReattach:
281 case ePacketTypeReply | eCommandTypeHostReboot:
282 case ePacketTypeReply | eCommandTypeReadMemory64:
283 case ePacketTypeReply | eCommandTypeWriteMemory64:
284 case ePacketTypeReply | eCommandTypeBreakpointSet64:
285 case ePacketTypeReply | eCommandTypeBreakpointRemove64:
286 case ePacketTypeReply | eCommandTypeKernelVersion:
287 {
288 offset = 2;
289 const uint16_t length = packet.GetU16 (&offset);
290 if (length <= bytes_available)
291 {
292 // We have an entire packet ready, we need to copy the data
293 // bytes into a buffer that will be owned by the packet and
294 // erase the bytes from our communcation buffer "m_bytes"
295 packet.SetData (DataBufferSP (new DataBufferHeap (&m_bytes[0], length)));
296 m_bytes.erase (0, length);
Greg Clayton8d2ea282011-07-17 20:36:25 +0000297
298 if (log)
299 {
300 PacketStreamType log_strm;
Greg Clayton0fa51242011-07-19 03:57:15 +0000301 DumpPacket (log_strm, packet);
Greg Clayton8d2ea282011-07-17 20:36:25 +0000302
Greg Clayton0fa51242011-07-19 03:57:15 +0000303 log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData());
Greg Clayton8d2ea282011-07-17 20:36:25 +0000304 }
Greg Claytond52d00f2011-07-16 03:19:08 +0000305 return true;
306 }
307 }
308 break;
309
310 default:
311 // Unrecognized reply command byte, erase this byte and try to get back on track
312 if (log)
313 log->Printf ("CommunicationKDP::%s: tossing junk byte: 0x%2.2x",
314 __FUNCTION__,
315 (uint8_t)m_bytes[0]);
316 m_bytes.erase(0, 1);
317 break;
318 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000319 }
320 packet.Clear();
321 return false;
322}
323
Greg Clayton1e5b0212011-07-15 16:31:38 +0000324
Greg Claytond52d00f2011-07-16 03:19:08 +0000325bool
Greg Clayton8d2ea282011-07-17 20:36:25 +0000326CommunicationKDP::SendRequestConnect (uint16_t reply_port,
327 uint16_t exc_port,
328 const char *greeting)
Greg Clayton1e5b0212011-07-15 16:31:38 +0000329{
Greg Clayton0fa51242011-07-19 03:57:15 +0000330 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Claytond52d00f2011-07-16 03:19:08 +0000331 if (greeting == NULL)
332 greeting = "";
333
334 const CommandType command = eCommandTypeConnect;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000335 // Length is 82 uint16_t and the length of the greeting C string with the terminating NULL
336 const uint32_t command_length = 8 + 2 + 2 + ::strlen(greeting) + 1;
Greg Claytond52d00f2011-07-16 03:19:08 +0000337 const uint32_t request_sequence_id = m_request_sequence_id;
338 MakeRequestPacketHeader (command, request_packet, command_length);
Greg Clayton8d2ea282011-07-17 20:36:25 +0000339 // Always send connect ports as little endian
340 request_packet.SetByteOrder (eByteOrderLittle);
341 request_packet.PutHex16 (reply_port);
342 request_packet.PutHex16 (exc_port);
343 request_packet.SetByteOrder (m_byte_order);
344 request_packet.PutCString (greeting);
Greg Claytond52d00f2011-07-16 03:19:08 +0000345 DataExtractor reply_packet;
346 return SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet);
Greg Clayton1e5b0212011-07-15 16:31:38 +0000347}
348
Greg Claytond52d00f2011-07-16 03:19:08 +0000349void
350CommunicationKDP::ClearKDPSettings ()
351{
352 m_request_sequence_id = 0;
353 m_kdp_version_version = 0;
354 m_kdp_version_feature = 0;
355 m_kdp_hostinfo_cpu_mask = 0;
356 m_kdp_hostinfo_cpu_type = 0;
357 m_kdp_hostinfo_cpu_subtype = 0;
358}
359
360bool
Greg Clayton8d2ea282011-07-17 20:36:25 +0000361CommunicationKDP::SendRequestReattach (uint16_t reply_port)
Greg Claytond52d00f2011-07-16 03:19:08 +0000362{
Greg Clayton0fa51242011-07-19 03:57:15 +0000363 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Claytond52d00f2011-07-16 03:19:08 +0000364 const CommandType command = eCommandTypeReattach;
365 // Length is 8 bytes for the header plus 2 bytes for the reply UDP port
366 const uint32_t command_length = 8 + 2;
367 const uint32_t request_sequence_id = m_request_sequence_id;
368 MakeRequestPacketHeader (command, request_packet, command_length);
Greg Clayton8d2ea282011-07-17 20:36:25 +0000369 // Always send connect ports as little endian
370 request_packet.SetByteOrder (eByteOrderLittle);
Greg Claytond52d00f2011-07-16 03:19:08 +0000371 request_packet.PutHex16(reply_port);
Greg Clayton8d2ea282011-07-17 20:36:25 +0000372 request_packet.SetByteOrder (m_byte_order);
Greg Claytond52d00f2011-07-16 03:19:08 +0000373 DataExtractor reply_packet;
374 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
375 {
376 // Reset the sequence ID to zero for reattach
377 ClearKDPSettings ();
378 uint32_t offset = 4;
379 m_session_key = reply_packet.GetU32 (&offset);
380 return true;
381 }
382 return false;
383}
384
385uint32_t
386CommunicationKDP::GetVersion ()
387{
388 if (!VersionIsValid())
389 SendRequestVersion();
390 return m_kdp_version_version;
391}
392
393uint32_t
394CommunicationKDP::GetFeatureFlags ()
395{
396 if (!VersionIsValid())
397 SendRequestVersion();
398 return m_kdp_version_feature;
399}
400
401bool
402CommunicationKDP::SendRequestVersion ()
403{
Greg Clayton0fa51242011-07-19 03:57:15 +0000404 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Claytond52d00f2011-07-16 03:19:08 +0000405 const CommandType command = eCommandTypeVersion;
406 const uint32_t command_length = 8;
407 const uint32_t request_sequence_id = m_request_sequence_id;
408 MakeRequestPacketHeader (command, request_packet, command_length);
409 DataExtractor reply_packet;
410 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
411 {
Greg Claytond52d00f2011-07-16 03:19:08 +0000412 uint32_t offset = 8;
413 m_kdp_version_version = reply_packet.GetU32 (&offset);
414 m_kdp_version_feature = reply_packet.GetU32 (&offset);
415 return true;
416 }
417 return false;
418}
419
420uint32_t
421CommunicationKDP::GetCPUMask ()
422{
423 if (!HostInfoIsValid())
424 SendRequestHostInfo();
425 return m_kdp_hostinfo_cpu_mask;
426}
427
428uint32_t
429CommunicationKDP::GetCPUType ()
430{
431 if (!HostInfoIsValid())
432 SendRequestHostInfo();
433 return m_kdp_hostinfo_cpu_type;
434}
435
436uint32_t
437CommunicationKDP::GetCPUSubtype ()
438{
439 if (!HostInfoIsValid())
440 SendRequestHostInfo();
441 return m_kdp_hostinfo_cpu_subtype;
442}
443
444bool
445CommunicationKDP::SendRequestHostInfo ()
446{
Greg Clayton0fa51242011-07-19 03:57:15 +0000447 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Claytond52d00f2011-07-16 03:19:08 +0000448 const CommandType command = eCommandTypeHostInfo;
449 const uint32_t command_length = 8;
450 const uint32_t request_sequence_id = m_request_sequence_id;
451 MakeRequestPacketHeader (command, request_packet, command_length);
452 DataExtractor reply_packet;
453 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
454 {
Greg Claytond52d00f2011-07-16 03:19:08 +0000455 uint32_t offset = 8;
Greg Clayton0fa51242011-07-19 03:57:15 +0000456 m_kdp_hostinfo_cpu_mask = reply_packet.GetU32 (&offset);
457 m_kdp_hostinfo_cpu_type = reply_packet.GetU32 (&offset);
458 m_kdp_hostinfo_cpu_subtype = reply_packet.GetU32 (&offset);
459
460 ArchSpec kernel_arch;
461 kernel_arch.SetArchitecture (eArchTypeMachO,
462 m_kdp_hostinfo_cpu_type,
463 m_kdp_hostinfo_cpu_subtype);
464
465 m_addr_byte_size = kernel_arch.GetAddressByteSize();
466 m_byte_order = kernel_arch.GetByteOrder();
Greg Claytond52d00f2011-07-16 03:19:08 +0000467 return true;
468 }
469 return false;
470}
471
472bool
Greg Clayton8d2ea282011-07-17 20:36:25 +0000473CommunicationKDP::SendRequestDisconnect ()
Greg Clayton1e5b0212011-07-15 16:31:38 +0000474{
Greg Clayton0fa51242011-07-19 03:57:15 +0000475 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Claytond52d00f2011-07-16 03:19:08 +0000476 const CommandType command = eCommandTypeDisconnect;
477 const uint32_t command_length = 8;
478 const uint32_t request_sequence_id = m_request_sequence_id;
479 MakeRequestPacketHeader (command, request_packet, command_length);
480 DataExtractor reply_packet;
481 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
482 {
483 // Are we supposed to get a reply for disconnect?
484 }
485 ClearKDPSettings ();
486 return true;
Greg Clayton1e5b0212011-07-15 16:31:38 +0000487}
488
Greg Clayton0fa51242011-07-19 03:57:15 +0000489uint32_t
490CommunicationKDP::SendRequestReadMemory (lldb::addr_t addr,
491 void *dst,
492 uint32_t dst_len,
493 Error &error)
494{
495 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
496 bool use_64 = (GetVersion() >= 11);
497 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
498 const CommandType command = use_64 ? eCommandTypeReadMemory64 : eCommandTypeReadMemory;
499 // Size is header + address size + uint32_t length
500 const uint32_t command_length = 8 + command_addr_byte_size + 4;
501 const uint32_t request_sequence_id = m_request_sequence_id;
502 MakeRequestPacketHeader (command, request_packet, command_length);
503 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
504 request_packet.PutHex32 (dst_len);
505 DataExtractor reply_packet;
506 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
507 {
Greg Clayton0fa51242011-07-19 03:57:15 +0000508 uint32_t offset = 8;
509 uint32_t kdp_error = reply_packet.GetU32 (&offset);
510 uint32_t src_len = reply_packet.GetByteSize() - 12;
511
512 if (src_len > 0)
513 {
514 const void *src = reply_packet.GetData(&offset, src_len);
515 if (src)
516 {
517 ::memcpy (dst, src, src_len);
518 error.Clear();
519 return src_len;
520 }
521 }
522 if (kdp_error)
523 error.SetErrorStringWithFormat ("kdp read memory failed (error %u)", kdp_error);
524 else
525 error.SetErrorString ("kdp read memory failed");
526 }
527 return 0;
528}
529
Greg Claytonec15f502011-07-20 01:32:50 +0000530
531uint32_t
532CommunicationKDP::SendRequestWriteMemory (lldb::addr_t addr,
533 const void *src,
534 uint32_t src_len,
535 Error &error)
536{
537 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
538 bool use_64 = (GetVersion() >= 11);
539 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
540 const CommandType command = use_64 ? eCommandTypeWriteMemory64 : eCommandTypeWriteMemory;
541 // Size is header + address size + uint32_t length
542 const uint32_t command_length = 8 + command_addr_byte_size + 4;
543 const uint32_t request_sequence_id = m_request_sequence_id;
544 MakeRequestPacketHeader (command, request_packet, command_length);
545 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
546 request_packet.PutHex32 (src_len);
547 request_packet.PutBytesAsRawHex8(src, src_len);
548
549 DataExtractor reply_packet;
550 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
551 {
552 uint32_t offset = 8;
553 uint32_t kdp_error = reply_packet.GetU32 (&offset);
554 if (kdp_error)
555 error.SetErrorStringWithFormat ("kdp write memory failed (error %u)", kdp_error);
556 else
557 {
558 error.Clear();
559 return src_len;
560 }
561 }
562 return 0;
563}
564
Greg Clayton0fa51242011-07-19 03:57:15 +0000565const char *
566CommunicationKDP::GetCommandAsCString (uint8_t command)
567{
568 switch (command)
569 {
570 case eCommandTypeConnect: return "KDP_CONNECT";
571 case eCommandTypeDisconnect: return "KDP_DISCONNECT";
572 case eCommandTypeHostInfo: return "KDP_HOSTINFO";
573 case eCommandTypeVersion: return "KDP_VERSION";
574 case eCommandTypeMaxBytes: return "KDP_MAXBYTES";
575 case eCommandTypeReadMemory: return "KDP_READMEM";
576 case eCommandTypeWriteMemory: return "KDP_WRITEMEM";
577 case eCommandTypeReadRegisters: return "KDP_READREGS";
578 case eCommandTypeWriteRegisters: return "KDP_WRITEREGS";
579 case eCommandTypeLoad: return "KDP_LOAD";
580 case eCommandTypeImagePath: return "KDP_IMAGEPATH";
581 case eCommandTypeSuspend: return "KDP_SUSPEND";
582 case eCommandTypeResume: return "KDP_RESUMECPUS";
583 case eCommandTypeException: return "KDP_EXCEPTION";
584 case eCommandTypeTermination: return "KDP_TERMINATION";
585 case eCommandTypeBreakpointSet: return "KDP_BREAKPOINT_SET";
586 case eCommandTypeBreakpointRemove: return "KDP_BREAKPOINT_REMOVE";
587 case eCommandTypeRegions: return "KDP_REGIONS";
588 case eCommandTypeReattach: return "KDP_REATTACH";
589 case eCommandTypeHostReboot: return "KDP_HOSTREBOOT";
590 case eCommandTypeReadMemory64: return "KDP_READMEM64";
591 case eCommandTypeWriteMemory64: return "KDP_WRITEMEM64";
592 case eCommandTypeBreakpointSet64: return "KDP_BREAKPOINT64_SET";
593 case eCommandTypeBreakpointRemove64: return "KDP_BREAKPOINT64_REMOVE";
594 case eCommandTypeKernelVersion: return "KDP_KERNELVERSION";
595 }
596 return NULL;
597}
598
599void
600CommunicationKDP::DumpPacket (Stream &s, const void *data, uint32_t data_len)
601{
602 DataExtractor extractor (data, data_len, m_byte_order, m_addr_byte_size);
603 DumpPacket (s, extractor);
604}
605
606void
607CommunicationKDP::DumpPacket (Stream &s, const DataExtractor& packet)
608{
609 const char *error_desc = NULL;
610 if (packet.GetByteSize() < 8)
611 {
612 error_desc = "error: invalid packet (too short): ";
613 }
614 else
615 {
616 uint32_t offset = 0;
617 const uint8_t first_packet_byte = packet.GetU8 (&offset);
618 const uint8_t sequence_id = packet.GetU8 (&offset);
619 const uint16_t length = packet.GetU16 (&offset);
620 const uint32_t key = packet.GetU32 (&offset);
621 const CommandType command = ExtractCommand (first_packet_byte);
622 const char *command_name = GetCommandAsCString (command);
623 if (command_name)
624 {
625 const bool is_reply = ExtractIsReply(first_packet_byte);
626 s.Printf ("%s {%u:%u} <0x%4.4x> %s",
627 is_reply ? "<--" : "-->",
628 key,
629 sequence_id,
630 length,
631 command_name);
632
633 if (is_reply)
634 {
635 // Dump request reply packets
636 switch (command)
637 {
Greg Claytonec15f502011-07-20 01:32:50 +0000638 // Commands that return a single 32 bit error
Greg Clayton0fa51242011-07-19 03:57:15 +0000639 case eCommandTypeConnect:
Greg Claytonec15f502011-07-20 01:32:50 +0000640 case eCommandTypeWriteMemory:
641 case eCommandTypeWriteMemory64:
642 case eCommandTypeBreakpointSet:
643 case eCommandTypeBreakpointRemove:
644 case eCommandTypeBreakpointSet64:
645 case eCommandTypeBreakpointRemove64:
646 case eCommandTypeWriteRegisters:
647 case eCommandTypeLoad:
Greg Clayton0fa51242011-07-19 03:57:15 +0000648 {
649 const uint32_t error = packet.GetU32 (&offset);
650 s.Printf(" (error=0x%8.8x)", error);
651 }
652 break;
653
654 case eCommandTypeDisconnect:
655 case eCommandTypeReattach:
656 case eCommandTypeHostReboot:
Greg Claytonec15f502011-07-20 01:32:50 +0000657 case eCommandTypeSuspend:
658 case eCommandTypeResume:
659 case eCommandTypeException:
660 case eCommandTypeTermination:
Greg Clayton0fa51242011-07-19 03:57:15 +0000661 // No return value for the reply, just the header to ack
Greg Claytonec15f502011-07-20 01:32:50 +0000662 s.PutCString(" ()");
Greg Clayton0fa51242011-07-19 03:57:15 +0000663 break;
664
665 case eCommandTypeHostInfo:
666 {
667 const uint32_t cpu_mask = packet.GetU32 (&offset);
668 const uint32_t cpu_type = packet.GetU32 (&offset);
669 const uint32_t cpu_subtype = packet.GetU32 (&offset);
670 s.Printf(" (cpu_mask=0x%8.8x, cpu_type=0x%8.8x, cpu_subtype=0x%8.8x)", cpu_mask, cpu_type, cpu_subtype);
671 }
672 break;
673
674 case eCommandTypeVersion:
675 {
676 const uint32_t version = packet.GetU32 (&offset);
677 const uint32_t feature = packet.GetU32 (&offset);
678 s.Printf(" (version=0x%8.8x, feature=0x%8.8x)", version, feature);
679 }
680 break;
681
682 case eCommandTypeRegions:
683 {
684 const uint32_t region_count = packet.GetU32 (&offset);
685 s.Printf(" (count = %u", region_count);
686 for (uint32_t i=0; i<region_count; ++i)
687 {
688 const addr_t region_addr = packet.GetPointer (&offset);
689 const uint32_t region_size = packet.GetU32 (&offset);
690 const uint32_t region_prot = packet.GetU32 (&offset);
691 s.Printf("\n\tregion[%i] = { range = [0x%16.16llx - 0x%16.16llx), size = 0x%8.8x, prot = %s }", region_addr, region_addr + region_size, region_size, GetPermissionsAsCString (region_prot));
692 }
693 }
694 break;
695
696 case eCommandTypeReadMemory:
697 case eCommandTypeReadMemory64:
Greg Clayton0fa51242011-07-19 03:57:15 +0000698 {
699 const uint32_t error = packet.GetU32 (&offset);
Greg Claytonec15f502011-07-20 01:32:50 +0000700 const uint32_t count = packet.GetByteSize() - offset;
Greg Clayton0fa51242011-07-19 03:57:15 +0000701 s.Printf(" (error = 0x%8.8x <0x%x>:\n", error, count);
702 if (count > 0)
703 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, count), count, 32, LLDB_INVALID_ADDRESS);
704 }
705 break;
706
Greg Claytonec15f502011-07-20 01:32:50 +0000707 case eCommandTypeReadRegisters:
708 {
709 const uint32_t error = packet.GetU32 (&offset);
710 const uint32_t count = packet.GetByteSize() - offset;
711 s.Printf(" (error = 0x%8.8x <0x%x> regs:\n", error, count);
712 if (count > 0)
713 packet.Dump (&s, // Stream to dump to
714 offset, // Offset within "packet"
715 eFormatHex, // Format to use
716 m_addr_byte_size, // Size of each item in bytes
717 count / m_addr_byte_size, // Number of items
718 16 / m_addr_byte_size, // Number per line
719 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
720 0, 0); // No bitfields
721 }
722 break;
723
Greg Clayton0fa51242011-07-19 03:57:15 +0000724 case eCommandTypeMaxBytes:
Greg Clayton0fa51242011-07-19 03:57:15 +0000725 case eCommandTypeImagePath:
Greg Clayton0fa51242011-07-19 03:57:15 +0000726 case eCommandTypeKernelVersion:
Greg Claytonec15f502011-07-20 01:32:50 +0000727 s.Printf(" (add support for dumping this packet reply!!!");
Greg Clayton0fa51242011-07-19 03:57:15 +0000728 break;
729
730 }
731 }
732 else
733 {
734 // Dump request packets
735 switch (command)
736 {
737 case eCommandTypeConnect:
738 {
739 const uint16_t reply_port = packet.GetU16 (&offset);
740 const uint16_t exc_port = packet.GetU16 (&offset);
741 s.Printf(" (reply_port=%u, exc_port=%u, greeting=\"%s\")", reply_port, exc_port, packet.GetCStr(&offset));
742 }
743 break;
744
745 case eCommandTypeDisconnect:
746 case eCommandTypeHostReboot:
747 case eCommandTypeHostInfo:
748 case eCommandTypeVersion:
749 case eCommandTypeRegions:
750 // No args, just the header in the request...
751 break;
752
753 case eCommandTypeReadMemory:
754 {
755 const uint32_t addr = packet.GetU32 (&offset);
756 const uint32_t size = packet.GetU32 (&offset);
757 s.Printf(" (addr = 0x%8.8x, size=%u)", addr, size);
758 }
759 break;
760
Greg Claytonec15f502011-07-20 01:32:50 +0000761 case eCommandTypeWriteMemory:
762 {
763 const uint32_t addr = packet.GetU32 (&offset);
764 const uint32_t size = packet.GetU32 (&offset);
765 s.Printf(" (addr = 0x%8.8x, size=%u, bytes:\n", addr, size);
766 if (size > 0)
767 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
768 }
769 break;
770
Greg Clayton0fa51242011-07-19 03:57:15 +0000771 case eCommandTypeReadMemory64:
772 {
773 const uint64_t addr = packet.GetU64 (&offset);
774 const uint32_t size = packet.GetU32 (&offset);
775 s.Printf(" (addr = 0x%16.16llx, size=%u)", addr, size);
776 }
777 break;
778
Greg Claytonec15f502011-07-20 01:32:50 +0000779 case eCommandTypeWriteMemory64:
780 {
781 const uint64_t addr = packet.GetU64 (&offset);
782 const uint32_t size = packet.GetU32 (&offset);
783 s.Printf(" (addr = 0x%16.16llx, size=%u, bytes:", addr, size);
784 if (size > 0)
785 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
786 }
787 break;
788
Greg Clayton0fa51242011-07-19 03:57:15 +0000789 case eCommandTypeReadRegisters:
790 {
791 const uint32_t cpu = packet.GetU32 (&offset);
792 const uint32_t flavor = packet.GetU32 (&offset);
793 s.Printf(" (cpu = %u, flavor=%u)", cpu, flavor);
794 }
795 break;
796
Greg Clayton0fa51242011-07-19 03:57:15 +0000797 case eCommandTypeWriteRegisters:
Greg Claytonec15f502011-07-20 01:32:50 +0000798 {
799 const uint32_t cpu = packet.GetU32 (&offset);
800 const uint32_t flavor = packet.GetU32 (&offset);
801 const uint32_t nbytes = packet.GetByteSize() - offset;
802 s.Printf(" (cpu = %u, flavor=%u, regs:\n", cpu, flavor);
803 if (nbytes > 0)
804 packet.Dump (&s, // Stream to dump to
805 offset, // Offset within "packet"
806 eFormatHex, // Format to use
807 m_addr_byte_size, // Size of each item in bytes
808 nbytes / m_addr_byte_size, // Number of items
809 16 / m_addr_byte_size, // Number per line
810 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
811 0, 0); // No bitfields
812 }
813 break;
814
815 case eCommandTypeMaxBytes:
816
Greg Clayton0fa51242011-07-19 03:57:15 +0000817 case eCommandTypeLoad:
818 case eCommandTypeImagePath:
819 case eCommandTypeSuspend:
820 case eCommandTypeResume:
821 case eCommandTypeException:
822 case eCommandTypeTermination:
823 case eCommandTypeBreakpointSet:
824 case eCommandTypeBreakpointRemove:
825 break;
826
827 case eCommandTypeReattach:
828 {
829 const uint16_t reply_port = packet.GetU16 (&offset);
830 s.Printf(" (reply_port=%u)", reply_port);
831 }
832 break;
833
Greg Clayton0fa51242011-07-19 03:57:15 +0000834 case eCommandTypeBreakpointSet64:
835 case eCommandTypeBreakpointRemove64:
836 case eCommandTypeKernelVersion:
Greg Claytonec15f502011-07-20 01:32:50 +0000837
Greg Clayton0fa51242011-07-19 03:57:15 +0000838 break;
839 }
840 }
841 }
842 else
843 {
844 error_desc = "error: invalid packet command: ";
845 }
846 }
847
848 if (error_desc)
849 {
850 s.PutCString (error_desc);
851
852 packet.Dump (&s, // Stream to dump to
853 0, // Offset into "packet"
854 eFormatBytes, // Dump as hex bytes
855 1, // Size of each item is 1 for single bytes
856 packet.GetByteSize(), // Number of bytes
857 UINT32_MAX, // Num bytes per line
858 LLDB_INVALID_ADDRESS, // Base address
859 0, 0); // Bitfield info set to not do anything bitfield related
860 }
861}
862
863uint32_t
864CommunicationKDP::SendRequestReadRegisters (uint32_t cpu,
865 uint32_t flavor,
866 void *dst,
867 uint32_t dst_len,
868 Error &error)
869{
870 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
871 const CommandType command = eCommandTypeReadRegisters;
872 // Size is header + 4 byte cpu and 4 byte flavor
873 const uint32_t command_length = 8 + 4 + 4;
874 const uint32_t request_sequence_id = m_request_sequence_id;
875 MakeRequestPacketHeader (command, request_packet, command_length);
876 request_packet.PutHex32 (cpu);
877 request_packet.PutHex32 (flavor);
878 DataExtractor reply_packet;
879 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
880 {
Greg Clayton0fa51242011-07-19 03:57:15 +0000881 uint32_t offset = 8;
882 uint32_t kdp_error = reply_packet.GetU32 (&offset);
883 uint32_t src_len = reply_packet.GetByteSize() - 12;
884
885 if (src_len > 0)
886 {
887 const uint32_t bytes_to_copy = std::min<uint32_t>(src_len, dst_len);
888 const void *src = reply_packet.GetData(&offset, bytes_to_copy);
889 if (src)
890 {
891 ::memcpy (dst, src, bytes_to_copy);
892 error.Clear();
893 // Return the number of bytes we could have returned regardless if
894 // we copied them or not, just so we know when things don't match up
895 return src_len;
896 }
897 }
898 if (kdp_error)
899 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error);
900 else
901 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u", cpu, flavor);
902 }
903 return 0;
904}
905