blob: df5e535fe1d06ece9326696528b7164d6a2b1d1f [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
Jason Molendafac2e622012-09-29 04:02:01 +000014#include <errno.h>
Greg Clayton363be3f2011-07-15 03:27:12 +000015#include <limits.h>
16#include <string.h>
17
18// C++ Includes
Greg Clayton0fa51242011-07-19 03:57:15 +000019#include "llvm/Support/MachO.h"
20
Greg Clayton363be3f2011-07-15 03:27:12 +000021// Other libraries and framework includes
Greg Claytond52d00f2011-07-16 03:19:08 +000022#include "lldb/Core/DataBufferHeap.h"
Greg Clayton1e5b0212011-07-15 16:31:38 +000023#include "lldb/Core/DataExtractor.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000024#include "lldb/Core/Log.h"
Greg Clayton0fa51242011-07-19 03:57:15 +000025#include "lldb/Core/State.h"
Jason Molendafac2e622012-09-29 04:02:01 +000026#include "lldb/Core/UUID.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000027#include "lldb/Host/FileSpec.h"
28#include "lldb/Host/Host.h"
29#include "lldb/Host/TimeValue.h"
30#include "lldb/Target/Process.h"
Greg Clayton363be3f2011-07-15 03:27:12 +000031
32// Project includes
33#include "ProcessKDPLog.h"
34
Greg Clayton363be3f2011-07-15 03:27:12 +000035using 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),
Greg Clayton3acaa922012-09-25 02:40:06 +000047 m_is_running (false),
Greg Claytond52d00f2011-07-16 03:19:08 +000048 m_session_key (0u),
49 m_request_sequence_id (0u),
50 m_exception_sequence_id (0u),
51 m_kdp_version_version (0u),
52 m_kdp_version_feature (0u),
53 m_kdp_hostinfo_cpu_mask (0u),
54 m_kdp_hostinfo_cpu_type (0u),
55 m_kdp_hostinfo_cpu_subtype (0u)
Greg Clayton363be3f2011-07-15 03:27:12 +000056{
57}
58
59//----------------------------------------------------------------------
60// Destructor
61//----------------------------------------------------------------------
62CommunicationKDP::~CommunicationKDP()
63{
64 if (IsConnected())
65 {
66 Disconnect();
67 }
68}
69
Greg Clayton1e5b0212011-07-15 16:31:38 +000070bool
Greg Claytond52d00f2011-07-16 03:19:08 +000071CommunicationKDP::SendRequestPacket (const PacketStreamType &request_packet)
Greg Clayton363be3f2011-07-15 03:27:12 +000072{
73 Mutex::Locker locker(m_sequence_mutex);
Greg Clayton1e5b0212011-07-15 16:31:38 +000074 return SendRequestPacketNoLock (request_packet);
Greg Clayton363be3f2011-07-15 03:27:12 +000075}
76
Greg Claytond52d00f2011-07-16 03:19:08 +000077#if 0
78typedef struct {
79 uint8_t request; // Either: CommandType | ePacketTypeRequest, or CommandType | ePacketTypeReply
80 uint8_t sequence;
81 uint16_t length; // Length of entire packet including this header
82 uint32_t key; // Session key
83} kdp_hdr_t;
84#endif
85
Greg Clayton1e5b0212011-07-15 16:31:38 +000086void
Greg Clayton307c7fd2012-10-19 22:22:57 +000087CommunicationKDP::MakeRequestPacketHeader (CommandType request_type,
Greg Claytond52d00f2011-07-16 03:19:08 +000088 PacketStreamType &request_packet,
89 uint16_t request_length)
Greg Clayton363be3f2011-07-15 03:27:12 +000090{
Greg Clayton1e5b0212011-07-15 16:31:38 +000091 request_packet.Clear();
Greg Claytond52d00f2011-07-16 03:19:08 +000092 request_packet.PutHex8 (request_type | ePacketTypeRequest); // Set the request type
93 request_packet.PutHex8 (m_request_sequence_id++); // Sequence number
94 request_packet.PutHex16 (request_length); // Length of the packet including this header
95 request_packet.PutHex32 (m_session_key); // Session key
Greg Clayton363be3f2011-07-15 03:27:12 +000096}
97
Greg Claytond52d00f2011-07-16 03:19:08 +000098bool
99CommunicationKDP::SendRequestAndGetReply (const CommandType command,
100 const uint8_t request_sequence_id,
101 const PacketStreamType &request_packet,
102 DataExtractor &reply_packet)
103{
Greg Clayton3acaa922012-09-25 02:40:06 +0000104 if (IsRunning())
105 {
106 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS));
107 if (log)
108 {
109 PacketStreamType log_strm;
110 DumpPacket (log_strm, request_packet.GetData(), request_packet.GetSize());
111 log->Printf("error: kdp running, not sending packet: %.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData());
112 }
113 return false;
114 }
Greg Claytond52d00f2011-07-16 03:19:08 +0000115
Greg Clayton3acaa922012-09-25 02:40:06 +0000116 Mutex::Locker locker(m_sequence_mutex);
117#ifdef LLDB_CONFIGURATION_DEBUG
118 // NOTE: this only works for packets that are in native endian byte order
119 assert (request_packet.GetSize() == *((uint16_t *)(request_packet.GetData() + 2)));
120#endif
Greg Claytond52d00f2011-07-16 03:19:08 +0000121 if (SendRequestPacketNoLock(request_packet))
122 {
Greg Clayton8d2ea282011-07-17 20:36:25 +0000123 if (WaitForPacketWithTimeoutMicroSecondsNoLock (reply_packet, GetPacketTimeoutInMicroSeconds ()))
Greg Claytond52d00f2011-07-16 03:19:08 +0000124 {
125 uint32_t offset = 0;
126 const uint8_t reply_command = reply_packet.GetU8 (&offset);
127 const uint8_t reply_sequence_id = reply_packet.GetU8 (&offset);
128 if ((reply_command & eCommandTypeMask) == command)
129 {
130 if (request_sequence_id == reply_sequence_id)
Greg Clayton3acaa922012-09-25 02:40:06 +0000131 {
132 if (command == KDP_RESUMECPUS)
133 m_is_running.SetValue(true, eBroadcastAlways);
Greg Claytond52d00f2011-07-16 03:19:08 +0000134 return true;
Greg Clayton3acaa922012-09-25 02:40:06 +0000135 }
Greg Claytond52d00f2011-07-16 03:19:08 +0000136 }
137 }
138 }
139 reply_packet.Clear();
140 return false;
141}
Greg Clayton363be3f2011-07-15 03:27:12 +0000142
Greg Clayton1e5b0212011-07-15 16:31:38 +0000143bool
Greg Claytond52d00f2011-07-16 03:19:08 +0000144CommunicationKDP::SendRequestPacketNoLock (const PacketStreamType &request_packet)
Greg Clayton363be3f2011-07-15 03:27:12 +0000145{
146 if (IsConnected())
147 {
Greg Clayton1e5b0212011-07-15 16:31:38 +0000148 const char *packet_data = request_packet.GetData();
149 const size_t packet_size = request_packet.GetSize();
Greg Clayton363be3f2011-07-15 03:27:12 +0000150
151 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS));
152 if (log)
Greg Clayton1e5b0212011-07-15 16:31:38 +0000153 {
Greg Clayton0fa51242011-07-19 03:57:15 +0000154 PacketStreamType log_strm;
155 DumpPacket (log_strm, packet_data, packet_size);
156 log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData());
Greg Clayton1e5b0212011-07-15 16:31:38 +0000157 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000158 ConnectionStatus status = eConnectionStatusSuccess;
Greg Clayton363be3f2011-07-15 03:27:12 +0000159
Greg Clayton1e5b0212011-07-15 16:31:38 +0000160 size_t bytes_written = Write (packet_data,
161 packet_size,
162 status,
163 NULL);
164
165 if (bytes_written == packet_size)
166 return true;
167
168 if (log)
Greg Clayton851e30e2012-09-18 18:04:04 +0000169 log->Printf ("error: failed to send packet entire packet %llu of %llu bytes sent", (uint64_t)bytes_written, (uint64_t)packet_size);
Greg Clayton1e5b0212011-07-15 16:31:38 +0000170 }
171 return false;
Greg Clayton363be3f2011-07-15 03:27:12 +0000172}
173
174bool
Greg Clayton516f0842012-04-11 00:24:49 +0000175CommunicationKDP::GetSequenceMutex (Mutex::Locker& locker)
Greg Clayton363be3f2011-07-15 03:27:12 +0000176{
Jim Ingham1b584eb2012-05-04 23:02:50 +0000177 return locker.TryLock (m_sequence_mutex);
Greg Clayton363be3f2011-07-15 03:27:12 +0000178}
179
180
181bool
182CommunicationKDP::WaitForNotRunningPrivate (const TimeValue *timeout_ptr)
183{
Greg Clayton3acaa922012-09-25 02:40:06 +0000184 return m_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL);
Greg Clayton363be3f2011-07-15 03:27:12 +0000185}
186
187size_t
Greg Claytond52d00f2011-07-16 03:19:08 +0000188CommunicationKDP::WaitForPacketWithTimeoutMicroSeconds (DataExtractor &packet, uint32_t timeout_usec)
Greg Clayton363be3f2011-07-15 03:27:12 +0000189{
190 Mutex::Locker locker(m_sequence_mutex);
191 return WaitForPacketWithTimeoutMicroSecondsNoLock (packet, timeout_usec);
192}
193
194size_t
Greg Claytond52d00f2011-07-16 03:19:08 +0000195CommunicationKDP::WaitForPacketWithTimeoutMicroSecondsNoLock (DataExtractor &packet, uint32_t timeout_usec)
Greg Clayton363be3f2011-07-15 03:27:12 +0000196{
197 uint8_t buffer[8192];
198 Error error;
199
200 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS | KDP_LOG_VERBOSE));
201
202 // Check for a packet from our cache first without trying any reading...
203 if (CheckForPacket (NULL, 0, packet))
Greg Claytond52d00f2011-07-16 03:19:08 +0000204 return packet.GetByteSize();
Greg Clayton363be3f2011-07-15 03:27:12 +0000205
206 bool timed_out = false;
207 while (IsConnected() && !timed_out)
208 {
Johnny Chen1e3a5222011-07-21 19:00:59 +0000209 lldb::ConnectionStatus status = eConnectionStatusNoConnection;
Greg Clayton363be3f2011-07-15 03:27:12 +0000210 size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error);
211
212 if (log)
Greg Clayton851e30e2012-09-18 18:04:04 +0000213 log->Printf ("%s: Read (buffer, (sizeof(buffer), timeout_usec = 0x%x, status = %s, error = %s) => bytes_read = %llu",
Greg Clayton363be3f2011-07-15 03:27:12 +0000214 __PRETTY_FUNCTION__,
215 timeout_usec,
216 Communication::ConnectionStatusAsCString (status),
217 error.AsCString(),
Greg Clayton851e30e2012-09-18 18:04:04 +0000218 (uint64_t)bytes_read);
Greg Clayton363be3f2011-07-15 03:27:12 +0000219
220 if (bytes_read > 0)
221 {
222 if (CheckForPacket (buffer, bytes_read, packet))
Greg Claytond52d00f2011-07-16 03:19:08 +0000223 return packet.GetByteSize();
Greg Clayton363be3f2011-07-15 03:27:12 +0000224 }
225 else
226 {
227 switch (status)
228 {
229 case eConnectionStatusTimedOut:
230 timed_out = true;
231 break;
232 case eConnectionStatusSuccess:
233 //printf ("status = success but error = %s\n", error.AsCString("<invalid>"));
234 break;
235
236 case eConnectionStatusEndOfFile:
237 case eConnectionStatusNoConnection:
238 case eConnectionStatusLostConnection:
239 case eConnectionStatusError:
240 Disconnect();
241 break;
242 }
243 }
244 }
245 packet.Clear ();
246 return 0;
247}
248
249bool
Greg Claytond52d00f2011-07-16 03:19:08 +0000250CommunicationKDP::CheckForPacket (const uint8_t *src, size_t src_len, DataExtractor &packet)
Greg Clayton363be3f2011-07-15 03:27:12 +0000251{
252 // Put the packet data into the buffer in a thread safe fashion
253 Mutex::Locker locker(m_bytes_mutex);
254
255 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS));
256
257 if (src && src_len > 0)
258 {
259 if (log && log->GetVerbose())
260 {
Greg Claytond52d00f2011-07-16 03:19:08 +0000261 PacketStreamType log_strm;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000262 DataExtractor::DumpHexBytes (&log_strm, src, src_len, UINT32_MAX, LLDB_INVALID_ADDRESS);
Greg Claytond52d00f2011-07-16 03:19:08 +0000263 log->Printf ("CommunicationKDP::%s adding %u bytes: %s",
Greg Clayton363be3f2011-07-15 03:27:12 +0000264 __FUNCTION__,
265 (uint32_t)src_len,
Greg Claytond52d00f2011-07-16 03:19:08 +0000266 log_strm.GetData());
Greg Clayton363be3f2011-07-15 03:27:12 +0000267 }
268 m_bytes.append ((const char *)src, src_len);
269 }
270
Greg Claytond52d00f2011-07-16 03:19:08 +0000271 // Make sure we at least have enough bytes for a packet header
272 const size_t bytes_available = m_bytes.size();
273 if (bytes_available >= 8)
Greg Clayton363be3f2011-07-15 03:27:12 +0000274 {
Greg Claytond52d00f2011-07-16 03:19:08 +0000275 packet.SetData (&m_bytes[0], bytes_available, m_byte_order);
276 uint32_t offset = 0;
277 uint8_t reply_command = packet.GetU8(&offset);
278 switch (reply_command)
279 {
Greg Claytonea636012012-09-21 01:55:30 +0000280 case ePacketTypeRequest | KDP_EXCEPTION:
281 case ePacketTypeRequest | KDP_TERMINATION:
282 // We got an exception request, so be sure to send an ACK
283 {
284 PacketStreamType request_ack_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
285 // Set the reply but and make the ACK packet
286 request_ack_packet.PutHex8 (reply_command | ePacketTypeReply);
287 request_ack_packet.PutHex8 (packet.GetU8(&offset));
288 request_ack_packet.PutHex16 (packet.GetU16(&offset));
289 request_ack_packet.PutHex32 (packet.GetU32(&offset));
Greg Clayton3acaa922012-09-25 02:40:06 +0000290 m_is_running.SetValue(false, eBroadcastAlways);
Greg Claytonea636012012-09-21 01:55:30 +0000291 // Ack to the exception or termination
292 SendRequestPacketNoLock (request_ack_packet);
293 }
294 // Fall through to case below to get packet contents
Greg Clayton7b139222011-07-21 01:12:01 +0000295 case ePacketTypeReply | KDP_CONNECT:
296 case ePacketTypeReply | KDP_DISCONNECT:
297 case ePacketTypeReply | KDP_HOSTINFO:
298 case ePacketTypeReply | KDP_VERSION:
299 case ePacketTypeReply | KDP_MAXBYTES:
300 case ePacketTypeReply | KDP_READMEM:
301 case ePacketTypeReply | KDP_WRITEMEM:
302 case ePacketTypeReply | KDP_READREGS:
303 case ePacketTypeReply | KDP_WRITEREGS:
304 case ePacketTypeReply | KDP_LOAD:
305 case ePacketTypeReply | KDP_IMAGEPATH:
306 case ePacketTypeReply | KDP_SUSPEND:
307 case ePacketTypeReply | KDP_RESUMECPUS:
Greg Clayton7b139222011-07-21 01:12:01 +0000308 case ePacketTypeReply | KDP_BREAKPOINT_SET:
309 case ePacketTypeReply | KDP_BREAKPOINT_REMOVE:
310 case ePacketTypeReply | KDP_REGIONS:
311 case ePacketTypeReply | KDP_REATTACH:
312 case ePacketTypeReply | KDP_HOSTREBOOT:
313 case ePacketTypeReply | KDP_READMEM64:
314 case ePacketTypeReply | KDP_WRITEMEM64:
315 case ePacketTypeReply | KDP_BREAKPOINT_SET64:
316 case ePacketTypeReply | KDP_BREAKPOINT_REMOVE64:
317 case ePacketTypeReply | KDP_KERNELVERSION:
Greg Claytond52d00f2011-07-16 03:19:08 +0000318 {
319 offset = 2;
320 const uint16_t length = packet.GetU16 (&offset);
321 if (length <= bytes_available)
322 {
323 // We have an entire packet ready, we need to copy the data
324 // bytes into a buffer that will be owned by the packet and
325 // erase the bytes from our communcation buffer "m_bytes"
326 packet.SetData (DataBufferSP (new DataBufferHeap (&m_bytes[0], length)));
327 m_bytes.erase (0, length);
Greg Clayton8d2ea282011-07-17 20:36:25 +0000328
329 if (log)
330 {
331 PacketStreamType log_strm;
Greg Clayton0fa51242011-07-19 03:57:15 +0000332 DumpPacket (log_strm, packet);
Greg Clayton8d2ea282011-07-17 20:36:25 +0000333
Greg Clayton0fa51242011-07-19 03:57:15 +0000334 log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData());
Greg Clayton8d2ea282011-07-17 20:36:25 +0000335 }
Greg Claytond52d00f2011-07-16 03:19:08 +0000336 return true;
337 }
338 }
339 break;
340
341 default:
342 // Unrecognized reply command byte, erase this byte and try to get back on track
343 if (log)
344 log->Printf ("CommunicationKDP::%s: tossing junk byte: 0x%2.2x",
345 __FUNCTION__,
346 (uint8_t)m_bytes[0]);
347 m_bytes.erase(0, 1);
348 break;
349 }
Greg Clayton363be3f2011-07-15 03:27:12 +0000350 }
351 packet.Clear();
352 return false;
353}
354
Greg Clayton1e5b0212011-07-15 16:31:38 +0000355
Greg Claytond52d00f2011-07-16 03:19:08 +0000356bool
Greg Clayton8d2ea282011-07-17 20:36:25 +0000357CommunicationKDP::SendRequestConnect (uint16_t reply_port,
358 uint16_t exc_port,
359 const char *greeting)
Greg Clayton1e5b0212011-07-15 16:31:38 +0000360{
Greg Clayton0fa51242011-07-19 03:57:15 +0000361 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Claytond52d00f2011-07-16 03:19:08 +0000362 if (greeting == NULL)
363 greeting = "";
364
Greg Clayton7b139222011-07-21 01:12:01 +0000365 const CommandType command = KDP_CONNECT;
Greg Clayton8d2ea282011-07-17 20:36:25 +0000366 // Length is 82 uint16_t and the length of the greeting C string with the terminating NULL
367 const uint32_t command_length = 8 + 2 + 2 + ::strlen(greeting) + 1;
Greg Claytond52d00f2011-07-16 03:19:08 +0000368 const uint32_t request_sequence_id = m_request_sequence_id;
369 MakeRequestPacketHeader (command, request_packet, command_length);
Greg Clayton8d2ea282011-07-17 20:36:25 +0000370 // Always send connect ports as little endian
371 request_packet.SetByteOrder (eByteOrderLittle);
372 request_packet.PutHex16 (reply_port);
373 request_packet.PutHex16 (exc_port);
374 request_packet.SetByteOrder (m_byte_order);
375 request_packet.PutCString (greeting);
Greg Claytond52d00f2011-07-16 03:19:08 +0000376 DataExtractor reply_packet;
377 return SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet);
Greg Clayton1e5b0212011-07-15 16:31:38 +0000378}
379
Greg Claytond52d00f2011-07-16 03:19:08 +0000380void
381CommunicationKDP::ClearKDPSettings ()
382{
383 m_request_sequence_id = 0;
384 m_kdp_version_version = 0;
385 m_kdp_version_feature = 0;
386 m_kdp_hostinfo_cpu_mask = 0;
387 m_kdp_hostinfo_cpu_type = 0;
388 m_kdp_hostinfo_cpu_subtype = 0;
389}
390
391bool
Greg Clayton8d2ea282011-07-17 20:36:25 +0000392CommunicationKDP::SendRequestReattach (uint16_t reply_port)
Greg Claytond52d00f2011-07-16 03:19:08 +0000393{
Greg Clayton0fa51242011-07-19 03:57:15 +0000394 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton7b139222011-07-21 01:12:01 +0000395 const CommandType command = KDP_REATTACH;
Greg Claytond52d00f2011-07-16 03:19:08 +0000396 // Length is 8 bytes for the header plus 2 bytes for the reply UDP port
397 const uint32_t command_length = 8 + 2;
398 const uint32_t request_sequence_id = m_request_sequence_id;
399 MakeRequestPacketHeader (command, request_packet, command_length);
Greg Clayton8d2ea282011-07-17 20:36:25 +0000400 // Always send connect ports as little endian
401 request_packet.SetByteOrder (eByteOrderLittle);
Greg Claytond52d00f2011-07-16 03:19:08 +0000402 request_packet.PutHex16(reply_port);
Greg Clayton8d2ea282011-07-17 20:36:25 +0000403 request_packet.SetByteOrder (m_byte_order);
Greg Claytond52d00f2011-07-16 03:19:08 +0000404 DataExtractor reply_packet;
405 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
406 {
407 // Reset the sequence ID to zero for reattach
408 ClearKDPSettings ();
409 uint32_t offset = 4;
410 m_session_key = reply_packet.GetU32 (&offset);
411 return true;
412 }
413 return false;
414}
415
416uint32_t
417CommunicationKDP::GetVersion ()
418{
419 if (!VersionIsValid())
420 SendRequestVersion();
421 return m_kdp_version_version;
422}
423
424uint32_t
425CommunicationKDP::GetFeatureFlags ()
426{
427 if (!VersionIsValid())
428 SendRequestVersion();
429 return m_kdp_version_feature;
430}
431
432bool
433CommunicationKDP::SendRequestVersion ()
434{
Greg Clayton0fa51242011-07-19 03:57:15 +0000435 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton7b139222011-07-21 01:12:01 +0000436 const CommandType command = KDP_VERSION;
Greg Claytond52d00f2011-07-16 03:19:08 +0000437 const uint32_t command_length = 8;
438 const uint32_t request_sequence_id = m_request_sequence_id;
439 MakeRequestPacketHeader (command, request_packet, command_length);
440 DataExtractor reply_packet;
441 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
442 {
Greg Claytond52d00f2011-07-16 03:19:08 +0000443 uint32_t offset = 8;
444 m_kdp_version_version = reply_packet.GetU32 (&offset);
445 m_kdp_version_feature = reply_packet.GetU32 (&offset);
446 return true;
447 }
448 return false;
449}
450
Greg Clayton7b139222011-07-21 01:12:01 +0000451#if 0 // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
452const char *
453CommunicationKDP::GetImagePath ()
454{
455 if (m_image_path.empty())
456 SendRequestImagePath();
457 return m_image_path.c_str();
458}
459
460bool
461CommunicationKDP::SendRequestImagePath ()
462{
463 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
464 const CommandType command = KDP_IMAGEPATH;
465 const uint32_t command_length = 8;
466 const uint32_t request_sequence_id = m_request_sequence_id;
467 MakeRequestPacketHeader (command, request_packet, command_length);
468 DataExtractor reply_packet;
469 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
470 {
471 const char *path = reply_packet.PeekCStr(8);
472 if (path && path[0])
473 m_kernel_version.assign (path);
474 return true;
475 }
476 return false;
477}
478#endif
479
Greg Claytond52d00f2011-07-16 03:19:08 +0000480uint32_t
481CommunicationKDP::GetCPUMask ()
482{
483 if (!HostInfoIsValid())
484 SendRequestHostInfo();
485 return m_kdp_hostinfo_cpu_mask;
486}
487
488uint32_t
489CommunicationKDP::GetCPUType ()
490{
491 if (!HostInfoIsValid())
492 SendRequestHostInfo();
493 return m_kdp_hostinfo_cpu_type;
494}
495
496uint32_t
497CommunicationKDP::GetCPUSubtype ()
498{
499 if (!HostInfoIsValid())
500 SendRequestHostInfo();
501 return m_kdp_hostinfo_cpu_subtype;
502}
503
Jason Molendafac2e622012-09-29 04:02:01 +0000504lldb_private::UUID
505CommunicationKDP::GetUUID ()
506{
507 UUID uuid;
508 if (GetKernelVersion() == NULL)
509 return uuid;
510
511 if (m_kernel_version.find("UUID=") == std::string::npos)
512 return uuid;
513
514 size_t p = m_kernel_version.find("UUID=") + strlen ("UUID=");
515 std::string uuid_str = m_kernel_version.substr(p, 36);
516 if (uuid_str.size() < 32)
517 return uuid;
518
519 if (uuid.SetFromCString (uuid_str.c_str()) == 0)
520 {
521 UUID invalid_uuid;
522 return invalid_uuid;
523 }
524
525 return uuid;
526}
527
528lldb::addr_t
529CommunicationKDP::GetLoadAddress ()
530{
531 if (GetKernelVersion() == NULL)
532 return LLDB_INVALID_ADDRESS;
533
534 if (m_kernel_version.find("stext=") == std::string::npos)
535 return LLDB_INVALID_ADDRESS;
536 size_t p = m_kernel_version.find("stext=") + strlen ("stext=");
537 if (m_kernel_version[p] != '0' || m_kernel_version[p + 1] != 'x')
538 return LLDB_INVALID_ADDRESS;
539
540 addr_t kernel_load_address;
541 errno = 0;
542 kernel_load_address = ::strtoul (m_kernel_version.c_str() + p, NULL, 16);
543 if (errno != 0 || kernel_load_address == 0)
544 return LLDB_INVALID_ADDRESS;
545
546 return kernel_load_address;
547}
548
Greg Claytond52d00f2011-07-16 03:19:08 +0000549bool
550CommunicationKDP::SendRequestHostInfo ()
551{
Greg Clayton0fa51242011-07-19 03:57:15 +0000552 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton7b139222011-07-21 01:12:01 +0000553 const CommandType command = KDP_HOSTINFO;
Greg Claytond52d00f2011-07-16 03:19:08 +0000554 const uint32_t command_length = 8;
555 const uint32_t request_sequence_id = m_request_sequence_id;
556 MakeRequestPacketHeader (command, request_packet, command_length);
557 DataExtractor reply_packet;
558 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
559 {
Greg Claytond52d00f2011-07-16 03:19:08 +0000560 uint32_t offset = 8;
Greg Clayton0fa51242011-07-19 03:57:15 +0000561 m_kdp_hostinfo_cpu_mask = reply_packet.GetU32 (&offset);
562 m_kdp_hostinfo_cpu_type = reply_packet.GetU32 (&offset);
563 m_kdp_hostinfo_cpu_subtype = reply_packet.GetU32 (&offset);
564
565 ArchSpec kernel_arch;
566 kernel_arch.SetArchitecture (eArchTypeMachO,
567 m_kdp_hostinfo_cpu_type,
568 m_kdp_hostinfo_cpu_subtype);
569
570 m_addr_byte_size = kernel_arch.GetAddressByteSize();
571 m_byte_order = kernel_arch.GetByteOrder();
Greg Claytond52d00f2011-07-16 03:19:08 +0000572 return true;
573 }
574 return false;
575}
576
Greg Clayton234981a2011-07-20 03:41:06 +0000577const char *
578CommunicationKDP::GetKernelVersion ()
579{
580 if (m_kernel_version.empty())
581 SendRequestKernelVersion ();
582 return m_kernel_version.c_str();
583}
584
585bool
586CommunicationKDP::SendRequestKernelVersion ()
587{
588 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton7b139222011-07-21 01:12:01 +0000589 const CommandType command = KDP_KERNELVERSION;
Greg Clayton234981a2011-07-20 03:41:06 +0000590 const uint32_t command_length = 8;
591 const uint32_t request_sequence_id = m_request_sequence_id;
592 MakeRequestPacketHeader (command, request_packet, command_length);
593 DataExtractor reply_packet;
594 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
595 {
596 const char *kernel_version_cstr = reply_packet.PeekCStr(8);
597 if (kernel_version_cstr && kernel_version_cstr[0])
598 m_kernel_version.assign (kernel_version_cstr);
599 return true;
600 }
601 return false;
602}
603
Greg Claytond52d00f2011-07-16 03:19:08 +0000604bool
Greg Clayton8d2ea282011-07-17 20:36:25 +0000605CommunicationKDP::SendRequestDisconnect ()
Greg Clayton1e5b0212011-07-15 16:31:38 +0000606{
Greg Clayton0fa51242011-07-19 03:57:15 +0000607 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton7b139222011-07-21 01:12:01 +0000608 const CommandType command = KDP_DISCONNECT;
Greg Claytond52d00f2011-07-16 03:19:08 +0000609 const uint32_t command_length = 8;
610 const uint32_t request_sequence_id = m_request_sequence_id;
611 MakeRequestPacketHeader (command, request_packet, command_length);
612 DataExtractor reply_packet;
613 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
614 {
615 // Are we supposed to get a reply for disconnect?
616 }
617 ClearKDPSettings ();
618 return true;
Greg Clayton1e5b0212011-07-15 16:31:38 +0000619}
620
Greg Clayton0fa51242011-07-19 03:57:15 +0000621uint32_t
622CommunicationKDP::SendRequestReadMemory (lldb::addr_t addr,
623 void *dst,
624 uint32_t dst_len,
625 Error &error)
626{
627 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
628 bool use_64 = (GetVersion() >= 11);
629 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
Greg Clayton7b139222011-07-21 01:12:01 +0000630 const CommandType command = use_64 ? KDP_READMEM64 : KDP_READMEM;
Greg Clayton0fa51242011-07-19 03:57:15 +0000631 // Size is header + address size + uint32_t length
632 const uint32_t command_length = 8 + command_addr_byte_size + 4;
633 const uint32_t request_sequence_id = m_request_sequence_id;
634 MakeRequestPacketHeader (command, request_packet, command_length);
635 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
636 request_packet.PutHex32 (dst_len);
637 DataExtractor reply_packet;
638 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
639 {
Greg Clayton0fa51242011-07-19 03:57:15 +0000640 uint32_t offset = 8;
641 uint32_t kdp_error = reply_packet.GetU32 (&offset);
642 uint32_t src_len = reply_packet.GetByteSize() - 12;
643
644 if (src_len > 0)
645 {
646 const void *src = reply_packet.GetData(&offset, src_len);
647 if (src)
648 {
649 ::memcpy (dst, src, src_len);
650 error.Clear();
651 return src_len;
652 }
653 }
654 if (kdp_error)
655 error.SetErrorStringWithFormat ("kdp read memory failed (error %u)", kdp_error);
656 else
657 error.SetErrorString ("kdp read memory failed");
658 }
Greg Clayton307c7fd2012-10-19 22:22:57 +0000659 else
660 {
661 error.SetErrorString ("failed to send packet");
662 }
Greg Clayton0fa51242011-07-19 03:57:15 +0000663 return 0;
664}
665
Greg Claytonec15f502011-07-20 01:32:50 +0000666
667uint32_t
668CommunicationKDP::SendRequestWriteMemory (lldb::addr_t addr,
669 const void *src,
670 uint32_t src_len,
671 Error &error)
672{
673 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
674 bool use_64 = (GetVersion() >= 11);
675 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
Greg Clayton7b139222011-07-21 01:12:01 +0000676 const CommandType command = use_64 ? KDP_WRITEMEM64 : KDP_WRITEMEM;
Greg Claytonec15f502011-07-20 01:32:50 +0000677 // Size is header + address size + uint32_t length
Greg Claytonfe3150f2012-10-12 23:24:05 +0000678 const uint32_t command_length = 8 + command_addr_byte_size + 4 + src_len;
Greg Claytonec15f502011-07-20 01:32:50 +0000679 const uint32_t request_sequence_id = m_request_sequence_id;
680 MakeRequestPacketHeader (command, request_packet, command_length);
681 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
682 request_packet.PutHex32 (src_len);
Jason Molenda62377462012-10-19 02:16:22 +0000683 request_packet.PutRawBytes(src, src_len);
Greg Claytonec15f502011-07-20 01:32:50 +0000684
685 DataExtractor reply_packet;
686 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
687 {
688 uint32_t offset = 8;
689 uint32_t kdp_error = reply_packet.GetU32 (&offset);
690 if (kdp_error)
691 error.SetErrorStringWithFormat ("kdp write memory failed (error %u)", kdp_error);
692 else
693 {
694 error.Clear();
695 return src_len;
696 }
697 }
Greg Clayton307c7fd2012-10-19 22:22:57 +0000698 else
699 {
700 error.SetErrorString ("failed to send packet");
701 }
Greg Claytonec15f502011-07-20 01:32:50 +0000702 return 0;
703}
704
Greg Clayton307c7fd2012-10-19 22:22:57 +0000705bool
706CommunicationKDP::SendRawRequest (uint8_t command_byte,
707 const void *src, // Raw packet payload bytes
708 uint32_t src_len, // Raw packet payload length
709 DataExtractor &reply_packet,
710 Error &error)
711{
712 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
713 // Size is header + address size + uint32_t length
714 const uint32_t command_length = 8 + src_len;
715 const CommandType command = (CommandType)command_byte;
716 const uint32_t request_sequence_id = m_request_sequence_id;
717 MakeRequestPacketHeader (command, request_packet, command_length);
718 request_packet.PutRawBytes(src, src_len);
719
720 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
721 {
722 uint32_t offset = 8;
723 uint32_t kdp_error = reply_packet.GetU32 (&offset);
724 if (kdp_error)
725 error.SetErrorStringWithFormat ("request packet 0x%8.8x failed (error %u)", command_byte, kdp_error);
726 else
727 {
728 error.Clear();
729 return true;
730 }
731 }
732 else
733 {
734 error.SetErrorString ("failed to send packet");
735 }
736 return false;
737}
738
739
Greg Clayton0fa51242011-07-19 03:57:15 +0000740const char *
741CommunicationKDP::GetCommandAsCString (uint8_t command)
742{
743 switch (command)
744 {
Greg Clayton7b139222011-07-21 01:12:01 +0000745 case KDP_CONNECT: return "KDP_CONNECT";
746 case KDP_DISCONNECT: return "KDP_DISCONNECT";
747 case KDP_HOSTINFO: return "KDP_HOSTINFO";
748 case KDP_VERSION: return "KDP_VERSION";
749 case KDP_MAXBYTES: return "KDP_MAXBYTES";
750 case KDP_READMEM: return "KDP_READMEM";
751 case KDP_WRITEMEM: return "KDP_WRITEMEM";
752 case KDP_READREGS: return "KDP_READREGS";
753 case KDP_WRITEREGS: return "KDP_WRITEREGS";
754 case KDP_LOAD: return "KDP_LOAD";
755 case KDP_IMAGEPATH: return "KDP_IMAGEPATH";
756 case KDP_SUSPEND: return "KDP_SUSPEND";
757 case KDP_RESUMECPUS: return "KDP_RESUMECPUS";
758 case KDP_EXCEPTION: return "KDP_EXCEPTION";
759 case KDP_TERMINATION: return "KDP_TERMINATION";
760 case KDP_BREAKPOINT_SET: return "KDP_BREAKPOINT_SET";
761 case KDP_BREAKPOINT_REMOVE: return "KDP_BREAKPOINT_REMOVE";
762 case KDP_REGIONS: return "KDP_REGIONS";
763 case KDP_REATTACH: return "KDP_REATTACH";
764 case KDP_HOSTREBOOT: return "KDP_HOSTREBOOT";
765 case KDP_READMEM64: return "KDP_READMEM64";
766 case KDP_WRITEMEM64: return "KDP_WRITEMEM64";
767 case KDP_BREAKPOINT_SET64: return "KDP_BREAKPOINT64_SET";
768 case KDP_BREAKPOINT_REMOVE64: return "KDP_BREAKPOINT64_REMOVE";
769 case KDP_KERNELVERSION: return "KDP_KERNELVERSION";
Greg Clayton0fa51242011-07-19 03:57:15 +0000770 }
771 return NULL;
772}
773
774void
775CommunicationKDP::DumpPacket (Stream &s, const void *data, uint32_t data_len)
776{
777 DataExtractor extractor (data, data_len, m_byte_order, m_addr_byte_size);
778 DumpPacket (s, extractor);
779}
780
781void
782CommunicationKDP::DumpPacket (Stream &s, const DataExtractor& packet)
783{
784 const char *error_desc = NULL;
785 if (packet.GetByteSize() < 8)
786 {
787 error_desc = "error: invalid packet (too short): ";
788 }
789 else
790 {
791 uint32_t offset = 0;
792 const uint8_t first_packet_byte = packet.GetU8 (&offset);
793 const uint8_t sequence_id = packet.GetU8 (&offset);
794 const uint16_t length = packet.GetU16 (&offset);
795 const uint32_t key = packet.GetU32 (&offset);
796 const CommandType command = ExtractCommand (first_packet_byte);
797 const char *command_name = GetCommandAsCString (command);
798 if (command_name)
799 {
800 const bool is_reply = ExtractIsReply(first_packet_byte);
Greg Clayton3acaa922012-09-25 02:40:06 +0000801 s.Printf ("(running=%i) %s %24s: 0x%2.2x 0x%2.2x 0x%4.4x 0x%8.8x ",
802 IsRunning(),
803 is_reply ? "<--" : "-->",
804 command_name,
805 first_packet_byte,
Greg Clayton0fa51242011-07-19 03:57:15 +0000806 sequence_id,
807 length,
Greg Clayton3acaa922012-09-25 02:40:06 +0000808 key);
Greg Clayton0fa51242011-07-19 03:57:15 +0000809
810 if (is_reply)
811 {
812 // Dump request reply packets
813 switch (command)
814 {
Greg Claytonec15f502011-07-20 01:32:50 +0000815 // Commands that return a single 32 bit error
Greg Clayton7b139222011-07-21 01:12:01 +0000816 case KDP_CONNECT:
817 case KDP_WRITEMEM:
818 case KDP_WRITEMEM64:
819 case KDP_BREAKPOINT_SET:
820 case KDP_BREAKPOINT_REMOVE:
821 case KDP_BREAKPOINT_SET64:
822 case KDP_BREAKPOINT_REMOVE64:
823 case KDP_WRITEREGS:
824 case KDP_LOAD:
Greg Clayton0fa51242011-07-19 03:57:15 +0000825 {
826 const uint32_t error = packet.GetU32 (&offset);
827 s.Printf(" (error=0x%8.8x)", error);
828 }
829 break;
830
Greg Clayton7b139222011-07-21 01:12:01 +0000831 case KDP_DISCONNECT:
832 case KDP_REATTACH:
833 case KDP_HOSTREBOOT:
834 case KDP_SUSPEND:
835 case KDP_RESUMECPUS:
836 case KDP_EXCEPTION:
837 case KDP_TERMINATION:
Greg Clayton0fa51242011-07-19 03:57:15 +0000838 // No return value for the reply, just the header to ack
Greg Claytonec15f502011-07-20 01:32:50 +0000839 s.PutCString(" ()");
Greg Clayton0fa51242011-07-19 03:57:15 +0000840 break;
841
Greg Clayton7b139222011-07-21 01:12:01 +0000842 case KDP_HOSTINFO:
Greg Clayton0fa51242011-07-19 03:57:15 +0000843 {
844 const uint32_t cpu_mask = packet.GetU32 (&offset);
845 const uint32_t cpu_type = packet.GetU32 (&offset);
846 const uint32_t cpu_subtype = packet.GetU32 (&offset);
847 s.Printf(" (cpu_mask=0x%8.8x, cpu_type=0x%8.8x, cpu_subtype=0x%8.8x)", cpu_mask, cpu_type, cpu_subtype);
848 }
849 break;
850
Greg Clayton7b139222011-07-21 01:12:01 +0000851 case KDP_VERSION:
Greg Clayton0fa51242011-07-19 03:57:15 +0000852 {
853 const uint32_t version = packet.GetU32 (&offset);
854 const uint32_t feature = packet.GetU32 (&offset);
855 s.Printf(" (version=0x%8.8x, feature=0x%8.8x)", version, feature);
856 }
857 break;
858
Greg Clayton7b139222011-07-21 01:12:01 +0000859 case KDP_REGIONS:
Greg Clayton0fa51242011-07-19 03:57:15 +0000860 {
861 const uint32_t region_count = packet.GetU32 (&offset);
862 s.Printf(" (count = %u", region_count);
863 for (uint32_t i=0; i<region_count; ++i)
864 {
865 const addr_t region_addr = packet.GetPointer (&offset);
866 const uint32_t region_size = packet.GetU32 (&offset);
867 const uint32_t region_prot = packet.GetU32 (&offset);
Jason Molenda7e5fa7f2011-09-20 21:44:10 +0000868 s.Printf("\n\tregion[%llu] = { range = [0x%16.16llx - 0x%16.16llx), size = 0x%8.8x, prot = %s }", region_addr, region_addr, region_addr + region_size, region_size, GetPermissionsAsCString (region_prot));
Greg Clayton0fa51242011-07-19 03:57:15 +0000869 }
870 }
871 break;
872
Greg Clayton7b139222011-07-21 01:12:01 +0000873 case KDP_READMEM:
874 case KDP_READMEM64:
Greg Clayton0fa51242011-07-19 03:57:15 +0000875 {
876 const uint32_t error = packet.GetU32 (&offset);
Greg Claytonec15f502011-07-20 01:32:50 +0000877 const uint32_t count = packet.GetByteSize() - offset;
Greg Clayton1bc04ed2011-07-29 22:26:36 +0000878 s.Printf(" (error = 0x%8.8x:\n", error);
Greg Clayton0fa51242011-07-19 03:57:15 +0000879 if (count > 0)
Greg Clayton234981a2011-07-20 03:41:06 +0000880 packet.Dump (&s, // Stream to dump to
881 offset, // Offset within "packet"
882 eFormatBytesWithASCII, // Format to use
883 1, // Size of each item in bytes
884 count, // Number of items
885 16, // Number per line
886 m_last_read_memory_addr, // Don't show addresses before each line
887 0, 0); // No bitfields
Greg Clayton0fa51242011-07-19 03:57:15 +0000888 }
889 break;
890
Greg Clayton7b139222011-07-21 01:12:01 +0000891 case KDP_READREGS:
Greg Claytonec15f502011-07-20 01:32:50 +0000892 {
893 const uint32_t error = packet.GetU32 (&offset);
894 const uint32_t count = packet.GetByteSize() - offset;
Greg Clayton1bc04ed2011-07-29 22:26:36 +0000895 s.Printf(" (error = 0x%8.8x regs:\n", error);
Greg Claytonec15f502011-07-20 01:32:50 +0000896 if (count > 0)
897 packet.Dump (&s, // Stream to dump to
898 offset, // Offset within "packet"
899 eFormatHex, // Format to use
900 m_addr_byte_size, // Size of each item in bytes
901 count / m_addr_byte_size, // Number of items
902 16 / m_addr_byte_size, // Number per line
903 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
904 0, 0); // No bitfields
905 }
906 break;
907
Greg Clayton7b139222011-07-21 01:12:01 +0000908 case KDP_KERNELVERSION:
Greg Clayton234981a2011-07-20 03:41:06 +0000909 {
910 const char *kernel_version = packet.PeekCStr(8);
911 s.Printf(" (version = \"%s\")", kernel_version);
912 }
913 break;
914
Greg Clayton7b139222011-07-21 01:12:01 +0000915 case KDP_MAXBYTES:
Greg Clayton234981a2011-07-20 03:41:06 +0000916 {
917 const uint32_t max_bytes = packet.GetU32 (&offset);
918 s.Printf(" (max_bytes = 0x%8.8x (%u))", max_bytes, max_bytes);
919 }
920 break;
Greg Clayton7b139222011-07-21 01:12:01 +0000921 case KDP_IMAGEPATH:
Greg Clayton234981a2011-07-20 03:41:06 +0000922 {
923 const char *path = packet.GetCStr(&offset);
924 s.Printf(" (path = \"%s\")", path);
925 }
926 break;
927 default:
Greg Claytonec15f502011-07-20 01:32:50 +0000928 s.Printf(" (add support for dumping this packet reply!!!");
Greg Clayton0fa51242011-07-19 03:57:15 +0000929 break;
930
931 }
932 }
933 else
934 {
935 // Dump request packets
936 switch (command)
937 {
Greg Clayton7b139222011-07-21 01:12:01 +0000938 case KDP_CONNECT:
Greg Clayton0fa51242011-07-19 03:57:15 +0000939 {
940 const uint16_t reply_port = packet.GetU16 (&offset);
941 const uint16_t exc_port = packet.GetU16 (&offset);
Greg Clayton1bc04ed2011-07-29 22:26:36 +0000942 s.Printf(" (reply_port = %u, exc_port = %u, greeting = \"%s\")", reply_port, exc_port, packet.GetCStr(&offset));
Greg Clayton0fa51242011-07-19 03:57:15 +0000943 }
944 break;
945
Greg Clayton7b139222011-07-21 01:12:01 +0000946 case KDP_DISCONNECT:
947 case KDP_HOSTREBOOT:
948 case KDP_HOSTINFO:
949 case KDP_VERSION:
950 case KDP_REGIONS:
951 case KDP_KERNELVERSION:
952 case KDP_MAXBYTES:
953 case KDP_IMAGEPATH:
954 case KDP_SUSPEND:
Greg Clayton0fa51242011-07-19 03:57:15 +0000955 // No args, just the header in the request...
Greg Clayton234981a2011-07-20 03:41:06 +0000956 s.PutCString(" ()");
957 break;
958
Greg Clayton7b139222011-07-21 01:12:01 +0000959 case KDP_RESUMECPUS:
Greg Clayton234981a2011-07-20 03:41:06 +0000960 {
961 const uint32_t cpu_mask = packet.GetU32 (&offset);
962 s.Printf(" (cpu_mask = 0x%8.8x)", cpu_mask);
963 }
Greg Clayton0fa51242011-07-19 03:57:15 +0000964 break;
965
Greg Clayton7b139222011-07-21 01:12:01 +0000966 case KDP_READMEM:
Greg Clayton0fa51242011-07-19 03:57:15 +0000967 {
968 const uint32_t addr = packet.GetU32 (&offset);
969 const uint32_t size = packet.GetU32 (&offset);
Greg Clayton1bc04ed2011-07-29 22:26:36 +0000970 s.Printf(" (addr = 0x%8.8x, size = %u)", addr, size);
Greg Clayton234981a2011-07-20 03:41:06 +0000971 m_last_read_memory_addr = addr;
Greg Clayton0fa51242011-07-19 03:57:15 +0000972 }
973 break;
974
Greg Clayton7b139222011-07-21 01:12:01 +0000975 case KDP_WRITEMEM:
Greg Claytonec15f502011-07-20 01:32:50 +0000976 {
977 const uint32_t addr = packet.GetU32 (&offset);
978 const uint32_t size = packet.GetU32 (&offset);
Greg Clayton1bc04ed2011-07-29 22:26:36 +0000979 s.Printf(" (addr = 0x%8.8x, size = %u, bytes = \n", addr, size);
Greg Claytonec15f502011-07-20 01:32:50 +0000980 if (size > 0)
981 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
982 }
983 break;
984
Greg Clayton7b139222011-07-21 01:12:01 +0000985 case KDP_READMEM64:
Greg Clayton0fa51242011-07-19 03:57:15 +0000986 {
987 const uint64_t addr = packet.GetU64 (&offset);
988 const uint32_t size = packet.GetU32 (&offset);
Greg Clayton1bc04ed2011-07-29 22:26:36 +0000989 s.Printf(" (addr = 0x%16.16llx, size = %u)", addr, size);
Greg Clayton234981a2011-07-20 03:41:06 +0000990 m_last_read_memory_addr = addr;
Greg Clayton0fa51242011-07-19 03:57:15 +0000991 }
992 break;
993
Greg Clayton7b139222011-07-21 01:12:01 +0000994 case KDP_WRITEMEM64:
Greg Claytonec15f502011-07-20 01:32:50 +0000995 {
996 const uint64_t addr = packet.GetU64 (&offset);
997 const uint32_t size = packet.GetU32 (&offset);
Greg Clayton1bc04ed2011-07-29 22:26:36 +0000998 s.Printf(" (addr = 0x%16.16llx, size = %u, bytes = \n", addr, size);
Greg Claytonec15f502011-07-20 01:32:50 +0000999 if (size > 0)
1000 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
1001 }
1002 break;
1003
Greg Clayton7b139222011-07-21 01:12:01 +00001004 case KDP_READREGS:
Greg Clayton0fa51242011-07-19 03:57:15 +00001005 {
1006 const uint32_t cpu = packet.GetU32 (&offset);
1007 const uint32_t flavor = packet.GetU32 (&offset);
Greg Clayton1bc04ed2011-07-29 22:26:36 +00001008 s.Printf(" (cpu = %u, flavor = %u)", cpu, flavor);
Greg Clayton0fa51242011-07-19 03:57:15 +00001009 }
1010 break;
1011
Greg Clayton7b139222011-07-21 01:12:01 +00001012 case KDP_WRITEREGS:
Greg Claytonec15f502011-07-20 01:32:50 +00001013 {
1014 const uint32_t cpu = packet.GetU32 (&offset);
1015 const uint32_t flavor = packet.GetU32 (&offset);
1016 const uint32_t nbytes = packet.GetByteSize() - offset;
Greg Clayton1bc04ed2011-07-29 22:26:36 +00001017 s.Printf(" (cpu = %u, flavor = %u, regs = \n", cpu, flavor);
Greg Claytonec15f502011-07-20 01:32:50 +00001018 if (nbytes > 0)
1019 packet.Dump (&s, // Stream to dump to
1020 offset, // Offset within "packet"
1021 eFormatHex, // Format to use
1022 m_addr_byte_size, // Size of each item in bytes
1023 nbytes / m_addr_byte_size, // Number of items
1024 16 / m_addr_byte_size, // Number per line
1025 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1026 0, 0); // No bitfields
1027 }
1028 break;
1029
Greg Claytonec15f502011-07-20 01:32:50 +00001030
Greg Clayton7b139222011-07-21 01:12:01 +00001031 case KDP_BREAKPOINT_SET:
1032 case KDP_BREAKPOINT_REMOVE:
Greg Clayton234981a2011-07-20 03:41:06 +00001033 {
1034 const uint32_t addr = packet.GetU32 (&offset);
1035 s.Printf(" (addr = 0x%8.8x)", addr);
1036 }
1037 break;
1038
Greg Clayton7b139222011-07-21 01:12:01 +00001039 case KDP_BREAKPOINT_SET64:
1040 case KDP_BREAKPOINT_REMOVE64:
Greg Clayton234981a2011-07-20 03:41:06 +00001041 {
1042 const uint64_t addr = packet.GetU64 (&offset);
1043 s.Printf(" (addr = 0x%16.16llx)", addr);
1044 }
1045 break;
1046
1047
Greg Clayton7b139222011-07-21 01:12:01 +00001048 case KDP_LOAD:
Greg Clayton234981a2011-07-20 03:41:06 +00001049 {
1050 const char *path = packet.GetCStr(&offset);
1051 s.Printf(" (path = \"%s\")", path);
1052 }
1053 break;
1054
Greg Clayton7b139222011-07-21 01:12:01 +00001055 case KDP_EXCEPTION:
Greg Clayton234981a2011-07-20 03:41:06 +00001056 {
1057 const uint32_t count = packet.GetU32 (&offset);
1058
Greg Clayton234981a2011-07-20 03:41:06 +00001059 for (uint32_t i=0; i<count; ++i)
1060 {
1061 const uint32_t cpu = packet.GetU32 (&offset);
1062 const uint32_t exc = packet.GetU32 (&offset);
1063 const uint32_t code = packet.GetU32 (&offset);
1064 const uint32_t subcode = packet.GetU32 (&offset);
1065 const char *exc_cstr = NULL;
1066 switch (exc)
1067 {
1068 case 1: exc_cstr = "EXC_BAD_ACCESS"; break;
1069 case 2: exc_cstr = "EXC_BAD_INSTRUCTION"; break;
1070 case 3: exc_cstr = "EXC_ARITHMETIC"; break;
1071 case 4: exc_cstr = "EXC_EMULATION"; break;
1072 case 5: exc_cstr = "EXC_SOFTWARE"; break;
1073 case 6: exc_cstr = "EXC_BREAKPOINT"; break;
1074 case 7: exc_cstr = "EXC_SYSCALL"; break;
1075 case 8: exc_cstr = "EXC_MACH_SYSCALL"; break;
1076 case 9: exc_cstr = "EXC_RPC_ALERT"; break;
1077 case 10: exc_cstr = "EXC_CRASH"; break;
1078 default:
1079 break;
1080 }
1081
Greg Clayton3acaa922012-09-25 02:40:06 +00001082 s.Printf ("{ cpu = 0x%8.8x, exc = %s (%u), code = %u (0x%8.8x), subcode = %u (0x%8.8x)} ",
Greg Clayton234981a2011-07-20 03:41:06 +00001083 cpu, exc_cstr, exc, code, code, subcode, subcode);
1084 }
1085 }
1086 break;
1087
Greg Clayton7b139222011-07-21 01:12:01 +00001088 case KDP_TERMINATION:
Greg Clayton234981a2011-07-20 03:41:06 +00001089 {
1090 const uint32_t term_code = packet.GetU32 (&offset);
1091 const uint32_t exit_code = packet.GetU32 (&offset);
1092 s.Printf(" (term_code = 0x%8.8x (%u), exit_code = 0x%8.8x (%u))", term_code, term_code, exit_code, exit_code);
1093 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001094 break;
1095
Greg Clayton7b139222011-07-21 01:12:01 +00001096 case KDP_REATTACH:
Greg Clayton0fa51242011-07-19 03:57:15 +00001097 {
1098 const uint16_t reply_port = packet.GetU16 (&offset);
Greg Clayton1bc04ed2011-07-29 22:26:36 +00001099 s.Printf(" (reply_port = %u)", reply_port);
Greg Clayton0fa51242011-07-19 03:57:15 +00001100 }
1101 break;
Greg Clayton0fa51242011-07-19 03:57:15 +00001102 }
1103 }
1104 }
1105 else
1106 {
1107 error_desc = "error: invalid packet command: ";
1108 }
1109 }
1110
1111 if (error_desc)
1112 {
1113 s.PutCString (error_desc);
1114
1115 packet.Dump (&s, // Stream to dump to
1116 0, // Offset into "packet"
1117 eFormatBytes, // Dump as hex bytes
1118 1, // Size of each item is 1 for single bytes
1119 packet.GetByteSize(), // Number of bytes
1120 UINT32_MAX, // Num bytes per line
1121 LLDB_INVALID_ADDRESS, // Base address
1122 0, 0); // Bitfield info set to not do anything bitfield related
1123 }
1124}
1125
1126uint32_t
1127CommunicationKDP::SendRequestReadRegisters (uint32_t cpu,
1128 uint32_t flavor,
Greg Claytonea636012012-09-21 01:55:30 +00001129 void *dst,
Greg Clayton0fa51242011-07-19 03:57:15 +00001130 uint32_t dst_len,
1131 Error &error)
1132{
1133 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton7b139222011-07-21 01:12:01 +00001134 const CommandType command = KDP_READREGS;
Greg Clayton0fa51242011-07-19 03:57:15 +00001135 // Size is header + 4 byte cpu and 4 byte flavor
1136 const uint32_t command_length = 8 + 4 + 4;
1137 const uint32_t request_sequence_id = m_request_sequence_id;
1138 MakeRequestPacketHeader (command, request_packet, command_length);
1139 request_packet.PutHex32 (cpu);
1140 request_packet.PutHex32 (flavor);
1141 DataExtractor reply_packet;
1142 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
1143 {
Greg Clayton0fa51242011-07-19 03:57:15 +00001144 uint32_t offset = 8;
1145 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1146 uint32_t src_len = reply_packet.GetByteSize() - 12;
1147
1148 if (src_len > 0)
1149 {
1150 const uint32_t bytes_to_copy = std::min<uint32_t>(src_len, dst_len);
1151 const void *src = reply_packet.GetData(&offset, bytes_to_copy);
1152 if (src)
1153 {
1154 ::memcpy (dst, src, bytes_to_copy);
1155 error.Clear();
1156 // Return the number of bytes we could have returned regardless if
1157 // we copied them or not, just so we know when things don't match up
Greg Claytonea636012012-09-21 01:55:30 +00001158 return src_len;
Greg Clayton0fa51242011-07-19 03:57:15 +00001159 }
1160 }
1161 if (kdp_error)
1162 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error);
1163 else
1164 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u", cpu, flavor);
1165 }
Greg Clayton307c7fd2012-10-19 22:22:57 +00001166 else
1167 {
1168 error.SetErrorString ("failed to send packet");
1169 }
Greg Clayton0fa51242011-07-19 03:57:15 +00001170 return 0;
1171}
1172
Greg Claytonea636012012-09-21 01:55:30 +00001173uint32_t
1174CommunicationKDP::SendRequestWriteRegisters (uint32_t cpu,
1175 uint32_t flavor,
1176 const void *src,
1177 uint32_t src_len,
1178 Error &error)
1179{
1180 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
1181 const CommandType command = KDP_WRITEREGS;
1182 // Size is header + 4 byte cpu and 4 byte flavor
Greg Clayton3acaa922012-09-25 02:40:06 +00001183 const uint32_t command_length = 8 + 4 + 4 + src_len;
Greg Claytonea636012012-09-21 01:55:30 +00001184 const uint32_t request_sequence_id = m_request_sequence_id;
1185 MakeRequestPacketHeader (command, request_packet, command_length);
1186 request_packet.PutHex32 (cpu);
1187 request_packet.PutHex32 (flavor);
1188 request_packet.Write(src, src_len);
1189 DataExtractor reply_packet;
1190 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
1191 {
1192 uint32_t offset = 8;
1193 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1194 if (kdp_error == 0)
1195 return src_len;
1196 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error);
1197 }
Greg Clayton307c7fd2012-10-19 22:22:57 +00001198 else
1199 {
1200 error.SetErrorString ("failed to send packet");
1201 }
Greg Claytonea636012012-09-21 01:55:30 +00001202 return 0;
1203}
1204
Greg Clayton234981a2011-07-20 03:41:06 +00001205
1206bool
Greg Clayton3acaa922012-09-25 02:40:06 +00001207CommunicationKDP::SendRequestResume ()
Greg Clayton234981a2011-07-20 03:41:06 +00001208{
Greg Clayton234981a2011-07-20 03:41:06 +00001209 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton7b139222011-07-21 01:12:01 +00001210 const CommandType command = KDP_RESUMECPUS;
Greg Clayton234981a2011-07-20 03:41:06 +00001211 const uint32_t command_length = 12;
1212 const uint32_t request_sequence_id = m_request_sequence_id;
1213 MakeRequestPacketHeader (command, request_packet, command_length);
Greg Clayton3acaa922012-09-25 02:40:06 +00001214 request_packet.PutHex32(GetCPUMask());
Greg Clayton234981a2011-07-20 03:41:06 +00001215
1216 DataExtractor reply_packet;
1217 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
1218 return true;
1219 return false;
1220}
1221
1222bool
1223CommunicationKDP::SendRequestBreakpoint (bool set, addr_t addr)
1224{
1225 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
1226 bool use_64 = (GetVersion() >= 11);
1227 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
Greg Clayton7b139222011-07-21 01:12:01 +00001228 const CommandType command = set ? (use_64 ? KDP_BREAKPOINT_SET64 : KDP_BREAKPOINT_SET ):
1229 (use_64 ? KDP_BREAKPOINT_REMOVE64 : KDP_BREAKPOINT_REMOVE);
Greg Clayton234981a2011-07-20 03:41:06 +00001230
1231 const uint32_t command_length = 8 + command_addr_byte_size;
1232 const uint32_t request_sequence_id = m_request_sequence_id;
1233 MakeRequestPacketHeader (command, request_packet, command_length);
1234 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
1235
1236 DataExtractor reply_packet;
1237 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
Greg Clayton1bc04ed2011-07-29 22:26:36 +00001238 {
1239 uint32_t offset = 8;
1240 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1241 if (kdp_error == 0)
1242 return true;
1243 }
Greg Clayton234981a2011-07-20 03:41:06 +00001244 return false;
1245}
1246
1247bool
1248CommunicationKDP::SendRequestSuspend ()
1249{
1250 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton7b139222011-07-21 01:12:01 +00001251 const CommandType command = KDP_SUSPEND;
Greg Clayton234981a2011-07-20 03:41:06 +00001252 const uint32_t command_length = 8;
1253 const uint32_t request_sequence_id = m_request_sequence_id;
1254 MakeRequestPacketHeader (command, request_packet, command_length);
1255 DataExtractor reply_packet;
1256 if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet))
1257 return true;
1258 return false;
1259}
1260