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