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 |
| 14 | #include <limits.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | // C++ Includes |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 18 | #include "llvm/Support/MachO.h" |
| 19 | |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 20 | // Other libraries and framework includes |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 21 | #include "lldb/Core/DataBufferHeap.h" |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 22 | #include "lldb/Core/DataExtractor.h" |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 23 | #include "lldb/Core/Log.h" |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 24 | #include "lldb/Core/State.h" |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 25 | #include "lldb/Host/FileSpec.h" |
| 26 | #include "lldb/Host/Host.h" |
| 27 | #include "lldb/Host/TimeValue.h" |
| 28 | #include "lldb/Target/Process.h" |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 29 | |
| 30 | // Project includes |
| 31 | #include "ProcessKDPLog.h" |
| 32 | |
| 33 | #define DEBUGSERVER_BASENAME "debugserver" |
| 34 | |
| 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), |
| 47 | m_public_is_running (false), |
| 48 | m_private_is_running (false), |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 49 | m_session_key (0u), |
| 50 | m_request_sequence_id (0u), |
| 51 | m_exception_sequence_id (0u), |
| 52 | m_kdp_version_version (0u), |
| 53 | m_kdp_version_feature (0u), |
| 54 | m_kdp_hostinfo_cpu_mask (0u), |
| 55 | m_kdp_hostinfo_cpu_type (0u), |
| 56 | m_kdp_hostinfo_cpu_subtype (0u) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 57 | { |
| 58 | } |
| 59 | |
| 60 | //---------------------------------------------------------------------- |
| 61 | // Destructor |
| 62 | //---------------------------------------------------------------------- |
| 63 | CommunicationKDP::~CommunicationKDP() |
| 64 | { |
| 65 | if (IsConnected()) |
| 66 | { |
| 67 | Disconnect(); |
| 68 | } |
| 69 | } |
| 70 | |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 71 | bool |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 72 | CommunicationKDP::SendRequestPacket (const PacketStreamType &request_packet) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 73 | { |
| 74 | Mutex::Locker locker(m_sequence_mutex); |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 75 | return SendRequestPacketNoLock (request_packet); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 78 | #if 0 |
| 79 | typedef struct { |
| 80 | uint8_t request; // Either: CommandType | ePacketTypeRequest, or CommandType | ePacketTypeReply |
| 81 | uint8_t sequence; |
| 82 | uint16_t length; // Length of entire packet including this header |
| 83 | uint32_t key; // Session key |
| 84 | } kdp_hdr_t; |
| 85 | #endif |
| 86 | |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 87 | void |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 88 | CommunicationKDP::MakeRequestPacketHeader (CommandType request_type, |
| 89 | PacketStreamType &request_packet, |
| 90 | uint16_t request_length) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 91 | { |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 92 | request_packet.Clear(); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 93 | request_packet.PutHex8 (request_type | ePacketTypeRequest); // Set the request type |
| 94 | request_packet.PutHex8 (m_request_sequence_id++); // Sequence number |
| 95 | request_packet.PutHex16 (request_length); // Length of the packet including this header |
| 96 | request_packet.PutHex32 (m_session_key); // Session key |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 99 | bool |
| 100 | CommunicationKDP::SendRequestAndGetReply (const CommandType command, |
| 101 | const uint8_t request_sequence_id, |
| 102 | const PacketStreamType &request_packet, |
| 103 | DataExtractor &reply_packet) |
| 104 | { |
| 105 | |
| 106 | Mutex::Locker locker(m_sequence_mutex); |
| 107 | if (SendRequestPacketNoLock(request_packet)) |
| 108 | { |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 109 | if (WaitForPacketWithTimeoutMicroSecondsNoLock (reply_packet, GetPacketTimeoutInMicroSeconds ())) |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 110 | { |
| 111 | uint32_t offset = 0; |
| 112 | const uint8_t reply_command = reply_packet.GetU8 (&offset); |
| 113 | const uint8_t reply_sequence_id = reply_packet.GetU8 (&offset); |
| 114 | if ((reply_command & eCommandTypeMask) == command) |
| 115 | { |
| 116 | if (request_sequence_id == reply_sequence_id) |
| 117 | return true; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | reply_packet.Clear(); |
| 122 | return false; |
| 123 | } |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 124 | |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 125 | bool |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 126 | CommunicationKDP::SendRequestPacketNoLock (const PacketStreamType &request_packet) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 127 | { |
| 128 | if (IsConnected()) |
| 129 | { |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 130 | const char *packet_data = request_packet.GetData(); |
| 131 | const size_t packet_size = request_packet.GetSize(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 132 | |
| 133 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS)); |
| 134 | if (log) |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 135 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 136 | PacketStreamType log_strm; |
| 137 | DumpPacket (log_strm, packet_data, packet_size); |
| 138 | log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData()); |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 139 | } |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 140 | ConnectionStatus status = eConnectionStatusSuccess; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 141 | |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 142 | size_t bytes_written = Write (packet_data, |
| 143 | packet_size, |
| 144 | status, |
| 145 | NULL); |
| 146 | |
| 147 | if (bytes_written == packet_size) |
| 148 | return true; |
| 149 | |
| 150 | if (log) |
| 151 | log->Printf ("error: failed to send packet entire packet %zu of %zu bytes sent", bytes_written, packet_size); |
| 152 | } |
| 153 | return false; |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | bool |
| 157 | CommunicationKDP::GetSequenceMutex (Mutex::Locker& locker) |
| 158 | { |
| 159 | return locker.TryLock (m_sequence_mutex.GetMutex()); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | bool |
| 164 | CommunicationKDP::WaitForNotRunningPrivate (const TimeValue *timeout_ptr) |
| 165 | { |
| 166 | return m_private_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL); |
| 167 | } |
| 168 | |
| 169 | size_t |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 170 | CommunicationKDP::WaitForPacketWithTimeoutMicroSeconds (DataExtractor &packet, uint32_t timeout_usec) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 171 | { |
| 172 | Mutex::Locker locker(m_sequence_mutex); |
| 173 | return WaitForPacketWithTimeoutMicroSecondsNoLock (packet, timeout_usec); |
| 174 | } |
| 175 | |
| 176 | size_t |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 177 | CommunicationKDP::WaitForPacketWithTimeoutMicroSecondsNoLock (DataExtractor &packet, uint32_t timeout_usec) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 178 | { |
| 179 | uint8_t buffer[8192]; |
| 180 | Error error; |
| 181 | |
| 182 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS | KDP_LOG_VERBOSE)); |
| 183 | |
| 184 | // Check for a packet from our cache first without trying any reading... |
| 185 | if (CheckForPacket (NULL, 0, packet)) |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 186 | return packet.GetByteSize(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 187 | |
| 188 | bool timed_out = false; |
| 189 | while (IsConnected() && !timed_out) |
| 190 | { |
| 191 | lldb::ConnectionStatus status; |
| 192 | size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error); |
| 193 | |
| 194 | if (log) |
| 195 | log->Printf ("%s: Read (buffer, (sizeof(buffer), timeout_usec = 0x%x, status = %s, error = %s) => bytes_read = %zu", |
| 196 | __PRETTY_FUNCTION__, |
| 197 | timeout_usec, |
| 198 | Communication::ConnectionStatusAsCString (status), |
| 199 | error.AsCString(), |
| 200 | bytes_read); |
| 201 | |
| 202 | if (bytes_read > 0) |
| 203 | { |
| 204 | if (CheckForPacket (buffer, bytes_read, packet)) |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 205 | return packet.GetByteSize(); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 206 | } |
| 207 | else |
| 208 | { |
| 209 | switch (status) |
| 210 | { |
| 211 | case eConnectionStatusTimedOut: |
| 212 | timed_out = true; |
| 213 | break; |
| 214 | case eConnectionStatusSuccess: |
| 215 | //printf ("status = success but error = %s\n", error.AsCString("<invalid>")); |
| 216 | break; |
| 217 | |
| 218 | case eConnectionStatusEndOfFile: |
| 219 | case eConnectionStatusNoConnection: |
| 220 | case eConnectionStatusLostConnection: |
| 221 | case eConnectionStatusError: |
| 222 | Disconnect(); |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | packet.Clear (); |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | bool |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 232 | CommunicationKDP::CheckForPacket (const uint8_t *src, size_t src_len, DataExtractor &packet) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 233 | { |
| 234 | // Put the packet data into the buffer in a thread safe fashion |
| 235 | Mutex::Locker locker(m_bytes_mutex); |
| 236 | |
| 237 | LogSP log (ProcessKDPLog::GetLogIfAllCategoriesSet (KDP_LOG_PACKETS)); |
| 238 | |
| 239 | if (src && src_len > 0) |
| 240 | { |
| 241 | if (log && log->GetVerbose()) |
| 242 | { |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 243 | PacketStreamType log_strm; |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 244 | DataExtractor::DumpHexBytes (&log_strm, src, src_len, UINT32_MAX, LLDB_INVALID_ADDRESS); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 245 | log->Printf ("CommunicationKDP::%s adding %u bytes: %s", |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 246 | __FUNCTION__, |
| 247 | (uint32_t)src_len, |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 248 | log_strm.GetData()); |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 249 | } |
| 250 | m_bytes.append ((const char *)src, src_len); |
| 251 | } |
| 252 | |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 253 | // Make sure we at least have enough bytes for a packet header |
| 254 | const size_t bytes_available = m_bytes.size(); |
| 255 | if (bytes_available >= 8) |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 256 | { |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 257 | packet.SetData (&m_bytes[0], bytes_available, m_byte_order); |
| 258 | uint32_t offset = 0; |
| 259 | uint8_t reply_command = packet.GetU8(&offset); |
| 260 | switch (reply_command) |
| 261 | { |
| 262 | case ePacketTypeReply | eCommandTypeConnect: |
| 263 | case ePacketTypeReply | eCommandTypeDisconnect: |
| 264 | case ePacketTypeReply | eCommandTypeHostInfo: |
| 265 | case ePacketTypeReply | eCommandTypeVersion: |
| 266 | case ePacketTypeReply | eCommandTypeMaxBytes: |
| 267 | case ePacketTypeReply | eCommandTypeReadMemory: |
| 268 | case ePacketTypeReply | eCommandTypeWriteMemory: |
| 269 | case ePacketTypeReply | eCommandTypeReadRegisters: |
| 270 | case ePacketTypeReply | eCommandTypeWriteRegisters: |
| 271 | case ePacketTypeReply | eCommandTypeLoad: |
| 272 | case ePacketTypeReply | eCommandTypeImagePath: |
| 273 | case ePacketTypeReply | eCommandTypeSuspend: |
| 274 | case ePacketTypeReply | eCommandTypeResume: |
| 275 | case ePacketTypeReply | eCommandTypeException: |
| 276 | case ePacketTypeReply | eCommandTypeTermination: |
| 277 | case ePacketTypeReply | eCommandTypeBreakpointSet: |
| 278 | case ePacketTypeReply | eCommandTypeBreakpointRemove: |
| 279 | case ePacketTypeReply | eCommandTypeRegions: |
| 280 | case ePacketTypeReply | eCommandTypeReattach: |
| 281 | case ePacketTypeReply | eCommandTypeHostReboot: |
| 282 | case ePacketTypeReply | eCommandTypeReadMemory64: |
| 283 | case ePacketTypeReply | eCommandTypeWriteMemory64: |
| 284 | case ePacketTypeReply | eCommandTypeBreakpointSet64: |
| 285 | case ePacketTypeReply | eCommandTypeBreakpointRemove64: |
| 286 | case ePacketTypeReply | eCommandTypeKernelVersion: |
| 287 | { |
| 288 | offset = 2; |
| 289 | const uint16_t length = packet.GetU16 (&offset); |
| 290 | if (length <= bytes_available) |
| 291 | { |
| 292 | // We have an entire packet ready, we need to copy the data |
| 293 | // bytes into a buffer that will be owned by the packet and |
| 294 | // erase the bytes from our communcation buffer "m_bytes" |
| 295 | packet.SetData (DataBufferSP (new DataBufferHeap (&m_bytes[0], length))); |
| 296 | m_bytes.erase (0, length); |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 297 | |
| 298 | if (log) |
| 299 | { |
| 300 | PacketStreamType log_strm; |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 301 | DumpPacket (log_strm, packet); |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 302 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 303 | log->Printf("%.*s", (uint32_t)log_strm.GetSize(), log_strm.GetData()); |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 304 | } |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 305 | return true; |
| 306 | } |
| 307 | } |
| 308 | break; |
| 309 | |
| 310 | default: |
| 311 | // Unrecognized reply command byte, erase this byte and try to get back on track |
| 312 | if (log) |
| 313 | log->Printf ("CommunicationKDP::%s: tossing junk byte: 0x%2.2x", |
| 314 | __FUNCTION__, |
| 315 | (uint8_t)m_bytes[0]); |
| 316 | m_bytes.erase(0, 1); |
| 317 | break; |
| 318 | } |
Greg Clayton | 363be3f | 2011-07-15 03:27:12 +0000 | [diff] [blame] | 319 | } |
| 320 | packet.Clear(); |
| 321 | return false; |
| 322 | } |
| 323 | |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 324 | |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 325 | bool |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 326 | CommunicationKDP::SendRequestConnect (uint16_t reply_port, |
| 327 | uint16_t exc_port, |
| 328 | const char *greeting) |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 329 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 330 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 331 | if (greeting == NULL) |
| 332 | greeting = ""; |
| 333 | |
| 334 | const CommandType command = eCommandTypeConnect; |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 335 | // Length is 82 uint16_t and the length of the greeting C string with the terminating NULL |
| 336 | const uint32_t command_length = 8 + 2 + 2 + ::strlen(greeting) + 1; |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 337 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 338 | MakeRequestPacketHeader (command, request_packet, command_length); |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 339 | // Always send connect ports as little endian |
| 340 | request_packet.SetByteOrder (eByteOrderLittle); |
| 341 | request_packet.PutHex16 (reply_port); |
| 342 | request_packet.PutHex16 (exc_port); |
| 343 | request_packet.SetByteOrder (m_byte_order); |
| 344 | request_packet.PutCString (greeting); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 345 | DataExtractor reply_packet; |
| 346 | return SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet); |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 349 | void |
| 350 | CommunicationKDP::ClearKDPSettings () |
| 351 | { |
| 352 | m_request_sequence_id = 0; |
| 353 | m_kdp_version_version = 0; |
| 354 | m_kdp_version_feature = 0; |
| 355 | m_kdp_hostinfo_cpu_mask = 0; |
| 356 | m_kdp_hostinfo_cpu_type = 0; |
| 357 | m_kdp_hostinfo_cpu_subtype = 0; |
| 358 | } |
| 359 | |
| 360 | bool |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 361 | CommunicationKDP::SendRequestReattach (uint16_t reply_port) |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 362 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 363 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 364 | const CommandType command = eCommandTypeReattach; |
| 365 | // Length is 8 bytes for the header plus 2 bytes for the reply UDP port |
| 366 | const uint32_t command_length = 8 + 2; |
| 367 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 368 | MakeRequestPacketHeader (command, request_packet, command_length); |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 369 | // Always send connect ports as little endian |
| 370 | request_packet.SetByteOrder (eByteOrderLittle); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 371 | request_packet.PutHex16(reply_port); |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 372 | request_packet.SetByteOrder (m_byte_order); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 373 | DataExtractor reply_packet; |
| 374 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 375 | { |
| 376 | // Reset the sequence ID to zero for reattach |
| 377 | ClearKDPSettings (); |
| 378 | uint32_t offset = 4; |
| 379 | m_session_key = reply_packet.GetU32 (&offset); |
| 380 | return true; |
| 381 | } |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | uint32_t |
| 386 | CommunicationKDP::GetVersion () |
| 387 | { |
| 388 | if (!VersionIsValid()) |
| 389 | SendRequestVersion(); |
| 390 | return m_kdp_version_version; |
| 391 | } |
| 392 | |
| 393 | uint32_t |
| 394 | CommunicationKDP::GetFeatureFlags () |
| 395 | { |
| 396 | if (!VersionIsValid()) |
| 397 | SendRequestVersion(); |
| 398 | return m_kdp_version_feature; |
| 399 | } |
| 400 | |
| 401 | bool |
| 402 | CommunicationKDP::SendRequestVersion () |
| 403 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 404 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 405 | const CommandType command = eCommandTypeVersion; |
| 406 | const uint32_t command_length = 8; |
| 407 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 408 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 409 | DataExtractor reply_packet; |
| 410 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 411 | { |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 412 | uint32_t offset = 8; |
| 413 | m_kdp_version_version = reply_packet.GetU32 (&offset); |
| 414 | m_kdp_version_feature = reply_packet.GetU32 (&offset); |
| 415 | return true; |
| 416 | } |
| 417 | return false; |
| 418 | } |
| 419 | |
| 420 | uint32_t |
| 421 | CommunicationKDP::GetCPUMask () |
| 422 | { |
| 423 | if (!HostInfoIsValid()) |
| 424 | SendRequestHostInfo(); |
| 425 | return m_kdp_hostinfo_cpu_mask; |
| 426 | } |
| 427 | |
| 428 | uint32_t |
| 429 | CommunicationKDP::GetCPUType () |
| 430 | { |
| 431 | if (!HostInfoIsValid()) |
| 432 | SendRequestHostInfo(); |
| 433 | return m_kdp_hostinfo_cpu_type; |
| 434 | } |
| 435 | |
| 436 | uint32_t |
| 437 | CommunicationKDP::GetCPUSubtype () |
| 438 | { |
| 439 | if (!HostInfoIsValid()) |
| 440 | SendRequestHostInfo(); |
| 441 | return m_kdp_hostinfo_cpu_subtype; |
| 442 | } |
| 443 | |
| 444 | bool |
| 445 | CommunicationKDP::SendRequestHostInfo () |
| 446 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 447 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 448 | const CommandType command = eCommandTypeHostInfo; |
| 449 | const uint32_t command_length = 8; |
| 450 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 451 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 452 | DataExtractor reply_packet; |
| 453 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 454 | { |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 455 | uint32_t offset = 8; |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 456 | m_kdp_hostinfo_cpu_mask = reply_packet.GetU32 (&offset); |
| 457 | m_kdp_hostinfo_cpu_type = reply_packet.GetU32 (&offset); |
| 458 | m_kdp_hostinfo_cpu_subtype = reply_packet.GetU32 (&offset); |
| 459 | |
| 460 | ArchSpec kernel_arch; |
| 461 | kernel_arch.SetArchitecture (eArchTypeMachO, |
| 462 | m_kdp_hostinfo_cpu_type, |
| 463 | m_kdp_hostinfo_cpu_subtype); |
| 464 | |
| 465 | m_addr_byte_size = kernel_arch.GetAddressByteSize(); |
| 466 | m_byte_order = kernel_arch.GetByteOrder(); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 467 | return true; |
| 468 | } |
| 469 | return false; |
| 470 | } |
| 471 | |
| 472 | bool |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 473 | CommunicationKDP::SendRequestDisconnect () |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 474 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 475 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 476 | const CommandType command = eCommandTypeDisconnect; |
| 477 | const uint32_t command_length = 8; |
| 478 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 479 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 480 | DataExtractor reply_packet; |
| 481 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 482 | { |
| 483 | // Are we supposed to get a reply for disconnect? |
| 484 | } |
| 485 | ClearKDPSettings (); |
| 486 | return true; |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 489 | uint32_t |
| 490 | CommunicationKDP::SendRequestReadMemory (lldb::addr_t addr, |
| 491 | void *dst, |
| 492 | uint32_t dst_len, |
| 493 | Error &error) |
| 494 | { |
| 495 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
| 496 | bool use_64 = (GetVersion() >= 11); |
| 497 | uint32_t command_addr_byte_size = use_64 ? 8 : 4; |
| 498 | const CommandType command = use_64 ? eCommandTypeReadMemory64 : eCommandTypeReadMemory; |
| 499 | // Size is header + address size + uint32_t length |
| 500 | const uint32_t command_length = 8 + command_addr_byte_size + 4; |
| 501 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 502 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 503 | request_packet.PutMaxHex64 (addr, command_addr_byte_size); |
| 504 | request_packet.PutHex32 (dst_len); |
| 505 | DataExtractor reply_packet; |
| 506 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 507 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 508 | uint32_t offset = 8; |
| 509 | uint32_t kdp_error = reply_packet.GetU32 (&offset); |
| 510 | uint32_t src_len = reply_packet.GetByteSize() - 12; |
| 511 | |
| 512 | if (src_len > 0) |
| 513 | { |
| 514 | const void *src = reply_packet.GetData(&offset, src_len); |
| 515 | if (src) |
| 516 | { |
| 517 | ::memcpy (dst, src, src_len); |
| 518 | error.Clear(); |
| 519 | return src_len; |
| 520 | } |
| 521 | } |
| 522 | if (kdp_error) |
| 523 | error.SetErrorStringWithFormat ("kdp read memory failed (error %u)", kdp_error); |
| 524 | else |
| 525 | error.SetErrorString ("kdp read memory failed"); |
| 526 | } |
| 527 | return 0; |
| 528 | } |
| 529 | |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 530 | |
| 531 | uint32_t |
| 532 | CommunicationKDP::SendRequestWriteMemory (lldb::addr_t addr, |
| 533 | const void *src, |
| 534 | uint32_t src_len, |
| 535 | Error &error) |
| 536 | { |
| 537 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
| 538 | bool use_64 = (GetVersion() >= 11); |
| 539 | uint32_t command_addr_byte_size = use_64 ? 8 : 4; |
| 540 | const CommandType command = use_64 ? eCommandTypeWriteMemory64 : eCommandTypeWriteMemory; |
| 541 | // Size is header + address size + uint32_t length |
| 542 | const uint32_t command_length = 8 + command_addr_byte_size + 4; |
| 543 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 544 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 545 | request_packet.PutMaxHex64 (addr, command_addr_byte_size); |
| 546 | request_packet.PutHex32 (src_len); |
| 547 | request_packet.PutBytesAsRawHex8(src, src_len); |
| 548 | |
| 549 | DataExtractor reply_packet; |
| 550 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 551 | { |
| 552 | uint32_t offset = 8; |
| 553 | uint32_t kdp_error = reply_packet.GetU32 (&offset); |
| 554 | if (kdp_error) |
| 555 | error.SetErrorStringWithFormat ("kdp write memory failed (error %u)", kdp_error); |
| 556 | else |
| 557 | { |
| 558 | error.Clear(); |
| 559 | return src_len; |
| 560 | } |
| 561 | } |
| 562 | return 0; |
| 563 | } |
| 564 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 565 | const char * |
| 566 | CommunicationKDP::GetCommandAsCString (uint8_t command) |
| 567 | { |
| 568 | switch (command) |
| 569 | { |
| 570 | case eCommandTypeConnect: return "KDP_CONNECT"; |
| 571 | case eCommandTypeDisconnect: return "KDP_DISCONNECT"; |
| 572 | case eCommandTypeHostInfo: return "KDP_HOSTINFO"; |
| 573 | case eCommandTypeVersion: return "KDP_VERSION"; |
| 574 | case eCommandTypeMaxBytes: return "KDP_MAXBYTES"; |
| 575 | case eCommandTypeReadMemory: return "KDP_READMEM"; |
| 576 | case eCommandTypeWriteMemory: return "KDP_WRITEMEM"; |
| 577 | case eCommandTypeReadRegisters: return "KDP_READREGS"; |
| 578 | case eCommandTypeWriteRegisters: return "KDP_WRITEREGS"; |
| 579 | case eCommandTypeLoad: return "KDP_LOAD"; |
| 580 | case eCommandTypeImagePath: return "KDP_IMAGEPATH"; |
| 581 | case eCommandTypeSuspend: return "KDP_SUSPEND"; |
| 582 | case eCommandTypeResume: return "KDP_RESUMECPUS"; |
| 583 | case eCommandTypeException: return "KDP_EXCEPTION"; |
| 584 | case eCommandTypeTermination: return "KDP_TERMINATION"; |
| 585 | case eCommandTypeBreakpointSet: return "KDP_BREAKPOINT_SET"; |
| 586 | case eCommandTypeBreakpointRemove: return "KDP_BREAKPOINT_REMOVE"; |
| 587 | case eCommandTypeRegions: return "KDP_REGIONS"; |
| 588 | case eCommandTypeReattach: return "KDP_REATTACH"; |
| 589 | case eCommandTypeHostReboot: return "KDP_HOSTREBOOT"; |
| 590 | case eCommandTypeReadMemory64: return "KDP_READMEM64"; |
| 591 | case eCommandTypeWriteMemory64: return "KDP_WRITEMEM64"; |
| 592 | case eCommandTypeBreakpointSet64: return "KDP_BREAKPOINT64_SET"; |
| 593 | case eCommandTypeBreakpointRemove64: return "KDP_BREAKPOINT64_REMOVE"; |
| 594 | case eCommandTypeKernelVersion: return "KDP_KERNELVERSION"; |
| 595 | } |
| 596 | return NULL; |
| 597 | } |
| 598 | |
| 599 | void |
| 600 | CommunicationKDP::DumpPacket (Stream &s, const void *data, uint32_t data_len) |
| 601 | { |
| 602 | DataExtractor extractor (data, data_len, m_byte_order, m_addr_byte_size); |
| 603 | DumpPacket (s, extractor); |
| 604 | } |
| 605 | |
| 606 | void |
| 607 | CommunicationKDP::DumpPacket (Stream &s, const DataExtractor& packet) |
| 608 | { |
| 609 | const char *error_desc = NULL; |
| 610 | if (packet.GetByteSize() < 8) |
| 611 | { |
| 612 | error_desc = "error: invalid packet (too short): "; |
| 613 | } |
| 614 | else |
| 615 | { |
| 616 | uint32_t offset = 0; |
| 617 | const uint8_t first_packet_byte = packet.GetU8 (&offset); |
| 618 | const uint8_t sequence_id = packet.GetU8 (&offset); |
| 619 | const uint16_t length = packet.GetU16 (&offset); |
| 620 | const uint32_t key = packet.GetU32 (&offset); |
| 621 | const CommandType command = ExtractCommand (first_packet_byte); |
| 622 | const char *command_name = GetCommandAsCString (command); |
| 623 | if (command_name) |
| 624 | { |
| 625 | const bool is_reply = ExtractIsReply(first_packet_byte); |
| 626 | s.Printf ("%s {%u:%u} <0x%4.4x> %s", |
| 627 | is_reply ? "<--" : "-->", |
| 628 | key, |
| 629 | sequence_id, |
| 630 | length, |
| 631 | command_name); |
| 632 | |
| 633 | if (is_reply) |
| 634 | { |
| 635 | // Dump request reply packets |
| 636 | switch (command) |
| 637 | { |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 638 | // Commands that return a single 32 bit error |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 639 | case eCommandTypeConnect: |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 640 | case eCommandTypeWriteMemory: |
| 641 | case eCommandTypeWriteMemory64: |
| 642 | case eCommandTypeBreakpointSet: |
| 643 | case eCommandTypeBreakpointRemove: |
| 644 | case eCommandTypeBreakpointSet64: |
| 645 | case eCommandTypeBreakpointRemove64: |
| 646 | case eCommandTypeWriteRegisters: |
| 647 | case eCommandTypeLoad: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 648 | { |
| 649 | const uint32_t error = packet.GetU32 (&offset); |
| 650 | s.Printf(" (error=0x%8.8x)", error); |
| 651 | } |
| 652 | break; |
| 653 | |
| 654 | case eCommandTypeDisconnect: |
| 655 | case eCommandTypeReattach: |
| 656 | case eCommandTypeHostReboot: |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 657 | case eCommandTypeSuspend: |
| 658 | case eCommandTypeResume: |
| 659 | case eCommandTypeException: |
| 660 | case eCommandTypeTermination: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 661 | // No return value for the reply, just the header to ack |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 662 | s.PutCString(" ()"); |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 663 | break; |
| 664 | |
| 665 | case eCommandTypeHostInfo: |
| 666 | { |
| 667 | const uint32_t cpu_mask = packet.GetU32 (&offset); |
| 668 | const uint32_t cpu_type = packet.GetU32 (&offset); |
| 669 | const uint32_t cpu_subtype = packet.GetU32 (&offset); |
| 670 | s.Printf(" (cpu_mask=0x%8.8x, cpu_type=0x%8.8x, cpu_subtype=0x%8.8x)", cpu_mask, cpu_type, cpu_subtype); |
| 671 | } |
| 672 | break; |
| 673 | |
| 674 | case eCommandTypeVersion: |
| 675 | { |
| 676 | const uint32_t version = packet.GetU32 (&offset); |
| 677 | const uint32_t feature = packet.GetU32 (&offset); |
| 678 | s.Printf(" (version=0x%8.8x, feature=0x%8.8x)", version, feature); |
| 679 | } |
| 680 | break; |
| 681 | |
| 682 | case eCommandTypeRegions: |
| 683 | { |
| 684 | const uint32_t region_count = packet.GetU32 (&offset); |
| 685 | s.Printf(" (count = %u", region_count); |
| 686 | for (uint32_t i=0; i<region_count; ++i) |
| 687 | { |
| 688 | const addr_t region_addr = packet.GetPointer (&offset); |
| 689 | const uint32_t region_size = packet.GetU32 (&offset); |
| 690 | const uint32_t region_prot = packet.GetU32 (&offset); |
| 691 | s.Printf("\n\tregion[%i] = { range = [0x%16.16llx - 0x%16.16llx), size = 0x%8.8x, prot = %s }", region_addr, region_addr + region_size, region_size, GetPermissionsAsCString (region_prot)); |
| 692 | } |
| 693 | } |
| 694 | break; |
| 695 | |
| 696 | case eCommandTypeReadMemory: |
| 697 | case eCommandTypeReadMemory64: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 698 | { |
| 699 | const uint32_t error = packet.GetU32 (&offset); |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 700 | const uint32_t count = packet.GetByteSize() - offset; |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 701 | s.Printf(" (error = 0x%8.8x <0x%x>:\n", error, count); |
| 702 | if (count > 0) |
| 703 | DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, count), count, 32, LLDB_INVALID_ADDRESS); |
| 704 | } |
| 705 | break; |
| 706 | |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 707 | case eCommandTypeReadRegisters: |
| 708 | { |
| 709 | const uint32_t error = packet.GetU32 (&offset); |
| 710 | const uint32_t count = packet.GetByteSize() - offset; |
| 711 | s.Printf(" (error = 0x%8.8x <0x%x> regs:\n", error, count); |
| 712 | if (count > 0) |
| 713 | packet.Dump (&s, // Stream to dump to |
| 714 | offset, // Offset within "packet" |
| 715 | eFormatHex, // Format to use |
| 716 | m_addr_byte_size, // Size of each item in bytes |
| 717 | count / m_addr_byte_size, // Number of items |
| 718 | 16 / m_addr_byte_size, // Number per line |
| 719 | LLDB_INVALID_ADDRESS, // Don't show addresses before each line |
| 720 | 0, 0); // No bitfields |
| 721 | } |
| 722 | break; |
| 723 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 724 | case eCommandTypeMaxBytes: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 725 | case eCommandTypeImagePath: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 726 | case eCommandTypeKernelVersion: |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 727 | s.Printf(" (add support for dumping this packet reply!!!"); |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 728 | break; |
| 729 | |
| 730 | } |
| 731 | } |
| 732 | else |
| 733 | { |
| 734 | // Dump request packets |
| 735 | switch (command) |
| 736 | { |
| 737 | case eCommandTypeConnect: |
| 738 | { |
| 739 | const uint16_t reply_port = packet.GetU16 (&offset); |
| 740 | const uint16_t exc_port = packet.GetU16 (&offset); |
| 741 | s.Printf(" (reply_port=%u, exc_port=%u, greeting=\"%s\")", reply_port, exc_port, packet.GetCStr(&offset)); |
| 742 | } |
| 743 | break; |
| 744 | |
| 745 | case eCommandTypeDisconnect: |
| 746 | case eCommandTypeHostReboot: |
| 747 | case eCommandTypeHostInfo: |
| 748 | case eCommandTypeVersion: |
| 749 | case eCommandTypeRegions: |
| 750 | // No args, just the header in the request... |
| 751 | break; |
| 752 | |
| 753 | case eCommandTypeReadMemory: |
| 754 | { |
| 755 | const uint32_t addr = packet.GetU32 (&offset); |
| 756 | const uint32_t size = packet.GetU32 (&offset); |
| 757 | s.Printf(" (addr = 0x%8.8x, size=%u)", addr, size); |
| 758 | } |
| 759 | break; |
| 760 | |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 761 | case eCommandTypeWriteMemory: |
| 762 | { |
| 763 | const uint32_t addr = packet.GetU32 (&offset); |
| 764 | const uint32_t size = packet.GetU32 (&offset); |
| 765 | s.Printf(" (addr = 0x%8.8x, size=%u, bytes:\n", addr, size); |
| 766 | if (size > 0) |
| 767 | DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr); |
| 768 | } |
| 769 | break; |
| 770 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 771 | case eCommandTypeReadMemory64: |
| 772 | { |
| 773 | const uint64_t addr = packet.GetU64 (&offset); |
| 774 | const uint32_t size = packet.GetU32 (&offset); |
| 775 | s.Printf(" (addr = 0x%16.16llx, size=%u)", addr, size); |
| 776 | } |
| 777 | break; |
| 778 | |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 779 | case eCommandTypeWriteMemory64: |
| 780 | { |
| 781 | const uint64_t addr = packet.GetU64 (&offset); |
| 782 | const uint32_t size = packet.GetU32 (&offset); |
| 783 | s.Printf(" (addr = 0x%16.16llx, size=%u, bytes:", addr, size); |
| 784 | if (size > 0) |
| 785 | DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr); |
| 786 | } |
| 787 | break; |
| 788 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 789 | case eCommandTypeReadRegisters: |
| 790 | { |
| 791 | const uint32_t cpu = packet.GetU32 (&offset); |
| 792 | const uint32_t flavor = packet.GetU32 (&offset); |
| 793 | s.Printf(" (cpu = %u, flavor=%u)", cpu, flavor); |
| 794 | } |
| 795 | break; |
| 796 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 797 | case eCommandTypeWriteRegisters: |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 798 | { |
| 799 | const uint32_t cpu = packet.GetU32 (&offset); |
| 800 | const uint32_t flavor = packet.GetU32 (&offset); |
| 801 | const uint32_t nbytes = packet.GetByteSize() - offset; |
| 802 | s.Printf(" (cpu = %u, flavor=%u, regs:\n", cpu, flavor); |
| 803 | if (nbytes > 0) |
| 804 | packet.Dump (&s, // Stream to dump to |
| 805 | offset, // Offset within "packet" |
| 806 | eFormatHex, // Format to use |
| 807 | m_addr_byte_size, // Size of each item in bytes |
| 808 | nbytes / m_addr_byte_size, // Number of items |
| 809 | 16 / m_addr_byte_size, // Number per line |
| 810 | LLDB_INVALID_ADDRESS, // Don't show addresses before each line |
| 811 | 0, 0); // No bitfields |
| 812 | } |
| 813 | break; |
| 814 | |
| 815 | case eCommandTypeMaxBytes: |
| 816 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 817 | case eCommandTypeLoad: |
| 818 | case eCommandTypeImagePath: |
| 819 | case eCommandTypeSuspend: |
| 820 | case eCommandTypeResume: |
| 821 | case eCommandTypeException: |
| 822 | case eCommandTypeTermination: |
| 823 | case eCommandTypeBreakpointSet: |
| 824 | case eCommandTypeBreakpointRemove: |
| 825 | break; |
| 826 | |
| 827 | case eCommandTypeReattach: |
| 828 | { |
| 829 | const uint16_t reply_port = packet.GetU16 (&offset); |
| 830 | s.Printf(" (reply_port=%u)", reply_port); |
| 831 | } |
| 832 | break; |
| 833 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 834 | case eCommandTypeBreakpointSet64: |
| 835 | case eCommandTypeBreakpointRemove64: |
| 836 | case eCommandTypeKernelVersion: |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 837 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 838 | break; |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | else |
| 843 | { |
| 844 | error_desc = "error: invalid packet command: "; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | if (error_desc) |
| 849 | { |
| 850 | s.PutCString (error_desc); |
| 851 | |
| 852 | packet.Dump (&s, // Stream to dump to |
| 853 | 0, // Offset into "packet" |
| 854 | eFormatBytes, // Dump as hex bytes |
| 855 | 1, // Size of each item is 1 for single bytes |
| 856 | packet.GetByteSize(), // Number of bytes |
| 857 | UINT32_MAX, // Num bytes per line |
| 858 | LLDB_INVALID_ADDRESS, // Base address |
| 859 | 0, 0); // Bitfield info set to not do anything bitfield related |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | uint32_t |
| 864 | CommunicationKDP::SendRequestReadRegisters (uint32_t cpu, |
| 865 | uint32_t flavor, |
| 866 | void *dst, |
| 867 | uint32_t dst_len, |
| 868 | Error &error) |
| 869 | { |
| 870 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
| 871 | const CommandType command = eCommandTypeReadRegisters; |
| 872 | // Size is header + 4 byte cpu and 4 byte flavor |
| 873 | const uint32_t command_length = 8 + 4 + 4; |
| 874 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 875 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 876 | request_packet.PutHex32 (cpu); |
| 877 | request_packet.PutHex32 (flavor); |
| 878 | DataExtractor reply_packet; |
| 879 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 880 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 881 | uint32_t offset = 8; |
| 882 | uint32_t kdp_error = reply_packet.GetU32 (&offset); |
| 883 | uint32_t src_len = reply_packet.GetByteSize() - 12; |
| 884 | |
| 885 | if (src_len > 0) |
| 886 | { |
| 887 | const uint32_t bytes_to_copy = std::min<uint32_t>(src_len, dst_len); |
| 888 | const void *src = reply_packet.GetData(&offset, bytes_to_copy); |
| 889 | if (src) |
| 890 | { |
| 891 | ::memcpy (dst, src, bytes_to_copy); |
| 892 | error.Clear(); |
| 893 | // Return the number of bytes we could have returned regardless if |
| 894 | // we copied them or not, just so we know when things don't match up |
| 895 | return src_len; |
| 896 | } |
| 897 | } |
| 898 | if (kdp_error) |
| 899 | error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error); |
| 900 | else |
| 901 | error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u", cpu, flavor); |
| 902 | } |
| 903 | return 0; |
| 904 | } |
| 905 | |