blob: bbde8ed0709dafb6d783f746037167ebee5bd3d2 [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 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000105 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS));
Greg Clayton97d5cf02012-09-25 02:40:06 +0000106 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
Greg Clayton5160ce52013-03-27 23:08:40 +0000156 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000157 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
Greg Clayton5160ce52013-03-27 23:08:40 +0000205 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS | KDP_LOG_VERBOSE));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000206
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
Greg Clayton5160ce52013-03-27 23:08:40 +0000260 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000261
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:
Jason Molendace62fd72013-03-01 00:43:19 +0000323 case ePacketTypeReply | KDP_READPHYSMEM64:
324 case ePacketTypeReply | KDP_WRITEPHYSMEM64:
325 case ePacketTypeReply | KDP_READIOPORT:
326 case ePacketTypeReply | KDP_WRITEIOPORT:
327 case ePacketTypeReply | KDP_READMSR64:
328 case ePacketTypeReply | KDP_WRITEMSR64:
329 case ePacketTypeReply | KDP_DUMPINFO:
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000330 {
331 offset = 2;
332 const uint16_t length = packet.GetU16 (&offset);
333 if (length <= bytes_available)
334 {
335 // We have an entire packet ready, we need to copy the data
336 // bytes into a buffer that will be owned by the packet and
337 // erase the bytes from our communcation buffer "m_bytes"
338 packet.SetData (DataBufferSP (new DataBufferHeap (&m_bytes[0], length)));
339 m_bytes.erase (0, length);
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000340
341 if (log)
342 {
343 PacketStreamType log_strm;
Greg Claytona63d08c2011-07-19 03:57:15 +0000344 DumpPacket (log_strm, packet);
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000345
Greg Claytona63d08c2011-07-19 03:57:15 +0000346 log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData());
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000347 }
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000348 return true;
349 }
350 }
351 break;
352
353 default:
354 // Unrecognized reply command byte, erase this byte and try to get back on track
355 if (log)
356 log->Printf ("CommunicationKDP::%s: tossing junk byte: 0x%2.2x",
357 __FUNCTION__,
358 (uint8_t)m_bytes[0]);
359 m_bytes.erase(0, 1);
360 break;
361 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000362 }
363 packet.Clear();
364 return false;
365}
366
Greg Clayton57508022011-07-15 16:31:38 +0000367
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000368bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000369CommunicationKDP::SendRequestConnect (uint16_t reply_port,
370 uint16_t exc_port,
371 const char *greeting)
Greg Clayton57508022011-07-15 16:31:38 +0000372{
Greg Claytona63d08c2011-07-19 03:57:15 +0000373 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000374 if (greeting == NULL)
375 greeting = "";
376
Greg Clayton5b882162011-07-21 01:12:01 +0000377 const CommandType command = KDP_CONNECT;
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000378 // Length is 82 uint16_t and the length of the greeting C string with the terminating NULL
379 const uint32_t command_length = 8 + 2 + 2 + ::strlen(greeting) + 1;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000380 MakeRequestPacketHeader (command, request_packet, command_length);
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000381 // Always send connect ports as little endian
382 request_packet.SetByteOrder (eByteOrderLittle);
383 request_packet.PutHex16 (reply_port);
384 request_packet.PutHex16 (exc_port);
385 request_packet.SetByteOrder (m_byte_order);
386 request_packet.PutCString (greeting);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000387 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000388 return SendRequestAndGetReply (command, request_packet, reply_packet);
Greg Clayton57508022011-07-15 16:31:38 +0000389}
390
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000391void
392CommunicationKDP::ClearKDPSettings ()
393{
394 m_request_sequence_id = 0;
395 m_kdp_version_version = 0;
396 m_kdp_version_feature = 0;
397 m_kdp_hostinfo_cpu_mask = 0;
398 m_kdp_hostinfo_cpu_type = 0;
399 m_kdp_hostinfo_cpu_subtype = 0;
400}
401
402bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000403CommunicationKDP::SendRequestReattach (uint16_t reply_port)
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000404{
Greg Claytona63d08c2011-07-19 03:57:15 +0000405 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000406 const CommandType command = KDP_REATTACH;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000407 // Length is 8 bytes for the header plus 2 bytes for the reply UDP port
408 const uint32_t command_length = 8 + 2;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000409 MakeRequestPacketHeader (command, request_packet, command_length);
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000410 // Always send connect ports as little endian
411 request_packet.SetByteOrder (eByteOrderLittle);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000412 request_packet.PutHex16(reply_port);
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000413 request_packet.SetByteOrder (m_byte_order);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000414 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000415 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000416 {
417 // Reset the sequence ID to zero for reattach
418 ClearKDPSettings ();
Greg Claytonc7bece562013-01-25 18:06:21 +0000419 lldb::offset_t offset = 4;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000420 m_session_key = reply_packet.GetU32 (&offset);
421 return true;
422 }
423 return false;
424}
425
426uint32_t
427CommunicationKDP::GetVersion ()
428{
429 if (!VersionIsValid())
430 SendRequestVersion();
431 return m_kdp_version_version;
432}
433
434uint32_t
435CommunicationKDP::GetFeatureFlags ()
436{
437 if (!VersionIsValid())
438 SendRequestVersion();
439 return m_kdp_version_feature;
440}
441
442bool
443CommunicationKDP::SendRequestVersion ()
444{
Greg Claytona63d08c2011-07-19 03:57:15 +0000445 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000446 const CommandType command = KDP_VERSION;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000447 const uint32_t command_length = 8;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000448 MakeRequestPacketHeader (command, request_packet, command_length);
449 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000450 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000451 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000452 lldb::offset_t offset = 8;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000453 m_kdp_version_version = reply_packet.GetU32 (&offset);
454 m_kdp_version_feature = reply_packet.GetU32 (&offset);
455 return true;
456 }
457 return false;
458}
459
Greg Clayton5b882162011-07-21 01:12:01 +0000460#if 0 // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
461const char *
462CommunicationKDP::GetImagePath ()
463{
464 if (m_image_path.empty())
465 SendRequestImagePath();
466 return m_image_path.c_str();
467}
468
469bool
470CommunicationKDP::SendRequestImagePath ()
471{
472 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
473 const CommandType command = KDP_IMAGEPATH;
474 const uint32_t command_length = 8;
Greg Clayton5b882162011-07-21 01:12:01 +0000475 MakeRequestPacketHeader (command, request_packet, command_length);
476 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000477 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton5b882162011-07-21 01:12:01 +0000478 {
479 const char *path = reply_packet.PeekCStr(8);
480 if (path && path[0])
481 m_kernel_version.assign (path);
482 return true;
483 }
484 return false;
485}
486#endif
487
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000488uint32_t
489CommunicationKDP::GetCPUMask ()
490{
491 if (!HostInfoIsValid())
492 SendRequestHostInfo();
493 return m_kdp_hostinfo_cpu_mask;
494}
495
496uint32_t
497CommunicationKDP::GetCPUType ()
498{
499 if (!HostInfoIsValid())
500 SendRequestHostInfo();
501 return m_kdp_hostinfo_cpu_type;
502}
503
504uint32_t
505CommunicationKDP::GetCPUSubtype ()
506{
507 if (!HostInfoIsValid())
508 SendRequestHostInfo();
509 return m_kdp_hostinfo_cpu_subtype;
510}
511
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000512lldb_private::UUID
513CommunicationKDP::GetUUID ()
514{
515 UUID uuid;
516 if (GetKernelVersion() == NULL)
517 return uuid;
518
519 if (m_kernel_version.find("UUID=") == std::string::npos)
520 return uuid;
521
522 size_t p = m_kernel_version.find("UUID=") + strlen ("UUID=");
523 std::string uuid_str = m_kernel_version.substr(p, 36);
524 if (uuid_str.size() < 32)
525 return uuid;
526
527 if (uuid.SetFromCString (uuid_str.c_str()) == 0)
528 {
529 UUID invalid_uuid;
530 return invalid_uuid;
531 }
532
533 return uuid;
534}
535
Jason Molenda840f12c2012-10-25 00:25:13 +0000536bool
537CommunicationKDP::RemoteIsEFI ()
538{
539 if (GetKernelVersion() == NULL)
540 return false;
541 if (strncmp (m_kernel_version.c_str(), "EFI", 3) == 0)
542 return true;
543 else
544 return false;
545}
546
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000547lldb::addr_t
548CommunicationKDP::GetLoadAddress ()
549{
550 if (GetKernelVersion() == NULL)
551 return LLDB_INVALID_ADDRESS;
552
553 if (m_kernel_version.find("stext=") == std::string::npos)
554 return LLDB_INVALID_ADDRESS;
555 size_t p = m_kernel_version.find("stext=") + strlen ("stext=");
556 if (m_kernel_version[p] != '0' || m_kernel_version[p + 1] != 'x')
557 return LLDB_INVALID_ADDRESS;
558
559 addr_t kernel_load_address;
560 errno = 0;
561 kernel_load_address = ::strtoul (m_kernel_version.c_str() + p, NULL, 16);
562 if (errno != 0 || kernel_load_address == 0)
563 return LLDB_INVALID_ADDRESS;
564
565 return kernel_load_address;
566}
567
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000568bool
569CommunicationKDP::SendRequestHostInfo ()
570{
Greg Claytona63d08c2011-07-19 03:57:15 +0000571 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000572 const CommandType command = KDP_HOSTINFO;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000573 const uint32_t command_length = 8;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000574 MakeRequestPacketHeader (command, request_packet, command_length);
575 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000576 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000577 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000578 lldb::offset_t offset = 8;
Greg Claytona63d08c2011-07-19 03:57:15 +0000579 m_kdp_hostinfo_cpu_mask = reply_packet.GetU32 (&offset);
580 m_kdp_hostinfo_cpu_type = reply_packet.GetU32 (&offset);
581 m_kdp_hostinfo_cpu_subtype = reply_packet.GetU32 (&offset);
582
583 ArchSpec kernel_arch;
584 kernel_arch.SetArchitecture (eArchTypeMachO,
585 m_kdp_hostinfo_cpu_type,
586 m_kdp_hostinfo_cpu_subtype);
587
588 m_addr_byte_size = kernel_arch.GetAddressByteSize();
589 m_byte_order = kernel_arch.GetByteOrder();
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000590 return true;
591 }
592 return false;
593}
594
Greg Clayton07e66e32011-07-20 03:41:06 +0000595const char *
596CommunicationKDP::GetKernelVersion ()
597{
598 if (m_kernel_version.empty())
599 SendRequestKernelVersion ();
600 return m_kernel_version.c_str();
601}
602
603bool
604CommunicationKDP::SendRequestKernelVersion ()
605{
606 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000607 const CommandType command = KDP_KERNELVERSION;
Greg Clayton07e66e32011-07-20 03:41:06 +0000608 const uint32_t command_length = 8;
Greg Clayton07e66e32011-07-20 03:41:06 +0000609 MakeRequestPacketHeader (command, request_packet, command_length);
610 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000611 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton07e66e32011-07-20 03:41:06 +0000612 {
613 const char *kernel_version_cstr = reply_packet.PeekCStr(8);
614 if (kernel_version_cstr && kernel_version_cstr[0])
615 m_kernel_version.assign (kernel_version_cstr);
616 return true;
617 }
618 return false;
619}
620
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000621bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000622CommunicationKDP::SendRequestDisconnect ()
Greg Clayton57508022011-07-15 16:31:38 +0000623{
Greg Claytona63d08c2011-07-19 03:57:15 +0000624 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000625 const CommandType command = KDP_DISCONNECT;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000626 const uint32_t command_length = 8;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000627 MakeRequestPacketHeader (command, request_packet, command_length);
628 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000629 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000630 {
631 // Are we supposed to get a reply for disconnect?
632 }
633 ClearKDPSettings ();
634 return true;
Greg Clayton57508022011-07-15 16:31:38 +0000635}
636
Greg Claytona63d08c2011-07-19 03:57:15 +0000637uint32_t
638CommunicationKDP::SendRequestReadMemory (lldb::addr_t addr,
639 void *dst,
640 uint32_t dst_len,
641 Error &error)
642{
643 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
644 bool use_64 = (GetVersion() >= 11);
645 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
Greg Clayton5b882162011-07-21 01:12:01 +0000646 const CommandType command = use_64 ? KDP_READMEM64 : KDP_READMEM;
Greg Claytona63d08c2011-07-19 03:57:15 +0000647 // Size is header + address size + uint32_t length
648 const uint32_t command_length = 8 + command_addr_byte_size + 4;
Greg Claytona63d08c2011-07-19 03:57:15 +0000649 MakeRequestPacketHeader (command, request_packet, command_length);
650 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
651 request_packet.PutHex32 (dst_len);
652 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000653 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Claytona63d08c2011-07-19 03:57:15 +0000654 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000655 lldb::offset_t offset = 8;
Greg Claytona63d08c2011-07-19 03:57:15 +0000656 uint32_t kdp_error = reply_packet.GetU32 (&offset);
657 uint32_t src_len = reply_packet.GetByteSize() - 12;
658
659 if (src_len > 0)
660 {
661 const void *src = reply_packet.GetData(&offset, src_len);
662 if (src)
663 {
664 ::memcpy (dst, src, src_len);
665 error.Clear();
666 return src_len;
667 }
668 }
669 if (kdp_error)
670 error.SetErrorStringWithFormat ("kdp read memory failed (error %u)", kdp_error);
671 else
672 error.SetErrorString ("kdp read memory failed");
673 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000674 else
675 {
676 error.SetErrorString ("failed to send packet");
677 }
Greg Claytona63d08c2011-07-19 03:57:15 +0000678 return 0;
679}
680
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000681
682uint32_t
683CommunicationKDP::SendRequestWriteMemory (lldb::addr_t addr,
684 const void *src,
685 uint32_t src_len,
686 Error &error)
687{
688 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
689 bool use_64 = (GetVersion() >= 11);
690 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
Greg Clayton5b882162011-07-21 01:12:01 +0000691 const CommandType command = use_64 ? KDP_WRITEMEM64 : KDP_WRITEMEM;
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000692 // Size is header + address size + uint32_t length
Greg Claytonb556c9b2012-10-12 23:24:05 +0000693 const uint32_t command_length = 8 + command_addr_byte_size + 4 + src_len;
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000694 MakeRequestPacketHeader (command, request_packet, command_length);
695 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
696 request_packet.PutHex32 (src_len);
Jason Molenda57656e72012-10-19 02:16:22 +0000697 request_packet.PutRawBytes(src, src_len);
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000698
699 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000700 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000701 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000702 lldb::offset_t offset = 8;
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000703 uint32_t kdp_error = reply_packet.GetU32 (&offset);
704 if (kdp_error)
705 error.SetErrorStringWithFormat ("kdp write memory failed (error %u)", kdp_error);
706 else
707 {
708 error.Clear();
709 return src_len;
710 }
711 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000712 else
713 {
714 error.SetErrorString ("failed to send packet");
715 }
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000716 return 0;
717}
718
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000719bool
720CommunicationKDP::SendRawRequest (uint8_t command_byte,
721 const void *src, // Raw packet payload bytes
722 uint32_t src_len, // Raw packet payload length
723 DataExtractor &reply_packet,
724 Error &error)
725{
726 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
727 // Size is header + address size + uint32_t length
728 const uint32_t command_length = 8 + src_len;
729 const CommandType command = (CommandType)command_byte;
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000730 MakeRequestPacketHeader (command, request_packet, command_length);
731 request_packet.PutRawBytes(src, src_len);
732
Greg Clayton0ee809b2013-02-14 19:11:23 +0000733 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000734 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000735 lldb::offset_t offset = 8;
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000736 uint32_t kdp_error = reply_packet.GetU32 (&offset);
Jason Molendace62fd72013-03-01 00:43:19 +0000737 if (kdp_error && (command_byte != KDP_DUMPINFO))
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000738 error.SetErrorStringWithFormat ("request packet 0x%8.8x failed (error %u)", command_byte, kdp_error);
739 else
740 {
741 error.Clear();
742 return true;
743 }
744 }
745 else
746 {
747 error.SetErrorString ("failed to send packet");
748 }
749 return false;
750}
751
752
Greg Claytona63d08c2011-07-19 03:57:15 +0000753const char *
754CommunicationKDP::GetCommandAsCString (uint8_t command)
755{
756 switch (command)
757 {
Greg Clayton5b882162011-07-21 01:12:01 +0000758 case KDP_CONNECT: return "KDP_CONNECT";
759 case KDP_DISCONNECT: return "KDP_DISCONNECT";
760 case KDP_HOSTINFO: return "KDP_HOSTINFO";
761 case KDP_VERSION: return "KDP_VERSION";
762 case KDP_MAXBYTES: return "KDP_MAXBYTES";
763 case KDP_READMEM: return "KDP_READMEM";
764 case KDP_WRITEMEM: return "KDP_WRITEMEM";
765 case KDP_READREGS: return "KDP_READREGS";
766 case KDP_WRITEREGS: return "KDP_WRITEREGS";
767 case KDP_LOAD: return "KDP_LOAD";
768 case KDP_IMAGEPATH: return "KDP_IMAGEPATH";
769 case KDP_SUSPEND: return "KDP_SUSPEND";
770 case KDP_RESUMECPUS: return "KDP_RESUMECPUS";
771 case KDP_EXCEPTION: return "KDP_EXCEPTION";
772 case KDP_TERMINATION: return "KDP_TERMINATION";
773 case KDP_BREAKPOINT_SET: return "KDP_BREAKPOINT_SET";
774 case KDP_BREAKPOINT_REMOVE: return "KDP_BREAKPOINT_REMOVE";
775 case KDP_REGIONS: return "KDP_REGIONS";
776 case KDP_REATTACH: return "KDP_REATTACH";
777 case KDP_HOSTREBOOT: return "KDP_HOSTREBOOT";
778 case KDP_READMEM64: return "KDP_READMEM64";
779 case KDP_WRITEMEM64: return "KDP_WRITEMEM64";
780 case KDP_BREAKPOINT_SET64: return "KDP_BREAKPOINT64_SET";
781 case KDP_BREAKPOINT_REMOVE64: return "KDP_BREAKPOINT64_REMOVE";
782 case KDP_KERNELVERSION: return "KDP_KERNELVERSION";
Jason Molendace62fd72013-03-01 00:43:19 +0000783 case KDP_READPHYSMEM64: return "KDP_READPHYSMEM64";
784 case KDP_WRITEPHYSMEM64: return "KDP_WRITEPHYSMEM64";
785 case KDP_READIOPORT: return "KDP_READIOPORT";
786 case KDP_WRITEIOPORT: return "KDP_WRITEIOPORT";
787 case KDP_READMSR64: return "KDP_READMSR64";
788 case KDP_WRITEMSR64: return "KDP_WRITEMSR64";
789 case KDP_DUMPINFO: return "KDP_DUMPINFO";
Greg Claytona63d08c2011-07-19 03:57:15 +0000790 }
791 return NULL;
792}
793
794void
795CommunicationKDP::DumpPacket (Stream &s, const void *data, uint32_t data_len)
796{
797 DataExtractor extractor (data, data_len, m_byte_order, m_addr_byte_size);
798 DumpPacket (s, extractor);
799}
800
801void
802CommunicationKDP::DumpPacket (Stream &s, const DataExtractor& packet)
803{
804 const char *error_desc = NULL;
805 if (packet.GetByteSize() < 8)
806 {
807 error_desc = "error: invalid packet (too short): ";
808 }
809 else
810 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000811 lldb::offset_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000812 const uint8_t first_packet_byte = packet.GetU8 (&offset);
813 const uint8_t sequence_id = packet.GetU8 (&offset);
814 const uint16_t length = packet.GetU16 (&offset);
815 const uint32_t key = packet.GetU32 (&offset);
816 const CommandType command = ExtractCommand (first_packet_byte);
817 const char *command_name = GetCommandAsCString (command);
818 if (command_name)
819 {
820 const bool is_reply = ExtractIsReply(first_packet_byte);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000821 s.Printf ("(running=%i) %s %24s: 0x%2.2x 0x%2.2x 0x%4.4x 0x%8.8x ",
822 IsRunning(),
823 is_reply ? "<--" : "-->",
824 command_name,
825 first_packet_byte,
Greg Claytona63d08c2011-07-19 03:57:15 +0000826 sequence_id,
827 length,
Greg Clayton97d5cf02012-09-25 02:40:06 +0000828 key);
Greg Claytona63d08c2011-07-19 03:57:15 +0000829
830 if (is_reply)
831 {
832 // Dump request reply packets
833 switch (command)
834 {
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000835 // Commands that return a single 32 bit error
Greg Clayton5b882162011-07-21 01:12:01 +0000836 case KDP_CONNECT:
837 case KDP_WRITEMEM:
838 case KDP_WRITEMEM64:
839 case KDP_BREAKPOINT_SET:
840 case KDP_BREAKPOINT_REMOVE:
841 case KDP_BREAKPOINT_SET64:
842 case KDP_BREAKPOINT_REMOVE64:
843 case KDP_WRITEREGS:
844 case KDP_LOAD:
Jason Molendace62fd72013-03-01 00:43:19 +0000845 case KDP_WRITEIOPORT:
846 case KDP_WRITEMSR64:
Greg Claytona63d08c2011-07-19 03:57:15 +0000847 {
848 const uint32_t error = packet.GetU32 (&offset);
849 s.Printf(" (error=0x%8.8x)", error);
850 }
851 break;
852
Greg Clayton5b882162011-07-21 01:12:01 +0000853 case KDP_DISCONNECT:
854 case KDP_REATTACH:
855 case KDP_HOSTREBOOT:
856 case KDP_SUSPEND:
857 case KDP_RESUMECPUS:
858 case KDP_EXCEPTION:
859 case KDP_TERMINATION:
Greg Claytona63d08c2011-07-19 03:57:15 +0000860 // No return value for the reply, just the header to ack
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000861 s.PutCString(" ()");
Greg Claytona63d08c2011-07-19 03:57:15 +0000862 break;
863
Greg Clayton5b882162011-07-21 01:12:01 +0000864 case KDP_HOSTINFO:
Greg Claytona63d08c2011-07-19 03:57:15 +0000865 {
866 const uint32_t cpu_mask = packet.GetU32 (&offset);
867 const uint32_t cpu_type = packet.GetU32 (&offset);
868 const uint32_t cpu_subtype = packet.GetU32 (&offset);
869 s.Printf(" (cpu_mask=0x%8.8x, cpu_type=0x%8.8x, cpu_subtype=0x%8.8x)", cpu_mask, cpu_type, cpu_subtype);
870 }
871 break;
872
Greg Clayton5b882162011-07-21 01:12:01 +0000873 case KDP_VERSION:
Greg Claytona63d08c2011-07-19 03:57:15 +0000874 {
875 const uint32_t version = packet.GetU32 (&offset);
876 const uint32_t feature = packet.GetU32 (&offset);
877 s.Printf(" (version=0x%8.8x, feature=0x%8.8x)", version, feature);
878 }
879 break;
880
Greg Clayton5b882162011-07-21 01:12:01 +0000881 case KDP_REGIONS:
Greg Claytona63d08c2011-07-19 03:57:15 +0000882 {
883 const uint32_t region_count = packet.GetU32 (&offset);
884 s.Printf(" (count = %u", region_count);
885 for (uint32_t i=0; i<region_count; ++i)
886 {
887 const addr_t region_addr = packet.GetPointer (&offset);
888 const uint32_t region_size = packet.GetU32 (&offset);
889 const uint32_t region_prot = packet.GetU32 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +0000890 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 +0000891 }
892 }
893 break;
894
Greg Clayton5b882162011-07-21 01:12:01 +0000895 case KDP_READMEM:
896 case KDP_READMEM64:
Jason Molendace62fd72013-03-01 00:43:19 +0000897 case KDP_READPHYSMEM64:
Greg Claytona63d08c2011-07-19 03:57:15 +0000898 {
899 const uint32_t error = packet.GetU32 (&offset);
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000900 const uint32_t count = packet.GetByteSize() - offset;
Greg Clayton5b442372011-07-29 22:26:36 +0000901 s.Printf(" (error = 0x%8.8x:\n", error);
Greg Claytona63d08c2011-07-19 03:57:15 +0000902 if (count > 0)
Greg Clayton07e66e32011-07-20 03:41:06 +0000903 packet.Dump (&s, // Stream to dump to
904 offset, // Offset within "packet"
905 eFormatBytesWithASCII, // Format to use
906 1, // Size of each item in bytes
907 count, // Number of items
908 16, // Number per line
909 m_last_read_memory_addr, // Don't show addresses before each line
910 0, 0); // No bitfields
Greg Claytona63d08c2011-07-19 03:57:15 +0000911 }
912 break;
913
Greg Clayton5b882162011-07-21 01:12:01 +0000914 case KDP_READREGS:
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000915 {
916 const uint32_t error = packet.GetU32 (&offset);
917 const uint32_t count = packet.GetByteSize() - offset;
Greg Clayton5b442372011-07-29 22:26:36 +0000918 s.Printf(" (error = 0x%8.8x regs:\n", error);
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000919 if (count > 0)
920 packet.Dump (&s, // Stream to dump to
921 offset, // Offset within "packet"
922 eFormatHex, // Format to use
923 m_addr_byte_size, // Size of each item in bytes
924 count / m_addr_byte_size, // Number of items
925 16 / m_addr_byte_size, // Number per line
926 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
927 0, 0); // No bitfields
928 }
929 break;
930
Greg Clayton5b882162011-07-21 01:12:01 +0000931 case KDP_KERNELVERSION:
Greg Clayton07e66e32011-07-20 03:41:06 +0000932 {
933 const char *kernel_version = packet.PeekCStr(8);
934 s.Printf(" (version = \"%s\")", kernel_version);
935 }
936 break;
937
Greg Clayton5b882162011-07-21 01:12:01 +0000938 case KDP_MAXBYTES:
Greg Clayton07e66e32011-07-20 03:41:06 +0000939 {
940 const uint32_t max_bytes = packet.GetU32 (&offset);
941 s.Printf(" (max_bytes = 0x%8.8x (%u))", max_bytes, max_bytes);
942 }
943 break;
Greg Clayton5b882162011-07-21 01:12:01 +0000944 case KDP_IMAGEPATH:
Greg Clayton07e66e32011-07-20 03:41:06 +0000945 {
946 const char *path = packet.GetCStr(&offset);
947 s.Printf(" (path = \"%s\")", path);
948 }
949 break;
Jason Molendace62fd72013-03-01 00:43:19 +0000950
951 case KDP_READIOPORT:
952 case KDP_READMSR64:
953 {
954 const uint32_t error = packet.GetU32 (&offset);
955 const uint32_t count = packet.GetByteSize() - offset;
956 s.Printf(" (error = 0x%8.8x io:\n", error);
957 if (count > 0)
958 packet.Dump (&s, // Stream to dump to
959 offset, // Offset within "packet"
960 eFormatHex, // Format to use
961 1, // Size of each item in bytes
962 count, // Number of items
963 16, // Number per line
964 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
965 0, 0); // No bitfields
966 }
967 break;
968 case KDP_DUMPINFO:
969 {
970 const uint32_t count = packet.GetByteSize() - offset;
971 s.Printf(" (count = %u, bytes = \n", count);
972 if (count > 0)
973 packet.Dump (&s, // Stream to dump to
974 offset, // Offset within "packet"
975 eFormatHex, // Format to use
976 1, // Size of each item in bytes
977 count, // Number of items
978 16, // Number per line
979 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
980 0, 0); // No bitfields
981
982 }
983 break;
984
Greg Clayton07e66e32011-07-20 03:41:06 +0000985 default:
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000986 s.Printf(" (add support for dumping this packet reply!!!");
Greg Claytona63d08c2011-07-19 03:57:15 +0000987 break;
988
989 }
990 }
991 else
992 {
993 // Dump request packets
994 switch (command)
995 {
Greg Clayton5b882162011-07-21 01:12:01 +0000996 case KDP_CONNECT:
Greg Claytona63d08c2011-07-19 03:57:15 +0000997 {
998 const uint16_t reply_port = packet.GetU16 (&offset);
999 const uint16_t exc_port = packet.GetU16 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +00001000 s.Printf(" (reply_port = %u, exc_port = %u, greeting = \"%s\")", reply_port, exc_port, packet.GetCStr(&offset));
Greg Claytona63d08c2011-07-19 03:57:15 +00001001 }
1002 break;
1003
Greg Clayton5b882162011-07-21 01:12:01 +00001004 case KDP_DISCONNECT:
1005 case KDP_HOSTREBOOT:
1006 case KDP_HOSTINFO:
1007 case KDP_VERSION:
1008 case KDP_REGIONS:
1009 case KDP_KERNELVERSION:
1010 case KDP_MAXBYTES:
1011 case KDP_IMAGEPATH:
1012 case KDP_SUSPEND:
Greg Claytona63d08c2011-07-19 03:57:15 +00001013 // No args, just the header in the request...
Greg Clayton07e66e32011-07-20 03:41:06 +00001014 s.PutCString(" ()");
1015 break;
1016
Greg Clayton5b882162011-07-21 01:12:01 +00001017 case KDP_RESUMECPUS:
Greg Clayton07e66e32011-07-20 03:41:06 +00001018 {
1019 const uint32_t cpu_mask = packet.GetU32 (&offset);
1020 s.Printf(" (cpu_mask = 0x%8.8x)", cpu_mask);
1021 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001022 break;
1023
Greg Clayton5b882162011-07-21 01:12:01 +00001024 case KDP_READMEM:
Greg Claytona63d08c2011-07-19 03:57:15 +00001025 {
1026 const uint32_t addr = packet.GetU32 (&offset);
1027 const uint32_t size = packet.GetU32 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +00001028 s.Printf(" (addr = 0x%8.8x, size = %u)", addr, size);
Greg Clayton07e66e32011-07-20 03:41:06 +00001029 m_last_read_memory_addr = addr;
Greg Claytona63d08c2011-07-19 03:57:15 +00001030 }
1031 break;
1032
Greg Clayton5b882162011-07-21 01:12:01 +00001033 case KDP_WRITEMEM:
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001034 {
1035 const uint32_t addr = packet.GetU32 (&offset);
1036 const uint32_t size = packet.GetU32 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +00001037 s.Printf(" (addr = 0x%8.8x, size = %u, bytes = \n", addr, size);
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001038 if (size > 0)
1039 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
1040 }
1041 break;
1042
Greg Clayton5b882162011-07-21 01:12:01 +00001043 case KDP_READMEM64:
Greg Claytona63d08c2011-07-19 03:57:15 +00001044 {
1045 const uint64_t addr = packet.GetU64 (&offset);
1046 const uint32_t size = packet.GetU32 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +00001047 s.Printf(" (addr = 0x%16.16" PRIx64 ", size = %u)", addr, size);
Greg Clayton07e66e32011-07-20 03:41:06 +00001048 m_last_read_memory_addr = addr;
Greg Claytona63d08c2011-07-19 03:57:15 +00001049 }
1050 break;
1051
Jason Molendace62fd72013-03-01 00:43:19 +00001052 case KDP_READPHYSMEM64:
1053 {
1054 const uint64_t addr = packet.GetU64 (&offset);
1055 const uint32_t size = packet.GetU32 (&offset);
1056 const uint32_t lcpu = packet.GetU16 (&offset);
1057 s.Printf(" (addr = 0x%16.16llx, size = %u, lcpu = %u)", addr, size, lcpu);
1058 m_last_read_memory_addr = addr;
1059 }
1060 break;
1061
Greg Clayton5b882162011-07-21 01:12:01 +00001062 case KDP_WRITEMEM64:
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001063 {
1064 const uint64_t addr = packet.GetU64 (&offset);
1065 const uint32_t size = packet.GetU32 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +00001066 s.Printf(" (addr = 0x%16.16" PRIx64 ", size = %u, bytes = \n", addr, size);
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001067 if (size > 0)
1068 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
1069 }
1070 break;
1071
Jason Molendace62fd72013-03-01 00:43:19 +00001072 case KDP_WRITEPHYSMEM64:
1073 {
1074 const uint64_t addr = packet.GetU64 (&offset);
1075 const uint32_t size = packet.GetU32 (&offset);
1076 const uint32_t lcpu = packet.GetU16 (&offset);
1077 s.Printf(" (addr = 0x%16.16llx, size = %u, lcpu = %u, bytes = \n", addr, size, lcpu);
1078 if (size > 0)
1079 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
1080 }
1081 break;
1082
Greg Clayton5b882162011-07-21 01:12:01 +00001083 case KDP_READREGS:
Greg Claytona63d08c2011-07-19 03:57:15 +00001084 {
1085 const uint32_t cpu = packet.GetU32 (&offset);
1086 const uint32_t flavor = packet.GetU32 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +00001087 s.Printf(" (cpu = %u, flavor = %u)", cpu, flavor);
Greg Claytona63d08c2011-07-19 03:57:15 +00001088 }
1089 break;
1090
Greg Clayton5b882162011-07-21 01:12:01 +00001091 case KDP_WRITEREGS:
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001092 {
1093 const uint32_t cpu = packet.GetU32 (&offset);
1094 const uint32_t flavor = packet.GetU32 (&offset);
1095 const uint32_t nbytes = packet.GetByteSize() - offset;
Greg Clayton5b442372011-07-29 22:26:36 +00001096 s.Printf(" (cpu = %u, flavor = %u, regs = \n", cpu, flavor);
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001097 if (nbytes > 0)
1098 packet.Dump (&s, // Stream to dump to
1099 offset, // Offset within "packet"
1100 eFormatHex, // Format to use
1101 m_addr_byte_size, // Size of each item in bytes
1102 nbytes / m_addr_byte_size, // Number of items
1103 16 / m_addr_byte_size, // Number per line
1104 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1105 0, 0); // No bitfields
1106 }
1107 break;
1108
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001109
Greg Clayton5b882162011-07-21 01:12:01 +00001110 case KDP_BREAKPOINT_SET:
1111 case KDP_BREAKPOINT_REMOVE:
Greg Clayton07e66e32011-07-20 03:41:06 +00001112 {
1113 const uint32_t addr = packet.GetU32 (&offset);
1114 s.Printf(" (addr = 0x%8.8x)", addr);
1115 }
1116 break;
1117
Greg Clayton5b882162011-07-21 01:12:01 +00001118 case KDP_BREAKPOINT_SET64:
1119 case KDP_BREAKPOINT_REMOVE64:
Greg Clayton07e66e32011-07-20 03:41:06 +00001120 {
1121 const uint64_t addr = packet.GetU64 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +00001122 s.Printf(" (addr = 0x%16.16" PRIx64 ")", addr);
Greg Clayton07e66e32011-07-20 03:41:06 +00001123 }
1124 break;
1125
1126
Greg Clayton5b882162011-07-21 01:12:01 +00001127 case KDP_LOAD:
Greg Clayton07e66e32011-07-20 03:41:06 +00001128 {
1129 const char *path = packet.GetCStr(&offset);
1130 s.Printf(" (path = \"%s\")", path);
1131 }
1132 break;
1133
Greg Clayton5b882162011-07-21 01:12:01 +00001134 case KDP_EXCEPTION:
Greg Clayton07e66e32011-07-20 03:41:06 +00001135 {
1136 const uint32_t count = packet.GetU32 (&offset);
1137
Greg Clayton07e66e32011-07-20 03:41:06 +00001138 for (uint32_t i=0; i<count; ++i)
1139 {
1140 const uint32_t cpu = packet.GetU32 (&offset);
1141 const uint32_t exc = packet.GetU32 (&offset);
1142 const uint32_t code = packet.GetU32 (&offset);
1143 const uint32_t subcode = packet.GetU32 (&offset);
1144 const char *exc_cstr = NULL;
1145 switch (exc)
1146 {
1147 case 1: exc_cstr = "EXC_BAD_ACCESS"; break;
1148 case 2: exc_cstr = "EXC_BAD_INSTRUCTION"; break;
1149 case 3: exc_cstr = "EXC_ARITHMETIC"; break;
1150 case 4: exc_cstr = "EXC_EMULATION"; break;
1151 case 5: exc_cstr = "EXC_SOFTWARE"; break;
1152 case 6: exc_cstr = "EXC_BREAKPOINT"; break;
1153 case 7: exc_cstr = "EXC_SYSCALL"; break;
1154 case 8: exc_cstr = "EXC_MACH_SYSCALL"; break;
1155 case 9: exc_cstr = "EXC_RPC_ALERT"; break;
1156 case 10: exc_cstr = "EXC_CRASH"; break;
1157 default:
1158 break;
1159 }
1160
Greg Clayton97d5cf02012-09-25 02:40:06 +00001161 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 +00001162 cpu, exc_cstr, exc, code, code, subcode, subcode);
1163 }
1164 }
1165 break;
1166
Greg Clayton5b882162011-07-21 01:12:01 +00001167 case KDP_TERMINATION:
Greg Clayton07e66e32011-07-20 03:41:06 +00001168 {
1169 const uint32_t term_code = packet.GetU32 (&offset);
1170 const uint32_t exit_code = packet.GetU32 (&offset);
1171 s.Printf(" (term_code = 0x%8.8x (%u), exit_code = 0x%8.8x (%u))", term_code, term_code, exit_code, exit_code);
1172 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001173 break;
1174
Greg Clayton5b882162011-07-21 01:12:01 +00001175 case KDP_REATTACH:
Greg Claytona63d08c2011-07-19 03:57:15 +00001176 {
1177 const uint16_t reply_port = packet.GetU16 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +00001178 s.Printf(" (reply_port = %u)", reply_port);
Greg Claytona63d08c2011-07-19 03:57:15 +00001179 }
1180 break;
Jason Molendace62fd72013-03-01 00:43:19 +00001181
1182 case KDP_READMSR64:
1183 {
1184 const uint32_t address = packet.GetU32 (&offset);
1185 const uint16_t lcpu = packet.GetU16 (&offset);
1186 s.Printf(" (address=0x%8.8x, lcpu=0x%4.4x)", address, lcpu);
1187 }
1188 break;
1189
1190 case KDP_WRITEMSR64:
1191 {
1192 const uint32_t address = packet.GetU32 (&offset);
1193 const uint16_t lcpu = packet.GetU16 (&offset);
1194 const uint32_t nbytes = packet.GetByteSize() - offset;
1195 s.Printf(" (address=0x%8.8x, lcpu=0x%4.4x, nbytes=0x%8.8x)", lcpu, address, nbytes);
1196 if (nbytes > 0)
1197 packet.Dump (&s, // Stream to dump to
1198 offset, // Offset within "packet"
1199 eFormatHex, // Format to use
1200 1, // Size of each item in bytes
1201 nbytes, // Number of items
1202 16, // Number per line
1203 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1204 0, 0); // No bitfields
1205 }
1206 break;
1207
1208 case KDP_READIOPORT:
1209 {
1210 const uint16_t lcpu = packet.GetU16 (&offset);
1211 const uint16_t address = packet.GetU16 (&offset);
1212 const uint16_t nbytes = packet.GetU16 (&offset);
1213 s.Printf(" (lcpu=0x%4.4x, address=0x%4.4x, nbytes=%u)", lcpu, address, nbytes);
1214 }
1215 break;
1216
1217 case KDP_WRITEIOPORT:
1218 {
1219 const uint16_t lcpu = packet.GetU16 (&offset);
1220 const uint16_t address = packet.GetU16 (&offset);
1221 const uint16_t nbytes = packet.GetU16 (&offset);
1222 s.Printf(" (lcpu = %u, addr = 0x%4.4x, nbytes = %u, bytes = \n", lcpu, address, nbytes);
1223 if (nbytes > 0)
1224 packet.Dump (&s, // Stream to dump to
1225 offset, // Offset within "packet"
1226 eFormatHex, // Format to use
1227 1, // Size of each item in bytes
1228 nbytes, // Number of items
1229 16, // Number per line
1230 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1231 0, 0); // No bitfields
1232 }
1233 break;
1234
1235 case KDP_DUMPINFO:
1236 {
1237 const uint32_t count = packet.GetByteSize() - offset;
1238 s.Printf(" (count = %u, bytes = \n", count);
1239 if (count > 0)
1240 packet.Dump (&s, // Stream to dump to
1241 offset, // Offset within "packet"
1242 eFormatHex, // Format to use
1243 1, // Size of each item in bytes
1244 count, // Number of items
1245 16, // Number per line
1246 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1247 0, 0); // No bitfields
1248
1249 }
1250 break;
1251
1252 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001253 }
1254 }
1255 else
1256 {
1257 error_desc = "error: invalid packet command: ";
1258 }
1259 }
1260
1261 if (error_desc)
1262 {
1263 s.PutCString (error_desc);
1264
1265 packet.Dump (&s, // Stream to dump to
1266 0, // Offset into "packet"
1267 eFormatBytes, // Dump as hex bytes
1268 1, // Size of each item is 1 for single bytes
1269 packet.GetByteSize(), // Number of bytes
1270 UINT32_MAX, // Num bytes per line
1271 LLDB_INVALID_ADDRESS, // Base address
1272 0, 0); // Bitfield info set to not do anything bitfield related
1273 }
1274}
1275
1276uint32_t
1277CommunicationKDP::SendRequestReadRegisters (uint32_t cpu,
1278 uint32_t flavor,
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001279 void *dst,
Greg Claytona63d08c2011-07-19 03:57:15 +00001280 uint32_t dst_len,
1281 Error &error)
1282{
1283 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +00001284 const CommandType command = KDP_READREGS;
Greg Claytona63d08c2011-07-19 03:57:15 +00001285 // Size is header + 4 byte cpu and 4 byte flavor
1286 const uint32_t command_length = 8 + 4 + 4;
Greg Claytona63d08c2011-07-19 03:57:15 +00001287 MakeRequestPacketHeader (command, request_packet, command_length);
1288 request_packet.PutHex32 (cpu);
1289 request_packet.PutHex32 (flavor);
1290 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001291 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Claytona63d08c2011-07-19 03:57:15 +00001292 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001293 lldb::offset_t offset = 8;
Greg Claytona63d08c2011-07-19 03:57:15 +00001294 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1295 uint32_t src_len = reply_packet.GetByteSize() - 12;
1296
1297 if (src_len > 0)
1298 {
1299 const uint32_t bytes_to_copy = std::min<uint32_t>(src_len, dst_len);
1300 const void *src = reply_packet.GetData(&offset, bytes_to_copy);
1301 if (src)
1302 {
1303 ::memcpy (dst, src, bytes_to_copy);
1304 error.Clear();
1305 // Return the number of bytes we could have returned regardless if
1306 // we copied them or not, just so we know when things don't match up
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001307 return src_len;
Greg Claytona63d08c2011-07-19 03:57:15 +00001308 }
1309 }
1310 if (kdp_error)
1311 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error);
1312 else
1313 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u", cpu, flavor);
1314 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +00001315 else
1316 {
1317 error.SetErrorString ("failed to send packet");
1318 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001319 return 0;
1320}
1321
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001322uint32_t
1323CommunicationKDP::SendRequestWriteRegisters (uint32_t cpu,
1324 uint32_t flavor,
1325 const void *src,
1326 uint32_t src_len,
1327 Error &error)
1328{
1329 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
1330 const CommandType command = KDP_WRITEREGS;
1331 // Size is header + 4 byte cpu and 4 byte flavor
Greg Clayton97d5cf02012-09-25 02:40:06 +00001332 const uint32_t command_length = 8 + 4 + 4 + src_len;
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001333 MakeRequestPacketHeader (command, request_packet, command_length);
1334 request_packet.PutHex32 (cpu);
1335 request_packet.PutHex32 (flavor);
1336 request_packet.Write(src, src_len);
1337 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001338 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001339 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001340 lldb::offset_t offset = 8;
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001341 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1342 if (kdp_error == 0)
1343 return src_len;
1344 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error);
1345 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +00001346 else
1347 {
1348 error.SetErrorString ("failed to send packet");
1349 }
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001350 return 0;
1351}
1352
Greg Clayton07e66e32011-07-20 03:41:06 +00001353
1354bool
Greg Clayton97d5cf02012-09-25 02:40:06 +00001355CommunicationKDP::SendRequestResume ()
Greg Clayton07e66e32011-07-20 03:41:06 +00001356{
Greg Clayton07e66e32011-07-20 03:41:06 +00001357 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +00001358 const CommandType command = KDP_RESUMECPUS;
Greg Clayton07e66e32011-07-20 03:41:06 +00001359 const uint32_t command_length = 12;
Greg Clayton07e66e32011-07-20 03:41:06 +00001360 MakeRequestPacketHeader (command, request_packet, command_length);
Greg Clayton97d5cf02012-09-25 02:40:06 +00001361 request_packet.PutHex32(GetCPUMask());
Greg Clayton07e66e32011-07-20 03:41:06 +00001362
1363 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001364 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton07e66e32011-07-20 03:41:06 +00001365 return true;
1366 return false;
1367}
1368
1369bool
1370CommunicationKDP::SendRequestBreakpoint (bool set, addr_t addr)
1371{
1372 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
1373 bool use_64 = (GetVersion() >= 11);
1374 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
Greg Clayton5b882162011-07-21 01:12:01 +00001375 const CommandType command = set ? (use_64 ? KDP_BREAKPOINT_SET64 : KDP_BREAKPOINT_SET ):
1376 (use_64 ? KDP_BREAKPOINT_REMOVE64 : KDP_BREAKPOINT_REMOVE);
Greg Clayton07e66e32011-07-20 03:41:06 +00001377
1378 const uint32_t command_length = 8 + command_addr_byte_size;
Greg Clayton07e66e32011-07-20 03:41:06 +00001379 MakeRequestPacketHeader (command, request_packet, command_length);
1380 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
1381
1382 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001383 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton5b442372011-07-29 22:26:36 +00001384 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001385 lldb::offset_t offset = 8;
Greg Clayton5b442372011-07-29 22:26:36 +00001386 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1387 if (kdp_error == 0)
1388 return true;
1389 }
Greg Clayton07e66e32011-07-20 03:41:06 +00001390 return false;
1391}
1392
1393bool
1394CommunicationKDP::SendRequestSuspend ()
1395{
1396 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +00001397 const CommandType command = KDP_SUSPEND;
Greg Clayton07e66e32011-07-20 03:41:06 +00001398 const uint32_t command_length = 8;
Greg Clayton07e66e32011-07-20 03:41:06 +00001399 MakeRequestPacketHeader (command, request_packet, command_length);
1400 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001401 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton07e66e32011-07-20 03:41:06 +00001402 return true;
1403 return false;
1404}
1405