Chris Lattner | 30fdc8d | 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 | a566355 | 2011-05-13 20:07:25 +0000 | [diff] [blame] | 14 | #include <limits.h> |
Stephen Wilson | a78867b | 2011-03-25 18:16:28 +0000 | [diff] [blame] | 15 | #include <string.h> |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 16 | #include <sys/stat.h> |
Stephen Wilson | a78867b | 2011-03-25 18:16:28 +0000 | [diff] [blame] | 17 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | // C++ Includes |
| 19 | // Other libraries and framework includes |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 20 | #include "lldb/Core/ConnectionFileDescriptor.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include "lldb/Core/Log.h" |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 22 | #include "lldb/Core/StreamFile.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | #include "lldb/Core/StreamString.h" |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 24 | #include "lldb/Host/FileSpec.h" |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 25 | #include "lldb/Host/FileSystem.h" |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 26 | #include "lldb/Host/Host.h" |
Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 27 | #include "lldb/Host/HostInfo.h" |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 28 | #include "lldb/Host/Socket.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 29 | #include "lldb/Host/ThreadLauncher.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | #include "lldb/Host/TimeValue.h" |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 31 | #include "lldb/Target/Process.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | |
| 33 | // Project includes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | #include "ProcessGDBRemoteLog.h" |
| 35 | |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 36 | #if defined(__APPLE__) |
| 37 | # define DEBUGSERVER_BASENAME "debugserver" |
| 38 | #else |
| 39 | # define DEBUGSERVER_BASENAME "lldb-gdbserver" |
| 40 | #endif |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 42 | using namespace lldb; |
| 43 | using namespace lldb_private; |
| 44 | |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 45 | GDBRemoteCommunication::History::History (uint32_t size) : |
| 46 | m_packets(), |
| 47 | m_curr_idx (0), |
| 48 | m_total_packet_count (0), |
| 49 | m_dumped_to_log (false) |
| 50 | { |
| 51 | m_packets.resize(size); |
| 52 | } |
| 53 | |
| 54 | GDBRemoteCommunication::History::~History () |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | void |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 59 | GDBRemoteCommunication::History::AddPacket (char packet_char, |
| 60 | PacketType type, |
| 61 | uint32_t bytes_transmitted) |
| 62 | { |
| 63 | const size_t size = m_packets.size(); |
| 64 | if (size > 0) |
| 65 | { |
| 66 | const uint32_t idx = GetNextIndex(); |
| 67 | m_packets[idx].packet.assign (1, packet_char); |
| 68 | m_packets[idx].type = type; |
| 69 | m_packets[idx].bytes_transmitted = bytes_transmitted; |
| 70 | m_packets[idx].packet_idx = m_total_packet_count; |
| 71 | m_packets[idx].tid = Host::GetCurrentThreadID(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | GDBRemoteCommunication::History::AddPacket (const std::string &src, |
| 77 | uint32_t src_len, |
| 78 | PacketType type, |
| 79 | uint32_t bytes_transmitted) |
| 80 | { |
| 81 | const size_t size = m_packets.size(); |
| 82 | if (size > 0) |
| 83 | { |
| 84 | const uint32_t idx = GetNextIndex(); |
| 85 | m_packets[idx].packet.assign (src, 0, src_len); |
| 86 | m_packets[idx].type = type; |
| 87 | m_packets[idx].bytes_transmitted = bytes_transmitted; |
| 88 | m_packets[idx].packet_idx = m_total_packet_count; |
| 89 | m_packets[idx].tid = Host::GetCurrentThreadID(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 94 | GDBRemoteCommunication::History::Dump (lldb_private::Stream &strm) const |
| 95 | { |
| 96 | const uint32_t size = GetNumPacketsInHistory (); |
| 97 | const uint32_t first_idx = GetFirstSavedPacketIndex (); |
| 98 | const uint32_t stop_idx = m_curr_idx + size; |
| 99 | for (uint32_t i = first_idx; i < stop_idx; ++i) |
| 100 | { |
| 101 | const uint32_t idx = NormalizeIndex (i); |
| 102 | const Entry &entry = m_packets[idx]; |
| 103 | if (entry.type == ePacketTypeInvalid || entry.packet.empty()) |
| 104 | break; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 105 | strm.Printf ("history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s\n", |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 106 | entry.packet_idx, |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 107 | entry.tid, |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 108 | entry.bytes_transmitted, |
| 109 | (entry.type == ePacketTypeSend) ? "send" : "read", |
| 110 | entry.packet.c_str()); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | GDBRemoteCommunication::History::Dump (lldb_private::Log *log) const |
| 116 | { |
| 117 | if (log && !m_dumped_to_log) |
| 118 | { |
| 119 | m_dumped_to_log = true; |
| 120 | const uint32_t size = GetNumPacketsInHistory (); |
| 121 | const uint32_t first_idx = GetFirstSavedPacketIndex (); |
| 122 | const uint32_t stop_idx = m_curr_idx + size; |
| 123 | for (uint32_t i = first_idx; i < stop_idx; ++i) |
| 124 | { |
| 125 | const uint32_t idx = NormalizeIndex (i); |
| 126 | const Entry &entry = m_packets[idx]; |
| 127 | if (entry.type == ePacketTypeInvalid || entry.packet.empty()) |
| 128 | break; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 129 | log->Printf ("history[%u] tid=0x%4.4" PRIx64 " <%4u> %s packet: %s", |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 130 | entry.packet_idx, |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 131 | entry.tid, |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 132 | entry.bytes_transmitted, |
| 133 | (entry.type == ePacketTypeSend) ? "send" : "read", |
| 134 | entry.packet.c_str()); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 139 | //---------------------------------------------------------------------- |
| 140 | // GDBRemoteCommunication constructor |
| 141 | //---------------------------------------------------------------------- |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 142 | GDBRemoteCommunication::GDBRemoteCommunication(const char *comm_name, |
| 143 | const char *listener_name, |
| 144 | bool is_platform) : |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 145 | Communication(comm_name), |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 146 | #ifdef LLDB_CONFIGURATION_DEBUG |
| 147 | m_packet_timeout (1000), |
| 148 | #else |
Greg Clayton | f3dd93c | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 149 | m_packet_timeout (1), |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 150 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 151 | m_sequence_mutex (Mutex::eMutexTypeRecursive), |
Greg Clayton | 4dc7228 | 2011-01-20 07:53:45 +0000 | [diff] [blame] | 152 | m_public_is_running (false), |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 153 | m_private_is_running (false), |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 154 | m_history (512), |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 155 | m_send_acks (true), |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 156 | m_is_platform (is_platform), |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 157 | m_listen_url () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 158 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | //---------------------------------------------------------------------- |
| 162 | // Destructor |
| 163 | //---------------------------------------------------------------------- |
| 164 | GDBRemoteCommunication::~GDBRemoteCommunication() |
| 165 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 166 | if (IsConnected()) |
| 167 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | Disconnect(); |
| 169 | } |
| 170 | } |
| 171 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 172 | char |
| 173 | GDBRemoteCommunication::CalculcateChecksum (const char *payload, size_t payload_length) |
| 174 | { |
| 175 | int checksum = 0; |
| 176 | |
Ed Maste | a6b4c77 | 2013-08-20 14:12:58 +0000 | [diff] [blame] | 177 | for (size_t i = 0; i < payload_length; ++i) |
| 178 | checksum += payload[i]; |
| 179 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | return checksum & 255; |
| 181 | } |
| 182 | |
| 183 | size_t |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 184 | GDBRemoteCommunication::SendAck () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 185 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 186 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 187 | ConnectionStatus status = eConnectionStatusSuccess; |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 188 | char ch = '+'; |
| 189 | const size_t bytes_written = Write (&ch, 1, status, NULL); |
| 190 | if (log) |
Greg Clayton | 4598907 | 2013-10-23 18:24:30 +0000 | [diff] [blame] | 191 | log->Printf ("<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 192 | m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written); |
| 193 | return bytes_written; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | size_t |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 197 | GDBRemoteCommunication::SendNack () |
| 198 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 199 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 200 | ConnectionStatus status = eConnectionStatusSuccess; |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 201 | char ch = '-'; |
| 202 | const size_t bytes_written = Write (&ch, 1, status, NULL); |
| 203 | if (log) |
Greg Clayton | 4598907 | 2013-10-23 18:24:30 +0000 | [diff] [blame] | 204 | log->Printf("<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 205 | m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written); |
| 206 | return bytes_written; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 209 | GDBRemoteCommunication::PacketResult |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 210 | GDBRemoteCommunication::SendPacket (const char *payload, size_t payload_length) |
| 211 | { |
| 212 | Mutex::Locker locker(m_sequence_mutex); |
| 213 | return SendPacketNoLock (payload, payload_length); |
| 214 | } |
| 215 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 216 | GDBRemoteCommunication::PacketResult |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 217 | GDBRemoteCommunication::SendPacketNoLock (const char *payload, size_t payload_length) |
| 218 | { |
| 219 | if (IsConnected()) |
| 220 | { |
| 221 | StreamString packet(0, 4, eByteOrderBig); |
| 222 | |
| 223 | packet.PutChar('$'); |
| 224 | packet.Write (payload, payload_length); |
| 225 | packet.PutChar('#'); |
| 226 | packet.PutHex8(CalculcateChecksum (payload, payload_length)); |
| 227 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 228 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 229 | ConnectionStatus status = eConnectionStatusSuccess; |
Greg Clayton | 7e24432 | 2014-09-18 00:17:36 +0000 | [diff] [blame] | 230 | const char *packet_data = packet.GetData(); |
| 231 | const size_t packet_length = packet.GetSize(); |
| 232 | size_t bytes_written = Write (packet_data, packet_length, status, NULL); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 233 | if (log) |
| 234 | { |
Greg Clayton | 7e24432 | 2014-09-18 00:17:36 +0000 | [diff] [blame] | 235 | size_t binary_start_offset = 0; |
| 236 | if (strncmp(packet_data, "$vFile:pwrite:", strlen("$vFile:pwrite:")) == 0) |
| 237 | { |
| 238 | const char *first_comma = strchr(packet_data, ','); |
| 239 | if (first_comma) |
| 240 | { |
| 241 | const char *second_comma = strchr(first_comma + 1, ','); |
| 242 | if (second_comma) |
| 243 | binary_start_offset = second_comma - packet_data + 1; |
| 244 | } |
| 245 | } |
| 246 | |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 247 | // If logging was just enabled and we have history, then dump out what |
| 248 | // we have to the log so we get the historical context. The Dump() call that |
| 249 | // logs all of the packet will set a boolean so that we don't dump this more |
| 250 | // than once |
| 251 | if (!m_history.DidDumpToLog ()) |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 252 | m_history.Dump (log); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 253 | |
Greg Clayton | 7e24432 | 2014-09-18 00:17:36 +0000 | [diff] [blame] | 254 | if (binary_start_offset) |
| 255 | { |
| 256 | StreamString strm; |
| 257 | // Print non binary data header |
| 258 | strm.Printf("<%4" PRIu64 "> send packet: %.*s", (uint64_t)bytes_written, (int)binary_start_offset, packet_data); |
| 259 | const uint8_t *p; |
| 260 | // Print binary data exactly as sent |
| 261 | for (p = (uint8_t*)packet_data + binary_start_offset; *p != '#'; ++p) |
| 262 | strm.Printf("\\x%2.2x", *p); |
| 263 | // Print the checksum |
| 264 | strm.Printf("%*s", (int)3, p); |
| 265 | log->PutCString(strm.GetString().c_str()); |
| 266 | } |
| 267 | else |
| 268 | log->Printf("<%4" PRIu64 "> send packet: %.*s", (uint64_t)bytes_written, (int)packet_length, packet_data); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Greg Clayton | 7e24432 | 2014-09-18 00:17:36 +0000 | [diff] [blame] | 271 | m_history.AddPacket (packet.GetString(), packet_length, History::ePacketTypeSend, bytes_written); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 272 | |
| 273 | |
Greg Clayton | 7e24432 | 2014-09-18 00:17:36 +0000 | [diff] [blame] | 274 | if (bytes_written == packet_length) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 275 | { |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 276 | if (GetSendAcks ()) |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 277 | return GetAck (); |
| 278 | else |
| 279 | return PacketResult::Success; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 280 | } |
Johnny Chen | d0c40dd | 2010-09-14 22:10:43 +0000 | [diff] [blame] | 281 | else |
| 282 | { |
Greg Clayton | 6d09345 | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 283 | if (log) |
Greg Clayton | 7e24432 | 2014-09-18 00:17:36 +0000 | [diff] [blame] | 284 | log->Printf ("error: failed to send packet: %.*s", (int)packet_length, packet_data); |
Johnny Chen | d0c40dd | 2010-09-14 22:10:43 +0000 | [diff] [blame] | 285 | } |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 286 | } |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 287 | return PacketResult::ErrorSendFailed; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 290 | GDBRemoteCommunication::PacketResult |
Greg Clayton | c574ede | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 291 | GDBRemoteCommunication::GetAck () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 292 | { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 293 | StringExtractorGDBRemote packet; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 294 | PacketResult result = WaitForPacketWithTimeoutMicroSecondsNoLock (packet, GetPacketTimeoutInMicroSeconds ()); |
| 295 | if (result == PacketResult::Success) |
| 296 | { |
| 297 | if (packet.GetResponseType() == StringExtractorGDBRemote::ResponseType::eAck) |
| 298 | return PacketResult::Success; |
| 299 | else |
| 300 | return PacketResult::ErrorSendAck; |
| 301 | } |
| 302 | return result; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | bool |
Jim Ingham | 4ceb928 | 2012-06-08 22:50:40 +0000 | [diff] [blame] | 306 | GDBRemoteCommunication::GetSequenceMutex (Mutex::Locker& locker, const char *failure_message) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 307 | { |
Greg Clayton | d989673 | 2012-05-31 16:54:51 +0000 | [diff] [blame] | 308 | if (IsRunning()) |
Jim Ingham | 4ceb928 | 2012-06-08 22:50:40 +0000 | [diff] [blame] | 309 | return locker.TryLock (m_sequence_mutex, failure_message); |
Greg Clayton | d989673 | 2012-05-31 16:54:51 +0000 | [diff] [blame] | 310 | |
| 311 | locker.Lock (m_sequence_mutex); |
| 312 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 315 | |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 316 | bool |
Greg Clayton | 6779606a | 2011-01-22 23:43:18 +0000 | [diff] [blame] | 317 | GDBRemoteCommunication::WaitForNotRunningPrivate (const TimeValue *timeout_ptr) |
| 318 | { |
| 319 | return m_private_is_running.WaitForValueEqualTo (false, timeout_ptr, NULL); |
| 320 | } |
| 321 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 322 | GDBRemoteCommunication::PacketResult |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 323 | GDBRemoteCommunication::WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &packet, uint32_t timeout_usec) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 324 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 325 | uint8_t buffer[8192]; |
| 326 | Error error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 327 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 328 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS | GDBR_LOG_VERBOSE)); |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 329 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 330 | // Check for a packet from our cache first without trying any reading... |
| 331 | if (CheckForPacket (NULL, 0, packet)) |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 332 | return PacketResult::Success; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 333 | |
Greg Clayton | 0c51ac3 | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 334 | bool timed_out = false; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 335 | bool disconnected = false; |
Greg Clayton | 0c51ac3 | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 336 | while (IsConnected() && !timed_out) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 337 | { |
Johnny Chen | 74549c8 | 2011-07-19 01:13:00 +0000 | [diff] [blame] | 338 | lldb::ConnectionStatus status = eConnectionStatusNoConnection; |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 339 | size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error); |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 340 | |
| 341 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 342 | log->Printf ("%s: Read (buffer, (sizeof(buffer), timeout_usec = 0x%x, status = %s, error = %s) => bytes_read = %" PRIu64, |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 343 | __PRETTY_FUNCTION__, |
| 344 | timeout_usec, |
| 345 | Communication::ConnectionStatusAsCString (status), |
| 346 | error.AsCString(), |
Greg Clayton | 43e0af0 | 2012-09-18 18:04:04 +0000 | [diff] [blame] | 347 | (uint64_t)bytes_read); |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 348 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 349 | if (bytes_read > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 350 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 351 | if (CheckForPacket (buffer, bytes_read, packet)) |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 352 | return PacketResult::Success; |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 353 | } |
| 354 | else |
| 355 | { |
| 356 | switch (status) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 357 | { |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 358 | case eConnectionStatusTimedOut: |
Greg Clayton | f0066ad | 2014-05-02 00:45:31 +0000 | [diff] [blame] | 359 | case eConnectionStatusInterrupted: |
Greg Clayton | 0c51ac3 | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 360 | timed_out = true; |
| 361 | break; |
| 362 | case eConnectionStatusSuccess: |
| 363 | //printf ("status = success but error = %s\n", error.AsCString("<invalid>")); |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 364 | break; |
| 365 | |
| 366 | case eConnectionStatusEndOfFile: |
| 367 | case eConnectionStatusNoConnection: |
| 368 | case eConnectionStatusLostConnection: |
| 369 | case eConnectionStatusError: |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 370 | disconnected = true; |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 371 | Disconnect(); |
| 372 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 375 | } |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 376 | packet.Clear (); |
| 377 | if (disconnected) |
| 378 | return PacketResult::ErrorDisconnected; |
| 379 | if (timed_out) |
| 380 | return PacketResult::ErrorReplyTimeout; |
| 381 | else |
| 382 | return PacketResult::ErrorReplyFailed; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 385 | bool |
| 386 | GDBRemoteCommunication::CheckForPacket (const uint8_t *src, size_t src_len, StringExtractorGDBRemote &packet) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 387 | { |
| 388 | // Put the packet data into the buffer in a thread safe fashion |
| 389 | Mutex::Locker locker(m_bytes_mutex); |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 390 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 391 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 392 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 393 | if (src && src_len > 0) |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 394 | { |
Greg Clayton | 0c51ac3 | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 395 | if (log && log->GetVerbose()) |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 396 | { |
| 397 | StreamString s; |
Greg Clayton | 0c51ac3 | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 398 | log->Printf ("GDBRemoteCommunication::%s adding %u bytes: %.*s", |
| 399 | __FUNCTION__, |
| 400 | (uint32_t)src_len, |
| 401 | (uint32_t)src_len, |
| 402 | src); |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 403 | } |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 404 | m_bytes.append ((const char *)src, src_len); |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 405 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 406 | |
| 407 | // Parse up the packets into gdb remote packets |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 408 | if (!m_bytes.empty()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 409 | { |
| 410 | // end_idx must be one past the last valid packet byte. Start |
| 411 | // it off with an invalid value that is the same as the current |
| 412 | // index. |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 413 | size_t content_start = 0; |
| 414 | size_t content_length = 0; |
| 415 | size_t total_length = 0; |
| 416 | size_t checksum_idx = std::string::npos; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 417 | |
| 418 | switch (m_bytes[0]) |
| 419 | { |
| 420 | case '+': // Look for ack |
| 421 | case '-': // Look for cancel |
| 422 | case '\x03': // ^C to halt target |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 423 | content_length = total_length = 1; // The command is one byte long... |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 424 | break; |
| 425 | |
| 426 | case '$': |
| 427 | // Look for a standard gdb packet? |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 428 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 429 | size_t hash_pos = m_bytes.find('#'); |
| 430 | if (hash_pos != std::string::npos) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 431 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 432 | if (hash_pos + 2 < m_bytes.size()) |
| 433 | { |
| 434 | checksum_idx = hash_pos + 1; |
| 435 | // Skip the dollar sign |
| 436 | content_start = 1; |
| 437 | // Don't include the # in the content or the $ in the content length |
| 438 | content_length = hash_pos - 1; |
| 439 | |
| 440 | total_length = hash_pos + 3; // Skip the # and the two hex checksum bytes |
| 441 | } |
| 442 | else |
| 443 | { |
| 444 | // Checksum bytes aren't all here yet |
| 445 | content_length = std::string::npos; |
| 446 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | break; |
| 450 | |
| 451 | default: |
Greg Clayton | f3dd93c | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 452 | { |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 453 | // We have an unexpected byte and we need to flush all bad |
| 454 | // data that is in m_bytes, so we need to find the first |
| 455 | // byte that is a '+' (ACK), '-' (NACK), \x03 (CTRL+C interrupt), |
| 456 | // or '$' character (start of packet header) or of course, |
| 457 | // the end of the data in m_bytes... |
| 458 | const size_t bytes_len = m_bytes.size(); |
| 459 | bool done = false; |
| 460 | uint32_t idx; |
| 461 | for (idx = 1; !done && idx < bytes_len; ++idx) |
| 462 | { |
| 463 | switch (m_bytes[idx]) |
| 464 | { |
| 465 | case '+': |
| 466 | case '-': |
| 467 | case '\x03': |
| 468 | case '$': |
| 469 | done = true; |
| 470 | break; |
| 471 | |
| 472 | default: |
| 473 | break; |
| 474 | } |
| 475 | } |
Greg Clayton | f3dd93c | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 476 | if (log) |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 477 | log->Printf ("GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'", |
| 478 | __FUNCTION__, idx, idx, m_bytes.c_str()); |
| 479 | m_bytes.erase(0, idx); |
Greg Clayton | f3dd93c | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 480 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 481 | break; |
| 482 | } |
| 483 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 484 | if (content_length == std::string::npos) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 485 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 486 | packet.Clear(); |
| 487 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 488 | } |
Greg Clayton | f3dd93c | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 489 | else if (total_length > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 490 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 491 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 492 | // We have a valid packet... |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 493 | assert (content_length <= m_bytes.size()); |
| 494 | assert (total_length <= m_bytes.size()); |
| 495 | assert (content_length <= total_length); |
Greg Clayton | 06f09b5 | 2014-06-20 20:41:07 +0000 | [diff] [blame] | 496 | const size_t content_end = content_start + content_length; |
| 497 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 498 | bool success = true; |
| 499 | std::string &packet_str = packet.GetStringRef(); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 500 | |
| 501 | |
| 502 | if (log) |
| 503 | { |
| 504 | // If logging was just enabled and we have history, then dump out what |
| 505 | // we have to the log so we get the historical context. The Dump() call that |
| 506 | // logs all of the packet will set a boolean so that we don't dump this more |
| 507 | // than once |
| 508 | if (!m_history.DidDumpToLog ()) |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 509 | m_history.Dump (log); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 510 | |
Greg Clayton | 06f09b5 | 2014-06-20 20:41:07 +0000 | [diff] [blame] | 511 | bool binary = false; |
| 512 | // Only detect binary for packets that start with a '$' and have a '#CC' checksum |
| 513 | if (m_bytes[0] == '$' && total_length > 4) |
| 514 | { |
| 515 | for (size_t i=0; !binary && i<total_length; ++i) |
| 516 | { |
| 517 | if (isprint(m_bytes[i]) == 0) |
| 518 | binary = true; |
| 519 | } |
| 520 | } |
| 521 | if (binary) |
| 522 | { |
| 523 | StreamString strm; |
| 524 | // Packet header... |
| 525 | strm.Printf("<%4" PRIu64 "> read packet: %c", (uint64_t)total_length, m_bytes[0]); |
| 526 | for (size_t i=content_start; i<content_end; ++i) |
| 527 | { |
| 528 | // Remove binary escaped bytes when displaying the packet... |
| 529 | const char ch = m_bytes[i]; |
| 530 | if (ch == 0x7d) |
| 531 | { |
| 532 | // 0x7d is the escape character. The next character is to |
| 533 | // be XOR'd with 0x20. |
| 534 | const char escapee = m_bytes[++i] ^ 0x20; |
| 535 | strm.Printf("%2.2x", escapee); |
| 536 | } |
| 537 | else |
| 538 | { |
| 539 | strm.Printf("%2.2x", (uint8_t)ch); |
| 540 | } |
| 541 | } |
| 542 | // Packet footer... |
| 543 | strm.Printf("%c%c%c", m_bytes[total_length-3], m_bytes[total_length-2], m_bytes[total_length-1]); |
| 544 | log->PutCString(strm.GetString().c_str()); |
| 545 | } |
| 546 | else |
| 547 | { |
| 548 | log->Printf("<%4" PRIu64 "> read packet: %.*s", (uint64_t)total_length, (int)(total_length), m_bytes.c_str()); |
| 549 | } |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | m_history.AddPacket (m_bytes.c_str(), total_length, History::ePacketTypeRecv, total_length); |
| 553 | |
Hafiz Abid Qadeer | e5fd5e1 | 2013-08-28 15:10:37 +0000 | [diff] [blame] | 554 | // Clear packet_str in case there is some existing data in it. |
| 555 | packet_str.clear(); |
Hafiz Abid Qadeer | da96ef2 | 2013-08-28 10:31:52 +0000 | [diff] [blame] | 556 | // Copy the packet from m_bytes to packet_str expanding the |
| 557 | // run-length encoding in the process. |
| 558 | // Reserve enough byte for the most common case (no RLE used) |
| 559 | packet_str.reserve(m_bytes.length()); |
Greg Clayton | 06f09b5 | 2014-06-20 20:41:07 +0000 | [diff] [blame] | 560 | for (std::string::const_iterator c = m_bytes.begin() + content_start; c != m_bytes.begin() + content_end; ++c) |
Hafiz Abid Qadeer | da96ef2 | 2013-08-28 10:31:52 +0000 | [diff] [blame] | 561 | { |
| 562 | if (*c == '*') |
| 563 | { |
| 564 | // '*' indicates RLE. Next character will give us the |
| 565 | // repeat count and previous character is what is to be |
| 566 | // repeated. |
| 567 | char char_to_repeat = packet_str.back(); |
| 568 | // Number of time the previous character is repeated |
| 569 | int repeat_count = *++c + 3 - ' '; |
| 570 | // We have the char_to_repeat and repeat_count. Now push |
| 571 | // it in the packet. |
| 572 | for (int i = 0; i < repeat_count; ++i) |
| 573 | packet_str.push_back(char_to_repeat); |
| 574 | } |
Steve Pucci | 3c5d333 | 2014-02-24 19:07:29 +0000 | [diff] [blame] | 575 | else if (*c == 0x7d) |
| 576 | { |
| 577 | // 0x7d is the escape character. The next character is to |
| 578 | // be XOR'd with 0x20. |
| 579 | char escapee = *++c ^ 0x20; |
| 580 | packet_str.push_back(escapee); |
| 581 | } |
Hafiz Abid Qadeer | da96ef2 | 2013-08-28 10:31:52 +0000 | [diff] [blame] | 582 | else |
| 583 | { |
| 584 | packet_str.push_back(*c); |
| 585 | } |
| 586 | } |
| 587 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 588 | if (m_bytes[0] == '$') |
| 589 | { |
| 590 | assert (checksum_idx < m_bytes.size()); |
| 591 | if (::isxdigit (m_bytes[checksum_idx+0]) || |
| 592 | ::isxdigit (m_bytes[checksum_idx+1])) |
| 593 | { |
| 594 | if (GetSendAcks ()) |
| 595 | { |
| 596 | const char *packet_checksum_cstr = &m_bytes[checksum_idx]; |
| 597 | char packet_checksum = strtol (packet_checksum_cstr, NULL, 16); |
| 598 | char actual_checksum = CalculcateChecksum (packet_str.c_str(), packet_str.size()); |
| 599 | success = packet_checksum == actual_checksum; |
| 600 | if (!success) |
| 601 | { |
| 602 | if (log) |
| 603 | log->Printf ("error: checksum mismatch: %.*s expected 0x%2.2x, got 0x%2.2x", |
| 604 | (int)(total_length), |
| 605 | m_bytes.c_str(), |
| 606 | (uint8_t)packet_checksum, |
| 607 | (uint8_t)actual_checksum); |
| 608 | } |
| 609 | // Send the ack or nack if needed |
| 610 | if (!success) |
| 611 | SendNack(); |
| 612 | else |
| 613 | SendAck(); |
| 614 | } |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 615 | } |
| 616 | else |
| 617 | { |
| 618 | success = false; |
| 619 | if (log) |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 620 | log->Printf ("error: invalid checksum in packet: '%s'\n", m_bytes.c_str()); |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 621 | } |
| 622 | } |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 623 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 624 | m_bytes.erase(0, total_length); |
| 625 | packet.SetFilePos(0); |
| 626 | return success; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 627 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 628 | } |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 629 | packet.Clear(); |
| 630 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 633 | Error |
Greg Clayton | d629980 | 2013-12-06 17:46:35 +0000 | [diff] [blame] | 634 | GDBRemoteCommunication::StartListenThread (const char *hostname, uint16_t port) |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 635 | { |
| 636 | Error error; |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 637 | if (m_listen_thread.IsJoinable()) |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 638 | { |
| 639 | error.SetErrorString("listen thread already running"); |
| 640 | } |
| 641 | else |
| 642 | { |
| 643 | char listen_url[512]; |
| 644 | if (hostname && hostname[0]) |
Jean-Daniel Dupas | 3c6774a | 2014-02-08 20:29:40 +0000 | [diff] [blame] | 645 | snprintf(listen_url, sizeof(listen_url), "listen://%s:%i", hostname, port); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 646 | else |
| 647 | snprintf(listen_url, sizeof(listen_url), "listen://%i", port); |
| 648 | m_listen_url = listen_url; |
| 649 | SetConnection(new ConnectionFileDescriptor()); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 650 | m_listen_thread = ThreadLauncher::LaunchThread(listen_url, GDBRemoteCommunication::ListenThread, this, &error); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 651 | } |
| 652 | return error; |
| 653 | } |
| 654 | |
| 655 | bool |
| 656 | GDBRemoteCommunication::JoinListenThread () |
| 657 | { |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 658 | if (m_listen_thread.IsJoinable()) |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 659 | m_listen_thread.Join(nullptr); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 660 | return true; |
| 661 | } |
| 662 | |
| 663 | lldb::thread_result_t |
| 664 | GDBRemoteCommunication::ListenThread (lldb::thread_arg_t arg) |
| 665 | { |
| 666 | GDBRemoteCommunication *comm = (GDBRemoteCommunication *)arg; |
| 667 | Error error; |
| 668 | ConnectionFileDescriptor *connection = (ConnectionFileDescriptor *)comm->GetConnection (); |
| 669 | |
| 670 | if (connection) |
| 671 | { |
| 672 | // Do the listen on another thread so we can continue on... |
| 673 | if (connection->Connect(comm->m_listen_url.c_str(), &error) != eConnectionStatusSuccess) |
| 674 | comm->SetConnection(NULL); |
| 675 | } |
| 676 | return NULL; |
| 677 | } |
| 678 | |
| 679 | Error |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 680 | GDBRemoteCommunication::StartDebugserverProcess (const char *hostname, |
| 681 | uint16_t in_port, |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 682 | lldb_private::ProcessLaunchInfo &launch_info, |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 683 | uint16_t &out_port) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 684 | { |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 685 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
| 686 | if (log) |
| 687 | log->Printf ("GDBRemoteCommunication::%s(hostname=%s, in_port=%" PRIu16 ", out_port=%" PRIu16, __FUNCTION__, hostname ? hostname : "<empty>", in_port, out_port); |
| 688 | |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 689 | out_port = in_port; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 690 | Error error; |
| 691 | // If we locate debugserver, keep that located version around |
| 692 | static FileSpec g_debugserver_file_spec; |
| 693 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 694 | char debugserver_path[PATH_MAX]; |
| 695 | FileSpec &debugserver_file_spec = launch_info.GetExecutableFile(); |
| 696 | |
| 697 | // Always check to see if we have an environment override for the path |
| 698 | // to the debugserver to use and use it if we do. |
| 699 | const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH"); |
| 700 | if (env_debugserver_path) |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 701 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 702 | debugserver_file_spec.SetFile (env_debugserver_path, false); |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 703 | if (log) |
| 704 | log->Printf ("GDBRemoteCommunication::%s() gdb-remote stub exe path set from environment variable: %s", __FUNCTION__, env_debugserver_path); |
| 705 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 706 | else |
| 707 | debugserver_file_spec = g_debugserver_file_spec; |
| 708 | bool debugserver_exists = debugserver_file_spec.Exists(); |
| 709 | if (!debugserver_exists) |
| 710 | { |
| 711 | // The debugserver binary is in the LLDB.framework/Resources |
Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 712 | // directory. |
| 713 | if (HostInfo::GetLLDBPath(ePathTypeSupportExecutableDir, debugserver_file_spec)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 714 | { |
Jason Molenda | 6fd8677 | 2014-08-21 23:22:33 +0000 | [diff] [blame] | 715 | debugserver_file_spec.AppendPathComponent (DEBUGSERVER_BASENAME); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 716 | debugserver_exists = debugserver_file_spec.Exists(); |
| 717 | if (debugserver_exists) |
| 718 | { |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 719 | if (log) |
| 720 | log->Printf ("GDBRemoteCommunication::%s() found gdb-remote stub exe '%s'", __FUNCTION__, debugserver_file_spec.GetPath ().c_str ()); |
| 721 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 722 | g_debugserver_file_spec = debugserver_file_spec; |
| 723 | } |
| 724 | else |
| 725 | { |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 726 | if (log) |
| 727 | log->Printf ("GDBRemoteCommunication::%s() could not find gdb-remote stub exe '%s'", __FUNCTION__, debugserver_file_spec.GetPath ().c_str ()); |
| 728 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 729 | g_debugserver_file_spec.Clear(); |
| 730 | debugserver_file_spec.Clear(); |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | if (debugserver_exists) |
| 736 | { |
| 737 | debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path)); |
| 738 | |
| 739 | Args &debugserver_args = launch_info.GetArguments(); |
| 740 | debugserver_args.Clear(); |
| 741 | char arg_cstr[PATH_MAX]; |
| 742 | |
| 743 | // Start args with "debugserver /file/path -r --" |
| 744 | debugserver_args.AppendArgument(debugserver_path); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 745 | |
| 746 | // If a host and port is supplied then use it |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 747 | char host_and_port[128]; |
| 748 | if (hostname) |
| 749 | { |
| 750 | snprintf (host_and_port, sizeof(host_and_port), "%s:%u", hostname, in_port); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 751 | debugserver_args.AppendArgument(host_and_port); |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 752 | } |
| 753 | else |
| 754 | { |
| 755 | host_and_port[0] = '\0'; |
| 756 | } |
| 757 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 758 | // use native registers, not the GDB registers |
| 759 | debugserver_args.AppendArgument("--native-regs"); |
| 760 | // make debugserver run in its own session so signals generated by |
| 761 | // special terminal key sequences (^C) don't affect debugserver |
| 762 | debugserver_args.AppendArgument("--setsid"); |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 763 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 764 | char named_pipe_path[PATH_MAX]; |
Jason Molenda | 6e20554 | 2014-01-25 03:57:13 +0000 | [diff] [blame] | 765 | named_pipe_path[0] = '\0'; |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 766 | |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 767 | bool listen = false; |
| 768 | if (host_and_port[0]) |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 769 | { |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 770 | // Create a temporary file to get the stdout/stderr and redirect the |
| 771 | // output of the command into this file. We will later read this file |
| 772 | // if all goes well and fill the data into "command_output_ptr" |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 773 | |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 774 | if (in_port == 0) |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 775 | { |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 776 | // Binding to port zero, we need to figure out what port it ends up |
| 777 | // using using a named pipe... |
| 778 | FileSpec tmpdir_file_spec; |
Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 779 | if (HostInfo::GetLLDBPath(ePathTypeLLDBTempSystemDir, tmpdir_file_spec)) |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 780 | { |
Zachary Turner | c25146b | 2014-08-21 23:56:55 +0000 | [diff] [blame] | 781 | tmpdir_file_spec.AppendPathComponent("debugserver-named-pipe.XXXXXX"); |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 782 | strncpy(named_pipe_path, tmpdir_file_spec.GetPath().c_str(), sizeof(named_pipe_path)); |
| 783 | } |
| 784 | else |
| 785 | { |
| 786 | strncpy(named_pipe_path, "/tmp/debugserver-named-pipe.XXXXXX", sizeof(named_pipe_path)); |
| 787 | } |
| 788 | |
| 789 | if (::mktemp (named_pipe_path)) |
| 790 | { |
Hafiz Abid Qadeer | 6eff101 | 2014-03-12 10:45:23 +0000 | [diff] [blame] | 791 | #if defined(_WIN32) |
Deepak Panickal | b36da43 | 2014-01-13 14:55:15 +0000 | [diff] [blame] | 792 | if ( false ) |
| 793 | #else |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 794 | if (::mkfifo(named_pipe_path, 0600) == 0) |
Deepak Panickal | b36da43 | 2014-01-13 14:55:15 +0000 | [diff] [blame] | 795 | #endif |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 796 | { |
| 797 | debugserver_args.AppendArgument("--named-pipe"); |
| 798 | debugserver_args.AppendArgument(named_pipe_path); |
| 799 | } |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 800 | } |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 801 | } |
| 802 | else |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 803 | { |
| 804 | listen = true; |
| 805 | } |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 806 | } |
| 807 | else |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 808 | { |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 809 | // No host and port given, so lets listen on our end and make the debugserver |
| 810 | // connect to us.. |
Greg Clayton | 1681092 | 2014-02-27 19:38:18 +0000 | [diff] [blame] | 811 | error = StartListenThread ("127.0.0.1", 0); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 812 | if (error.Fail()) |
| 813 | return error; |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 814 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 815 | ConnectionFileDescriptor *connection = (ConnectionFileDescriptor *)GetConnection (); |
Greg Clayton | 1681092 | 2014-02-27 19:38:18 +0000 | [diff] [blame] | 816 | // Wait for 10 seconds to resolve the bound port |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 817 | out_port = connection->GetListeningPort(10); |
Greg Clayton | 1681092 | 2014-02-27 19:38:18 +0000 | [diff] [blame] | 818 | if (out_port > 0) |
| 819 | { |
| 820 | char port_cstr[32]; |
| 821 | snprintf(port_cstr, sizeof(port_cstr), "127.0.0.1:%i", out_port); |
| 822 | // Send the host and port down that debugserver and specify an option |
| 823 | // so that it connects back to the port we are listening to in this process |
| 824 | debugserver_args.AppendArgument("--reverse-connect"); |
| 825 | debugserver_args.AppendArgument(port_cstr); |
| 826 | } |
| 827 | else |
| 828 | { |
| 829 | error.SetErrorString ("failed to bind to port 0 on 127.0.0.1"); |
| 830 | return error; |
| 831 | } |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 832 | } |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 833 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 834 | const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE"); |
| 835 | if (env_debugserver_log_file) |
| 836 | { |
| 837 | ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file); |
| 838 | debugserver_args.AppendArgument(arg_cstr); |
| 839 | } |
| 840 | |
| 841 | const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS"); |
| 842 | if (env_debugserver_log_flags) |
| 843 | { |
| 844 | ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags); |
| 845 | debugserver_args.AppendArgument(arg_cstr); |
| 846 | } |
Todd Fiala | 34ba426 | 2014-08-29 17:10:31 +0000 | [diff] [blame] | 847 | |
| 848 | // Add additional args, starting with LLDB_DEBUGSERVER_EXTRA_ARG_1 until an env var doesn't come back. |
| 849 | uint32_t env_var_index = 1; |
| 850 | bool has_env_var; |
| 851 | do |
| 852 | { |
| 853 | char env_var_name[64]; |
| 854 | snprintf (env_var_name, sizeof (env_var_name), "LLDB_DEBUGSERVER_EXTRA_ARG_%" PRIu32, env_var_index++); |
| 855 | const char *extra_arg = getenv(env_var_name); |
| 856 | has_env_var = extra_arg != nullptr; |
| 857 | |
| 858 | if (has_env_var) |
| 859 | { |
| 860 | debugserver_args.AppendArgument (extra_arg); |
| 861 | if (log) |
| 862 | log->Printf ("GDBRemoteCommunication::%s adding env var %s contents to stub command line (%s)", __FUNCTION__, env_var_name, extra_arg); |
| 863 | } |
| 864 | } while (has_env_var); |
| 865 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 866 | // Close STDIN, STDOUT and STDERR. We might need to redirect them |
| 867 | // to "/dev/null" if we run into any problems. |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 868 | launch_info.AppendCloseFileAction (STDIN_FILENO); |
| 869 | launch_info.AppendCloseFileAction (STDOUT_FILENO); |
| 870 | launch_info.AppendCloseFileAction (STDERR_FILENO); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 871 | |
| 872 | error = Host::LaunchProcess(launch_info); |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 873 | |
Greg Clayton | 3121fde | 2014-02-28 20:47:08 +0000 | [diff] [blame] | 874 | if (error.Success() && launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 875 | { |
Greg Clayton | 3121fde | 2014-02-28 20:47:08 +0000 | [diff] [blame] | 876 | if (named_pipe_path[0]) |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 877 | { |
Greg Clayton | 3121fde | 2014-02-28 20:47:08 +0000 | [diff] [blame] | 878 | File name_pipe_file; |
| 879 | error = name_pipe_file.Open(named_pipe_path, File::eOpenOptionRead); |
| 880 | if (error.Success()) |
| 881 | { |
| 882 | char port_cstr[256]; |
| 883 | port_cstr[0] = '\0'; |
| 884 | size_t num_bytes = sizeof(port_cstr); |
| 885 | error = name_pipe_file.Read(port_cstr, num_bytes); |
| 886 | assert (error.Success()); |
| 887 | assert (num_bytes > 0 && port_cstr[num_bytes-1] == '\0'); |
| 888 | out_port = Args::StringToUInt32(port_cstr, 0); |
| 889 | name_pipe_file.Close(); |
| 890 | } |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 891 | FileSystem::Unlink(named_pipe_path); |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 892 | } |
Greg Clayton | 3121fde | 2014-02-28 20:47:08 +0000 | [diff] [blame] | 893 | else if (listen) |
| 894 | { |
| 895 | |
| 896 | } |
| 897 | else |
| 898 | { |
| 899 | // Make sure we actually connect with the debugserver... |
| 900 | JoinListenThread(); |
| 901 | } |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 902 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 903 | } |
| 904 | else |
| 905 | { |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 906 | error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME ); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 907 | } |
| 908 | return error; |
| 909 | } |
| 910 | |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 911 | void |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 912 | GDBRemoteCommunication::DumpHistory(Stream &strm) |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 913 | { |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 914 | m_history.Dump (strm); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 915 | } |