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