1 //===-- CommunicationKDP.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "CommunicationKDP.h"
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"
32 #include "ProcessKDPLog.h"
35 using namespace lldb_private;
37 //----------------------------------------------------------------------
38 // CommunicationKDP constructor
39 //----------------------------------------------------------------------
40 CommunicationKDP::CommunicationKDP(const char *comm_name)
41 : Communication(comm_name),
43 m_byte_order(eByteOrderLittle),
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)
58 //----------------------------------------------------------------------
60 //----------------------------------------------------------------------
61 CommunicationKDP::~CommunicationKDP()
70 CommunicationKDP::SendRequestPacket (const PacketStreamType &request_packet)
72 std::lock_guard<std::recursive_mutex> guard(m_sequence_mutex);
73 return SendRequestPacketNoLock (request_packet);
78 uint8_t request; // Either: CommandType | ePacketTypeRequest, or CommandType | ePacketTypeReply
80 uint16_t length; // Length of entire packet including this header
81 uint32_t key; // Session key
86 CommunicationKDP::MakeRequestPacketHeader (CommandType request_type,
87 PacketStreamType &request_packet,
88 uint16_t request_length)
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
98 CommunicationKDP::SendRequestAndGetReply (const CommandType command,
99 const PacketStreamType &request_packet,
100 DataExtractor &reply_packet)
104 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS));
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());
114 std::lock_guard<std::recursive_mutex> guard(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)));
119 lldb::offset_t offset = 1;
120 const uint32_t num_retries = 3;
121 for (uint32_t i=0; i<num_retries; ++i)
123 if (SendRequestPacketNoLock(request_packet))
125 const uint8_t request_sequence_id = (uint8_t)request_packet.GetData()[1];
128 if (WaitForPacketWithTimeoutMicroSecondsNoLock (reply_packet, GetPacketTimeoutInMicroSeconds ()))
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)
135 // The sequent ID was correct, now verify we got the response we were looking for
136 if ((reply_command & eCommandTypeMask) == command)
139 if (command == KDP_RESUMECPUS)
140 m_is_running.SetValue(true, eBroadcastAlways);
145 // Failed to get the correct response, bail
146 reply_packet.Clear();
150 else if (reply_sequence_id > request_sequence_id)
152 // Sequence ID was greater than the sequence ID of the packet we sent, something
153 // is really wrong...
154 reply_packet.Clear();
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.
166 // Break and retry sending the packet as we didn't get a response due to timeout
172 reply_packet.Clear();
177 CommunicationKDP::SendRequestPacketNoLock (const PacketStreamType &request_packet)
181 const char *packet_data = request_packet.GetData();
182 const size_t packet_size = request_packet.GetSize();
184 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS));
187 PacketStreamType log_strm;
188 DumpPacket (log_strm, packet_data, packet_size);
189 log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData());
191 ConnectionStatus status = eConnectionStatusSuccess;
193 size_t bytes_written = Write (packet_data,
198 if (bytes_written == packet_size)
202 log->Printf ("error: failed to send packet entire packet %" PRIu64 " of %" PRIu64 " bytes sent", (uint64_t)bytes_written, (uint64_t)packet_size);
208 CommunicationKDP::GetSequenceMutex(std::unique_lock<std::recursive_mutex> &lock)
210 return (lock = std::unique_lock<std::recursive_mutex>(m_sequence_mutex, std::try_to_lock)).owns_lock();
215 CommunicationKDP::WaitForNotRunningPrivate (const TimeValue *timeout_ptr)
217 return m_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL);
221 CommunicationKDP::WaitForPacketWithTimeoutMicroSeconds (DataExtractor &packet, uint32_t timeout_usec)
223 std::lock_guard<std::recursive_mutex> guard(m_sequence_mutex);
224 return WaitForPacketWithTimeoutMicroSecondsNoLock (packet, timeout_usec);
228 CommunicationKDP::WaitForPacketWithTimeoutMicroSecondsNoLock (DataExtractor &packet, uint32_t timeout_usec)
230 uint8_t buffer[8192];
233 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS | KDP_LOG_VERBOSE));
235 // Check for a packet from our cache first without trying any reading...
236 if (CheckForPacket (NULL, 0, packet))
237 return packet.GetByteSize();
239 bool timed_out = false;
240 while (IsConnected() && !timed_out)
242 lldb::ConnectionStatus status = eConnectionStatusNoConnection;
243 size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error);
246 log->Printf ("%s: Read (buffer, (sizeof(buffer), timeout_usec = 0x%x, status = %s, error = %s) => bytes_read = %" PRIu64,
249 Communication::ConnectionStatusAsCString (status),
251 (uint64_t)bytes_read);
255 if (CheckForPacket (buffer, bytes_read, packet))
256 return packet.GetByteSize();
262 case eConnectionStatusInterrupted:
263 case eConnectionStatusTimedOut:
266 case eConnectionStatusSuccess:
267 //printf ("status = success but error = %s\n", error.AsCString("<invalid>"));
270 case eConnectionStatusEndOfFile:
271 case eConnectionStatusNoConnection:
272 case eConnectionStatusLostConnection:
273 case eConnectionStatusError:
284 CommunicationKDP::CheckForPacket (const uint8_t *src, size_t src_len, DataExtractor &packet)
286 // Put the packet data into the buffer in a thread safe fashion
287 std::lock_guard<std::recursive_mutex> guard(m_bytes_mutex);
289 Log *log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS));
291 if (src && src_len > 0)
293 if (log && log->GetVerbose())
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",
302 m_bytes.append ((const char *)src, src_len);
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)
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)
314 case ePacketTypeRequest | KDP_EXCEPTION:
315 case ePacketTypeRequest | KDP_TERMINATION:
316 // We got an exception request, so be sure to send an ACK
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);
328 // Fall through to case below to get packet contents
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:
362 const uint16_t length = packet.GetU16 (&offset);
363 if (length <= bytes_available)
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);
373 PacketStreamType log_strm;
374 DumpPacket (log_strm, packet);
376 log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData());
384 // Unrecognized reply command byte, erase this byte and try to get back on track
386 log->Printf ("CommunicationKDP::%s: tossing junk byte: 0x%2.2x",
388 (uint8_t)m_bytes[0]);
399 CommunicationKDP::SendRequestConnect (uint16_t reply_port,
401 const char *greeting)
403 PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order);
404 if (greeting == NULL)
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);
422 CommunicationKDP::ClearKDPSettings ()
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;
433 CommunicationKDP::SendRequestReattach (uint16_t reply_port)
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))
447 // Reset the sequence ID to zero for reattach
449 lldb::offset_t offset = 4;
450 m_session_key = reply_packet.GetU32 (&offset);
457 CommunicationKDP::GetVersion ()
459 if (!VersionIsValid())
460 SendRequestVersion();
461 return m_kdp_version_version;
465 CommunicationKDP::GetFeatureFlags ()
467 if (!VersionIsValid())
468 SendRequestVersion();
469 return m_kdp_version_feature;
473 CommunicationKDP::SendRequestVersion ()
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))
482 lldb::offset_t offset = 8;
483 m_kdp_version_version = reply_packet.GetU32 (&offset);
484 m_kdp_version_feature = reply_packet.GetU32 (&offset);
490 #if 0 // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection...
492 CommunicationKDP::GetImagePath ()
494 if (m_image_path.empty())
495 SendRequestImagePath();
496 return m_image_path.c_str();
500 CommunicationKDP::SendRequestImagePath ()
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))
509 const char *path = reply_packet.PeekCStr(8);
511 m_kernel_version.assign (path);
519 CommunicationKDP::GetCPUMask ()
521 if (!HostInfoIsValid())
522 SendRequestHostInfo();
523 return m_kdp_hostinfo_cpu_mask;
527 CommunicationKDP::GetCPUType ()
529 if (!HostInfoIsValid())
530 SendRequestHostInfo();
531 return m_kdp_hostinfo_cpu_type;
535 CommunicationKDP::GetCPUSubtype ()
537 if (!HostInfoIsValid())
538 SendRequestHostInfo();
539 return m_kdp_hostinfo_cpu_subtype;
543 CommunicationKDP::GetUUID ()
546 if (GetKernelVersion() == NULL)
549 if (m_kernel_version.find("UUID=") == std::string::npos)
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)
557 if (uuid.SetFromCString (uuid_str.c_str()) == 0)
567 CommunicationKDP::RemoteIsEFI ()
569 if (GetKernelVersion() == NULL)
571 if (strncmp (m_kernel_version.c_str(), "EFI", 3) == 0)
578 CommunicationKDP::RemoteIsDarwinKernel ()
580 if (GetKernelVersion() == NULL)
582 if (m_kernel_version.find("Darwin Kernel") != std::string::npos)
589 CommunicationKDP::GetLoadAddress ()
591 if (GetKernelVersion() == NULL)
592 return LLDB_INVALID_ADDRESS;
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;
600 addr_t kernel_load_address;
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;
606 return kernel_load_address;
610 CommunicationKDP::SendRequestHostInfo ()
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))
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);
624 ArchSpec kernel_arch;
625 kernel_arch.SetArchitecture (eArchTypeMachO,
626 m_kdp_hostinfo_cpu_type,
627 m_kdp_hostinfo_cpu_subtype);
629 m_addr_byte_size = kernel_arch.GetAddressByteSize();
630 m_byte_order = kernel_arch.GetByteOrder();
637 CommunicationKDP::GetKernelVersion ()
639 if (m_kernel_version.empty())
640 SendRequestKernelVersion ();
641 return m_kernel_version.c_str();
645 CommunicationKDP::SendRequestKernelVersion ()
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))
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);
663 CommunicationKDP::SendRequestDisconnect ()
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))
672 // Are we supposed to get a reply for disconnect?
679 CommunicationKDP::SendRequestReadMemory (lldb::addr_t addr,
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))
696 lldb::offset_t offset = 8;
697 uint32_t kdp_error = reply_packet.GetU32 (&offset);
698 uint32_t src_len = reply_packet.GetByteSize() - 12;
702 const void *src = reply_packet.GetData(&offset, src_len);
705 ::memcpy (dst, src, src_len);
711 error.SetErrorStringWithFormat ("kdp read memory failed (error %u)", kdp_error);
713 error.SetErrorString ("kdp read memory failed");
717 error.SetErrorString ("failed to send packet");
724 CommunicationKDP::SendRequestWriteMemory (lldb::addr_t addr,
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);
740 DataExtractor reply_packet;
741 if (SendRequestAndGetReply (command, request_packet, reply_packet))
743 lldb::offset_t offset = 8;
744 uint32_t kdp_error = reply_packet.GetU32 (&offset);
746 error.SetErrorStringWithFormat ("kdp write memory failed (error %u)", kdp_error);
755 error.SetErrorString ("failed to send packet");
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,
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);
774 if (SendRequestAndGetReply (command, request_packet, reply_packet))
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);
788 error.SetErrorString ("failed to send packet");
795 CommunicationKDP::GetCommandAsCString (uint8_t command)
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";
836 CommunicationKDP::DumpPacket (Stream &s, const void *data, uint32_t data_len)
838 DataExtractor extractor (data, data_len, m_byte_order, m_addr_byte_size);
839 DumpPacket (s, extractor);
843 CommunicationKDP::DumpPacket (Stream &s, const DataExtractor& packet)
845 const char *error_desc = NULL;
846 if (packet.GetByteSize() < 8)
848 error_desc = "error: invalid packet (too short): ";
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);
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 ",
864 is_reply ? "<--" : "-->",
873 // Dump request reply packets
876 // Commands that return a single 32 bit error
880 case KDP_BREAKPOINT_SET:
881 case KDP_BREAKPOINT_REMOVE:
882 case KDP_BREAKPOINT_SET64:
883 case KDP_BREAKPOINT_REMOVE64:
886 case KDP_WRITEIOPORT:
889 const uint32_t error = packet.GetU32 (&offset);
890 s.Printf(" (error=0x%8.8x)", error);
900 case KDP_TERMINATION:
901 // No return value for the reply, just the header to ack
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);
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);
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)
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));
938 case KDP_READPHYSMEM64:
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);
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
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);
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
972 case KDP_KERNELVERSION:
974 const char *kernel_version = packet.PeekCStr(8);
975 s.Printf(" (version = \"%s\")", kernel_version);
981 const uint32_t max_bytes = packet.GetU32 (&offset);
982 s.Printf(" (max_bytes = 0x%8.8x (%u))", max_bytes, max_bytes);
987 const char *path = packet.GetCStr(&offset);
988 s.Printf(" (path = \"%s\")", path);
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);
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
1011 const uint32_t count = packet.GetByteSize() - offset;
1012 s.Printf(" (count = %u, bytes = \n", count);
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
1027 s.Printf(" (add support for dumping this packet reply!!!");
1034 // Dump request packets
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));
1045 case KDP_DISCONNECT:
1046 case KDP_HOSTREBOOT:
1050 case KDP_KERNELVERSION:
1054 // No args, just the header in the request...
1055 s.PutCString(" ()");
1058 case KDP_RESUMECPUS:
1060 const uint32_t cpu_mask = packet.GetU32 (&offset);
1061 s.Printf(" (cpu_mask = 0x%8.8x)", cpu_mask);
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;
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);
1080 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
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;
1093 case KDP_READPHYSMEM64:
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;
1103 case KDP_WRITEMEM64:
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);
1109 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
1113 case KDP_WRITEPHYSMEM64:
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);
1120 DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr);
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);
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);
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
1151 case KDP_BREAKPOINT_SET:
1152 case KDP_BREAKPOINT_REMOVE:
1154 const uint32_t addr = packet.GetU32 (&offset);
1155 s.Printf(" (addr = 0x%8.8x)", addr);
1159 case KDP_BREAKPOINT_SET64:
1160 case KDP_BREAKPOINT_REMOVE64:
1162 const uint64_t addr = packet.GetU64 (&offset);
1163 s.Printf(" (addr = 0x%16.16" PRIx64 ")", addr);
1170 const char *path = packet.GetCStr(&offset);
1171 s.Printf(" (path = \"%s\")", path);
1177 const uint32_t count = packet.GetU32 (&offset);
1179 for (uint32_t i=0; i<count; ++i)
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;
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;
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);
1208 case KDP_TERMINATION:
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);
1218 const uint16_t reply_port = ntohs(packet.GetU16 (&offset));
1219 s.Printf(" (reply_port = %u)", reply_port);
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);
1231 case KDP_WRITEMSR64:
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);
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
1249 case KDP_READIOPORT:
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);
1258 case KDP_WRITEIOPORT:
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);
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
1278 const uint32_t count = packet.GetByteSize() - offset;
1279 s.Printf(" (count = %u, bytes = \n", count);
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
1298 error_desc = "error: invalid packet command: ";
1304 s.PutCString (error_desc);
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
1318 CommunicationKDP::SendRequestReadRegisters (uint32_t cpu,
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))
1334 lldb::offset_t offset = 8;
1335 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1336 uint32_t src_len = reply_packet.GetByteSize() - 12;
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);
1344 ::memcpy (dst, src, bytes_to_copy);
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
1352 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error);
1354 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u", cpu, flavor);
1358 error.SetErrorString ("failed to send packet");
1364 CommunicationKDP::SendRequestWriteRegisters (uint32_t cpu,
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))
1381 lldb::offset_t offset = 8;
1382 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1385 error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error);
1389 error.SetErrorString ("failed to send packet");
1396 CommunicationKDP::SendRequestResume ()
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());
1404 DataExtractor reply_packet;
1405 if (SendRequestAndGetReply (command, request_packet, reply_packet))
1411 CommunicationKDP::SendRequestBreakpoint (bool set, addr_t addr)
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);
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);
1423 DataExtractor reply_packet;
1424 if (SendRequestAndGetReply (command, request_packet, reply_packet))
1426 lldb::offset_t offset = 8;
1427 uint32_t kdp_error = reply_packet.GetU32 (&offset);
1435 CommunicationKDP::SendRequestSuspend ()
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))