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) {
166 return m_is_running.WaitForValueEqualTo(
false, 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}",
203 if (bytes_read > 0) {
237 if (src && src_len > 0) {
242 LLDB_LOGF(log,
"CommunicationKDP::%s adding %u bytes: %s", __FUNCTION__,
243 (uint32_t)src_len, log_strm.
GetData());
245 m_bytes.append((
const char *)src, src_len);
249 const size_t bytes_available =
m_bytes.size();
250 if (bytes_available >= 8) {
253 uint8_t reply_command = packet.
GetU8(&offset);
254 switch (reply_command) {
303 const uint16_t length = packet.
GetU16(&offset);
304 if (length <= bytes_available) {
325 LLDB_LOGF(log,
"CommunicationKDP::%s: tossing junk byte: 0x%2.2x",
326 __FUNCTION__, (uint8_t)
m_bytes[0]);
337 const char *greeting) {
340 if (greeting == NULL)
346 const uint32_t command_length = 8 + 2 + 2 + ::strlen(greeting) + 1;
350 request_packet.
PutHex16(htons(reply_port));
351 request_packet.
PutHex16(htons(exc_port));
372 const uint32_t command_length = 8 + 2;
376 request_packet.
PutHex16(htons(reply_port));
405 const uint32_t command_length = 8;
445 if (uuid_str.size() < 32)
478 addr_t kernel_load_address;
481 if (errno != 0 || kernel_load_address == 0)
484 return kernel_load_address;
491 const uint32_t command_length = 8;
521 const uint32_t command_length = 8;
525 const char *kernel_version_cstr = reply_packet.
PeekCStr(8);
526 if (kernel_version_cstr && kernel_version_cstr[0])
537 const uint32_t command_length = 8;
553 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
556 const uint32_t command_length = 8 + command_addr_byte_size + 4;
558 request_packet.
PutMaxHex64(addr, command_addr_byte_size);
563 uint32_t kdp_error = reply_packet.
GetU32(&offset);
564 uint32_t src_len = reply_packet.
GetByteSize() - 12;
567 const void *src = reply_packet.
GetData(&offset, src_len);
569 ::memcpy(dst, src, src_len);
576 "kdp read memory failed (error %u)", kdp_error);
592 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
595 const uint32_t command_length = 8 + command_addr_byte_size + 4 + src_len;
597 request_packet.
PutMaxHex64(addr, command_addr_byte_size);
604 uint32_t kdp_error = reply_packet.
GetU32(&offset);
607 "kdp write memory failed (error %u)", kdp_error);
619 uint8_t command_byte,
626 const uint32_t command_length = 8 + src_len;
633 uint32_t kdp_error = reply_packet.
GetU32(&offset);
636 "request packet 0x%8.8x failed (error %u)", command_byte, kdp_error);
650 return "KDP_CONNECT";
652 return "KDP_DISCONNECT";
654 return "KDP_HOSTINFO";
656 return "KDP_VERSION";
658 return "KDP_MAXBYTES";
660 return "KDP_READMEM";
662 return "KDP_WRITEMEM";
664 return "KDP_READREGS";
666 return "KDP_WRITEREGS";
670 return "KDP_IMAGEPATH";
672 return "KDP_SUSPEND";
674 return "KDP_RESUMECPUS";
676 return "KDP_EXCEPTION";
678 return "KDP_TERMINATION";
680 return "KDP_BREAKPOINT_SET";
682 return "KDP_BREAKPOINT_REMOVE";
684 return "KDP_REGIONS";
686 return "KDP_REATTACH";
688 return "KDP_HOSTREBOOT";
690 return "KDP_READMEM64";
692 return "KDP_WRITEMEM64";
694 return "KDP_BREAKPOINT64_SET";
696 return "KDP_BREAKPOINT64_REMOVE";
698 return "KDP_KERNELVERSION";
700 return "KDP_READPHYSMEM64";
702 return "KDP_WRITEPHYSMEM64";
704 return "KDP_READIOPORT";
706 return "KDP_WRITEIOPORT";
708 return "KDP_READMSR64";
710 return "KDP_WRITEMSR64";
712 return "KDP_DUMPINFO";
724 const char *error_desc = NULL;
726 error_desc =
"error: invalid packet (too short): ";
729 const uint8_t first_packet_byte = packet.
GetU8(&offset);
730 const uint8_t sequence_id = packet.
GetU8(&offset);
731 const uint16_t length = packet.
GetU16(&offset);
732 const uint32_t key = packet.
GetU32(&offset);
737 s.
Printf(
"(running=%i) %s %24s: 0x%2.2x 0x%2.2x 0x%4.4x 0x%8.8x ",
738 IsRunning(), is_reply ?
"<--" :
"-->", command_name,
739 first_packet_byte, sequence_id, length, key);
772 const uint32_t cpu_mask = packet.
GetU32(&offset);
773 const uint32_t cpu_type = packet.
GetU32(&offset);
774 const uint32_t cpu_subtype = packet.
GetU32(&offset);
775 s.
Printf(
" (cpu_mask=0x%8.8x, cpu_type=0x%8.8x, cpu_subtype=0x%8.8x)",
776 cpu_mask, cpu_type, cpu_subtype);
780 const uint32_t version = packet.
GetU32(&offset);
781 const uint32_t feature = packet.
GetU32(&offset);
782 s.
Printf(
" (version=0x%8.8x, feature=0x%8.8x)", version, feature);
786 const uint32_t region_count = packet.
GetU32(&offset);
787 s.
Printf(
" (count = %u", region_count);
788 for (uint32_t i = 0; i < region_count; ++i) {
790 const uint32_t region_size = packet.
GetU32(&offset);
791 const uint32_t region_prot = packet.
GetU32(&offset);
792 s.
Printf(
"\n\tregion[%" PRIu64
"] = { range = [0x%16.16" PRIx64
793 " - 0x%16.16" PRIx64
"), size = 0x%8.8x, prot = %s }",
794 region_addr, region_addr, region_addr + region_size,
803 const uint32_t count = packet.
GetByteSize() - offset;
821 const uint32_t count = packet.
GetByteSize() - offset;
840 const char *kernel_version = packet.
PeekCStr(8);
841 s.
Printf(
" (version = \"%s\")", kernel_version);
845 const uint32_t max_bytes = packet.
GetU32(&offset);
846 s.
Printf(
" (max_bytes = 0x%8.8x (%u))", max_bytes, max_bytes);
849 const char *path = packet.
GetCStr(&offset);
850 s.
Printf(
" (path = \"%s\")", path);
856 const uint32_t count = packet.
GetByteSize() - offset;
871 const uint32_t count = packet.
GetByteSize() - offset;
872 s.
Printf(
" (count = %u, bytes = \n", count);
889 s.
Printf(
" (add support for dumping this packet reply!!!");
896 const uint16_t reply_port = ntohs(packet.
GetU16(&offset));
897 const uint16_t exc_port = ntohs(packet.
GetU16(&offset));
898 s.
Printf(
" (reply_port = %u, exc_port = %u, greeting = \"%s\")",
899 reply_port, exc_port, packet.
GetCStr(&offset));
916 const uint32_t cpu_mask = packet.
GetU32(&offset);
917 s.
Printf(
" (cpu_mask = 0x%8.8x)", cpu_mask);
921 const uint32_t addr = packet.
GetU32(&offset);
922 const uint32_t size = packet.
GetU32(&offset);
923 s.
Printf(
" (addr = 0x%8.8x, size = %u)", addr, size);
928 const uint32_t addr = packet.
GetU32(&offset);
929 const uint32_t size = packet.
GetU32(&offset);
930 s.
Printf(
" (addr = 0x%8.8x, size = %u, bytes = \n", addr, size);
936 const uint64_t addr = packet.
GetU64(&offset);
937 const uint32_t size = packet.
GetU32(&offset);
938 s.
Printf(
" (addr = 0x%16.16" PRIx64
", size = %u)", addr, size);
943 const uint64_t addr = packet.
GetU64(&offset);
944 const uint32_t size = packet.
GetU32(&offset);
945 const uint32_t lcpu = packet.
GetU16(&offset);
946 s.
Printf(
" (addr = 0x%16.16llx, size = %u, lcpu = %u)", addr, size,
952 const uint64_t addr = packet.
GetU64(&offset);
953 const uint32_t size = packet.
GetU32(&offset);
954 s.
Printf(
" (addr = 0x%16.16" PRIx64
", size = %u, bytes = \n", addr,
961 const uint64_t addr = packet.
GetU64(&offset);
962 const uint32_t size = packet.
GetU32(&offset);
963 const uint32_t lcpu = packet.
GetU16(&offset);
964 s.
Printf(
" (addr = 0x%16.16llx, size = %u, lcpu = %u, bytes = \n",
971 const uint32_t cpu = packet.
GetU32(&offset);
972 const uint32_t flavor = packet.
GetU32(&offset);
973 s.
Printf(
" (cpu = %u, flavor = %u)", cpu, flavor);
977 const uint32_t cpu = packet.
GetU32(&offset);
978 const uint32_t flavor = packet.
GetU32(&offset);
979 const uint32_t nbytes = packet.
GetByteSize() - offset;
980 s.
Printf(
" (cpu = %u, flavor = %u, regs = \n", cpu, flavor);
998 const uint32_t addr = packet.
GetU32(&offset);
999 s.
Printf(
" (addr = 0x%8.8x)", addr);
1004 const uint64_t addr = packet.
GetU64(&offset);
1005 s.
Printf(
" (addr = 0x%16.16" PRIx64
")", addr);
1009 const char *path = packet.
GetCStr(&offset);
1010 s.
Printf(
" (path = \"%s\")", path);
1014 const uint32_t count = packet.
GetU32(&offset);
1016 for (uint32_t i = 0; i < count; ++i) {
1017 const uint32_t cpu = packet.
GetU32(&offset);
1018 const uint32_t exc = packet.
GetU32(&offset);
1019 const uint32_t code = packet.
GetU32(&offset);
1020 const uint32_t subcode = packet.
GetU32(&offset);
1021 const char *exc_cstr = NULL;
1024 exc_cstr =
"EXC_BAD_ACCESS";
1027 exc_cstr =
"EXC_BAD_INSTRUCTION";
1030 exc_cstr =
"EXC_ARITHMETIC";
1033 exc_cstr =
"EXC_EMULATION";
1036 exc_cstr =
"EXC_SOFTWARE";
1039 exc_cstr =
"EXC_BREAKPOINT";
1042 exc_cstr =
"EXC_SYSCALL";
1045 exc_cstr =
"EXC_MACH_SYSCALL";
1048 exc_cstr =
"EXC_RPC_ALERT";
1051 exc_cstr =
"EXC_CRASH";
1057 s.
Printf(
"{ cpu = 0x%8.8x, exc = %s (%u), code = %u (0x%8.8x), "
1058 "subcode = %u (0x%8.8x)} ",
1059 cpu, exc_cstr, exc, code, code, subcode, subcode);
1064 const uint32_t term_code = packet.
GetU32(&offset);
1065 const uint32_t exit_code = packet.
GetU32(&offset);
1066 s.
Printf(
" (term_code = 0x%8.8x (%u), exit_code = 0x%8.8x (%u))",
1067 term_code, term_code, exit_code, exit_code);
1071 const uint16_t reply_port = ntohs(packet.
GetU16(&offset));
1072 s.
Printf(
" (reply_port = %u)", reply_port);
1076 const uint32_t address = packet.
GetU32(&offset);
1077 const uint16_t lcpu = packet.
GetU16(&offset);
1078 s.
Printf(
" (address=0x%8.8x, lcpu=0x%4.4x)", address, lcpu);
1082 const uint32_t address = packet.
GetU32(&offset);
1083 const uint16_t lcpu = packet.
GetU16(&offset);
1084 const uint32_t nbytes = packet.
GetByteSize() - offset;
1085 s.
Printf(
" (address=0x%8.8x, lcpu=0x%4.4x, nbytes=0x%8.8x)", lcpu,
1102 const uint16_t lcpu = packet.
GetU16(&offset);
1103 const uint16_t address = packet.
GetU16(&offset);
1104 const uint16_t nbytes = packet.
GetU16(&offset);
1105 s.
Printf(
" (lcpu=0x%4.4x, address=0x%4.4x, nbytes=%u)", lcpu, address,
1110 const uint16_t lcpu = packet.
GetU16(&offset);
1111 const uint16_t address = packet.
GetU16(&offset);
1112 const uint16_t nbytes = packet.
GetU16(&offset);
1113 s.
Printf(
" (lcpu = %u, addr = 0x%4.4x, nbytes = %u, bytes = \n", lcpu,
1130 const uint32_t count = packet.
GetByteSize() - offset;
1131 s.
Printf(
" (count = %u, bytes = \n", count);
1147 error_desc =
"error: invalid packet command: ";
1169 uint32_t flavor,
void *dst,
1176 const uint32_t command_length = 8 + 4 + 4;
1183 uint32_t kdp_error = reply_packet.
GetU32(&offset);
1184 uint32_t src_len = reply_packet.
GetByteSize() - 12;
1187 const uint32_t bytes_to_copy = std::min<uint32_t>(src_len, dst_len);
1188 const void *src = reply_packet.
GetData(&offset, bytes_to_copy);
1190 ::memcpy(dst, src, bytes_to_copy);
1199 "failed to read kdp registers for cpu %u flavor %u (error %u)", cpu,
1203 "failed to read kdp registers for cpu %u flavor %u", cpu, flavor);
1219 const uint32_t command_length = 8 + 4 + 4 + src_len;
1223 request_packet.
Write(src, src_len);
1227 uint32_t kdp_error = reply_packet.
GetU32(&offset);
1231 "failed to read kdp registers for cpu %u flavor %u (error %u)", cpu,
1243 const uint32_t command_length = 12;
1255 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
1260 const uint32_t command_length = 8 + command_addr_byte_size;
1262 request_packet.
PutMaxHex64(addr, command_addr_byte_size);
1267 uint32_t kdp_error = reply_packet.
GetU32(&offset);
1278 const uint32_t command_length = 8;
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
#define LLDB_LOG_VERBOSE(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
lldb_private::StreamBuffer< 4096 > PacketStreamType
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
std::chrono::seconds m_packet_timeout
@ 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
uint8_t m_exception_sequence_id
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.
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.
Communication()
Construct the Communication object.
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.
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.
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.
@ eBinary
Get and put data as binary instead of as the default string mode.
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)
Represents UUID's of various sizes.
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