blob: 0a480cf280d9bafce4d6a0e8cfbfbdbdeb3dd2f5 [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
Greg Claytonf9765ac2011-07-15 03:27:12 +000010#include "CommunicationKDP.h"
11
12// C Includes
Jason Molenda4bd4e7e2012-09-29 04:02:01 +000013#include <errno.h>
Greg Claytonf9765ac2011-07-15 03:27:12 +000014#include <limits.h>
15#include <string.h>
16
17// C++ Includes
Greg Claytona63d08c2011-07-19 03:57:15 +000018
Greg Claytonf9765ac2011-07-15 03:27:12 +000019// Other libraries and framework includes
Greg Clayton4df8ddf2011-07-16 03:19:08 +000020#include "lldb/Core/DataBufferHeap.h"
Greg Clayton57508022011-07-15 16:31:38 +000021#include "lldb/Core/DataExtractor.h"
Zachary Turner9739a552017-03-03 23:52:09 +000022#include "lldb/Core/DumpDataExtractor.h"
Greg Claytona63d08c2011-07-19 03:57:15 +000023#include "lldb/Core/State.h"
Jason Molenda4bd4e7e2012-09-29 04:02:01 +000024#include "lldb/Core/UUID.h"
Greg Claytonf9765ac2011-07-15 03:27:12 +000025#include "lldb/Host/FileSpec.h"
26#include "lldb/Host/Host.h"
Greg Claytonf9765ac2011-07-15 03:27:12 +000027#include "lldb/Target/Process.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000028#include "lldb/Utility/Log.h"
Greg Claytonf9765ac2011-07-15 03:27:12 +000029
30// Project includes
31#include "ProcessKDPLog.h"
32
Greg Claytonf9765ac2011-07-15 03:27:12 +000033using namespace lldb;
34using namespace lldb_private;
35
36//----------------------------------------------------------------------
37// CommunicationKDP constructor
38//----------------------------------------------------------------------
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000039CommunicationKDP::CommunicationKDP(const char *comm_name)
Kate Stoneb9c1b512016-09-06 20:57:50 +000040 : Communication(comm_name), m_addr_byte_size(4),
41 m_byte_order(eByteOrderLittle), m_packet_timeout(5), m_sequence_mutex(),
42 m_is_running(false), m_session_key(0u), m_request_sequence_id(0u),
43 m_exception_sequence_id(0u), m_kdp_version_version(0u),
44 m_kdp_version_feature(0u), m_kdp_hostinfo_cpu_mask(0u),
45 m_kdp_hostinfo_cpu_type(0u), m_kdp_hostinfo_cpu_subtype(0u) {}
Greg Claytonf9765ac2011-07-15 03:27:12 +000046
47//----------------------------------------------------------------------
48// Destructor
49//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000050CommunicationKDP::~CommunicationKDP() {
51 if (IsConnected()) {
52 Disconnect();
53 }
Greg Claytonf9765ac2011-07-15 03:27:12 +000054}
55
Kate Stoneb9c1b512016-09-06 20:57:50 +000056bool CommunicationKDP::SendRequestPacket(
57 const PacketStreamType &request_packet) {
58 std::lock_guard<std::recursive_mutex> guard(m_sequence_mutex);
59 return SendRequestPacketNoLock(request_packet);
Greg Claytonf9765ac2011-07-15 03:27:12 +000060}
61
Greg Clayton4df8ddf2011-07-16 03:19:08 +000062#if 0
63typedef struct {
64 uint8_t request; // Either: CommandType | ePacketTypeRequest, or CommandType | ePacketTypeReply
65 uint8_t sequence;
66 uint16_t length; // Length of entire packet including this header
67 uint32_t key; // Session key
68} kdp_hdr_t;
69#endif
70
Kate Stoneb9c1b512016-09-06 20:57:50 +000071void CommunicationKDP::MakeRequestPacketHeader(CommandType request_type,
72 PacketStreamType &request_packet,
73 uint16_t request_length) {
74 request_packet.Clear();
75 request_packet.PutHex8(request_type |
76 ePacketTypeRequest); // Set the request type
77 request_packet.PutHex8(m_request_sequence_id++); // Sequence number
78 request_packet.PutHex16(
79 request_length); // Length of the packet including this header
80 request_packet.PutHex32(m_session_key); // Session key
Greg Claytonf9765ac2011-07-15 03:27:12 +000081}
82
Kate Stoneb9c1b512016-09-06 20:57:50 +000083bool CommunicationKDP::SendRequestAndGetReply(
84 const CommandType command, const PacketStreamType &request_packet,
85 DataExtractor &reply_packet) {
86 if (IsRunning()) {
87 Log *log(ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PACKETS));
88 if (log) {
89 PacketStreamType log_strm;
90 DumpPacket(log_strm, request_packet.GetData(), request_packet.GetSize());
91 log->Printf("error: kdp running, not sending packet: %.*s",
92 (uint32_t)log_strm.GetSize(), log_strm.GetData());
Greg Clayton97d5cf02012-09-25 02:40:06 +000093 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 return false;
95 }
Greg Clayton4df8ddf2011-07-16 03:19:08 +000096
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 std::lock_guard<std::recursive_mutex> guard(m_sequence_mutex);
Greg Clayton97d5cf02012-09-25 02:40:06 +000098#ifdef LLDB_CONFIGURATION_DEBUG
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 // NOTE: this only works for packets that are in native endian byte order
100 assert(request_packet.GetSize() ==
101 *((uint16_t *)(request_packet.GetData() + 2)));
Greg Clayton97d5cf02012-09-25 02:40:06 +0000102#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 lldb::offset_t offset = 1;
104 const uint32_t num_retries = 3;
105 for (uint32_t i = 0; i < num_retries; ++i) {
106 if (SendRequestPacketNoLock(request_packet)) {
107 const uint8_t request_sequence_id = (uint8_t)request_packet.GetData()[1];
108 while (1) {
109 if (WaitForPacketWithTimeoutMicroSecondsNoLock(
Pavel Labath5cddd602016-11-02 10:13:54 +0000110 reply_packet,
111 std::chrono::microseconds(GetPacketTimeout()).count())) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000112 offset = 0;
113 const uint8_t reply_command = reply_packet.GetU8(&offset);
114 const uint8_t reply_sequence_id = reply_packet.GetU8(&offset);
115 if (request_sequence_id == reply_sequence_id) {
116 // The sequent ID was correct, now verify we got the response we
117 // were looking for
118 if ((reply_command & eCommandTypeMask) == command) {
119 // Success
120 if (command == KDP_RESUMECPUS)
121 m_is_running.SetValue(true, eBroadcastAlways);
122 return true;
123 } else {
124 // Failed to get the correct response, bail
125 reply_packet.Clear();
126 return false;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000127 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 } else if (reply_sequence_id > request_sequence_id) {
129 // Sequence ID was greater than the sequence ID of the packet we
130 // sent, something
131 // is really wrong...
132 reply_packet.Clear();
133 return false;
134 } else {
135 // The reply sequence ID was less than our current packet's sequence
136 // ID
137 // so we should keep trying to get a response because this was a
138 // response
139 // for a previous packet that we must have retried.
140 }
141 } else {
142 // Break and retry sending the packet as we didn't get a response due
143 // to timeout
144 break;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000145 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 }
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000147 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 }
149 reply_packet.Clear();
150 return false;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000151}
Greg Claytonf9765ac2011-07-15 03:27:12 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153bool CommunicationKDP::SendRequestPacketNoLock(
154 const PacketStreamType &request_packet) {
155 if (IsConnected()) {
156 const char *packet_data = request_packet.GetData();
157 const size_t packet_size = request_packet.GetSize();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000158
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 Log *log(ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PACKETS));
160 if (log) {
161 PacketStreamType log_strm;
162 DumpPacket(log_strm, packet_data, packet_size);
163 log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData());
Greg Clayton57508022011-07-15 16:31:38 +0000164 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 ConnectionStatus status = eConnectionStatusSuccess;
166
167 size_t bytes_written = Write(packet_data, packet_size, status, NULL);
168
169 if (bytes_written == packet_size)
170 return true;
171
172 if (log)
173 log->Printf("error: failed to send packet entire packet %" PRIu64
174 " of %" PRIu64 " bytes sent",
175 (uint64_t)bytes_written, (uint64_t)packet_size);
176 }
177 return false;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000178}
179
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180bool CommunicationKDP::GetSequenceMutex(
181 std::unique_lock<std::recursive_mutex> &lock) {
182 return (lock = std::unique_lock<std::recursive_mutex>(m_sequence_mutex,
183 std::try_to_lock))
184 .owns_lock();
Greg Claytonf9765ac2011-07-15 03:27:12 +0000185}
186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187bool CommunicationKDP::WaitForNotRunningPrivate(
188 const std::chrono::microseconds &timeout) {
189 return m_is_running.WaitForValueEqualTo(false, timeout, NULL);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000190}
191
192size_t
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193CommunicationKDP::WaitForPacketWithTimeoutMicroSeconds(DataExtractor &packet,
194 uint32_t timeout_usec) {
195 std::lock_guard<std::recursive_mutex> guard(m_sequence_mutex);
196 return WaitForPacketWithTimeoutMicroSecondsNoLock(packet, timeout_usec);
Greg Claytonf9765ac2011-07-15 03:27:12 +0000197}
198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199size_t CommunicationKDP::WaitForPacketWithTimeoutMicroSecondsNoLock(
200 DataExtractor &packet, uint32_t timeout_usec) {
201 uint8_t buffer[8192];
202 Error error;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000203
Pavel Labath250858a2017-02-06 21:46:22 +0000204 Log *log(ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PACKETS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000205
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 // Check for a packet from our cache first without trying any reading...
207 if (CheckForPacket(NULL, 0, packet))
208 return packet.GetByteSize();
209
210 bool timed_out = false;
211 while (IsConnected() && !timed_out) {
212 lldb::ConnectionStatus status = eConnectionStatusNoConnection;
Pavel Labathce8d6d92016-11-25 14:43:37 +0000213 size_t bytes_read = Read(buffer, sizeof(buffer),
214 timeout_usec == UINT32_MAX
215 ? Timeout<std::micro>(llvm::None)
216 : std::chrono::microseconds(timeout_usec),
217 status, &error);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218
Pavel Labath250858a2017-02-06 21:46:22 +0000219 LLDB_LOGV(log,
220 "Read (buffer, sizeof(buffer), timeout_usec = 0x{0:x}, "
221 "status = {1}, error = {2}) => bytes_read = {4}",
222 timeout_usec,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223 Communication::ConnectionStatusAsCString(status),
Pavel Labath250858a2017-02-06 21:46:22 +0000224 error, bytes_read);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000225
226 if (bytes_read > 0) {
227 if (CheckForPacket(buffer, bytes_read, packet))
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000228 return packet.GetByteSize();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000229 } else {
230 switch (status) {
231 case eConnectionStatusInterrupted:
232 case eConnectionStatusTimedOut:
233 timed_out = true;
234 break;
235 case eConnectionStatusSuccess:
236 // printf ("status = success but error = %s\n",
237 // error.AsCString("<invalid>"));
238 break;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000239
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240 case eConnectionStatusEndOfFile:
241 case eConnectionStatusNoConnection:
242 case eConnectionStatusLostConnection:
243 case eConnectionStatusError:
244 Disconnect();
245 break;
246 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000247 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000248 }
249 packet.Clear();
250 return 0;
Greg Claytonf9765ac2011-07-15 03:27:12 +0000251}
252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253bool CommunicationKDP::CheckForPacket(const uint8_t *src, size_t src_len,
254 DataExtractor &packet) {
255 // Put the packet data into the buffer in a thread safe fashion
256 std::lock_guard<std::recursive_mutex> guard(m_bytes_mutex);
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000257
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258 Log *log(ProcessKDPLog::GetLogIfAllCategoriesSet(KDP_LOG_PACKETS));
Greg Claytonf9765ac2011-07-15 03:27:12 +0000259
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260 if (src && src_len > 0) {
261 if (log && log->GetVerbose()) {
262 PacketStreamType log_strm;
Zachary Turner9739a552017-03-03 23:52:09 +0000263 DumpHexBytes(&log_strm, src, src_len, UINT32_MAX, LLDB_INVALID_ADDRESS);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264 log->Printf("CommunicationKDP::%s adding %u bytes: %s", __FUNCTION__,
265 (uint32_t)src_len, log_strm.GetData());
Greg Claytonf9765ac2011-07-15 03:27:12 +0000266 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 m_bytes.append((const char *)src, src_len);
268 }
Greg Claytonf9765ac2011-07-15 03:27:12 +0000269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270 // Make sure we at least have enough bytes for a packet header
271 const size_t bytes_available = m_bytes.size();
272 if (bytes_available >= 8) {
273 packet.SetData(&m_bytes[0], bytes_available, m_byte_order);
274 lldb::offset_t offset = 0;
275 uint8_t reply_command = packet.GetU8(&offset);
276 switch (reply_command) {
277 case ePacketTypeRequest | KDP_EXCEPTION:
278 case ePacketTypeRequest | KDP_TERMINATION:
279 // We got an exception request, so be sure to send an ACK
280 {
281 PacketStreamType request_ack_packet(Stream::eBinary, m_addr_byte_size,
282 m_byte_order);
283 // Set the reply but and make the ACK packet
284 request_ack_packet.PutHex8(reply_command | ePacketTypeReply);
285 request_ack_packet.PutHex8(packet.GetU8(&offset));
286 request_ack_packet.PutHex16(packet.GetU16(&offset));
287 request_ack_packet.PutHex32(packet.GetU32(&offset));
288 m_is_running.SetValue(false, eBroadcastAlways);
289 // Ack to the exception or termination
290 SendRequestPacketNoLock(request_ack_packet);
291 }
292 // Fall through to case below to get packet contents
293 LLVM_FALLTHROUGH;
294 case ePacketTypeReply | KDP_CONNECT:
295 case ePacketTypeReply | KDP_DISCONNECT:
296 case ePacketTypeReply | KDP_HOSTINFO:
297 case ePacketTypeReply | KDP_VERSION:
298 case ePacketTypeReply | KDP_MAXBYTES:
299 case ePacketTypeReply | KDP_READMEM:
300 case ePacketTypeReply | KDP_WRITEMEM:
301 case ePacketTypeReply | KDP_READREGS:
302 case ePacketTypeReply | KDP_WRITEREGS:
303 case ePacketTypeReply | KDP_LOAD:
304 case ePacketTypeReply | KDP_IMAGEPATH:
305 case ePacketTypeReply | KDP_SUSPEND:
306 case ePacketTypeReply | KDP_RESUMECPUS:
307 case ePacketTypeReply | KDP_BREAKPOINT_SET:
308 case ePacketTypeReply | KDP_BREAKPOINT_REMOVE:
309 case ePacketTypeReply | KDP_REGIONS:
310 case ePacketTypeReply | KDP_REATTACH:
311 case ePacketTypeReply | KDP_HOSTREBOOT:
312 case ePacketTypeReply | KDP_READMEM64:
313 case ePacketTypeReply | KDP_WRITEMEM64:
314 case ePacketTypeReply | KDP_BREAKPOINT_SET64:
315 case ePacketTypeReply | KDP_BREAKPOINT_REMOVE64:
316 case ePacketTypeReply | KDP_KERNELVERSION:
317 case ePacketTypeReply | KDP_READPHYSMEM64:
318 case ePacketTypeReply | KDP_WRITEPHYSMEM64:
319 case ePacketTypeReply | KDP_READIOPORT:
320 case ePacketTypeReply | KDP_WRITEIOPORT:
321 case ePacketTypeReply | KDP_READMSR64:
322 case ePacketTypeReply | KDP_WRITEMSR64:
323 case ePacketTypeReply | KDP_DUMPINFO: {
324 offset = 2;
325 const uint16_t length = packet.GetU16(&offset);
326 if (length <= bytes_available) {
327 // We have an entire packet ready, we need to copy the data
328 // bytes into a buffer that will be owned by the packet and
329 // erase the bytes from our communcation buffer "m_bytes"
330 packet.SetData(DataBufferSP(new DataBufferHeap(&m_bytes[0], length)));
331 m_bytes.erase(0, length);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000332
Kate Stoneb9c1b512016-09-06 20:57:50 +0000333 if (log) {
334 PacketStreamType log_strm;
335 DumpPacket(log_strm, packet);
336
337 log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData());
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000338 }
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000339 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000340 }
341 } break;
342
343 default:
344 // Unrecognized reply command byte, erase this byte and try to get back on
345 // track
346 if (log)
347 log->Printf("CommunicationKDP::%s: tossing junk byte: 0x%2.2x",
348 __FUNCTION__, (uint8_t)m_bytes[0]);
349 m_bytes.erase(0, 1);
350 break;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000351 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000352 }
353 packet.Clear();
354 return false;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000355}
356
Kate Stoneb9c1b512016-09-06 20:57:50 +0000357bool CommunicationKDP::SendRequestConnect(uint16_t reply_port,
358 uint16_t exc_port,
359 const char *greeting) {
360 PacketStreamType request_packet(Stream::eBinary, m_addr_byte_size,
361 m_byte_order);
362 if (greeting == NULL)
363 greeting = "";
364
365 const CommandType command = KDP_CONNECT;
366 // Length is 82 uint16_t and the length of the greeting C string with the
367 // terminating NULL
368 const uint32_t command_length = 8 + 2 + 2 + ::strlen(greeting) + 1;
369 MakeRequestPacketHeader(command, request_packet, command_length);
370 // Always send connect ports as little endian
371 request_packet.SetByteOrder(eByteOrderLittle);
372 request_packet.PutHex16(htons(reply_port));
373 request_packet.PutHex16(htons(exc_port));
374 request_packet.SetByteOrder(m_byte_order);
375 request_packet.PutCString(greeting);
376 DataExtractor reply_packet;
377 return SendRequestAndGetReply(command, request_packet, reply_packet);
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000378}
379
Kate Stoneb9c1b512016-09-06 20:57:50 +0000380void CommunicationKDP::ClearKDPSettings() {
381 m_request_sequence_id = 0;
382 m_kdp_version_version = 0;
383 m_kdp_version_feature = 0;
384 m_kdp_hostinfo_cpu_mask = 0;
385 m_kdp_hostinfo_cpu_type = 0;
386 m_kdp_hostinfo_cpu_subtype = 0;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000387}
388
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389bool CommunicationKDP::SendRequestReattach(uint16_t reply_port) {
390 PacketStreamType request_packet(Stream::eBinary, m_addr_byte_size,
391 m_byte_order);
392 const CommandType command = KDP_REATTACH;
393 // Length is 8 bytes for the header plus 2 bytes for the reply UDP port
394 const uint32_t command_length = 8 + 2;
395 MakeRequestPacketHeader(command, request_packet, command_length);
396 // Always send connect ports as little endian
397 request_packet.SetByteOrder(eByteOrderLittle);
398 request_packet.PutHex16(htons(reply_port));
399 request_packet.SetByteOrder(m_byte_order);
400 DataExtractor reply_packet;
401 if (SendRequestAndGetReply(command, request_packet, reply_packet)) {
402 // Reset the sequence ID to zero for reattach
403 ClearKDPSettings();
404 lldb::offset_t offset = 4;
405 m_session_key = reply_packet.GetU32(&offset);
406 return true;
407 }
408 return false;
409}
410
411uint32_t CommunicationKDP::GetVersion() {
412 if (!VersionIsValid())
413 SendRequestVersion();
414 return m_kdp_version_version;
415}
416
417uint32_t CommunicationKDP::GetFeatureFlags() {
418 if (!VersionIsValid())
419 SendRequestVersion();
420 return m_kdp_version_feature;
421}
422
423bool CommunicationKDP::SendRequestVersion() {
424 PacketStreamType request_packet(Stream::eBinary, m_addr_byte_size,
425 m_byte_order);
426 const CommandType command = KDP_VERSION;
427 const uint32_t command_length = 8;
428 MakeRequestPacketHeader(command, request_packet, command_length);
429 DataExtractor reply_packet;
430 if (SendRequestAndGetReply(command, request_packet, reply_packet)) {
431 lldb::offset_t offset = 8;
432 m_kdp_version_version = reply_packet.GetU32(&offset);
433 m_kdp_version_feature = reply_packet.GetU32(&offset);
434 return true;
435 }
436 return false;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000437}
438
Greg Clayton5b882162011-07-21 01:12:01 +0000439#if 0 // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
440const char *
441CommunicationKDP::GetImagePath ()
442{
443 if (m_image_path.empty())
444 SendRequestImagePath();
445 return m_image_path.c_str();
446}
447
448bool
449CommunicationKDP::SendRequestImagePath ()
450{
451 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
452 const CommandType command = KDP_IMAGEPATH;
453 const uint32_t command_length = 8;
Greg Clayton5b882162011-07-21 01:12:01 +0000454 MakeRequestPacketHeader (command, request_packet, command_length);
455 DataExtractor reply_packet;
Greg Clayton0ee809b2013-02-14 19:11:23 +0000456 if (SendRequestAndGetReply (command, request_packet, reply_packet))
Greg Clayton5b882162011-07-21 01:12:01 +0000457 {
458 const char *path = reply_packet.PeekCStr(8);
459 if (path && path[0])
460 m_kernel_version.assign (path);
461 return true;
462 }
463 return false;
464}
465#endif
466
Kate Stoneb9c1b512016-09-06 20:57:50 +0000467uint32_t CommunicationKDP::GetCPUMask() {
468 if (!HostInfoIsValid())
469 SendRequestHostInfo();
470 return m_kdp_hostinfo_cpu_mask;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000471}
472
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473uint32_t CommunicationKDP::GetCPUType() {
474 if (!HostInfoIsValid())
475 SendRequestHostInfo();
476 return m_kdp_hostinfo_cpu_type;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000477}
478
Kate Stoneb9c1b512016-09-06 20:57:50 +0000479uint32_t CommunicationKDP::GetCPUSubtype() {
480 if (!HostInfoIsValid())
481 SendRequestHostInfo();
482 return m_kdp_hostinfo_cpu_subtype;
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000483}
484
Kate Stoneb9c1b512016-09-06 20:57:50 +0000485lldb_private::UUID CommunicationKDP::GetUUID() {
486 UUID uuid;
487 if (GetKernelVersion() == NULL)
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000488 return uuid;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000489
490 if (m_kernel_version.find("UUID=") == std::string::npos)
491 return uuid;
492
493 size_t p = m_kernel_version.find("UUID=") + strlen("UUID=");
494 std::string uuid_str = m_kernel_version.substr(p, 36);
495 if (uuid_str.size() < 32)
496 return uuid;
497
498 if (uuid.SetFromCString(uuid_str.c_str()) == 0) {
499 UUID invalid_uuid;
500 return invalid_uuid;
501 }
502
503 return uuid;
Jason Molenda4bd4e7e2012-09-29 04:02:01 +0000504}
505
Kate Stoneb9c1b512016-09-06 20:57:50 +0000506bool CommunicationKDP::RemoteIsEFI() {
507 if (GetKernelVersion() == NULL)
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000508 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000509 if (strncmp(m_kernel_version.c_str(), "EFI", 3) == 0)
Greg Clayton4df8ddf2011-07-16 03:19:08 +0000510 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000511 else
Greg Clayton1d19a2f2012-10-19 22:22:57 +0000512 return false;
513}
514
Kate Stoneb9c1b512016-09-06 20:57:50 +0000515bool CommunicationKDP::RemoteIsDarwinKernel() {
516 if (GetKernelVersion() == NULL)
517 return false;
518 if (m_kernel_version.find("Darwin Kernel") != std::string::npos)
519 return true;
520 else
Greg Clayton07e66e32011-07-20 03:41:06 +0000521 return false;
522}
523
Kate Stoneb9c1b512016-09-06 20:57:50 +0000524lldb::addr_t CommunicationKDP::GetLoadAddress() {
525 if (GetKernelVersion() == NULL)
526 return LLDB_INVALID_ADDRESS;
Greg Clayton07e66e32011-07-20 03:41:06 +0000527
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528 if (m_kernel_version.find("stext=") == std::string::npos)
529 return LLDB_INVALID_ADDRESS;
530 size_t p = m_kernel_version.find("stext=") + strlen("stext=");
531 if (m_kernel_version[p] != '0' || m_kernel_version[p + 1] != 'x')
532 return LLDB_INVALID_ADDRESS;
533
534 addr_t kernel_load_address;
535 errno = 0;
536 kernel_load_address = ::strtoul(m_kernel_version.c_str() + p, NULL, 16);
537 if (errno != 0 || kernel_load_address == 0)
538 return LLDB_INVALID_ADDRESS;
539
540 return kernel_load_address;
541}
542
543bool CommunicationKDP::SendRequestHostInfo() {
544 PacketStreamType request_packet(Stream::eBinary, m_addr_byte_size,
545 m_byte_order);
546 const CommandType command = KDP_HOSTINFO;
547 const uint32_t command_length = 8;
548 MakeRequestPacketHeader(command, request_packet, command_length);
549 DataExtractor reply_packet;
550 if (SendRequestAndGetReply(command, request_packet, reply_packet)) {
551 lldb::offset_t offset = 8;
552 m_kdp_hostinfo_cpu_mask = reply_packet.GetU32(&offset);
553 m_kdp_hostinfo_cpu_type = reply_packet.GetU32(&offset);
554 m_kdp_hostinfo_cpu_subtype = reply_packet.GetU32(&offset);
555
556 ArchSpec kernel_arch;
557 kernel_arch.SetArchitecture(eArchTypeMachO, m_kdp_hostinfo_cpu_type,
558 m_kdp_hostinfo_cpu_subtype);
559
560 m_addr_byte_size = kernel_arch.GetAddressByteSize();
561 m_byte_order = kernel_arch.GetByteOrder();
562 return true;
563 }
564 return false;
565}
566
567const char *CommunicationKDP::GetKernelVersion() {
568 if (m_kernel_version.empty())
569 SendRequestKernelVersion();
570 return m_kernel_version.c_str();
571}
572
573bool CommunicationKDP::SendRequestKernelVersion() {
574 PacketStreamType request_packet(Stream::eBinary, m_addr_byte_size,
575 m_byte_order);
576 const CommandType command = KDP_KERNELVERSION;
577 const uint32_t command_length = 8;
578 MakeRequestPacketHeader(command, request_packet, command_length);
579 DataExtractor reply_packet;
580 if (SendRequestAndGetReply(command, request_packet, reply_packet)) {
581 const char *kernel_version_cstr = reply_packet.PeekCStr(8);
582 if (kernel_version_cstr && kernel_version_cstr[0])
583 m_kernel_version.assign(kernel_version_cstr);
584 return true;
585 }
586 return false;
587}
588
589bool CommunicationKDP::SendRequestDisconnect() {
590 PacketStreamType request_packet(Stream::eBinary, m_addr_byte_size,
591 m_byte_order);
592 const CommandType command = KDP_DISCONNECT;
593 const uint32_t command_length = 8;
594 MakeRequestPacketHeader(command, request_packet, command_length);
595 DataExtractor reply_packet;
596 if (SendRequestAndGetReply(command, request_packet, reply_packet)) {
597 // Are we supposed to get a reply for disconnect?
598 }
599 ClearKDPSettings();
600 return true;
601}
602
603uint32_t CommunicationKDP::SendRequestReadMemory(lldb::addr_t addr, void *dst,
604 uint32_t dst_len,
605 Error &error) {
606 PacketStreamType request_packet(Stream::eBinary, m_addr_byte_size,
607 m_byte_order);
608 bool use_64 = (GetVersion() >= 11);
609 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
610 const CommandType command = use_64 ? KDP_READMEM64 : KDP_READMEM;
611 // Size is header + address size + uint32_t length
612 const uint32_t command_length = 8 + command_addr_byte_size + 4;
613 MakeRequestPacketHeader(command, request_packet, command_length);
614 request_packet.PutMaxHex64(addr, command_addr_byte_size);
615 request_packet.PutHex32(dst_len);
616 DataExtractor reply_packet;
617 if (SendRequestAndGetReply(command, request_packet, reply_packet)) {
618 lldb::offset_t offset = 8;
619 uint32_t kdp_error = reply_packet.GetU32(&offset);
620 uint32_t src_len = reply_packet.GetByteSize() - 12;
621
622 if (src_len > 0) {
623 const void *src = reply_packet.GetData(&offset, src_len);
624 if (src) {
625 ::memcpy(dst, src, src_len);
626 error.Clear();
627 return src_len;
628 }
Greg Clayton5b442372011-07-29 22:26:36 +0000629 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000630 if (kdp_error)
631 error.SetErrorStringWithFormat("kdp read memory failed (error %u)",
632 kdp_error);
633 else
634 error.SetErrorString("kdp read memory failed");
635 } else {
636 error.SetErrorString("failed to send packet");
637 }
638 return 0;
Greg Clayton07e66e32011-07-20 03:41:06 +0000639}
640
Kate Stoneb9c1b512016-09-06 20:57:50 +0000641uint32_t CommunicationKDP::SendRequestWriteMemory(lldb::addr_t addr,
642 const void *src,
643 uint32_t src_len,
644 Error &error) {
645 PacketStreamType request_packet(Stream::eBinary, m_addr_byte_size,
646 m_byte_order);
647 bool use_64 = (GetVersion() >= 11);
648 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
649 const CommandType command = use_64 ? KDP_WRITEMEM64 : KDP_WRITEMEM;
650 // Size is header + address size + uint32_t length
651 const uint32_t command_length = 8 + command_addr_byte_size + 4 + src_len;
652 MakeRequestPacketHeader(command, request_packet, command_length);
653 request_packet.PutMaxHex64(addr, command_addr_byte_size);
654 request_packet.PutHex32(src_len);
655 request_packet.PutRawBytes(src, src_len);
656
657 DataExtractor reply_packet;
658 if (SendRequestAndGetReply(command, request_packet, reply_packet)) {
659 lldb::offset_t offset = 8;
660 uint32_t kdp_error = reply_packet.GetU32(&offset);
661 if (kdp_error)
662 error.SetErrorStringWithFormat("kdp write memory failed (error %u)",
663 kdp_error);
664 else {
665 error.Clear();
666 return src_len;
667 }
668 } else {
669 error.SetErrorString("failed to send packet");
670 }
671 return 0;
Greg Clayton07e66e32011-07-20 03:41:06 +0000672}
673
Kate Stoneb9c1b512016-09-06 20:57:50 +0000674bool CommunicationKDP::SendRawRequest(
675 uint8_t command_byte,
676 const void *src, // Raw packet payload bytes
677 uint32_t src_len, // Raw packet payload length
678 DataExtractor &reply_packet, Error &error) {
679 PacketStreamType request_packet(Stream::eBinary, m_addr_byte_size,
680 m_byte_order);
681 // Size is header + address size + uint32_t length
682 const uint32_t command_length = 8 + src_len;
683 const CommandType command = (CommandType)command_byte;
684 MakeRequestPacketHeader(command, request_packet, command_length);
685 request_packet.PutRawBytes(src, src_len);
686
687 if (SendRequestAndGetReply(command, request_packet, reply_packet)) {
688 lldb::offset_t offset = 8;
689 uint32_t kdp_error = reply_packet.GetU32(&offset);
690 if (kdp_error && (command_byte != KDP_DUMPINFO))
691 error.SetErrorStringWithFormat("request packet 0x%8.8x failed (error %u)",
692 command_byte, kdp_error);
693 else {
694 error.Clear();
695 return true;
696 }
697 } else {
698 error.SetErrorString("failed to send packet");
699 }
700 return false;
701}
702
703const char *CommunicationKDP::GetCommandAsCString(uint8_t command) {
704 switch (command) {
705 case KDP_CONNECT:
706 return "KDP_CONNECT";
707 case KDP_DISCONNECT:
708 return "KDP_DISCONNECT";
709 case KDP_HOSTINFO:
710 return "KDP_HOSTINFO";
711 case KDP_VERSION:
712 return "KDP_VERSION";
713 case KDP_MAXBYTES:
714 return "KDP_MAXBYTES";
715 case KDP_READMEM:
716 return "KDP_READMEM";
717 case KDP_WRITEMEM:
718 return "KDP_WRITEMEM";
719 case KDP_READREGS:
720 return "KDP_READREGS";
721 case KDP_WRITEREGS:
722 return "KDP_WRITEREGS";
723 case KDP_LOAD:
724 return "KDP_LOAD";
725 case KDP_IMAGEPATH:
726 return "KDP_IMAGEPATH";
727 case KDP_SUSPEND:
728 return "KDP_SUSPEND";
729 case KDP_RESUMECPUS:
730 return "KDP_RESUMECPUS";
731 case KDP_EXCEPTION:
732 return "KDP_EXCEPTION";
733 case KDP_TERMINATION:
734 return "KDP_TERMINATION";
735 case KDP_BREAKPOINT_SET:
736 return "KDP_BREAKPOINT_SET";
737 case KDP_BREAKPOINT_REMOVE:
738 return "KDP_BREAKPOINT_REMOVE";
739 case KDP_REGIONS:
740 return "KDP_REGIONS";
741 case KDP_REATTACH:
742 return "KDP_REATTACH";
743 case KDP_HOSTREBOOT:
744 return "KDP_HOSTREBOOT";
745 case KDP_READMEM64:
746 return "KDP_READMEM64";
747 case KDP_WRITEMEM64:
748 return "KDP_WRITEMEM64";
749 case KDP_BREAKPOINT_SET64:
750 return "KDP_BREAKPOINT64_SET";
751 case KDP_BREAKPOINT_REMOVE64:
752 return "KDP_BREAKPOINT64_REMOVE";
753 case KDP_KERNELVERSION:
754 return "KDP_KERNELVERSION";
755 case KDP_READPHYSMEM64:
756 return "KDP_READPHYSMEM64";
757 case KDP_WRITEPHYSMEM64:
758 return "KDP_WRITEPHYSMEM64";
759 case KDP_READIOPORT:
760 return "KDP_READIOPORT";
761 case KDP_WRITEIOPORT:
762 return "KDP_WRITEIOPORT";
763 case KDP_READMSR64:
764 return "KDP_READMSR64";
765 case KDP_WRITEMSR64:
766 return "KDP_WRITEMSR64";
767 case KDP_DUMPINFO:
768 return "KDP_DUMPINFO";
769 }
770 return NULL;
771}
772
773void CommunicationKDP::DumpPacket(Stream &s, const void *data,
774 uint32_t data_len) {
775 DataExtractor extractor(data, data_len, m_byte_order, m_addr_byte_size);
776 DumpPacket(s, extractor);
777}
778
779void CommunicationKDP::DumpPacket(Stream &s, const DataExtractor &packet) {
780 const char *error_desc = NULL;
781 if (packet.GetByteSize() < 8) {
782 error_desc = "error: invalid packet (too short): ";
783 } else {
784 lldb::offset_t offset = 0;
785 const uint8_t first_packet_byte = packet.GetU8(&offset);
786 const uint8_t sequence_id = packet.GetU8(&offset);
787 const uint16_t length = packet.GetU16(&offset);
788 const uint32_t key = packet.GetU32(&offset);
789 const CommandType command = ExtractCommand(first_packet_byte);
790 const char *command_name = GetCommandAsCString(command);
791 if (command_name) {
792 const bool is_reply = ExtractIsReply(first_packet_byte);
793 s.Printf("(running=%i) %s %24s: 0x%2.2x 0x%2.2x 0x%4.4x 0x%8.8x ",
794 IsRunning(), is_reply ? "<--" : "-->", command_name,
795 first_packet_byte, sequence_id, length, key);
796
797 if (is_reply) {
798 // Dump request reply packets
799 switch (command) {
800 // Commands that return a single 32 bit error
801 case KDP_CONNECT:
802 case KDP_WRITEMEM:
803 case KDP_WRITEMEM64:
804 case KDP_BREAKPOINT_SET:
805 case KDP_BREAKPOINT_REMOVE:
806 case KDP_BREAKPOINT_SET64:
807 case KDP_BREAKPOINT_REMOVE64:
808 case KDP_WRITEREGS:
809 case KDP_LOAD:
810 case KDP_WRITEIOPORT:
811 case KDP_WRITEMSR64: {
812 const uint32_t error = packet.GetU32(&offset);
813 s.Printf(" (error=0x%8.8x)", error);
814 } break;
815
816 case KDP_DISCONNECT:
817 case KDP_REATTACH:
818 case KDP_HOSTREBOOT:
819 case KDP_SUSPEND:
820 case KDP_RESUMECPUS:
821 case KDP_EXCEPTION:
822 case KDP_TERMINATION:
823 // No return value for the reply, just the header to ack
824 s.PutCString(" ()");
825 break;
826
827 case KDP_HOSTINFO: {
828 const uint32_t cpu_mask = packet.GetU32(&offset);
829 const uint32_t cpu_type = packet.GetU32(&offset);
830 const uint32_t cpu_subtype = packet.GetU32(&offset);
831 s.Printf(" (cpu_mask=0x%8.8x, cpu_type=0x%8.8x, cpu_subtype=0x%8.8x)",
832 cpu_mask, cpu_type, cpu_subtype);
833 } break;
834
835 case KDP_VERSION: {
836 const uint32_t version = packet.GetU32(&offset);
837 const uint32_t feature = packet.GetU32(&offset);
838 s.Printf(" (version=0x%8.8x, feature=0x%8.8x)", version, feature);
839 } break;
840
841 case KDP_REGIONS: {
842 const uint32_t region_count = packet.GetU32(&offset);
843 s.Printf(" (count = %u", region_count);
844 for (uint32_t i = 0; i < region_count; ++i) {
845 const addr_t region_addr = packet.GetPointer(&offset);
846 const uint32_t region_size = packet.GetU32(&offset);
847 const uint32_t region_prot = packet.GetU32(&offset);
848 s.Printf("\n\tregion[%" PRIu64 "] = { range = [0x%16.16" PRIx64
849 " - 0x%16.16" PRIx64 "), size = 0x%8.8x, prot = %s }",
850 region_addr, region_addr, region_addr + region_size,
851 region_size, GetPermissionsAsCString(region_prot));
852 }
853 } break;
854
855 case KDP_READMEM:
856 case KDP_READMEM64:
857 case KDP_READPHYSMEM64: {
858 const uint32_t error = packet.GetU32(&offset);
859 const uint32_t count = packet.GetByteSize() - offset;
860 s.Printf(" (error = 0x%8.8x:\n", error);
861 if (count > 0)
862 packet.Dump(&s, // Stream to dump to
863 offset, // Offset within "packet"
864 eFormatBytesWithASCII, // Format to use
865 1, // Size of each item in bytes
866 count, // Number of items
867 16, // Number per line
868 m_last_read_memory_addr, // Don't show addresses before
869 // each line
870 0, 0); // No bitfields
871 } break;
872
873 case KDP_READREGS: {
874 const uint32_t error = packet.GetU32(&offset);
875 const uint32_t count = packet.GetByteSize() - offset;
876 s.Printf(" (error = 0x%8.8x regs:\n", error);
877 if (count > 0)
878 packet.Dump(
879 &s, // Stream to dump to
880 offset, // Offset within "packet"
881 eFormatHex, // Format to use
882 m_addr_byte_size, // Size of each item in bytes
883 count / m_addr_byte_size, // Number of items
884 16 / m_addr_byte_size, // Number per line
885 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
886 0, 0); // No bitfields
887 } break;
888
889 case KDP_KERNELVERSION: {
890 const char *kernel_version = packet.PeekCStr(8);
891 s.Printf(" (version = \"%s\")", kernel_version);
892 } break;
893
894 case KDP_MAXBYTES: {
895 const uint32_t max_bytes = packet.GetU32(&offset);
896 s.Printf(" (max_bytes = 0x%8.8x (%u))", max_bytes, max_bytes);
897 } break;
898 case KDP_IMAGEPATH: {
899 const char *path = packet.GetCStr(&offset);
900 s.Printf(" (path = \"%s\")", path);
901 } break;
902
903 case KDP_READIOPORT:
904 case KDP_READMSR64: {
905 const uint32_t error = packet.GetU32(&offset);
906 const uint32_t count = packet.GetByteSize() - offset;
907 s.Printf(" (error = 0x%8.8x io:\n", error);
908 if (count > 0)
909 packet.Dump(
910 &s, // Stream to dump to
911 offset, // Offset within "packet"
912 eFormatHex, // Format to use
913 1, // Size of each item in bytes
914 count, // Number of items
915 16, // Number per line
916 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
917 0, 0); // No bitfields
918 } break;
919 case KDP_DUMPINFO: {
920 const uint32_t count = packet.GetByteSize() - offset;
921 s.Printf(" (count = %u, bytes = \n", count);
922 if (count > 0)
923 packet.Dump(
924 &s, // Stream to dump to
925 offset, // Offset within "packet"
926 eFormatHex, // Format to use
927 1, // Size of each item in bytes
928 count, // Number of items
929 16, // Number per line
930 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
931 0, 0); // No bitfields
932
933 } break;
934
935 default:
936 s.Printf(" (add support for dumping this packet reply!!!");
937 break;
938 }
939 } else {
940 // Dump request packets
941 switch (command) {
942 case KDP_CONNECT: {
943 const uint16_t reply_port = ntohs(packet.GetU16(&offset));
944 const uint16_t exc_port = ntohs(packet.GetU16(&offset));
945 s.Printf(" (reply_port = %u, exc_port = %u, greeting = \"%s\")",
946 reply_port, exc_port, packet.GetCStr(&offset));
947 } break;
948
949 case KDP_DISCONNECT:
950 case KDP_HOSTREBOOT:
951 case KDP_HOSTINFO:
952 case KDP_VERSION:
953 case KDP_REGIONS:
954 case KDP_KERNELVERSION:
955 case KDP_MAXBYTES:
956 case KDP_IMAGEPATH:
957 case KDP_SUSPEND:
958 // No args, just the header in the request...
959 s.PutCString(" ()");
960 break;
961
962 case KDP_RESUMECPUS: {
963 const uint32_t cpu_mask = packet.GetU32(&offset);
964 s.Printf(" (cpu_mask = 0x%8.8x)", cpu_mask);
965 } break;
966
967 case KDP_READMEM: {
968 const uint32_t addr = packet.GetU32(&offset);
969 const uint32_t size = packet.GetU32(&offset);
970 s.Printf(" (addr = 0x%8.8x, size = %u)", addr, size);
971 m_last_read_memory_addr = addr;
972 } break;
973
974 case KDP_WRITEMEM: {
975 const uint32_t addr = packet.GetU32(&offset);
976 const uint32_t size = packet.GetU32(&offset);
977 s.Printf(" (addr = 0x%8.8x, size = %u, bytes = \n", addr, size);
978 if (size > 0)
Zachary Turner9739a552017-03-03 23:52:09 +0000979 DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000980 } break;
981
982 case KDP_READMEM64: {
983 const uint64_t addr = packet.GetU64(&offset);
984 const uint32_t size = packet.GetU32(&offset);
985 s.Printf(" (addr = 0x%16.16" PRIx64 ", size = %u)", addr, size);
986 m_last_read_memory_addr = addr;
987 } break;
988
989 case KDP_READPHYSMEM64: {
990 const uint64_t addr = packet.GetU64(&offset);
991 const uint32_t size = packet.GetU32(&offset);
992 const uint32_t lcpu = packet.GetU16(&offset);
993 s.Printf(" (addr = 0x%16.16llx, size = %u, lcpu = %u)", addr, size,
994 lcpu);
995 m_last_read_memory_addr = addr;
996 } break;
997
998 case KDP_WRITEMEM64: {
999 const uint64_t addr = packet.GetU64(&offset);
1000 const uint32_t size = packet.GetU32(&offset);
1001 s.Printf(" (addr = 0x%16.16" PRIx64 ", size = %u, bytes = \n", addr,
1002 size);
1003 if (size > 0)
Zachary Turner9739a552017-03-03 23:52:09 +00001004 DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001005 } break;
1006
1007 case KDP_WRITEPHYSMEM64: {
1008 const uint64_t addr = packet.GetU64(&offset);
1009 const uint32_t size = packet.GetU32(&offset);
1010 const uint32_t lcpu = packet.GetU16(&offset);
1011 s.Printf(" (addr = 0x%16.16llx, size = %u, lcpu = %u, bytes = \n",
1012 addr, size, lcpu);
1013 if (size > 0)
Zachary Turner9739a552017-03-03 23:52:09 +00001014 DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001015 } break;
1016
1017 case KDP_READREGS: {
1018 const uint32_t cpu = packet.GetU32(&offset);
1019 const uint32_t flavor = packet.GetU32(&offset);
1020 s.Printf(" (cpu = %u, flavor = %u)", cpu, flavor);
1021 } break;
1022
1023 case KDP_WRITEREGS: {
1024 const uint32_t cpu = packet.GetU32(&offset);
1025 const uint32_t flavor = packet.GetU32(&offset);
1026 const uint32_t nbytes = packet.GetByteSize() - offset;
1027 s.Printf(" (cpu = %u, flavor = %u, regs = \n", cpu, flavor);
1028 if (nbytes > 0)
1029 packet.Dump(
1030 &s, // Stream to dump to
1031 offset, // Offset within "packet"
1032 eFormatHex, // Format to use
1033 m_addr_byte_size, // Size of each item in bytes
1034 nbytes / m_addr_byte_size, // Number of items
1035 16 / m_addr_byte_size, // Number per line
1036 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1037 0, 0); // No bitfields
1038 } break;
1039
1040 case KDP_BREAKPOINT_SET:
1041 case KDP_BREAKPOINT_REMOVE: {
1042 const uint32_t addr = packet.GetU32(&offset);
1043 s.Printf(" (addr = 0x%8.8x)", addr);
1044 } break;
1045
1046 case KDP_BREAKPOINT_SET64:
1047 case KDP_BREAKPOINT_REMOVE64: {
1048 const uint64_t addr = packet.GetU64(&offset);
1049 s.Printf(" (addr = 0x%16.16" PRIx64 ")", addr);
1050 } break;
1051
1052 case KDP_LOAD: {
1053 const char *path = packet.GetCStr(&offset);
1054 s.Printf(" (path = \"%s\")", path);
1055 } break;
1056
1057 case KDP_EXCEPTION: {
1058 const uint32_t count = packet.GetU32(&offset);
1059
1060 for (uint32_t i = 0; i < count; ++i) {
1061 const uint32_t cpu = packet.GetU32(&offset);
1062 const uint32_t exc = packet.GetU32(&offset);
1063 const uint32_t code = packet.GetU32(&offset);
1064 const uint32_t subcode = packet.GetU32(&offset);
1065 const char *exc_cstr = NULL;
1066 switch (exc) {
1067 case 1:
1068 exc_cstr = "EXC_BAD_ACCESS";
1069 break;
1070 case 2:
1071 exc_cstr = "EXC_BAD_INSTRUCTION";
1072 break;
1073 case 3:
1074 exc_cstr = "EXC_ARITHMETIC";
1075 break;
1076 case 4:
1077 exc_cstr = "EXC_EMULATION";
1078 break;
1079 case 5:
1080 exc_cstr = "EXC_SOFTWARE";
1081 break;
1082 case 6:
1083 exc_cstr = "EXC_BREAKPOINT";
1084 break;
1085 case 7:
1086 exc_cstr = "EXC_SYSCALL";
1087 break;
1088 case 8:
1089 exc_cstr = "EXC_MACH_SYSCALL";
1090 break;
1091 case 9:
1092 exc_cstr = "EXC_RPC_ALERT";
1093 break;
1094 case 10:
1095 exc_cstr = "EXC_CRASH";
1096 break;
1097 default:
1098 break;
1099 }
1100
1101 s.Printf("{ cpu = 0x%8.8x, exc = %s (%u), code = %u (0x%8.8x), "
1102 "subcode = %u (0x%8.8x)} ",
1103 cpu, exc_cstr, exc, code, code, subcode, subcode);
1104 }
1105 } break;
1106
1107 case KDP_TERMINATION: {
1108 const uint32_t term_code = packet.GetU32(&offset);
1109 const uint32_t exit_code = packet.GetU32(&offset);
1110 s.Printf(" (term_code = 0x%8.8x (%u), exit_code = 0x%8.8x (%u))",
1111 term_code, term_code, exit_code, exit_code);
1112 } break;
1113
1114 case KDP_REATTACH: {
1115 const uint16_t reply_port = ntohs(packet.GetU16(&offset));
1116 s.Printf(" (reply_port = %u)", reply_port);
1117 } break;
1118
1119 case KDP_READMSR64: {
1120 const uint32_t address = packet.GetU32(&offset);
1121 const uint16_t lcpu = packet.GetU16(&offset);
1122 s.Printf(" (address=0x%8.8x, lcpu=0x%4.4x)", address, lcpu);
1123 } break;
1124
1125 case KDP_WRITEMSR64: {
1126 const uint32_t address = packet.GetU32(&offset);
1127 const uint16_t lcpu = packet.GetU16(&offset);
1128 const uint32_t nbytes = packet.GetByteSize() - offset;
1129 s.Printf(" (address=0x%8.8x, lcpu=0x%4.4x, nbytes=0x%8.8x)", lcpu,
1130 address, nbytes);
1131 if (nbytes > 0)
1132 packet.Dump(
1133 &s, // Stream to dump to
1134 offset, // Offset within "packet"
1135 eFormatHex, // Format to use
1136 1, // Size of each item in bytes
1137 nbytes, // Number of items
1138 16, // Number per line
1139 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1140 0, 0); // No bitfields
1141 } break;
1142
1143 case KDP_READIOPORT: {
1144 const uint16_t lcpu = packet.GetU16(&offset);
1145 const uint16_t address = packet.GetU16(&offset);
1146 const uint16_t nbytes = packet.GetU16(&offset);
1147 s.Printf(" (lcpu=0x%4.4x, address=0x%4.4x, nbytes=%u)", lcpu, address,
1148 nbytes);
1149 } break;
1150
1151 case KDP_WRITEIOPORT: {
1152 const uint16_t lcpu = packet.GetU16(&offset);
1153 const uint16_t address = packet.GetU16(&offset);
1154 const uint16_t nbytes = packet.GetU16(&offset);
1155 s.Printf(" (lcpu = %u, addr = 0x%4.4x, nbytes = %u, bytes = \n", lcpu,
1156 address, nbytes);
1157 if (nbytes > 0)
1158 packet.Dump(
1159 &s, // Stream to dump to
1160 offset, // Offset within "packet"
1161 eFormatHex, // Format to use
1162 1, // Size of each item in bytes
1163 nbytes, // Number of items
1164 16, // Number per line
1165 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1166 0, 0); // No bitfields
1167 } break;
1168
1169 case KDP_DUMPINFO: {
1170 const uint32_t count = packet.GetByteSize() - offset;
1171 s.Printf(" (count = %u, bytes = \n", count);
1172 if (count > 0)
1173 packet.Dump(
1174 &s, // Stream to dump to
1175 offset, // Offset within "packet"
1176 eFormatHex, // Format to use
1177 1, // Size of each item in bytes
1178 count, // Number of items
1179 16, // Number per line
1180 LLDB_INVALID_ADDRESS, // Don't show addresses before each line
1181 0, 0); // No bitfields
1182
1183 } break;
1184 }
1185 }
1186 } else {
1187 error_desc = "error: invalid packet command: ";
1188 }
1189 }
1190
1191 if (error_desc) {
1192 s.PutCString(error_desc);
1193
1194 packet.Dump(&s, // Stream to dump to
1195 0, // Offset into "packet"
1196 eFormatBytes, // Dump as hex bytes
1197 1, // Size of each item is 1 for single bytes
1198 packet.GetByteSize(), // Number of bytes
1199 UINT32_MAX, // Num bytes per line
1200 LLDB_INVALID_ADDRESS, // Base address
1201 0, 0); // Bitfield info set to not do anything bitfield related
1202 }
1203}
1204
1205uint32_t CommunicationKDP::SendRequestReadRegisters(uint32_t cpu,
1206 uint32_t flavor, void *dst,
1207 uint32_t dst_len,
1208 Error &error) {
1209 PacketStreamType request_packet(Stream::eBinary, m_addr_byte_size,
1210 m_byte_order);
1211 const CommandType command = KDP_READREGS;
1212 // Size is header + 4 byte cpu and 4 byte flavor
1213 const uint32_t command_length = 8 + 4 + 4;
1214 MakeRequestPacketHeader(command, request_packet, command_length);
1215 request_packet.PutHex32(cpu);
1216 request_packet.PutHex32(flavor);
1217 DataExtractor reply_packet;
1218 if (SendRequestAndGetReply(command, request_packet, reply_packet)) {
1219 lldb::offset_t offset = 8;
1220 uint32_t kdp_error = reply_packet.GetU32(&offset);
1221 uint32_t src_len = reply_packet.GetByteSize() - 12;
1222
1223 if (src_len > 0) {
1224 const uint32_t bytes_to_copy = std::min<uint32_t>(src_len, dst_len);
1225 const void *src = reply_packet.GetData(&offset, bytes_to_copy);
1226 if (src) {
1227 ::memcpy(dst, src, bytes_to_copy);
1228 error.Clear();
1229 // Return the number of bytes we could have returned regardless if
1230 // we copied them or not, just so we know when things don't match up
1231 return src_len;
1232 }
1233 }
1234 if (kdp_error)
1235 error.SetErrorStringWithFormat(
1236 "failed to read kdp registers for cpu %u flavor %u (error %u)", cpu,
1237 flavor, kdp_error);
1238 else
1239 error.SetErrorStringWithFormat(
1240 "failed to read kdp registers for cpu %u flavor %u", cpu, flavor);
1241 } else {
1242 error.SetErrorString("failed to send packet");
1243 }
1244 return 0;
1245}
1246
1247uint32_t CommunicationKDP::SendRequestWriteRegisters(uint32_t cpu,
1248 uint32_t flavor,
1249 const void *src,
1250 uint32_t src_len,
1251 Error &error) {
1252 PacketStreamType request_packet(Stream::eBinary, m_addr_byte_size,
1253 m_byte_order);
1254 const CommandType command = KDP_WRITEREGS;
1255 // Size is header + 4 byte cpu and 4 byte flavor
1256 const uint32_t command_length = 8 + 4 + 4 + src_len;
1257 MakeRequestPacketHeader(command, request_packet, command_length);
1258 request_packet.PutHex32(cpu);
1259 request_packet.PutHex32(flavor);
1260 request_packet.Write(src, src_len);
1261 DataExtractor reply_packet;
1262 if (SendRequestAndGetReply(command, request_packet, reply_packet)) {
1263 lldb::offset_t offset = 8;
1264 uint32_t kdp_error = reply_packet.GetU32(&offset);
1265 if (kdp_error == 0)
1266 return src_len;
1267 error.SetErrorStringWithFormat(
1268 "failed to read kdp registers for cpu %u flavor %u (error %u)", cpu,
1269 flavor, kdp_error);
1270 } else {
1271 error.SetErrorString("failed to send packet");
1272 }
1273 return 0;
1274}
1275
1276bool CommunicationKDP::SendRequestResume() {
1277 PacketStreamType request_packet(Stream::eBinary, m_addr_byte_size,
1278 m_byte_order);
1279 const CommandType command = KDP_RESUMECPUS;
1280 const uint32_t command_length = 12;
1281 MakeRequestPacketHeader(command, request_packet, command_length);
1282 request_packet.PutHex32(GetCPUMask());
1283
1284 DataExtractor reply_packet;
1285 if (SendRequestAndGetReply(command, request_packet, reply_packet))
1286 return true;
1287 return false;
1288}
1289
1290bool CommunicationKDP::SendRequestBreakpoint(bool set, addr_t addr) {
1291 PacketStreamType request_packet(Stream::eBinary, m_addr_byte_size,
1292 m_byte_order);
1293 bool use_64 = (GetVersion() >= 11);
1294 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
1295 const CommandType command =
1296 set ? (use_64 ? KDP_BREAKPOINT_SET64 : KDP_BREAKPOINT_SET)
1297 : (use_64 ? KDP_BREAKPOINT_REMOVE64 : KDP_BREAKPOINT_REMOVE);
1298
1299 const uint32_t command_length = 8 + command_addr_byte_size;
1300 MakeRequestPacketHeader(command, request_packet, command_length);
1301 request_packet.PutMaxHex64(addr, command_addr_byte_size);
1302
1303 DataExtractor reply_packet;
1304 if (SendRequestAndGetReply(command, request_packet, reply_packet)) {
1305 lldb::offset_t offset = 8;
1306 uint32_t kdp_error = reply_packet.GetU32(&offset);
1307 if (kdp_error == 0)
1308 return true;
1309 }
1310 return false;
1311}
1312
1313bool CommunicationKDP::SendRequestSuspend() {
1314 PacketStreamType request_packet(Stream::eBinary, m_addr_byte_size,
1315 m_byte_order);
1316 const CommandType command = KDP_SUSPEND;
1317 const uint32_t command_length = 8;
1318 MakeRequestPacketHeader(command, request_packet, command_length);
1319 DataExtractor reply_packet;
1320 if (SendRequestAndGetReply(command, request_packet, reply_packet))
1321 return true;
1322 return false;
1323}