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 | |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame^] | 472 | const char * |
| 473 | CommunicationKDP::GetKernelVersion () |
| 474 | { |
| 475 | if (m_kernel_version.empty()) |
| 476 | SendRequestKernelVersion (); |
| 477 | return m_kernel_version.c_str(); |
| 478 | } |
| 479 | |
| 480 | bool |
| 481 | CommunicationKDP::SendRequestKernelVersion () |
| 482 | { |
| 483 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
| 484 | const CommandType command = eCommandTypeKernelVersion; |
| 485 | const uint32_t command_length = 8; |
| 486 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 487 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 488 | DataExtractor reply_packet; |
| 489 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 490 | { |
| 491 | const char *kernel_version_cstr = reply_packet.PeekCStr(8); |
| 492 | if (kernel_version_cstr && kernel_version_cstr[0]) |
| 493 | m_kernel_version.assign (kernel_version_cstr); |
| 494 | return true; |
| 495 | } |
| 496 | return false; |
| 497 | } |
| 498 | |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 499 | bool |
Greg Clayton | 8d2ea28 | 2011-07-17 20:36:25 +0000 | [diff] [blame] | 500 | CommunicationKDP::SendRequestDisconnect () |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 501 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 502 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
Greg Clayton | d52d00f | 2011-07-16 03:19:08 +0000 | [diff] [blame] | 503 | const CommandType command = eCommandTypeDisconnect; |
| 504 | const uint32_t command_length = 8; |
| 505 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 506 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 507 | DataExtractor reply_packet; |
| 508 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 509 | { |
| 510 | // Are we supposed to get a reply for disconnect? |
| 511 | } |
| 512 | ClearKDPSettings (); |
| 513 | return true; |
Greg Clayton | 1e5b021 | 2011-07-15 16:31:38 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 516 | uint32_t |
| 517 | CommunicationKDP::SendRequestReadMemory (lldb::addr_t addr, |
| 518 | void *dst, |
| 519 | uint32_t dst_len, |
| 520 | Error &error) |
| 521 | { |
| 522 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
| 523 | bool use_64 = (GetVersion() >= 11); |
| 524 | uint32_t command_addr_byte_size = use_64 ? 8 : 4; |
| 525 | const CommandType command = use_64 ? eCommandTypeReadMemory64 : eCommandTypeReadMemory; |
| 526 | // Size is header + address size + uint32_t length |
| 527 | const uint32_t command_length = 8 + command_addr_byte_size + 4; |
| 528 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 529 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 530 | request_packet.PutMaxHex64 (addr, command_addr_byte_size); |
| 531 | request_packet.PutHex32 (dst_len); |
| 532 | DataExtractor reply_packet; |
| 533 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 534 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 535 | uint32_t offset = 8; |
| 536 | uint32_t kdp_error = reply_packet.GetU32 (&offset); |
| 537 | uint32_t src_len = reply_packet.GetByteSize() - 12; |
| 538 | |
| 539 | if (src_len > 0) |
| 540 | { |
| 541 | const void *src = reply_packet.GetData(&offset, src_len); |
| 542 | if (src) |
| 543 | { |
| 544 | ::memcpy (dst, src, src_len); |
| 545 | error.Clear(); |
| 546 | return src_len; |
| 547 | } |
| 548 | } |
| 549 | if (kdp_error) |
| 550 | error.SetErrorStringWithFormat ("kdp read memory failed (error %u)", kdp_error); |
| 551 | else |
| 552 | error.SetErrorString ("kdp read memory failed"); |
| 553 | } |
| 554 | return 0; |
| 555 | } |
| 556 | |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 557 | |
| 558 | uint32_t |
| 559 | CommunicationKDP::SendRequestWriteMemory (lldb::addr_t addr, |
| 560 | const void *src, |
| 561 | uint32_t src_len, |
| 562 | Error &error) |
| 563 | { |
| 564 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
| 565 | bool use_64 = (GetVersion() >= 11); |
| 566 | uint32_t command_addr_byte_size = use_64 ? 8 : 4; |
| 567 | const CommandType command = use_64 ? eCommandTypeWriteMemory64 : eCommandTypeWriteMemory; |
| 568 | // Size is header + address size + uint32_t length |
| 569 | const uint32_t command_length = 8 + command_addr_byte_size + 4; |
| 570 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 571 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 572 | request_packet.PutMaxHex64 (addr, command_addr_byte_size); |
| 573 | request_packet.PutHex32 (src_len); |
| 574 | request_packet.PutBytesAsRawHex8(src, src_len); |
| 575 | |
| 576 | DataExtractor reply_packet; |
| 577 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 578 | { |
| 579 | uint32_t offset = 8; |
| 580 | uint32_t kdp_error = reply_packet.GetU32 (&offset); |
| 581 | if (kdp_error) |
| 582 | error.SetErrorStringWithFormat ("kdp write memory failed (error %u)", kdp_error); |
| 583 | else |
| 584 | { |
| 585 | error.Clear(); |
| 586 | return src_len; |
| 587 | } |
| 588 | } |
| 589 | return 0; |
| 590 | } |
| 591 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 592 | const char * |
| 593 | CommunicationKDP::GetCommandAsCString (uint8_t command) |
| 594 | { |
| 595 | switch (command) |
| 596 | { |
| 597 | case eCommandTypeConnect: return "KDP_CONNECT"; |
| 598 | case eCommandTypeDisconnect: return "KDP_DISCONNECT"; |
| 599 | case eCommandTypeHostInfo: return "KDP_HOSTINFO"; |
| 600 | case eCommandTypeVersion: return "KDP_VERSION"; |
| 601 | case eCommandTypeMaxBytes: return "KDP_MAXBYTES"; |
| 602 | case eCommandTypeReadMemory: return "KDP_READMEM"; |
| 603 | case eCommandTypeWriteMemory: return "KDP_WRITEMEM"; |
| 604 | case eCommandTypeReadRegisters: return "KDP_READREGS"; |
| 605 | case eCommandTypeWriteRegisters: return "KDP_WRITEREGS"; |
| 606 | case eCommandTypeLoad: return "KDP_LOAD"; |
| 607 | case eCommandTypeImagePath: return "KDP_IMAGEPATH"; |
| 608 | case eCommandTypeSuspend: return "KDP_SUSPEND"; |
| 609 | case eCommandTypeResume: return "KDP_RESUMECPUS"; |
| 610 | case eCommandTypeException: return "KDP_EXCEPTION"; |
| 611 | case eCommandTypeTermination: return "KDP_TERMINATION"; |
| 612 | case eCommandTypeBreakpointSet: return "KDP_BREAKPOINT_SET"; |
| 613 | case eCommandTypeBreakpointRemove: return "KDP_BREAKPOINT_REMOVE"; |
| 614 | case eCommandTypeRegions: return "KDP_REGIONS"; |
| 615 | case eCommandTypeReattach: return "KDP_REATTACH"; |
| 616 | case eCommandTypeHostReboot: return "KDP_HOSTREBOOT"; |
| 617 | case eCommandTypeReadMemory64: return "KDP_READMEM64"; |
| 618 | case eCommandTypeWriteMemory64: return "KDP_WRITEMEM64"; |
| 619 | case eCommandTypeBreakpointSet64: return "KDP_BREAKPOINT64_SET"; |
| 620 | case eCommandTypeBreakpointRemove64: return "KDP_BREAKPOINT64_REMOVE"; |
| 621 | case eCommandTypeKernelVersion: return "KDP_KERNELVERSION"; |
| 622 | } |
| 623 | return NULL; |
| 624 | } |
| 625 | |
| 626 | void |
| 627 | CommunicationKDP::DumpPacket (Stream &s, const void *data, uint32_t data_len) |
| 628 | { |
| 629 | DataExtractor extractor (data, data_len, m_byte_order, m_addr_byte_size); |
| 630 | DumpPacket (s, extractor); |
| 631 | } |
| 632 | |
| 633 | void |
| 634 | CommunicationKDP::DumpPacket (Stream &s, const DataExtractor& packet) |
| 635 | { |
| 636 | const char *error_desc = NULL; |
| 637 | if (packet.GetByteSize() < 8) |
| 638 | { |
| 639 | error_desc = "error: invalid packet (too short): "; |
| 640 | } |
| 641 | else |
| 642 | { |
| 643 | uint32_t offset = 0; |
| 644 | const uint8_t first_packet_byte = packet.GetU8 (&offset); |
| 645 | const uint8_t sequence_id = packet.GetU8 (&offset); |
| 646 | const uint16_t length = packet.GetU16 (&offset); |
| 647 | const uint32_t key = packet.GetU32 (&offset); |
| 648 | const CommandType command = ExtractCommand (first_packet_byte); |
| 649 | const char *command_name = GetCommandAsCString (command); |
| 650 | if (command_name) |
| 651 | { |
| 652 | const bool is_reply = ExtractIsReply(first_packet_byte); |
| 653 | s.Printf ("%s {%u:%u} <0x%4.4x> %s", |
| 654 | is_reply ? "<--" : "-->", |
| 655 | key, |
| 656 | sequence_id, |
| 657 | length, |
| 658 | command_name); |
| 659 | |
| 660 | if (is_reply) |
| 661 | { |
| 662 | // Dump request reply packets |
| 663 | switch (command) |
| 664 | { |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 665 | // Commands that return a single 32 bit error |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 666 | case eCommandTypeConnect: |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 667 | case eCommandTypeWriteMemory: |
| 668 | case eCommandTypeWriteMemory64: |
| 669 | case eCommandTypeBreakpointSet: |
| 670 | case eCommandTypeBreakpointRemove: |
| 671 | case eCommandTypeBreakpointSet64: |
| 672 | case eCommandTypeBreakpointRemove64: |
| 673 | case eCommandTypeWriteRegisters: |
| 674 | case eCommandTypeLoad: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 675 | { |
| 676 | const uint32_t error = packet.GetU32 (&offset); |
| 677 | s.Printf(" (error=0x%8.8x)", error); |
| 678 | } |
| 679 | break; |
| 680 | |
| 681 | case eCommandTypeDisconnect: |
| 682 | case eCommandTypeReattach: |
| 683 | case eCommandTypeHostReboot: |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 684 | case eCommandTypeSuspend: |
| 685 | case eCommandTypeResume: |
| 686 | case eCommandTypeException: |
| 687 | case eCommandTypeTermination: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 688 | // No return value for the reply, just the header to ack |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 689 | s.PutCString(" ()"); |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 690 | break; |
| 691 | |
| 692 | case eCommandTypeHostInfo: |
| 693 | { |
| 694 | const uint32_t cpu_mask = packet.GetU32 (&offset); |
| 695 | const uint32_t cpu_type = packet.GetU32 (&offset); |
| 696 | const uint32_t cpu_subtype = packet.GetU32 (&offset); |
| 697 | s.Printf(" (cpu_mask=0x%8.8x, cpu_type=0x%8.8x, cpu_subtype=0x%8.8x)", cpu_mask, cpu_type, cpu_subtype); |
| 698 | } |
| 699 | break; |
| 700 | |
| 701 | case eCommandTypeVersion: |
| 702 | { |
| 703 | const uint32_t version = packet.GetU32 (&offset); |
| 704 | const uint32_t feature = packet.GetU32 (&offset); |
| 705 | s.Printf(" (version=0x%8.8x, feature=0x%8.8x)", version, feature); |
| 706 | } |
| 707 | break; |
| 708 | |
| 709 | case eCommandTypeRegions: |
| 710 | { |
| 711 | const uint32_t region_count = packet.GetU32 (&offset); |
| 712 | s.Printf(" (count = %u", region_count); |
| 713 | for (uint32_t i=0; i<region_count; ++i) |
| 714 | { |
| 715 | const addr_t region_addr = packet.GetPointer (&offset); |
| 716 | const uint32_t region_size = packet.GetU32 (&offset); |
| 717 | const uint32_t region_prot = packet.GetU32 (&offset); |
| 718 | 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)); |
| 719 | } |
| 720 | } |
| 721 | break; |
| 722 | |
| 723 | case eCommandTypeReadMemory: |
| 724 | case eCommandTypeReadMemory64: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 725 | { |
| 726 | const uint32_t error = packet.GetU32 (&offset); |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 727 | const uint32_t count = packet.GetByteSize() - offset; |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 728 | s.Printf(" (error = 0x%8.8x <0x%x>:\n", error, count); |
| 729 | if (count > 0) |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame^] | 730 | packet.Dump (&s, // Stream to dump to |
| 731 | offset, // Offset within "packet" |
| 732 | eFormatBytesWithASCII, // Format to use |
| 733 | 1, // Size of each item in bytes |
| 734 | count, // Number of items |
| 735 | 16, // Number per line |
| 736 | m_last_read_memory_addr, // Don't show addresses before each line |
| 737 | 0, 0); // No bitfields |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 738 | } |
| 739 | break; |
| 740 | |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 741 | case eCommandTypeReadRegisters: |
| 742 | { |
| 743 | const uint32_t error = packet.GetU32 (&offset); |
| 744 | const uint32_t count = packet.GetByteSize() - offset; |
| 745 | s.Printf(" (error = 0x%8.8x <0x%x> regs:\n", error, count); |
| 746 | if (count > 0) |
| 747 | packet.Dump (&s, // Stream to dump to |
| 748 | offset, // Offset within "packet" |
| 749 | eFormatHex, // Format to use |
| 750 | m_addr_byte_size, // Size of each item in bytes |
| 751 | count / m_addr_byte_size, // Number of items |
| 752 | 16 / m_addr_byte_size, // Number per line |
| 753 | LLDB_INVALID_ADDRESS, // Don't show addresses before each line |
| 754 | 0, 0); // No bitfields |
| 755 | } |
| 756 | break; |
| 757 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 758 | case eCommandTypeKernelVersion: |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame^] | 759 | { |
| 760 | const char *kernel_version = packet.PeekCStr(8); |
| 761 | s.Printf(" (version = \"%s\")", kernel_version); |
| 762 | } |
| 763 | break; |
| 764 | |
| 765 | case eCommandTypeMaxBytes: |
| 766 | { |
| 767 | const uint32_t max_bytes = packet.GetU32 (&offset); |
| 768 | s.Printf(" (max_bytes = 0x%8.8x (%u))", max_bytes, max_bytes); |
| 769 | } |
| 770 | break; |
| 771 | case eCommandTypeImagePath: |
| 772 | { |
| 773 | const char *path = packet.GetCStr(&offset); |
| 774 | s.Printf(" (path = \"%s\")", path); |
| 775 | } |
| 776 | break; |
| 777 | default: |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 778 | s.Printf(" (add support for dumping this packet reply!!!"); |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 779 | break; |
| 780 | |
| 781 | } |
| 782 | } |
| 783 | else |
| 784 | { |
| 785 | // Dump request packets |
| 786 | switch (command) |
| 787 | { |
| 788 | case eCommandTypeConnect: |
| 789 | { |
| 790 | const uint16_t reply_port = packet.GetU16 (&offset); |
| 791 | const uint16_t exc_port = packet.GetU16 (&offset); |
| 792 | s.Printf(" (reply_port=%u, exc_port=%u, greeting=\"%s\")", reply_port, exc_port, packet.GetCStr(&offset)); |
| 793 | } |
| 794 | break; |
| 795 | |
| 796 | case eCommandTypeDisconnect: |
| 797 | case eCommandTypeHostReboot: |
| 798 | case eCommandTypeHostInfo: |
| 799 | case eCommandTypeVersion: |
| 800 | case eCommandTypeRegions: |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame^] | 801 | case eCommandTypeKernelVersion: |
| 802 | case eCommandTypeMaxBytes: |
| 803 | case eCommandTypeImagePath: |
| 804 | case eCommandTypeSuspend: |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 805 | // No args, just the header in the request... |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame^] | 806 | s.PutCString(" ()"); |
| 807 | break; |
| 808 | |
| 809 | case eCommandTypeResume: |
| 810 | { |
| 811 | const uint32_t cpu_mask = packet.GetU32 (&offset); |
| 812 | s.Printf(" (cpu_mask = 0x%8.8x)", cpu_mask); |
| 813 | } |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 814 | break; |
| 815 | |
| 816 | case eCommandTypeReadMemory: |
| 817 | { |
| 818 | const uint32_t addr = packet.GetU32 (&offset); |
| 819 | const uint32_t size = packet.GetU32 (&offset); |
| 820 | s.Printf(" (addr = 0x%8.8x, size=%u)", addr, size); |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame^] | 821 | m_last_read_memory_addr = addr; |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 822 | } |
| 823 | break; |
| 824 | |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 825 | case eCommandTypeWriteMemory: |
| 826 | { |
| 827 | const uint32_t addr = packet.GetU32 (&offset); |
| 828 | const uint32_t size = packet.GetU32 (&offset); |
| 829 | s.Printf(" (addr = 0x%8.8x, size=%u, bytes:\n", addr, size); |
| 830 | if (size > 0) |
| 831 | DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr); |
| 832 | } |
| 833 | break; |
| 834 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 835 | case eCommandTypeReadMemory64: |
| 836 | { |
| 837 | const uint64_t addr = packet.GetU64 (&offset); |
| 838 | const uint32_t size = packet.GetU32 (&offset); |
| 839 | s.Printf(" (addr = 0x%16.16llx, size=%u)", addr, size); |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame^] | 840 | m_last_read_memory_addr = addr; |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 841 | } |
| 842 | break; |
| 843 | |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 844 | case eCommandTypeWriteMemory64: |
| 845 | { |
| 846 | const uint64_t addr = packet.GetU64 (&offset); |
| 847 | const uint32_t size = packet.GetU32 (&offset); |
| 848 | s.Printf(" (addr = 0x%16.16llx, size=%u, bytes:", addr, size); |
| 849 | if (size > 0) |
| 850 | DataExtractor::DumpHexBytes(&s, packet.GetData(&offset, size), size, 32, addr); |
| 851 | } |
| 852 | break; |
| 853 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 854 | case eCommandTypeReadRegisters: |
| 855 | { |
| 856 | const uint32_t cpu = packet.GetU32 (&offset); |
| 857 | const uint32_t flavor = packet.GetU32 (&offset); |
| 858 | s.Printf(" (cpu = %u, flavor=%u)", cpu, flavor); |
| 859 | } |
| 860 | break; |
| 861 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 862 | case eCommandTypeWriteRegisters: |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 863 | { |
| 864 | const uint32_t cpu = packet.GetU32 (&offset); |
| 865 | const uint32_t flavor = packet.GetU32 (&offset); |
| 866 | const uint32_t nbytes = packet.GetByteSize() - offset; |
| 867 | s.Printf(" (cpu = %u, flavor=%u, regs:\n", cpu, flavor); |
| 868 | if (nbytes > 0) |
| 869 | packet.Dump (&s, // Stream to dump to |
| 870 | offset, // Offset within "packet" |
| 871 | eFormatHex, // Format to use |
| 872 | m_addr_byte_size, // Size of each item in bytes |
| 873 | nbytes / m_addr_byte_size, // Number of items |
| 874 | 16 / m_addr_byte_size, // Number per line |
| 875 | LLDB_INVALID_ADDRESS, // Don't show addresses before each line |
| 876 | 0, 0); // No bitfields |
| 877 | } |
| 878 | break; |
| 879 | |
Greg Clayton | ec15f50 | 2011-07-20 01:32:50 +0000 | [diff] [blame] | 880 | |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 881 | case eCommandTypeBreakpointSet: |
| 882 | case eCommandTypeBreakpointRemove: |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame^] | 883 | { |
| 884 | const uint32_t addr = packet.GetU32 (&offset); |
| 885 | s.Printf(" (addr = 0x%8.8x)", addr); |
| 886 | } |
| 887 | break; |
| 888 | |
| 889 | case eCommandTypeBreakpointSet64: |
| 890 | case eCommandTypeBreakpointRemove64: |
| 891 | { |
| 892 | const uint64_t addr = packet.GetU64 (&offset); |
| 893 | s.Printf(" (addr = 0x%16.16llx)", addr); |
| 894 | } |
| 895 | break; |
| 896 | |
| 897 | |
| 898 | case eCommandTypeLoad: |
| 899 | { |
| 900 | const char *path = packet.GetCStr(&offset); |
| 901 | s.Printf(" (path = \"%s\")", path); |
| 902 | } |
| 903 | break; |
| 904 | |
| 905 | case eCommandTypeException: |
| 906 | { |
| 907 | const uint32_t count = packet.GetU32 (&offset); |
| 908 | |
| 909 | s.Printf(" (count = %u:", count); |
| 910 | for (uint32_t i=0; i<count; ++i) |
| 911 | { |
| 912 | const uint32_t cpu = packet.GetU32 (&offset); |
| 913 | const uint32_t exc = packet.GetU32 (&offset); |
| 914 | const uint32_t code = packet.GetU32 (&offset); |
| 915 | const uint32_t subcode = packet.GetU32 (&offset); |
| 916 | const char *exc_cstr = NULL; |
| 917 | switch (exc) |
| 918 | { |
| 919 | case 1: exc_cstr = "EXC_BAD_ACCESS"; break; |
| 920 | case 2: exc_cstr = "EXC_BAD_INSTRUCTION"; break; |
| 921 | case 3: exc_cstr = "EXC_ARITHMETIC"; break; |
| 922 | case 4: exc_cstr = "EXC_EMULATION"; break; |
| 923 | case 5: exc_cstr = "EXC_SOFTWARE"; break; |
| 924 | case 6: exc_cstr = "EXC_BREAKPOINT"; break; |
| 925 | case 7: exc_cstr = "EXC_SYSCALL"; break; |
| 926 | case 8: exc_cstr = "EXC_MACH_SYSCALL"; break; |
| 927 | case 9: exc_cstr = "EXC_RPC_ALERT"; break; |
| 928 | case 10: exc_cstr = "EXC_CRASH"; break; |
| 929 | default: |
| 930 | break; |
| 931 | } |
| 932 | |
| 933 | s.Printf ("\n cpu = 0x%8.8x, exc = %s (%u), code = %u (0x%8.8x), subcode = %u (0x%8.8x)\n", |
| 934 | cpu, exc_cstr, exc, code, code, subcode, subcode); |
| 935 | } |
| 936 | } |
| 937 | break; |
| 938 | |
| 939 | case eCommandTypeTermination: |
| 940 | { |
| 941 | const uint32_t term_code = packet.GetU32 (&offset); |
| 942 | const uint32_t exit_code = packet.GetU32 (&offset); |
| 943 | s.Printf(" (term_code = 0x%8.8x (%u), exit_code = 0x%8.8x (%u))", term_code, term_code, exit_code, exit_code); |
| 944 | } |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 945 | break; |
| 946 | |
| 947 | case eCommandTypeReattach: |
| 948 | { |
| 949 | const uint16_t reply_port = packet.GetU16 (&offset); |
| 950 | s.Printf(" (reply_port=%u)", reply_port); |
| 951 | } |
| 952 | break; |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | } |
| 956 | else |
| 957 | { |
| 958 | error_desc = "error: invalid packet command: "; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | if (error_desc) |
| 963 | { |
| 964 | s.PutCString (error_desc); |
| 965 | |
| 966 | packet.Dump (&s, // Stream to dump to |
| 967 | 0, // Offset into "packet" |
| 968 | eFormatBytes, // Dump as hex bytes |
| 969 | 1, // Size of each item is 1 for single bytes |
| 970 | packet.GetByteSize(), // Number of bytes |
| 971 | UINT32_MAX, // Num bytes per line |
| 972 | LLDB_INVALID_ADDRESS, // Base address |
| 973 | 0, 0); // Bitfield info set to not do anything bitfield related |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | uint32_t |
| 978 | CommunicationKDP::SendRequestReadRegisters (uint32_t cpu, |
| 979 | uint32_t flavor, |
| 980 | void *dst, |
| 981 | uint32_t dst_len, |
| 982 | Error &error) |
| 983 | { |
| 984 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
| 985 | const CommandType command = eCommandTypeReadRegisters; |
| 986 | // Size is header + 4 byte cpu and 4 byte flavor |
| 987 | const uint32_t command_length = 8 + 4 + 4; |
| 988 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 989 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 990 | request_packet.PutHex32 (cpu); |
| 991 | request_packet.PutHex32 (flavor); |
| 992 | DataExtractor reply_packet; |
| 993 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 994 | { |
Greg Clayton | 0fa5124 | 2011-07-19 03:57:15 +0000 | [diff] [blame] | 995 | uint32_t offset = 8; |
| 996 | uint32_t kdp_error = reply_packet.GetU32 (&offset); |
| 997 | uint32_t src_len = reply_packet.GetByteSize() - 12; |
| 998 | |
| 999 | if (src_len > 0) |
| 1000 | { |
| 1001 | const uint32_t bytes_to_copy = std::min<uint32_t>(src_len, dst_len); |
| 1002 | const void *src = reply_packet.GetData(&offset, bytes_to_copy); |
| 1003 | if (src) |
| 1004 | { |
| 1005 | ::memcpy (dst, src, bytes_to_copy); |
| 1006 | error.Clear(); |
| 1007 | // Return the number of bytes we could have returned regardless if |
| 1008 | // we copied them or not, just so we know when things don't match up |
| 1009 | return src_len; |
| 1010 | } |
| 1011 | } |
| 1012 | if (kdp_error) |
| 1013 | error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u (error %u)", cpu, flavor, kdp_error); |
| 1014 | else |
| 1015 | error.SetErrorStringWithFormat("failed to read kdp registers for cpu %u flavor %u", cpu, flavor); |
| 1016 | } |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
Greg Clayton | 234981a | 2011-07-20 03:41:06 +0000 | [diff] [blame^] | 1020 | |
| 1021 | bool |
| 1022 | CommunicationKDP::SendRequestResume (uint32_t cpu_mask) |
| 1023 | { |
| 1024 | if (cpu_mask == 0) |
| 1025 | cpu_mask = GetCPUMask(); |
| 1026 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
| 1027 | const CommandType command = eCommandTypeResume; |
| 1028 | const uint32_t command_length = 12; |
| 1029 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 1030 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 1031 | request_packet.PutHex32(cpu_mask); |
| 1032 | |
| 1033 | DataExtractor reply_packet; |
| 1034 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 1035 | return true; |
| 1036 | return false; |
| 1037 | } |
| 1038 | |
| 1039 | bool |
| 1040 | CommunicationKDP::SendRequestBreakpoint (bool set, addr_t addr) |
| 1041 | { |
| 1042 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
| 1043 | bool use_64 = (GetVersion() >= 11); |
| 1044 | uint32_t command_addr_byte_size = use_64 ? 8 : 4; |
| 1045 | const CommandType command = set ? (use_64 ? eCommandTypeBreakpointSet64 : eCommandTypeBreakpointSet ): |
| 1046 | (use_64 ? eCommandTypeBreakpointRemove64 : eCommandTypeBreakpointRemove); |
| 1047 | |
| 1048 | const uint32_t command_length = 8 + command_addr_byte_size; |
| 1049 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 1050 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 1051 | request_packet.PutMaxHex64 (addr, command_addr_byte_size); |
| 1052 | |
| 1053 | DataExtractor reply_packet; |
| 1054 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 1055 | return true; |
| 1056 | return false; |
| 1057 | } |
| 1058 | |
| 1059 | bool |
| 1060 | CommunicationKDP::SendRequestSuspend () |
| 1061 | { |
| 1062 | PacketStreamType request_packet (Stream::eBinary, m_addr_byte_size, m_byte_order); |
| 1063 | const CommandType command = eCommandTypeSuspend; |
| 1064 | const uint32_t command_length = 8; |
| 1065 | const uint32_t request_sequence_id = m_request_sequence_id; |
| 1066 | MakeRequestPacketHeader (command, request_packet, command_length); |
| 1067 | DataExtractor reply_packet; |
| 1068 | if (SendRequestAndGetReply (command, request_sequence_id, request_packet, reply_packet)) |
| 1069 | return true; |
| 1070 | return false; |
| 1071 | } |
| 1072 | |