blob: 12c178017677f007049ef5c4180aa4a26814a652 [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),
Jason Molenda54d04f32013-07-02 01:29:59 +000045 m_packet_timeout (5),
Greg Claytonf9765ac2011-07-15 03:27:12 +000046 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 Molendaca2ffa72013-05-09 23:52:21 +0000547bool
548CommunicationKDP::RemoteIsDarwinKernel ()
549{
550 if (GetKernelVersion() == NULL)
551 return false;
552 if (m_kernel_version.find("Darwin Kernel") != std::string::npos)
553 return true;
554 else
555 return false;
556}
557
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000558lldb::addr_t
559CommunicationKDP::GetLoadAddress ()
560{
561 if (GetKernelVersion() == NULL)
562 return LLDB_INVALID_ADDRESS;
563
564 if (m_kernel_version.find("stext=") == std::string::npos)
565 return LLDB_INVALID_ADDRESS;
566 size_t p = m_kernel_version.find("stext=") + strlen ("stext=");
567 if (m_kernel_version[p] != '0' || m_kernel_version[p + 1] != 'x')
568 return LLDB_INVALID_ADDRESS;
569
570 addr_t kernel_load_address;
571 errno = 0;
572 kernel_load_address = ::strtoul (m_kernel_version.c_str() + p, NULL, 16);
573 if (errno != 0 || kernel_load_address == 0)
574 return LLDB_INVALID_ADDRESS;
575
576 return kernel_load_address;
577}
578
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000579bool
580CommunicationKDP::SendRequestHostInfo ()
581{
Greg Claytona63d08c2011-07-19 03:57:15 +0000582 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000583 const CommandType command = KDP_HOSTINFO;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000584 const uint32_t command_length = 8;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000585 MakeRequestPacketHeader (command, request_packet, command_length);
586 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000587 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000588 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000589 lldb::offset_t offset = 8;
Greg Claytona63d08c2011-07-19 03:57:15 +0000590 m_kdp_hostinfo_cpu_mask = reply_packet.GetU32 (&offset);
591 m_kdp_hostinfo_cpu_type = reply_packet.GetU32 (&offset);
592 m_kdp_hostinfo_cpu_subtype = reply_packet.GetU32 (&offset);
593
594 ArchSpec kernel_arch;
595 kernel_arch.SetArchitecture (eArchTypeMachO,
596 m_kdp_hostinfo_cpu_type,
597 m_kdp_hostinfo_cpu_subtype);
598
599 m_addr_byte_size = kernel_arch.GetAddressByteSize();
600 m_byte_order = kernel_arch.GetByteOrder();
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000601 return true;
602 }
603 return false;
604}
605
Greg Clayton07e66e32011-07-20 03:41:06 +0000606const char *
607CommunicationKDP::GetKernelVersion ()
608{
609 if (m_kernel_version.empty())
610 SendRequestKernelVersion ();
611 return m_kernel_version.c_str();
612}
613
614bool
615CommunicationKDP::SendRequestKernelVersion ()
616{
617 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000618 const CommandType command = KDP_KERNELVERSION;
Greg Clayton07e66e32011-07-20 03:41:06 +0000619 const uint32_t command_length = 8;
Greg Clayton07e66e32011-07-20 03:41:06 +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 Clayton07e66e32011-07-20 03:41:06 +0000623 {
624 const char *kernel_version_cstr = reply_packet.PeekCStr(8);
625 if (kernel_version_cstr && kernel_version_cstr[0])
626 m_kernel_version.assign (kernel_version_cstr);
627 return true;
628 }
629 return false;
630}
631
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000632bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000633CommunicationKDP::SendRequestDisconnect ()
Greg Clayton57508022011-07-15 16:31:38 +0000634{
Greg Claytona63d08c2011-07-19 03:57:15 +0000635 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000636 const CommandType command = KDP_DISCONNECT;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000637 const uint32_t command_length = 8;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000638 MakeRequestPacketHeader (command, request_packet, command_length);
639 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000640 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000641 {
642 // Are we supposed to get a reply for disconnect?
643 }
644 ClearKDPSettings ();
645 return true;
Greg Clayton57508022011-07-15 16:31:38 +0000646}
647
Greg Claytona63d08c2011-07-19 03:57:15 +0000648uint32_t
649CommunicationKDP::SendRequestReadMemory (lldb::addr_t addr,
650 void *dst,
651 uint32_t dst_len,
652 Error &error)
653{
654 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
655 bool use_64 = (GetVersion() >= 11);
656 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
Greg Clayton5b882162011-07-21 01:12:01 +0000657 const CommandType command = use_64 ? KDP_READMEM64 : KDP_READMEM;
Greg Claytona63d08c2011-07-19 03:57:15 +0000658 // Size is header + address size + uint32_t length
659 const uint32_t command_length = 8 + command_addr_byte_size + 4;
Greg Claytona63d08c2011-07-19 03:57:15 +0000660 MakeRequestPacketHeader (command, request_packet, command_length);
661 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
662 request_packet.PutHex32 (dst_len);
663 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000664 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Claytona63d08c2011-07-19 03:57:15 +0000665 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000666 lldb::offset_t offset = 8;
Greg Claytona63d08c2011-07-19 03:57:15 +0000667 uint32_t kdp_error = reply_packet.GetU32 (&offset);
668 uint32_t src_len = reply_packet.GetByteSize() - 12;
669
670 if (src_len > 0)
671 {
672 const void *src = reply_packet.GetData(&offset, src_len);
673 if (src)
674 {
675 ::memcpy (dst, src, src_len);
676 error.Clear();
677 return src_len;
678 }
679 }
680 if (kdp_error)
681 error.SetErrorStringWithFormat ("kdp read memory failed (error %u)", kdp_error);
682 else
683 error.SetErrorString ("kdp read memory failed");
684 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000685 else
686 {
687 error.SetErrorString ("failed to send packet");
688 }
Greg Claytona63d08c2011-07-19 03:57:15 +0000689 return 0;
690}
691
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000692
693uint32_t
694CommunicationKDP::SendRequestWriteMemory (lldb::addr_t addr,
695 const void *src,
696 uint32_t src_len,
697 Error &error)
698{
699 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
700 bool use_64 = (GetVersion() >= 11);
701 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
Greg Clayton5b882162011-07-21 01:12:01 +0000702 const CommandType command = use_64 ? KDP_WRITEMEM64 : KDP_WRITEMEM;
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000703 // Size is header + address size + uint32_t length
Greg Claytonb556c9b2012-10-12 23:24:05 +0000704 const uint32_t command_length = 8 + command_addr_byte_size + 4 + src_len;
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000705 MakeRequestPacketHeader (command, request_packet, command_length);
706 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
707 request_packet.PutHex32 (src_len);
Jason Molenda57656e72012-10-19 02:16:22 +0000708 request_packet.PutRawBytes(src, src_len);
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000709
710 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000711 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000712 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000713 lldb::offset_t offset = 8;
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000714 uint32_t kdp_error = reply_packet.GetU32 (&offset);
715 if (kdp_error)
716 error.SetErrorStringWithFormat ("kdp write memory failed (error %u)", kdp_error);
717 else
718 {
719 error.Clear();
720 return src_len;
721 }
722 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000723 else
724 {
725 error.SetErrorString ("failed to send packet");
726 }
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000727 return 0;
728}
729
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000730bool
731CommunicationKDP::SendRawRequest (uint8_t command_byte,
732 const void *src, // Raw packet payload bytes
733 uint32_t src_len, // Raw packet payload length
734 DataExtractor &reply_packet,
735 Error &error)
736{
737 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
738 // Size is header + address size + uint32_t length
739 const uint32_t command_length = 8 + src_len;
740 const CommandType command = (CommandType)command_byte;
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000741 MakeRequestPacketHeader (command, request_packet, command_length);
742 request_packet.PutRawBytes(src, src_len);
743
Greg Clayton0ee809b2013-02-14 19:11:23 +0000744 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000745 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000746 lldb::offset_t offset = 8;
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000747 uint32_t kdp_error = reply_packet.GetU32 (&offset);
Jason Molendace62fd72013-03-01 00:43:19 +0000748 if (kdp_error && (command_byte != KDP_DUMPINFO))
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000749 error.SetErrorStringWithFormat ("request packet 0x%8.8x failed (error %u)", command_byte, kdp_error);
750 else
751 {
752 error.Clear();
753 return true;
754 }
755 }
756 else
757 {
758 error.SetErrorString ("failed to send packet");
759 }
760 return false;
761}
762
763
Greg Claytona63d08c2011-07-19 03:57:15 +0000764const char *
765CommunicationKDP::GetCommandAsCString (uint8_t command)
766{
767 switch (command)
768 {
Greg Clayton5b882162011-07-21 01:12:01 +0000769 case KDP_CONNECT: return "KDP_CONNECT";
770 case KDP_DISCONNECT: return "KDP_DISCONNECT";
771 case KDP_HOSTINFO: return "KDP_HOSTINFO";
772 case KDP_VERSION: return "KDP_VERSION";
773 case KDP_MAXBYTES: return "KDP_MAXBYTES";
774 case KDP_READMEM: return "KDP_READMEM";
775 case KDP_WRITEMEM: return "KDP_WRITEMEM";
776 case KDP_READREGS: return "KDP_READREGS";
777 case KDP_WRITEREGS: return "KDP_WRITEREGS";
778 case KDP_LOAD: return "KDP_LOAD";
779 case KDP_IMAGEPATH: return "KDP_IMAGEPATH";
780 case KDP_SUSPEND: return "KDP_SUSPEND";
781 case KDP_RESUMECPUS: return "KDP_RESUMECPUS";
782 case KDP_EXCEPTION: return "KDP_EXCEPTION";
783 case KDP_TERMINATION: return "KDP_TERMINATION";
784 case KDP_BREAKPOINT_SET: return "KDP_BREAKPOINT_SET";
785 case KDP_BREAKPOINT_REMOVE: return "KDP_BREAKPOINT_REMOVE";
786 case KDP_REGIONS: return "KDP_REGIONS";
787 case KDP_REATTACH: return "KDP_REATTACH";
788 case KDP_HOSTREBOOT: return "KDP_HOSTREBOOT";
789 case KDP_READMEM64: return "KDP_READMEM64";
790 case KDP_WRITEMEM64: return "KDP_WRITEMEM64";
791 case KDP_BREAKPOINT_SET64: return "KDP_BREAKPOINT64_SET";
792 case KDP_BREAKPOINT_REMOVE64: return "KDP_BREAKPOINT64_REMOVE";
793 case KDP_KERNELVERSION: return "KDP_KERNELVERSION";
Jason Molendace62fd72013-03-01 00:43:19 +0000794 case KDP_READPHYSMEM64: return "KDP_READPHYSMEM64";
795 case KDP_WRITEPHYSMEM64: return "KDP_WRITEPHYSMEM64";
796 case KDP_READIOPORT: return "KDP_READIOPORT";
797 case KDP_WRITEIOPORT: return "KDP_WRITEIOPORT";
798 case KDP_READMSR64: return "KDP_READMSR64";
799 case KDP_WRITEMSR64: return "KDP_WRITEMSR64";
800 case KDP_DUMPINFO: return "KDP_DUMPINFO";
Greg Claytona63d08c2011-07-19 03:57:15 +0000801 }
802 return NULL;
803}
804
805void
806CommunicationKDP::DumpPacket (Stream &s, const void *data, uint32_t data_len)
807{
808 DataExtractor extractor (data, data_len, m_byte_order, m_addr_byte_size);
809 DumpPacket (s, extractor);
810}
811
812void
813CommunicationKDP::DumpPacket (Stream &s, const DataExtractor& packet)
814{
815 const char *error_desc = NULL;
816 if (packet.GetByteSize() < 8)
817 {
818 error_desc = "error: invalid packet (too short): ";
819 }
820 else
821 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000822 lldb::offset_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000823 const uint8_t first_packet_byte = packet.GetU8 (&offset);
824 const uint8_t sequence_id = packet.GetU8 (&offset);
825 const uint16_t length = packet.GetU16 (&offset);
826 const uint32_t key = packet.GetU32 (&offset);
827 const CommandType command = ExtractCommand (first_packet_byte);
828 const char *command_name = GetCommandAsCString (command);
829 if (command_name)
830 {
831 const bool is_reply = ExtractIsReply(first_packet_byte);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000832 s.Printf ("(running=%i) %s %24s: 0x%2.2x 0x%2.2x 0x%4.4x 0x%8.8x ",
833 IsRunning(),
834 is_reply ? "<--" : "-->",
835 command_name,
836 first_packet_byte,
Greg Claytona63d08c2011-07-19 03:57:15 +0000837 sequence_id,
838 length,
Greg Clayton97d5cf02012-09-25 02:40:06 +0000839 key);
Greg Claytona63d08c2011-07-19 03:57:15 +0000840
841 if (is_reply)
842 {
843 // Dump request reply packets
844 switch (command)
845 {
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000846 // Commands that return a single 32 bit error
Greg Clayton5b882162011-07-21 01:12:01 +0000847 case KDP_CONNECT:
848 case KDP_WRITEMEM:
849 case KDP_WRITEMEM64:
850 case KDP_BREAKPOINT_SET:
851 case KDP_BREAKPOINT_REMOVE:
852 case KDP_BREAKPOINT_SET64:
853 case KDP_BREAKPOINT_REMOVE64:
854 case KDP_WRITEREGS:
855 case KDP_LOAD:
Jason Molendace62fd72013-03-01 00:43:19 +0000856 case KDP_WRITEIOPORT:
857 case KDP_WRITEMSR64:
Greg Claytona63d08c2011-07-19 03:57:15 +0000858 {
859 const uint32_t error = packet.GetU32 (&offset);
860 s.Printf(" (error=0x%8.8x)", error);
861 }
862 break;
863
Greg Clayton5b882162011-07-21 01:12:01 +0000864 case KDP_DISCONNECT:
865 case KDP_REATTACH:
866 case KDP_HOSTREBOOT:
867 case KDP_SUSPEND:
868 case KDP_RESUMECPUS:
869 case KDP_EXCEPTION:
870 case KDP_TERMINATION:
Greg Claytona63d08c2011-07-19 03:57:15 +0000871 // No return value for the reply, just the header to ack
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000872 s.PutCString(" ()");
Greg Claytona63d08c2011-07-19 03:57:15 +0000873 break;
874
Greg Clayton5b882162011-07-21 01:12:01 +0000875 case KDP_HOSTINFO:
Greg Claytona63d08c2011-07-19 03:57:15 +0000876 {
877 const uint32_t cpu_mask = packet.GetU32 (&offset);
878 const uint32_t cpu_type = packet.GetU32 (&offset);
879 const uint32_t cpu_subtype = packet.GetU32 (&offset);
880 s.Printf(" (cpu_mask=0x%8.8x, cpu_type=0x%8.8x, cpu_subtype=0x%8.8x)", cpu_mask, cpu_type, cpu_subtype);
881 }
882 break;
883
Greg Clayton5b882162011-07-21 01:12:01 +0000884 case KDP_VERSION:
Greg Claytona63d08c2011-07-19 03:57:15 +0000885 {
886 const uint32_t version = packet.GetU32 (&offset);
887 const uint32_t feature = packet.GetU32 (&offset);
888 s.Printf(" (version=0x%8.8x, feature=0x%8.8x)", version, feature);
889 }
890 break;
891
Greg Clayton5b882162011-07-21 01:12:01 +0000892 case KDP_REGIONS:
Greg Claytona63d08c2011-07-19 03:57:15 +0000893 {
894 const uint32_t region_count = packet.GetU32 (&offset);
895 s.Printf(" (count = %u", region_count);
896 for (uint32_t i=0; i<region_count; ++i)
897 {
898 const addr_t region_addr = packet.GetPointer (&offset);
899 const uint32_t region_size = packet.GetU32 (&offset);
900 const uint32_t region_prot = packet.GetU32 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +0000901 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 +0000902 }
903 }
904 break;
905
Greg Clayton5b882162011-07-21 01:12:01 +0000906 case KDP_READMEM:
907 case KDP_READMEM64:
Jason Molendace62fd72013-03-01 00:43:19 +0000908 case KDP_READPHYSMEM64:
Greg Claytona63d08c2011-07-19 03:57:15 +0000909 {
910 const uint32_t error = packet.GetU32 (&offset);
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000911 const uint32_t count = packet.GetByteSize() - offset;
Greg Clayton5b442372011-07-29 22:26:36 +0000912 s.Printf(" (error = 0x%8.8x:\n", error);
Greg Claytona63d08c2011-07-19 03:57:15 +0000913 if (count > 0)
Greg Clayton07e66e32011-07-20 03:41:06 +0000914 packet.Dump (&s, // Stream to dump to
915 offset, // Offset within "packet"
916 eFormatBytesWithASCII, // Format to use
917 1, // Size of each item in bytes
918 count, // Number of items
919 16, // Number per line
920 m_last_read_memory_addr, // Don't show addresses before each line
921 0, 0); // No bitfields
Greg Claytona63d08c2011-07-19 03:57:15 +0000922 }
923 break;
924
Greg Clayton5b882162011-07-21 01:12:01 +0000925 case KDP_READREGS:
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000926 {
927 const uint32_t error = packet.GetU32 (&offset);
928 const uint32_t count = packet.GetByteSize() - offset;
Greg Clayton5b442372011-07-29 22:26:36 +0000929 s.Printf(" (error = 0x%8.8x regs:\n", error);
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000930 if (count > 0)
931 packet.Dump (&s, // Stream to dump to
932 offset, // Offset within "packet"
933 eFormatHex, // Format to use
934 m_addr_byte_size, // Size of each item in bytes
935 count / m_addr_byte_size, // Number of items
936 16 / m_addr_byte_size, // Number per line
937 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
938 0, 0); // No bitfields
939 }
940 break;
941
Greg Clayton5b882162011-07-21 01:12:01 +0000942 case KDP_KERNELVERSION:
Greg Clayton07e66e32011-07-20 03:41:06 +0000943 {
944 const char *kernel_version = packet.PeekCStr(8);
945 s.Printf(" (version = \"%s\")", kernel_version);
946 }
947 break;
948
Greg Clayton5b882162011-07-21 01:12:01 +0000949 case KDP_MAXBYTES:
Greg Clayton07e66e32011-07-20 03:41:06 +0000950 {
951 const uint32_t max_bytes = packet.GetU32 (&offset);
952 s.Printf(" (max_bytes = 0x%8.8x (%u))", max_bytes, max_bytes);
953 }
954 break;
Greg Clayton5b882162011-07-21 01:12:01 +0000955 case KDP_IMAGEPATH:
Greg Clayton07e66e32011-07-20 03:41:06 +0000956 {
957 const char *path = packet.GetCStr(&offset);
958 s.Printf(" (path = \"%s\")", path);
959 }
960 break;
Jason Molendace62fd72013-03-01 00:43:19 +0000961
962 case KDP_READIOPORT:
963 case KDP_READMSR64:
964 {
965 const uint32_t error = packet.GetU32 (&offset);
966 const uint32_t count = packet.GetByteSize() - offset;
967 s.Printf(" (error = 0x%8.8x io:\n", error);
968 if (count > 0)
969 packet.Dump (&s, // Stream to dump to
970 offset, // Offset within "packet"
971 eFormatHex, // Format to use
972 1, // Size of each item in bytes
973 count, // Number of items
974 16, // Number per line
975 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
976 0, 0); // No bitfields
977 }
978 break;
979 case KDP_DUMPINFO:
980 {
981 const uint32_t count = packet.GetByteSize() - offset;
982 s.Printf(" (count = %u, bytes = \n", count);
983 if (count > 0)
984 packet.Dump (&s, // Stream to dump to
985 offset, // Offset within "packet"
986 eFormatHex, // Format to use
987 1, // Size of each item in bytes
988 count, // Number of items
989 16, // Number per line
990 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
991 0, 0); // No bitfields
992
993 }
994 break;
995
Greg Clayton07e66e32011-07-20 03:41:06 +0000996 default:
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000997 s.Printf(" (add support for dumping this packet reply!!!");
Greg Claytona63d08c2011-07-19 03:57:15 +0000998 break;
999
1000 }
1001 }
1002 else
1003 {
1004 // Dump request packets
1005 switch (command)
1006 {
Greg Clayton5b882162011-07-21 01:12:01 +00001007 case KDP_CONNECT:
Greg Claytona63d08c2011-07-19 03:57:15 +00001008 {
1009 const uint16_t reply_port = packet.GetU16 (&offset);
1010 const uint16_t exc_port = packet.GetU16 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +00001011 s.Printf(" (reply_port = %u, exc_port = %u, greeting = \"%s\")", reply_port, exc_port, packet.GetCStr(&offset));
Greg Claytona63d08c2011-07-19 03:57:15 +00001012 }
1013 break;
1014
Greg Clayton5b882162011-07-21 01:12:01 +00001015 case KDP_DISCONNECT:
1016 case KDP_HOSTREBOOT:
1017 case KDP_HOSTINFO:
1018 case KDP_VERSION:
1019 case KDP_REGIONS:
1020 case KDP_KERNELVERSION:
1021 case KDP_MAXBYTES:
1022 case KDP_IMAGEPATH:
1023 case KDP_SUSPEND:
Greg Claytona63d08c2011-07-19 03:57:15 +00001024 // No args, just the header in the request...
Greg Clayton07e66e32011-07-20 03:41:06 +00001025 s.PutCString(" ()");
1026 break;
1027
Greg Clayton5b882162011-07-21 01:12:01 +00001028 case KDP_RESUMECPUS:
Greg Clayton07e66e32011-07-20 03:41:06 +00001029 {
1030 const uint32_t cpu_mask = packet.GetU32 (&offset);
1031 s.Printf(" (cpu_mask = 0x%8.8x)", cpu_mask);
1032 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001033 break;
1034
Greg Clayton5b882162011-07-21 01:12:01 +00001035 case KDP_READMEM:
Greg Claytona63d08c2011-07-19 03:57:15 +00001036 {
1037 const uint32_t addr = packet.GetU32 (&offset);
1038 const uint32_t size = packet.GetU32 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +00001039 s.Printf(" (addr = 0x%8.8x, size = %u)", addr, size);
Greg Clayton07e66e32011-07-20 03:41:06 +00001040 m_last_read_memory_addr = addr;
Greg Claytona63d08c2011-07-19 03:57:15 +00001041 }
1042 break;
1043
Greg Clayton5b882162011-07-21 01:12:01 +00001044 case KDP_WRITEMEM:
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001045 {
1046 const uint32_t addr = packet.GetU32 (&offset);
1047 const uint32_t size = packet.GetU32 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +00001048 s.Printf(" (addr = 0x%8.8x, size = %u, bytes = \n", addr, size);
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001049 if (size > 0)
1050 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
1051 }
1052 break;
1053
Greg Clayton5b882162011-07-21 01:12:01 +00001054 case KDP_READMEM64:
Greg Claytona63d08c2011-07-19 03:57:15 +00001055 {
1056 const uint64_t addr = packet.GetU64 (&offset);
1057 const uint32_t size = packet.GetU32 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +00001058 s.Printf(" (addr = 0x%16.16" PRIx64 ", size = %u)", addr, size);
Greg Clayton07e66e32011-07-20 03:41:06 +00001059 m_last_read_memory_addr = addr;
Greg Claytona63d08c2011-07-19 03:57:15 +00001060 }
1061 break;
1062
Jason Molendace62fd72013-03-01 00:43:19 +00001063 case KDP_READPHYSMEM64:
1064 {
1065 const uint64_t addr = packet.GetU64 (&offset);
1066 const uint32_t size = packet.GetU32 (&offset);
1067 const uint32_t lcpu = packet.GetU16 (&offset);
1068 s.Printf(" (addr = 0x%16.16llx, size = %u, lcpu = %u)", addr, size, lcpu);
1069 m_last_read_memory_addr = addr;
1070 }
1071 break;
1072
Greg Clayton5b882162011-07-21 01:12:01 +00001073 case KDP_WRITEMEM64:
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001074 {
1075 const uint64_t addr = packet.GetU64 (&offset);
1076 const uint32_t size = packet.GetU32 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +00001077 s.Printf(" (addr = 0x%16.16" PRIx64 ", size = %u, bytes = \n", addr, size);
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001078 if (size > 0)
1079 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
1080 }
1081 break;
1082
Jason Molendace62fd72013-03-01 00:43:19 +00001083 case KDP_WRITEPHYSMEM64:
1084 {
1085 const uint64_t addr = packet.GetU64 (&offset);
1086 const uint32_t size = packet.GetU32 (&offset);
1087 const uint32_t lcpu = packet.GetU16 (&offset);
1088 s.Printf(" (addr = 0x%16.16llx, size = %u, lcpu = %u, bytes = \n", addr, size, lcpu);
1089 if (size > 0)
1090 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
1091 }
1092 break;
1093
Greg Clayton5b882162011-07-21 01:12:01 +00001094 case KDP_READREGS:
Greg Claytona63d08c2011-07-19 03:57:15 +00001095 {
1096 const uint32_t cpu = packet.GetU32 (&offset);
1097 const uint32_t flavor = packet.GetU32 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +00001098 s.Printf(" (cpu = %u, flavor = %u)", cpu, flavor);
Greg Claytona63d08c2011-07-19 03:57:15 +00001099 }
1100 break;
1101
Greg Clayton5b882162011-07-21 01:12:01 +00001102 case KDP_WRITEREGS:
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001103 {
1104 const uint32_t cpu = packet.GetU32 (&offset);
1105 const uint32_t flavor = packet.GetU32 (&offset);
1106 const uint32_t nbytes = packet.GetByteSize() - offset;
Greg Clayton5b442372011-07-29 22:26:36 +00001107 s.Printf(" (cpu = %u, flavor = %u, regs = \n", cpu, flavor);
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001108 if (nbytes > 0)
1109 packet.Dump (&s, // Stream to dump to
1110 offset, // Offset within "packet"
1111 eFormatHex, // Format to use
1112 m_addr_byte_size, // Size of each item in bytes
1113 nbytes / m_addr_byte_size, // Number of items
1114 16 / m_addr_byte_size, // Number per line
1115 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1116 0, 0); // No bitfields
1117 }
1118 break;
1119
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001120
Greg Clayton5b882162011-07-21 01:12:01 +00001121 case KDP_BREAKPOINT_SET:
1122 case KDP_BREAKPOINT_REMOVE:
Greg Clayton07e66e32011-07-20 03:41:06 +00001123 {
1124 const uint32_t addr = packet.GetU32 (&offset);
1125 s.Printf(" (addr = 0x%8.8x)", addr);
1126 }
1127 break;
1128
Greg Clayton5b882162011-07-21 01:12:01 +00001129 case KDP_BREAKPOINT_SET64:
1130 case KDP_BREAKPOINT_REMOVE64:
Greg Clayton07e66e32011-07-20 03:41:06 +00001131 {
1132 const uint64_t addr = packet.GetU64 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +00001133 s.Printf(" (addr = 0x%16.16" PRIx64 ")", addr);
Greg Clayton07e66e32011-07-20 03:41:06 +00001134 }
1135 break;
1136
1137
Greg Clayton5b882162011-07-21 01:12:01 +00001138 case KDP_LOAD:
Greg Clayton07e66e32011-07-20 03:41:06 +00001139 {
1140 const char *path = packet.GetCStr(&offset);
1141 s.Printf(" (path = \"%s\")", path);
1142 }
1143 break;
1144
Greg Clayton5b882162011-07-21 01:12:01 +00001145 case KDP_EXCEPTION:
Greg Clayton07e66e32011-07-20 03:41:06 +00001146 {
1147 const uint32_t count = packet.GetU32 (&offset);
1148
Greg Clayton07e66e32011-07-20 03:41:06 +00001149 for (uint32_t i=0; i<count; ++i)
1150 {
1151 const uint32_t cpu = packet.GetU32 (&offset);
1152 const uint32_t exc = packet.GetU32 (&offset);
1153 const uint32_t code = packet.GetU32 (&offset);
1154 const uint32_t subcode = packet.GetU32 (&offset);
1155 const char *exc_cstr = NULL;
1156 switch (exc)
1157 {
1158 case 1: exc_cstr = "EXC_BAD_ACCESS"; break;
1159 case 2: exc_cstr = "EXC_BAD_INSTRUCTION"; break;
1160 case 3: exc_cstr = "EXC_ARITHMETIC"; break;
1161 case 4: exc_cstr = "EXC_EMULATION"; break;
1162 case 5: exc_cstr = "EXC_SOFTWARE"; break;
1163 case 6: exc_cstr = "EXC_BREAKPOINT"; break;
1164 case 7: exc_cstr = "EXC_SYSCALL"; break;
1165 case 8: exc_cstr = "EXC_MACH_SYSCALL"; break;
1166 case 9: exc_cstr = "EXC_RPC_ALERT"; break;
1167 case 10: exc_cstr = "EXC_CRASH"; break;
1168 default:
1169 break;
1170 }
1171
Greg Clayton97d5cf02012-09-25 02:40:06 +00001172 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 +00001173 cpu, exc_cstr, exc, code, code, subcode, subcode);
1174 }
1175 }
1176 break;
1177
Greg Clayton5b882162011-07-21 01:12:01 +00001178 case KDP_TERMINATION:
Greg Clayton07e66e32011-07-20 03:41:06 +00001179 {
1180 const uint32_t term_code = packet.GetU32 (&offset);
1181 const uint32_t exit_code = packet.GetU32 (&offset);
1182 s.Printf(" (term_code = 0x%8.8x (%u), exit_code = 0x%8.8x (%u))", term_code, term_code, exit_code, exit_code);
1183 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001184 break;
1185
Greg Clayton5b882162011-07-21 01:12:01 +00001186 case KDP_REATTACH:
Greg Claytona63d08c2011-07-19 03:57:15 +00001187 {
1188 const uint16_t reply_port = packet.GetU16 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +00001189 s.Printf(" (reply_port = %u)", reply_port);
Greg Claytona63d08c2011-07-19 03:57:15 +00001190 }
1191 break;
Jason Molendace62fd72013-03-01 00:43:19 +00001192
1193 case KDP_READMSR64:
1194 {
1195 const uint32_t address = packet.GetU32 (&offset);
1196 const uint16_t lcpu = packet.GetU16 (&offset);
1197 s.Printf(" (address=0x%8.8x, lcpu=0x%4.4x)", address, lcpu);
1198 }
1199 break;
1200
1201 case KDP_WRITEMSR64:
1202 {
1203 const uint32_t address = packet.GetU32 (&offset);
1204 const uint16_t lcpu = packet.GetU16 (&offset);
1205 const uint32_t nbytes = packet.GetByteSize() - offset;
1206 s.Printf(" (address=0x%8.8x, lcpu=0x%4.4x, nbytes=0x%8.8x)", lcpu, address, nbytes);
1207 if (nbytes > 0)
1208 packet.Dump (&s, // Stream to dump to
1209 offset, // Offset within "packet"
1210 eFormatHex, // Format to use
1211 1, // Size of each item in bytes
1212 nbytes, // Number of items
1213 16, // Number per line
1214 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1215 0, 0); // No bitfields
1216 }
1217 break;
1218
1219 case KDP_READIOPORT:
1220 {
1221 const uint16_t lcpu = packet.GetU16 (&offset);
1222 const uint16_t address = packet.GetU16 (&offset);
1223 const uint16_t nbytes = packet.GetU16 (&offset);
1224 s.Printf(" (lcpu=0x%4.4x, address=0x%4.4x, nbytes=%u)", lcpu, address, nbytes);
1225 }
1226 break;
1227
1228 case KDP_WRITEIOPORT:
1229 {
1230 const uint16_t lcpu = packet.GetU16 (&offset);
1231 const uint16_t address = packet.GetU16 (&offset);
1232 const uint16_t nbytes = packet.GetU16 (&offset);
1233 s.Printf(" (lcpu = %u, addr = 0x%4.4x, nbytes = %u, bytes = \n", lcpu, address, nbytes);
1234 if (nbytes > 0)
1235 packet.Dump (&s, // Stream to dump to
1236 offset, // Offset within "packet"
1237 eFormatHex, // Format to use
1238 1, // Size of each item in bytes
1239 nbytes, // Number of items
1240 16, // Number per line
1241 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1242 0, 0); // No bitfields
1243 }
1244 break;
1245
1246 case KDP_DUMPINFO:
1247 {
1248 const uint32_t count = packet.GetByteSize() - offset;
1249 s.Printf(" (count = %u, bytes = \n", count);
1250 if (count > 0)
1251 packet.Dump (&s, // Stream to dump to
1252 offset, // Offset within "packet"
1253 eFormatHex, // Format to use
1254 1, // Size of each item in bytes
1255 count, // Number of items
1256 16, // Number per line
1257 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1258 0, 0); // No bitfields
1259
1260 }
1261 break;
1262
1263 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001264 }
1265 }
1266 else
1267 {
1268 error_desc = "error: invalid packet command: ";
1269 }
1270 }
1271
1272 if (error_desc)
1273 {
1274 s.PutCString (error_desc);
1275
1276 packet.Dump (&s, // Stream to dump to
1277 0, // Offset into "packet"
1278 eFormatBytes, // Dump as hex bytes
1279 1, // Size of each item is 1 for single bytes
1280 packet.GetByteSize(), // Number of bytes
1281 UINT32_MAX, // Num bytes per line
1282 LLDB_INVALID_ADDRESS, // Base address
1283 0, 0); // Bitfield info set to not do anything bitfield related
1284 }
1285}
1286
1287uint32_t
1288CommunicationKDP::SendRequestReadRegisters (uint32_t cpu,
1289 uint32_t flavor,
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001290 void *dst,
Greg Claytona63d08c2011-07-19 03:57:15 +00001291 uint32_t dst_len,
1292 Error &error)
1293{
1294 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +00001295 const CommandType command = KDP_READREGS;
Greg Claytona63d08c2011-07-19 03:57:15 +00001296 // Size is header + 4 byte cpu and 4 byte flavor
1297 const uint32_t command_length = 8 + 4 + 4;
Greg Claytona63d08c2011-07-19 03:57:15 +00001298 MakeRequestPacketHeader (command, request_packet, command_length);
1299 request_packet.PutHex32 (cpu);
1300 request_packet.PutHex32 (flavor);
1301 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001302 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Claytona63d08c2011-07-19 03:57:15 +00001303 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001304 lldb::offset_t offset = 8;
Greg Claytona63d08c2011-07-19 03:57:15 +00001305 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1306 uint32_t src_len = reply_packet.GetByteSize() - 12;
1307
1308 if (src_len > 0)
1309 {
1310 const uint32_t bytes_to_copy = std::min<uint32_t>(src_len, dst_len);
1311 const void *src = reply_packet.GetData(&offset, bytes_to_copy);
1312 if (src)
1313 {
1314 ::memcpy (dst, src, bytes_to_copy);
1315 error.Clear();
1316 // Return the number of bytes we could have returned regardless if
1317 // we copied them or not, just so we know when things don't match up
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001318 return src_len;
Greg Claytona63d08c2011-07-19 03:57:15 +00001319 }
1320 }
1321 if (kdp_error)
1322 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error);
1323 else
1324 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u", cpu, flavor);
1325 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +00001326 else
1327 {
1328 error.SetErrorString ("failed to send packet");
1329 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001330 return 0;
1331}
1332
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001333uint32_t
1334CommunicationKDP::SendRequestWriteRegisters (uint32_t cpu,
1335 uint32_t flavor,
1336 const void *src,
1337 uint32_t src_len,
1338 Error &error)
1339{
1340 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
1341 const CommandType command = KDP_WRITEREGS;
1342 // Size is header + 4 byte cpu and 4 byte flavor
Greg Clayton97d5cf02012-09-25 02:40:06 +00001343 const uint32_t command_length = 8 + 4 + 4 + src_len;
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001344 MakeRequestPacketHeader (command, request_packet, command_length);
1345 request_packet.PutHex32 (cpu);
1346 request_packet.PutHex32 (flavor);
1347 request_packet.Write(src, src_len);
1348 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001349 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001350 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001351 lldb::offset_t offset = 8;
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001352 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1353 if (kdp_error == 0)
1354 return src_len;
1355 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error);
1356 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +00001357 else
1358 {
1359 error.SetErrorString ("failed to send packet");
1360 }
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001361 return 0;
1362}
1363
Greg Clayton07e66e32011-07-20 03:41:06 +00001364
1365bool
Greg Clayton97d5cf02012-09-25 02:40:06 +00001366CommunicationKDP::SendRequestResume ()
Greg Clayton07e66e32011-07-20 03:41:06 +00001367{
Greg Clayton07e66e32011-07-20 03:41:06 +00001368 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +00001369 const CommandType command = KDP_RESUMECPUS;
Greg Clayton07e66e32011-07-20 03:41:06 +00001370 const uint32_t command_length = 12;
Greg Clayton07e66e32011-07-20 03:41:06 +00001371 MakeRequestPacketHeader (command, request_packet, command_length);
Greg Clayton97d5cf02012-09-25 02:40:06 +00001372 request_packet.PutHex32(GetCPUMask());
Greg Clayton07e66e32011-07-20 03:41:06 +00001373
1374 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001375 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton07e66e32011-07-20 03:41:06 +00001376 return true;
1377 return false;
1378}
1379
1380bool
1381CommunicationKDP::SendRequestBreakpoint (bool set, addr_t addr)
1382{
1383 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
1384 bool use_64 = (GetVersion() >= 11);
1385 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
Greg Clayton5b882162011-07-21 01:12:01 +00001386 const CommandType command = set ? (use_64 ? KDP_BREAKPOINT_SET64 : KDP_BREAKPOINT_SET ):
1387 (use_64 ? KDP_BREAKPOINT_REMOVE64 : KDP_BREAKPOINT_REMOVE);
Greg Clayton07e66e32011-07-20 03:41:06 +00001388
1389 const uint32_t command_length = 8 + command_addr_byte_size;
Greg Clayton07e66e32011-07-20 03:41:06 +00001390 MakeRequestPacketHeader (command, request_packet, command_length);
1391 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
1392
1393 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001394 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton5b442372011-07-29 22:26:36 +00001395 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001396 lldb::offset_t offset = 8;
Greg Clayton5b442372011-07-29 22:26:36 +00001397 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1398 if (kdp_error == 0)
1399 return true;
1400 }
Greg Clayton07e66e32011-07-20 03:41:06 +00001401 return false;
1402}
1403
1404bool
1405CommunicationKDP::SendRequestSuspend ()
1406{
1407 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +00001408 const CommandType command = KDP_SUSPEND;
Greg Clayton07e66e32011-07-20 03:41:06 +00001409 const uint32_t command_length = 8;
Greg Clayton07e66e32011-07-20 03:41:06 +00001410 MakeRequestPacketHeader (command, request_packet, command_length);
1411 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001412 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton07e66e32011-07-20 03:41:06 +00001413 return true;
1414 return false;
1415}
1416