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 |
Stephen Wilson | 50daf77 | 2011-03-25 18:16:28 +0000 | [diff] [blame^] | 14 | #include <string.h> |
| 15 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | // C++ Includes |
| 17 | // Other libraries and framework includes |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "lldb/Core/Log.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include "lldb/Core/StreamString.h" |
| 20 | #include "lldb/Host/TimeValue.h" |
| 21 | |
| 22 | // Project includes |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #include "ProcessGDBRemoteLog.h" |
| 24 | |
| 25 | using namespace lldb; |
| 26 | using namespace lldb_private; |
| 27 | |
| 28 | //---------------------------------------------------------------------- |
| 29 | // GDBRemoteCommunication constructor |
| 30 | //---------------------------------------------------------------------- |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 31 | GDBRemoteCommunication::GDBRemoteCommunication(const char *comm_name, const char *listener_name) : |
| 32 | Communication(comm_name), |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 33 | m_packet_timeout (1), |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 34 | m_rx_packet_listener (listener_name), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | m_sequence_mutex (Mutex::eMutexTypeRecursive), |
Greg Clayton | cecf348 | 2011-01-20 07:53:45 +0000 | [diff] [blame] | 36 | m_public_is_running (false), |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 37 | m_private_is_running (false), |
| 38 | m_send_acks (true) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | { |
| 40 | m_rx_packet_listener.StartListeningForEvents(this, |
| 41 | Communication::eBroadcastBitPacketAvailable | |
| 42 | Communication::eBroadcastBitReadThreadDidExit); |
| 43 | } |
| 44 | |
| 45 | //---------------------------------------------------------------------- |
| 46 | // Destructor |
| 47 | //---------------------------------------------------------------------- |
| 48 | GDBRemoteCommunication::~GDBRemoteCommunication() |
| 49 | { |
| 50 | m_rx_packet_listener.StopListeningForEvents(this, |
| 51 | Communication::eBroadcastBitPacketAvailable | |
| 52 | Communication::eBroadcastBitReadThreadDidExit); |
| 53 | if (IsConnected()) |
| 54 | { |
| 55 | StopReadThread(); |
| 56 | Disconnect(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | |
| 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 = '+'; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 83 | return Write (&ack_char, 1, status, NULL) == 1; |
| 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 = '-'; |
| 94 | return Write (&nack_char, 1, status, NULL) == 1; |
| 95 | } |
| 96 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 97 | size_t |
| 98 | GDBRemoteCommunication::SendPacket (const char *payload) |
| 99 | { |
| 100 | Mutex::Locker locker(m_sequence_mutex); |
| 101 | return SendPacketNoLock (payload, ::strlen (payload)); |
| 102 | } |
| 103 | |
| 104 | size_t |
| 105 | GDBRemoteCommunication::SendPacket (const char *payload, size_t payload_length) |
| 106 | { |
| 107 | Mutex::Locker locker(m_sequence_mutex); |
| 108 | return SendPacketNoLock (payload, payload_length); |
| 109 | } |
| 110 | |
| 111 | size_t |
| 112 | GDBRemoteCommunication::SendPacketNoLock (const char *payload, size_t payload_length) |
| 113 | { |
| 114 | if (IsConnected()) |
| 115 | { |
| 116 | StreamString packet(0, 4, eByteOrderBig); |
| 117 | |
| 118 | packet.PutChar('$'); |
| 119 | packet.Write (payload, payload_length); |
| 120 | packet.PutChar('#'); |
| 121 | packet.PutHex8(CalculcateChecksum (payload, payload_length)); |
| 122 | |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 123 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
| 124 | if (log) |
| 125 | log->Printf ("send packet: %s", packet.GetData()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 126 | ConnectionStatus status = eConnectionStatusSuccess; |
| 127 | size_t bytes_written = Write (packet.GetData(), packet.GetSize(), status, NULL); |
| 128 | if (bytes_written == packet.GetSize()) |
| 129 | { |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 130 | if (GetSendAcks ()) |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 131 | { |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 132 | if (GetAck () != '+') |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 133 | { |
| 134 | printf("get ack failed..."); |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 135 | return 0; |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 136 | } |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 137 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 138 | } |
Johnny Chen | 515ea54 | 2010-09-14 22:10:43 +0000 | [diff] [blame] | 139 | else |
| 140 | { |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 141 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
| 142 | if (log) |
| 143 | log->Printf ("error: failed to send packet: %s", packet.GetData()); |
Johnny Chen | 515ea54 | 2010-09-14 22:10:43 +0000 | [diff] [blame] | 144 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 145 | return bytes_written; |
Greg Clayton | eea2640 | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 146 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | char |
Greg Clayton | c97bfdb | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 151 | GDBRemoteCommunication::GetAck () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 152 | { |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 153 | StringExtractorGDBRemote packet; |
| 154 | if (WaitForPacket (packet, m_packet_timeout) == 1) |
| 155 | return packet.GetChar(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | bool |
| 160 | GDBRemoteCommunication::GetSequenceMutex (Mutex::Locker& locker) |
| 161 | { |
| 162 | return locker.TryLock (m_sequence_mutex.GetMutex()); |
| 163 | } |
| 164 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 165 | |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 166 | bool |
Greg Clayton | 72e1c78 | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 167 | GDBRemoteCommunication::WaitForNotRunningPrivate (const TimeValue *timeout_ptr) |
| 168 | { |
| 169 | return m_private_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL); |
| 170 | } |
| 171 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 172 | size_t |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 173 | GDBRemoteCommunication::WaitForPacket (StringExtractorGDBRemote &packet, uint32_t timeout_seconds) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 174 | { |
Greg Clayton | cecf348 | 2011-01-20 07:53:45 +0000 | [diff] [blame] | 175 | Mutex::Locker locker(m_sequence_mutex); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 176 | TimeValue timeout_time; |
| 177 | timeout_time = TimeValue::Now(); |
| 178 | timeout_time.OffsetWithSeconds (timeout_seconds); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 179 | return WaitForPacketNoLock (packet, &timeout_time); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | size_t |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 183 | GDBRemoteCommunication::WaitForPacket (StringExtractorGDBRemote &packet, const TimeValue* timeout_time_ptr) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 184 | { |
| 185 | Mutex::Locker locker(m_sequence_mutex); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 186 | return WaitForPacketNoLock (packet, timeout_time_ptr); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | size_t |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 190 | GDBRemoteCommunication::WaitForPacketNoLock (StringExtractorGDBRemote &packet, const TimeValue* timeout_time_ptr) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 191 | { |
| 192 | bool checksum_error = false; |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 193 | packet.Clear (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 194 | |
| 195 | EventSP event_sp; |
| 196 | |
| 197 | if (m_rx_packet_listener.WaitForEvent (timeout_time_ptr, event_sp)) |
| 198 | { |
| 199 | const uint32_t event_type = event_sp->GetType(); |
| 200 | if (event_type | Communication::eBroadcastBitPacketAvailable) |
| 201 | { |
| 202 | const EventDataBytes *event_bytes = EventDataBytes::GetEventDataFromEvent(event_sp.get()); |
| 203 | if (event_bytes) |
| 204 | { |
| 205 | const char * packet_data = (const char *)event_bytes->GetBytes(); |
Greg Clayton | 0bfda0b | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 206 | LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
| 207 | if (log) |
| 208 | log->Printf ("read packet: %s", packet_data); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 209 | const size_t packet_size = event_bytes->GetByteSize(); |
| 210 | if (packet_data && packet_size > 0) |
| 211 | { |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 212 | std::string &packet_str = packet.GetStringRef(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 213 | if (packet_data[0] == '$') |
| 214 | { |
Greg Clayton | 4862fa2 | 2011-01-08 03:17:57 +0000 | [diff] [blame] | 215 | bool success = false; |
| 216 | if (packet_size < 4) |
| 217 | ::fprintf (stderr, "Packet that starts with $ is too short: '%s'\n", packet_data); |
| 218 | else if (packet_data[packet_size-3] != '#' || |
| 219 | !::isxdigit (packet_data[packet_size-2]) || |
| 220 | !::isxdigit (packet_data[packet_size-1])) |
| 221 | ::fprintf (stderr, "Invalid checksum footer for packet: '%s'\n", packet_data); |
| 222 | else |
| 223 | success = true; |
| 224 | |
| 225 | if (success) |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 226 | packet_str.assign (packet_data + 1, packet_size - 4); |
Greg Clayton | c1f4587 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 227 | if (GetSendAcks ()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 228 | { |
| 229 | char packet_checksum = strtol (&packet_data[packet_size-2], NULL, 16); |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 230 | char actual_checksum = CalculcateChecksum (&packet_str[0], packet_str.size()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 231 | checksum_error = packet_checksum != actual_checksum; |
| 232 | // Send the ack or nack if needed |
Greg Clayton | 4862fa2 | 2011-01-08 03:17:57 +0000 | [diff] [blame] | 233 | if (checksum_error || !success) |
Greg Clayton | a4881d0 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 234 | SendNack(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 235 | else |
Greg Clayton | a4881d0 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 236 | SendAck(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | else |
| 240 | { |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 241 | packet_str.assign (packet_data, packet_size); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 242 | } |
Greg Clayton | 61d043b | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 243 | return packet_str.size(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | } |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 247 | else if (event_type | Communication::eBroadcastBitReadThreadDidExit) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 248 | { |
| 249 | // Our read thread exited on us so just fall through and return zero... |
Greg Clayton | 58e26e0 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 250 | Disconnect(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | void |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 257 | GDBRemoteCommunication::AppendBytesToCache (const uint8_t *src, size_t src_len, bool broadcast, |
| 258 | ConnectionStatus status) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 259 | { |
| 260 | // Put the packet data into the buffer in a thread safe fashion |
| 261 | Mutex::Locker locker(m_bytes_mutex); |
| 262 | m_bytes.append ((const char *)src, src_len); |
| 263 | |
| 264 | // Parse up the packets into gdb remote packets |
| 265 | while (!m_bytes.empty()) |
| 266 | { |
| 267 | // end_idx must be one past the last valid packet byte. Start |
| 268 | // it off with an invalid value that is the same as the current |
| 269 | // index. |
| 270 | size_t end_idx = 0; |
| 271 | |
| 272 | switch (m_bytes[0]) |
| 273 | { |
| 274 | case '+': // Look for ack |
| 275 | case '-': // Look for cancel |
| 276 | case '\x03': // ^C to halt target |
| 277 | end_idx = 1; // The command is one byte long... |
| 278 | break; |
| 279 | |
| 280 | case '$': |
| 281 | // Look for a standard gdb packet? |
| 282 | end_idx = m_bytes.find('#'); |
| 283 | if (end_idx != std::string::npos) |
| 284 | { |
| 285 | if (end_idx + 2 < m_bytes.size()) |
| 286 | { |
| 287 | end_idx += 3; |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | // Checksum bytes aren't all here yet |
| 292 | end_idx = std::string::npos; |
| 293 | } |
| 294 | } |
| 295 | break; |
| 296 | |
| 297 | default: |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | if (end_idx == std::string::npos) |
| 302 | { |
| 303 | //ProcessGDBRemoteLog::LogIf (GDBR_LOG_PACKETS | GDBR_LOG_VERBOSE, "GDBRemoteCommunication::%s packet not yet complete: '%s'",__FUNCTION__, m_bytes.c_str()); |
| 304 | return; |
| 305 | } |
| 306 | else if (end_idx > 0) |
| 307 | { |
| 308 | // We have a valid packet... |
| 309 | assert (end_idx <= m_bytes.size()); |
| 310 | std::auto_ptr<EventDataBytes> event_bytes_ap (new EventDataBytes (&m_bytes[0], end_idx)); |
| 311 | ProcessGDBRemoteLog::LogIf (GDBR_LOG_COMM, "got full packet: %s", event_bytes_ap->GetBytes()); |
| 312 | BroadcastEvent (eBroadcastBitPacketAvailable, event_bytes_ap.release()); |
| 313 | m_bytes.erase(0, end_idx); |
| 314 | } |
| 315 | else |
| 316 | { |
| 317 | assert (1 <= m_bytes.size()); |
| 318 | ProcessGDBRemoteLog::LogIf (GDBR_LOG_COMM, "GDBRemoteCommunication::%s tossing junk byte at %c",__FUNCTION__, m_bytes[0]); |
| 319 | m_bytes.erase(0, 1); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |