Greg Clayton | 269f91e | 2011-07-15 18:02:58 +0000 | [diff] [blame] | 1 | //===-- CommunicationKDP.cpp ------------------------------------*- C++ -*-===// |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | |
| 11 | #include "CommunicationKDP.h" |
| 12 | |
| 13 | // C Includes |
Jason Molenda | fac2e62 | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 14 | #include <errno.h> |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 15 | #include <limits.h> |
| 16 | #include <string.h> |
| 17 | |
| 18 | // C++ Includes |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 19 | #include "llvm/Support/MachO.h" |
| 20 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 21 | // Other libraries and framework includes |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 22 | #include "lldb/Core/DataBufferHeap.h" |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 23 | #include "lldb/Core/DataExtractor.h" |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 24 | #include "lldb/Core/Log.h" |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 25 | #include "lldb/Core/State.h" |
Jason Molenda | fac2e62 | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 26 | #include "lldb/Core/UUID.h" |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 27 | #include "lldb/Host/FileSpec.h" |
| 28 | #include "lldb/Host/Host.h" |
| 29 | #include "lldb/Host/TimeValue.h" |
| 30 | #include "lldb/Target/Process.h" |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 31 | |
| 32 | // Project includes |
| 33 | #include "ProcessKDPLog.h" |
| 34 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 35 | using namespace lldb; |
| 36 | using namespace lldb_private; |
| 37 | |
| 38 | //---------------------------------------------------------------------- |
| 39 | // CommunicationKDP constructor |
| 40 | //---------------------------------------------------------------------- |
| 41 | CommunicationKDP::CommunicationKDP (const char *comm_name) : |
| 42 | Communication(comm_name), |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 43 | m_addr_byte_size (4), |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 44 | m_byte_order (eByteOrderLittle), |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 45 | m_packet_timeout (1), |
| 46 | m_sequence_mutex (Mutex::eMutexTypeRecursive), |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 47 | m_is_running (false), |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 48 | m_session_key (0u), |
| 49 | m_request_sequence_id (0u), |
| 50 | m_exception_sequence_id (0u), |
| 51 | m_kdp_version_version (0u), |
| 52 | m_kdp_version_feature (0u), |
| 53 | m_kdp_hostinfo_cpu_mask (0u), |
| 54 | m_kdp_hostinfo_cpu_type (0u), |
| 55 | m_kdp_hostinfo_cpu_subtype (0u) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 56 | { |
| 57 | } |
| 58 | |
| 59 | //---------------------------------------------------------------------- |
| 60 | // Destructor |
| 61 | //---------------------------------------------------------------------- |
| 62 | CommunicationKDP::~CommunicationKDP() |
| 63 | { |
| 64 | if (IsConnected()) |
| 65 | { |
| 66 | Disconnect(); |
| 67 | } |
| 68 | } |
| 69 | |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 70 | bool |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 71 | CommunicationKDP::SendRequestPacket (const PacketStreamType &request_packet) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 72 | { |
| 73 | Mutex::Locker locker(m_sequence_mutex); |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 74 | return SendRequestPacketNoLock (request_packet); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 77 | #if 0 |
| 78 | typedef struct { |
| 79 | uint8_t request; // Either: CommandType | ePacketTypeRequest, or CommandType | ePacketTypeReply |
| 80 | uint8_t sequence; |
| 81 | uint16_t length; // Length of entire packet including this header |
| 82 | uint32_t key; // Session key |
| 83 | } kdp_hdr_t; |
| 84 | #endif |
| 85 | |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 86 | void |
Greg Clayton | 307c7fd | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 87 | CommunicationKDP::MakeRequestPacketHeader (CommandType request_type, |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 88 | PacketStreamType &request_packet, |
| 89 | uint16_t request_length) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 90 | { |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 91 | request_packet.Clear(); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 92 | request_packet.PutHex8 (request_type | ePacketTypeRequest); // Set the request type |
| 93 | request_packet.PutHex8 (m_request_sequence_id++); // Sequence number |
| 94 | request_packet.PutHex16 (request_length); // Length of the packet including this header |
| 95 | request_packet.PutHex32 (m_session_key); // Session key |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 98 | bool |
| 99 | CommunicationKDP::SendRequestAndGetReply (const CommandType command, |
| 100 | const uint8_t request_sequence_id, |
| 101 | const PacketStreamType &request_packet, |
| 102 | DataExtractor &reply_packet) |
| 103 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 104 | if (IsRunning()) |
| 105 | { |
| 106 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS)); |
| 107 | if (log) |
| 108 | { |
| 109 | PacketStreamType log_strm; |
| 110 | DumpPacket (log_strm, request_packet.GetData(), request_packet.GetSize()); |
| 111 | log->Printf("error: kdp running, not sending packet: %.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData()); |
| 112 | } |
| 113 | return false; |
| 114 | } |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 115 | |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 116 | Mutex::Locker locker(m_sequence_mutex); |
| 117 | #ifdef LLDB_CONFIGURATION_DEBUG |
| 118 | // NOTE: this only works for packets that are in native endian byte order |
| 119 | assert (request_packet.GetSize() == *((uint16_t *)(request_packet.GetData() + 2))); |
| 120 | #endif |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 121 | if (SendRequestPacketNoLock(request_packet)) |
| 122 | { |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 123 | if (WaitForPacketWithTimeoutMicroSecondsNoLock (reply_packet, GetPacketTimeoutInMicroSeconds ())) |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 124 | { |
| 125 | uint32_t offset = 0; |
| 126 | const uint8_t reply_command = reply_packet.GetU8 (&offset); |
| 127 | const uint8_t reply_sequence_id = reply_packet.GetU8 (&offset); |
| 128 | if ((reply_command & eCommandTypeMask) == command) |
| 129 | { |
| 130 | if (request_sequence_id == reply_sequence_id) |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 131 | { |
| 132 | if (command == KDP_RESUMECPUS) |
| 133 | m_is_running.SetValue(true, eBroadcastAlways); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 134 | return true; |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 135 | } |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
| 139 | reply_packet.Clear(); |
| 140 | return false; |
| 141 | } |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 142 | |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 143 | bool |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 144 | CommunicationKDP::SendRequestPacketNoLock (const PacketStreamType &request_packet) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 145 | { |
| 146 | if (IsConnected()) |
| 147 | { |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 148 | const char *packet_data = request_packet.GetData(); |
| 149 | const size_t packet_size = request_packet.GetSize(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 150 | |
| 151 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS)); |
| 152 | if (log) |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 153 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 154 | PacketStreamType log_strm; |
| 155 | DumpPacket (log_strm, packet_data, packet_size); |
| 156 | log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData()); |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 157 | } |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 158 | ConnectionStatus status = eConnectionStatusSuccess; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 159 | |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 160 | size_t bytes_written = Write (packet_data, |
| 161 | packet_size, |
| 162 | status, |
| 163 | NULL); |
| 164 | |
| 165 | if (bytes_written == packet_size) |
| 166 | return true; |
| 167 | |
| 168 | if (log) |
Greg Clayton | 851e30e | 2012-09-18 18:04:04 +0000 | [diff] [blame] | 169 | log->Printf ("error: failed to send packet entire packet %llu of %llu bytes sent", (uint64_t)bytes_written, (uint64_t)packet_size); |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 170 | } |
| 171 | return false; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | bool |
Greg Clayton | 516f084 | 2012-04-11 00:24:49 +0000 | [diff] [blame] | 175 | CommunicationKDP::GetSequenceMutex (Mutex::Locker& locker) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 176 | { |
Jim Ingham | 1b584eb | 2012-05-04 23:02:50 +0000 | [diff] [blame] | 177 | return locker.TryLock (m_sequence_mutex); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | |
| 181 | bool |
| 182 | CommunicationKDP::WaitForNotRunningPrivate (const TimeValue *timeout_ptr) |
| 183 | { |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 184 | return m_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | size_t |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 188 | CommunicationKDP::WaitForPacketWithTimeoutMicroSeconds (DataExtractor &packet, uint32_t timeout_usec) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 189 | { |
| 190 | Mutex::Locker locker(m_sequence_mutex); |
| 191 | return WaitForPacketWithTimeoutMicroSecondsNoLock (packet, timeout_usec); |
| 192 | } |
| 193 | |
| 194 | size_t |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 195 | CommunicationKDP::WaitForPacketWithTimeoutMicroSecondsNoLock (DataExtractor &packet, uint32_t timeout_usec) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 196 | { |
| 197 | uint8_t buffer[8192]; |
| 198 | Error error; |
| 199 | |
| 200 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS | KDP_LOG_VERBOSE)); |
| 201 | |
| 202 | // Check for a packet from our cache first without trying any reading... |
| 203 | if (CheckForPacket (NULL, 0, packet)) |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 204 | return packet.GetByteSize(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 205 | |
| 206 | bool timed_out = false; |
| 207 | while (IsConnected() && !timed_out) |
| 208 | { |
Johnny Chen | 1e3a522 | 2011-07-21 19:00:59 +0000 | [diff] [blame] | 209 | lldb::ConnectionStatus status = eConnectionStatusNoConnection; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 210 | size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error); |
| 211 | |
| 212 | if (log) |
Greg Clayton | 851e30e | 2012-09-18 18:04:04 +0000 | [diff] [blame] | 213 | log->Printf ("%s: Read (buffer, (sizeof(buffer), timeout_usec = 0x%x, status = %s, error = %s) => bytes_read = %llu", |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 214 | __PRETTY_FUNCTION__, |
| 215 | timeout_usec, |
| 216 | Communication::ConnectionStatusAsCString (status), |
| 217 | error.AsCString(), |
Greg Clayton | 851e30e | 2012-09-18 18:04:04 +0000 | [diff] [blame] | 218 | (uint64_t)bytes_read); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 219 | |
| 220 | if (bytes_read > 0) |
| 221 | { |
| 222 | if (CheckForPacket (buffer, bytes_read, packet)) |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 223 | return packet.GetByteSize(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 224 | } |
| 225 | else |
| 226 | { |
| 227 | switch (status) |
| 228 | { |
| 229 | case eConnectionStatusTimedOut: |
| 230 | timed_out = true; |
| 231 | break; |
| 232 | case eConnectionStatusSuccess: |
| 233 | //printf ("status = success but error = %s\n", error.AsCString("<invalid>")); |
| 234 | break; |
| 235 | |
| 236 | case eConnectionStatusEndOfFile: |
| 237 | case eConnectionStatusNoConnection: |
| 238 | case eConnectionStatusLostConnection: |
| 239 | case eConnectionStatusError: |
| 240 | Disconnect(); |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | packet.Clear (); |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | bool |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 250 | CommunicationKDP::CheckForPacket (const uint8_t *src, size_t src_len, DataExtractor &packet) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 251 | { |
| 252 | // Put the packet data into the buffer in a thread safe fashion |
| 253 | Mutex::Locker locker(m_bytes_mutex); |
| 254 | |
| 255 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS)); |
| 256 | |
| 257 | if (src && src_len > 0) |
| 258 | { |
| 259 | if (log && log->GetVerbose()) |
| 260 | { |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 261 | PacketStreamType log_strm; |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 262 | DataExtractor::DumpHexBytes (&log_strm, src, src_len, UINT32_MAX, LLDB_INVALID_ADDRESS); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 263 | log->Printf ("CommunicationKDP::%s adding %u bytes: %s", |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 264 | __FUNCTION__, |
| 265 | (uint32_t)src_len, |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 266 | log_strm.GetData()); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 267 | } |
| 268 | m_bytes.append ((const char *)src, src_len); |
| 269 | } |
| 270 | |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 271 | // Make sure we at least have enough bytes for a packet header |
| 272 | const size_t bytes_available = m_bytes.size(); |
| 273 | if (bytes_available >= 8) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 274 | { |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 275 | packet.SetData (&m_bytes[0], bytes_available, m_byte_order); |
| 276 | uint32_t offset = 0; |
| 277 | uint8_t reply_command = packet.GetU8(&offset); |
| 278 | switch (reply_command) |
| 279 | { |
Greg Clayton | ea63601 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 280 | case ePacketTypeRequest | KDP_EXCEPTION: |
| 281 | case ePacketTypeRequest | KDP_TERMINATION: |
| 282 | // We got an exception request, so be sure to send an ACK |
| 283 | { |
| 284 | PacketStreamType request_ack_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
| 285 | // Set the reply but and make the ACK packet |
| 286 | request_ack_packet.PutHex8 (reply_command | ePacketTypeReply); |
| 287 | request_ack_packet.PutHex8 (packet.GetU8(&offset)); |
| 288 | request_ack_packet.PutHex16 (packet.GetU16(&offset)); |
| 289 | request_ack_packet.PutHex32 (packet.GetU32(&offset)); |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 290 | m_is_running.SetValue(false, eBroadcastAlways); |
Greg Clayton | ea63601 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 291 | // Ack to the exception or termination |
| 292 | SendRequestPacketNoLock (request_ack_packet); |
| 293 | } |
| 294 | // Fall through to case below to get packet contents |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 295 | case ePacketTypeReply | KDP_CONNECT: |
| 296 | case ePacketTypeReply | KDP_DISCONNECT: |
| 297 | case ePacketTypeReply | KDP_HOSTINFO: |
| 298 | case ePacketTypeReply | KDP_VERSION: |
| 299 | case ePacketTypeReply | KDP_MAXBYTES: |
| 300 | case ePacketTypeReply | KDP_READMEM: |
| 301 | case ePacketTypeReply | KDP_WRITEMEM: |
| 302 | case ePacketTypeReply | KDP_READREGS: |
| 303 | case ePacketTypeReply | KDP_WRITEREGS: |
| 304 | case ePacketTypeReply | KDP_LOAD: |
| 305 | case ePacketTypeReply | KDP_IMAGEPATH: |
| 306 | case ePacketTypeReply | KDP_SUSPEND: |
| 307 | case ePacketTypeReply | KDP_RESUMECPUS: |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 308 | case ePacketTypeReply | KDP_BREAKPOINT_SET: |
| 309 | case ePacketTypeReply | KDP_BREAKPOINT_REMOVE: |
| 310 | case ePacketTypeReply | KDP_REGIONS: |
| 311 | case ePacketTypeReply | KDP_REATTACH: |
| 312 | case ePacketTypeReply | KDP_HOSTREBOOT: |
| 313 | case ePacketTypeReply | KDP_READMEM64: |
| 314 | case ePacketTypeReply | KDP_WRITEMEM64: |
| 315 | case ePacketTypeReply | KDP_BREAKPOINT_SET64: |
| 316 | case ePacketTypeReply | KDP_BREAKPOINT_REMOVE64: |
| 317 | case ePacketTypeReply | KDP_KERNELVERSION: |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 318 | { |
| 319 | offset = 2; |
| 320 | const uint16_t length = packet.GetU16 (&offset); |
| 321 | if (length <= bytes_available) |
| 322 | { |
| 323 | // We have an entire packet ready, we need to copy the data |
| 324 | // bytes into a buffer that will be owned by the packet and |
| 325 | // erase the bytes from our communcation buffer "m_bytes" |
| 326 | packet.SetData (DataBufferSP (new DataBufferHeap (&m_bytes[0], length))); |
| 327 | m_bytes.erase (0, length); |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 328 | |
| 329 | if (log) |
| 330 | { |
| 331 | PacketStreamType log_strm; |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 332 | DumpPacket (log_strm, packet); |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 333 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 334 | log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData()); |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 335 | } |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 336 | return true; |
| 337 | } |
| 338 | } |
| 339 | break; |
| 340 | |
| 341 | default: |
| 342 | // Unrecognized reply command byte, erase this byte and try to get back on track |
| 343 | if (log) |
| 344 | log->Printf ("CommunicationKDP::%s: tossing junk byte: 0x%2.2x", |
| 345 | __FUNCTION__, |
| 346 | (uint8_t)m_bytes[0]); |
| 347 | m_bytes.erase(0, 1); |
| 348 | break; |
| 349 | } |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 350 | } |
| 351 | packet.Clear(); |
| 352 | return false; |
| 353 | } |
| 354 | |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 355 | |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 356 | bool |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 357 | CommunicationKDP::SendRequestConnect (uint16_t reply_port, |
| 358 | uint16_t exc_port, |
| 359 | const char *greeting) |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 360 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 361 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 362 | if (greeting == NULL) |
| 363 | greeting = ""; |
| 364 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 365 | const CommandType command = KDP_CONNECT; |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 366 | // Length is 82 uint16_t and the length of the greeting C string with the terminating NULL |
| 367 | const uint32_t command_length = 8 + 2 + 2 + ::strlen(greeting) + 1; |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 368 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 369 | MakeRequestPacketHeader (command, request_packet, command_length); |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 370 | // Always send connect ports as little endian |
| 371 | request_packet.SetByteOrder (eByteOrderLittle); |
| 372 | request_packet.PutHex16 (reply_port); |
| 373 | request_packet.PutHex16 (exc_port); |
| 374 | request_packet.SetByteOrder (m_byte_order); |
| 375 | request_packet.PutCString (greeting); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 376 | DataExtractor reply_packet; |
| 377 | return SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet); |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 380 | void |
| 381 | CommunicationKDP::ClearKDPSettings () |
| 382 | { |
| 383 | m_request_sequence_id = 0; |
| 384 | m_kdp_version_version = 0; |
| 385 | m_kdp_version_feature = 0; |
| 386 | m_kdp_hostinfo_cpu_mask = 0; |
| 387 | m_kdp_hostinfo_cpu_type = 0; |
| 388 | m_kdp_hostinfo_cpu_subtype = 0; |
| 389 | } |
| 390 | |
| 391 | bool |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 392 | CommunicationKDP::SendRequestReattach (uint16_t reply_port) |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 393 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 394 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 395 | const CommandType command = KDP_REATTACH; |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 396 | // Length is 8 bytes for the header plus 2 bytes for the reply UDP port |
| 397 | const uint32_t command_length = 8 + 2; |
| 398 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 399 | MakeRequestPacketHeader (command, request_packet, command_length); |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 400 | // Always send connect ports as little endian |
| 401 | request_packet.SetByteOrder (eByteOrderLittle); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 402 | request_packet.PutHex16(reply_port); |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 403 | request_packet.SetByteOrder (m_byte_order); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 404 | DataExtractor reply_packet; |
| 405 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 406 | { |
| 407 | // Reset the sequence ID to zero for reattach |
| 408 | ClearKDPSettings (); |
| 409 | uint32_t offset = 4; |
| 410 | m_session_key = reply_packet.GetU32 (&offset); |
| 411 | return true; |
| 412 | } |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | uint32_t |
| 417 | CommunicationKDP::GetVersion () |
| 418 | { |
| 419 | if (!VersionIsValid()) |
| 420 | SendRequestVersion(); |
| 421 | return m_kdp_version_version; |
| 422 | } |
| 423 | |
| 424 | uint32_t |
| 425 | CommunicationKDP::GetFeatureFlags () |
| 426 | { |
| 427 | if (!VersionIsValid()) |
| 428 | SendRequestVersion(); |
| 429 | return m_kdp_version_feature; |
| 430 | } |
| 431 | |
| 432 | bool |
| 433 | CommunicationKDP::SendRequestVersion () |
| 434 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 435 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 436 | const CommandType command = KDP_VERSION; |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 437 | const uint32_t command_length = 8; |
| 438 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 439 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 440 | DataExtractor reply_packet; |
| 441 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 442 | { |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 443 | uint32_t offset = 8; |
| 444 | m_kdp_version_version = reply_packet.GetU32 (&offset); |
| 445 | m_kdp_version_feature = reply_packet.GetU32 (&offset); |
| 446 | return true; |
| 447 | } |
| 448 | return false; |
| 449 | } |
| 450 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 451 | #if 0 // Disable KDP_IMAGEPATH for now, it seems to hang the KDP connection... |
| 452 | const char * |
| 453 | CommunicationKDP::GetImagePath () |
| 454 | { |
| 455 | if (m_image_path.empty()) |
| 456 | SendRequestImagePath(); |
| 457 | return m_image_path.c_str(); |
| 458 | } |
| 459 | |
| 460 | bool |
| 461 | CommunicationKDP::SendRequestImagePath () |
| 462 | { |
| 463 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
| 464 | const CommandType command = KDP_IMAGEPATH; |
| 465 | const uint32_t command_length = 8; |
| 466 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 467 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 468 | DataExtractor reply_packet; |
| 469 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 470 | { |
| 471 | const char *path = reply_packet.PeekCStr(8); |
| 472 | if (path && path[0]) |
| 473 | m_kernel_version.assign (path); |
| 474 | return true; |
| 475 | } |
| 476 | return false; |
| 477 | } |
| 478 | #endif |
| 479 | |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 480 | uint32_t |
| 481 | CommunicationKDP::GetCPUMask () |
| 482 | { |
| 483 | if (!HostInfoIsValid()) |
| 484 | SendRequestHostInfo(); |
| 485 | return m_kdp_hostinfo_cpu_mask; |
| 486 | } |
| 487 | |
| 488 | uint32_t |
| 489 | CommunicationKDP::GetCPUType () |
| 490 | { |
| 491 | if (!HostInfoIsValid()) |
| 492 | SendRequestHostInfo(); |
| 493 | return m_kdp_hostinfo_cpu_type; |
| 494 | } |
| 495 | |
| 496 | uint32_t |
| 497 | CommunicationKDP::GetCPUSubtype () |
| 498 | { |
| 499 | if (!HostInfoIsValid()) |
| 500 | SendRequestHostInfo(); |
| 501 | return m_kdp_hostinfo_cpu_subtype; |
| 502 | } |
| 503 | |
Jason Molenda | fac2e62 | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 504 | lldb_private::UUID |
| 505 | CommunicationKDP::GetUUID () |
| 506 | { |
| 507 | UUID uuid; |
| 508 | if (GetKernelVersion() == NULL) |
| 509 | return uuid; |
| 510 | |
| 511 | if (m_kernel_version.find("UUID=") == std::string::npos) |
| 512 | return uuid; |
| 513 | |
| 514 | size_t p = m_kernel_version.find("UUID=") + strlen ("UUID="); |
| 515 | std::string uuid_str = m_kernel_version.substr(p, 36); |
| 516 | if (uuid_str.size() < 32) |
| 517 | return uuid; |
| 518 | |
| 519 | if (uuid.SetFromCString (uuid_str.c_str()) == 0) |
| 520 | { |
| 521 | UUID invalid_uuid; |
| 522 | return invalid_uuid; |
| 523 | } |
| 524 | |
| 525 | return uuid; |
| 526 | } |
| 527 | |
Jason Molenda | d06bb50 | 2012-10-25 00:25:13 +0000 | [diff] [blame^] | 528 | bool |
| 529 | CommunicationKDP::RemoteIsEFI () |
| 530 | { |
| 531 | if (GetKernelVersion() == NULL) |
| 532 | return false; |
| 533 | if (strncmp (m_kernel_version.c_str(), "EFI", 3) == 0) |
| 534 | return true; |
| 535 | else |
| 536 | return false; |
| 537 | } |
| 538 | |
Jason Molenda | fac2e62 | 2012-09-29 04:02:01 +0000 | [diff] [blame] | 539 | lldb::addr_t |
| 540 | CommunicationKDP::GetLoadAddress () |
| 541 | { |
| 542 | if (GetKernelVersion() == NULL) |
| 543 | return LLDB_INVALID_ADDRESS; |
| 544 | |
| 545 | if (m_kernel_version.find("stext=") == std::string::npos) |
| 546 | return LLDB_INVALID_ADDRESS; |
| 547 | size_t p = m_kernel_version.find("stext=") + strlen ("stext="); |
| 548 | if (m_kernel_version[p] != '0' || m_kernel_version[p + 1] != 'x') |
| 549 | return LLDB_INVALID_ADDRESS; |
| 550 | |
| 551 | addr_t kernel_load_address; |
| 552 | errno = 0; |
| 553 | kernel_load_address = ::strtoul (m_kernel_version.c_str() + p, NULL, 16); |
| 554 | if (errno != 0 || kernel_load_address == 0) |
| 555 | return LLDB_INVALID_ADDRESS; |
| 556 | |
| 557 | return kernel_load_address; |
| 558 | } |
| 559 | |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 560 | bool |
| 561 | CommunicationKDP::SendRequestHostInfo () |
| 562 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 563 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 564 | const CommandType command = KDP_HOSTINFO; |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 565 | const uint32_t command_length = 8; |
| 566 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 567 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 568 | DataExtractor reply_packet; |
| 569 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 570 | { |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 571 | uint32_t offset = 8; |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 572 | m_kdp_hostinfo_cpu_mask = reply_packet.GetU32 (&offset); |
| 573 | m_kdp_hostinfo_cpu_type = reply_packet.GetU32 (&offset); |
| 574 | m_kdp_hostinfo_cpu_subtype = reply_packet.GetU32 (&offset); |
| 575 | |
| 576 | ArchSpec kernel_arch; |
| 577 | kernel_arch.SetArchitecture (eArchTypeMachO, |
| 578 | m_kdp_hostinfo_cpu_type, |
| 579 | m_kdp_hostinfo_cpu_subtype); |
| 580 | |
| 581 | m_addr_byte_size = kernel_arch.GetAddressByteSize(); |
| 582 | m_byte_order = kernel_arch.GetByteOrder(); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 583 | return true; |
| 584 | } |
| 585 | return false; |
| 586 | } |
| 587 | |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 588 | const char * |
| 589 | CommunicationKDP::GetKernelVersion () |
| 590 | { |
| 591 | if (m_kernel_version.empty()) |
| 592 | SendRequestKernelVersion (); |
| 593 | return m_kernel_version.c_str(); |
| 594 | } |
| 595 | |
| 596 | bool |
| 597 | CommunicationKDP::SendRequestKernelVersion () |
| 598 | { |
| 599 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 600 | const CommandType command = KDP_KERNELVERSION; |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 601 | const uint32_t command_length = 8; |
| 602 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 603 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 604 | DataExtractor reply_packet; |
| 605 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 606 | { |
| 607 | const char *kernel_version_cstr = reply_packet.PeekCStr(8); |
| 608 | if (kernel_version_cstr && kernel_version_cstr[0]) |
| 609 | m_kernel_version.assign (kernel_version_cstr); |
| 610 | return true; |
| 611 | } |
| 612 | return false; |
| 613 | } |
| 614 | |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 615 | bool |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 616 | CommunicationKDP::SendRequestDisconnect () |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 617 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 618 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 619 | const CommandType command = KDP_DISCONNECT; |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 620 | const uint32_t command_length = 8; |
| 621 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 622 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 623 | DataExtractor reply_packet; |
| 624 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 625 | { |
| 626 | // Are we supposed to get a reply for disconnect? |
| 627 | } |
| 628 | ClearKDPSettings (); |
| 629 | return true; |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 632 | uint32_t |
| 633 | CommunicationKDP::SendRequestReadMemory (lldb::addr_t addr, |
| 634 | void *dst, |
| 635 | uint32_t dst_len, |
| 636 | Error &error) |
| 637 | { |
| 638 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
| 639 | bool use_64 = (GetVersion() >= 11); |
| 640 | uint32_t command_addr_byte_size = use_64 ? 8 : 4; |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 641 | const CommandType command = use_64 ? KDP_READMEM64 : KDP_READMEM; |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 642 | // Size is header + address size + uint32_t length |
| 643 | const uint32_t command_length = 8 + command_addr_byte_size + 4; |
| 644 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 645 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 646 | request_packet.PutMaxHex64 (addr, command_addr_byte_size); |
| 647 | request_packet.PutHex32 (dst_len); |
| 648 | DataExtractor reply_packet; |
| 649 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 650 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 651 | uint32_t offset = 8; |
| 652 | uint32_t kdp_error = reply_packet.GetU32 (&offset); |
| 653 | uint32_t src_len = reply_packet.GetByteSize() - 12; |
| 654 | |
| 655 | if (src_len > 0) |
| 656 | { |
| 657 | const void *src = reply_packet.GetData(&offset, src_len); |
| 658 | if (src) |
| 659 | { |
| 660 | ::memcpy (dst, src, src_len); |
| 661 | error.Clear(); |
| 662 | return src_len; |
| 663 | } |
| 664 | } |
| 665 | if (kdp_error) |
| 666 | error.SetErrorStringWithFormat ("kdp read memory failed (error %u)", kdp_error); |
| 667 | else |
| 668 | error.SetErrorString ("kdp read memory failed"); |
| 669 | } |
Greg Clayton | 307c7fd | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 670 | else |
| 671 | { |
| 672 | error.SetErrorString ("failed to send packet"); |
| 673 | } |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 674 | return 0; |
| 675 | } |
| 676 | |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 677 | |
| 678 | uint32_t |
| 679 | CommunicationKDP::SendRequestWriteMemory (lldb::addr_t addr, |
| 680 | const void *src, |
| 681 | uint32_t src_len, |
| 682 | Error &error) |
| 683 | { |
| 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; |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 687 | const CommandType command = use_64 ? KDP_WRITEMEM64 : KDP_WRITEMEM; |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 688 | // Size is header + address size + uint32_t length |
Greg Clayton | fe3150f | 2012-10-12 23:24:05 +0000 | [diff] [blame] | 689 | const uint32_t command_length = 8 + command_addr_byte_size + 4 + src_len; |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 690 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 691 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 692 | request_packet.PutMaxHex64 (addr, command_addr_byte_size); |
| 693 | request_packet.PutHex32 (src_len); |
Jason Molenda | 6237746 | 2012-10-19 02:16:22 +0000 | [diff] [blame] | 694 | request_packet.PutRawBytes(src, src_len); |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 695 | |
| 696 | DataExtractor reply_packet; |
| 697 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 698 | { |
| 699 | uint32_t offset = 8; |
| 700 | uint32_t kdp_error = reply_packet.GetU32 (&offset); |
| 701 | if (kdp_error) |
| 702 | error.SetErrorStringWithFormat ("kdp write memory failed (error %u)", kdp_error); |
| 703 | else |
| 704 | { |
| 705 | error.Clear(); |
| 706 | return src_len; |
| 707 | } |
| 708 | } |
Greg Clayton | 307c7fd | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 709 | else |
| 710 | { |
| 711 | error.SetErrorString ("failed to send packet"); |
| 712 | } |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 713 | return 0; |
| 714 | } |
| 715 | |
Greg Clayton | 307c7fd | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 716 | bool |
| 717 | CommunicationKDP::SendRawRequest (uint8_t command_byte, |
| 718 | const void *src, // Raw packet payload bytes |
| 719 | uint32_t src_len, // Raw packet payload length |
| 720 | DataExtractor &reply_packet, |
| 721 | Error &error) |
| 722 | { |
| 723 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
| 724 | // Size is header + address size + uint32_t length |
| 725 | const uint32_t command_length = 8 + src_len; |
| 726 | const CommandType command = (CommandType)command_byte; |
| 727 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 728 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 729 | request_packet.PutRawBytes(src, src_len); |
| 730 | |
| 731 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 732 | { |
| 733 | uint32_t offset = 8; |
| 734 | uint32_t kdp_error = reply_packet.GetU32 (&offset); |
| 735 | if (kdp_error) |
| 736 | error.SetErrorStringWithFormat ("request packet 0x%8.8x failed (error %u)", command_byte, kdp_error); |
| 737 | else |
| 738 | { |
| 739 | error.Clear(); |
| 740 | return true; |
| 741 | } |
| 742 | } |
| 743 | else |
| 744 | { |
| 745 | error.SetErrorString ("failed to send packet"); |
| 746 | } |
| 747 | return false; |
| 748 | } |
| 749 | |
| 750 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 751 | const char * |
| 752 | CommunicationKDP::GetCommandAsCString (uint8_t command) |
| 753 | { |
| 754 | switch (command) |
| 755 | { |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 756 | case KDP_CONNECT: return "KDP_CONNECT"; |
| 757 | case KDP_DISCONNECT: return "KDP_DISCONNECT"; |
| 758 | case KDP_HOSTINFO: return "KDP_HOSTINFO"; |
| 759 | case KDP_VERSION: return "KDP_VERSION"; |
| 760 | case KDP_MAXBYTES: return "KDP_MAXBYTES"; |
| 761 | case KDP_READMEM: return "KDP_READMEM"; |
| 762 | case KDP_WRITEMEM: return "KDP_WRITEMEM"; |
| 763 | case KDP_READREGS: return "KDP_READREGS"; |
| 764 | case KDP_WRITEREGS: return "KDP_WRITEREGS"; |
| 765 | case KDP_LOAD: return "KDP_LOAD"; |
| 766 | case KDP_IMAGEPATH: return "KDP_IMAGEPATH"; |
| 767 | case KDP_SUSPEND: return "KDP_SUSPEND"; |
| 768 | case KDP_RESUMECPUS: return "KDP_RESUMECPUS"; |
| 769 | case KDP_EXCEPTION: return "KDP_EXCEPTION"; |
| 770 | case KDP_TERMINATION: return "KDP_TERMINATION"; |
| 771 | case KDP_BREAKPOINT_SET: return "KDP_BREAKPOINT_SET"; |
| 772 | case KDP_BREAKPOINT_REMOVE: return "KDP_BREAKPOINT_REMOVE"; |
| 773 | case KDP_REGIONS: return "KDP_REGIONS"; |
| 774 | case KDP_REATTACH: return "KDP_REATTACH"; |
| 775 | case KDP_HOSTREBOOT: return "KDP_HOSTREBOOT"; |
| 776 | case KDP_READMEM64: return "KDP_READMEM64"; |
| 777 | case KDP_WRITEMEM64: return "KDP_WRITEMEM64"; |
| 778 | case KDP_BREAKPOINT_SET64: return "KDP_BREAKPOINT64_SET"; |
| 779 | case KDP_BREAKPOINT_REMOVE64: return "KDP_BREAKPOINT64_REMOVE"; |
| 780 | case KDP_KERNELVERSION: return "KDP_KERNELVERSION"; |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 781 | } |
| 782 | return NULL; |
| 783 | } |
| 784 | |
| 785 | void |
| 786 | CommunicationKDP::DumpPacket (Stream &s, const void *data, uint32_t data_len) |
| 787 | { |
| 788 | DataExtractor extractor (data, data_len, m_byte_order, m_addr_byte_size); |
| 789 | DumpPacket (s, extractor); |
| 790 | } |
| 791 | |
| 792 | void |
| 793 | CommunicationKDP::DumpPacket (Stream &s, const DataExtractor& packet) |
| 794 | { |
| 795 | const char *error_desc = NULL; |
| 796 | if (packet.GetByteSize() < 8) |
| 797 | { |
| 798 | error_desc = "error: invalid packet (too short): "; |
| 799 | } |
| 800 | else |
| 801 | { |
| 802 | uint32_t offset = 0; |
| 803 | const uint8_t first_packet_byte = packet.GetU8 (&offset); |
| 804 | const uint8_t sequence_id = packet.GetU8 (&offset); |
| 805 | const uint16_t length = packet.GetU16 (&offset); |
| 806 | const uint32_t key = packet.GetU32 (&offset); |
| 807 | const CommandType command = ExtractCommand (first_packet_byte); |
| 808 | const char *command_name = GetCommandAsCString (command); |
| 809 | if (command_name) |
| 810 | { |
| 811 | const bool is_reply = ExtractIsReply(first_packet_byte); |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 812 | s.Printf ("(running=%i) %s %24s: 0x%2.2x 0x%2.2x 0x%4.4x 0x%8.8x ", |
| 813 | IsRunning(), |
| 814 | is_reply ? "<--" : "-->", |
| 815 | command_name, |
| 816 | first_packet_byte, |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 817 | sequence_id, |
| 818 | length, |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 819 | key); |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 820 | |
| 821 | if (is_reply) |
| 822 | { |
| 823 | // Dump request reply packets |
| 824 | switch (command) |
| 825 | { |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 826 | // Commands that return a single 32 bit error |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 827 | case KDP_CONNECT: |
| 828 | case KDP_WRITEMEM: |
| 829 | case KDP_WRITEMEM64: |
| 830 | case KDP_BREAKPOINT_SET: |
| 831 | case KDP_BREAKPOINT_REMOVE: |
| 832 | case KDP_BREAKPOINT_SET64: |
| 833 | case KDP_BREAKPOINT_REMOVE64: |
| 834 | case KDP_WRITEREGS: |
| 835 | case KDP_LOAD: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 836 | { |
| 837 | const uint32_t error = packet.GetU32 (&offset); |
| 838 | s.Printf(" (error=0x%8.8x)", error); |
| 839 | } |
| 840 | break; |
| 841 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 842 | case KDP_DISCONNECT: |
| 843 | case KDP_REATTACH: |
| 844 | case KDP_HOSTREBOOT: |
| 845 | case KDP_SUSPEND: |
| 846 | case KDP_RESUMECPUS: |
| 847 | case KDP_EXCEPTION: |
| 848 | case KDP_TERMINATION: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 849 | // No return value for the reply, just the header to ack |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 850 | s.PutCString(" ()"); |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 851 | break; |
| 852 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 853 | case KDP_HOSTINFO: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 854 | { |
| 855 | const uint32_t cpu_mask = packet.GetU32 (&offset); |
| 856 | const uint32_t cpu_type = packet.GetU32 (&offset); |
| 857 | const uint32_t cpu_subtype = packet.GetU32 (&offset); |
| 858 | s.Printf(" (cpu_mask=0x%8.8x, cpu_type=0x%8.8x, cpu_subtype=0x%8.8x)", cpu_mask, cpu_type, cpu_subtype); |
| 859 | } |
| 860 | break; |
| 861 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 862 | case KDP_VERSION: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 863 | { |
| 864 | const uint32_t version = packet.GetU32 (&offset); |
| 865 | const uint32_t feature = packet.GetU32 (&offset); |
| 866 | s.Printf(" (version=0x%8.8x, feature=0x%8.8x)", version, feature); |
| 867 | } |
| 868 | break; |
| 869 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 870 | case KDP_REGIONS: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 871 | { |
| 872 | const uint32_t region_count = packet.GetU32 (&offset); |
| 873 | s.Printf(" (count = %u", region_count); |
| 874 | for (uint32_t i=0; i<region_count; ++i) |
| 875 | { |
| 876 | const addr_t region_addr = packet.GetPointer (&offset); |
| 877 | const uint32_t region_size = packet.GetU32 (&offset); |
| 878 | const uint32_t region_prot = packet.GetU32 (&offset); |
Jason Molenda | 7e5fa7f | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 879 | s.Printf("\n\tregion[%llu] = { range = [0x%16.16llx - 0x%16.16llx), size = 0x%8.8x, prot = %s }", region_addr, region_addr, region_addr + region_size, region_size, GetPermissionsAsCString (region_prot)); |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | break; |
| 883 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 884 | case KDP_READMEM: |
| 885 | case KDP_READMEM64: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 886 | { |
| 887 | const uint32_t error = packet.GetU32 (&offset); |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 888 | const uint32_t count = packet.GetByteSize() - offset; |
Greg Clayton | 1bc04ed | 2011-07-29 22:26:36 +0000 | [diff] [blame] | 889 | s.Printf(" (error = 0x%8.8x:\n", error); |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 890 | if (count > 0) |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 891 | packet.Dump (&s, // Stream to dump to |
| 892 | offset, // Offset within "packet" |
| 893 | eFormatBytesWithASCII, // Format to use |
| 894 | 1, // Size of each item in bytes |
| 895 | count, // Number of items |
| 896 | 16, // Number per line |
| 897 | m_last_read_memory_addr, // Don't show addresses before each line |
| 898 | 0, 0); // No bitfields |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 899 | } |
| 900 | break; |
| 901 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 902 | case KDP_READREGS: |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 903 | { |
| 904 | const uint32_t error = packet.GetU32 (&offset); |
| 905 | const uint32_t count = packet.GetByteSize() - offset; |
Greg Clayton | 1bc04ed | 2011-07-29 22:26:36 +0000 | [diff] [blame] | 906 | s.Printf(" (error = 0x%8.8x regs:\n", error); |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 907 | if (count > 0) |
| 908 | packet.Dump (&s, // Stream to dump to |
| 909 | offset, // Offset within "packet" |
| 910 | eFormatHex, // Format to use |
| 911 | m_addr_byte_size, // Size of each item in bytes |
| 912 | count / m_addr_byte_size, // Number of items |
| 913 | 16 / m_addr_byte_size, // Number per line |
| 914 | LLDB_INVALID_ADDRESS, // Don't show addresses before each line |
| 915 | 0, 0); // No bitfields |
| 916 | } |
| 917 | break; |
| 918 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 919 | case KDP_KERNELVERSION: |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 920 | { |
| 921 | const char *kernel_version = packet.PeekCStr(8); |
| 922 | s.Printf(" (version = \"%s\")", kernel_version); |
| 923 | } |
| 924 | break; |
| 925 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 926 | case KDP_MAXBYTES: |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 927 | { |
| 928 | const uint32_t max_bytes = packet.GetU32 (&offset); |
| 929 | s.Printf(" (max_bytes = 0x%8.8x (%u))", max_bytes, max_bytes); |
| 930 | } |
| 931 | break; |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 932 | case KDP_IMAGEPATH: |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 933 | { |
| 934 | const char *path = packet.GetCStr(&offset); |
| 935 | s.Printf(" (path = \"%s\")", path); |
| 936 | } |
| 937 | break; |
| 938 | default: |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 939 | s.Printf(" (add support for dumping this packet reply!!!"); |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 940 | break; |
| 941 | |
| 942 | } |
| 943 | } |
| 944 | else |
| 945 | { |
| 946 | // Dump request packets |
| 947 | switch (command) |
| 948 | { |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 949 | case KDP_CONNECT: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 950 | { |
| 951 | const uint16_t reply_port = packet.GetU16 (&offset); |
| 952 | const uint16_t exc_port = packet.GetU16 (&offset); |
Greg Clayton | 1bc04ed | 2011-07-29 22:26:36 +0000 | [diff] [blame] | 953 | s.Printf(" (reply_port = %u, exc_port = %u, greeting = \"%s\")", reply_port, exc_port, packet.GetCStr(&offset)); |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 954 | } |
| 955 | break; |
| 956 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 957 | case KDP_DISCONNECT: |
| 958 | case KDP_HOSTREBOOT: |
| 959 | case KDP_HOSTINFO: |
| 960 | case KDP_VERSION: |
| 961 | case KDP_REGIONS: |
| 962 | case KDP_KERNELVERSION: |
| 963 | case KDP_MAXBYTES: |
| 964 | case KDP_IMAGEPATH: |
| 965 | case KDP_SUSPEND: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 966 | // No args, just the header in the request... |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 967 | s.PutCString(" ()"); |
| 968 | break; |
| 969 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 970 | case KDP_RESUMECPUS: |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 971 | { |
| 972 | const uint32_t cpu_mask = packet.GetU32 (&offset); |
| 973 | s.Printf(" (cpu_mask = 0x%8.8x)", cpu_mask); |
| 974 | } |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 975 | break; |
| 976 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 977 | case KDP_READMEM: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 978 | { |
| 979 | const uint32_t addr = packet.GetU32 (&offset); |
| 980 | const uint32_t size = packet.GetU32 (&offset); |
Greg Clayton | 1bc04ed | 2011-07-29 22:26:36 +0000 | [diff] [blame] | 981 | s.Printf(" (addr = 0x%8.8x, size = %u)", addr, size); |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 982 | m_last_read_memory_addr = addr; |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 983 | } |
| 984 | break; |
| 985 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 986 | case KDP_WRITEMEM: |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 987 | { |
| 988 | const uint32_t addr = packet.GetU32 (&offset); |
| 989 | const uint32_t size = packet.GetU32 (&offset); |
Greg Clayton | 1bc04ed | 2011-07-29 22:26:36 +0000 | [diff] [blame] | 990 | s.Printf(" (addr = 0x%8.8x, size = %u, bytes = \n", addr, size); |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 991 | if (size > 0) |
| 992 | DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr); |
| 993 | } |
| 994 | break; |
| 995 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 996 | case KDP_READMEM64: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 997 | { |
| 998 | const uint64_t addr = packet.GetU64 (&offset); |
| 999 | const uint32_t size = packet.GetU32 (&offset); |
Greg Clayton | 1bc04ed | 2011-07-29 22:26:36 +0000 | [diff] [blame] | 1000 | s.Printf(" (addr = 0x%16.16llx, size = %u)", addr, size); |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 1001 | m_last_read_memory_addr = addr; |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 1002 | } |
| 1003 | break; |
| 1004 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 1005 | case KDP_WRITEMEM64: |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 1006 | { |
| 1007 | const uint64_t addr = packet.GetU64 (&offset); |
| 1008 | const uint32_t size = packet.GetU32 (&offset); |
Greg Clayton | 1bc04ed | 2011-07-29 22:26:36 +0000 | [diff] [blame] | 1009 | s.Printf(" (addr = 0x%16.16llx, size = %u, bytes = \n", addr, size); |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 1010 | if (size > 0) |
| 1011 | DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr); |
| 1012 | } |
| 1013 | break; |
| 1014 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 1015 | case KDP_READREGS: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 1016 | { |
| 1017 | const uint32_t cpu = packet.GetU32 (&offset); |
| 1018 | const uint32_t flavor = packet.GetU32 (&offset); |
Greg Clayton | 1bc04ed | 2011-07-29 22:26:36 +0000 | [diff] [blame] | 1019 | s.Printf(" (cpu = %u, flavor = %u)", cpu, flavor); |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 1020 | } |
| 1021 | break; |
| 1022 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 1023 | case KDP_WRITEREGS: |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 1024 | { |
| 1025 | const uint32_t cpu = packet.GetU32 (&offset); |
| 1026 | const uint32_t flavor = packet.GetU32 (&offset); |
| 1027 | const uint32_t nbytes = packet.GetByteSize() - offset; |
Greg Clayton | 1bc04ed | 2011-07-29 22:26:36 +0000 | [diff] [blame] | 1028 | s.Printf(" (cpu = %u, flavor = %u, regs = \n", cpu, flavor); |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 1029 | if (nbytes > 0) |
| 1030 | packet.Dump (&s, // Stream to dump to |
| 1031 | offset, // Offset within "packet" |
| 1032 | eFormatHex, // Format to use |
| 1033 | m_addr_byte_size, // Size of each item in bytes |
| 1034 | nbytes / m_addr_byte_size, // Number of items |
| 1035 | 16 / m_addr_byte_size, // Number per line |
| 1036 | LLDB_INVALID_ADDRESS, // Don't show addresses before each line |
| 1037 | 0, 0); // No bitfields |
| 1038 | } |
| 1039 | break; |
| 1040 | |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 1041 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 1042 | case KDP_BREAKPOINT_SET: |
| 1043 | case KDP_BREAKPOINT_REMOVE: |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 1044 | { |
| 1045 | const uint32_t addr = packet.GetU32 (&offset); |
| 1046 | s.Printf(" (addr = 0x%8.8x)", addr); |
| 1047 | } |
| 1048 | break; |
| 1049 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 1050 | case KDP_BREAKPOINT_SET64: |
| 1051 | case KDP_BREAKPOINT_REMOVE64: |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 1052 | { |
| 1053 | const uint64_t addr = packet.GetU64 (&offset); |
| 1054 | s.Printf(" (addr = 0x%16.16llx)", addr); |
| 1055 | } |
| 1056 | break; |
| 1057 | |
| 1058 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 1059 | case KDP_LOAD: |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 1060 | { |
| 1061 | const char *path = packet.GetCStr(&offset); |
| 1062 | s.Printf(" (path = \"%s\")", path); |
| 1063 | } |
| 1064 | break; |
| 1065 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 1066 | case KDP_EXCEPTION: |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 1067 | { |
| 1068 | const uint32_t count = packet.GetU32 (&offset); |
| 1069 | |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 1070 | for (uint32_t i=0; i<count; ++i) |
| 1071 | { |
| 1072 | const uint32_t cpu = packet.GetU32 (&offset); |
| 1073 | const uint32_t exc = packet.GetU32 (&offset); |
| 1074 | const uint32_t code = packet.GetU32 (&offset); |
| 1075 | const uint32_t subcode = packet.GetU32 (&offset); |
| 1076 | const char *exc_cstr = NULL; |
| 1077 | switch (exc) |
| 1078 | { |
| 1079 | case 1: exc_cstr = "EXC_BAD_ACCESS"; break; |
| 1080 | case 2: exc_cstr = "EXC_BAD_INSTRUCTION"; break; |
| 1081 | case 3: exc_cstr = "EXC_ARITHMETIC"; break; |
| 1082 | case 4: exc_cstr = "EXC_EMULATION"; break; |
| 1083 | case 5: exc_cstr = "EXC_SOFTWARE"; break; |
| 1084 | case 6: exc_cstr = "EXC_BREAKPOINT"; break; |
| 1085 | case 7: exc_cstr = "EXC_SYSCALL"; break; |
| 1086 | case 8: exc_cstr = "EXC_MACH_SYSCALL"; break; |
| 1087 | case 9: exc_cstr = "EXC_RPC_ALERT"; break; |
| 1088 | case 10: exc_cstr = "EXC_CRASH"; break; |
| 1089 | default: |
| 1090 | break; |
| 1091 | } |
| 1092 | |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 1093 | s.Printf ("{ cpu = 0x%8.8x, exc = %s (%u), code = %u (0x%8.8x), subcode = %u (0x%8.8x)} ", |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 1094 | cpu, exc_cstr, exc, code, code, subcode, subcode); |
| 1095 | } |
| 1096 | } |
| 1097 | break; |
| 1098 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 1099 | case KDP_TERMINATION: |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 1100 | { |
| 1101 | const uint32_t term_code = packet.GetU32 (&offset); |
| 1102 | const uint32_t exit_code = packet.GetU32 (&offset); |
| 1103 | s.Printf(" (term_code = 0x%8.8x (%u), exit_code = 0x%8.8x (%u))", term_code, term_code, exit_code, exit_code); |
| 1104 | } |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 1105 | break; |
| 1106 | |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 1107 | case KDP_REATTACH: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 1108 | { |
| 1109 | const uint16_t reply_port = packet.GetU16 (&offset); |
Greg Clayton | 1bc04ed | 2011-07-29 22:26:36 +0000 | [diff] [blame] | 1110 | s.Printf(" (reply_port = %u)", reply_port); |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 1111 | } |
| 1112 | break; |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | else |
| 1117 | { |
| 1118 | error_desc = "error: invalid packet command: "; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | if (error_desc) |
| 1123 | { |
| 1124 | s.PutCString (error_desc); |
| 1125 | |
| 1126 | packet.Dump (&s, // Stream to dump to |
| 1127 | 0, // Offset into "packet" |
| 1128 | eFormatBytes, // Dump as hex bytes |
| 1129 | 1, // Size of each item is 1 for single bytes |
| 1130 | packet.GetByteSize(), // Number of bytes |
| 1131 | UINT32_MAX, // Num bytes per line |
| 1132 | LLDB_INVALID_ADDRESS, // Base address |
| 1133 | 0, 0); // Bitfield info set to not do anything bitfield related |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | uint32_t |
| 1138 | CommunicationKDP::SendRequestReadRegisters (uint32_t cpu, |
| 1139 | uint32_t flavor, |
Greg Clayton | ea63601 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 1140 | void *dst, |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 1141 | uint32_t dst_len, |
| 1142 | Error &error) |
| 1143 | { |
| 1144 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 1145 | const CommandType command = KDP_READREGS; |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 1146 | // Size is header + 4 byte cpu and 4 byte flavor |
| 1147 | const uint32_t command_length = 8 + 4 + 4; |
| 1148 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 1149 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 1150 | request_packet.PutHex32 (cpu); |
| 1151 | request_packet.PutHex32 (flavor); |
| 1152 | DataExtractor reply_packet; |
| 1153 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 1154 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 1155 | uint32_t offset = 8; |
| 1156 | uint32_t kdp_error = reply_packet.GetU32 (&offset); |
| 1157 | uint32_t src_len = reply_packet.GetByteSize() - 12; |
| 1158 | |
| 1159 | if (src_len > 0) |
| 1160 | { |
| 1161 | const uint32_t bytes_to_copy = std::min<uint32_t>(src_len, dst_len); |
| 1162 | const void *src = reply_packet.GetData(&offset, bytes_to_copy); |
| 1163 | if (src) |
| 1164 | { |
| 1165 | ::memcpy (dst, src, bytes_to_copy); |
| 1166 | error.Clear(); |
| 1167 | // Return the number of bytes we could have returned regardless if |
| 1168 | // we copied them or not, just so we know when things don't match up |
Greg Clayton | ea63601 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 1169 | return src_len; |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 1170 | } |
| 1171 | } |
| 1172 | if (kdp_error) |
| 1173 | error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error); |
| 1174 | else |
| 1175 | error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u", cpu, flavor); |
| 1176 | } |
Greg Clayton | 307c7fd | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 1177 | else |
| 1178 | { |
| 1179 | error.SetErrorString ("failed to send packet"); |
| 1180 | } |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 1181 | return 0; |
| 1182 | } |
| 1183 | |
Greg Clayton | ea63601 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 1184 | uint32_t |
| 1185 | CommunicationKDP::SendRequestWriteRegisters (uint32_t cpu, |
| 1186 | uint32_t flavor, |
| 1187 | const void *src, |
| 1188 | uint32_t src_len, |
| 1189 | Error &error) |
| 1190 | { |
| 1191 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
| 1192 | const CommandType command = KDP_WRITEREGS; |
| 1193 | // Size is header + 4 byte cpu and 4 byte flavor |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 1194 | const uint32_t command_length = 8 + 4 + 4 + src_len; |
Greg Clayton | ea63601 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 1195 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 1196 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 1197 | request_packet.PutHex32 (cpu); |
| 1198 | request_packet.PutHex32 (flavor); |
| 1199 | request_packet.Write(src, src_len); |
| 1200 | DataExtractor reply_packet; |
| 1201 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 1202 | { |
| 1203 | uint32_t offset = 8; |
| 1204 | uint32_t kdp_error = reply_packet.GetU32 (&offset); |
| 1205 | if (kdp_error == 0) |
| 1206 | return src_len; |
| 1207 | error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error); |
| 1208 | } |
Greg Clayton | 307c7fd | 2012-10-19 22:22:57 +0000 | [diff] [blame] | 1209 | else |
| 1210 | { |
| 1211 | error.SetErrorString ("failed to send packet"); |
| 1212 | } |
Greg Clayton | ea63601 | 2012-09-21 01:55:30 +0000 | [diff] [blame] | 1213 | return 0; |
| 1214 | } |
| 1215 | |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 1216 | |
| 1217 | bool |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 1218 | CommunicationKDP::SendRequestResume () |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 1219 | { |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 1220 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 1221 | const CommandType command = KDP_RESUMECPUS; |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 1222 | const uint32_t command_length = 12; |
| 1223 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 1224 | MakeRequestPacketHeader (command, request_packet, command_length); |
Greg Clayton | 3acaa92 | 2012-09-25 02:40:06 +0000 | [diff] [blame] | 1225 | request_packet.PutHex32(GetCPUMask()); |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 1226 | |
| 1227 | DataExtractor reply_packet; |
| 1228 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 1229 | return true; |
| 1230 | return false; |
| 1231 | } |
| 1232 | |
| 1233 | bool |
| 1234 | CommunicationKDP::SendRequestBreakpoint (bool set, addr_t addr) |
| 1235 | { |
| 1236 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
| 1237 | bool use_64 = (GetVersion() >= 11); |
| 1238 | uint32_t command_addr_byte_size = use_64 ? 8 : 4; |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 1239 | const CommandType command = set ? (use_64 ? KDP_BREAKPOINT_SET64 : KDP_BREAKPOINT_SET ): |
| 1240 | (use_64 ? KDP_BREAKPOINT_REMOVE64 : KDP_BREAKPOINT_REMOVE); |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 1241 | |
| 1242 | const uint32_t command_length = 8 + command_addr_byte_size; |
| 1243 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 1244 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 1245 | request_packet.PutMaxHex64 (addr, command_addr_byte_size); |
| 1246 | |
| 1247 | DataExtractor reply_packet; |
| 1248 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
Greg Clayton | 1bc04ed | 2011-07-29 22:26:36 +0000 | [diff] [blame] | 1249 | { |
| 1250 | uint32_t offset = 8; |
| 1251 | uint32_t kdp_error = reply_packet.GetU32 (&offset); |
| 1252 | if (kdp_error == 0) |
| 1253 | return true; |
| 1254 | } |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 1255 | return false; |
| 1256 | } |
| 1257 | |
| 1258 | bool |
| 1259 | CommunicationKDP::SendRequestSuspend () |
| 1260 | { |
| 1261 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
Greg Clayton | 7b13922 | 2011-07-21 01:12:01 +0000 | [diff] [blame] | 1262 | const CommandType command = KDP_SUSPEND; |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame] | 1263 | const uint32_t command_length = 8; |
| 1264 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 1265 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 1266 | DataExtractor reply_packet; |
| 1267 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 1268 | return true; |
| 1269 | return false; |
| 1270 | } |
| 1271 | |