blob: efebc8606d6e574682c4f0ef9eda616da840cd32 [file] [log] [blame]
Greg Clayton59ec5122011-07-15 18:02:58 +00001//===-- CommunicationKDP.cpp ------------------------------------*- 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
11#include "CommunicationKDP.h"
12
13// C Includes
Jason Molenda4bd4e7e2012-09-29 04:02:01 +000014#include <errno.h>
Greg Claytonf9765ac2011-07-15 03:27:12 +000015#include <limits.h>
16#include <string.h>
17
18// C++ Includes
Greg Claytona63d08c2011-07-19 03:57:15 +000019#include "llvm/Support/MachO.h"
20
Greg Claytonf9765ac2011-07-15 03:27:12 +000021// Other libraries and framework includes
Greg Clayton4df8ddf2011-07-16 03:19:08 +000022#include "lldb/Core/DataBufferHeap.h"
Greg Clayton57508022011-07-15 16:31:38 +000023#include "lldb/Core/DataExtractor.h"
Greg Claytonf9765ac2011-07-15 03:27:12 +000024#include "lldb/Core/Log.h"
Greg Claytona63d08c2011-07-19 03:57:15 +000025#include "lldb/Core/State.h"
Jason Molenda4bd4e7e2012-09-29 04:02:01 +000026#include "lldb/Core/UUID.h"
Greg Claytonf9765ac2011-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 Claytonf9765ac2011-07-15 03:27:12 +000031
32// Project includes
33#include "ProcessKDPLog.h"
34
Greg Claytonf9765ac2011-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 Claytona63d08c2011-07-19 03:57:15 +000043 m_addr_byte_size (4),
Greg Clayton4df8ddf2011-07-16 03:19:08 +000044 m_byte_order (eByteOrderLittle),
Greg Claytonf9765ac2011-07-15 03:27:12 +000045 m_packet_timeout (1),
46 m_sequence_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton97d5cf02012-09-25 02:40:06 +000047 m_is_running (false),
Greg Clayton4df8ddf2011-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 Claytonf9765ac2011-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 Clayton57508022011-07-15 16:31:38 +000070bool
Greg Clayton4df8ddf2011-07-16 03:19:08 +000071CommunicationKDP::SendRequestPacket (const PacketStreamType &request_packet)
Greg Claytonf9765ac2011-07-15 03:27:12 +000072{
73 Mutex::Locker locker(m_sequence_mutex);
Greg Clayton57508022011-07-15 16:31:38 +000074 return SendRequestPacketNoLock (request_packet);
Greg Claytonf9765ac2011-07-15 03:27:12 +000075}
76
Greg Clayton4df8ddf2011-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 Clayton57508022011-07-15 16:31:38 +000086void
Greg Clayton1d19a2f2012-10-19 22:22:57 +000087CommunicationKDP::MakeRequestPacketHeader (CommandType request_type,
Greg Clayton4df8ddf2011-07-16 03:19:08 +000088 PacketStreamType &request_packet,
89 uint16_t request_length)
Greg Claytonf9765ac2011-07-15 03:27:12 +000090{
Greg Clayton57508022011-07-15 16:31:38 +000091 request_packet.Clear();
Greg Clayton4df8ddf2011-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 Claytonf9765ac2011-07-15 03:27:12 +000096}
97
Greg Clayton4df8ddf2011-07-16 03:19:08 +000098bool
99CommunicationKDP::SendRequestAndGetReply (const CommandType command,
Greg Clayton0ee809b2013-02-14 19:11:23 +0000100 const PacketStreamType &request_packet,
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000101 DataExtractor &reply_packet)
102{
Greg Clayton97d5cf02012-09-25 02:40:06 +0000103 if (IsRunning())
104 {
105 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS));
106 if (log)
107 {
108 PacketStreamType log_strm;
109 DumpPacket (log_strm, request_packet.GetData(), request_packet.GetSize());
110 log->Printf("error: kdp running, not sending packet: %.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData());
111 }
112 return false;
113 }
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000114
Greg Clayton97d5cf02012-09-25 02:40:06 +0000115 Mutex::Locker locker(m_sequence_mutex);
116#ifdef LLDB_CONFIGURATION_DEBUG
117 // NOTE: this only works for packets that are in native endian byte order
118 assert (request_packet.GetSize() == *((uint16_t *)(request_packet.GetData() + 2)));
119#endif
Greg Clayton0ee809b2013-02-14 19:11:23 +0000120 lldb::offset_t offset = 1;
121 const uint32_t num_retries = 3;
122 for (uint32_t i=0; i<num_retries; ++i)
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000123 {
Greg Clayton0ee809b2013-02-14 19:11:23 +0000124 if (SendRequestPacketNoLock(request_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000125 {
Greg Clayton0ee809b2013-02-14 19:11:23 +0000126 const uint8_t request_sequence_id = (uint8_t)request_packet.GetData()[1];
127 if (WaitForPacketWithTimeoutMicroSecondsNoLock (reply_packet, GetPacketTimeoutInMicroSeconds ()))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000128 {
Greg Clayton0ee809b2013-02-14 19:11:23 +0000129 offset = 0;
130 const uint8_t reply_command = reply_packet.GetU8 (&offset);
131 const uint8_t reply_sequence_id = reply_packet.GetU8 (&offset);
132 if ((reply_command & eCommandTypeMask) == command)
Greg Clayton97d5cf02012-09-25 02:40:06 +0000133 {
Greg Clayton0ee809b2013-02-14 19:11:23 +0000134 if (request_sequence_id == reply_sequence_id)
135 {
136 if (command == KDP_RESUMECPUS)
137 m_is_running.SetValue(true, eBroadcastAlways);
138 return true;
139 }
Greg Clayton97d5cf02012-09-25 02:40:06 +0000140 }
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000141 }
142 }
143 }
144 reply_packet.Clear();
145 return false;
146}
Greg Claytonf9765ac2011-07-15 03:27:12 +0000147
Greg Clayton57508022011-07-15 16:31:38 +0000148bool
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000149CommunicationKDP::SendRequestPacketNoLock (const PacketStreamType &request_packet)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000150{
151 if (IsConnected())
152 {
Greg Clayton57508022011-07-15 16:31:38 +0000153 const char *packet_data = request_packet.GetData();
154 const size_t packet_size = request_packet.GetSize();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000155
156 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS));
157 if (log)
Greg Clayton57508022011-07-15 16:31:38 +0000158 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000159 PacketStreamType log_strm;
160 DumpPacket (log_strm, packet_data, packet_size);
161 log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData());
Greg Clayton57508022011-07-15 16:31:38 +0000162 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000163 ConnectionStatus status = eConnectionStatusSuccess;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000164
Greg Clayton57508022011-07-15 16:31:38 +0000165 size_t bytes_written = Write (packet_data,
166 packet_size,
167 status,
168 NULL);
169
170 if (bytes_written == packet_size)
171 return true;
172
173 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000174 log->Printf ("error: failed to send packet entire packet %" PRIu64 " of %" PRIu64 " bytes sent", (uint64_t)bytes_written, (uint64_t)packet_size);
Greg Clayton57508022011-07-15 16:31:38 +0000175 }
176 return false;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000177}
178
179bool
Greg Clayton37a0a242012-04-11 00:24:49 +0000180CommunicationKDP::GetSequenceMutex (Mutex::Locker& locker)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000181{
Jim Ingham10ebffa2012-05-04 23:02:50 +0000182 return locker.TryLock (m_sequence_mutex);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000183}
184
185
186bool
187CommunicationKDP::WaitForNotRunningPrivate (const TimeValue *timeout_ptr)
188{
Greg Clayton97d5cf02012-09-25 02:40:06 +0000189 return m_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000190}
191
192size_t
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000193CommunicationKDP::WaitForPacketWithTimeoutMicroSeconds (DataExtractor &packet, uint32_t timeout_usec)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000194{
195 Mutex::Locker locker(m_sequence_mutex);
196 return WaitForPacketWithTimeoutMicroSecondsNoLock (packet, timeout_usec);
197}
198
199size_t
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000200CommunicationKDP::WaitForPacketWithTimeoutMicroSecondsNoLock (DataExtractor &packet, uint32_t timeout_usec)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000201{
202 uint8_t buffer[8192];
203 Error error;
204
205 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS | KDP_LOG_VERBOSE));
206
207 // Check for a packet from our cache first without trying any reading...
208 if (CheckForPacket (NULL, 0, packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000209 return packet.GetByteSize();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000210
211 bool timed_out = false;
212 while (IsConnected() && !timed_out)
213 {
Johnny Chenbecabe02011-07-21 19:00:59 +0000214 lldb::ConnectionStatus status = eConnectionStatusNoConnection;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000215 size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error);
216
217 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000218 log->Printf ("%s: Read (buffer, (sizeof(buffer), timeout_usec = 0x%x, status = %s, error = %s) => bytes_read = %" PRIu64,
Greg Claytonf9765ac2011-07-15 03:27:12 +0000219 __PRETTY_FUNCTION__,
220 timeout_usec,
221 Communication::ConnectionStatusAsCString (status),
222 error.AsCString(),
Greg Clayton43e0af02012-09-18 18:04:04 +0000223 (uint64_t)bytes_read);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000224
225 if (bytes_read > 0)
226 {
227 if (CheckForPacket (buffer, bytes_read, packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000228 return packet.GetByteSize();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000229 }
230 else
231 {
232 switch (status)
233 {
234 case eConnectionStatusTimedOut:
235 timed_out = true;
236 break;
237 case eConnectionStatusSuccess:
238 //printf ("status = success but error = %s\n", error.AsCString("<invalid>"));
239 break;
240
241 case eConnectionStatusEndOfFile:
242 case eConnectionStatusNoConnection:
243 case eConnectionStatusLostConnection:
244 case eConnectionStatusError:
245 Disconnect();
246 break;
247 }
248 }
249 }
250 packet.Clear ();
251 return 0;
252}
253
254bool
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000255CommunicationKDP::CheckForPacket (const uint8_t *src, size_t src_len, DataExtractor &packet)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000256{
257 // Put the packet data into the buffer in a thread safe fashion
258 Mutex::Locker locker(m_bytes_mutex);
259
260 LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS));
261
262 if (src && src_len > 0)
263 {
264 if (log && log->GetVerbose())
265 {
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000266 PacketStreamType log_strm;
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000267 DataExtractor::DumpHexBytes (&log_strm, src, src_len, UINT32_MAX, LLDB_INVALID_ADDRESS);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000268 log->Printf ("CommunicationKDP::%s adding %u bytes: %s",
Greg Claytonf9765ac2011-07-15 03:27:12 +0000269 __FUNCTION__,
270 (uint32_t)src_len,
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000271 log_strm.GetData());
Greg Claytonf9765ac2011-07-15 03:27:12 +0000272 }
273 m_bytes.append ((const char *)src, src_len);
274 }
275
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000276 // Make sure we at least have enough bytes for a packet header
277 const size_t bytes_available = m_bytes.size();
278 if (bytes_available >= 8)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000279 {
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000280 packet.SetData (&m_bytes[0], bytes_available, m_byte_order);
Greg Claytonc7bece562013-01-25 18:06:21 +0000281 lldb::offset_t offset = 0;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000282 uint8_t reply_command = packet.GetU8(&offset);
283 switch (reply_command)
284 {
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000285 case ePacketTypeRequest | KDP_EXCEPTION:
286 case ePacketTypeRequest | KDP_TERMINATION:
287 // We got an exception request, so be sure to send an ACK
288 {
289 PacketStreamType request_ack_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
290 // Set the reply but and make the ACK packet
291 request_ack_packet.PutHex8 (reply_command | ePacketTypeReply);
292 request_ack_packet.PutHex8 (packet.GetU8(&offset));
293 request_ack_packet.PutHex16 (packet.GetU16(&offset));
294 request_ack_packet.PutHex32 (packet.GetU32(&offset));
Greg Clayton97d5cf02012-09-25 02:40:06 +0000295 m_is_running.SetValue(false, eBroadcastAlways);
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000296 // Ack to the exception or termination
297 SendRequestPacketNoLock (request_ack_packet);
298 }
299 // Fall through to case below to get packet contents
Greg Clayton5b882162011-07-21 01:12:01 +0000300 case ePacketTypeReply | KDP_CONNECT:
301 case ePacketTypeReply | KDP_DISCONNECT:
302 case ePacketTypeReply | KDP_HOSTINFO:
303 case ePacketTypeReply | KDP_VERSION:
304 case ePacketTypeReply | KDP_MAXBYTES:
305 case ePacketTypeReply | KDP_READMEM:
306 case ePacketTypeReply | KDP_WRITEMEM:
307 case ePacketTypeReply | KDP_READREGS:
308 case ePacketTypeReply | KDP_WRITEREGS:
309 case ePacketTypeReply | KDP_LOAD:
310 case ePacketTypeReply | KDP_IMAGEPATH:
311 case ePacketTypeReply | KDP_SUSPEND:
312 case ePacketTypeReply | KDP_RESUMECPUS:
Greg Clayton5b882162011-07-21 01:12:01 +0000313 case ePacketTypeReply | KDP_BREAKPOINT_SET:
314 case ePacketTypeReply | KDP_BREAKPOINT_REMOVE:
315 case ePacketTypeReply | KDP_REGIONS:
316 case ePacketTypeReply | KDP_REATTACH:
317 case ePacketTypeReply | KDP_HOSTREBOOT:
318 case ePacketTypeReply | KDP_READMEM64:
319 case ePacketTypeReply | KDP_WRITEMEM64:
320 case ePacketTypeReply | KDP_BREAKPOINT_SET64:
321 case ePacketTypeReply | KDP_BREAKPOINT_REMOVE64:
322 case ePacketTypeReply | KDP_KERNELVERSION:
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000323 {
324 offset = 2;
325 const uint16_t length = packet.GetU16 (&offset);
326 if (length <= bytes_available)
327 {
328 // We have an entire packet ready, we need to copy the data
329 // bytes into a buffer that will be owned by the packet and
330 // erase the bytes from our communcation buffer "m_bytes"
331 packet.SetData (DataBufferSP (new DataBufferHeap (&m_bytes[0], length)));
332 m_bytes.erase (0, length);
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000333
334 if (log)
335 {
336 PacketStreamType log_strm;
Greg Claytona63d08c2011-07-19 03:57:15 +0000337 DumpPacket (log_strm, packet);
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000338
Greg Claytona63d08c2011-07-19 03:57:15 +0000339 log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData());
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000340 }
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000341 return true;
342 }
343 }
344 break;
345
346 default:
347 // Unrecognized reply command byte, erase this byte and try to get back on track
348 if (log)
349 log->Printf ("CommunicationKDP::%s: tossing junk byte: 0x%2.2x",
350 __FUNCTION__,
351 (uint8_t)m_bytes[0]);
352 m_bytes.erase(0, 1);
353 break;
354 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000355 }
356 packet.Clear();
357 return false;
358}
359
Greg Clayton57508022011-07-15 16:31:38 +0000360
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000361bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000362CommunicationKDP::SendRequestConnect (uint16_t reply_port,
363 uint16_t exc_port,
364 const char *greeting)
Greg Clayton57508022011-07-15 16:31:38 +0000365{
Greg Claytona63d08c2011-07-19 03:57:15 +0000366 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000367 if (greeting == NULL)
368 greeting = "";
369
Greg Clayton5b882162011-07-21 01:12:01 +0000370 const CommandType command = KDP_CONNECT;
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000371 // Length is 82 uint16_t and the length of the greeting C string with the terminating NULL
372 const uint32_t command_length = 8 + 2 + 2 + ::strlen(greeting) + 1;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000373 MakeRequestPacketHeader (command, request_packet, command_length);
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000374 // Always send connect ports as little endian
375 request_packet.SetByteOrder (eByteOrderLittle);
376 request_packet.PutHex16 (reply_port);
377 request_packet.PutHex16 (exc_port);
378 request_packet.SetByteOrder (m_byte_order);
379 request_packet.PutCString (greeting);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000380 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000381 return SendRequestAndGetReply (command, request_packet, reply_packet);
Greg Clayton57508022011-07-15 16:31:38 +0000382}
383
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000384void
385CommunicationKDP::ClearKDPSettings ()
386{
387 m_request_sequence_id = 0;
388 m_kdp_version_version = 0;
389 m_kdp_version_feature = 0;
390 m_kdp_hostinfo_cpu_mask = 0;
391 m_kdp_hostinfo_cpu_type = 0;
392 m_kdp_hostinfo_cpu_subtype = 0;
393}
394
395bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000396CommunicationKDP::SendRequestReattach (uint16_t reply_port)
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000397{
Greg Claytona63d08c2011-07-19 03:57:15 +0000398 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000399 const CommandType command = KDP_REATTACH;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000400 // Length is 8 bytes for the header plus 2 bytes for the reply UDP port
401 const uint32_t command_length = 8 + 2;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000402 MakeRequestPacketHeader (command, request_packet, command_length);
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000403 // Always send connect ports as little endian
404 request_packet.SetByteOrder (eByteOrderLittle);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000405 request_packet.PutHex16(reply_port);
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000406 request_packet.SetByteOrder (m_byte_order);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000407 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000408 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000409 {
410 // Reset the sequence ID to zero for reattach
411 ClearKDPSettings ();
Greg Claytonc7bece562013-01-25 18:06:21 +0000412 lldb::offset_t offset = 4;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000413 m_session_key = reply_packet.GetU32 (&offset);
414 return true;
415 }
416 return false;
417}
418
419uint32_t
420CommunicationKDP::GetVersion ()
421{
422 if (!VersionIsValid())
423 SendRequestVersion();
424 return m_kdp_version_version;
425}
426
427uint32_t
428CommunicationKDP::GetFeatureFlags ()
429{
430 if (!VersionIsValid())
431 SendRequestVersion();
432 return m_kdp_version_feature;
433}
434
435bool
436CommunicationKDP::SendRequestVersion ()
437{
Greg Claytona63d08c2011-07-19 03:57:15 +0000438 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000439 const CommandType command = KDP_VERSION;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000440 const uint32_t command_length = 8;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000441 MakeRequestPacketHeader (command, request_packet, command_length);
442 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000443 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000444 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000445 lldb::offset_t offset = 8;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000446 m_kdp_version_version = reply_packet.GetU32 (&offset);
447 m_kdp_version_feature = reply_packet.GetU32 (&offset);
448 return true;
449 }
450 return false;
451}
452
Greg Clayton5b882162011-07-21 01:12:01 +0000453#if 0 // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
454const char *
455CommunicationKDP::GetImagePath ()
456{
457 if (m_image_path.empty())
458 SendRequestImagePath();
459 return m_image_path.c_str();
460}
461
462bool
463CommunicationKDP::SendRequestImagePath ()
464{
465 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
466 const CommandType command = KDP_IMAGEPATH;
467 const uint32_t command_length = 8;
Greg Clayton5b882162011-07-21 01:12:01 +0000468 MakeRequestPacketHeader (command, request_packet, command_length);
469 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000470 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton5b882162011-07-21 01:12:01 +0000471 {
472 const char *path = reply_packet.PeekCStr(8);
473 if (path && path[0])
474 m_kernel_version.assign (path);
475 return true;
476 }
477 return false;
478}
479#endif
480
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000481uint32_t
482CommunicationKDP::GetCPUMask ()
483{
484 if (!HostInfoIsValid())
485 SendRequestHostInfo();
486 return m_kdp_hostinfo_cpu_mask;
487}
488
489uint32_t
490CommunicationKDP::GetCPUType ()
491{
492 if (!HostInfoIsValid())
493 SendRequestHostInfo();
494 return m_kdp_hostinfo_cpu_type;
495}
496
497uint32_t
498CommunicationKDP::GetCPUSubtype ()
499{
500 if (!HostInfoIsValid())
501 SendRequestHostInfo();
502 return m_kdp_hostinfo_cpu_subtype;
503}
504
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000505lldb_private::UUID
506CommunicationKDP::GetUUID ()
507{
508 UUID uuid;
509 if (GetKernelVersion() == NULL)
510 return uuid;
511
512 if (m_kernel_version.find("UUID=") == std::string::npos)
513 return uuid;
514
515 size_t p = m_kernel_version.find("UUID=") + strlen ("UUID=");
516 std::string uuid_str = m_kernel_version.substr(p, 36);
517 if (uuid_str.size() < 32)
518 return uuid;
519
520 if (uuid.SetFromCString (uuid_str.c_str()) == 0)
521 {
522 UUID invalid_uuid;
523 return invalid_uuid;
524 }
525
526 return uuid;
527}
528
Jason Molenda840f12c2012-10-25 00:25:13 +0000529bool
530CommunicationKDP::RemoteIsEFI ()
531{
532 if (GetKernelVersion() == NULL)
533 return false;
534 if (strncmp (m_kernel_version.c_str(), "EFI", 3) == 0)
535 return true;
536 else
537 return false;
538}
539
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000540lldb::addr_t
541CommunicationKDP::GetLoadAddress ()
542{
543 if (GetKernelVersion() == NULL)
544 return LLDB_INVALID_ADDRESS;
545
546 if (m_kernel_version.find("stext=") == std::string::npos)
547 return LLDB_INVALID_ADDRESS;
548 size_t p = m_kernel_version.find("stext=") + strlen ("stext=");
549 if (m_kernel_version[p] != '0' || m_kernel_version[p + 1] != 'x')
550 return LLDB_INVALID_ADDRESS;
551
552 addr_t kernel_load_address;
553 errno = 0;
554 kernel_load_address = ::strtoul (m_kernel_version.c_str() + p, NULL, 16);
555 if (errno != 0 || kernel_load_address == 0)
556 return LLDB_INVALID_ADDRESS;
557
558 return kernel_load_address;
559}
560
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000561bool
562CommunicationKDP::SendRequestHostInfo ()
563{
Greg Claytona63d08c2011-07-19 03:57:15 +0000564 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000565 const CommandType command = KDP_HOSTINFO;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000566 const uint32_t command_length = 8;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000567 MakeRequestPacketHeader (command, request_packet, command_length);
568 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000569 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000570 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000571 lldb::offset_t offset = 8;
Greg Claytona63d08c2011-07-19 03:57:15 +0000572 m_kdp_hostinfo_cpu_mask = reply_packet.GetU32 (&offset);
573 m_kdp_hostinfo_cpu_type = reply_packet.GetU32 (&offset);
574 m_kdp_hostinfo_cpu_subtype = reply_packet.GetU32 (&offset);
575
576 ArchSpec kernel_arch;
577 kernel_arch.SetArchitecture (eArchTypeMachO,
578 m_kdp_hostinfo_cpu_type,
579 m_kdp_hostinfo_cpu_subtype);
580
581 m_addr_byte_size = kernel_arch.GetAddressByteSize();
582 m_byte_order = kernel_arch.GetByteOrder();
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000583 return true;
584 }
585 return false;
586}
587
Greg Clayton07e66e32011-07-20 03:41:06 +0000588const char *
589CommunicationKDP::GetKernelVersion ()
590{
591 if (m_kernel_version.empty())
592 SendRequestKernelVersion ();
593 return m_kernel_version.c_str();
594}
595
596bool
597CommunicationKDP::SendRequestKernelVersion ()
598{
599 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000600 const CommandType command = KDP_KERNELVERSION;
Greg Clayton07e66e32011-07-20 03:41:06 +0000601 const uint32_t command_length = 8;
Greg Clayton07e66e32011-07-20 03:41:06 +0000602 MakeRequestPacketHeader (command, request_packet, command_length);
603 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000604 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton07e66e32011-07-20 03:41:06 +0000605 {
606 const char *kernel_version_cstr = reply_packet.PeekCStr(8);
607 if (kernel_version_cstr && kernel_version_cstr[0])
608 m_kernel_version.assign (kernel_version_cstr);
609 return true;
610 }
611 return false;
612}
613
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000614bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000615CommunicationKDP::SendRequestDisconnect ()
Greg Clayton57508022011-07-15 16:31:38 +0000616{
Greg Claytona63d08c2011-07-19 03:57:15 +0000617 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000618 const CommandType command = KDP_DISCONNECT;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000619 const uint32_t command_length = 8;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000620 MakeRequestPacketHeader (command, request_packet, command_length);
621 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000622 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000623 {
624 // Are we supposed to get a reply for disconnect?
625 }
626 ClearKDPSettings ();
627 return true;
Greg Clayton57508022011-07-15 16:31:38 +0000628}
629
Greg Claytona63d08c2011-07-19 03:57:15 +0000630uint32_t
631CommunicationKDP::SendRequestReadMemory (lldb::addr_t addr,
632 void *dst,
633 uint32_t dst_len,
634 Error &error)
635{
636 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
637 bool use_64 = (GetVersion() >= 11);
638 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
Greg Clayton5b882162011-07-21 01:12:01 +0000639 const CommandType command = use_64 ? KDP_READMEM64 : KDP_READMEM;
Greg Claytona63d08c2011-07-19 03:57:15 +0000640 // Size is header + address size + uint32_t length
641 const uint32_t command_length = 8 + command_addr_byte_size + 4;
Greg Claytona63d08c2011-07-19 03:57:15 +0000642 MakeRequestPacketHeader (command, request_packet, command_length);
643 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
644 request_packet.PutHex32 (dst_len);
645 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000646 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Claytona63d08c2011-07-19 03:57:15 +0000647 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000648 lldb::offset_t offset = 8;
Greg Claytona63d08c2011-07-19 03:57:15 +0000649 uint32_t kdp_error = reply_packet.GetU32 (&offset);
650 uint32_t src_len = reply_packet.GetByteSize() - 12;
651
652 if (src_len > 0)
653 {
654 const void *src = reply_packet.GetData(&offset, src_len);
655 if (src)
656 {
657 ::memcpy (dst, src, src_len);
658 error.Clear();
659 return src_len;
660 }
661 }
662 if (kdp_error)
663 error.SetErrorStringWithFormat ("kdp read memory failed (error %u)", kdp_error);
664 else
665 error.SetErrorString ("kdp read memory failed");
666 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000667 else
668 {
669 error.SetErrorString ("failed to send packet");
670 }
Greg Claytona63d08c2011-07-19 03:57:15 +0000671 return 0;
672}
673
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000674
675uint32_t
676CommunicationKDP::SendRequestWriteMemory (lldb::addr_t addr,
677 const void *src,
678 uint32_t src_len,
679 Error &error)
680{
681 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
682 bool use_64 = (GetVersion() >= 11);
683 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
Greg Clayton5b882162011-07-21 01:12:01 +0000684 const CommandType command = use_64 ? KDP_WRITEMEM64 : KDP_WRITEMEM;
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000685 // Size is header + address size + uint32_t length
Greg Claytonb556c9b2012-10-12 23:24:05 +0000686 const uint32_t command_length = 8 + command_addr_byte_size + 4 + src_len;
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000687 MakeRequestPacketHeader (command, request_packet, command_length);
688 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
689 request_packet.PutHex32 (src_len);
Jason Molenda57656e72012-10-19 02:16:22 +0000690 request_packet.PutRawBytes(src, src_len);
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000691
692 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000693 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000694 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000695 lldb::offset_t offset = 8;
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000696 uint32_t kdp_error = reply_packet.GetU32 (&offset);
697 if (kdp_error)
698 error.SetErrorStringWithFormat ("kdp write memory failed (error %u)", kdp_error);
699 else
700 {
701 error.Clear();
702 return src_len;
703 }
704 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000705 else
706 {
707 error.SetErrorString ("failed to send packet");
708 }
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000709 return 0;
710}
711
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000712bool
713CommunicationKDP::SendRawRequest (uint8_t command_byte,
714 const void *src, // Raw packet payload bytes
715 uint32_t src_len, // Raw packet payload length
716 DataExtractor &reply_packet,
717 Error &error)
718{
719 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
720 // Size is header + address size + uint32_t length
721 const uint32_t command_length = 8 + src_len;
722 const CommandType command = (CommandType)command_byte;
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000723 MakeRequestPacketHeader (command, request_packet, command_length);
724 request_packet.PutRawBytes(src, src_len);
725
Greg Clayton0ee809b2013-02-14 19:11:23 +0000726 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000727 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000728 lldb::offset_t offset = 8;
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000729 uint32_t kdp_error = reply_packet.GetU32 (&offset);
730 if (kdp_error)
731 error.SetErrorStringWithFormat ("request packet 0x%8.8x failed (error %u)", command_byte, kdp_error);
732 else
733 {
734 error.Clear();
735 return true;
736 }
737 }
738 else
739 {
740 error.SetErrorString ("failed to send packet");
741 }
742 return false;
743}
744
745
Greg Claytona63d08c2011-07-19 03:57:15 +0000746const char *
747CommunicationKDP::GetCommandAsCString (uint8_t command)
748{
749 switch (command)
750 {
Greg Clayton5b882162011-07-21 01:12:01 +0000751 case KDP_CONNECT: return "KDP_CONNECT";
752 case KDP_DISCONNECT: return "KDP_DISCONNECT";
753 case KDP_HOSTINFO: return "KDP_HOSTINFO";
754 case KDP_VERSION: return "KDP_VERSION";
755 case KDP_MAXBYTES: return "KDP_MAXBYTES";
756 case KDP_READMEM: return "KDP_READMEM";
757 case KDP_WRITEMEM: return "KDP_WRITEMEM";
758 case KDP_READREGS: return "KDP_READREGS";
759 case KDP_WRITEREGS: return "KDP_WRITEREGS";
760 case KDP_LOAD: return "KDP_LOAD";
761 case KDP_IMAGEPATH: return "KDP_IMAGEPATH";
762 case KDP_SUSPEND: return "KDP_SUSPEND";
763 case KDP_RESUMECPUS: return "KDP_RESUMECPUS";
764 case KDP_EXCEPTION: return "KDP_EXCEPTION";
765 case KDP_TERMINATION: return "KDP_TERMINATION";
766 case KDP_BREAKPOINT_SET: return "KDP_BREAKPOINT_SET";
767 case KDP_BREAKPOINT_REMOVE: return "KDP_BREAKPOINT_REMOVE";
768 case KDP_REGIONS: return "KDP_REGIONS";
769 case KDP_REATTACH: return "KDP_REATTACH";
770 case KDP_HOSTREBOOT: return "KDP_HOSTREBOOT";
771 case KDP_READMEM64: return "KDP_READMEM64";
772 case KDP_WRITEMEM64: return "KDP_WRITEMEM64";
773 case KDP_BREAKPOINT_SET64: return "KDP_BREAKPOINT64_SET";
774 case KDP_BREAKPOINT_REMOVE64: return "KDP_BREAKPOINT64_REMOVE";
775 case KDP_KERNELVERSION: return "KDP_KERNELVERSION";
Greg Claytona63d08c2011-07-19 03:57:15 +0000776 }
777 return NULL;
778}
779
780void
781CommunicationKDP::DumpPacket (Stream &s, const void *data, uint32_t data_len)
782{
783 DataExtractor extractor (data, data_len, m_byte_order, m_addr_byte_size);
784 DumpPacket (s, extractor);
785}
786
787void
788CommunicationKDP::DumpPacket (Stream &s, const DataExtractor& packet)
789{
790 const char *error_desc = NULL;
791 if (packet.GetByteSize() < 8)
792 {
793 error_desc = "error: invalid packet (too short): ";
794 }
795 else
796 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000797 lldb::offset_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000798 const uint8_t first_packet_byte = packet.GetU8 (&offset);
799 const uint8_t sequence_id = packet.GetU8 (&offset);
800 const uint16_t length = packet.GetU16 (&offset);
801 const uint32_t key = packet.GetU32 (&offset);
802 const CommandType command = ExtractCommand (first_packet_byte);
803 const char *command_name = GetCommandAsCString (command);
804 if (command_name)
805 {
806 const bool is_reply = ExtractIsReply(first_packet_byte);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000807 s.Printf ("(running=%i) %s %24s: 0x%2.2x 0x%2.2x 0x%4.4x 0x%8.8x ",
808 IsRunning(),
809 is_reply ? "<--" : "-->",
810 command_name,
811 first_packet_byte,
Greg Claytona63d08c2011-07-19 03:57:15 +0000812 sequence_id,
813 length,
Greg Clayton97d5cf02012-09-25 02:40:06 +0000814 key);
Greg Claytona63d08c2011-07-19 03:57:15 +0000815
816 if (is_reply)
817 {
818 // Dump request reply packets
819 switch (command)
820 {
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000821 // Commands that return a single 32 bit error
Greg Clayton5b882162011-07-21 01:12:01 +0000822 case KDP_CONNECT:
823 case KDP_WRITEMEM:
824 case KDP_WRITEMEM64:
825 case KDP_BREAKPOINT_SET:
826 case KDP_BREAKPOINT_REMOVE:
827 case KDP_BREAKPOINT_SET64:
828 case KDP_BREAKPOINT_REMOVE64:
829 case KDP_WRITEREGS:
830 case KDP_LOAD:
Greg Claytona63d08c2011-07-19 03:57:15 +0000831 {
832 const uint32_t error = packet.GetU32 (&offset);
833 s.Printf(" (error=0x%8.8x)", error);
834 }
835 break;
836
Greg Clayton5b882162011-07-21 01:12:01 +0000837 case KDP_DISCONNECT:
838 case KDP_REATTACH:
839 case KDP_HOSTREBOOT:
840 case KDP_SUSPEND:
841 case KDP_RESUMECPUS:
842 case KDP_EXCEPTION:
843 case KDP_TERMINATION:
Greg Claytona63d08c2011-07-19 03:57:15 +0000844 // No return value for the reply, just the header to ack
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000845 s.PutCString(" ()");
Greg Claytona63d08c2011-07-19 03:57:15 +0000846 break;
847
Greg Clayton5b882162011-07-21 01:12:01 +0000848 case KDP_HOSTINFO:
Greg Claytona63d08c2011-07-19 03:57:15 +0000849 {
850 const uint32_t cpu_mask = packet.GetU32 (&offset);
851 const uint32_t cpu_type = packet.GetU32 (&offset);
852 const uint32_t cpu_subtype = packet.GetU32 (&offset);
853 s.Printf(" (cpu_mask=0x%8.8x, cpu_type=0x%8.8x, cpu_subtype=0x%8.8x)", cpu_mask, cpu_type, cpu_subtype);
854 }
855 break;
856
Greg Clayton5b882162011-07-21 01:12:01 +0000857 case KDP_VERSION:
Greg Claytona63d08c2011-07-19 03:57:15 +0000858 {
859 const uint32_t version = packet.GetU32 (&offset);
860 const uint32_t feature = packet.GetU32 (&offset);
861 s.Printf(" (version=0x%8.8x, feature=0x%8.8x)", version, feature);
862 }
863 break;
864
Greg Clayton5b882162011-07-21 01:12:01 +0000865 case KDP_REGIONS:
Greg Claytona63d08c2011-07-19 03:57:15 +0000866 {
867 const uint32_t region_count = packet.GetU32 (&offset);
868 s.Printf(" (count = %u", region_count);
869 for (uint32_t i=0; i<region_count; ++i)
870 {
871 const addr_t region_addr = packet.GetPointer (&offset);
872 const uint32_t region_size = packet.GetU32 (&offset);
873 const uint32_t region_prot = packet.GetU32 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +0000874 s.Printf("\n\tregion[%" PRIu64 "] = { range = [0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), size = 0x%8.8x, prot = %s }", region_addr, region_addr, region_addr + region_size, region_size, GetPermissionsAsCString (region_prot));
Greg Claytona63d08c2011-07-19 03:57:15 +0000875 }
876 }
877 break;
878
Greg Clayton5b882162011-07-21 01:12:01 +0000879 case KDP_READMEM:
880 case KDP_READMEM64:
Greg Claytona63d08c2011-07-19 03:57:15 +0000881 {
882 const uint32_t error = packet.GetU32 (&offset);
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000883 const uint32_t count = packet.GetByteSize() - offset;
Greg Clayton5b442372011-07-29 22:26:36 +0000884 s.Printf(" (error = 0x%8.8x:\n", error);
Greg Claytona63d08c2011-07-19 03:57:15 +0000885 if (count > 0)
Greg Clayton07e66e32011-07-20 03:41:06 +0000886 packet.Dump (&s, // Stream to dump to
887 offset, // Offset within "packet"
888 eFormatBytesWithASCII, // Format to use
889 1, // Size of each item in bytes
890 count, // Number of items
891 16, // Number per line
892 m_last_read_memory_addr, // Don't show addresses before each line
893 0, 0); // No bitfields
Greg Claytona63d08c2011-07-19 03:57:15 +0000894 }
895 break;
896
Greg Clayton5b882162011-07-21 01:12:01 +0000897 case KDP_READREGS:
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000898 {
899 const uint32_t error = packet.GetU32 (&offset);
900 const uint32_t count = packet.GetByteSize() - offset;
Greg Clayton5b442372011-07-29 22:26:36 +0000901 s.Printf(" (error = 0x%8.8x regs:\n", error);
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000902 if (count > 0)
903 packet.Dump (&s, // Stream to dump to
904 offset, // Offset within "packet"
905 eFormatHex, // Format to use
906 m_addr_byte_size, // Size of each item in bytes
907 count / m_addr_byte_size, // Number of items
908 16 / m_addr_byte_size, // Number per line
909 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
910 0, 0); // No bitfields
911 }
912 break;
913
Greg Clayton5b882162011-07-21 01:12:01 +0000914 case KDP_KERNELVERSION:
Greg Clayton07e66e32011-07-20 03:41:06 +0000915 {
916 const char *kernel_version = packet.PeekCStr(8);
917 s.Printf(" (version = \"%s\")", kernel_version);
918 }
919 break;
920
Greg Clayton5b882162011-07-21 01:12:01 +0000921 case KDP_MAXBYTES:
Greg Clayton07e66e32011-07-20 03:41:06 +0000922 {
923 const uint32_t max_bytes = packet.GetU32 (&offset);
924 s.Printf(" (max_bytes = 0x%8.8x (%u))", max_bytes, max_bytes);
925 }
926 break;
Greg Clayton5b882162011-07-21 01:12:01 +0000927 case KDP_IMAGEPATH:
Greg Clayton07e66e32011-07-20 03:41:06 +0000928 {
929 const char *path = packet.GetCStr(&offset);
930 s.Printf(" (path = \"%s\")", path);
931 }
932 break;
933 default:
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000934 s.Printf(" (add support for dumping this packet reply!!!");
Greg Claytona63d08c2011-07-19 03:57:15 +0000935 break;
936
937 }
938 }
939 else
940 {
941 // Dump request packets
942 switch (command)
943 {
Greg Clayton5b882162011-07-21 01:12:01 +0000944 case KDP_CONNECT:
Greg Claytona63d08c2011-07-19 03:57:15 +0000945 {
946 const uint16_t reply_port = packet.GetU16 (&offset);
947 const uint16_t exc_port = packet.GetU16 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +0000948 s.Printf(" (reply_port = %u, exc_port = %u, greeting = \"%s\")", reply_port, exc_port, packet.GetCStr(&offset));
Greg Claytona63d08c2011-07-19 03:57:15 +0000949 }
950 break;
951
Greg Clayton5b882162011-07-21 01:12:01 +0000952 case KDP_DISCONNECT:
953 case KDP_HOSTREBOOT:
954 case KDP_HOSTINFO:
955 case KDP_VERSION:
956 case KDP_REGIONS:
957 case KDP_KERNELVERSION:
958 case KDP_MAXBYTES:
959 case KDP_IMAGEPATH:
960 case KDP_SUSPEND:
Greg Claytona63d08c2011-07-19 03:57:15 +0000961 // No args, just the header in the request...
Greg Clayton07e66e32011-07-20 03:41:06 +0000962 s.PutCString(" ()");
963 break;
964
Greg Clayton5b882162011-07-21 01:12:01 +0000965 case KDP_RESUMECPUS:
Greg Clayton07e66e32011-07-20 03:41:06 +0000966 {
967 const uint32_t cpu_mask = packet.GetU32 (&offset);
968 s.Printf(" (cpu_mask = 0x%8.8x)", cpu_mask);
969 }
Greg Claytona63d08c2011-07-19 03:57:15 +0000970 break;
971
Greg Clayton5b882162011-07-21 01:12:01 +0000972 case KDP_READMEM:
Greg Claytona63d08c2011-07-19 03:57:15 +0000973 {
974 const uint32_t addr = packet.GetU32 (&offset);
975 const uint32_t size = packet.GetU32 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +0000976 s.Printf(" (addr = 0x%8.8x, size = %u)", addr, size);
Greg Clayton07e66e32011-07-20 03:41:06 +0000977 m_last_read_memory_addr = addr;
Greg Claytona63d08c2011-07-19 03:57:15 +0000978 }
979 break;
980
Greg Clayton5b882162011-07-21 01:12:01 +0000981 case KDP_WRITEMEM:
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000982 {
983 const uint32_t addr = packet.GetU32 (&offset);
984 const uint32_t size = packet.GetU32 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +0000985 s.Printf(" (addr = 0x%8.8x, size = %u, bytes = \n", addr, size);
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000986 if (size > 0)
987 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
988 }
989 break;
990
Greg Clayton5b882162011-07-21 01:12:01 +0000991 case KDP_READMEM64:
Greg Claytona63d08c2011-07-19 03:57:15 +0000992 {
993 const uint64_t addr = packet.GetU64 (&offset);
994 const uint32_t size = packet.GetU32 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +0000995 s.Printf(" (addr = 0x%16.16" PRIx64 ", size = %u)", addr, size);
Greg Clayton07e66e32011-07-20 03:41:06 +0000996 m_last_read_memory_addr = addr;
Greg Claytona63d08c2011-07-19 03:57:15 +0000997 }
998 break;
999
Greg Clayton5b882162011-07-21 01:12:01 +00001000 case KDP_WRITEMEM64:
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001001 {
1002 const uint64_t addr = packet.GetU64 (&offset);
1003 const uint32_t size = packet.GetU32 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +00001004 s.Printf(" (addr = 0x%16.16" PRIx64 ", size = %u, bytes = \n", addr, size);
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001005 if (size > 0)
1006 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
1007 }
1008 break;
1009
Greg Clayton5b882162011-07-21 01:12:01 +00001010 case KDP_READREGS:
Greg Claytona63d08c2011-07-19 03:57:15 +00001011 {
1012 const uint32_t cpu = packet.GetU32 (&offset);
1013 const uint32_t flavor = packet.GetU32 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +00001014 s.Printf(" (cpu = %u, flavor = %u)", cpu, flavor);
Greg Claytona63d08c2011-07-19 03:57:15 +00001015 }
1016 break;
1017
Greg Clayton5b882162011-07-21 01:12:01 +00001018 case KDP_WRITEREGS:
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001019 {
1020 const uint32_t cpu = packet.GetU32 (&offset);
1021 const uint32_t flavor = packet.GetU32 (&offset);
1022 const uint32_t nbytes = packet.GetByteSize() - offset;
Greg Clayton5b442372011-07-29 22:26:36 +00001023 s.Printf(" (cpu = %u, flavor = %u, regs = \n", cpu, flavor);
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001024 if (nbytes > 0)
1025 packet.Dump (&s, // Stream to dump to
1026 offset, // Offset within "packet"
1027 eFormatHex, // Format to use
1028 m_addr_byte_size, // Size of each item in bytes
1029 nbytes / m_addr_byte_size, // Number of items
1030 16 / m_addr_byte_size, // Number per line
1031 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1032 0, 0); // No bitfields
1033 }
1034 break;
1035
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001036
Greg Clayton5b882162011-07-21 01:12:01 +00001037 case KDP_BREAKPOINT_SET:
1038 case KDP_BREAKPOINT_REMOVE:
Greg Clayton07e66e32011-07-20 03:41:06 +00001039 {
1040 const uint32_t addr = packet.GetU32 (&offset);
1041 s.Printf(" (addr = 0x%8.8x)", addr);
1042 }
1043 break;
1044
Greg Clayton5b882162011-07-21 01:12:01 +00001045 case KDP_BREAKPOINT_SET64:
1046 case KDP_BREAKPOINT_REMOVE64:
Greg Clayton07e66e32011-07-20 03:41:06 +00001047 {
1048 const uint64_t addr = packet.GetU64 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +00001049 s.Printf(" (addr = 0x%16.16" PRIx64 ")", addr);
Greg Clayton07e66e32011-07-20 03:41:06 +00001050 }
1051 break;
1052
1053
Greg Clayton5b882162011-07-21 01:12:01 +00001054 case KDP_LOAD:
Greg Clayton07e66e32011-07-20 03:41:06 +00001055 {
1056 const char *path = packet.GetCStr(&offset);
1057 s.Printf(" (path = \"%s\")", path);
1058 }
1059 break;
1060
Greg Clayton5b882162011-07-21 01:12:01 +00001061 case KDP_EXCEPTION:
Greg Clayton07e66e32011-07-20 03:41:06 +00001062 {
1063 const uint32_t count = packet.GetU32 (&offset);
1064
Greg Clayton07e66e32011-07-20 03:41:06 +00001065 for (uint32_t i=0; i<count; ++i)
1066 {
1067 const uint32_t cpu = packet.GetU32 (&offset);
1068 const uint32_t exc = packet.GetU32 (&offset);
1069 const uint32_t code = packet.GetU32 (&offset);
1070 const uint32_t subcode = packet.GetU32 (&offset);
1071 const char *exc_cstr = NULL;
1072 switch (exc)
1073 {
1074 case 1: exc_cstr = "EXC_BAD_ACCESS"; break;
1075 case 2: exc_cstr = "EXC_BAD_INSTRUCTION"; break;
1076 case 3: exc_cstr = "EXC_ARITHMETIC"; break;
1077 case 4: exc_cstr = "EXC_EMULATION"; break;
1078 case 5: exc_cstr = "EXC_SOFTWARE"; break;
1079 case 6: exc_cstr = "EXC_BREAKPOINT"; break;
1080 case 7: exc_cstr = "EXC_SYSCALL"; break;
1081 case 8: exc_cstr = "EXC_MACH_SYSCALL"; break;
1082 case 9: exc_cstr = "EXC_RPC_ALERT"; break;
1083 case 10: exc_cstr = "EXC_CRASH"; break;
1084 default:
1085 break;
1086 }
1087
Greg Clayton97d5cf02012-09-25 02:40:06 +00001088 s.Printf ("{ cpu = 0x%8.8x, exc = %s (%u), code = %u (0x%8.8x), subcode = %u (0x%8.8x)} ",
Greg Clayton07e66e32011-07-20 03:41:06 +00001089 cpu, exc_cstr, exc, code, code, subcode, subcode);
1090 }
1091 }
1092 break;
1093
Greg Clayton5b882162011-07-21 01:12:01 +00001094 case KDP_TERMINATION:
Greg Clayton07e66e32011-07-20 03:41:06 +00001095 {
1096 const uint32_t term_code = packet.GetU32 (&offset);
1097 const uint32_t exit_code = packet.GetU32 (&offset);
1098 s.Printf(" (term_code = 0x%8.8x (%u), exit_code = 0x%8.8x (%u))", term_code, term_code, exit_code, exit_code);
1099 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001100 break;
1101
Greg Clayton5b882162011-07-21 01:12:01 +00001102 case KDP_REATTACH:
Greg Claytona63d08c2011-07-19 03:57:15 +00001103 {
1104 const uint16_t reply_port = packet.GetU16 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +00001105 s.Printf(" (reply_port = %u)", reply_port);
Greg Claytona63d08c2011-07-19 03:57:15 +00001106 }
1107 break;
Greg Claytona63d08c2011-07-19 03:57:15 +00001108 }
1109 }
1110 }
1111 else
1112 {
1113 error_desc = "error: invalid packet command: ";
1114 }
1115 }
1116
1117 if (error_desc)
1118 {
1119 s.PutCString (error_desc);
1120
1121 packet.Dump (&s, // Stream to dump to
1122 0, // Offset into "packet"
1123 eFormatBytes, // Dump as hex bytes
1124 1, // Size of each item is 1 for single bytes
1125 packet.GetByteSize(), // Number of bytes
1126 UINT32_MAX, // Num bytes per line
1127 LLDB_INVALID_ADDRESS, // Base address
1128 0, 0); // Bitfield info set to not do anything bitfield related
1129 }
1130}
1131
1132uint32_t
1133CommunicationKDP::SendRequestReadRegisters (uint32_t cpu,
1134 uint32_t flavor,
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001135 void *dst,
Greg Claytona63d08c2011-07-19 03:57:15 +00001136 uint32_t dst_len,
1137 Error &error)
1138{
1139 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +00001140 const CommandType command = KDP_READREGS;
Greg Claytona63d08c2011-07-19 03:57:15 +00001141 // Size is header + 4 byte cpu and 4 byte flavor
1142 const uint32_t command_length = 8 + 4 + 4;
Greg Claytona63d08c2011-07-19 03:57:15 +00001143 MakeRequestPacketHeader (command, request_packet, command_length);
1144 request_packet.PutHex32 (cpu);
1145 request_packet.PutHex32 (flavor);
1146 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001147 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Claytona63d08c2011-07-19 03:57:15 +00001148 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001149 lldb::offset_t offset = 8;
Greg Claytona63d08c2011-07-19 03:57:15 +00001150 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1151 uint32_t src_len = reply_packet.GetByteSize() - 12;
1152
1153 if (src_len > 0)
1154 {
1155 const uint32_t bytes_to_copy = std::min<uint32_t>(src_len, dst_len);
1156 const void *src = reply_packet.GetData(&offset, bytes_to_copy);
1157 if (src)
1158 {
1159 ::memcpy (dst, src, bytes_to_copy);
1160 error.Clear();
1161 // Return the number of bytes we could have returned regardless if
1162 // we copied them or not, just so we know when things don't match up
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001163 return src_len;
Greg Claytona63d08c2011-07-19 03:57:15 +00001164 }
1165 }
1166 if (kdp_error)
1167 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error);
1168 else
1169 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u", cpu, flavor);
1170 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +00001171 else
1172 {
1173 error.SetErrorString ("failed to send packet");
1174 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001175 return 0;
1176}
1177
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001178uint32_t
1179CommunicationKDP::SendRequestWriteRegisters (uint32_t cpu,
1180 uint32_t flavor,
1181 const void *src,
1182 uint32_t src_len,
1183 Error &error)
1184{
1185 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
1186 const CommandType command = KDP_WRITEREGS;
1187 // Size is header + 4 byte cpu and 4 byte flavor
Greg Clayton97d5cf02012-09-25 02:40:06 +00001188 const uint32_t command_length = 8 + 4 + 4 + src_len;
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001189 MakeRequestPacketHeader (command, request_packet, command_length);
1190 request_packet.PutHex32 (cpu);
1191 request_packet.PutHex32 (flavor);
1192 request_packet.Write(src, src_len);
1193 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001194 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001195 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001196 lldb::offset_t offset = 8;
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001197 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1198 if (kdp_error == 0)
1199 return src_len;
1200 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error);
1201 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +00001202 else
1203 {
1204 error.SetErrorString ("failed to send packet");
1205 }
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001206 return 0;
1207}
1208
Greg Clayton07e66e32011-07-20 03:41:06 +00001209
1210bool
Greg Clayton97d5cf02012-09-25 02:40:06 +00001211CommunicationKDP::SendRequestResume ()
Greg Clayton07e66e32011-07-20 03:41:06 +00001212{
Greg Clayton07e66e32011-07-20 03:41:06 +00001213 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +00001214 const CommandType command = KDP_RESUMECPUS;
Greg Clayton07e66e32011-07-20 03:41:06 +00001215 const uint32_t command_length = 12;
Greg Clayton07e66e32011-07-20 03:41:06 +00001216 MakeRequestPacketHeader (command, request_packet, command_length);
Greg Clayton97d5cf02012-09-25 02:40:06 +00001217 request_packet.PutHex32(GetCPUMask());
Greg Clayton07e66e32011-07-20 03:41:06 +00001218
1219 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001220 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton07e66e32011-07-20 03:41:06 +00001221 return true;
1222 return false;
1223}
1224
1225bool
1226CommunicationKDP::SendRequestBreakpoint (bool set, addr_t addr)
1227{
1228 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
1229 bool use_64 = (GetVersion() >= 11);
1230 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
Greg Clayton5b882162011-07-21 01:12:01 +00001231 const CommandType command = set ? (use_64 ? KDP_BREAKPOINT_SET64 : KDP_BREAKPOINT_SET ):
1232 (use_64 ? KDP_BREAKPOINT_REMOVE64 : KDP_BREAKPOINT_REMOVE);
Greg Clayton07e66e32011-07-20 03:41:06 +00001233
1234 const uint32_t command_length = 8 + command_addr_byte_size;
Greg Clayton07e66e32011-07-20 03:41:06 +00001235 MakeRequestPacketHeader (command, request_packet, command_length);
1236 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
1237
1238 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001239 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton5b442372011-07-29 22:26:36 +00001240 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001241 lldb::offset_t offset = 8;
Greg Clayton5b442372011-07-29 22:26:36 +00001242 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1243 if (kdp_error == 0)
1244 return true;
1245 }
Greg Clayton07e66e32011-07-20 03:41:06 +00001246 return false;
1247}
1248
1249bool
1250CommunicationKDP::SendRequestSuspend ()
1251{
1252 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +00001253 const CommandType command = KDP_SUSPEND;
Greg Clayton07e66e32011-07-20 03:41:06 +00001254 const uint32_t command_length = 8;
Greg Clayton07e66e32011-07-20 03:41:06 +00001255 MakeRequestPacketHeader (command, request_packet, command_length);
1256 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001257 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton07e66e32011-07-20 03:41:06 +00001258 return true;
1259 return false;
1260}
1261