Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- GDBRemoteCommunication.cpp ------------------------------*- C++ -*-===// |
| 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 "GDBRemoteCommunication.h" |
| 12 | |
| 13 | // C Includes |
Johnny Chen | f387823 | 2011-05-13 20:07:25 +0000 | [diff] [blame] | 14 | #include <limits.h> |
Stephen Wilson | 50daf77 | 2011-03-25 18:16:28 +0000 | [diff] [blame] | 15 | #include <string.h> |
| 16 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | // C++ Includes |
| 18 | // Other libraries and framework includes |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Log.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "lldb/Core/StreamString.h" |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 21 | #include "lldb/Host/FileSpec.h" |
| 22 | #include "lldb/Host/Host.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #include "lldb/Host/TimeValue.h" |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 24 | #include "lldb/Target/Process.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | |
| 26 | // Project includes |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | #include "ProcessGDBRemoteLog.h" |
| 28 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 29 | #define DEBUGSERVER_BASENAME "debugserver" |
| 30 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | using namespace lldb; |
| 32 | using namespace lldb_private; |
| 33 | |
| 34 | //---------------------------------------------------------------------- |
| 35 | // GDBRemoteCommunication constructor |
| 36 | //---------------------------------------------------------------------- |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 37 | GDBRemoteCommunication::GDBRemoteCommunication(const char *comm_name, |
| 38 | const char *listener_name, |
| 39 | bool is_platform) : |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 40 | Communication(comm_name), |
Greg Clayton | 604f0d3 | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 41 | m_packet_timeout (1), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | m_sequence_mutex (Mutex::eMutexTypeRecursive), |
Greg Clayton | cecf348 | 2011-01-20 07:53:45 +0000 | [diff] [blame] | 43 | m_public_is_running (false), |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 44 | m_private_is_running (false), |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 45 | m_send_acks (true), |
| 46 | m_is_platform (is_platform) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | //---------------------------------------------------------------------- |
| 51 | // Destructor |
| 52 | //---------------------------------------------------------------------- |
| 53 | GDBRemoteCommunication::~GDBRemoteCommunication() |
| 54 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 55 | if (IsConnected()) |
| 56 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 57 | Disconnect(); |
| 58 | } |
| 59 | } |
| 60 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | char |
| 62 | GDBRemoteCommunication::CalculcateChecksum (const char *payload, size_t payload_length) |
| 63 | { |
| 64 | int checksum = 0; |
| 65 | |
| 66 | // We only need to compute the checksum if we are sending acks |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 67 | if (GetSendAcks ()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 68 | { |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 69 | for (size_t i = 0; i < payload_length; ++i) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | checksum += payload[i]; |
| 71 | } |
| 72 | return checksum & 255; |
| 73 | } |
| 74 | |
| 75 | size_t |
Greg Clayton | a4881d0 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 76 | GDBRemoteCommunication::SendAck () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | { |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 78 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
| 79 | if (log) |
| 80 | log->Printf ("send packet: +"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | ConnectionStatus status = eConnectionStatusSuccess; |
Greg Clayton | a4881d0 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 82 | char ack_char = '+'; |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 83 | return Write (&ack_char, 1, status, NULL); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | size_t |
Greg Clayton | a4881d0 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 87 | GDBRemoteCommunication::SendNack () |
| 88 | { |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 89 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
| 90 | if (log) |
| 91 | log->Printf ("send packet: -"); |
Greg Clayton | a4881d0 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 92 | ConnectionStatus status = eConnectionStatusSuccess; |
| 93 | char nack_char = '-'; |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 94 | return Write (&nack_char, 1, status, NULL); |
| 95 | } |
| 96 | |
| 97 | size_t |
| 98 | GDBRemoteCommunication::SendPacket (lldb_private::StreamString &payload) |
| 99 | { |
| 100 | Mutex::Locker locker(m_sequence_mutex); |
| 101 | const std::string &p (payload.GetString()); |
| 102 | return SendPacketNoLock (p.c_str(), p.size()); |
Greg Clayton | a4881d0 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 105 | size_t |
| 106 | GDBRemoteCommunication::SendPacket (const char *payload) |
| 107 | { |
| 108 | Mutex::Locker locker(m_sequence_mutex); |
| 109 | return SendPacketNoLock (payload, ::strlen (payload)); |
| 110 | } |
| 111 | |
| 112 | size_t |
| 113 | GDBRemoteCommunication::SendPacket (const char *payload, size_t payload_length) |
| 114 | { |
| 115 | Mutex::Locker locker(m_sequence_mutex); |
| 116 | return SendPacketNoLock (payload, payload_length); |
| 117 | } |
| 118 | |
| 119 | size_t |
| 120 | GDBRemoteCommunication::SendPacketNoLock (const char *payload, size_t payload_length) |
| 121 | { |
| 122 | if (IsConnected()) |
| 123 | { |
| 124 | StreamString packet(0, 4, eByteOrderBig); |
| 125 | |
| 126 | packet.PutChar('$'); |
| 127 | packet.Write (payload, payload_length); |
| 128 | packet.PutChar('#'); |
| 129 | packet.PutHex8(CalculcateChecksum (payload, payload_length)); |
| 130 | |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 131 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
| 132 | if (log) |
Greg Clayton | 139da72 | 2011-05-20 03:15:54 +0000 | [diff] [blame] | 133 | log->Printf ("send packet: %.*s", (int)packet.GetSize(), packet.GetData()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 134 | ConnectionStatus status = eConnectionStatusSuccess; |
| 135 | size_t bytes_written = Write (packet.GetData(), packet.GetSize(), status, NULL); |
| 136 | if (bytes_written == packet.GetSize()) |
| 137 | { |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 138 | if (GetSendAcks ()) |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 139 | { |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 140 | if (GetAck () != '+') |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 141 | { |
| 142 | printf("get ack failed..."); |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 143 | return 0; |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 144 | } |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 145 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 146 | } |
Johnny Chen | 515ea54 | 2010-09-14 22:10:43 +0000 | [diff] [blame] | 147 | else |
| 148 | { |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 149 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
| 150 | if (log) |
Greg Clayton | 139da72 | 2011-05-20 03:15:54 +0000 | [diff] [blame] | 151 | log->Printf ("error: failed to send packet: %.*s", (int)packet.GetSize(), packet.GetData()); |
Johnny Chen | 515ea54 | 2010-09-14 22:10:43 +0000 | [diff] [blame] | 152 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 153 | return bytes_written; |
Greg Clayton | eea2640 | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 154 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | char |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 159 | GDBRemoteCommunication::GetAck () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 160 | { |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 161 | StringExtractorGDBRemote packet; |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 162 | if (WaitForPacketWithTimeoutMicroSeconds (packet, GetPacketTimeoutInMicroSeconds ()) == 1) |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 163 | return packet.GetChar(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | bool |
| 168 | GDBRemoteCommunication::GetSequenceMutex (Mutex::Locker& locker) |
| 169 | { |
| 170 | return locker.TryLock (m_sequence_mutex.GetMutex()); |
| 171 | } |
| 172 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 173 | |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 174 | bool |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 175 | GDBRemoteCommunication::WaitForNotRunningPrivate (const TimeValue *timeout_ptr) |
| 176 | { |
| 177 | return m_private_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL); |
| 178 | } |
| 179 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | size_t |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 181 | GDBRemoteCommunication::WaitForPacketWithTimeoutMicroSeconds (StringExtractorGDBRemote &packet, uint32_t timeout_usec) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 182 | { |
Greg Clayton | cecf348 | 2011-01-20 07:53:45 +0000 | [diff] [blame] | 183 | Mutex::Locker locker(m_sequence_mutex); |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 184 | return WaitForPacketWithTimeoutMicroSecondsNoLock (packet, timeout_usec); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | size_t |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 188 | GDBRemoteCommunication::WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &packet, uint32_t timeout_usec) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 189 | { |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 190 | uint8_t buffer[8192]; |
| 191 | Error error; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 192 | |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 193 | // Check for a packet from our cache first without trying any reading... |
| 194 | if (CheckForPacket (NULL, 0, packet)) |
| 195 | return packet.GetStringRef().size(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 196 | |
Greg Clayton | d0691fe | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 197 | bool timed_out = false; |
| 198 | while (IsConnected() && !timed_out) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 199 | { |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 200 | lldb::ConnectionStatus status; |
| 201 | size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error); |
| 202 | if (bytes_read > 0) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 203 | { |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 204 | if (CheckForPacket (buffer, bytes_read, packet)) |
| 205 | return packet.GetStringRef().size(); |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | switch (status) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 210 | { |
Greg Clayton | a9eb827 | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 211 | case eConnectionStatusTimedOut: |
Greg Clayton | d0691fe | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 212 | timed_out = true; |
| 213 | break; |
| 214 | case eConnectionStatusSuccess: |
| 215 | //printf ("status = success but error = %s\n", error.AsCString("<invalid>")); |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 216 | break; |
| 217 | |
| 218 | case eConnectionStatusEndOfFile: |
| 219 | case eConnectionStatusNoConnection: |
| 220 | case eConnectionStatusLostConnection: |
| 221 | case eConnectionStatusError: |
| 222 | Disconnect(); |
| 223 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 224 | } |
| 225 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 226 | } |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 227 | packet.Clear (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 228 | return 0; |
| 229 | } |
| 230 | |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 231 | bool |
| 232 | GDBRemoteCommunication::CheckForPacket (const uint8_t *src, size_t src_len, StringExtractorGDBRemote &packet) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 233 | { |
| 234 | // Put the packet data into the buffer in a thread safe fashion |
| 235 | Mutex::Locker locker(m_bytes_mutex); |
Greg Clayton | a9eb827 | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 236 | |
| 237 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
| 238 | |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 239 | if (src && src_len > 0) |
Greg Clayton | a9eb827 | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 240 | { |
Greg Clayton | d0691fe | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 241 | if (log && log->GetVerbose()) |
Greg Clayton | a9eb827 | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 242 | { |
| 243 | StreamString s; |
Greg Clayton | d0691fe | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 244 | log->Printf ("GDBRemoteCommunication::%s adding %u bytes: %.*s", |
| 245 | __FUNCTION__, |
| 246 | (uint32_t)src_len, |
| 247 | (uint32_t)src_len, |
| 248 | src); |
Greg Clayton | a9eb827 | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 249 | } |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 250 | m_bytes.append ((const char *)src, src_len); |
Greg Clayton | a9eb827 | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 251 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 252 | |
| 253 | // Parse up the packets into gdb remote packets |
Greg Clayton | a9eb827 | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 254 | if (!m_bytes.empty()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 255 | { |
| 256 | // end_idx must be one past the last valid packet byte. Start |
| 257 | // it off with an invalid value that is the same as the current |
| 258 | // index. |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 259 | size_t content_start = 0; |
| 260 | size_t content_length = 0; |
| 261 | size_t total_length = 0; |
| 262 | size_t checksum_idx = std::string::npos; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 263 | |
| 264 | switch (m_bytes[0]) |
| 265 | { |
| 266 | case '+': // Look for ack |
| 267 | case '-': // Look for cancel |
| 268 | case '\x03': // ^C to halt target |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 269 | content_length = total_length = 1; // The command is one byte long... |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 270 | break; |
| 271 | |
| 272 | case '$': |
| 273 | // Look for a standard gdb packet? |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 274 | { |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 275 | size_t hash_pos = m_bytes.find('#'); |
| 276 | if (hash_pos != std::string::npos) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 277 | { |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 278 | if (hash_pos + 2 < m_bytes.size()) |
| 279 | { |
| 280 | checksum_idx = hash_pos + 1; |
| 281 | // Skip the dollar sign |
| 282 | content_start = 1; |
| 283 | // Don't include the # in the content or the $ in the content length |
| 284 | content_length = hash_pos - 1; |
| 285 | |
| 286 | total_length = hash_pos + 3; // Skip the # and the two hex checksum bytes |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | // Checksum bytes aren't all here yet |
| 291 | content_length = std::string::npos; |
| 292 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | break; |
| 296 | |
| 297 | default: |
Greg Clayton | 604f0d3 | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 298 | { |
Greg Clayton | a9eb827 | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 299 | // We have an unexpected byte and we need to flush all bad |
| 300 | // data that is in m_bytes, so we need to find the first |
| 301 | // byte that is a '+' (ACK), '-' (NACK), \x03 (CTRL+C interrupt), |
| 302 | // or '$' character (start of packet header) or of course, |
| 303 | // the end of the data in m_bytes... |
| 304 | const size_t bytes_len = m_bytes.size(); |
| 305 | bool done = false; |
| 306 | uint32_t idx; |
| 307 | for (idx = 1; !done && idx < bytes_len; ++idx) |
| 308 | { |
| 309 | switch (m_bytes[idx]) |
| 310 | { |
| 311 | case '+': |
| 312 | case '-': |
| 313 | case '\x03': |
| 314 | case '$': |
| 315 | done = true; |
| 316 | break; |
| 317 | |
| 318 | default: |
| 319 | break; |
| 320 | } |
| 321 | } |
Greg Clayton | 604f0d3 | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 322 | if (log) |
Greg Clayton | a9eb827 | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 323 | log->Printf ("GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'", |
| 324 | __FUNCTION__, idx, idx, m_bytes.c_str()); |
| 325 | m_bytes.erase(0, idx); |
Greg Clayton | 604f0d3 | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 326 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 327 | break; |
| 328 | } |
| 329 | |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 330 | if (content_length == std::string::npos) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 331 | { |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 332 | packet.Clear(); |
| 333 | return false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 334 | } |
Greg Clayton | 604f0d3 | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 335 | else if (total_length > 0) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 336 | { |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 337 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 338 | // We have a valid packet... |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 339 | assert (content_length <= m_bytes.size()); |
| 340 | assert (total_length <= m_bytes.size()); |
| 341 | assert (content_length <= total_length); |
| 342 | |
| 343 | bool success = true; |
| 344 | std::string &packet_str = packet.GetStringRef(); |
| 345 | packet_str.assign (m_bytes, content_start, content_length); |
| 346 | if (m_bytes[0] == '$') |
| 347 | { |
| 348 | assert (checksum_idx < m_bytes.size()); |
| 349 | if (::isxdigit (m_bytes[checksum_idx+0]) || |
| 350 | ::isxdigit (m_bytes[checksum_idx+1])) |
| 351 | { |
| 352 | if (GetSendAcks ()) |
| 353 | { |
| 354 | const char *packet_checksum_cstr = &m_bytes[checksum_idx]; |
| 355 | char packet_checksum = strtol (packet_checksum_cstr, NULL, 16); |
| 356 | char actual_checksum = CalculcateChecksum (packet_str.c_str(), packet_str.size()); |
| 357 | success = packet_checksum == actual_checksum; |
| 358 | if (!success) |
| 359 | { |
| 360 | if (log) |
| 361 | log->Printf ("error: checksum mismatch: %.*s expected 0x%2.2x, got 0x%2.2x", |
| 362 | (int)(total_length), |
| 363 | m_bytes.c_str(), |
| 364 | (uint8_t)packet_checksum, |
| 365 | (uint8_t)actual_checksum); |
| 366 | } |
| 367 | // Send the ack or nack if needed |
| 368 | if (!success) |
| 369 | SendNack(); |
| 370 | else |
| 371 | SendAck(); |
| 372 | } |
| 373 | if (success) |
| 374 | { |
| 375 | if (log) |
| 376 | log->Printf ("read packet: %.*s", (int)(total_length), m_bytes.c_str()); |
| 377 | } |
| 378 | } |
| 379 | else |
| 380 | { |
| 381 | success = false; |
| 382 | if (log) |
| 383 | log->Printf ("error: invalid checksum in packet: '%s'\n", (int)(total_length), m_bytes.c_str()); |
| 384 | } |
| 385 | } |
| 386 | m_bytes.erase(0, total_length); |
| 387 | packet.SetFilePos(0); |
| 388 | return success; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 389 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 390 | } |
Greg Clayton | 63afdb0 | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 391 | packet.Clear(); |
| 392 | return false; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 395 | Error |
| 396 | GDBRemoteCommunication::StartDebugserverProcess (const char *debugserver_url, |
| 397 | const char *unix_socket_name, // For handshaking |
| 398 | lldb_private::ProcessLaunchInfo &launch_info) |
| 399 | { |
| 400 | Error error; |
| 401 | // If we locate debugserver, keep that located version around |
| 402 | static FileSpec g_debugserver_file_spec; |
| 403 | |
| 404 | // This function will fill in the launch information for the debugserver |
| 405 | // instance that gets launched. |
| 406 | launch_info.Clear(); |
| 407 | |
| 408 | char debugserver_path[PATH_MAX]; |
| 409 | FileSpec &debugserver_file_spec = launch_info.GetExecutableFile(); |
| 410 | |
| 411 | // Always check to see if we have an environment override for the path |
| 412 | // to the debugserver to use and use it if we do. |
| 413 | const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH"); |
| 414 | if (env_debugserver_path) |
| 415 | debugserver_file_spec.SetFile (env_debugserver_path, false); |
| 416 | else |
| 417 | debugserver_file_spec = g_debugserver_file_spec; |
| 418 | bool debugserver_exists = debugserver_file_spec.Exists(); |
| 419 | if (!debugserver_exists) |
| 420 | { |
| 421 | // The debugserver binary is in the LLDB.framework/Resources |
| 422 | // directory. |
| 423 | if (Host::GetLLDBPath (ePathTypeSupportExecutableDir, debugserver_file_spec)) |
| 424 | { |
| 425 | debugserver_file_spec.GetFilename().SetCString(DEBUGSERVER_BASENAME); |
| 426 | debugserver_exists = debugserver_file_spec.Exists(); |
| 427 | if (debugserver_exists) |
| 428 | { |
| 429 | g_debugserver_file_spec = debugserver_file_spec; |
| 430 | } |
| 431 | else |
| 432 | { |
| 433 | g_debugserver_file_spec.Clear(); |
| 434 | debugserver_file_spec.Clear(); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | if (debugserver_exists) |
| 440 | { |
| 441 | debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path)); |
| 442 | |
| 443 | Args &debugserver_args = launch_info.GetArguments(); |
| 444 | debugserver_args.Clear(); |
| 445 | char arg_cstr[PATH_MAX]; |
| 446 | |
| 447 | // Start args with "debugserver /file/path -r --" |
| 448 | debugserver_args.AppendArgument(debugserver_path); |
| 449 | debugserver_args.AppendArgument(debugserver_url); |
| 450 | // use native registers, not the GDB registers |
| 451 | debugserver_args.AppendArgument("--native-regs"); |
| 452 | // make debugserver run in its own session so signals generated by |
| 453 | // special terminal key sequences (^C) don't affect debugserver |
| 454 | debugserver_args.AppendArgument("--setsid"); |
| 455 | |
| 456 | if (unix_socket_name && unix_socket_name[0]) |
| 457 | { |
| 458 | debugserver_args.AppendArgument("--unix-socket"); |
| 459 | debugserver_args.AppendArgument(unix_socket_name); |
| 460 | } |
| 461 | |
| 462 | const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE"); |
| 463 | if (env_debugserver_log_file) |
| 464 | { |
| 465 | ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file); |
| 466 | debugserver_args.AppendArgument(arg_cstr); |
| 467 | } |
| 468 | |
| 469 | const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS"); |
| 470 | if (env_debugserver_log_flags) |
| 471 | { |
| 472 | ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags); |
| 473 | debugserver_args.AppendArgument(arg_cstr); |
| 474 | } |
| 475 | // debugserver_args.AppendArgument("--log-file=/tmp/debugserver.txt"); |
| 476 | // debugserver_args.AppendArgument("--log-flags=0x802e0e"); |
| 477 | |
| 478 | // We currently send down all arguments, attach pids, or attach |
| 479 | // process names in dedicated GDB server packets, so we don't need |
| 480 | // to pass them as arguments. This is currently because of all the |
| 481 | // things we need to setup prior to launching: the environment, |
| 482 | // current working dir, file actions, etc. |
| 483 | #if 0 |
| 484 | // Now append the program arguments |
| 485 | if (inferior_argv) |
| 486 | { |
| 487 | // Terminate the debugserver args so we can now append the inferior args |
| 488 | debugserver_args.AppendArgument("--"); |
| 489 | |
| 490 | for (int i = 0; inferior_argv[i] != NULL; ++i) |
| 491 | debugserver_args.AppendArgument (inferior_argv[i]); |
| 492 | } |
| 493 | else if (attach_pid != LLDB_INVALID_PROCESS_ID) |
| 494 | { |
| 495 | ::snprintf (arg_cstr, sizeof(arg_cstr), "--attach=%u", attach_pid); |
| 496 | debugserver_args.AppendArgument (arg_cstr); |
| 497 | } |
| 498 | else if (attach_name && attach_name[0]) |
| 499 | { |
| 500 | if (wait_for_launch) |
| 501 | debugserver_args.AppendArgument ("--waitfor"); |
| 502 | else |
| 503 | debugserver_args.AppendArgument ("--attach"); |
| 504 | debugserver_args.AppendArgument (attach_name); |
| 505 | } |
| 506 | #endif |
| 507 | |
| 508 | // Close STDIN, STDOUT and STDERR. We might need to redirect them |
| 509 | // to "/dev/null" if we run into any problems. |
| 510 | // launch_info.AppendCloseFileAction (STDIN_FILENO); |
| 511 | // launch_info.AppendCloseFileAction (STDOUT_FILENO); |
| 512 | // launch_info.AppendCloseFileAction (STDERR_FILENO); |
| 513 | |
| 514 | error = Host::LaunchProcess(launch_info); |
| 515 | } |
| 516 | else |
| 517 | { |
| 518 | error.SetErrorStringWithFormat ("Unable to locate " DEBUGSERVER_BASENAME ".\n"); |
| 519 | } |
| 520 | return error; |
| 521 | } |
| 522 | |