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