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