34 m_is_running(false), m_session_key(0u), m_request_sequence_id(0u),
35 m_exception_sequence_id(0u), m_kdp_version_version(0u),
36 m_kdp_version_feature(0u), m_kdp_hostinfo_cpu_mask(0u),
37 m_kdp_hostinfo_cpu_type(0u), m_kdp_hostinfo_cpu_subtype(0u) {}
55 request_packet.
Clear();
56 request_packet.
PutHex8(request_type |
72 LLDB_LOGF(log,
"error: kdp running, not sending packet: %.*s",
80 assert(request_packet.
GetSize() ==
84 for (
uint32_t i = 0; i < num_retries; ++i) {
86 const uint8_t request_sequence_id = (uint8_t)request_packet.
GetData()[1];
92 const uint8_t reply_command = reply_packet.
GetU8(&offset);
93 const uint8_t reply_sequence_id = reply_packet.
GetU8(&offset);
94 if (request_sequence_id == reply_sequence_id) {
104 reply_packet.
Clear();
107 }
else if (reply_sequence_id > request_sequence_id) {
110 reply_packet.
Clear();
126 reply_packet.
Clear();
133 const char *packet_data = request_packet.
GetData();
134 const size_t packet_size = request_packet.
GetSize();
139 DumpPacket(log_strm, packet_data, packet_size);
144 size_t bytes_written =
Write(packet_data, packet_size, status, NULL);
146 if (bytes_written == packet_size)
150 "error: failed to send packet entire packet %" PRIu64
151 " of %" PRIu64
" bytes sent",
152 (uint64_t)bytes_written, (uint64_t)packet_size);
158 std::unique_lock<std::recursive_mutex> &lock) {
165 const std::chrono::microseconds &timeout) {
178 uint8_t buffer[8192];
187 bool timed_out =
false;
190 size_t bytes_read =
Read(buffer,
sizeof(buffer),
193 : std::chrono::microseconds(timeout_usec),
197 "Read (buffer, sizeof(buffer), timeout_usec = 0x{0:x}, "
198 "status = {1}, error = {2}) => bytes_read = {4}",
199 timeout_usec, Communication::ConnectionStatusAsString(status),
202 if (bytes_read > 0) {
236 if (src && src_len > 0) {
241 LLDB_LOGF(log,
"CommunicationKDP::%s adding %u bytes: %s", __FUNCTION__,
244 m_bytes.append((
const char *)src, src_len);
248 const size_t bytes_available =
m_bytes.size();
249 if (bytes_available >= 8) {
252 uint8_t reply_command = packet.
GetU8(&offset);
253 switch (reply_command) {
303 if (length <= bytes_available) {
324 LLDB_LOGF(log,
"CommunicationKDP::%s: tossing junk byte: 0x%2.2x",
325 __FUNCTION__, (uint8_t)
m_bytes[0]);
336 const char *greeting) {
339 if (greeting == NULL)
345 const uint32_t command_length = 8 + 2 + 2 + ::strlen(greeting) + 1;
349 request_packet.
PutHex16(htons(reply_port));
350 request_packet.
PutHex16(htons(exc_port));
371 const uint32_t command_length = 8 + 2;
375 request_packet.
PutHex16(htons(reply_port));
444 if (uuid_str.size() < 32)
477 addr_t kernel_load_address;
480 if (errno != 0 || kernel_load_address == 0)
483 return kernel_load_address;
524 const char *kernel_version_cstr = reply_packet.
PeekCStr(8);
525 if (kernel_version_cstr && kernel_version_cstr[0])
552 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
555 const uint32_t command_length = 8 + command_addr_byte_size + 4;
557 request_packet.
PutMaxHex64(addr, command_addr_byte_size);
566 const void *src = reply_packet.
GetData(&offset, src_len);
568 ::memcpy(dst, src, src_len);
574 error.SetErrorStringWithFormat(
"kdp read memory failed (error %u)",
577 error.SetErrorString(
"kdp read memory failed");
579 error.SetErrorString(
"failed to send packet");
591 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
594 const uint32_t command_length = 8 + command_addr_byte_size + 4 + src_len;
596 request_packet.
PutMaxHex64(addr, command_addr_byte_size);
605 error.SetErrorStringWithFormat(
"kdp write memory failed (error %u)",
612 error.SetErrorString(
"failed to send packet");
618 uint8_t command_byte,
625 const uint32_t command_length = 8 + src_len;
634 error.SetErrorStringWithFormat(
"request packet 0x%8.8x failed (error %u)",
635 command_byte, kdp_error);
641 error.SetErrorString(
"failed to send packet");
649 return "KDP_CONNECT";
651 return "KDP_DISCONNECT";
653 return "KDP_HOSTINFO";
655 return "KDP_VERSION";
657 return "KDP_MAXBYTES";
659 return "KDP_READMEM";
661 return "KDP_WRITEMEM";
663 return "KDP_READREGS";
665 return "KDP_WRITEREGS";
669 return "KDP_IMAGEPATH";
671 return "KDP_SUSPEND";
673 return "KDP_RESUMECPUS";
675 return "KDP_EXCEPTION";
677 return "KDP_TERMINATION";
679 return "KDP_BREAKPOINT_SET";
681 return "KDP_BREAKPOINT_REMOVE";
683 return "KDP_REGIONS";
685 return "KDP_REATTACH";
687 return "KDP_HOSTREBOOT";
689 return "KDP_READMEM64";
691 return "KDP_WRITEMEM64";
693 return "KDP_BREAKPOINT64_SET";
695 return "KDP_BREAKPOINT64_REMOVE";
697 return "KDP_KERNELVERSION";
699 return "KDP_READPHYSMEM64";
701 return "KDP_WRITEPHYSMEM64";
703 return "KDP_READIOPORT";
705 return "KDP_WRITEIOPORT";
707 return "KDP_READMSR64";
709 return "KDP_WRITEMSR64";
711 return "KDP_DUMPINFO";
723 const char *error_desc = NULL;
725 error_desc =
"error: invalid packet (too short): ";
728 const uint8_t first_packet_byte = packet.
GetU8(&offset);
729 const uint8_t sequence_id = packet.
GetU8(&offset);
736 s.
Printf(
"(running=%i) %s %24s: 0x%2.2x 0x%2.2x 0x%4.4x 0x%8.8x ",
737 IsRunning(), is_reply ?
"<--" :
"-->", command_name,
738 first_packet_byte, sequence_id, length, key);
774 s.
Printf(
" (cpu_mask=0x%8.8x, cpu_type=0x%8.8x, cpu_subtype=0x%8.8x)",
775 cpu_mask, cpu_type, cpu_subtype);
781 s.
Printf(
" (version=0x%8.8x, feature=0x%8.8x)", version, feature);
786 s.
Printf(
" (count = %u", region_count);
787 for (
uint32_t i = 0; i < region_count; ++i) {
791 s.
Printf(
"\n\tregion[%" PRIu64
"] = { range = [0x%16.16" PRIx64
792 " - 0x%16.16" PRIx64
"), size = 0x%8.8x, prot = %s }",
793 region_addr, region_addr, region_addr + region_size,
839 const char *kernel_version = packet.
PeekCStr(8);
840 s.
Printf(
" (version = \"%s\")", kernel_version);
845 s.
Printf(
" (max_bytes = 0x%8.8x (%u))", max_bytes, max_bytes);
848 const char *path = packet.
GetCStr(&offset);
849 s.
Printf(
" (path = \"%s\")", path);
871 s.
Printf(
" (count = %u, bytes = \n", count);
888 s.
Printf(
" (add support for dumping this packet reply!!!");
897 s.
Printf(
" (reply_port = %u, exc_port = %u, greeting = \"%s\")",
898 reply_port, exc_port, packet.
GetCStr(&offset));
916 s.
Printf(
" (cpu_mask = 0x%8.8x)", cpu_mask);
922 s.
Printf(
" (addr = 0x%8.8x, size = %u)", addr, size);
929 s.
Printf(
" (addr = 0x%8.8x, size = %u, bytes = \n", addr, size);
935 const uint64_t addr = packet.
GetU64(&offset);
937 s.
Printf(
" (addr = 0x%16.16" PRIx64
", size = %u)", addr, size);
942 const uint64_t addr = packet.
GetU64(&offset);
945 s.
Printf(
" (addr = 0x%16.16llx, size = %u, lcpu = %u)", addr, size,
951 const uint64_t addr = packet.
GetU64(&offset);
953 s.
Printf(
" (addr = 0x%16.16" PRIx64
", size = %u, bytes = \n", addr,
960 const uint64_t addr = packet.
GetU64(&offset);
963 s.
Printf(
" (addr = 0x%16.16llx, size = %u, lcpu = %u, bytes = \n",
972 s.
Printf(
" (cpu = %u, flavor = %u)", cpu, flavor);
979 s.
Printf(
" (cpu = %u, flavor = %u, regs = \n", cpu, flavor);
998 s.
Printf(
" (addr = 0x%8.8x)", addr);
1003 const uint64_t addr = packet.
GetU64(&offset);
1004 s.
Printf(
" (addr = 0x%16.16" PRIx64
")", addr);
1008 const char *path = packet.
GetCStr(&offset);
1009 s.
Printf(
" (path = \"%s\")", path);
1015 for (
uint32_t i = 0; i < count; ++i) {
1020 const char *exc_cstr = NULL;
1023 exc_cstr =
"EXC_BAD_ACCESS";
1026 exc_cstr =
"EXC_BAD_INSTRUCTION";
1029 exc_cstr =
"EXC_ARITHMETIC";
1032 exc_cstr =
"EXC_EMULATION";
1035 exc_cstr =
"EXC_SOFTWARE";
1038 exc_cstr =
"EXC_BREAKPOINT";
1041 exc_cstr =
"EXC_SYSCALL";
1044 exc_cstr =
"EXC_MACH_SYSCALL";
1047 exc_cstr =
"EXC_RPC_ALERT";
1050 exc_cstr =
"EXC_CRASH";
1056 s.
Printf(
"{ cpu = 0x%8.8x, exc = %s (%u), code = %u (0x%8.8x), "
1057 "subcode = %u (0x%8.8x)} ",
1058 cpu, exc_cstr, exc, code, code, subcode, subcode);
1065 s.
Printf(
" (term_code = 0x%8.8x (%u), exit_code = 0x%8.8x (%u))",
1066 term_code, term_code, exit_code, exit_code);
1071 s.
Printf(
" (reply_port = %u)", reply_port);
1077 s.
Printf(
" (address=0x%8.8x, lcpu=0x%4.4x)", address, lcpu);
1084 s.
Printf(
" (address=0x%8.8x, lcpu=0x%4.4x, nbytes=0x%8.8x)", lcpu,
1104 s.
Printf(
" (lcpu=0x%4.4x, address=0x%4.4x, nbytes=%u)", lcpu, address,
1112 s.
Printf(
" (lcpu = %u, addr = 0x%4.4x, nbytes = %u, bytes = \n", lcpu,
1130 s.
Printf(
" (count = %u, bytes = \n", count);
1146 error_desc =
"error: invalid packet command: ";
1175 const uint32_t command_length = 8 + 4 + 4;
1186 const uint32_t bytes_to_copy = std::min<uint32_t>(src_len, dst_len);
1187 const void *src = reply_packet.
GetData(&offset, bytes_to_copy);
1189 ::memcpy(dst, src, bytes_to_copy);
1197 error.SetErrorStringWithFormat(
1198 "failed to read kdp registers for cpu %u flavor %u (error %u)", cpu,
1201 error.SetErrorStringWithFormat(
1202 "failed to read kdp registers for cpu %u flavor %u", cpu, flavor);
1204 error.SetErrorString(
"failed to send packet");
1218 const uint32_t command_length = 8 + 4 + 4 + src_len;
1222 request_packet.
Write(src, src_len);
1229 error.SetErrorStringWithFormat(
1230 "failed to read kdp registers for cpu %u flavor %u (error %u)", cpu,
1233 error.SetErrorString(
"failed to send packet");
1242 const uint32_t command_length = 12;
1254 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
1259 const uint32_t command_length = 8 + command_addr_byte_size;
1261 request_packet.
PutMaxHex64(addr, command_addr_byte_size);