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