blob: 2e7aeb24f347831ad5f0c2358137c34553a63758 [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
Greg Claytonf9765ac2011-07-15 03:27:12 +000020// Other libraries and framework includes
Greg Clayton4df8ddf2011-07-16 03:19:08 +000021#include "lldb/Core/DataBufferHeap.h"
Greg Clayton57508022011-07-15 16:31:38 +000022#include "lldb/Core/DataExtractor.h"
Greg Claytonf9765ac2011-07-15 03:27:12 +000023#include "lldb/Core/Log.h"
Greg Claytona63d08c2011-07-19 03:57:15 +000024#include "lldb/Core/State.h"
Jason Molenda4bd4e7e2012-09-29 04:02:01 +000025#include "lldb/Core/UUID.h"
Greg Claytonf9765ac2011-07-15 03:27:12 +000026#include "lldb/Host/FileSpec.h"
27#include "lldb/Host/Host.h"
28#include "lldb/Host/TimeValue.h"
29#include "lldb/Target/Process.h"
Greg Claytonf9765ac2011-07-15 03:27:12 +000030
31// Project includes
32#include "ProcessKDPLog.h"
33
Greg Claytonf9765ac2011-07-15 03:27:12 +000034using namespace lldb;
35using namespace lldb_private;
36
37//----------------------------------------------------------------------
38// CommunicationKDP constructor
39//----------------------------------------------------------------------
40CommunicationKDP::CommunicationKDP (const char *comm_name) :
41 Communication(comm_name),
Greg Claytona63d08c2011-07-19 03:57:15 +000042 m_addr_byte_size (4),
Greg Clayton4df8ddf2011-07-16 03:19:08 +000043 m_byte_order (eByteOrderLittle),
Jason Molenda54d04f32013-07-02 01:29:59 +000044 m_packet_timeout (5),
Greg Claytonf9765ac2011-07-15 03:27:12 +000045 m_sequence_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton97d5cf02012-09-25 02:40:06 +000046 m_is_running (false),
Greg Clayton4df8ddf2011-07-16 03:19:08 +000047 m_session_key (0u),
48 m_request_sequence_id (0u),
49 m_exception_sequence_id (0u),
50 m_kdp_version_version (0u),
51 m_kdp_version_feature (0u),
52 m_kdp_hostinfo_cpu_mask (0u),
53 m_kdp_hostinfo_cpu_type (0u),
54 m_kdp_hostinfo_cpu_subtype (0u)
Greg Claytonf9765ac2011-07-15 03:27:12 +000055{
56}
57
58//----------------------------------------------------------------------
59// Destructor
60//----------------------------------------------------------------------
61CommunicationKDP::~CommunicationKDP()
62{
63 if (IsConnected())
64 {
65 Disconnect();
66 }
67}
68
Greg Clayton57508022011-07-15 16:31:38 +000069bool
Greg Clayton4df8ddf2011-07-16 03:19:08 +000070CommunicationKDP::SendRequestPacket (const PacketStreamType &request_packet)
Greg Claytonf9765ac2011-07-15 03:27:12 +000071{
72 Mutex::Locker locker(m_sequence_mutex);
Greg Clayton57508022011-07-15 16:31:38 +000073 return SendRequestPacketNoLock (request_packet);
Greg Claytonf9765ac2011-07-15 03:27:12 +000074}
75
Greg Clayton4df8ddf2011-07-16 03:19:08 +000076#if 0
77typedef struct {
78 uint8_t request; // Either: CommandType | ePacketTypeRequest, or CommandType | ePacketTypeReply
79 uint8_t sequence;
80 uint16_t length; // Length of entire packet including this header
81 uint32_t key; // Session key
82} kdp_hdr_t;
83#endif
84
Greg Clayton57508022011-07-15 16:31:38 +000085void
Greg Clayton1d19a2f2012-10-19 22:22:57 +000086CommunicationKDP::MakeRequestPacketHeader (CommandType request_type,
Greg Clayton4df8ddf2011-07-16 03:19:08 +000087 PacketStreamType &request_packet,
88 uint16_t request_length)
Greg Claytonf9765ac2011-07-15 03:27:12 +000089{
Greg Clayton57508022011-07-15 16:31:38 +000090 request_packet.Clear();
Greg Clayton4df8ddf2011-07-16 03:19:08 +000091 request_packet.PutHex8 (request_type | ePacketTypeRequest); // Set the request type
92 request_packet.PutHex8 (m_request_sequence_id++); // Sequence number
93 request_packet.PutHex16 (request_length); // Length of the packet including this header
94 request_packet.PutHex32 (m_session_key); // Session key
Greg Claytonf9765ac2011-07-15 03:27:12 +000095}
96
Greg Clayton4df8ddf2011-07-16 03:19:08 +000097bool
98CommunicationKDP::SendRequestAndGetReply (const CommandType command,
Greg Clayton0ee809b2013-02-14 19:11:23 +000099 const PacketStreamType &request_packet,
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000100 DataExtractor &reply_packet)
101{
Greg Clayton97d5cf02012-09-25 02:40:06 +0000102 if (IsRunning())
103 {
Greg Clayton5160ce52013-03-27 23:08:40 +0000104 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS));
Greg Clayton97d5cf02012-09-25 02:40:06 +0000105 if (log)
106 {
107 PacketStreamType log_strm;
108 DumpPacket (log_strm, request_packet.GetData(), request_packet.GetSize());
109 log->Printf("error: kdp running, not sending packet: %.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData());
110 }
111 return false;
112 }
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000113
Greg Clayton97d5cf02012-09-25 02:40:06 +0000114 Mutex::Locker locker(m_sequence_mutex);
115#ifdef LLDB_CONFIGURATION_DEBUG
116 // NOTE: this only works for packets that are in native endian byte order
117 assert (request_packet.GetSize() == *((uint16_t *)(request_packet.GetData() + 2)));
118#endif
Greg Clayton0ee809b2013-02-14 19:11:23 +0000119 lldb::offset_t offset = 1;
120 const uint32_t num_retries = 3;
121 for (uint32_t i=0; i<num_retries; ++i)
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000122 {
Greg Clayton0ee809b2013-02-14 19:11:23 +0000123 if (SendRequestPacketNoLock(request_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000124 {
Greg Clayton0ee809b2013-02-14 19:11:23 +0000125 const uint8_t request_sequence_id = (uint8_t)request_packet.GetData()[1];
Greg Clayton7ad05d12013-07-10 01:05:05 +0000126 while (1)
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000127 {
Greg Clayton7ad05d12013-07-10 01:05:05 +0000128 if (WaitForPacketWithTimeoutMicroSecondsNoLock (reply_packet, GetPacketTimeoutInMicroSeconds ()))
Greg Clayton97d5cf02012-09-25 02:40:06 +0000129 {
Greg Clayton7ad05d12013-07-10 01:05:05 +0000130 offset = 0;
Greg Claytona063c812013-07-10 17:58:19 +0000131 const uint8_t reply_command = reply_packet.GetU8 (&offset);
Greg Clayton7ad05d12013-07-10 01:05:05 +0000132 const uint8_t reply_sequence_id = reply_packet.GetU8 (&offset);
Greg Clayton0ee809b2013-02-14 19:11:23 +0000133 if (request_sequence_id == reply_sequence_id)
134 {
Greg Clayton7ad05d12013-07-10 01:05:05 +0000135 // The sequent ID was correct, now verify we got the response we were looking for
136 if ((reply_command & eCommandTypeMask) == command)
137 {
138 // Success
139 if (command == KDP_RESUMECPUS)
140 m_is_running.SetValue(true, eBroadcastAlways);
141 return true;
142 }
143 else
144 {
145 // Failed to get the correct response, bail
146 reply_packet.Clear();
147 return false;
148 }
Greg Clayton0ee809b2013-02-14 19:11:23 +0000149 }
Greg Clayton7ad05d12013-07-10 01:05:05 +0000150 else if (reply_sequence_id > request_sequence_id)
151 {
152 // Sequence ID was greater than the sequence ID of the packet we sent, something
153 // is really wrong...
154 reply_packet.Clear();
155 return false;
156 }
157 else
158 {
159 // The reply sequence ID was less than our current packet's sequence ID
160 // so we should keep trying to get a response because this was a response
161 // for a previous packet that we must have retried.
162 }
163 }
164 else
165 {
166 // Break and retry sending the packet as we didn't get a response due to timeout
167 break;
Greg Clayton97d5cf02012-09-25 02:40:06 +0000168 }
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000169 }
170 }
171 }
172 reply_packet.Clear();
173 return false;
174}
Greg Claytonf9765ac2011-07-15 03:27:12 +0000175
Greg Clayton57508022011-07-15 16:31:38 +0000176bool
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000177CommunicationKDP::SendRequestPacketNoLock (const PacketStreamType &request_packet)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000178{
179 if (IsConnected())
180 {
Greg Clayton57508022011-07-15 16:31:38 +0000181 const char *packet_data = request_packet.GetData();
182 const size_t packet_size = request_packet.GetSize();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000183
Greg Clayton5160ce52013-03-27 23:08:40 +0000184 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000185 if (log)
Greg Clayton57508022011-07-15 16:31:38 +0000186 {
Greg Claytona63d08c2011-07-19 03:57:15 +0000187 PacketStreamType log_strm;
188 DumpPacket (log_strm, packet_data, packet_size);
189 log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData());
Greg Clayton57508022011-07-15 16:31:38 +0000190 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000191 ConnectionStatus status = eConnectionStatusSuccess;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000192
Greg Clayton57508022011-07-15 16:31:38 +0000193 size_t bytes_written = Write (packet_data,
194 packet_size,
195 status,
196 NULL);
197
198 if (bytes_written == packet_size)
199 return true;
200
201 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000202 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 +0000203 }
204 return false;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000205}
206
207bool
Greg Clayton37a0a242012-04-11 00:24:49 +0000208CommunicationKDP::GetSequenceMutex (Mutex::Locker& locker)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000209{
Jim Ingham10ebffa2012-05-04 23:02:50 +0000210 return locker.TryLock (m_sequence_mutex);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000211}
212
213
214bool
215CommunicationKDP::WaitForNotRunningPrivate (const TimeValue *timeout_ptr)
216{
Greg Clayton97d5cf02012-09-25 02:40:06 +0000217 return m_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000218}
219
220size_t
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000221CommunicationKDP::WaitForPacketWithTimeoutMicroSeconds (DataExtractor &packet, uint32_t timeout_usec)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000222{
223 Mutex::Locker locker(m_sequence_mutex);
224 return WaitForPacketWithTimeoutMicroSecondsNoLock (packet, timeout_usec);
225}
226
227size_t
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000228CommunicationKDP::WaitForPacketWithTimeoutMicroSecondsNoLock (DataExtractor &packet, uint32_t timeout_usec)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000229{
230 uint8_t buffer[8192];
231 Error error;
232
Greg Clayton5160ce52013-03-27 23:08:40 +0000233 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS | KDP_LOG_VERBOSE));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000234
235 // Check for a packet from our cache first without trying any reading...
236 if (CheckForPacket (NULL, 0, packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000237 return packet.GetByteSize();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000238
239 bool timed_out = false;
240 while (IsConnected() && !timed_out)
241 {
Johnny Chenbecabe02011-07-21 19:00:59 +0000242 lldb::ConnectionStatus status = eConnectionStatusNoConnection;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000243 size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error);
244
245 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000246 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 +0000247 __PRETTY_FUNCTION__,
248 timeout_usec,
249 Communication::ConnectionStatusAsCString (status),
250 error.AsCString(),
Greg Clayton43e0af02012-09-18 18:04:04 +0000251 (uint64_t)bytes_read);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000252
253 if (bytes_read > 0)
254 {
255 if (CheckForPacket (buffer, bytes_read, packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000256 return packet.GetByteSize();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000257 }
258 else
259 {
260 switch (status)
261 {
262 case eConnectionStatusTimedOut:
263 timed_out = true;
264 break;
265 case eConnectionStatusSuccess:
266 //printf ("status = success but error = %s\n", error.AsCString("<invalid>"));
267 break;
268
269 case eConnectionStatusEndOfFile:
270 case eConnectionStatusNoConnection:
271 case eConnectionStatusLostConnection:
272 case eConnectionStatusError:
273 Disconnect();
274 break;
275 }
276 }
277 }
278 packet.Clear ();
279 return 0;
280}
281
282bool
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000283CommunicationKDP::CheckForPacket (const uint8_t *src, size_t src_len, DataExtractor &packet)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000284{
285 // Put the packet data into the buffer in a thread safe fashion
286 Mutex::Locker locker(m_bytes_mutex);
287
Greg Clayton5160ce52013-03-27 23:08:40 +0000288 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000289
290 if (src && src_len > 0)
291 {
292 if (log && log->GetVerbose())
293 {
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000294 PacketStreamType log_strm;
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000295 DataExtractor::DumpHexBytes (&log_strm, src, src_len, UINT32_MAX, LLDB_INVALID_ADDRESS);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000296 log->Printf ("CommunicationKDP::%s adding %u bytes: %s",
Greg Claytonf9765ac2011-07-15 03:27:12 +0000297 __FUNCTION__,
298 (uint32_t)src_len,
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000299 log_strm.GetData());
Greg Claytonf9765ac2011-07-15 03:27:12 +0000300 }
301 m_bytes.append ((const char *)src, src_len);
302 }
303
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000304 // Make sure we at least have enough bytes for a packet header
305 const size_t bytes_available = m_bytes.size();
306 if (bytes_available >= 8)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000307 {
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000308 packet.SetData (&m_bytes[0], bytes_available, m_byte_order);
Greg Claytonc7bece562013-01-25 18:06:21 +0000309 lldb::offset_t offset = 0;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000310 uint8_t reply_command = packet.GetU8(&offset);
311 switch (reply_command)
312 {
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000313 case ePacketTypeRequest | KDP_EXCEPTION:
314 case ePacketTypeRequest | KDP_TERMINATION:
315 // We got an exception request, so be sure to send an ACK
316 {
317 PacketStreamType request_ack_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
318 // Set the reply but and make the ACK packet
319 request_ack_packet.PutHex8 (reply_command | ePacketTypeReply);
320 request_ack_packet.PutHex8 (packet.GetU8(&offset));
321 request_ack_packet.PutHex16 (packet.GetU16(&offset));
322 request_ack_packet.PutHex32 (packet.GetU32(&offset));
Greg Clayton97d5cf02012-09-25 02:40:06 +0000323 m_is_running.SetValue(false, eBroadcastAlways);
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000324 // Ack to the exception or termination
325 SendRequestPacketNoLock (request_ack_packet);
326 }
327 // Fall through to case below to get packet contents
Greg Clayton5b882162011-07-21 01:12:01 +0000328 case ePacketTypeReply | KDP_CONNECT:
329 case ePacketTypeReply | KDP_DISCONNECT:
330 case ePacketTypeReply | KDP_HOSTINFO:
331 case ePacketTypeReply | KDP_VERSION:
332 case ePacketTypeReply | KDP_MAXBYTES:
333 case ePacketTypeReply | KDP_READMEM:
334 case ePacketTypeReply | KDP_WRITEMEM:
335 case ePacketTypeReply | KDP_READREGS:
336 case ePacketTypeReply | KDP_WRITEREGS:
337 case ePacketTypeReply | KDP_LOAD:
338 case ePacketTypeReply | KDP_IMAGEPATH:
339 case ePacketTypeReply | KDP_SUSPEND:
340 case ePacketTypeReply | KDP_RESUMECPUS:
Greg Clayton5b882162011-07-21 01:12:01 +0000341 case ePacketTypeReply | KDP_BREAKPOINT_SET:
342 case ePacketTypeReply | KDP_BREAKPOINT_REMOVE:
343 case ePacketTypeReply | KDP_REGIONS:
344 case ePacketTypeReply | KDP_REATTACH:
345 case ePacketTypeReply | KDP_HOSTREBOOT:
346 case ePacketTypeReply | KDP_READMEM64:
347 case ePacketTypeReply | KDP_WRITEMEM64:
348 case ePacketTypeReply | KDP_BREAKPOINT_SET64:
349 case ePacketTypeReply | KDP_BREAKPOINT_REMOVE64:
350 case ePacketTypeReply | KDP_KERNELVERSION:
Jason Molendace62fd72013-03-01 00:43:19 +0000351 case ePacketTypeReply | KDP_READPHYSMEM64:
352 case ePacketTypeReply | KDP_WRITEPHYSMEM64:
353 case ePacketTypeReply | KDP_READIOPORT:
354 case ePacketTypeReply | KDP_WRITEIOPORT:
355 case ePacketTypeReply | KDP_READMSR64:
356 case ePacketTypeReply | KDP_WRITEMSR64:
357 case ePacketTypeReply | KDP_DUMPINFO:
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000358 {
359 offset = 2;
360 const uint16_t length = packet.GetU16 (&offset);
361 if (length <= bytes_available)
362 {
363 // We have an entire packet ready, we need to copy the data
364 // bytes into a buffer that will be owned by the packet and
365 // erase the bytes from our communcation buffer "m_bytes"
366 packet.SetData (DataBufferSP (new DataBufferHeap (&m_bytes[0], length)));
367 m_bytes.erase (0, length);
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000368
369 if (log)
370 {
371 PacketStreamType log_strm;
Greg Claytona63d08c2011-07-19 03:57:15 +0000372 DumpPacket (log_strm, packet);
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000373
Greg Claytona63d08c2011-07-19 03:57:15 +0000374 log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData());
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000375 }
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000376 return true;
377 }
378 }
379 break;
380
381 default:
382 // Unrecognized reply command byte, erase this byte and try to get back on track
383 if (log)
384 log->Printf ("CommunicationKDP::%s: tossing junk byte: 0x%2.2x",
385 __FUNCTION__,
386 (uint8_t)m_bytes[0]);
387 m_bytes.erase(0, 1);
388 break;
389 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000390 }
391 packet.Clear();
392 return false;
393}
394
Greg Clayton57508022011-07-15 16:31:38 +0000395
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000396bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000397CommunicationKDP::SendRequestConnect (uint16_t reply_port,
398 uint16_t exc_port,
399 const char *greeting)
Greg Clayton57508022011-07-15 16:31:38 +0000400{
Greg Claytona63d08c2011-07-19 03:57:15 +0000401 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000402 if (greeting == NULL)
403 greeting = "";
404
Greg Clayton5b882162011-07-21 01:12:01 +0000405 const CommandType command = KDP_CONNECT;
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000406 // Length is 82 uint16_t and the length of the greeting C string with the terminating NULL
407 const uint32_t command_length = 8 + 2 + 2 + ::strlen(greeting) + 1;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000408 MakeRequestPacketHeader (command, request_packet, command_length);
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000409 // Always send connect ports as little endian
410 request_packet.SetByteOrder (eByteOrderLittle);
Greg Clayton99e384c2014-01-10 00:31:10 +0000411 request_packet.PutHex16 (htons(reply_port));
412 request_packet.PutHex16 (htons(exc_port));
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000413 request_packet.SetByteOrder (m_byte_order);
414 request_packet.PutCString (greeting);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000415 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000416 return SendRequestAndGetReply (command, request_packet, reply_packet);
Greg Clayton57508022011-07-15 16:31:38 +0000417}
418
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000419void
420CommunicationKDP::ClearKDPSettings ()
421{
422 m_request_sequence_id = 0;
423 m_kdp_version_version = 0;
424 m_kdp_version_feature = 0;
425 m_kdp_hostinfo_cpu_mask = 0;
426 m_kdp_hostinfo_cpu_type = 0;
427 m_kdp_hostinfo_cpu_subtype = 0;
428}
429
430bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000431CommunicationKDP::SendRequestReattach (uint16_t reply_port)
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000432{
Greg Claytona63d08c2011-07-19 03:57:15 +0000433 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000434 const CommandType command = KDP_REATTACH;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000435 // Length is 8 bytes for the header plus 2 bytes for the reply UDP port
436 const uint32_t command_length = 8 + 2;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000437 MakeRequestPacketHeader (command, request_packet, command_length);
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000438 // Always send connect ports as little endian
439 request_packet.SetByteOrder (eByteOrderLittle);
Greg Clayton99e384c2014-01-10 00:31:10 +0000440 request_packet.PutHex16(htons(reply_port));
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000441 request_packet.SetByteOrder (m_byte_order);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000442 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000443 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000444 {
445 // Reset the sequence ID to zero for reattach
446 ClearKDPSettings ();
Greg Claytonc7bece562013-01-25 18:06:21 +0000447 lldb::offset_t offset = 4;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000448 m_session_key = reply_packet.GetU32 (&offset);
449 return true;
450 }
451 return false;
452}
453
454uint32_t
455CommunicationKDP::GetVersion ()
456{
457 if (!VersionIsValid())
458 SendRequestVersion();
459 return m_kdp_version_version;
460}
461
462uint32_t
463CommunicationKDP::GetFeatureFlags ()
464{
465 if (!VersionIsValid())
466 SendRequestVersion();
467 return m_kdp_version_feature;
468}
469
470bool
471CommunicationKDP::SendRequestVersion ()
472{
Greg Claytona63d08c2011-07-19 03:57:15 +0000473 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000474 const CommandType command = KDP_VERSION;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000475 const uint32_t command_length = 8;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000476 MakeRequestPacketHeader (command, request_packet, command_length);
477 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000478 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000479 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000480 lldb::offset_t offset = 8;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000481 m_kdp_version_version = reply_packet.GetU32 (&offset);
482 m_kdp_version_feature = reply_packet.GetU32 (&offset);
483 return true;
484 }
485 return false;
486}
487
Greg Clayton5b882162011-07-21 01:12:01 +0000488#if 0 // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
489const char *
490CommunicationKDP::GetImagePath ()
491{
492 if (m_image_path.empty())
493 SendRequestImagePath();
494 return m_image_path.c_str();
495}
496
497bool
498CommunicationKDP::SendRequestImagePath ()
499{
500 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
501 const CommandType command = KDP_IMAGEPATH;
502 const uint32_t command_length = 8;
Greg Clayton5b882162011-07-21 01:12:01 +0000503 MakeRequestPacketHeader (command, request_packet, command_length);
504 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000505 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton5b882162011-07-21 01:12:01 +0000506 {
507 const char *path = reply_packet.PeekCStr(8);
508 if (path && path[0])
509 m_kernel_version.assign (path);
510 return true;
511 }
512 return false;
513}
514#endif
515
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000516uint32_t
517CommunicationKDP::GetCPUMask ()
518{
519 if (!HostInfoIsValid())
520 SendRequestHostInfo();
521 return m_kdp_hostinfo_cpu_mask;
522}
523
524uint32_t
525CommunicationKDP::GetCPUType ()
526{
527 if (!HostInfoIsValid())
528 SendRequestHostInfo();
529 return m_kdp_hostinfo_cpu_type;
530}
531
532uint32_t
533CommunicationKDP::GetCPUSubtype ()
534{
535 if (!HostInfoIsValid())
536 SendRequestHostInfo();
537 return m_kdp_hostinfo_cpu_subtype;
538}
539
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000540lldb_private::UUID
541CommunicationKDP::GetUUID ()
542{
543 UUID uuid;
544 if (GetKernelVersion() == NULL)
545 return uuid;
546
547 if (m_kernel_version.find("UUID=") == std::string::npos)
548 return uuid;
549
550 size_t p = m_kernel_version.find("UUID=") + strlen ("UUID=");
551 std::string uuid_str = m_kernel_version.substr(p, 36);
552 if (uuid_str.size() < 32)
553 return uuid;
554
555 if (uuid.SetFromCString (uuid_str.c_str()) == 0)
556 {
557 UUID invalid_uuid;
558 return invalid_uuid;
559 }
560
561 return uuid;
562}
563
Jason Molenda840f12c2012-10-25 00:25:13 +0000564bool
565CommunicationKDP::RemoteIsEFI ()
566{
567 if (GetKernelVersion() == NULL)
568 return false;
569 if (strncmp (m_kernel_version.c_str(), "EFI", 3) == 0)
570 return true;
571 else
572 return false;
573}
574
Jason Molendaca2ffa72013-05-09 23:52:21 +0000575bool
576CommunicationKDP::RemoteIsDarwinKernel ()
577{
578 if (GetKernelVersion() == NULL)
579 return false;
580 if (m_kernel_version.find("Darwin Kernel") != std::string::npos)
581 return true;
582 else
583 return false;
584}
585
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000586lldb::addr_t
587CommunicationKDP::GetLoadAddress ()
588{
589 if (GetKernelVersion() == NULL)
590 return LLDB_INVALID_ADDRESS;
591
592 if (m_kernel_version.find("stext=") == std::string::npos)
593 return LLDB_INVALID_ADDRESS;
594 size_t p = m_kernel_version.find("stext=") + strlen ("stext=");
595 if (m_kernel_version[p] != '0' || m_kernel_version[p + 1] != 'x')
596 return LLDB_INVALID_ADDRESS;
597
598 addr_t kernel_load_address;
599 errno = 0;
600 kernel_load_address = ::strtoul (m_kernel_version.c_str() + p, NULL, 16);
601 if (errno != 0 || kernel_load_address == 0)
602 return LLDB_INVALID_ADDRESS;
603
604 return kernel_load_address;
605}
606
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000607bool
608CommunicationKDP::SendRequestHostInfo ()
609{
Greg Claytona63d08c2011-07-19 03:57:15 +0000610 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000611 const CommandType command = KDP_HOSTINFO;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000612 const uint32_t command_length = 8;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000613 MakeRequestPacketHeader (command, request_packet, command_length);
614 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000615 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000616 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000617 lldb::offset_t offset = 8;
Greg Claytona63d08c2011-07-19 03:57:15 +0000618 m_kdp_hostinfo_cpu_mask = reply_packet.GetU32 (&offset);
619 m_kdp_hostinfo_cpu_type = reply_packet.GetU32 (&offset);
620 m_kdp_hostinfo_cpu_subtype = reply_packet.GetU32 (&offset);
621
622 ArchSpec kernel_arch;
623 kernel_arch.SetArchitecture (eArchTypeMachO,
624 m_kdp_hostinfo_cpu_type,
625 m_kdp_hostinfo_cpu_subtype);
626
627 m_addr_byte_size = kernel_arch.GetAddressByteSize();
628 m_byte_order = kernel_arch.GetByteOrder();
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000629 return true;
630 }
631 return false;
632}
633
Greg Clayton07e66e32011-07-20 03:41:06 +0000634const char *
635CommunicationKDP::GetKernelVersion ()
636{
637 if (m_kernel_version.empty())
638 SendRequestKernelVersion ();
639 return m_kernel_version.c_str();
640}
641
642bool
643CommunicationKDP::SendRequestKernelVersion ()
644{
645 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000646 const CommandType command = KDP_KERNELVERSION;
Greg Clayton07e66e32011-07-20 03:41:06 +0000647 const uint32_t command_length = 8;
Greg Clayton07e66e32011-07-20 03:41:06 +0000648 MakeRequestPacketHeader (command, request_packet, command_length);
649 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000650 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton07e66e32011-07-20 03:41:06 +0000651 {
652 const char *kernel_version_cstr = reply_packet.PeekCStr(8);
653 if (kernel_version_cstr && kernel_version_cstr[0])
654 m_kernel_version.assign (kernel_version_cstr);
655 return true;
656 }
657 return false;
658}
659
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000660bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000661CommunicationKDP::SendRequestDisconnect ()
Greg Clayton57508022011-07-15 16:31:38 +0000662{
Greg Claytona63d08c2011-07-19 03:57:15 +0000663 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000664 const CommandType command = KDP_DISCONNECT;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000665 const uint32_t command_length = 8;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000666 MakeRequestPacketHeader (command, request_packet, command_length);
667 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000668 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000669 {
670 // Are we supposed to get a reply for disconnect?
671 }
672 ClearKDPSettings ();
673 return true;
Greg Clayton57508022011-07-15 16:31:38 +0000674}
675
Greg Claytona63d08c2011-07-19 03:57:15 +0000676uint32_t
677CommunicationKDP::SendRequestReadMemory (lldb::addr_t addr,
678 void *dst,
679 uint32_t dst_len,
680 Error &error)
681{
682 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
683 bool use_64 = (GetVersion() >= 11);
684 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
Greg Clayton5b882162011-07-21 01:12:01 +0000685 const CommandType command = use_64 ? KDP_READMEM64 : KDP_READMEM;
Greg Claytona63d08c2011-07-19 03:57:15 +0000686 // Size is header + address size + uint32_t length
687 const uint32_t command_length = 8 + command_addr_byte_size + 4;
Greg Claytona63d08c2011-07-19 03:57:15 +0000688 MakeRequestPacketHeader (command, request_packet, command_length);
689 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
690 request_packet.PutHex32 (dst_len);
691 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000692 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Claytona63d08c2011-07-19 03:57:15 +0000693 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000694 lldb::offset_t offset = 8;
Greg Claytona63d08c2011-07-19 03:57:15 +0000695 uint32_t kdp_error = reply_packet.GetU32 (&offset);
696 uint32_t src_len = reply_packet.GetByteSize() - 12;
697
698 if (src_len > 0)
699 {
700 const void *src = reply_packet.GetData(&offset, src_len);
701 if (src)
702 {
703 ::memcpy (dst, src, src_len);
704 error.Clear();
705 return src_len;
706 }
707 }
708 if (kdp_error)
709 error.SetErrorStringWithFormat ("kdp read memory failed (error %u)", kdp_error);
710 else
711 error.SetErrorString ("kdp read memory failed");
712 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000713 else
714 {
715 error.SetErrorString ("failed to send packet");
716 }
Greg Claytona63d08c2011-07-19 03:57:15 +0000717 return 0;
718}
719
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000720
721uint32_t
722CommunicationKDP::SendRequestWriteMemory (lldb::addr_t addr,
723 const void *src,
724 uint32_t src_len,
725 Error &error)
726{
727 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
728 bool use_64 = (GetVersion() >= 11);
729 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
Greg Clayton5b882162011-07-21 01:12:01 +0000730 const CommandType command = use_64 ? KDP_WRITEMEM64 : KDP_WRITEMEM;
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000731 // Size is header + address size + uint32_t length
Greg Claytonb556c9b2012-10-12 23:24:05 +0000732 const uint32_t command_length = 8 + command_addr_byte_size + 4 + src_len;
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000733 MakeRequestPacketHeader (command, request_packet, command_length);
734 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
735 request_packet.PutHex32 (src_len);
Jason Molenda57656e72012-10-19 02:16:22 +0000736 request_packet.PutRawBytes(src, src_len);
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000737
738 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000739 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000740 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000741 lldb::offset_t offset = 8;
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000742 uint32_t kdp_error = reply_packet.GetU32 (&offset);
743 if (kdp_error)
744 error.SetErrorStringWithFormat ("kdp write memory failed (error %u)", kdp_error);
745 else
746 {
747 error.Clear();
748 return src_len;
749 }
750 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000751 else
752 {
753 error.SetErrorString ("failed to send packet");
754 }
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000755 return 0;
756}
757
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000758bool
759CommunicationKDP::SendRawRequest (uint8_t command_byte,
760 const void *src, // Raw packet payload bytes
761 uint32_t src_len, // Raw packet payload length
762 DataExtractor &reply_packet,
763 Error &error)
764{
765 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
766 // Size is header + address size + uint32_t length
767 const uint32_t command_length = 8 + src_len;
768 const CommandType command = (CommandType)command_byte;
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000769 MakeRequestPacketHeader (command, request_packet, command_length);
770 request_packet.PutRawBytes(src, src_len);
771
Greg Clayton0ee809b2013-02-14 19:11:23 +0000772 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000773 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000774 lldb::offset_t offset = 8;
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000775 uint32_t kdp_error = reply_packet.GetU32 (&offset);
Jason Molendace62fd72013-03-01 00:43:19 +0000776 if (kdp_error && (command_byte != KDP_DUMPINFO))
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000777 error.SetErrorStringWithFormat ("request packet 0x%8.8x failed (error %u)", command_byte, kdp_error);
778 else
779 {
780 error.Clear();
781 return true;
782 }
783 }
784 else
785 {
786 error.SetErrorString ("failed to send packet");
787 }
788 return false;
789}
790
791
Greg Claytona63d08c2011-07-19 03:57:15 +0000792const char *
793CommunicationKDP::GetCommandAsCString (uint8_t command)
794{
795 switch (command)
796 {
Greg Clayton5b882162011-07-21 01:12:01 +0000797 case KDP_CONNECT: return "KDP_CONNECT";
798 case KDP_DISCONNECT: return "KDP_DISCONNECT";
799 case KDP_HOSTINFO: return "KDP_HOSTINFO";
800 case KDP_VERSION: return "KDP_VERSION";
801 case KDP_MAXBYTES: return "KDP_MAXBYTES";
802 case KDP_READMEM: return "KDP_READMEM";
803 case KDP_WRITEMEM: return "KDP_WRITEMEM";
804 case KDP_READREGS: return "KDP_READREGS";
805 case KDP_WRITEREGS: return "KDP_WRITEREGS";
806 case KDP_LOAD: return "KDP_LOAD";
807 case KDP_IMAGEPATH: return "KDP_IMAGEPATH";
808 case KDP_SUSPEND: return "KDP_SUSPEND";
809 case KDP_RESUMECPUS: return "KDP_RESUMECPUS";
810 case KDP_EXCEPTION: return "KDP_EXCEPTION";
811 case KDP_TERMINATION: return "KDP_TERMINATION";
812 case KDP_BREAKPOINT_SET: return "KDP_BREAKPOINT_SET";
813 case KDP_BREAKPOINT_REMOVE: return "KDP_BREAKPOINT_REMOVE";
814 case KDP_REGIONS: return "KDP_REGIONS";
815 case KDP_REATTACH: return "KDP_REATTACH";
816 case KDP_HOSTREBOOT: return "KDP_HOSTREBOOT";
817 case KDP_READMEM64: return "KDP_READMEM64";
818 case KDP_WRITEMEM64: return "KDP_WRITEMEM64";
819 case KDP_BREAKPOINT_SET64: return "KDP_BREAKPOINT64_SET";
820 case KDP_BREAKPOINT_REMOVE64: return "KDP_BREAKPOINT64_REMOVE";
821 case KDP_KERNELVERSION: return "KDP_KERNELVERSION";
Jason Molendace62fd72013-03-01 00:43:19 +0000822 case KDP_READPHYSMEM64: return "KDP_READPHYSMEM64";
823 case KDP_WRITEPHYSMEM64: return "KDP_WRITEPHYSMEM64";
824 case KDP_READIOPORT: return "KDP_READIOPORT";
825 case KDP_WRITEIOPORT: return "KDP_WRITEIOPORT";
826 case KDP_READMSR64: return "KDP_READMSR64";
827 case KDP_WRITEMSR64: return "KDP_WRITEMSR64";
828 case KDP_DUMPINFO: return "KDP_DUMPINFO";
Greg Claytona63d08c2011-07-19 03:57:15 +0000829 }
830 return NULL;
831}
832
833void
834CommunicationKDP::DumpPacket (Stream &s, const void *data, uint32_t data_len)
835{
836 DataExtractor extractor (data, data_len, m_byte_order, m_addr_byte_size);
837 DumpPacket (s, extractor);
838}
839
840void
841CommunicationKDP::DumpPacket (Stream &s, const DataExtractor& packet)
842{
843 const char *error_desc = NULL;
844 if (packet.GetByteSize() < 8)
845 {
846 error_desc = "error: invalid packet (too short): ";
847 }
848 else
849 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000850 lldb::offset_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000851 const uint8_t first_packet_byte = packet.GetU8 (&offset);
852 const uint8_t sequence_id = packet.GetU8 (&offset);
853 const uint16_t length = packet.GetU16 (&offset);
854 const uint32_t key = packet.GetU32 (&offset);
855 const CommandType command = ExtractCommand (first_packet_byte);
856 const char *command_name = GetCommandAsCString (command);
857 if (command_name)
858 {
859 const bool is_reply = ExtractIsReply(first_packet_byte);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000860 s.Printf ("(running=%i) %s %24s: 0x%2.2x 0x%2.2x 0x%4.4x 0x%8.8x ",
861 IsRunning(),
862 is_reply ? "<--" : "-->",
863 command_name,
864 first_packet_byte,
Greg Claytona63d08c2011-07-19 03:57:15 +0000865 sequence_id,
866 length,
Greg Clayton97d5cf02012-09-25 02:40:06 +0000867 key);
Greg Claytona63d08c2011-07-19 03:57:15 +0000868
869 if (is_reply)
870 {
871 // Dump request reply packets
872 switch (command)
873 {
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000874 // Commands that return a single 32 bit error
Greg Clayton5b882162011-07-21 01:12:01 +0000875 case KDP_CONNECT:
876 case KDP_WRITEMEM:
877 case KDP_WRITEMEM64:
878 case KDP_BREAKPOINT_SET:
879 case KDP_BREAKPOINT_REMOVE:
880 case KDP_BREAKPOINT_SET64:
881 case KDP_BREAKPOINT_REMOVE64:
882 case KDP_WRITEREGS:
883 case KDP_LOAD:
Jason Molendace62fd72013-03-01 00:43:19 +0000884 case KDP_WRITEIOPORT:
885 case KDP_WRITEMSR64:
Greg Claytona63d08c2011-07-19 03:57:15 +0000886 {
887 const uint32_t error = packet.GetU32 (&offset);
888 s.Printf(" (error=0x%8.8x)", error);
889 }
890 break;
891
Greg Clayton5b882162011-07-21 01:12:01 +0000892 case KDP_DISCONNECT:
893 case KDP_REATTACH:
894 case KDP_HOSTREBOOT:
895 case KDP_SUSPEND:
896 case KDP_RESUMECPUS:
897 case KDP_EXCEPTION:
898 case KDP_TERMINATION:
Greg Claytona63d08c2011-07-19 03:57:15 +0000899 // No return value for the reply, just the header to ack
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000900 s.PutCString(" ()");
Greg Claytona63d08c2011-07-19 03:57:15 +0000901 break;
902
Greg Clayton5b882162011-07-21 01:12:01 +0000903 case KDP_HOSTINFO:
Greg Claytona63d08c2011-07-19 03:57:15 +0000904 {
905 const uint32_t cpu_mask = packet.GetU32 (&offset);
906 const uint32_t cpu_type = packet.GetU32 (&offset);
907 const uint32_t cpu_subtype = packet.GetU32 (&offset);
908 s.Printf(" (cpu_mask=0x%8.8x, cpu_type=0x%8.8x, cpu_subtype=0x%8.8x)", cpu_mask, cpu_type, cpu_subtype);
909 }
910 break;
911
Greg Clayton5b882162011-07-21 01:12:01 +0000912 case KDP_VERSION:
Greg Claytona63d08c2011-07-19 03:57:15 +0000913 {
914 const uint32_t version = packet.GetU32 (&offset);
915 const uint32_t feature = packet.GetU32 (&offset);
916 s.Printf(" (version=0x%8.8x, feature=0x%8.8x)", version, feature);
917 }
918 break;
919
Greg Clayton5b882162011-07-21 01:12:01 +0000920 case KDP_REGIONS:
Greg Claytona63d08c2011-07-19 03:57:15 +0000921 {
922 const uint32_t region_count = packet.GetU32 (&offset);
923 s.Printf(" (count = %u", region_count);
924 for (uint32_t i=0; i<region_count; ++i)
925 {
926 const addr_t region_addr = packet.GetPointer (&offset);
927 const uint32_t region_size = packet.GetU32 (&offset);
928 const uint32_t region_prot = packet.GetU32 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +0000929 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 +0000930 }
931 }
932 break;
933
Greg Clayton5b882162011-07-21 01:12:01 +0000934 case KDP_READMEM:
935 case KDP_READMEM64:
Jason Molendace62fd72013-03-01 00:43:19 +0000936 case KDP_READPHYSMEM64:
Greg Claytona63d08c2011-07-19 03:57:15 +0000937 {
938 const uint32_t error = packet.GetU32 (&offset);
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000939 const uint32_t count = packet.GetByteSize() - offset;
Greg Clayton5b442372011-07-29 22:26:36 +0000940 s.Printf(" (error = 0x%8.8x:\n", error);
Greg Claytona63d08c2011-07-19 03:57:15 +0000941 if (count > 0)
Greg Clayton07e66e32011-07-20 03:41:06 +0000942 packet.Dump (&s, // Stream to dump to
943 offset, // Offset within "packet"
944 eFormatBytesWithASCII, // Format to use
945 1, // Size of each item in bytes
946 count, // Number of items
947 16, // Number per line
948 m_last_read_memory_addr, // Don't show addresses before each line
949 0, 0); // No bitfields
Greg Claytona63d08c2011-07-19 03:57:15 +0000950 }
951 break;
952
Greg Clayton5b882162011-07-21 01:12:01 +0000953 case KDP_READREGS:
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000954 {
955 const uint32_t error = packet.GetU32 (&offset);
956 const uint32_t count = packet.GetByteSize() - offset;
Greg Clayton5b442372011-07-29 22:26:36 +0000957 s.Printf(" (error = 0x%8.8x regs:\n", error);
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000958 if (count > 0)
959 packet.Dump (&s, // Stream to dump to
960 offset, // Offset within "packet"
961 eFormatHex, // Format to use
962 m_addr_byte_size, // Size of each item in bytes
963 count / m_addr_byte_size, // Number of items
964 16 / m_addr_byte_size, // Number per line
965 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
966 0, 0); // No bitfields
967 }
968 break;
969
Greg Clayton5b882162011-07-21 01:12:01 +0000970 case KDP_KERNELVERSION:
Greg Clayton07e66e32011-07-20 03:41:06 +0000971 {
972 const char *kernel_version = packet.PeekCStr(8);
973 s.Printf(" (version = \"%s\")", kernel_version);
974 }
975 break;
976
Greg Clayton5b882162011-07-21 01:12:01 +0000977 case KDP_MAXBYTES:
Greg Clayton07e66e32011-07-20 03:41:06 +0000978 {
979 const uint32_t max_bytes = packet.GetU32 (&offset);
980 s.Printf(" (max_bytes = 0x%8.8x (%u))", max_bytes, max_bytes);
981 }
982 break;
Greg Clayton5b882162011-07-21 01:12:01 +0000983 case KDP_IMAGEPATH:
Greg Clayton07e66e32011-07-20 03:41:06 +0000984 {
985 const char *path = packet.GetCStr(&offset);
986 s.Printf(" (path = \"%s\")", path);
987 }
988 break;
Jason Molendace62fd72013-03-01 00:43:19 +0000989
990 case KDP_READIOPORT:
991 case KDP_READMSR64:
992 {
993 const uint32_t error = packet.GetU32 (&offset);
994 const uint32_t count = packet.GetByteSize() - offset;
995 s.Printf(" (error = 0x%8.8x io:\n", error);
996 if (count > 0)
997 packet.Dump (&s, // Stream to dump to
998 offset, // Offset within "packet"
999 eFormatHex, // Format to use
1000 1, // Size of each item in bytes
1001 count, // Number of items
1002 16, // Number per line
1003 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1004 0, 0); // No bitfields
1005 }
1006 break;
1007 case KDP_DUMPINFO:
1008 {
1009 const uint32_t count = packet.GetByteSize() - offset;
1010 s.Printf(" (count = %u, bytes = \n", count);
1011 if (count > 0)
1012 packet.Dump (&s, // Stream to dump to
1013 offset, // Offset within "packet"
1014 eFormatHex, // Format to use
1015 1, // Size of each item in bytes
1016 count, // Number of items
1017 16, // Number per line
1018 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1019 0, 0); // No bitfields
1020
1021 }
1022 break;
1023
Greg Clayton07e66e32011-07-20 03:41:06 +00001024 default:
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001025 s.Printf(" (add support for dumping this packet reply!!!");
Greg Claytona63d08c2011-07-19 03:57:15 +00001026 break;
1027
1028 }
1029 }
1030 else
1031 {
1032 // Dump request packets
1033 switch (command)
1034 {
Greg Clayton5b882162011-07-21 01:12:01 +00001035 case KDP_CONNECT:
Greg Claytona63d08c2011-07-19 03:57:15 +00001036 {
Greg Clayton99e384c2014-01-10 00:31:10 +00001037 const uint16_t reply_port = ntohs(packet.GetU16 (&offset));
1038 const uint16_t exc_port = ntohs(packet.GetU16 (&offset));
Greg Clayton5b442372011-07-29 22:26:36 +00001039 s.Printf(" (reply_port = %u, exc_port = %u, greeting = \"%s\")", reply_port, exc_port, packet.GetCStr(&offset));
Greg Claytona63d08c2011-07-19 03:57:15 +00001040 }
1041 break;
1042
Greg Clayton5b882162011-07-21 01:12:01 +00001043 case KDP_DISCONNECT:
1044 case KDP_HOSTREBOOT:
1045 case KDP_HOSTINFO:
1046 case KDP_VERSION:
1047 case KDP_REGIONS:
1048 case KDP_KERNELVERSION:
1049 case KDP_MAXBYTES:
1050 case KDP_IMAGEPATH:
1051 case KDP_SUSPEND:
Greg Claytona63d08c2011-07-19 03:57:15 +00001052 // No args, just the header in the request...
Greg Clayton07e66e32011-07-20 03:41:06 +00001053 s.PutCString(" ()");
1054 break;
1055
Greg Clayton5b882162011-07-21 01:12:01 +00001056 case KDP_RESUMECPUS:
Greg Clayton07e66e32011-07-20 03:41:06 +00001057 {
1058 const uint32_t cpu_mask = packet.GetU32 (&offset);
1059 s.Printf(" (cpu_mask = 0x%8.8x)", cpu_mask);
1060 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001061 break;
1062
Greg Clayton5b882162011-07-21 01:12:01 +00001063 case KDP_READMEM:
Greg Claytona63d08c2011-07-19 03:57:15 +00001064 {
1065 const uint32_t addr = packet.GetU32 (&offset);
1066 const uint32_t size = packet.GetU32 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +00001067 s.Printf(" (addr = 0x%8.8x, size = %u)", addr, size);
Greg Clayton07e66e32011-07-20 03:41:06 +00001068 m_last_read_memory_addr = addr;
Greg Claytona63d08c2011-07-19 03:57:15 +00001069 }
1070 break;
1071
Greg Clayton5b882162011-07-21 01:12:01 +00001072 case KDP_WRITEMEM:
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001073 {
1074 const uint32_t addr = packet.GetU32 (&offset);
1075 const uint32_t size = packet.GetU32 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +00001076 s.Printf(" (addr = 0x%8.8x, size = %u, bytes = \n", addr, size);
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001077 if (size > 0)
1078 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
1079 }
1080 break;
1081
Greg Clayton5b882162011-07-21 01:12:01 +00001082 case KDP_READMEM64:
Greg Claytona63d08c2011-07-19 03:57:15 +00001083 {
1084 const uint64_t addr = packet.GetU64 (&offset);
1085 const uint32_t size = packet.GetU32 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +00001086 s.Printf(" (addr = 0x%16.16" PRIx64 ", size = %u)", addr, size);
Greg Clayton07e66e32011-07-20 03:41:06 +00001087 m_last_read_memory_addr = addr;
Greg Claytona63d08c2011-07-19 03:57:15 +00001088 }
1089 break;
1090
Jason Molendace62fd72013-03-01 00:43:19 +00001091 case KDP_READPHYSMEM64:
1092 {
1093 const uint64_t addr = packet.GetU64 (&offset);
1094 const uint32_t size = packet.GetU32 (&offset);
1095 const uint32_t lcpu = packet.GetU16 (&offset);
1096 s.Printf(" (addr = 0x%16.16llx, size = %u, lcpu = %u)", addr, size, lcpu);
1097 m_last_read_memory_addr = addr;
1098 }
1099 break;
1100
Greg Clayton5b882162011-07-21 01:12:01 +00001101 case KDP_WRITEMEM64:
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001102 {
1103 const uint64_t addr = packet.GetU64 (&offset);
1104 const uint32_t size = packet.GetU32 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +00001105 s.Printf(" (addr = 0x%16.16" PRIx64 ", size = %u, bytes = \n", addr, size);
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001106 if (size > 0)
1107 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
1108 }
1109 break;
1110
Jason Molendace62fd72013-03-01 00:43:19 +00001111 case KDP_WRITEPHYSMEM64:
1112 {
1113 const uint64_t addr = packet.GetU64 (&offset);
1114 const uint32_t size = packet.GetU32 (&offset);
1115 const uint32_t lcpu = packet.GetU16 (&offset);
1116 s.Printf(" (addr = 0x%16.16llx, size = %u, lcpu = %u, bytes = \n", addr, size, lcpu);
1117 if (size > 0)
1118 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
1119 }
1120 break;
1121
Greg Clayton5b882162011-07-21 01:12:01 +00001122 case KDP_READREGS:
Greg Claytona63d08c2011-07-19 03:57:15 +00001123 {
1124 const uint32_t cpu = packet.GetU32 (&offset);
1125 const uint32_t flavor = packet.GetU32 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +00001126 s.Printf(" (cpu = %u, flavor = %u)", cpu, flavor);
Greg Claytona63d08c2011-07-19 03:57:15 +00001127 }
1128 break;
1129
Greg Clayton5b882162011-07-21 01:12:01 +00001130 case KDP_WRITEREGS:
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001131 {
1132 const uint32_t cpu = packet.GetU32 (&offset);
1133 const uint32_t flavor = packet.GetU32 (&offset);
1134 const uint32_t nbytes = packet.GetByteSize() - offset;
Greg Clayton5b442372011-07-29 22:26:36 +00001135 s.Printf(" (cpu = %u, flavor = %u, regs = \n", cpu, flavor);
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001136 if (nbytes > 0)
1137 packet.Dump (&s, // Stream to dump to
1138 offset, // Offset within "packet"
1139 eFormatHex, // Format to use
1140 m_addr_byte_size, // Size of each item in bytes
1141 nbytes / m_addr_byte_size, // Number of items
1142 16 / m_addr_byte_size, // Number per line
1143 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1144 0, 0); // No bitfields
1145 }
1146 break;
1147
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001148
Greg Clayton5b882162011-07-21 01:12:01 +00001149 case KDP_BREAKPOINT_SET:
1150 case KDP_BREAKPOINT_REMOVE:
Greg Clayton07e66e32011-07-20 03:41:06 +00001151 {
1152 const uint32_t addr = packet.GetU32 (&offset);
1153 s.Printf(" (addr = 0x%8.8x)", addr);
1154 }
1155 break;
1156
Greg Clayton5b882162011-07-21 01:12:01 +00001157 case KDP_BREAKPOINT_SET64:
1158 case KDP_BREAKPOINT_REMOVE64:
Greg Clayton07e66e32011-07-20 03:41:06 +00001159 {
1160 const uint64_t addr = packet.GetU64 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +00001161 s.Printf(" (addr = 0x%16.16" PRIx64 ")", addr);
Greg Clayton07e66e32011-07-20 03:41:06 +00001162 }
1163 break;
1164
1165
Greg Clayton5b882162011-07-21 01:12:01 +00001166 case KDP_LOAD:
Greg Clayton07e66e32011-07-20 03:41:06 +00001167 {
1168 const char *path = packet.GetCStr(&offset);
1169 s.Printf(" (path = \"%s\")", path);
1170 }
1171 break;
1172
Greg Clayton5b882162011-07-21 01:12:01 +00001173 case KDP_EXCEPTION:
Greg Clayton07e66e32011-07-20 03:41:06 +00001174 {
1175 const uint32_t count = packet.GetU32 (&offset);
1176
Greg Clayton07e66e32011-07-20 03:41:06 +00001177 for (uint32_t i=0; i<count; ++i)
1178 {
1179 const uint32_t cpu = packet.GetU32 (&offset);
1180 const uint32_t exc = packet.GetU32 (&offset);
1181 const uint32_t code = packet.GetU32 (&offset);
1182 const uint32_t subcode = packet.GetU32 (&offset);
1183 const char *exc_cstr = NULL;
1184 switch (exc)
1185 {
1186 case 1: exc_cstr = "EXC_BAD_ACCESS"; break;
1187 case 2: exc_cstr = "EXC_BAD_INSTRUCTION"; break;
1188 case 3: exc_cstr = "EXC_ARITHMETIC"; break;
1189 case 4: exc_cstr = "EXC_EMULATION"; break;
1190 case 5: exc_cstr = "EXC_SOFTWARE"; break;
1191 case 6: exc_cstr = "EXC_BREAKPOINT"; break;
1192 case 7: exc_cstr = "EXC_SYSCALL"; break;
1193 case 8: exc_cstr = "EXC_MACH_SYSCALL"; break;
1194 case 9: exc_cstr = "EXC_RPC_ALERT"; break;
1195 case 10: exc_cstr = "EXC_CRASH"; break;
1196 default:
1197 break;
1198 }
1199
Greg Clayton97d5cf02012-09-25 02:40:06 +00001200 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 +00001201 cpu, exc_cstr, exc, code, code, subcode, subcode);
1202 }
1203 }
1204 break;
1205
Greg Clayton5b882162011-07-21 01:12:01 +00001206 case KDP_TERMINATION:
Greg Clayton07e66e32011-07-20 03:41:06 +00001207 {
1208 const uint32_t term_code = packet.GetU32 (&offset);
1209 const uint32_t exit_code = packet.GetU32 (&offset);
1210 s.Printf(" (term_code = 0x%8.8x (%u), exit_code = 0x%8.8x (%u))", term_code, term_code, exit_code, exit_code);
1211 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001212 break;
1213
Greg Clayton5b882162011-07-21 01:12:01 +00001214 case KDP_REATTACH:
Greg Claytona63d08c2011-07-19 03:57:15 +00001215 {
Greg Clayton99e384c2014-01-10 00:31:10 +00001216 const uint16_t reply_port = ntohs(packet.GetU16 (&offset));
Greg Clayton5b442372011-07-29 22:26:36 +00001217 s.Printf(" (reply_port = %u)", reply_port);
Greg Claytona63d08c2011-07-19 03:57:15 +00001218 }
1219 break;
Jason Molendace62fd72013-03-01 00:43:19 +00001220
1221 case KDP_READMSR64:
1222 {
1223 const uint32_t address = packet.GetU32 (&offset);
1224 const uint16_t lcpu = packet.GetU16 (&offset);
1225 s.Printf(" (address=0x%8.8x, lcpu=0x%4.4x)", address, lcpu);
1226 }
1227 break;
1228
1229 case KDP_WRITEMSR64:
1230 {
1231 const uint32_t address = packet.GetU32 (&offset);
1232 const uint16_t lcpu = packet.GetU16 (&offset);
1233 const uint32_t nbytes = packet.GetByteSize() - offset;
1234 s.Printf(" (address=0x%8.8x, lcpu=0x%4.4x, nbytes=0x%8.8x)", lcpu, address, nbytes);
1235 if (nbytes > 0)
1236 packet.Dump (&s, // Stream to dump to
1237 offset, // Offset within "packet"
1238 eFormatHex, // Format to use
1239 1, // Size of each item in bytes
1240 nbytes, // Number of items
1241 16, // Number per line
1242 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1243 0, 0); // No bitfields
1244 }
1245 break;
1246
1247 case KDP_READIOPORT:
1248 {
1249 const uint16_t lcpu = packet.GetU16 (&offset);
1250 const uint16_t address = packet.GetU16 (&offset);
1251 const uint16_t nbytes = packet.GetU16 (&offset);
1252 s.Printf(" (lcpu=0x%4.4x, address=0x%4.4x, nbytes=%u)", lcpu, address, nbytes);
1253 }
1254 break;
1255
1256 case KDP_WRITEIOPORT:
1257 {
1258 const uint16_t lcpu = packet.GetU16 (&offset);
1259 const uint16_t address = packet.GetU16 (&offset);
1260 const uint16_t nbytes = packet.GetU16 (&offset);
1261 s.Printf(" (lcpu = %u, addr = 0x%4.4x, nbytes = %u, bytes = \n", lcpu, address, nbytes);
1262 if (nbytes > 0)
1263 packet.Dump (&s, // Stream to dump to
1264 offset, // Offset within "packet"
1265 eFormatHex, // Format to use
1266 1, // Size of each item in bytes
1267 nbytes, // Number of items
1268 16, // Number per line
1269 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1270 0, 0); // No bitfields
1271 }
1272 break;
1273
1274 case KDP_DUMPINFO:
1275 {
1276 const uint32_t count = packet.GetByteSize() - offset;
1277 s.Printf(" (count = %u, bytes = \n", count);
1278 if (count > 0)
1279 packet.Dump (&s, // Stream to dump to
1280 offset, // Offset within "packet"
1281 eFormatHex, // Format to use
1282 1, // Size of each item in bytes
1283 count, // Number of items
1284 16, // Number per line
1285 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1286 0, 0); // No bitfields
1287
1288 }
1289 break;
1290
1291 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001292 }
1293 }
1294 else
1295 {
1296 error_desc = "error: invalid packet command: ";
1297 }
1298 }
1299
1300 if (error_desc)
1301 {
1302 s.PutCString (error_desc);
1303
1304 packet.Dump (&s, // Stream to dump to
1305 0, // Offset into "packet"
1306 eFormatBytes, // Dump as hex bytes
1307 1, // Size of each item is 1 for single bytes
1308 packet.GetByteSize(), // Number of bytes
1309 UINT32_MAX, // Num bytes per line
1310 LLDB_INVALID_ADDRESS, // Base address
1311 0, 0); // Bitfield info set to not do anything bitfield related
1312 }
1313}
1314
1315uint32_t
1316CommunicationKDP::SendRequestReadRegisters (uint32_t cpu,
1317 uint32_t flavor,
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001318 void *dst,
Greg Claytona63d08c2011-07-19 03:57:15 +00001319 uint32_t dst_len,
1320 Error &error)
1321{
1322 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +00001323 const CommandType command = KDP_READREGS;
Greg Claytona63d08c2011-07-19 03:57:15 +00001324 // Size is header + 4 byte cpu and 4 byte flavor
1325 const uint32_t command_length = 8 + 4 + 4;
Greg Claytona63d08c2011-07-19 03:57:15 +00001326 MakeRequestPacketHeader (command, request_packet, command_length);
1327 request_packet.PutHex32 (cpu);
1328 request_packet.PutHex32 (flavor);
1329 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001330 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Claytona63d08c2011-07-19 03:57:15 +00001331 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001332 lldb::offset_t offset = 8;
Greg Claytona63d08c2011-07-19 03:57:15 +00001333 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1334 uint32_t src_len = reply_packet.GetByteSize() - 12;
1335
1336 if (src_len > 0)
1337 {
1338 const uint32_t bytes_to_copy = std::min<uint32_t>(src_len, dst_len);
1339 const void *src = reply_packet.GetData(&offset, bytes_to_copy);
1340 if (src)
1341 {
1342 ::memcpy (dst, src, bytes_to_copy);
1343 error.Clear();
1344 // Return the number of bytes we could have returned regardless if
1345 // we copied them or not, just so we know when things don't match up
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001346 return src_len;
Greg Claytona63d08c2011-07-19 03:57:15 +00001347 }
1348 }
1349 if (kdp_error)
1350 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error);
1351 else
1352 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u", cpu, flavor);
1353 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +00001354 else
1355 {
1356 error.SetErrorString ("failed to send packet");
1357 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001358 return 0;
1359}
1360
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001361uint32_t
1362CommunicationKDP::SendRequestWriteRegisters (uint32_t cpu,
1363 uint32_t flavor,
1364 const void *src,
1365 uint32_t src_len,
1366 Error &error)
1367{
1368 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
1369 const CommandType command = KDP_WRITEREGS;
1370 // Size is header + 4 byte cpu and 4 byte flavor
Greg Clayton97d5cf02012-09-25 02:40:06 +00001371 const uint32_t command_length = 8 + 4 + 4 + src_len;
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001372 MakeRequestPacketHeader (command, request_packet, command_length);
1373 request_packet.PutHex32 (cpu);
1374 request_packet.PutHex32 (flavor);
1375 request_packet.Write(src, src_len);
1376 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001377 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001378 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001379 lldb::offset_t offset = 8;
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001380 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1381 if (kdp_error == 0)
1382 return src_len;
1383 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error);
1384 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +00001385 else
1386 {
1387 error.SetErrorString ("failed to send packet");
1388 }
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001389 return 0;
1390}
1391
Greg Clayton07e66e32011-07-20 03:41:06 +00001392
1393bool
Greg Clayton97d5cf02012-09-25 02:40:06 +00001394CommunicationKDP::SendRequestResume ()
Greg Clayton07e66e32011-07-20 03:41:06 +00001395{
Greg Clayton07e66e32011-07-20 03:41:06 +00001396 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +00001397 const CommandType command = KDP_RESUMECPUS;
Greg Clayton07e66e32011-07-20 03:41:06 +00001398 const uint32_t command_length = 12;
Greg Clayton07e66e32011-07-20 03:41:06 +00001399 MakeRequestPacketHeader (command, request_packet, command_length);
Greg Clayton97d5cf02012-09-25 02:40:06 +00001400 request_packet.PutHex32(GetCPUMask());
Greg Clayton07e66e32011-07-20 03:41:06 +00001401
1402 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001403 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton07e66e32011-07-20 03:41:06 +00001404 return true;
1405 return false;
1406}
1407
1408bool
1409CommunicationKDP::SendRequestBreakpoint (bool set, addr_t addr)
1410{
1411 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
1412 bool use_64 = (GetVersion() >= 11);
1413 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
Greg Clayton5b882162011-07-21 01:12:01 +00001414 const CommandType command = set ? (use_64 ? KDP_BREAKPOINT_SET64 : KDP_BREAKPOINT_SET ):
1415 (use_64 ? KDP_BREAKPOINT_REMOVE64 : KDP_BREAKPOINT_REMOVE);
Greg Clayton07e66e32011-07-20 03:41:06 +00001416
1417 const uint32_t command_length = 8 + command_addr_byte_size;
Greg Clayton07e66e32011-07-20 03:41:06 +00001418 MakeRequestPacketHeader (command, request_packet, command_length);
1419 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
1420
1421 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001422 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton5b442372011-07-29 22:26:36 +00001423 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001424 lldb::offset_t offset = 8;
Greg Clayton5b442372011-07-29 22:26:36 +00001425 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1426 if (kdp_error == 0)
1427 return true;
1428 }
Greg Clayton07e66e32011-07-20 03:41:06 +00001429 return false;
1430}
1431
1432bool
1433CommunicationKDP::SendRequestSuspend ()
1434{
1435 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +00001436 const CommandType command = KDP_SUSPEND;
Greg Clayton07e66e32011-07-20 03:41:06 +00001437 const uint32_t command_length = 8;
Greg Clayton07e66e32011-07-20 03:41:06 +00001438 MakeRequestPacketHeader (command, request_packet, command_length);
1439 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001440 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton07e66e32011-07-20 03:41:06 +00001441 return true;
1442 return false;
1443}
1444