blob: 29a0d58f7080e289929096bc4c147d089e809701 [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//----------------------------------------------------------------------
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000040CommunicationKDP::CommunicationKDP(const char *comm_name)
41 : Communication(comm_name),
42 m_addr_byte_size(4),
43 m_byte_order(eByteOrderLittle),
44 m_packet_timeout(5),
45 m_sequence_mutex(),
46 m_is_running(false),
47 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{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000072 std::lock_guard<std::recursive_mutex> guard(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
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000114 std::lock_guard<std::recursive_mutex> guard(m_sequence_mutex);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000115#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
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000208CommunicationKDP::GetSequenceMutex(std::unique_lock<std::recursive_mutex> &lock)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000209{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000210 return (lock = std::unique_lock<std::recursive_mutex>(m_sequence_mutex, std::try_to_lock)).owns_lock();
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{
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +0000223 std::lock_guard<std::recursive_mutex> guard(m_sequence_mutex);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000224 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 {
Greg Claytonf0066ad2014-05-02 00:45:31 +0000262 case eConnectionStatusInterrupted:
Greg Claytonf9765ac2011-07-15 03:27:12 +0000263 case eConnectionStatusTimedOut:
264 timed_out = true;
265 break;
266 case eConnectionStatusSuccess:
267 //printf ("status = success but error = %s\n", error.AsCString("<invalid>"));
268 break;
269
270 case eConnectionStatusEndOfFile:
271 case eConnectionStatusNoConnection:
272 case eConnectionStatusLostConnection:
273 case eConnectionStatusError:
274 Disconnect();
275 break;
276 }
277 }
278 }
279 packet.Clear ();
280 return 0;
281}
282
283bool
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000284CommunicationKDP::CheckForPacket (const uint8_t *src, size_t src_len, DataExtractor &packet)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000285{
286 // Put the packet data into the buffer in a thread safe fashion
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000287 std::lock_guard<std::recursive_mutex> guard(m_bytes_mutex);
288
Greg Clayton5160ce52013-03-27 23:08:40 +0000289 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000290
291 if (src && src_len > 0)
292 {
293 if (log && log->GetVerbose())
294 {
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000295 PacketStreamType log_strm;
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000296 DataExtractor::DumpHexBytes (&log_strm, src, src_len, UINT32_MAX, LLDB_INVALID_ADDRESS);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000297 log->Printf ("CommunicationKDP::%s adding %u bytes: %s",
Greg Claytonf9765ac2011-07-15 03:27:12 +0000298 __FUNCTION__,
299 (uint32_t)src_len,
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000300 log_strm.GetData());
Greg Claytonf9765ac2011-07-15 03:27:12 +0000301 }
302 m_bytes.append ((const char *)src, src_len);
303 }
304
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000305 // Make sure we at least have enough bytes for a packet header
306 const size_t bytes_available = m_bytes.size();
307 if (bytes_available >= 8)
Greg Claytonf9765ac2011-07-15 03:27:12 +0000308 {
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000309 packet.SetData (&m_bytes[0], bytes_available, m_byte_order);
Greg Claytonc7bece562013-01-25 18:06:21 +0000310 lldb::offset_t offset = 0;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000311 uint8_t reply_command = packet.GetU8(&offset);
312 switch (reply_command)
313 {
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000314 case ePacketTypeRequest | KDP_EXCEPTION:
315 case ePacketTypeRequest | KDP_TERMINATION:
316 // We got an exception request, so be sure to send an ACK
317 {
318 PacketStreamType request_ack_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
319 // Set the reply but and make the ACK packet
320 request_ack_packet.PutHex8 (reply_command | ePacketTypeReply);
321 request_ack_packet.PutHex8 (packet.GetU8(&offset));
322 request_ack_packet.PutHex16 (packet.GetU16(&offset));
323 request_ack_packet.PutHex32 (packet.GetU32(&offset));
Greg Clayton97d5cf02012-09-25 02:40:06 +0000324 m_is_running.SetValue(false, eBroadcastAlways);
Greg Clayton4b1b8b32012-09-21 01:55:30 +0000325 // Ack to the exception or termination
326 SendRequestPacketNoLock (request_ack_packet);
327 }
328 // Fall through to case below to get packet contents
Jason Molenda62e06812016-02-16 04:14:33 +0000329 LLVM_FALLTHROUGH;
Greg Clayton5b882162011-07-21 01:12:01 +0000330 case ePacketTypeReply | KDP_CONNECT:
331 case ePacketTypeReply | KDP_DISCONNECT:
332 case ePacketTypeReply | KDP_HOSTINFO:
333 case ePacketTypeReply | KDP_VERSION:
334 case ePacketTypeReply | KDP_MAXBYTES:
335 case ePacketTypeReply | KDP_READMEM:
336 case ePacketTypeReply | KDP_WRITEMEM:
337 case ePacketTypeReply | KDP_READREGS:
338 case ePacketTypeReply | KDP_WRITEREGS:
339 case ePacketTypeReply | KDP_LOAD:
340 case ePacketTypeReply | KDP_IMAGEPATH:
341 case ePacketTypeReply | KDP_SUSPEND:
342 case ePacketTypeReply | KDP_RESUMECPUS:
Greg Clayton5b882162011-07-21 01:12:01 +0000343 case ePacketTypeReply | KDP_BREAKPOINT_SET:
344 case ePacketTypeReply | KDP_BREAKPOINT_REMOVE:
345 case ePacketTypeReply | KDP_REGIONS:
346 case ePacketTypeReply | KDP_REATTACH:
347 case ePacketTypeReply | KDP_HOSTREBOOT:
348 case ePacketTypeReply | KDP_READMEM64:
349 case ePacketTypeReply | KDP_WRITEMEM64:
350 case ePacketTypeReply | KDP_BREAKPOINT_SET64:
351 case ePacketTypeReply | KDP_BREAKPOINT_REMOVE64:
352 case ePacketTypeReply | KDP_KERNELVERSION:
Jason Molendace62fd72013-03-01 00:43:19 +0000353 case ePacketTypeReply | KDP_READPHYSMEM64:
354 case ePacketTypeReply | KDP_WRITEPHYSMEM64:
355 case ePacketTypeReply | KDP_READIOPORT:
356 case ePacketTypeReply | KDP_WRITEIOPORT:
357 case ePacketTypeReply | KDP_READMSR64:
358 case ePacketTypeReply | KDP_WRITEMSR64:
359 case ePacketTypeReply | KDP_DUMPINFO:
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000360 {
361 offset = 2;
362 const uint16_t length = packet.GetU16 (&offset);
363 if (length <= bytes_available)
364 {
365 // We have an entire packet ready, we need to copy the data
366 // bytes into a buffer that will be owned by the packet and
367 // erase the bytes from our communcation buffer "m_bytes"
368 packet.SetData (DataBufferSP (new DataBufferHeap (&m_bytes[0], length)));
369 m_bytes.erase (0, length);
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000370
371 if (log)
372 {
373 PacketStreamType log_strm;
Greg Claytona63d08c2011-07-19 03:57:15 +0000374 DumpPacket (log_strm, packet);
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000375
Greg Claytona63d08c2011-07-19 03:57:15 +0000376 log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData());
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000377 }
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000378 return true;
379 }
380 }
381 break;
382
383 default:
384 // Unrecognized reply command byte, erase this byte and try to get back on track
385 if (log)
386 log->Printf ("CommunicationKDP::%s: tossing junk byte: 0x%2.2x",
387 __FUNCTION__,
388 (uint8_t)m_bytes[0]);
389 m_bytes.erase(0, 1);
390 break;
391 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000392 }
393 packet.Clear();
394 return false;
395}
396
Greg Clayton57508022011-07-15 16:31:38 +0000397
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000398bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000399CommunicationKDP::SendRequestConnect (uint16_t reply_port,
400 uint16_t exc_port,
401 const char *greeting)
Greg Clayton57508022011-07-15 16:31:38 +0000402{
Greg Claytona63d08c2011-07-19 03:57:15 +0000403 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000404 if (greeting == NULL)
405 greeting = "";
406
Greg Clayton5b882162011-07-21 01:12:01 +0000407 const CommandType command = KDP_CONNECT;
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000408 // Length is 82 uint16_t and the length of the greeting C string with the terminating NULL
409 const uint32_t command_length = 8 + 2 + 2 + ::strlen(greeting) + 1;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000410 MakeRequestPacketHeader (command, request_packet, command_length);
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000411 // Always send connect ports as little endian
412 request_packet.SetByteOrder (eByteOrderLittle);
Greg Clayton99e384c2014-01-10 00:31:10 +0000413 request_packet.PutHex16 (htons(reply_port));
414 request_packet.PutHex16 (htons(exc_port));
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000415 request_packet.SetByteOrder (m_byte_order);
416 request_packet.PutCString (greeting);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000417 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000418 return SendRequestAndGetReply (command, request_packet, reply_packet);
Greg Clayton57508022011-07-15 16:31:38 +0000419}
420
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000421void
422CommunicationKDP::ClearKDPSettings ()
423{
424 m_request_sequence_id = 0;
425 m_kdp_version_version = 0;
426 m_kdp_version_feature = 0;
427 m_kdp_hostinfo_cpu_mask = 0;
428 m_kdp_hostinfo_cpu_type = 0;
429 m_kdp_hostinfo_cpu_subtype = 0;
430}
431
432bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000433CommunicationKDP::SendRequestReattach (uint16_t reply_port)
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000434{
Greg Claytona63d08c2011-07-19 03:57:15 +0000435 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000436 const CommandType command = KDP_REATTACH;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000437 // Length is 8 bytes for the header plus 2 bytes for the reply UDP port
438 const uint32_t command_length = 8 + 2;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000439 MakeRequestPacketHeader (command, request_packet, command_length);
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000440 // Always send connect ports as little endian
441 request_packet.SetByteOrder (eByteOrderLittle);
Greg Clayton99e384c2014-01-10 00:31:10 +0000442 request_packet.PutHex16(htons(reply_port));
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000443 request_packet.SetByteOrder (m_byte_order);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000444 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000445 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000446 {
447 // Reset the sequence ID to zero for reattach
448 ClearKDPSettings ();
Greg Claytonc7bece562013-01-25 18:06:21 +0000449 lldb::offset_t offset = 4;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000450 m_session_key = reply_packet.GetU32 (&offset);
451 return true;
452 }
453 return false;
454}
455
456uint32_t
457CommunicationKDP::GetVersion ()
458{
459 if (!VersionIsValid())
460 SendRequestVersion();
461 return m_kdp_version_version;
462}
463
464uint32_t
465CommunicationKDP::GetFeatureFlags ()
466{
467 if (!VersionIsValid())
468 SendRequestVersion();
469 return m_kdp_version_feature;
470}
471
472bool
473CommunicationKDP::SendRequestVersion ()
474{
Greg Claytona63d08c2011-07-19 03:57:15 +0000475 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000476 const CommandType command = KDP_VERSION;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000477 const uint32_t command_length = 8;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000478 MakeRequestPacketHeader (command, request_packet, command_length);
479 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000480 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000481 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000482 lldb::offset_t offset = 8;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000483 m_kdp_version_version = reply_packet.GetU32 (&offset);
484 m_kdp_version_feature = reply_packet.GetU32 (&offset);
485 return true;
486 }
487 return false;
488}
489
Greg Clayton5b882162011-07-21 01:12:01 +0000490#if 0 // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
491const char *
492CommunicationKDP::GetImagePath ()
493{
494 if (m_image_path.empty())
495 SendRequestImagePath();
496 return m_image_path.c_str();
497}
498
499bool
500CommunicationKDP::SendRequestImagePath ()
501{
502 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
503 const CommandType command = KDP_IMAGEPATH;
504 const uint32_t command_length = 8;
Greg Clayton5b882162011-07-21 01:12:01 +0000505 MakeRequestPacketHeader (command, request_packet, command_length);
506 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000507 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton5b882162011-07-21 01:12:01 +0000508 {
509 const char *path = reply_packet.PeekCStr(8);
510 if (path && path[0])
511 m_kernel_version.assign (path);
512 return true;
513 }
514 return false;
515}
516#endif
517
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000518uint32_t
519CommunicationKDP::GetCPUMask ()
520{
521 if (!HostInfoIsValid())
522 SendRequestHostInfo();
523 return m_kdp_hostinfo_cpu_mask;
524}
525
526uint32_t
527CommunicationKDP::GetCPUType ()
528{
529 if (!HostInfoIsValid())
530 SendRequestHostInfo();
531 return m_kdp_hostinfo_cpu_type;
532}
533
534uint32_t
535CommunicationKDP::GetCPUSubtype ()
536{
537 if (!HostInfoIsValid())
538 SendRequestHostInfo();
539 return m_kdp_hostinfo_cpu_subtype;
540}
541
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000542lldb_private::UUID
543CommunicationKDP::GetUUID ()
544{
545 UUID uuid;
546 if (GetKernelVersion() == NULL)
547 return uuid;
548
549 if (m_kernel_version.find("UUID=") == std::string::npos)
550 return uuid;
551
552 size_t p = m_kernel_version.find("UUID=") + strlen ("UUID=");
553 std::string uuid_str = m_kernel_version.substr(p, 36);
554 if (uuid_str.size() < 32)
555 return uuid;
556
557 if (uuid.SetFromCString (uuid_str.c_str()) == 0)
558 {
559 UUID invalid_uuid;
560 return invalid_uuid;
561 }
562
563 return uuid;
564}
565
Jason Molenda840f12c2012-10-25 00:25:13 +0000566bool
567CommunicationKDP::RemoteIsEFI ()
568{
569 if (GetKernelVersion() == NULL)
570 return false;
571 if (strncmp (m_kernel_version.c_str(), "EFI", 3) == 0)
572 return true;
573 else
574 return false;
575}
576
Jason Molendaca2ffa72013-05-09 23:52:21 +0000577bool
578CommunicationKDP::RemoteIsDarwinKernel ()
579{
580 if (GetKernelVersion() == NULL)
581 return false;
582 if (m_kernel_version.find("Darwin Kernel") != std::string::npos)
583 return true;
584 else
585 return false;
586}
587
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000588lldb::addr_t
589CommunicationKDP::GetLoadAddress ()
590{
591 if (GetKernelVersion() == NULL)
592 return LLDB_INVALID_ADDRESS;
593
594 if (m_kernel_version.find("stext=") == std::string::npos)
595 return LLDB_INVALID_ADDRESS;
596 size_t p = m_kernel_version.find("stext=") + strlen ("stext=");
597 if (m_kernel_version[p] != '0' || m_kernel_version[p + 1] != 'x')
598 return LLDB_INVALID_ADDRESS;
599
600 addr_t kernel_load_address;
601 errno = 0;
602 kernel_load_address = ::strtoul (m_kernel_version.c_str() + p, NULL, 16);
603 if (errno != 0 || kernel_load_address == 0)
604 return LLDB_INVALID_ADDRESS;
605
606 return kernel_load_address;
607}
608
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000609bool
610CommunicationKDP::SendRequestHostInfo ()
611{
Greg Claytona63d08c2011-07-19 03:57:15 +0000612 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000613 const CommandType command = KDP_HOSTINFO;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000614 const uint32_t command_length = 8;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000615 MakeRequestPacketHeader (command, request_packet, command_length);
616 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000617 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000618 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000619 lldb::offset_t offset = 8;
Greg Claytona63d08c2011-07-19 03:57:15 +0000620 m_kdp_hostinfo_cpu_mask = reply_packet.GetU32 (&offset);
621 m_kdp_hostinfo_cpu_type = reply_packet.GetU32 (&offset);
622 m_kdp_hostinfo_cpu_subtype = reply_packet.GetU32 (&offset);
623
624 ArchSpec kernel_arch;
625 kernel_arch.SetArchitecture (eArchTypeMachO,
626 m_kdp_hostinfo_cpu_type,
627 m_kdp_hostinfo_cpu_subtype);
628
629 m_addr_byte_size = kernel_arch.GetAddressByteSize();
630 m_byte_order = kernel_arch.GetByteOrder();
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000631 return true;
632 }
633 return false;
634}
635
Greg Clayton07e66e32011-07-20 03:41:06 +0000636const char *
637CommunicationKDP::GetKernelVersion ()
638{
639 if (m_kernel_version.empty())
640 SendRequestKernelVersion ();
641 return m_kernel_version.c_str();
642}
643
644bool
645CommunicationKDP::SendRequestKernelVersion ()
646{
647 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000648 const CommandType command = KDP_KERNELVERSION;
Greg Clayton07e66e32011-07-20 03:41:06 +0000649 const uint32_t command_length = 8;
Greg Clayton07e66e32011-07-20 03:41:06 +0000650 MakeRequestPacketHeader (command, request_packet, command_length);
651 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000652 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton07e66e32011-07-20 03:41:06 +0000653 {
654 const char *kernel_version_cstr = reply_packet.PeekCStr(8);
655 if (kernel_version_cstr && kernel_version_cstr[0])
656 m_kernel_version.assign (kernel_version_cstr);
657 return true;
658 }
659 return false;
660}
661
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000662bool
Greg Clayton3a29bdb2011-07-17 20:36:25 +0000663CommunicationKDP::SendRequestDisconnect ()
Greg Clayton57508022011-07-15 16:31:38 +0000664{
Greg Claytona63d08c2011-07-19 03:57:15 +0000665 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +0000666 const CommandType command = KDP_DISCONNECT;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000667 const uint32_t command_length = 8;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000668 MakeRequestPacketHeader (command, request_packet, command_length);
669 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000670 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000671 {
672 // Are we supposed to get a reply for disconnect?
673 }
674 ClearKDPSettings ();
675 return true;
Greg Clayton57508022011-07-15 16:31:38 +0000676}
677
Greg Claytona63d08c2011-07-19 03:57:15 +0000678uint32_t
679CommunicationKDP::SendRequestReadMemory (lldb::addr_t addr,
680 void *dst,
681 uint32_t dst_len,
682 Error &error)
683{
684 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
685 bool use_64 = (GetVersion() >= 11);
686 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
Greg Clayton5b882162011-07-21 01:12:01 +0000687 const CommandType command = use_64 ? KDP_READMEM64 : KDP_READMEM;
Greg Claytona63d08c2011-07-19 03:57:15 +0000688 // Size is header + address size + uint32_t length
689 const uint32_t command_length = 8 + command_addr_byte_size + 4;
Greg Claytona63d08c2011-07-19 03:57:15 +0000690 MakeRequestPacketHeader (command, request_packet, command_length);
691 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
692 request_packet.PutHex32 (dst_len);
693 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000694 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Claytona63d08c2011-07-19 03:57:15 +0000695 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000696 lldb::offset_t offset = 8;
Greg Claytona63d08c2011-07-19 03:57:15 +0000697 uint32_t kdp_error = reply_packet.GetU32 (&offset);
698 uint32_t src_len = reply_packet.GetByteSize() - 12;
699
700 if (src_len > 0)
701 {
702 const void *src = reply_packet.GetData(&offset, src_len);
703 if (src)
704 {
705 ::memcpy (dst, src, src_len);
706 error.Clear();
707 return src_len;
708 }
709 }
710 if (kdp_error)
711 error.SetErrorStringWithFormat ("kdp read memory failed (error %u)", kdp_error);
712 else
713 error.SetErrorString ("kdp read memory failed");
714 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000715 else
716 {
717 error.SetErrorString ("failed to send packet");
718 }
Greg Claytona63d08c2011-07-19 03:57:15 +0000719 return 0;
720}
721
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000722
723uint32_t
724CommunicationKDP::SendRequestWriteMemory (lldb::addr_t addr,
725 const void *src,
726 uint32_t src_len,
727 Error &error)
728{
729 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
730 bool use_64 = (GetVersion() >= 11);
731 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
Greg Clayton5b882162011-07-21 01:12:01 +0000732 const CommandType command = use_64 ? KDP_WRITEMEM64 : KDP_WRITEMEM;
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000733 // Size is header + address size + uint32_t length
Greg Claytonb556c9b2012-10-12 23:24:05 +0000734 const uint32_t command_length = 8 + command_addr_byte_size + 4 + src_len;
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000735 MakeRequestPacketHeader (command, request_packet, command_length);
736 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
737 request_packet.PutHex32 (src_len);
Jason Molenda57656e72012-10-19 02:16:22 +0000738 request_packet.PutRawBytes(src, src_len);
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000739
740 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000741 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000742 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000743 lldb::offset_t offset = 8;
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000744 uint32_t kdp_error = reply_packet.GetU32 (&offset);
745 if (kdp_error)
746 error.SetErrorStringWithFormat ("kdp write memory failed (error %u)", kdp_error);
747 else
748 {
749 error.Clear();
750 return src_len;
751 }
752 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000753 else
754 {
755 error.SetErrorString ("failed to send packet");
756 }
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000757 return 0;
758}
759
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000760bool
761CommunicationKDP::SendRawRequest (uint8_t command_byte,
762 const void *src, // Raw packet payload bytes
763 uint32_t src_len, // Raw packet payload length
764 DataExtractor &reply_packet,
765 Error &error)
766{
767 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
768 // Size is header + address size + uint32_t length
769 const uint32_t command_length = 8 + src_len;
770 const CommandType command = (CommandType)command_byte;
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000771 MakeRequestPacketHeader (command, request_packet, command_length);
772 request_packet.PutRawBytes(src, src_len);
773
Greg Clayton0ee809b2013-02-14 19:11:23 +0000774 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000775 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000776 lldb::offset_t offset = 8;
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000777 uint32_t kdp_error = reply_packet.GetU32 (&offset);
Jason Molendace62fd72013-03-01 00:43:19 +0000778 if (kdp_error && (command_byte != KDP_DUMPINFO))
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000779 error.SetErrorStringWithFormat ("request packet 0x%8.8x failed (error %u)", command_byte, kdp_error);
780 else
781 {
782 error.Clear();
783 return true;
784 }
785 }
786 else
787 {
788 error.SetErrorString ("failed to send packet");
789 }
790 return false;
791}
792
793
Greg Claytona63d08c2011-07-19 03:57:15 +0000794const char *
795CommunicationKDP::GetCommandAsCString (uint8_t command)
796{
797 switch (command)
798 {
Greg Clayton5b882162011-07-21 01:12:01 +0000799 case KDP_CONNECT: return "KDP_CONNECT";
800 case KDP_DISCONNECT: return "KDP_DISCONNECT";
801 case KDP_HOSTINFO: return "KDP_HOSTINFO";
802 case KDP_VERSION: return "KDP_VERSION";
803 case KDP_MAXBYTES: return "KDP_MAXBYTES";
804 case KDP_READMEM: return "KDP_READMEM";
805 case KDP_WRITEMEM: return "KDP_WRITEMEM";
806 case KDP_READREGS: return "KDP_READREGS";
807 case KDP_WRITEREGS: return "KDP_WRITEREGS";
808 case KDP_LOAD: return "KDP_LOAD";
809 case KDP_IMAGEPATH: return "KDP_IMAGEPATH";
810 case KDP_SUSPEND: return "KDP_SUSPEND";
811 case KDP_RESUMECPUS: return "KDP_RESUMECPUS";
812 case KDP_EXCEPTION: return "KDP_EXCEPTION";
813 case KDP_TERMINATION: return "KDP_TERMINATION";
814 case KDP_BREAKPOINT_SET: return "KDP_BREAKPOINT_SET";
815 case KDP_BREAKPOINT_REMOVE: return "KDP_BREAKPOINT_REMOVE";
816 case KDP_REGIONS: return "KDP_REGIONS";
817 case KDP_REATTACH: return "KDP_REATTACH";
818 case KDP_HOSTREBOOT: return "KDP_HOSTREBOOT";
819 case KDP_READMEM64: return "KDP_READMEM64";
820 case KDP_WRITEMEM64: return "KDP_WRITEMEM64";
821 case KDP_BREAKPOINT_SET64: return "KDP_BREAKPOINT64_SET";
822 case KDP_BREAKPOINT_REMOVE64: return "KDP_BREAKPOINT64_REMOVE";
823 case KDP_KERNELVERSION: return "KDP_KERNELVERSION";
Jason Molendace62fd72013-03-01 00:43:19 +0000824 case KDP_READPHYSMEM64: return "KDP_READPHYSMEM64";
825 case KDP_WRITEPHYSMEM64: return "KDP_WRITEPHYSMEM64";
826 case KDP_READIOPORT: return "KDP_READIOPORT";
827 case KDP_WRITEIOPORT: return "KDP_WRITEIOPORT";
828 case KDP_READMSR64: return "KDP_READMSR64";
829 case KDP_WRITEMSR64: return "KDP_WRITEMSR64";
830 case KDP_DUMPINFO: return "KDP_DUMPINFO";
Greg Claytona63d08c2011-07-19 03:57:15 +0000831 }
832 return NULL;
833}
834
835void
836CommunicationKDP::DumpPacket (Stream &s, const void *data, uint32_t data_len)
837{
838 DataExtractor extractor (data, data_len, m_byte_order, m_addr_byte_size);
839 DumpPacket (s, extractor);
840}
841
842void
843CommunicationKDP::DumpPacket (Stream &s, const DataExtractor& packet)
844{
845 const char *error_desc = NULL;
846 if (packet.GetByteSize() < 8)
847 {
848 error_desc = "error: invalid packet (too short): ";
849 }
850 else
851 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000852 lldb::offset_t offset = 0;
Greg Claytona63d08c2011-07-19 03:57:15 +0000853 const uint8_t first_packet_byte = packet.GetU8 (&offset);
854 const uint8_t sequence_id = packet.GetU8 (&offset);
855 const uint16_t length = packet.GetU16 (&offset);
856 const uint32_t key = packet.GetU32 (&offset);
857 const CommandType command = ExtractCommand (first_packet_byte);
858 const char *command_name = GetCommandAsCString (command);
859 if (command_name)
860 {
861 const bool is_reply = ExtractIsReply(first_packet_byte);
Greg Clayton97d5cf02012-09-25 02:40:06 +0000862 s.Printf ("(running=%i) %s %24s: 0x%2.2x 0x%2.2x 0x%4.4x 0x%8.8x ",
863 IsRunning(),
864 is_reply ? "<--" : "-->",
865 command_name,
866 first_packet_byte,
Greg Claytona63d08c2011-07-19 03:57:15 +0000867 sequence_id,
868 length,
Greg Clayton97d5cf02012-09-25 02:40:06 +0000869 key);
Greg Claytona63d08c2011-07-19 03:57:15 +0000870
871 if (is_reply)
872 {
873 // Dump request reply packets
874 switch (command)
875 {
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000876 // Commands that return a single 32 bit error
Greg Clayton5b882162011-07-21 01:12:01 +0000877 case KDP_CONNECT:
878 case KDP_WRITEMEM:
879 case KDP_WRITEMEM64:
880 case KDP_BREAKPOINT_SET:
881 case KDP_BREAKPOINT_REMOVE:
882 case KDP_BREAKPOINT_SET64:
883 case KDP_BREAKPOINT_REMOVE64:
884 case KDP_WRITEREGS:
885 case KDP_LOAD:
Jason Molendace62fd72013-03-01 00:43:19 +0000886 case KDP_WRITEIOPORT:
887 case KDP_WRITEMSR64:
Greg Claytona63d08c2011-07-19 03:57:15 +0000888 {
889 const uint32_t error = packet.GetU32 (&offset);
890 s.Printf(" (error=0x%8.8x)", error);
891 }
892 break;
893
Greg Clayton5b882162011-07-21 01:12:01 +0000894 case KDP_DISCONNECT:
895 case KDP_REATTACH:
896 case KDP_HOSTREBOOT:
897 case KDP_SUSPEND:
898 case KDP_RESUMECPUS:
899 case KDP_EXCEPTION:
900 case KDP_TERMINATION:
Greg Claytona63d08c2011-07-19 03:57:15 +0000901 // No return value for the reply, just the header to ack
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000902 s.PutCString(" ()");
Greg Claytona63d08c2011-07-19 03:57:15 +0000903 break;
904
Greg Clayton5b882162011-07-21 01:12:01 +0000905 case KDP_HOSTINFO:
Greg Claytona63d08c2011-07-19 03:57:15 +0000906 {
907 const uint32_t cpu_mask = packet.GetU32 (&offset);
908 const uint32_t cpu_type = packet.GetU32 (&offset);
909 const uint32_t cpu_subtype = packet.GetU32 (&offset);
910 s.Printf(" (cpu_mask=0x%8.8x, cpu_type=0x%8.8x, cpu_subtype=0x%8.8x)", cpu_mask, cpu_type, cpu_subtype);
911 }
912 break;
913
Greg Clayton5b882162011-07-21 01:12:01 +0000914 case KDP_VERSION:
Greg Claytona63d08c2011-07-19 03:57:15 +0000915 {
916 const uint32_t version = packet.GetU32 (&offset);
917 const uint32_t feature = packet.GetU32 (&offset);
918 s.Printf(" (version=0x%8.8x, feature=0x%8.8x)", version, feature);
919 }
920 break;
921
Greg Clayton5b882162011-07-21 01:12:01 +0000922 case KDP_REGIONS:
Greg Claytona63d08c2011-07-19 03:57:15 +0000923 {
924 const uint32_t region_count = packet.GetU32 (&offset);
925 s.Printf(" (count = %u", region_count);
926 for (uint32_t i=0; i<region_count; ++i)
927 {
928 const addr_t region_addr = packet.GetPointer (&offset);
929 const uint32_t region_size = packet.GetU32 (&offset);
930 const uint32_t region_prot = packet.GetU32 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +0000931 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 +0000932 }
933 }
934 break;
935
Greg Clayton5b882162011-07-21 01:12:01 +0000936 case KDP_READMEM:
937 case KDP_READMEM64:
Jason Molendace62fd72013-03-01 00:43:19 +0000938 case KDP_READPHYSMEM64:
Greg Claytona63d08c2011-07-19 03:57:15 +0000939 {
940 const uint32_t error = packet.GetU32 (&offset);
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000941 const uint32_t count = packet.GetByteSize() - offset;
Greg Clayton5b442372011-07-29 22:26:36 +0000942 s.Printf(" (error = 0x%8.8x:\n", error);
Greg Claytona63d08c2011-07-19 03:57:15 +0000943 if (count > 0)
Greg Clayton07e66e32011-07-20 03:41:06 +0000944 packet.Dump (&s, // Stream to dump to
945 offset, // Offset within "packet"
946 eFormatBytesWithASCII, // Format to use
947 1, // Size of each item in bytes
948 count, // Number of items
949 16, // Number per line
950 m_last_read_memory_addr, // Don't show addresses before each line
951 0, 0); // No bitfields
Greg Claytona63d08c2011-07-19 03:57:15 +0000952 }
953 break;
954
Greg Clayton5b882162011-07-21 01:12:01 +0000955 case KDP_READREGS:
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000956 {
957 const uint32_t error = packet.GetU32 (&offset);
958 const uint32_t count = packet.GetByteSize() - offset;
Greg Clayton5b442372011-07-29 22:26:36 +0000959 s.Printf(" (error = 0x%8.8x regs:\n", error);
Greg Clayton0c54d8a2011-07-20 01:32:50 +0000960 if (count > 0)
961 packet.Dump (&s, // Stream to dump to
962 offset, // Offset within "packet"
963 eFormatHex, // Format to use
964 m_addr_byte_size, // Size of each item in bytes
965 count / m_addr_byte_size, // Number of items
966 16 / m_addr_byte_size, // Number per line
967 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
968 0, 0); // No bitfields
969 }
970 break;
971
Greg Clayton5b882162011-07-21 01:12:01 +0000972 case KDP_KERNELVERSION:
Greg Clayton07e66e32011-07-20 03:41:06 +0000973 {
974 const char *kernel_version = packet.PeekCStr(8);
975 s.Printf(" (version = \"%s\")", kernel_version);
976 }
977 break;
978
Greg Clayton5b882162011-07-21 01:12:01 +0000979 case KDP_MAXBYTES:
Greg Clayton07e66e32011-07-20 03:41:06 +0000980 {
981 const uint32_t max_bytes = packet.GetU32 (&offset);
982 s.Printf(" (max_bytes = 0x%8.8x (%u))", max_bytes, max_bytes);
983 }
984 break;
Greg Clayton5b882162011-07-21 01:12:01 +0000985 case KDP_IMAGEPATH:
Greg Clayton07e66e32011-07-20 03:41:06 +0000986 {
987 const char *path = packet.GetCStr(&offset);
988 s.Printf(" (path = \"%s\")", path);
989 }
990 break;
Jason Molendace62fd72013-03-01 00:43:19 +0000991
992 case KDP_READIOPORT:
993 case KDP_READMSR64:
994 {
995 const uint32_t error = packet.GetU32 (&offset);
996 const uint32_t count = packet.GetByteSize() - offset;
997 s.Printf(" (error = 0x%8.8x io:\n", error);
998 if (count > 0)
999 packet.Dump (&s, // Stream to dump to
1000 offset, // Offset within "packet"
1001 eFormatHex, // Format to use
1002 1, // Size of each item in bytes
1003 count, // Number of items
1004 16, // Number per line
1005 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1006 0, 0); // No bitfields
1007 }
1008 break;
1009 case KDP_DUMPINFO:
1010 {
1011 const uint32_t count = packet.GetByteSize() - offset;
1012 s.Printf(" (count = %u, bytes = \n", count);
1013 if (count > 0)
1014 packet.Dump (&s, // Stream to dump to
1015 offset, // Offset within "packet"
1016 eFormatHex, // Format to use
1017 1, // Size of each item in bytes
1018 count, // Number of items
1019 16, // Number per line
1020 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1021 0, 0); // No bitfields
1022
1023 }
1024 break;
1025
Greg Clayton07e66e32011-07-20 03:41:06 +00001026 default:
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001027 s.Printf(" (add support for dumping this packet reply!!!");
Greg Claytona63d08c2011-07-19 03:57:15 +00001028 break;
1029
1030 }
1031 }
1032 else
1033 {
1034 // Dump request packets
1035 switch (command)
1036 {
Greg Clayton5b882162011-07-21 01:12:01 +00001037 case KDP_CONNECT:
Greg Claytona63d08c2011-07-19 03:57:15 +00001038 {
Greg Clayton99e384c2014-01-10 00:31:10 +00001039 const uint16_t reply_port = ntohs(packet.GetU16 (&offset));
1040 const uint16_t exc_port = ntohs(packet.GetU16 (&offset));
Greg Clayton5b442372011-07-29 22:26:36 +00001041 s.Printf(" (reply_port = %u, exc_port = %u, greeting = \"%s\")", reply_port, exc_port, packet.GetCStr(&offset));
Greg Claytona63d08c2011-07-19 03:57:15 +00001042 }
1043 break;
1044
Greg Clayton5b882162011-07-21 01:12:01 +00001045 case KDP_DISCONNECT:
1046 case KDP_HOSTREBOOT:
1047 case KDP_HOSTINFO:
1048 case KDP_VERSION:
1049 case KDP_REGIONS:
1050 case KDP_KERNELVERSION:
1051 case KDP_MAXBYTES:
1052 case KDP_IMAGEPATH:
1053 case KDP_SUSPEND:
Greg Claytona63d08c2011-07-19 03:57:15 +00001054 // No args, just the header in the request...
Greg Clayton07e66e32011-07-20 03:41:06 +00001055 s.PutCString(" ()");
1056 break;
1057
Greg Clayton5b882162011-07-21 01:12:01 +00001058 case KDP_RESUMECPUS:
Greg Clayton07e66e32011-07-20 03:41:06 +00001059 {
1060 const uint32_t cpu_mask = packet.GetU32 (&offset);
1061 s.Printf(" (cpu_mask = 0x%8.8x)", cpu_mask);
1062 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001063 break;
1064
Greg Clayton5b882162011-07-21 01:12:01 +00001065 case KDP_READMEM:
Greg Claytona63d08c2011-07-19 03:57:15 +00001066 {
1067 const uint32_t addr = packet.GetU32 (&offset);
1068 const uint32_t size = packet.GetU32 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +00001069 s.Printf(" (addr = 0x%8.8x, size = %u)", addr, size);
Greg Clayton07e66e32011-07-20 03:41:06 +00001070 m_last_read_memory_addr = addr;
Greg Claytona63d08c2011-07-19 03:57:15 +00001071 }
1072 break;
1073
Greg Clayton5b882162011-07-21 01:12:01 +00001074 case KDP_WRITEMEM:
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001075 {
1076 const uint32_t addr = packet.GetU32 (&offset);
1077 const uint32_t size = packet.GetU32 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +00001078 s.Printf(" (addr = 0x%8.8x, size = %u, bytes = \n", addr, size);
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001079 if (size > 0)
1080 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
1081 }
1082 break;
1083
Greg Clayton5b882162011-07-21 01:12:01 +00001084 case KDP_READMEM64:
Greg Claytona63d08c2011-07-19 03:57:15 +00001085 {
1086 const uint64_t addr = packet.GetU64 (&offset);
1087 const uint32_t size = packet.GetU32 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +00001088 s.Printf(" (addr = 0x%16.16" PRIx64 ", size = %u)", addr, size);
Greg Clayton07e66e32011-07-20 03:41:06 +00001089 m_last_read_memory_addr = addr;
Greg Claytona63d08c2011-07-19 03:57:15 +00001090 }
1091 break;
1092
Jason Molendace62fd72013-03-01 00:43:19 +00001093 case KDP_READPHYSMEM64:
1094 {
1095 const uint64_t addr = packet.GetU64 (&offset);
1096 const uint32_t size = packet.GetU32 (&offset);
1097 const uint32_t lcpu = packet.GetU16 (&offset);
1098 s.Printf(" (addr = 0x%16.16llx, size = %u, lcpu = %u)", addr, size, lcpu);
1099 m_last_read_memory_addr = addr;
1100 }
1101 break;
1102
Greg Clayton5b882162011-07-21 01:12:01 +00001103 case KDP_WRITEMEM64:
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001104 {
1105 const uint64_t addr = packet.GetU64 (&offset);
1106 const uint32_t size = packet.GetU32 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +00001107 s.Printf(" (addr = 0x%16.16" PRIx64 ", size = %u, bytes = \n", addr, size);
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001108 if (size > 0)
1109 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
1110 }
1111 break;
1112
Jason Molendace62fd72013-03-01 00:43:19 +00001113 case KDP_WRITEPHYSMEM64:
1114 {
1115 const uint64_t addr = packet.GetU64 (&offset);
1116 const uint32_t size = packet.GetU32 (&offset);
1117 const uint32_t lcpu = packet.GetU16 (&offset);
1118 s.Printf(" (addr = 0x%16.16llx, size = %u, lcpu = %u, bytes = \n", addr, size, lcpu);
1119 if (size > 0)
1120 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
1121 }
1122 break;
1123
Greg Clayton5b882162011-07-21 01:12:01 +00001124 case KDP_READREGS:
Greg Claytona63d08c2011-07-19 03:57:15 +00001125 {
1126 const uint32_t cpu = packet.GetU32 (&offset);
1127 const uint32_t flavor = packet.GetU32 (&offset);
Greg Clayton5b442372011-07-29 22:26:36 +00001128 s.Printf(" (cpu = %u, flavor = %u)", cpu, flavor);
Greg Claytona63d08c2011-07-19 03:57:15 +00001129 }
1130 break;
1131
Greg Clayton5b882162011-07-21 01:12:01 +00001132 case KDP_WRITEREGS:
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001133 {
1134 const uint32_t cpu = packet.GetU32 (&offset);
1135 const uint32_t flavor = packet.GetU32 (&offset);
1136 const uint32_t nbytes = packet.GetByteSize() - offset;
Greg Clayton5b442372011-07-29 22:26:36 +00001137 s.Printf(" (cpu = %u, flavor = %u, regs = \n", cpu, flavor);
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001138 if (nbytes > 0)
1139 packet.Dump (&s, // Stream to dump to
1140 offset, // Offset within "packet"
1141 eFormatHex, // Format to use
1142 m_addr_byte_size, // Size of each item in bytes
1143 nbytes / m_addr_byte_size, // Number of items
1144 16 / m_addr_byte_size, // Number per line
1145 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1146 0, 0); // No bitfields
1147 }
1148 break;
1149
Greg Clayton0c54d8a2011-07-20 01:32:50 +00001150
Greg Clayton5b882162011-07-21 01:12:01 +00001151 case KDP_BREAKPOINT_SET:
1152 case KDP_BREAKPOINT_REMOVE:
Greg Clayton07e66e32011-07-20 03:41:06 +00001153 {
1154 const uint32_t addr = packet.GetU32 (&offset);
1155 s.Printf(" (addr = 0x%8.8x)", addr);
1156 }
1157 break;
1158
Greg Clayton5b882162011-07-21 01:12:01 +00001159 case KDP_BREAKPOINT_SET64:
1160 case KDP_BREAKPOINT_REMOVE64:
Greg Clayton07e66e32011-07-20 03:41:06 +00001161 {
1162 const uint64_t addr = packet.GetU64 (&offset);
Daniel Malead01b2952012-11-29 21:49:15 +00001163 s.Printf(" (addr = 0x%16.16" PRIx64 ")", addr);
Greg Clayton07e66e32011-07-20 03:41:06 +00001164 }
1165 break;
1166
1167
Greg Clayton5b882162011-07-21 01:12:01 +00001168 case KDP_LOAD:
Greg Clayton07e66e32011-07-20 03:41:06 +00001169 {
1170 const char *path = packet.GetCStr(&offset);
1171 s.Printf(" (path = \"%s\")", path);
1172 }
1173 break;
1174
Greg Clayton5b882162011-07-21 01:12:01 +00001175 case KDP_EXCEPTION:
Greg Clayton07e66e32011-07-20 03:41:06 +00001176 {
1177 const uint32_t count = packet.GetU32 (&offset);
1178
Greg Clayton07e66e32011-07-20 03:41:06 +00001179 for (uint32_t i=0; i<count; ++i)
1180 {
1181 const uint32_t cpu = packet.GetU32 (&offset);
1182 const uint32_t exc = packet.GetU32 (&offset);
1183 const uint32_t code = packet.GetU32 (&offset);
1184 const uint32_t subcode = packet.GetU32 (&offset);
1185 const char *exc_cstr = NULL;
1186 switch (exc)
1187 {
1188 case 1: exc_cstr = "EXC_BAD_ACCESS"; break;
1189 case 2: exc_cstr = "EXC_BAD_INSTRUCTION"; break;
1190 case 3: exc_cstr = "EXC_ARITHMETIC"; break;
1191 case 4: exc_cstr = "EXC_EMULATION"; break;
1192 case 5: exc_cstr = "EXC_SOFTWARE"; break;
1193 case 6: exc_cstr = "EXC_BREAKPOINT"; break;
1194 case 7: exc_cstr = "EXC_SYSCALL"; break;
1195 case 8: exc_cstr = "EXC_MACH_SYSCALL"; break;
1196 case 9: exc_cstr = "EXC_RPC_ALERT"; break;
1197 case 10: exc_cstr = "EXC_CRASH"; break;
1198 default:
1199 break;
1200 }
1201
Greg Clayton97d5cf02012-09-25 02:40:06 +00001202 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 +00001203 cpu, exc_cstr, exc, code, code, subcode, subcode);
1204 }
1205 }
1206 break;
1207
Greg Clayton5b882162011-07-21 01:12:01 +00001208 case KDP_TERMINATION:
Greg Clayton07e66e32011-07-20 03:41:06 +00001209 {
1210 const uint32_t term_code = packet.GetU32 (&offset);
1211 const uint32_t exit_code = packet.GetU32 (&offset);
1212 s.Printf(" (term_code = 0x%8.8x (%u), exit_code = 0x%8.8x (%u))", term_code, term_code, exit_code, exit_code);
1213 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001214 break;
1215
Greg Clayton5b882162011-07-21 01:12:01 +00001216 case KDP_REATTACH:
Greg Claytona63d08c2011-07-19 03:57:15 +00001217 {
Greg Clayton99e384c2014-01-10 00:31:10 +00001218 const uint16_t reply_port = ntohs(packet.GetU16 (&offset));
Greg Clayton5b442372011-07-29 22:26:36 +00001219 s.Printf(" (reply_port = %u)", reply_port);
Greg Claytona63d08c2011-07-19 03:57:15 +00001220 }
1221 break;
Jason Molendace62fd72013-03-01 00:43:19 +00001222
1223 case KDP_READMSR64:
1224 {
1225 const uint32_t address = packet.GetU32 (&offset);
1226 const uint16_t lcpu = packet.GetU16 (&offset);
1227 s.Printf(" (address=0x%8.8x, lcpu=0x%4.4x)", address, lcpu);
1228 }
1229 break;
1230
1231 case KDP_WRITEMSR64:
1232 {
1233 const uint32_t address = packet.GetU32 (&offset);
1234 const uint16_t lcpu = packet.GetU16 (&offset);
1235 const uint32_t nbytes = packet.GetByteSize() - offset;
1236 s.Printf(" (address=0x%8.8x, lcpu=0x%4.4x, nbytes=0x%8.8x)", lcpu, address, nbytes);
1237 if (nbytes > 0)
1238 packet.Dump (&s, // Stream to dump to
1239 offset, // Offset within "packet"
1240 eFormatHex, // Format to use
1241 1, // Size of each item in bytes
1242 nbytes, // Number of items
1243 16, // Number per line
1244 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1245 0, 0); // No bitfields
1246 }
1247 break;
1248
1249 case KDP_READIOPORT:
1250 {
1251 const uint16_t lcpu = packet.GetU16 (&offset);
1252 const uint16_t address = packet.GetU16 (&offset);
1253 const uint16_t nbytes = packet.GetU16 (&offset);
1254 s.Printf(" (lcpu=0x%4.4x, address=0x%4.4x, nbytes=%u)", lcpu, address, nbytes);
1255 }
1256 break;
1257
1258 case KDP_WRITEIOPORT:
1259 {
1260 const uint16_t lcpu = packet.GetU16 (&offset);
1261 const uint16_t address = packet.GetU16 (&offset);
1262 const uint16_t nbytes = packet.GetU16 (&offset);
1263 s.Printf(" (lcpu = %u, addr = 0x%4.4x, nbytes = %u, bytes = \n", lcpu, address, nbytes);
1264 if (nbytes > 0)
1265 packet.Dump (&s, // Stream to dump to
1266 offset, // Offset within "packet"
1267 eFormatHex, // Format to use
1268 1, // Size of each item in bytes
1269 nbytes, // Number of items
1270 16, // Number per line
1271 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1272 0, 0); // No bitfields
1273 }
1274 break;
1275
1276 case KDP_DUMPINFO:
1277 {
1278 const uint32_t count = packet.GetByteSize() - offset;
1279 s.Printf(" (count = %u, bytes = \n", count);
1280 if (count > 0)
1281 packet.Dump (&s, // Stream to dump to
1282 offset, // Offset within "packet"
1283 eFormatHex, // Format to use
1284 1, // Size of each item in bytes
1285 count, // Number of items
1286 16, // Number per line
1287 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1288 0, 0); // No bitfields
1289
1290 }
1291 break;
1292
1293 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001294 }
1295 }
1296 else
1297 {
1298 error_desc = "error: invalid packet command: ";
1299 }
1300 }
1301
1302 if (error_desc)
1303 {
1304 s.PutCString (error_desc);
1305
1306 packet.Dump (&s, // Stream to dump to
1307 0, // Offset into "packet"
1308 eFormatBytes, // Dump as hex bytes
1309 1, // Size of each item is 1 for single bytes
1310 packet.GetByteSize(), // Number of bytes
1311 UINT32_MAX, // Num bytes per line
1312 LLDB_INVALID_ADDRESS, // Base address
1313 0, 0); // Bitfield info set to not do anything bitfield related
1314 }
1315}
1316
1317uint32_t
1318CommunicationKDP::SendRequestReadRegisters (uint32_t cpu,
1319 uint32_t flavor,
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001320 void *dst,
Greg Claytona63d08c2011-07-19 03:57:15 +00001321 uint32_t dst_len,
1322 Error &error)
1323{
1324 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +00001325 const CommandType command = KDP_READREGS;
Greg Claytona63d08c2011-07-19 03:57:15 +00001326 // Size is header + 4 byte cpu and 4 byte flavor
1327 const uint32_t command_length = 8 + 4 + 4;
Greg Claytona63d08c2011-07-19 03:57:15 +00001328 MakeRequestPacketHeader (command, request_packet, command_length);
1329 request_packet.PutHex32 (cpu);
1330 request_packet.PutHex32 (flavor);
1331 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001332 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Claytona63d08c2011-07-19 03:57:15 +00001333 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001334 lldb::offset_t offset = 8;
Greg Claytona63d08c2011-07-19 03:57:15 +00001335 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1336 uint32_t src_len = reply_packet.GetByteSize() - 12;
1337
1338 if (src_len > 0)
1339 {
1340 const uint32_t bytes_to_copy = std::min<uint32_t>(src_len, dst_len);
1341 const void *src = reply_packet.GetData(&offset, bytes_to_copy);
1342 if (src)
1343 {
1344 ::memcpy (dst, src, bytes_to_copy);
1345 error.Clear();
1346 // Return the number of bytes we could have returned regardless if
1347 // we copied them or not, just so we know when things don't match up
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001348 return src_len;
Greg Claytona63d08c2011-07-19 03:57:15 +00001349 }
1350 }
1351 if (kdp_error)
1352 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error);
1353 else
1354 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u", cpu, flavor);
1355 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +00001356 else
1357 {
1358 error.SetErrorString ("failed to send packet");
1359 }
Greg Claytona63d08c2011-07-19 03:57:15 +00001360 return 0;
1361}
1362
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001363uint32_t
1364CommunicationKDP::SendRequestWriteRegisters (uint32_t cpu,
1365 uint32_t flavor,
1366 const void *src,
1367 uint32_t src_len,
1368 Error &error)
1369{
1370 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
1371 const CommandType command = KDP_WRITEREGS;
1372 // Size is header + 4 byte cpu and 4 byte flavor
Greg Clayton97d5cf02012-09-25 02:40:06 +00001373 const uint32_t command_length = 8 + 4 + 4 + src_len;
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001374 MakeRequestPacketHeader (command, request_packet, command_length);
1375 request_packet.PutHex32 (cpu);
1376 request_packet.PutHex32 (flavor);
1377 request_packet.Write(src, src_len);
1378 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001379 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001380 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001381 lldb::offset_t offset = 8;
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001382 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1383 if (kdp_error == 0)
1384 return src_len;
1385 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error);
1386 }
Greg Clayton1d19a2f2012-10-19 22:22:57 +00001387 else
1388 {
1389 error.SetErrorString ("failed to send packet");
1390 }
Greg Clayton4b1b8b32012-09-21 01:55:30 +00001391 return 0;
1392}
1393
Greg Clayton07e66e32011-07-20 03:41:06 +00001394
1395bool
Greg Clayton97d5cf02012-09-25 02:40:06 +00001396CommunicationKDP::SendRequestResume ()
Greg Clayton07e66e32011-07-20 03:41:06 +00001397{
Greg Clayton07e66e32011-07-20 03:41:06 +00001398 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +00001399 const CommandType command = KDP_RESUMECPUS;
Greg Clayton07e66e32011-07-20 03:41:06 +00001400 const uint32_t command_length = 12;
Greg Clayton07e66e32011-07-20 03:41:06 +00001401 MakeRequestPacketHeader (command, request_packet, command_length);
Greg Clayton97d5cf02012-09-25 02:40:06 +00001402 request_packet.PutHex32(GetCPUMask());
Greg Clayton07e66e32011-07-20 03:41:06 +00001403
1404 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001405 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton07e66e32011-07-20 03:41:06 +00001406 return true;
1407 return false;
1408}
1409
1410bool
1411CommunicationKDP::SendRequestBreakpoint (bool set, addr_t addr)
1412{
1413 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
1414 bool use_64 = (GetVersion() >= 11);
1415 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
Greg Clayton5b882162011-07-21 01:12:01 +00001416 const CommandType command = set ? (use_64 ? KDP_BREAKPOINT_SET64 : KDP_BREAKPOINT_SET ):
1417 (use_64 ? KDP_BREAKPOINT_REMOVE64 : KDP_BREAKPOINT_REMOVE);
Greg Clayton07e66e32011-07-20 03:41:06 +00001418
1419 const uint32_t command_length = 8 + command_addr_byte_size;
Greg Clayton07e66e32011-07-20 03:41:06 +00001420 MakeRequestPacketHeader (command, request_packet, command_length);
1421 request_packet.PutMaxHex64 (addr, command_addr_byte_size);
1422
1423 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001424 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton5b442372011-07-29 22:26:36 +00001425 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001426 lldb::offset_t offset = 8;
Greg Clayton5b442372011-07-29 22:26:36 +00001427 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1428 if (kdp_error == 0)
1429 return true;
1430 }
Greg Clayton07e66e32011-07-20 03:41:06 +00001431 return false;
1432}
1433
1434bool
1435CommunicationKDP::SendRequestSuspend ()
1436{
1437 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
Greg Clayton5b882162011-07-21 01:12:01 +00001438 const CommandType command = KDP_SUSPEND;
Greg Clayton07e66e32011-07-20 03:41:06 +00001439 const uint32_t command_length = 8;
Greg Clayton07e66e32011-07-20 03:41:06 +00001440 MakeRequestPacketHeader (command, request_packet, command_length);
1441 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +00001442 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton07e66e32011-07-20 03:41:06 +00001443 return true;
1444 return false;
1445}
1446