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) {}
54 uint16_t request_length) {
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() ==
81 *((
const uint16_t *)(request_packet.
GetData() + 2)));
83 const uint32_t num_retries = 3;
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) {
171 uint32_t timeout_usec) {
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}",
202 if (bytes_read > 0) {
236 if (src && src_len > 0) {
241 LLDB_LOGF(log,
"CommunicationKDP::%s adding %u bytes: %s", __FUNCTION__,
242 (uint32_t)src_len, log_strm.
GetData());
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) {
302 const uint16_t length = packet.
GetU16(&offset);
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));
404 const uint32_t command_length = 8;
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;
490 const uint32_t command_length = 8;
520 const uint32_t command_length = 8;
524 const char *kernel_version_cstr = reply_packet.
PeekCStr(8);
525 if (kernel_version_cstr && kernel_version_cstr[0])
536 const uint32_t command_length = 8;
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);
562 uint32_t kdp_error = reply_packet.
GetU32(&offset);
563 uint32_t src_len = reply_packet.
GetByteSize() - 12;
566 const void *src = reply_packet.
GetData(&offset, src_len);
568 ::memcpy(dst, src, src_len);
575 "kdp read memory failed (error %u)", kdp_error);
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);
603 uint32_t kdp_error = reply_packet.
GetU32(&offset);
606 "kdp write memory failed (error %u)", kdp_error);
618 uint8_t command_byte,
625 const uint32_t command_length = 8 + src_len;
632 uint32_t kdp_error = reply_packet.
GetU32(&offset);
635 "request packet 0x%8.8x failed (error %u)", command_byte, kdp_error);
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);
730 const uint16_t length = packet.
GetU16(&offset);
731 const uint32_t key = packet.
GetU32(&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);
771 const uint32_t cpu_mask = packet.
GetU32(&offset);
772 const uint32_t cpu_type = packet.
GetU32(&offset);
773 const uint32_t cpu_subtype = packet.
GetU32(&offset);
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);
779 const uint32_t version = packet.
GetU32(&offset);
780 const uint32_t feature = packet.
GetU32(&offset);
781 s.
Printf(
" (version=0x%8.8x, feature=0x%8.8x)", version, feature);
785 const uint32_t region_count = packet.
GetU32(&offset);
786 s.
Printf(
" (count = %u", region_count);
787 for (uint32_t i = 0; i < region_count; ++i) {
789 const uint32_t region_size = packet.
GetU32(&offset);
790 const uint32_t region_prot = packet.
GetU32(&offset);
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,
802 const uint32_t count = packet.
GetByteSize() - offset;
820 const uint32_t count = packet.
GetByteSize() - offset;
839 const char *kernel_version = packet.
PeekCStr(8);
840 s.
Printf(
" (version = \"%s\")", kernel_version);
844 const uint32_t max_bytes = packet.
GetU32(&offset);
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);
855 const uint32_t count = packet.
GetByteSize() - offset;
870 const uint32_t count = packet.
GetByteSize() - offset;
871 s.
Printf(
" (count = %u, bytes = \n", count);
888 s.
Printf(
" (add support for dumping this packet reply!!!");
895 const uint16_t reply_port = ntohs(packet.
GetU16(&offset));
896 const uint16_t exc_port = ntohs(packet.
GetU16(&offset));
897 s.
Printf(
" (reply_port = %u, exc_port = %u, greeting = \"%s\")",
898 reply_port, exc_port, packet.
GetCStr(&offset));
915 const uint32_t cpu_mask = packet.
GetU32(&offset);
916 s.
Printf(
" (cpu_mask = 0x%8.8x)", cpu_mask);
920 const uint32_t addr = packet.
GetU32(&offset);
921 const uint32_t size = packet.
GetU32(&offset);
922 s.
Printf(
" (addr = 0x%8.8x, size = %u)", addr, size);
927 const uint32_t addr = packet.
GetU32(&offset);
928 const uint32_t size = packet.
GetU32(&offset);
929 s.
Printf(
" (addr = 0x%8.8x, size = %u, bytes = \n", addr, size);
935 const uint64_t addr = packet.
GetU64(&offset);
936 const uint32_t size = packet.
GetU32(&offset);
937 s.
Printf(
" (addr = 0x%16.16" PRIx64
", size = %u)", addr, size);
942 const uint64_t addr = packet.
GetU64(&offset);
943 const uint32_t size = packet.
GetU32(&offset);
944 const uint32_t lcpu = packet.
GetU16(&offset);
945 s.
Printf(
" (addr = 0x%16.16llx, size = %u, lcpu = %u)", addr, size,
951 const uint64_t addr = packet.
GetU64(&offset);
952 const uint32_t size = packet.
GetU32(&offset);
953 s.
Printf(
" (addr = 0x%16.16" PRIx64
", size = %u, bytes = \n", addr,
960 const uint64_t addr = packet.
GetU64(&offset);
961 const uint32_t size = packet.
GetU32(&offset);
962 const uint32_t lcpu = packet.
GetU16(&offset);
963 s.
Printf(
" (addr = 0x%16.16llx, size = %u, lcpu = %u, bytes = \n",
970 const uint32_t cpu = packet.
GetU32(&offset);
971 const uint32_t flavor = packet.
GetU32(&offset);
972 s.
Printf(
" (cpu = %u, flavor = %u)", cpu, flavor);
976 const uint32_t cpu = packet.
GetU32(&offset);
977 const uint32_t flavor = packet.
GetU32(&offset);
978 const uint32_t nbytes = packet.
GetByteSize() - offset;
979 s.
Printf(
" (cpu = %u, flavor = %u, regs = \n", cpu, flavor);
997 const uint32_t addr = packet.
GetU32(&offset);
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);
1013 const uint32_t count = packet.
GetU32(&offset);
1015 for (uint32_t i = 0; i < count; ++i) {
1016 const uint32_t cpu = packet.
GetU32(&offset);
1017 const uint32_t exc = packet.
GetU32(&offset);
1018 const uint32_t code = packet.
GetU32(&offset);
1019 const uint32_t subcode = packet.
GetU32(&offset);
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);
1063 const uint32_t term_code = packet.
GetU32(&offset);
1064 const uint32_t exit_code = packet.
GetU32(&offset);
1065 s.
Printf(
" (term_code = 0x%8.8x (%u), exit_code = 0x%8.8x (%u))",
1066 term_code, term_code, exit_code, exit_code);
1070 const uint16_t reply_port = ntohs(packet.
GetU16(&offset));
1071 s.
Printf(
" (reply_port = %u)", reply_port);
1075 const uint32_t address = packet.
GetU32(&offset);
1076 const uint16_t lcpu = packet.
GetU16(&offset);
1077 s.
Printf(
" (address=0x%8.8x, lcpu=0x%4.4x)", address, lcpu);
1081 const uint32_t address = packet.
GetU32(&offset);
1082 const uint16_t lcpu = packet.
GetU16(&offset);
1083 const uint32_t nbytes = packet.
GetByteSize() - offset;
1084 s.
Printf(
" (address=0x%8.8x, lcpu=0x%4.4x, nbytes=0x%8.8x)", lcpu,
1101 const uint16_t lcpu = packet.
GetU16(&offset);
1102 const uint16_t address = packet.
GetU16(&offset);
1103 const uint16_t nbytes = packet.
GetU16(&offset);
1104 s.
Printf(
" (lcpu=0x%4.4x, address=0x%4.4x, nbytes=%u)", lcpu, address,
1109 const uint16_t lcpu = packet.
GetU16(&offset);
1110 const uint16_t address = packet.
GetU16(&offset);
1111 const uint16_t nbytes = packet.
GetU16(&offset);
1112 s.
Printf(
" (lcpu = %u, addr = 0x%4.4x, nbytes = %u, bytes = \n", lcpu,
1129 const uint32_t count = packet.
GetByteSize() - offset;
1130 s.
Printf(
" (count = %u, bytes = \n", count);
1146 error_desc =
"error: invalid packet command: ";
1168 uint32_t flavor,
void *dst,
1175 const uint32_t command_length = 8 + 4 + 4;
1182 uint32_t kdp_error = reply_packet.
GetU32(&offset);
1183 uint32_t src_len = reply_packet.
GetByteSize() - 12;
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);
1198 "failed to read kdp registers for cpu %u flavor %u (error %u)", cpu,
1202 "failed to read kdp registers for cpu %u flavor %u", cpu, flavor);
1218 const uint32_t command_length = 8 + 4 + 4 + src_len;
1222 request_packet.
Write(src, src_len);
1226 uint32_t kdp_error = reply_packet.
GetU32(&offset);
1230 "failed to read kdp registers for cpu %u flavor %u (error %u)", cpu,
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);
1266 uint32_t kdp_error = reply_packet.
GetU32(&offset);
1277 const uint32_t command_length = 8;
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
#define LLDB_LOGV(log,...)
bool SendRequestBreakpoint(bool set, lldb::addr_t addr)
bool SendRequestConnect(uint16_t reply_port, uint16_t exc_port, const char *greeting)
bool SendRequestAndGetReply(const CommandType command, const PacketStreamType &request_packet, lldb_private::DataExtractor &reply_packet)
bool SendRequestPacketNoLock(const PacketStreamType &request_packet)
bool WaitForNotRunningPrivate(const std::chrono::microseconds &timeout)
lldb::addr_t m_last_read_memory_addr
bool SendRequestHostInfo()
void DumpPacket(lldb_private::Stream &s, const void *data, uint32_t data_len)
uint32_t m_kdp_hostinfo_cpu_mask
uint32_t m_kdp_hostinfo_cpu_type
std::chrono::seconds GetPacketTimeout() const
uint32_t SendRequestWriteMemory(lldb::addr_t addr, const void *src, uint32_t src_len, lldb_private::Status &error)
uint32_t m_kdp_version_version
bool SendRawRequest(uint8_t command_byte, const void *src, uint32_t src_len, lldb_private::DataExtractor &reply, lldb_private::Status &error)
bool GetSequenceMutex(std::unique_lock< std::recursive_mutex > &lock)
uint32_t SendRequestReadMemory(lldb::addr_t addr, void *dst, uint32_t dst_size, lldb_private::Status &error)
bool SendRequestSuspend()
CommunicationKDP(const char *comm_name)
bool SendRequestPacket(const PacketStreamType &request_packet)
bool SendRequestKernelVersion()
lldb::addr_t GetLoadAddress()
size_t WaitForPacketWithTimeoutMicroSecondsNoLock(lldb_private::DataExtractor &response, uint32_t timeout_usec)
~CommunicationKDP() override
std::recursive_mutex m_bytes_mutex
@ KDP_BREAKPOINT_REMOVE64
uint32_t SendRequestReadRegisters(uint32_t cpu, uint32_t flavor, void *dst, uint32_t dst_size, lldb_private::Status &error)
bool CheckForPacket(const uint8_t *src, size_t src_len, lldb_private::DataExtractor &packet)
uint8_t m_request_sequence_id
void MakeRequestPacketHeader(CommandType request_type, PacketStreamType &request_packet, uint16_t request_length)
uint32_t SendRequestWriteRegisters(uint32_t cpu, uint32_t flavor, const void *src, uint32_t src_size, lldb_private::Status &error)
bool SendRequestDisconnect()
uint32_t m_kdp_version_feature
const char * GetKernelVersion()
uint32_t GetFeatureFlags()
bool SendRequestVersion()
lldb_private::UUID GetUUID()
bool ExtractIsReply(uint8_t first_packet_byte) const
bool HostInfoIsValid() const
size_t WaitForPacketWithTimeoutMicroSeconds(lldb_private::DataExtractor &response, uint32_t usec)
bool RemoteIsDarwinKernel()
uint32_t m_addr_byte_size
static const char * GetCommandAsCString(uint8_t command)
uint32_t m_kdp_hostinfo_cpu_subtype
lldb_private::Predicate< bool > m_is_running
std::string m_kernel_version
bool SendRequestReattach(uint16_t reply_port)
CommandType ExtractCommand(uint8_t first_packet_byte) const
lldb::ByteOrder m_byte_order
std::recursive_mutex m_sequence_mutex
bool VersionIsValid() const
An architecture specification class.
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
bool SetArchitecture(ArchitectureType arch_type, uint32_t cpu, uint32_t sub, uint32_t os=0)
Change the architecture object type, CPU type and OS type.
lldb::ByteOrder GetByteOrder() const
Returns the byte order for the architecture specification.
An abstract communications class.
virtual size_t Read(void *dst, size_t dst_len, const Timeout< std::micro > &timeout, lldb::ConnectionStatus &status, Status *error_ptr)
Read bytes from the current connection.
bool IsConnected() const
Check if the connection is valid.
virtual lldb::ConnectionStatus Disconnect(Status *error_ptr=nullptr)
Disconnect the communications connection if one is currently connected.
static std::string ConnectionStatusAsString(lldb::ConnectionStatus status)
size_t Write(const void *src, size_t src_len, lldb::ConnectionStatus &status, Status *error_ptr)
The actual write function that attempts to write to the communications protocol.
A subclass of DataBuffer that stores a data buffer on the heap.
void SetValue(T value, PredicateBroadcastType broadcast_type)
Value set accessor.
bool WaitForValueEqualTo(T value, const Timeout< std::micro > &timeout=std::nullopt)
Wait for m_value to be equal to value.
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
static Status FromErrorString(const char *str)
const char * GetData() const
A stream class that can stream formatted output to a file.
size_t Write(const void *src, size_t src_len)
Output character bytes to the stream.
@ eBinary
Get and put data as binary instead of as the default string mode.
size_t size_t PutHex8(uint8_t uvalue)
Append an uint8_t value in the hexadecimal format to the stream.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
lldb::ByteOrder SetByteOrder(lldb::ByteOrder byte_order)
Set the byte_order value.
size_t PutHex16(uint16_t uvalue, lldb::ByteOrder byte_order=lldb::eByteOrderInvalid)
size_t PutHex32(uint32_t uvalue, lldb::ByteOrder byte_order=lldb::eByteOrderInvalid)
size_t PutRawBytes(const void *s, size_t src_len, lldb::ByteOrder src_byte_order=lldb::eByteOrderInvalid, lldb::ByteOrder dst_byte_order=lldb::eByteOrderInvalid)
size_t PutMaxHex64(uint64_t uvalue, size_t byte_size, lldb::ByteOrder byte_order=lldb::eByteOrderInvalid)
bool SetFromStringRef(llvm::StringRef str)
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
void DumpHexBytes(Stream *s, const void *src, size_t src_len, uint32_t bytes_per_line, lldb::addr_t base_addr)
@ eBroadcastAlways
Always send a broadcast when the value is modified.
lldb::offset_t DumpDataExtractor(const DataExtractor &DE, Stream *s, lldb::offset_t offset, lldb::Format item_format, size_t item_byte_size, size_t item_count, size_t num_per_line, uint64_t base_addr, uint32_t item_bit_size, uint32_t item_bit_offset, ExecutionContextScope *exe_scope=nullptr, bool show_memory_tags=false)
Dumps item_count objects into the stream s.
const char * GetPermissionsAsCString(uint32_t permissions)
ConnectionStatus
Connection Status Types.
@ eConnectionStatusError
Check GetError() for details.
@ eConnectionStatusInterrupted
Interrupted read.
@ eConnectionStatusTimedOut
Request timed out.
@ eConnectionStatusEndOfFile
End-of-file encountered.
@ eConnectionStatusSuccess
Success.
@ eConnectionStatusLostConnection
Lost connection while connected to a valid connection.
@ eConnectionStatusNoConnection
No connection.
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP