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) {
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) {
338 if (greeting == NULL)
344 const uint32_t command_length = 8 + 2 + 2 + ::strlen(greeting) + 1;
348 request_packet.
PutHex16(htons(reply_port));
349 request_packet.
PutHex16(htons(exc_port));
369 const uint32_t command_length = 8 + 2;
373 request_packet.
PutHex16(htons(reply_port));
401 const uint32_t command_length = 8;
441 if (uuid_str.size() < 32)
474 addr_t kernel_load_address;
477 if (errno != 0 || kernel_load_address == 0)
480 return kernel_load_address;
486 const uint32_t command_length = 8;
515 const uint32_t command_length = 8;
519 const char *kernel_version_cstr = reply_packet.
PeekCStr(8);
520 if (kernel_version_cstr && kernel_version_cstr[0])
530 const uint32_t command_length = 8;
545 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
548 const uint32_t command_length = 8 + command_addr_byte_size + 4;
550 request_packet.
PutMaxHex64(addr, command_addr_byte_size);
555 uint32_t kdp_error = reply_packet.
GetU32(&offset);
556 uint32_t src_len = reply_packet.
GetByteSize() - 12;
559 const void *src = reply_packet.
GetData(&offset, src_len);
561 ::memcpy(dst, src, src_len);
568 "kdp read memory failed (error %u)", kdp_error);
583 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
586 const uint32_t command_length = 8 + command_addr_byte_size + 4 + src_len;
588 request_packet.
PutMaxHex64(addr, command_addr_byte_size);
595 uint32_t kdp_error = reply_packet.
GetU32(&offset);
598 "kdp write memory failed (error %u)", kdp_error);
610 uint8_t command_byte,
616 const uint32_t command_length = 8 + src_len;
623 uint32_t kdp_error = reply_packet.
GetU32(&offset);
626 "request packet 0x%8.8x failed (error %u)", command_byte, kdp_error);
640 return "KDP_CONNECT";
642 return "KDP_DISCONNECT";
644 return "KDP_HOSTINFO";
646 return "KDP_VERSION";
648 return "KDP_MAXBYTES";
650 return "KDP_READMEM";
652 return "KDP_WRITEMEM";
654 return "KDP_READREGS";
656 return "KDP_WRITEREGS";
660 return "KDP_IMAGEPATH";
662 return "KDP_SUSPEND";
664 return "KDP_RESUMECPUS";
666 return "KDP_EXCEPTION";
668 return "KDP_TERMINATION";
670 return "KDP_BREAKPOINT_SET";
672 return "KDP_BREAKPOINT_REMOVE";
674 return "KDP_REGIONS";
676 return "KDP_REATTACH";
678 return "KDP_HOSTREBOOT";
680 return "KDP_READMEM64";
682 return "KDP_WRITEMEM64";
684 return "KDP_BREAKPOINT64_SET";
686 return "KDP_BREAKPOINT64_REMOVE";
688 return "KDP_KERNELVERSION";
690 return "KDP_READPHYSMEM64";
692 return "KDP_WRITEPHYSMEM64";
694 return "KDP_READIOPORT";
696 return "KDP_WRITEIOPORT";
698 return "KDP_READMSR64";
700 return "KDP_WRITEMSR64";
702 return "KDP_DUMPINFO";
714 const char *error_desc = NULL;
716 error_desc =
"error: invalid packet (too short): ";
719 const uint8_t first_packet_byte = packet.
GetU8(&offset);
720 const uint8_t sequence_id = packet.
GetU8(&offset);
721 const uint16_t length = packet.
GetU16(&offset);
722 const uint32_t key = packet.
GetU32(&offset);
727 s.
Printf(
"(running=%i) %s %24s: 0x%2.2x 0x%2.2x 0x%4.4x 0x%8.8x ",
728 IsRunning(), is_reply ?
"<--" :
"-->", command_name,
729 first_packet_byte, sequence_id, length, key);
762 const uint32_t cpu_mask = packet.
GetU32(&offset);
763 const uint32_t cpu_type = packet.
GetU32(&offset);
764 const uint32_t cpu_subtype = packet.
GetU32(&offset);
765 s.
Printf(
" (cpu_mask=0x%8.8x, cpu_type=0x%8.8x, cpu_subtype=0x%8.8x)",
766 cpu_mask, cpu_type, cpu_subtype);
770 const uint32_t version = packet.
GetU32(&offset);
771 const uint32_t feature = packet.
GetU32(&offset);
772 s.
Printf(
" (version=0x%8.8x, feature=0x%8.8x)", version, feature);
776 const uint32_t region_count = packet.
GetU32(&offset);
777 s.
Printf(
" (count = %u", region_count);
778 for (uint32_t i = 0; i < region_count; ++i) {
780 const uint32_t region_size = packet.
GetU32(&offset);
781 const uint32_t region_prot = packet.
GetU32(&offset);
782 s.
Printf(
"\n\tregion[%" PRIu64
"] = { range = [0x%16.16" PRIx64
783 " - 0x%16.16" PRIx64
"), size = 0x%8.8x, prot = %s }",
784 region_addr, region_addr, region_addr + region_size,
793 const uint32_t count = packet.
GetByteSize() - offset;
811 const uint32_t count = packet.
GetByteSize() - offset;
830 const char *kernel_version = packet.
PeekCStr(8);
831 s.
Printf(
" (version = \"%s\")", kernel_version);
835 const uint32_t max_bytes = packet.
GetU32(&offset);
836 s.
Printf(
" (max_bytes = 0x%8.8x (%u))", max_bytes, max_bytes);
839 const char *path = packet.
GetCStr(&offset);
840 s.
Printf(
" (path = \"%s\")", path);
846 const uint32_t count = packet.
GetByteSize() - offset;
861 const uint32_t count = packet.
GetByteSize() - offset;
862 s.
Printf(
" (count = %u, bytes = \n", count);
879 s.
Printf(
" (add support for dumping this packet reply!!!");
886 const uint16_t reply_port = ntohs(packet.
GetU16(&offset));
887 const uint16_t exc_port = ntohs(packet.
GetU16(&offset));
888 s.
Printf(
" (reply_port = %u, exc_port = %u, greeting = \"%s\")",
889 reply_port, exc_port, packet.
GetCStr(&offset));
906 const uint32_t cpu_mask = packet.
GetU32(&offset);
907 s.
Printf(
" (cpu_mask = 0x%8.8x)", cpu_mask);
911 const uint32_t addr = packet.
GetU32(&offset);
912 const uint32_t size = packet.
GetU32(&offset);
913 s.
Printf(
" (addr = 0x%8.8x, size = %u)", addr, size);
918 const uint32_t addr = packet.
GetU32(&offset);
919 const uint32_t size = packet.
GetU32(&offset);
920 s.
Printf(
" (addr = 0x%8.8x, size = %u, bytes = \n", addr, size);
926 const uint64_t addr = packet.
GetU64(&offset);
927 const uint32_t size = packet.
GetU32(&offset);
928 s.
Printf(
" (addr = 0x%16.16" PRIx64
", size = %u)", addr, size);
933 const uint64_t addr = packet.
GetU64(&offset);
934 const uint32_t size = packet.
GetU32(&offset);
935 const uint32_t lcpu = packet.
GetU16(&offset);
936 s.
Printf(
" (addr = 0x%16.16llx, size = %u, lcpu = %u)", addr, size,
942 const uint64_t addr = packet.
GetU64(&offset);
943 const uint32_t size = packet.
GetU32(&offset);
944 s.
Printf(
" (addr = 0x%16.16" PRIx64
", size = %u, bytes = \n", addr,
951 const uint64_t addr = packet.
GetU64(&offset);
952 const uint32_t size = packet.
GetU32(&offset);
953 const uint32_t lcpu = packet.
GetU16(&offset);
954 s.
Printf(
" (addr = 0x%16.16llx, size = %u, lcpu = %u, bytes = \n",
961 const uint32_t cpu = packet.
GetU32(&offset);
962 const uint32_t flavor = packet.
GetU32(&offset);
963 s.
Printf(
" (cpu = %u, flavor = %u)", cpu, flavor);
967 const uint32_t cpu = packet.
GetU32(&offset);
968 const uint32_t flavor = packet.
GetU32(&offset);
969 const uint32_t nbytes = packet.
GetByteSize() - offset;
970 s.
Printf(
" (cpu = %u, flavor = %u, regs = \n", cpu, flavor);
988 const uint32_t addr = packet.
GetU32(&offset);
989 s.
Printf(
" (addr = 0x%8.8x)", addr);
994 const uint64_t addr = packet.
GetU64(&offset);
995 s.
Printf(
" (addr = 0x%16.16" PRIx64
")", addr);
999 const char *path = packet.
GetCStr(&offset);
1000 s.
Printf(
" (path = \"%s\")", path);
1004 const uint32_t count = packet.
GetU32(&offset);
1006 for (uint32_t i = 0; i < count; ++i) {
1007 const uint32_t cpu = packet.
GetU32(&offset);
1008 const uint32_t exc = packet.
GetU32(&offset);
1009 const uint32_t code = packet.
GetU32(&offset);
1010 const uint32_t subcode = packet.
GetU32(&offset);
1011 const char *exc_cstr = NULL;
1014 exc_cstr =
"EXC_BAD_ACCESS";
1017 exc_cstr =
"EXC_BAD_INSTRUCTION";
1020 exc_cstr =
"EXC_ARITHMETIC";
1023 exc_cstr =
"EXC_EMULATION";
1026 exc_cstr =
"EXC_SOFTWARE";
1029 exc_cstr =
"EXC_BREAKPOINT";
1032 exc_cstr =
"EXC_SYSCALL";
1035 exc_cstr =
"EXC_MACH_SYSCALL";
1038 exc_cstr =
"EXC_RPC_ALERT";
1041 exc_cstr =
"EXC_CRASH";
1047 s.
Printf(
"{ cpu = 0x%8.8x, exc = %s (%u), code = %u (0x%8.8x), "
1048 "subcode = %u (0x%8.8x)} ",
1049 cpu, exc_cstr, exc, code, code, subcode, subcode);
1054 const uint32_t term_code = packet.
GetU32(&offset);
1055 const uint32_t exit_code = packet.
GetU32(&offset);
1056 s.
Printf(
" (term_code = 0x%8.8x (%u), exit_code = 0x%8.8x (%u))",
1057 term_code, term_code, exit_code, exit_code);
1061 const uint16_t reply_port = ntohs(packet.
GetU16(&offset));
1062 s.
Printf(
" (reply_port = %u)", reply_port);
1066 const uint32_t address = packet.
GetU32(&offset);
1067 const uint16_t lcpu = packet.
GetU16(&offset);
1068 s.
Printf(
" (address=0x%8.8x, lcpu=0x%4.4x)", address, lcpu);
1072 const uint32_t address = packet.
GetU32(&offset);
1073 const uint16_t lcpu = packet.
GetU16(&offset);
1074 const uint32_t nbytes = packet.
GetByteSize() - offset;
1075 s.
Printf(
" (address=0x%8.8x, lcpu=0x%4.4x, nbytes=0x%8.8x)", lcpu,
1092 const uint16_t lcpu = packet.
GetU16(&offset);
1093 const uint16_t address = packet.
GetU16(&offset);
1094 const uint16_t nbytes = packet.
GetU16(&offset);
1095 s.
Printf(
" (lcpu=0x%4.4x, address=0x%4.4x, nbytes=%u)", lcpu, address,
1100 const uint16_t lcpu = packet.
GetU16(&offset);
1101 const uint16_t address = packet.
GetU16(&offset);
1102 const uint16_t nbytes = packet.
GetU16(&offset);
1103 s.
Printf(
" (lcpu = %u, addr = 0x%4.4x, nbytes = %u, bytes = \n", lcpu,
1120 const uint32_t count = packet.
GetByteSize() - offset;
1121 s.
Printf(
" (count = %u, bytes = \n", count);
1137 error_desc =
"error: invalid packet command: ";
1159 uint32_t flavor,
void *dst,
1165 const uint32_t command_length = 8 + 4 + 4;
1172 uint32_t kdp_error = reply_packet.
GetU32(&offset);
1173 uint32_t src_len = reply_packet.
GetByteSize() - 12;
1176 const uint32_t bytes_to_copy = std::min<uint32_t>(src_len, dst_len);
1177 const void *src = reply_packet.
GetData(&offset, bytes_to_copy);
1179 ::memcpy(dst, src, bytes_to_copy);
1188 "failed to read kdp registers for cpu %u flavor %u (error %u)", cpu,
1192 "failed to read kdp registers for cpu %u flavor %u", cpu, flavor);
1207 const uint32_t command_length = 8 + 4 + 4 + src_len;
1211 request_packet.
Write(src, src_len);
1215 uint32_t kdp_error = reply_packet.
GetU32(&offset);
1219 "failed to read kdp registers for cpu %u flavor %u (error %u)", cpu,
1230 const uint32_t command_length = 12;
1241 uint32_t command_addr_byte_size = use_64 ? 8 : 4;
1246 const uint32_t command_length = 8 + command_addr_byte_size;
1248 request_packet.
PutMaxHex64(addr, command_addr_byte_size);
1253 uint32_t kdp_error = reply_packet.
GetU32(&offset);
1263 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