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 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | #include "lldb/Core/Log.h" |
Greg Clayton | b30c50c | 2015-05-29 00:01:55 +0000 | [diff] [blame] | 21 | #include "lldb/Core/RegularExpression.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" |
Zachary Turner | 93a66fc | 2014-10-06 21:22:36 +0000 | [diff] [blame] | 24 | #include "lldb/Host/ConnectionFileDescriptor.h" |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 25 | #include "lldb/Host/FileSpec.h" |
| 26 | #include "lldb/Host/Host.h" |
Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 27 | #include "lldb/Host/HostInfo.h" |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 28 | #include "lldb/Host/Pipe.h" |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 29 | #include "lldb/Host/Socket.h" |
Vince Harron | 5275aaa | 2015-01-15 20:08:35 +0000 | [diff] [blame] | 30 | #include "lldb/Host/StringConvert.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 31 | #include "lldb/Host/ThreadLauncher.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | #include "lldb/Host/TimeValue.h" |
Greg Clayton | 6988abc | 2015-10-19 20:44:01 +0000 | [diff] [blame] | 33 | #include "lldb/Target/Platform.h" |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 34 | #include "lldb/Target/Process.h" |
Oleksiy Vyalov | 4536c45 | 2015-02-05 16:29:12 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallString.h" |
Pavel Labath | 1eb0d42 | 2016-08-08 12:54:36 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ScopedPrinter.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | |
| 38 | // Project includes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 39 | #include "ProcessGDBRemoteLog.h" |
| 40 | |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 41 | #if defined(__APPLE__) |
| 42 | # define DEBUGSERVER_BASENAME "debugserver" |
| 43 | #else |
Tamas Berghammer | c2c3d71 | 2015-02-18 15:39:41 +0000 | [diff] [blame] | 44 | # define DEBUGSERVER_BASENAME "lldb-server" |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 45 | #endif |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 46 | |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 47 | #if defined (HAVE_LIBCOMPRESSION) |
| 48 | #include <compression.h> |
| 49 | #endif |
| 50 | |
| 51 | #if defined (HAVE_LIBZ) |
| 52 | #include <zlib.h> |
| 53 | #endif |
| 54 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 55 | using namespace lldb; |
| 56 | using namespace lldb_private; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 57 | using namespace lldb_private::process_gdb_remote; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 58 | |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 59 | GDBRemoteCommunication::History::History (uint32_t size) : |
| 60 | m_packets(), |
| 61 | m_curr_idx (0), |
| 62 | m_total_packet_count (0), |
| 63 | m_dumped_to_log (false) |
| 64 | { |
| 65 | m_packets.resize(size); |
| 66 | } |
| 67 | |
| 68 | GDBRemoteCommunication::History::~History () |
| 69 | { |
| 70 | } |
| 71 | |
| 72 | void |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 73 | GDBRemoteCommunication::History::AddPacket (char packet_char, |
| 74 | PacketType type, |
| 75 | uint32_t bytes_transmitted) |
| 76 | { |
| 77 | const size_t size = m_packets.size(); |
| 78 | if (size > 0) |
| 79 | { |
| 80 | const uint32_t idx = GetNextIndex(); |
| 81 | m_packets[idx].packet.assign (1, packet_char); |
| 82 | m_packets[idx].type = type; |
| 83 | m_packets[idx].bytes_transmitted = bytes_transmitted; |
| 84 | m_packets[idx].packet_idx = m_total_packet_count; |
| 85 | m_packets[idx].tid = Host::GetCurrentThreadID(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | GDBRemoteCommunication::History::AddPacket (const std::string &src, |
| 91 | uint32_t src_len, |
| 92 | PacketType type, |
| 93 | uint32_t bytes_transmitted) |
| 94 | { |
| 95 | const size_t size = m_packets.size(); |
| 96 | if (size > 0) |
| 97 | { |
| 98 | const uint32_t idx = GetNextIndex(); |
| 99 | m_packets[idx].packet.assign (src, 0, src_len); |
| 100 | m_packets[idx].type = type; |
| 101 | m_packets[idx].bytes_transmitted = bytes_transmitted; |
| 102 | m_packets[idx].packet_idx = m_total_packet_count; |
| 103 | m_packets[idx].tid = Host::GetCurrentThreadID(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 108 | GDBRemoteCommunication::History::Dump (Stream &strm) const |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 109 | { |
| 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 | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 119 | 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] | 120 | entry.packet_idx, |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 121 | entry.tid, |
Greg Clayton | c1422c1 | 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 | void |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 129 | GDBRemoteCommunication::History::Dump (Log *log) const |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 130 | { |
| 131 | if (log && !m_dumped_to_log) |
| 132 | { |
| 133 | m_dumped_to_log = true; |
| 134 | const uint32_t size = GetNumPacketsInHistory (); |
| 135 | const uint32_t first_idx = GetFirstSavedPacketIndex (); |
| 136 | const uint32_t stop_idx = m_curr_idx + size; |
| 137 | for (uint32_t i = first_idx; i < stop_idx; ++i) |
| 138 | { |
| 139 | const uint32_t idx = NormalizeIndex (i); |
| 140 | const Entry &entry = m_packets[idx]; |
| 141 | if (entry.type == ePacketTypeInvalid || entry.packet.empty()) |
| 142 | break; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 143 | 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] | 144 | entry.packet_idx, |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 145 | entry.tid, |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 146 | entry.bytes_transmitted, |
| 147 | (entry.type == ePacketTypeSend) ? "send" : "read", |
| 148 | entry.packet.c_str()); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 153 | //---------------------------------------------------------------------- |
| 154 | // GDBRemoteCommunication constructor |
| 155 | //---------------------------------------------------------------------- |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 156 | GDBRemoteCommunication::GDBRemoteCommunication(const char *comm_name, const char *listener_name) |
| 157 | : Communication(comm_name), |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 158 | #ifdef LLDB_CONFIGURATION_DEBUG |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 159 | m_packet_timeout(1000), |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 160 | #else |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 161 | m_packet_timeout(1), |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 162 | #endif |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 163 | m_echo_number(0), |
| 164 | m_supports_qEcho(eLazyBoolCalculate), |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 165 | m_history(512), |
| 166 | m_send_acks(true), |
| 167 | m_compression_type(CompressionType::None), |
| 168 | m_listen_url() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 169 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | //---------------------------------------------------------------------- |
| 173 | // Destructor |
| 174 | //---------------------------------------------------------------------- |
| 175 | GDBRemoteCommunication::~GDBRemoteCommunication() |
| 176 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 177 | if (IsConnected()) |
| 178 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 179 | Disconnect(); |
| 180 | } |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 181 | |
| 182 | // Stop the communications read thread which is used to parse all |
| 183 | // incoming packets. This function will block until the read |
| 184 | // thread returns. |
| 185 | if (m_read_thread_enabled) |
| 186 | StopReadThread(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 189 | char |
| 190 | GDBRemoteCommunication::CalculcateChecksum (const char *payload, size_t payload_length) |
| 191 | { |
| 192 | int checksum = 0; |
| 193 | |
Ed Maste | a6b4c77 | 2013-08-20 14:12:58 +0000 | [diff] [blame] | 194 | for (size_t i = 0; i < payload_length; ++i) |
| 195 | checksum += payload[i]; |
| 196 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 197 | return checksum & 255; |
| 198 | } |
| 199 | |
| 200 | size_t |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 201 | GDBRemoteCommunication::SendAck () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 202 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 203 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 204 | ConnectionStatus status = eConnectionStatusSuccess; |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 205 | char ch = '+'; |
| 206 | const size_t bytes_written = Write (&ch, 1, status, NULL); |
| 207 | if (log) |
Greg Clayton | 4598907 | 2013-10-23 18:24:30 +0000 | [diff] [blame] | 208 | log->Printf ("<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 209 | m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written); |
| 210 | return bytes_written; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | size_t |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 214 | GDBRemoteCommunication::SendNack () |
| 215 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 216 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
Greg Clayton | 6ed9594 | 2011-01-22 07:12:45 +0000 | [diff] [blame] | 217 | ConnectionStatus status = eConnectionStatusSuccess; |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 218 | char ch = '-'; |
| 219 | const size_t bytes_written = Write (&ch, 1, status, NULL); |
| 220 | if (log) |
Greg Clayton | 4598907 | 2013-10-23 18:24:30 +0000 | [diff] [blame] | 221 | log->Printf("<%4" PRIu64 "> send packet: %c", (uint64_t)bytes_written, ch); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 222 | m_history.AddPacket (ch, History::ePacketTypeSend, bytes_written); |
| 223 | return bytes_written; |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 226 | GDBRemoteCommunication::PacketResult |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 227 | GDBRemoteCommunication::SendPacketNoLock (const char *payload, size_t payload_length) |
| 228 | { |
| 229 | if (IsConnected()) |
| 230 | { |
| 231 | StreamString packet(0, 4, eByteOrderBig); |
| 232 | |
| 233 | packet.PutChar('$'); |
| 234 | packet.Write (payload, payload_length); |
| 235 | packet.PutChar('#'); |
| 236 | packet.PutHex8(CalculcateChecksum (payload, payload_length)); |
| 237 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 238 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 239 | ConnectionStatus status = eConnectionStatusSuccess; |
Greg Clayton | 7e24432 | 2014-09-18 00:17:36 +0000 | [diff] [blame] | 240 | const char *packet_data = packet.GetData(); |
| 241 | const size_t packet_length = packet.GetSize(); |
| 242 | size_t bytes_written = Write (packet_data, packet_length, status, NULL); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 243 | if (log) |
| 244 | { |
Greg Clayton | 7e24432 | 2014-09-18 00:17:36 +0000 | [diff] [blame] | 245 | size_t binary_start_offset = 0; |
| 246 | if (strncmp(packet_data, "$vFile:pwrite:", strlen("$vFile:pwrite:")) == 0) |
| 247 | { |
| 248 | const char *first_comma = strchr(packet_data, ','); |
| 249 | if (first_comma) |
| 250 | { |
| 251 | const char *second_comma = strchr(first_comma + 1, ','); |
| 252 | if (second_comma) |
| 253 | binary_start_offset = second_comma - packet_data + 1; |
| 254 | } |
| 255 | } |
| 256 | |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 257 | // If logging was just enabled and we have history, then dump out what |
| 258 | // we have to the log so we get the historical context. The Dump() call that |
| 259 | // logs all of the packet will set a boolean so that we don't dump this more |
| 260 | // than once |
| 261 | if (!m_history.DidDumpToLog ()) |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 262 | m_history.Dump (log); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 263 | |
Greg Clayton | 7e24432 | 2014-09-18 00:17:36 +0000 | [diff] [blame] | 264 | if (binary_start_offset) |
| 265 | { |
| 266 | StreamString strm; |
| 267 | // Print non binary data header |
| 268 | strm.Printf("<%4" PRIu64 "> send packet: %.*s", (uint64_t)bytes_written, (int)binary_start_offset, packet_data); |
| 269 | const uint8_t *p; |
| 270 | // Print binary data exactly as sent |
Vince Harron | 8b33567 | 2015-05-12 01:10:56 +0000 | [diff] [blame] | 271 | for (p = (const uint8_t*)packet_data + binary_start_offset; *p != '#'; ++p) |
Greg Clayton | 7e24432 | 2014-09-18 00:17:36 +0000 | [diff] [blame] | 272 | strm.Printf("\\x%2.2x", *p); |
| 273 | // Print the checksum |
| 274 | strm.Printf("%*s", (int)3, p); |
| 275 | log->PutCString(strm.GetString().c_str()); |
| 276 | } |
| 277 | else |
| 278 | 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] | 279 | } |
| 280 | |
Greg Clayton | 7e24432 | 2014-09-18 00:17:36 +0000 | [diff] [blame] | 281 | m_history.AddPacket (packet.GetString(), packet_length, History::ePacketTypeSend, bytes_written); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 282 | |
| 283 | |
Greg Clayton | 7e24432 | 2014-09-18 00:17:36 +0000 | [diff] [blame] | 284 | if (bytes_written == packet_length) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 285 | { |
Greg Clayton | 71fc2a3 | 2011-02-12 06:28:37 +0000 | [diff] [blame] | 286 | if (GetSendAcks ()) |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 287 | return GetAck (); |
| 288 | else |
| 289 | return PacketResult::Success; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 290 | } |
Johnny Chen | d0c40dd | 2010-09-14 22:10:43 +0000 | [diff] [blame] | 291 | else |
| 292 | { |
Greg Clayton | 6d09345 | 2011-02-05 02:25:06 +0000 | [diff] [blame] | 293 | if (log) |
Greg Clayton | 7e24432 | 2014-09-18 00:17:36 +0000 | [diff] [blame] | 294 | 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] | 295 | } |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 296 | } |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 297 | return PacketResult::ErrorSendFailed; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 300 | GDBRemoteCommunication::PacketResult |
Greg Clayton | c574ede | 2011-03-10 02:26:48 +0000 | [diff] [blame] | 301 | GDBRemoteCommunication::GetAck () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 302 | { |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 303 | StringExtractorGDBRemote packet; |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 304 | PacketResult result = ReadPacket (packet, GetPacketTimeoutInMicroSeconds (), false); |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 305 | if (result == PacketResult::Success) |
| 306 | { |
| 307 | if (packet.GetResponseType() == StringExtractorGDBRemote::ResponseType::eAck) |
| 308 | return PacketResult::Success; |
| 309 | else |
| 310 | return PacketResult::ErrorSendAck; |
| 311 | } |
| 312 | return result; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 315 | GDBRemoteCommunication::PacketResult |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 316 | GDBRemoteCommunication::ReadPacket (StringExtractorGDBRemote &response, uint32_t timeout_usec, bool sync_on_timeout) |
| 317 | { |
| 318 | if (m_read_thread_enabled) |
| 319 | return PopPacketFromQueue (response, timeout_usec); |
| 320 | else |
| 321 | return WaitForPacketWithTimeoutMicroSecondsNoLock (response, timeout_usec, sync_on_timeout); |
| 322 | } |
| 323 | |
| 324 | |
| 325 | // This function is called when a packet is requested. |
| 326 | // A whole packet is popped from the packet queue and returned to the caller. |
| 327 | // Packets are placed into this queue from the communication read thread. |
| 328 | // See GDBRemoteCommunication::AppendBytesToCache. |
| 329 | GDBRemoteCommunication::PacketResult |
| 330 | GDBRemoteCommunication::PopPacketFromQueue (StringExtractorGDBRemote &response, uint32_t timeout_usec) |
| 331 | { |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 332 | auto until = std::chrono::system_clock::now() + std::chrono::microseconds(timeout_usec); |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 333 | |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 334 | while (true) |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 335 | { |
| 336 | // scope for the mutex |
| 337 | { |
| 338 | // lock down the packet queue |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 339 | std::unique_lock<std::mutex> lock(m_packet_queue_mutex); |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 340 | |
| 341 | // Wait on condition variable. |
| 342 | if (m_packet_queue.size() == 0) |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 343 | { |
| 344 | std::cv_status result = m_condition_queue_not_empty.wait_until(lock, until); |
| 345 | if (result == std::cv_status::timeout) |
| 346 | break; |
| 347 | } |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 348 | |
| 349 | if (m_packet_queue.size() > 0) |
| 350 | { |
| 351 | // get the front element of the queue |
| 352 | response = m_packet_queue.front(); |
| 353 | |
| 354 | // remove the front element |
| 355 | m_packet_queue.pop(); |
| 356 | |
| 357 | // we got a packet |
| 358 | return PacketResult::Success; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | // Disconnected |
| 363 | if (!IsConnected()) |
| 364 | return PacketResult::ErrorDisconnected; |
| 365 | |
| 366 | // Loop while not timed out |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 367 | } |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 368 | |
| 369 | return PacketResult::ErrorReplyTimeout; |
| 370 | } |
| 371 | |
| 372 | |
| 373 | GDBRemoteCommunication::PacketResult |
Greg Clayton | b30c50c | 2015-05-29 00:01:55 +0000 | [diff] [blame] | 374 | GDBRemoteCommunication::WaitForPacketWithTimeoutMicroSecondsNoLock (StringExtractorGDBRemote &packet, uint32_t timeout_usec, bool sync_on_timeout) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 375 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 376 | uint8_t buffer[8192]; |
| 377 | Error error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 378 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 379 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS | GDBR_LOG_VERBOSE)); |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 380 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 381 | // Check for a packet from our cache first without trying any reading... |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 382 | if (CheckForPacket(NULL, 0, packet) != PacketType::Invalid) |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 383 | return PacketResult::Success; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 384 | |
Greg Clayton | 0c51ac3 | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 385 | bool timed_out = false; |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 386 | bool disconnected = false; |
Greg Clayton | 0c51ac3 | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 387 | while (IsConnected() && !timed_out) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 388 | { |
Johnny Chen | 74549c8 | 2011-07-19 01:13:00 +0000 | [diff] [blame] | 389 | lldb::ConnectionStatus status = eConnectionStatusNoConnection; |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 390 | size_t bytes_read = Read (buffer, sizeof(buffer), timeout_usec, status, &error); |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 391 | |
| 392 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 393 | log->Printf ("%s: Read (buffer, (sizeof(buffer), timeout_usec = 0x%x, status = %s, error = %s) => bytes_read = %" PRIu64, |
Zachary Turner | f343968f | 2016-08-09 23:06:08 +0000 | [diff] [blame] | 394 | LLVM_PRETTY_FUNCTION, |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 395 | timeout_usec, |
| 396 | Communication::ConnectionStatusAsCString (status), |
| 397 | error.AsCString(), |
Greg Clayton | 43e0af0 | 2012-09-18 18:04:04 +0000 | [diff] [blame] | 398 | (uint64_t)bytes_read); |
Greg Clayton | 644247c | 2011-07-07 01:59:51 +0000 | [diff] [blame] | 399 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 400 | if (bytes_read > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 401 | { |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 402 | if (CheckForPacket(buffer, bytes_read, packet) != PacketType::Invalid) |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 403 | return PacketResult::Success; |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 404 | } |
| 405 | else |
| 406 | { |
| 407 | switch (status) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 408 | { |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 409 | case eConnectionStatusTimedOut: |
Greg Clayton | f0066ad | 2014-05-02 00:45:31 +0000 | [diff] [blame] | 410 | case eConnectionStatusInterrupted: |
Greg Clayton | b30c50c | 2015-05-29 00:01:55 +0000 | [diff] [blame] | 411 | if (sync_on_timeout) |
| 412 | { |
| 413 | //------------------------------------------------------------------ |
| 414 | /// Sync the remote GDB server and make sure we get a response that |
| 415 | /// corresponds to what we send. |
| 416 | /// |
| 417 | /// Sends a "qEcho" packet and makes sure it gets the exact packet |
| 418 | /// echoed back. If the qEcho packet isn't supported, we send a qC |
| 419 | /// packet and make sure we get a valid thread ID back. We use the |
| 420 | /// "qC" packet since its response if very unique: is responds with |
| 421 | /// "QC%x" where %x is the thread ID of the current thread. This |
| 422 | /// makes the response unique enough from other packet responses to |
| 423 | /// ensure we are back on track. |
| 424 | /// |
| 425 | /// This packet is needed after we time out sending a packet so we |
| 426 | /// can ensure that we are getting the response for the packet we |
| 427 | /// are sending. There are no sequence IDs in the GDB remote |
| 428 | /// protocol (there used to be, but they are not supported anymore) |
| 429 | /// so if you timeout sending packet "abc", you might then send |
| 430 | /// packet "cde" and get the response for the previous "abc" packet. |
| 431 | /// Many responses are "OK" or "" (unsupported) or "EXX" (error) so |
| 432 | /// many responses for packets can look like responses for other |
| 433 | /// packets. So if we timeout, we need to ensure that we can get |
| 434 | /// back on track. If we can't get back on track, we must |
| 435 | /// disconnect. |
| 436 | //------------------------------------------------------------------ |
| 437 | bool sync_success = false; |
| 438 | bool got_actual_response = false; |
| 439 | // We timed out, we need to sync back up with the |
| 440 | char echo_packet[32]; |
| 441 | int echo_packet_len = 0; |
| 442 | RegularExpression response_regex; |
| 443 | |
| 444 | if (m_supports_qEcho == eLazyBoolYes) |
| 445 | { |
| 446 | echo_packet_len = ::snprintf (echo_packet, sizeof(echo_packet), "qEcho:%u", ++m_echo_number); |
| 447 | std::string regex_str = "^"; |
| 448 | regex_str += echo_packet; |
| 449 | regex_str += "$"; |
| 450 | response_regex.Compile(regex_str.c_str()); |
| 451 | } |
| 452 | else |
| 453 | { |
| 454 | echo_packet_len = ::snprintf (echo_packet, sizeof(echo_packet), "qC"); |
| 455 | response_regex.Compile("^QC[0-9A-Fa-f]+$"); |
| 456 | } |
| 457 | |
| 458 | PacketResult echo_packet_result = SendPacketNoLock (echo_packet, echo_packet_len); |
| 459 | if (echo_packet_result == PacketResult::Success) |
| 460 | { |
| 461 | const uint32_t max_retries = 3; |
| 462 | uint32_t successful_responses = 0; |
| 463 | for (uint32_t i=0; i<max_retries; ++i) |
| 464 | { |
| 465 | StringExtractorGDBRemote echo_response; |
| 466 | echo_packet_result = WaitForPacketWithTimeoutMicroSecondsNoLock (echo_response, timeout_usec, false); |
| 467 | if (echo_packet_result == PacketResult::Success) |
| 468 | { |
| 469 | ++successful_responses; |
| 470 | if (response_regex.Execute(echo_response.GetStringRef().c_str())) |
| 471 | { |
| 472 | sync_success = true; |
| 473 | break; |
| 474 | } |
| 475 | else if (successful_responses == 1) |
| 476 | { |
| 477 | // We got something else back as the first successful response, it probably is |
| 478 | // the response to the packet we actually wanted, so copy it over if this |
| 479 | // is the first success and continue to try to get the qEcho response |
| 480 | packet = echo_response; |
| 481 | got_actual_response = true; |
| 482 | } |
| 483 | } |
| 484 | else if (echo_packet_result == PacketResult::ErrorReplyTimeout) |
| 485 | continue; // Packet timed out, continue waiting for a response |
| 486 | else |
| 487 | break; // Something else went wrong getting the packet back, we failed and are done trying |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | // We weren't able to sync back up with the server, we must abort otherwise |
| 492 | // all responses might not be from the right packets... |
| 493 | if (sync_success) |
| 494 | { |
| 495 | // We timed out, but were able to recover |
| 496 | if (got_actual_response) |
| 497 | { |
| 498 | // We initially timed out, but we did get a response that came in before the successful |
| 499 | // reply to our qEcho packet, so lets say everything is fine... |
| 500 | return PacketResult::Success; |
| 501 | } |
| 502 | } |
| 503 | else |
| 504 | { |
| 505 | disconnected = true; |
| 506 | Disconnect(); |
| 507 | } |
| 508 | } |
Greg Clayton | 0c51ac3 | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 509 | timed_out = true; |
| 510 | break; |
| 511 | case eConnectionStatusSuccess: |
| 512 | //printf ("status = success but error = %s\n", error.AsCString("<invalid>")); |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 513 | break; |
| 514 | |
| 515 | case eConnectionStatusEndOfFile: |
| 516 | case eConnectionStatusNoConnection: |
| 517 | case eConnectionStatusLostConnection: |
| 518 | case eConnectionStatusError: |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 519 | disconnected = true; |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 520 | Disconnect(); |
| 521 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 524 | } |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 525 | packet.Clear (); |
| 526 | if (disconnected) |
| 527 | return PacketResult::ErrorDisconnected; |
| 528 | if (timed_out) |
| 529 | return PacketResult::ErrorReplyTimeout; |
| 530 | else |
| 531 | return PacketResult::ErrorReplyFailed; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 534 | bool |
| 535 | GDBRemoteCommunication::DecompressPacket () |
| 536 | { |
| 537 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
| 538 | |
| 539 | if (!CompressionIsEnabled()) |
| 540 | return true; |
| 541 | |
| 542 | size_t pkt_size = m_bytes.size(); |
Jason Molenda | fea7765 | 2015-07-14 19:19:07 +0000 | [diff] [blame] | 543 | |
| 544 | // Smallest possible compressed packet is $N#00 - an uncompressed empty reply, most commonly indicating |
| 545 | // an unsupported packet. Anything less than 5 characters, it's definitely not a compressed packet. |
Jason Molenda | 21c34ac | 2015-07-14 04:51:05 +0000 | [diff] [blame] | 546 | if (pkt_size < 5) |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 547 | return true; |
Jason Molenda | fea7765 | 2015-07-14 19:19:07 +0000 | [diff] [blame] | 548 | |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 549 | if (m_bytes[0] != '$' && m_bytes[0] != '%') |
| 550 | return true; |
| 551 | if (m_bytes[1] != 'C' && m_bytes[1] != 'N') |
| 552 | return true; |
Jason Molenda | a21fdb0 | 2015-08-02 01:36:09 +0000 | [diff] [blame] | 553 | |
| 554 | size_t hash_mark_idx = m_bytes.find ('#'); |
| 555 | if (hash_mark_idx == std::string::npos) |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 556 | return true; |
Jason Molenda | a21fdb0 | 2015-08-02 01:36:09 +0000 | [diff] [blame] | 557 | if (hash_mark_idx + 2 >= m_bytes.size()) |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 558 | return true; |
| 559 | |
Jason Molenda | a21fdb0 | 2015-08-02 01:36:09 +0000 | [diff] [blame] | 560 | if (!::isxdigit (m_bytes[hash_mark_idx + 1]) || !::isxdigit (m_bytes[hash_mark_idx + 2])) |
| 561 | return true; |
| 562 | |
| 563 | size_t content_length = pkt_size - 5; // not counting '$', 'C' | 'N', '#', & the two hex checksum chars |
| 564 | size_t content_start = 2; // The first character of the compressed/not-compressed text of the packet |
| 565 | size_t checksum_idx = hash_mark_idx + 1; // The first character of the two hex checksum characters |
| 566 | |
| 567 | // Normally size_of_first_packet == m_bytes.size() but m_bytes may contain multiple packets. |
| 568 | // size_of_first_packet is the size of the initial packet which we'll replace with the decompressed |
| 569 | // version of, leaving the rest of m_bytes unmodified. |
| 570 | size_t size_of_first_packet = hash_mark_idx + 3; |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 571 | |
| 572 | // Compressed packets ("$C") start with a base10 number which is the size of the uncompressed payload, |
| 573 | // then a : and then the compressed data. e.g. $C1024:<binary>#00 |
| 574 | // Update content_start and content_length to only include the <binary> part of the packet. |
| 575 | |
| 576 | uint64_t decompressed_bufsize = ULONG_MAX; |
| 577 | if (m_bytes[1] == 'C') |
| 578 | { |
| 579 | size_t i = content_start; |
| 580 | while (i < hash_mark_idx && isdigit(m_bytes[i])) |
| 581 | i++; |
| 582 | if (i < hash_mark_idx && m_bytes[i] == ':') |
| 583 | { |
| 584 | i++; |
| 585 | content_start = i; |
| 586 | content_length = hash_mark_idx - content_start; |
| 587 | std::string bufsize_str (m_bytes.data() + 2, i - 2 - 1); |
| 588 | errno = 0; |
| 589 | decompressed_bufsize = ::strtoul (bufsize_str.c_str(), NULL, 10); |
| 590 | if (errno != 0 || decompressed_bufsize == ULONG_MAX) |
| 591 | { |
Jason Molenda | a21fdb0 | 2015-08-02 01:36:09 +0000 | [diff] [blame] | 592 | m_bytes.erase (0, size_of_first_packet); |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 593 | return false; |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | if (GetSendAcks ()) |
| 599 | { |
| 600 | char packet_checksum_cstr[3]; |
| 601 | packet_checksum_cstr[0] = m_bytes[checksum_idx]; |
| 602 | packet_checksum_cstr[1] = m_bytes[checksum_idx + 1]; |
| 603 | packet_checksum_cstr[2] = '\0'; |
| 604 | long packet_checksum = strtol (packet_checksum_cstr, NULL, 16); |
| 605 | |
| 606 | long actual_checksum = CalculcateChecksum (m_bytes.data() + 1, hash_mark_idx - 1); |
| 607 | bool success = packet_checksum == actual_checksum; |
| 608 | if (!success) |
| 609 | { |
| 610 | if (log) |
| 611 | log->Printf ("error: checksum mismatch: %.*s expected 0x%2.2x, got 0x%2.2x", |
| 612 | (int)(pkt_size), |
| 613 | m_bytes.c_str(), |
| 614 | (uint8_t)packet_checksum, |
| 615 | (uint8_t)actual_checksum); |
| 616 | } |
| 617 | // Send the ack or nack if needed |
| 618 | if (!success) |
| 619 | { |
| 620 | SendNack(); |
Jason Molenda | a21fdb0 | 2015-08-02 01:36:09 +0000 | [diff] [blame] | 621 | m_bytes.erase (0, size_of_first_packet); |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 622 | return false; |
| 623 | } |
| 624 | else |
| 625 | { |
| 626 | SendAck(); |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | if (m_bytes[1] == 'N') |
| 631 | { |
| 632 | // This packet was not compressed -- delete the 'N' character at the |
| 633 | // start and the packet may be processed as-is. |
| 634 | m_bytes.erase(1, 1); |
| 635 | return true; |
| 636 | } |
| 637 | |
| 638 | // Reverse the gdb-remote binary escaping that was done to the compressed text to |
| 639 | // guard characters like '$', '#', '}', etc. |
| 640 | std::vector<uint8_t> unescaped_content; |
| 641 | unescaped_content.reserve (content_length); |
| 642 | size_t i = content_start; |
| 643 | while (i < hash_mark_idx) |
| 644 | { |
| 645 | if (m_bytes[i] == '}') |
| 646 | { |
| 647 | i++; |
| 648 | unescaped_content.push_back (m_bytes[i] ^ 0x20); |
| 649 | } |
| 650 | else |
| 651 | { |
| 652 | unescaped_content.push_back (m_bytes[i]); |
| 653 | } |
| 654 | i++; |
| 655 | } |
| 656 | |
| 657 | uint8_t *decompressed_buffer = nullptr; |
| 658 | size_t decompressed_bytes = 0; |
| 659 | |
| 660 | if (decompressed_bufsize != ULONG_MAX) |
| 661 | { |
| 662 | decompressed_buffer = (uint8_t *) malloc (decompressed_bufsize + 1); |
| 663 | if (decompressed_buffer == nullptr) |
| 664 | { |
Jason Molenda | a21fdb0 | 2015-08-02 01:36:09 +0000 | [diff] [blame] | 665 | m_bytes.erase (0, size_of_first_packet); |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 666 | return false; |
| 667 | } |
| 668 | |
| 669 | } |
| 670 | |
| 671 | #if defined (HAVE_LIBCOMPRESSION) |
| 672 | // libcompression is weak linked so check that compression_decode_buffer() is available |
| 673 | if (compression_decode_buffer != NULL && |
| 674 | (m_compression_type == CompressionType::ZlibDeflate |
| 675 | || m_compression_type == CompressionType::LZFSE |
| 676 | || m_compression_type == CompressionType::LZ4)) |
| 677 | { |
| 678 | compression_algorithm compression_type; |
| 679 | if (m_compression_type == CompressionType::ZlibDeflate) |
| 680 | compression_type = COMPRESSION_ZLIB; |
| 681 | else if (m_compression_type == CompressionType::LZFSE) |
| 682 | compression_type = COMPRESSION_LZFSE; |
| 683 | else if (m_compression_type == CompressionType::LZ4) |
| 684 | compression_type = COMPRESSION_LZ4_RAW; |
| 685 | else if (m_compression_type == CompressionType::LZMA) |
| 686 | compression_type = COMPRESSION_LZMA; |
| 687 | |
| 688 | |
| 689 | // If we have the expected size of the decompressed payload, we can allocate |
| 690 | // the right-sized buffer and do it. If we don't have that information, we'll |
| 691 | // need to try decoding into a big buffer and if the buffer wasn't big enough, |
| 692 | // increase it and try again. |
| 693 | |
| 694 | if (decompressed_bufsize != ULONG_MAX && decompressed_buffer != nullptr) |
| 695 | { |
| 696 | decompressed_bytes = compression_decode_buffer (decompressed_buffer, decompressed_bufsize + 10 , |
| 697 | (uint8_t*) unescaped_content.data(), |
| 698 | unescaped_content.size(), |
| 699 | NULL, |
| 700 | compression_type); |
| 701 | } |
| 702 | } |
| 703 | #endif |
| 704 | |
| 705 | #if defined (HAVE_LIBZ) |
| 706 | if (decompressed_bytes == 0 |
| 707 | && decompressed_bufsize != ULONG_MAX |
| 708 | && decompressed_buffer != nullptr |
| 709 | && m_compression_type == CompressionType::ZlibDeflate) |
| 710 | { |
| 711 | z_stream stream; |
| 712 | memset (&stream, 0, sizeof (z_stream)); |
| 713 | stream.next_in = (Bytef *) unescaped_content.data(); |
| 714 | stream.avail_in = (uInt) unescaped_content.size(); |
| 715 | stream.total_in = 0; |
| 716 | stream.next_out = (Bytef *) decompressed_buffer; |
| 717 | stream.avail_out = decompressed_bufsize; |
| 718 | stream.total_out = 0; |
| 719 | stream.zalloc = Z_NULL; |
| 720 | stream.zfree = Z_NULL; |
| 721 | stream.opaque = Z_NULL; |
| 722 | |
| 723 | if (inflateInit2 (&stream, -15) == Z_OK) |
| 724 | { |
| 725 | int status = inflate (&stream, Z_NO_FLUSH); |
| 726 | inflateEnd (&stream); |
| 727 | if (status == Z_STREAM_END) |
| 728 | { |
| 729 | decompressed_bytes = stream.total_out; |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | #endif |
| 734 | |
| 735 | if (decompressed_bytes == 0 || decompressed_buffer == nullptr) |
| 736 | { |
| 737 | if (decompressed_buffer) |
| 738 | free (decompressed_buffer); |
Jason Molenda | a21fdb0 | 2015-08-02 01:36:09 +0000 | [diff] [blame] | 739 | m_bytes.erase (0, size_of_first_packet); |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 740 | return false; |
| 741 | } |
| 742 | |
| 743 | std::string new_packet; |
| 744 | new_packet.reserve (decompressed_bytes + 6); |
| 745 | new_packet.push_back (m_bytes[0]); |
| 746 | new_packet.append ((const char *) decompressed_buffer, decompressed_bytes); |
| 747 | new_packet.push_back ('#'); |
| 748 | if (GetSendAcks ()) |
| 749 | { |
| 750 | uint8_t decompressed_checksum = CalculcateChecksum ((const char *) decompressed_buffer, decompressed_bytes); |
| 751 | char decompressed_checksum_str[3]; |
| 752 | snprintf (decompressed_checksum_str, 3, "%02x", decompressed_checksum); |
| 753 | new_packet.append (decompressed_checksum_str); |
| 754 | } |
| 755 | else |
| 756 | { |
| 757 | new_packet.push_back ('0'); |
| 758 | new_packet.push_back ('0'); |
| 759 | } |
| 760 | |
Jason Molenda | a21fdb0 | 2015-08-02 01:36:09 +0000 | [diff] [blame] | 761 | m_bytes.replace (0, size_of_first_packet, new_packet.data(), new_packet.size()); |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 762 | |
| 763 | free (decompressed_buffer); |
| 764 | return true; |
| 765 | } |
| 766 | |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 767 | GDBRemoteCommunication::PacketType |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 768 | GDBRemoteCommunication::CheckForPacket (const uint8_t *src, size_t src_len, StringExtractorGDBRemote &packet) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 769 | { |
| 770 | // Put the packet data into the buffer in a thread safe fashion |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 771 | std::lock_guard<std::recursive_mutex> guard(m_bytes_mutex); |
| 772 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 773 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 774 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 775 | if (src && src_len > 0) |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 776 | { |
Greg Clayton | 0c51ac3 | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 777 | if (log && log->GetVerbose()) |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 778 | { |
| 779 | StreamString s; |
Greg Clayton | 0c51ac3 | 2011-07-02 23:21:06 +0000 | [diff] [blame] | 780 | log->Printf ("GDBRemoteCommunication::%s adding %u bytes: %.*s", |
| 781 | __FUNCTION__, |
| 782 | (uint32_t)src_len, |
| 783 | (uint32_t)src_len, |
| 784 | src); |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 785 | } |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 786 | m_bytes.append ((const char *)src, src_len); |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 787 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 788 | |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 789 | bool isNotifyPacket = false; |
| 790 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 791 | // Parse up the packets into gdb remote packets |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 792 | if (!m_bytes.empty()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 793 | { |
| 794 | // end_idx must be one past the last valid packet byte. Start |
| 795 | // it off with an invalid value that is the same as the current |
| 796 | // index. |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 797 | size_t content_start = 0; |
Greg Clayton | 118593a | 2014-11-03 21:02:54 +0000 | [diff] [blame] | 798 | size_t content_length = 0; |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 799 | size_t total_length = 0; |
| 800 | size_t checksum_idx = std::string::npos; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 801 | |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 802 | // Size of packet before it is decompressed, for logging purposes |
| 803 | size_t original_packet_size = m_bytes.size(); |
| 804 | if (CompressionIsEnabled()) |
| 805 | { |
| 806 | if (DecompressPacket() == false) |
| 807 | { |
| 808 | packet.Clear(); |
| 809 | return GDBRemoteCommunication::PacketType::Standard; |
| 810 | } |
| 811 | } |
| 812 | |
Greg Clayton | 118593a | 2014-11-03 21:02:54 +0000 | [diff] [blame] | 813 | switch (m_bytes[0]) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 814 | { |
Greg Clayton | 118593a | 2014-11-03 21:02:54 +0000 | [diff] [blame] | 815 | case '+': // Look for ack |
| 816 | case '-': // Look for cancel |
| 817 | case '\x03': // ^C to halt target |
| 818 | content_length = total_length = 1; // The command is one byte long... |
| 819 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 820 | |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 821 | case '%': // Async notify packet |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 822 | isNotifyPacket = true; |
Jason Molenda | 62e0681 | 2016-02-16 04:14:33 +0000 | [diff] [blame] | 823 | LLVM_FALLTHROUGH; |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 824 | |
Greg Clayton | 118593a | 2014-11-03 21:02:54 +0000 | [diff] [blame] | 825 | case '$': |
| 826 | // Look for a standard gdb packet? |
| 827 | { |
| 828 | size_t hash_pos = m_bytes.find('#'); |
| 829 | if (hash_pos != std::string::npos) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 830 | { |
Greg Clayton | 118593a | 2014-11-03 21:02:54 +0000 | [diff] [blame] | 831 | if (hash_pos + 2 < m_bytes.size()) |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 832 | { |
Greg Clayton | 118593a | 2014-11-03 21:02:54 +0000 | [diff] [blame] | 833 | checksum_idx = hash_pos + 1; |
| 834 | // Skip the dollar sign |
| 835 | content_start = 1; |
| 836 | // Don't include the # in the content or the $ in the content length |
| 837 | content_length = hash_pos - 1; |
| 838 | |
| 839 | total_length = hash_pos + 3; // Skip the # and the two hex checksum bytes |
| 840 | } |
| 841 | else |
| 842 | { |
| 843 | // Checksum bytes aren't all here yet |
| 844 | content_length = std::string::npos; |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 845 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 846 | } |
Greg Clayton | 118593a | 2014-11-03 21:02:54 +0000 | [diff] [blame] | 847 | } |
| 848 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 849 | |
Greg Clayton | 118593a | 2014-11-03 21:02:54 +0000 | [diff] [blame] | 850 | default: |
| 851 | { |
| 852 | // We have an unexpected byte and we need to flush all bad |
| 853 | // data that is in m_bytes, so we need to find the first |
| 854 | // byte that is a '+' (ACK), '-' (NACK), \x03 (CTRL+C interrupt), |
| 855 | // or '$' character (start of packet header) or of course, |
| 856 | // the end of the data in m_bytes... |
| 857 | const size_t bytes_len = m_bytes.size(); |
| 858 | bool done = false; |
| 859 | uint32_t idx; |
| 860 | for (idx = 1; !done && idx < bytes_len; ++idx) |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 861 | { |
Greg Clayton | 118593a | 2014-11-03 21:02:54 +0000 | [diff] [blame] | 862 | switch (m_bytes[idx]) |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 863 | { |
Greg Clayton | 118593a | 2014-11-03 21:02:54 +0000 | [diff] [blame] | 864 | case '+': |
| 865 | case '-': |
| 866 | case '\x03': |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 867 | case '%': |
Greg Clayton | 118593a | 2014-11-03 21:02:54 +0000 | [diff] [blame] | 868 | case '$': |
| 869 | done = true; |
| 870 | break; |
| 871 | |
| 872 | default: |
| 873 | break; |
Greg Clayton | 197bacf | 2011-07-02 21:07:54 +0000 | [diff] [blame] | 874 | } |
| 875 | } |
Greg Clayton | 118593a | 2014-11-03 21:02:54 +0000 | [diff] [blame] | 876 | if (log) |
| 877 | log->Printf ("GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'", |
| 878 | __FUNCTION__, idx - 1, idx - 1, m_bytes.c_str()); |
| 879 | m_bytes.erase(0, idx - 1); |
| 880 | } |
| 881 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 884 | if (content_length == std::string::npos) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 885 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 886 | packet.Clear(); |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 887 | return GDBRemoteCommunication::PacketType::Invalid; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 888 | } |
Greg Clayton | f3dd93c | 2011-06-17 03:31:01 +0000 | [diff] [blame] | 889 | else if (total_length > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 890 | { |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 891 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 892 | // We have a valid packet... |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 893 | assert (content_length <= m_bytes.size()); |
| 894 | assert (total_length <= m_bytes.size()); |
| 895 | assert (content_length <= total_length); |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 896 | size_t content_end = content_start + content_length; |
Greg Clayton | 06f09b5 | 2014-06-20 20:41:07 +0000 | [diff] [blame] | 897 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 898 | bool success = true; |
| 899 | std::string &packet_str = packet.GetStringRef(); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 900 | if (log) |
| 901 | { |
| 902 | // If logging was just enabled and we have history, then dump out what |
| 903 | // we have to the log so we get the historical context. The Dump() call that |
| 904 | // logs all of the packet will set a boolean so that we don't dump this more |
| 905 | // than once |
| 906 | if (!m_history.DidDumpToLog ()) |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 907 | m_history.Dump (log); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 908 | |
Greg Clayton | 06f09b5 | 2014-06-20 20:41:07 +0000 | [diff] [blame] | 909 | bool binary = false; |
| 910 | // Only detect binary for packets that start with a '$' and have a '#CC' checksum |
| 911 | if (m_bytes[0] == '$' && total_length > 4) |
| 912 | { |
| 913 | for (size_t i=0; !binary && i<total_length; ++i) |
| 914 | { |
Jason Molenda | 0ace3f5 | 2015-09-09 03:24:52 +0000 | [diff] [blame] | 915 | if (isprint (m_bytes[i]) == 0 && isspace (m_bytes[i]) == 0) |
| 916 | { |
Greg Clayton | 06f09b5 | 2014-06-20 20:41:07 +0000 | [diff] [blame] | 917 | binary = true; |
Jason Molenda | 0ace3f5 | 2015-09-09 03:24:52 +0000 | [diff] [blame] | 918 | } |
Greg Clayton | 06f09b5 | 2014-06-20 20:41:07 +0000 | [diff] [blame] | 919 | } |
| 920 | } |
| 921 | if (binary) |
| 922 | { |
| 923 | StreamString strm; |
| 924 | // Packet header... |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 925 | if (CompressionIsEnabled()) |
| 926 | strm.Printf("<%4" PRIu64 ":%" PRIu64 "> read packet: %c", (uint64_t) original_packet_size, (uint64_t)total_length, m_bytes[0]); |
| 927 | else |
| 928 | strm.Printf("<%4" PRIu64 "> read packet: %c", (uint64_t)total_length, m_bytes[0]); |
Greg Clayton | 06f09b5 | 2014-06-20 20:41:07 +0000 | [diff] [blame] | 929 | for (size_t i=content_start; i<content_end; ++i) |
| 930 | { |
| 931 | // Remove binary escaped bytes when displaying the packet... |
| 932 | const char ch = m_bytes[i]; |
| 933 | if (ch == 0x7d) |
| 934 | { |
| 935 | // 0x7d is the escape character. The next character is to |
| 936 | // be XOR'd with 0x20. |
| 937 | const char escapee = m_bytes[++i] ^ 0x20; |
| 938 | strm.Printf("%2.2x", escapee); |
| 939 | } |
| 940 | else |
| 941 | { |
| 942 | strm.Printf("%2.2x", (uint8_t)ch); |
| 943 | } |
| 944 | } |
| 945 | // Packet footer... |
| 946 | strm.Printf("%c%c%c", m_bytes[total_length-3], m_bytes[total_length-2], m_bytes[total_length-1]); |
| 947 | log->PutCString(strm.GetString().c_str()); |
| 948 | } |
| 949 | else |
| 950 | { |
Jason Molenda | 91ffe0a | 2015-06-18 21:46:06 +0000 | [diff] [blame] | 951 | if (CompressionIsEnabled()) |
| 952 | log->Printf("<%4" PRIu64 ":%" PRIu64 "> read packet: %.*s", (uint64_t) original_packet_size, (uint64_t)total_length, (int)(total_length), m_bytes.c_str()); |
| 953 | else |
| 954 | log->Printf("<%4" PRIu64 "> read packet: %.*s", (uint64_t)total_length, (int)(total_length), m_bytes.c_str()); |
Greg Clayton | 06f09b5 | 2014-06-20 20:41:07 +0000 | [diff] [blame] | 955 | } |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | m_history.AddPacket (m_bytes.c_str(), total_length, History::ePacketTypeRecv, total_length); |
| 959 | |
Hafiz Abid Qadeer | e5fd5e1 | 2013-08-28 15:10:37 +0000 | [diff] [blame] | 960 | // Clear packet_str in case there is some existing data in it. |
| 961 | packet_str.clear(); |
Hafiz Abid Qadeer | da96ef2 | 2013-08-28 10:31:52 +0000 | [diff] [blame] | 962 | // Copy the packet from m_bytes to packet_str expanding the |
| 963 | // run-length encoding in the process. |
| 964 | // Reserve enough byte for the most common case (no RLE used) |
| 965 | packet_str.reserve(m_bytes.length()); |
Greg Clayton | 06f09b5 | 2014-06-20 20:41:07 +0000 | [diff] [blame] | 966 | 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] | 967 | { |
| 968 | if (*c == '*') |
| 969 | { |
| 970 | // '*' indicates RLE. Next character will give us the |
| 971 | // repeat count and previous character is what is to be |
| 972 | // repeated. |
| 973 | char char_to_repeat = packet_str.back(); |
| 974 | // Number of time the previous character is repeated |
| 975 | int repeat_count = *++c + 3 - ' '; |
| 976 | // We have the char_to_repeat and repeat_count. Now push |
| 977 | // it in the packet. |
| 978 | for (int i = 0; i < repeat_count; ++i) |
| 979 | packet_str.push_back(char_to_repeat); |
| 980 | } |
Steve Pucci | 3c5d333 | 2014-02-24 19:07:29 +0000 | [diff] [blame] | 981 | else if (*c == 0x7d) |
| 982 | { |
| 983 | // 0x7d is the escape character. The next character is to |
| 984 | // be XOR'd with 0x20. |
| 985 | char escapee = *++c ^ 0x20; |
| 986 | packet_str.push_back(escapee); |
| 987 | } |
Hafiz Abid Qadeer | da96ef2 | 2013-08-28 10:31:52 +0000 | [diff] [blame] | 988 | else |
| 989 | { |
| 990 | packet_str.push_back(*c); |
| 991 | } |
| 992 | } |
| 993 | |
Ewan Crawford | 78baa19 | 2015-05-13 09:18:18 +0000 | [diff] [blame] | 994 | if (m_bytes[0] == '$' || m_bytes[0] == '%') |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 995 | { |
| 996 | assert (checksum_idx < m_bytes.size()); |
| 997 | if (::isxdigit (m_bytes[checksum_idx+0]) || |
| 998 | ::isxdigit (m_bytes[checksum_idx+1])) |
| 999 | { |
| 1000 | if (GetSendAcks ()) |
| 1001 | { |
| 1002 | const char *packet_checksum_cstr = &m_bytes[checksum_idx]; |
| 1003 | char packet_checksum = strtol (packet_checksum_cstr, NULL, 16); |
| 1004 | char actual_checksum = CalculcateChecksum (packet_str.c_str(), packet_str.size()); |
| 1005 | success = packet_checksum == actual_checksum; |
| 1006 | if (!success) |
| 1007 | { |
| 1008 | if (log) |
| 1009 | log->Printf ("error: checksum mismatch: %.*s expected 0x%2.2x, got 0x%2.2x", |
| 1010 | (int)(total_length), |
| 1011 | m_bytes.c_str(), |
| 1012 | (uint8_t)packet_checksum, |
| 1013 | (uint8_t)actual_checksum); |
| 1014 | } |
| 1015 | // Send the ack or nack if needed |
| 1016 | if (!success) |
| 1017 | SendNack(); |
| 1018 | else |
| 1019 | SendAck(); |
| 1020 | } |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 1021 | } |
| 1022 | else |
| 1023 | { |
| 1024 | success = false; |
| 1025 | if (log) |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 1026 | 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] | 1027 | } |
| 1028 | } |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 1029 | |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 1030 | m_bytes.erase(0, total_length); |
| 1031 | packet.SetFilePos(0); |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 1032 | |
| 1033 | if (isNotifyPacket) |
| 1034 | return GDBRemoteCommunication::PacketType::Notify; |
| 1035 | else |
| 1036 | return GDBRemoteCommunication::PacketType::Standard; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1037 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1038 | } |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 1039 | packet.Clear(); |
Ewan Crawford | 9aa2da00 | 2015-05-27 14:12:34 +0000 | [diff] [blame] | 1040 | return GDBRemoteCommunication::PacketType::Invalid; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1043 | Error |
Greg Clayton | d629980 | 2013-12-06 17:46:35 +0000 | [diff] [blame] | 1044 | GDBRemoteCommunication::StartListenThread (const char *hostname, uint16_t port) |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1045 | { |
| 1046 | Error error; |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 1047 | if (m_listen_thread.IsJoinable()) |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1048 | { |
| 1049 | error.SetErrorString("listen thread already running"); |
| 1050 | } |
| 1051 | else |
| 1052 | { |
| 1053 | char listen_url[512]; |
| 1054 | if (hostname && hostname[0]) |
Jean-Daniel Dupas | 3c6774a | 2014-02-08 20:29:40 +0000 | [diff] [blame] | 1055 | snprintf(listen_url, sizeof(listen_url), "listen://%s:%i", hostname, port); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1056 | else |
| 1057 | snprintf(listen_url, sizeof(listen_url), "listen://%i", port); |
| 1058 | m_listen_url = listen_url; |
| 1059 | SetConnection(new ConnectionFileDescriptor()); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 1060 | m_listen_thread = ThreadLauncher::LaunchThread(listen_url, GDBRemoteCommunication::ListenThread, this, &error); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1061 | } |
| 1062 | return error; |
| 1063 | } |
| 1064 | |
| 1065 | bool |
| 1066 | GDBRemoteCommunication::JoinListenThread () |
| 1067 | { |
Zachary Turner | acee96a | 2014-09-23 18:32:09 +0000 | [diff] [blame] | 1068 | if (m_listen_thread.IsJoinable()) |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 1069 | m_listen_thread.Join(nullptr); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1070 | return true; |
| 1071 | } |
| 1072 | |
| 1073 | lldb::thread_result_t |
| 1074 | GDBRemoteCommunication::ListenThread (lldb::thread_arg_t arg) |
| 1075 | { |
| 1076 | GDBRemoteCommunication *comm = (GDBRemoteCommunication *)arg; |
| 1077 | Error error; |
| 1078 | ConnectionFileDescriptor *connection = (ConnectionFileDescriptor *)comm->GetConnection (); |
| 1079 | |
| 1080 | if (connection) |
| 1081 | { |
| 1082 | // Do the listen on another thread so we can continue on... |
| 1083 | if (connection->Connect(comm->m_listen_url.c_str(), &error) != eConnectionStatusSuccess) |
| 1084 | comm->SetConnection(NULL); |
| 1085 | } |
| 1086 | return NULL; |
| 1087 | } |
| 1088 | |
| 1089 | Error |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1090 | GDBRemoteCommunication::StartDebugserverProcess (const char *url, |
Greg Clayton | 6988abc | 2015-10-19 20:44:01 +0000 | [diff] [blame] | 1091 | Platform *platform, |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 1092 | ProcessLaunchInfo &launch_info, |
Tamas Berghammer | ccd6cff | 2015-12-08 14:08:19 +0000 | [diff] [blame] | 1093 | uint16_t *port, |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame^] | 1094 | const Args* inferior_args, |
| 1095 | int pass_comm_fd) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1096 | { |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 1097 | Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); |
| 1098 | if (log) |
Todd Fiala | 7aa4d97 | 2016-05-31 18:32:20 +0000 | [diff] [blame] | 1099 | log->Printf ("GDBRemoteCommunication::%s(url=%s, port=%" PRIu16 ")", __FUNCTION__, url ? url : "<empty>", port ? *port : uint16_t(0)); |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 1100 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1101 | Error error; |
| 1102 | // If we locate debugserver, keep that located version around |
| 1103 | static FileSpec g_debugserver_file_spec; |
| 1104 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1105 | char debugserver_path[PATH_MAX]; |
| 1106 | FileSpec &debugserver_file_spec = launch_info.GetExecutableFile(); |
| 1107 | |
| 1108 | // Always check to see if we have an environment override for the path |
| 1109 | // to the debugserver to use and use it if we do. |
| 1110 | const char *env_debugserver_path = getenv("LLDB_DEBUGSERVER_PATH"); |
| 1111 | if (env_debugserver_path) |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 1112 | { |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1113 | debugserver_file_spec.SetFile (env_debugserver_path, false); |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 1114 | if (log) |
| 1115 | log->Printf ("GDBRemoteCommunication::%s() gdb-remote stub exe path set from environment variable: %s", __FUNCTION__, env_debugserver_path); |
| 1116 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1117 | else |
| 1118 | debugserver_file_spec = g_debugserver_file_spec; |
| 1119 | bool debugserver_exists = debugserver_file_spec.Exists(); |
| 1120 | if (!debugserver_exists) |
| 1121 | { |
| 1122 | // The debugserver binary is in the LLDB.framework/Resources |
Zachary Turner | 42ff0ad | 2014-08-21 17:29:12 +0000 | [diff] [blame] | 1123 | // directory. |
| 1124 | if (HostInfo::GetLLDBPath(ePathTypeSupportExecutableDir, debugserver_file_spec)) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1125 | { |
Jason Molenda | 6fd8677 | 2014-08-21 23:22:33 +0000 | [diff] [blame] | 1126 | debugserver_file_spec.AppendPathComponent (DEBUGSERVER_BASENAME); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1127 | debugserver_exists = debugserver_file_spec.Exists(); |
| 1128 | if (debugserver_exists) |
| 1129 | { |
Todd Fiala | 015d818 | 2014-07-22 23:41:36 +0000 | [diff] [blame] | 1130 | if (log) |
| 1131 | log->Printf ("GDBRemoteCommunication::%s() found gdb-remote stub exe '%s'", __FUNCTION__, debugserver_file_spec.GetPath ().c_str ()); |
| 1132 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1133 | g_debugserver_file_spec = debugserver_file_spec; |
| 1134 | } |
| 1135 | else |
| 1136 | { |
Greg Clayton | 6988abc | 2015-10-19 20:44:01 +0000 | [diff] [blame] | 1137 | debugserver_file_spec = platform->LocateExecutable(DEBUGSERVER_BASENAME); |
| 1138 | if (debugserver_file_spec) |
| 1139 | { |
| 1140 | // Platform::LocateExecutable() wouldn't return a path if it doesn't exist |
| 1141 | debugserver_exists = true; |
| 1142 | } |
| 1143 | else |
| 1144 | { |
| 1145 | if (log) |
| 1146 | log->Printf ("GDBRemoteCommunication::%s() could not find gdb-remote stub exe '%s'", __FUNCTION__, debugserver_file_spec.GetPath ().c_str ()); |
| 1147 | } |
| 1148 | // Don't cache the platform specific GDB server binary as it could change |
| 1149 | // from platform to platform |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1150 | g_debugserver_file_spec.Clear(); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | |
| 1155 | if (debugserver_exists) |
| 1156 | { |
| 1157 | debugserver_file_spec.GetPath (debugserver_path, sizeof(debugserver_path)); |
| 1158 | |
| 1159 | Args &debugserver_args = launch_info.GetArguments(); |
| 1160 | debugserver_args.Clear(); |
| 1161 | char arg_cstr[PATH_MAX]; |
Tamas Berghammer | c2c3d71 | 2015-02-18 15:39:41 +0000 | [diff] [blame] | 1162 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1163 | // Start args with "debugserver /file/path -r --" |
| 1164 | debugserver_args.AppendArgument(debugserver_path); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1165 | |
Tamas Berghammer | c2c3d71 | 2015-02-18 15:39:41 +0000 | [diff] [blame] | 1166 | #if !defined(__APPLE__) |
| 1167 | // First argument to lldb-server must be mode in which to run. |
| 1168 | debugserver_args.AppendArgument("gdbserver"); |
| 1169 | #endif |
| 1170 | |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1171 | // If a url is supplied then use it |
| 1172 | if (url) |
| 1173 | debugserver_args.AppendArgument(url); |
Greg Clayton | fda4fab | 2014-01-10 22:24:11 +0000 | [diff] [blame] | 1174 | |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame^] | 1175 | if (pass_comm_fd >= 0) |
| 1176 | { |
| 1177 | StreamString fd_arg; |
| 1178 | fd_arg.Printf("--fd=%i", pass_comm_fd); |
| 1179 | debugserver_args.AppendArgument(fd_arg.GetData()); |
| 1180 | // Send "pass_comm_fd" down to the inferior so it can use it to |
| 1181 | // communicate back with this process |
| 1182 | launch_info.AppendDuplicateFileAction(pass_comm_fd, pass_comm_fd); |
| 1183 | } |
| 1184 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1185 | // use native registers, not the GDB registers |
Oleksiy Vyalov | f8ce61c | 2015-01-28 17:36:59 +0000 | [diff] [blame] | 1186 | debugserver_args.AppendArgument("--native-regs"); |
| 1187 | |
| 1188 | if (launch_info.GetLaunchInSeparateProcessGroup()) |
| 1189 | { |
| 1190 | debugserver_args.AppendArgument("--setsid"); |
| 1191 | } |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 1192 | |
Oleksiy Vyalov | 4536c45 | 2015-02-05 16:29:12 +0000 | [diff] [blame] | 1193 | llvm::SmallString<PATH_MAX> named_pipe_path; |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1194 | // socket_pipe is used by debug server to communicate back either |
| 1195 | // TCP port or domain socket name which it listens on. |
| 1196 | // The second purpose of the pipe to serve as a synchronization point - |
| 1197 | // once data is written to the pipe, debug server is up and running. |
| 1198 | Pipe socket_pipe; |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1199 | |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1200 | // port is null when debug server should listen on domain socket - |
| 1201 | // we're not interested in port value but rather waiting for debug server |
| 1202 | // to become available. |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame^] | 1203 | if (pass_comm_fd == -1 && ((port != nullptr && *port == 0) || port == nullptr)) |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 1204 | { |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1205 | if (url) |
Jason Molenda | 0409754 | 2015-09-22 23:25:44 +0000 | [diff] [blame] | 1206 | { |
| 1207 | // Create a temporary file to get the stdout/stderr and redirect the |
| 1208 | // output of the command into this file. We will later read this file |
| 1209 | // if all goes well and fill the data into "command_output_ptr" |
| 1210 | |
Chaoren Lin | 46951b5 | 2015-07-30 17:48:44 +0000 | [diff] [blame] | 1211 | #if defined(__APPLE__) |
Jason Molenda | 0409754 | 2015-09-22 23:25:44 +0000 | [diff] [blame] | 1212 | // Binding to port zero, we need to figure out what port it ends up |
| 1213 | // using using a named pipe... |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1214 | error = socket_pipe.CreateWithUniqueName("debugserver-named-pipe", false, named_pipe_path); |
Jason Molenda | 0409754 | 2015-09-22 23:25:44 +0000 | [diff] [blame] | 1215 | if (error.Fail()) |
| 1216 | { |
| 1217 | if (log) |
| 1218 | log->Printf("GDBRemoteCommunication::%s() " |
| 1219 | "named pipe creation failed: %s", |
| 1220 | __FUNCTION__, error.AsCString()); |
| 1221 | return error; |
| 1222 | } |
| 1223 | debugserver_args.AppendArgument("--named-pipe"); |
| 1224 | debugserver_args.AppendArgument(named_pipe_path.c_str()); |
Chaoren Lin | 46951b5 | 2015-07-30 17:48:44 +0000 | [diff] [blame] | 1225 | #else |
Jason Molenda | 0409754 | 2015-09-22 23:25:44 +0000 | [diff] [blame] | 1226 | // Binding to port zero, we need to figure out what port it ends up |
| 1227 | // using using an unnamed pipe... |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1228 | error = socket_pipe.CreateNew(true); |
Jason Molenda | 0409754 | 2015-09-22 23:25:44 +0000 | [diff] [blame] | 1229 | if (error.Fail()) |
| 1230 | { |
| 1231 | if (log) |
| 1232 | log->Printf("GDBRemoteCommunication::%s() " |
| 1233 | "unnamed pipe creation failed: %s", |
| 1234 | __FUNCTION__, error.AsCString()); |
| 1235 | return error; |
| 1236 | } |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1237 | int write_fd = socket_pipe.GetWriteFileDescriptor(); |
Jason Molenda | 0409754 | 2015-09-22 23:25:44 +0000 | [diff] [blame] | 1238 | debugserver_args.AppendArgument("--pipe"); |
Pavel Labath | 1eb0d42 | 2016-08-08 12:54:36 +0000 | [diff] [blame] | 1239 | debugserver_args.AppendArgument(llvm::to_string(write_fd).c_str()); |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1240 | launch_info.AppendCloseFileAction(socket_pipe.GetReadFileDescriptor()); |
Chaoren Lin | 46951b5 | 2015-07-30 17:48:44 +0000 | [diff] [blame] | 1241 | #endif |
Greg Clayton | 1681092 | 2014-02-27 19:38:18 +0000 | [diff] [blame] | 1242 | } |
| 1243 | else |
| 1244 | { |
Jason Molenda | 0409754 | 2015-09-22 23:25:44 +0000 | [diff] [blame] | 1245 | // No host and port given, so lets listen on our end and make the debugserver |
| 1246 | // connect to us.. |
| 1247 | error = StartListenThread ("127.0.0.1", 0); |
| 1248 | if (error.Fail()) |
| 1249 | { |
| 1250 | if (log) |
| 1251 | log->Printf ("GDBRemoteCommunication::%s() unable to start listen thread: %s", __FUNCTION__, error.AsCString()); |
| 1252 | return error; |
| 1253 | } |
| 1254 | |
| 1255 | ConnectionFileDescriptor *connection = (ConnectionFileDescriptor *)GetConnection (); |
| 1256 | // Wait for 10 seconds to resolve the bound port |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1257 | *port = connection->GetListeningPort(10); |
| 1258 | if (*port > 0) |
Jason Molenda | 0409754 | 2015-09-22 23:25:44 +0000 | [diff] [blame] | 1259 | { |
| 1260 | char port_cstr[32]; |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1261 | snprintf(port_cstr, sizeof(port_cstr), "127.0.0.1:%i", *port); |
Jason Molenda | 0409754 | 2015-09-22 23:25:44 +0000 | [diff] [blame] | 1262 | // Send the host and port down that debugserver and specify an option |
| 1263 | // so that it connects back to the port we are listening to in this process |
| 1264 | debugserver_args.AppendArgument("--reverse-connect"); |
| 1265 | debugserver_args.AppendArgument(port_cstr); |
| 1266 | } |
| 1267 | else |
| 1268 | { |
| 1269 | error.SetErrorString ("failed to bind to port 0 on 127.0.0.1"); |
| 1270 | if (log) |
| 1271 | log->Printf ("GDBRemoteCommunication::%s() failed: %s", __FUNCTION__, error.AsCString()); |
| 1272 | return error; |
| 1273 | } |
Greg Clayton | 1681092 | 2014-02-27 19:38:18 +0000 | [diff] [blame] | 1274 | } |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1275 | } |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1276 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1277 | const char *env_debugserver_log_file = getenv("LLDB_DEBUGSERVER_LOG_FILE"); |
| 1278 | if (env_debugserver_log_file) |
| 1279 | { |
| 1280 | ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-file=%s", env_debugserver_log_file); |
| 1281 | debugserver_args.AppendArgument(arg_cstr); |
| 1282 | } |
| 1283 | |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 1284 | #if defined(__APPLE__) |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1285 | const char *env_debugserver_log_flags = getenv("LLDB_DEBUGSERVER_LOG_FLAGS"); |
| 1286 | if (env_debugserver_log_flags) |
| 1287 | { |
| 1288 | ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-flags=%s", env_debugserver_log_flags); |
| 1289 | debugserver_args.AppendArgument(arg_cstr); |
| 1290 | } |
Vince Harron | 9753dd9 | 2015-05-10 15:22:09 +0000 | [diff] [blame] | 1291 | #else |
| 1292 | const char *env_debugserver_log_channels = getenv("LLDB_SERVER_LOG_CHANNELS"); |
| 1293 | if (env_debugserver_log_channels) |
| 1294 | { |
| 1295 | ::snprintf (arg_cstr, sizeof(arg_cstr), "--log-channels=%s", env_debugserver_log_channels); |
| 1296 | debugserver_args.AppendArgument(arg_cstr); |
| 1297 | } |
| 1298 | #endif |
Todd Fiala | 34ba426 | 2014-08-29 17:10:31 +0000 | [diff] [blame] | 1299 | |
| 1300 | // Add additional args, starting with LLDB_DEBUGSERVER_EXTRA_ARG_1 until an env var doesn't come back. |
| 1301 | uint32_t env_var_index = 1; |
| 1302 | bool has_env_var; |
| 1303 | do |
| 1304 | { |
| 1305 | char env_var_name[64]; |
| 1306 | snprintf (env_var_name, sizeof (env_var_name), "LLDB_DEBUGSERVER_EXTRA_ARG_%" PRIu32, env_var_index++); |
| 1307 | const char *extra_arg = getenv(env_var_name); |
| 1308 | has_env_var = extra_arg != nullptr; |
| 1309 | |
| 1310 | if (has_env_var) |
| 1311 | { |
| 1312 | debugserver_args.AppendArgument (extra_arg); |
| 1313 | if (log) |
| 1314 | log->Printf ("GDBRemoteCommunication::%s adding env var %s contents to stub command line (%s)", __FUNCTION__, env_var_name, extra_arg); |
| 1315 | } |
| 1316 | } while (has_env_var); |
| 1317 | |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame^] | 1318 | if (inferior_args && inferior_args->GetArgumentCount() > 0) |
Tamas Berghammer | ccd6cff | 2015-12-08 14:08:19 +0000 | [diff] [blame] | 1319 | { |
| 1320 | debugserver_args.AppendArgument ("--"); |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame^] | 1321 | debugserver_args.AppendArguments (*inferior_args); |
Tamas Berghammer | ccd6cff | 2015-12-08 14:08:19 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | // Copy the current environment to the gdbserver/debugserver instance |
| 1325 | StringList env; |
| 1326 | if (Host::GetEnvironment(env)) |
| 1327 | { |
| 1328 | for (size_t i = 0; i < env.GetSize(); ++i) |
| 1329 | launch_info.GetEnvironmentEntries().AppendArgument(env[i].c_str()); |
| 1330 | } |
| 1331 | |
Shawn Best | 629680e | 2014-11-05 00:58:55 +0000 | [diff] [blame] | 1332 | // Close STDIN, STDOUT and STDERR. |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 1333 | launch_info.AppendCloseFileAction (STDIN_FILENO); |
| 1334 | launch_info.AppendCloseFileAction (STDOUT_FILENO); |
| 1335 | launch_info.AppendCloseFileAction (STDERR_FILENO); |
Shawn Best | 629680e | 2014-11-05 00:58:55 +0000 | [diff] [blame] | 1336 | |
| 1337 | // Redirect STDIN, STDOUT and STDERR to "/dev/null". |
| 1338 | launch_info.AppendSuppressFileAction (STDIN_FILENO, true, false); |
| 1339 | launch_info.AppendSuppressFileAction (STDOUT_FILENO, false, true); |
| 1340 | launch_info.AppendSuppressFileAction (STDERR_FILENO, false, true); |
Todd Fiala | 7aa4d97 | 2016-05-31 18:32:20 +0000 | [diff] [blame] | 1341 | |
| 1342 | if (log) |
| 1343 | { |
| 1344 | StreamString string_stream; |
| 1345 | Platform *const platform = nullptr; |
| 1346 | launch_info.Dump(string_stream, platform); |
| 1347 | log->Printf("launch info for gdb-remote stub:\n%s", string_stream.GetString().c_str()); |
| 1348 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1349 | error = Host::LaunchProcess(launch_info); |
Zachary Turner | 9b69327 | 2014-12-04 22:06:42 +0000 | [diff] [blame] | 1350 | |
Greg Clayton | c6c420f | 2016-08-12 16:46:18 +0000 | [diff] [blame^] | 1351 | if (error.Success() && (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) && pass_comm_fd == -1) |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 1352 | { |
Oleksiy Vyalov | 4536c45 | 2015-02-05 16:29:12 +0000 | [diff] [blame] | 1353 | if (named_pipe_path.size() > 0) |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 1354 | { |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1355 | error = socket_pipe.OpenAsReader(named_pipe_path, false); |
Chaoren Lin | 368c9f6 | 2015-04-27 23:20:30 +0000 | [diff] [blame] | 1356 | if (error.Fail()) |
| 1357 | if (log) |
| 1358 | log->Printf("GDBRemoteCommunication::%s() " |
| 1359 | "failed to open named pipe %s for reading: %s", |
| 1360 | __FUNCTION__, named_pipe_path.c_str(), error.AsCString()); |
| 1361 | } |
| 1362 | |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1363 | if (socket_pipe.CanWrite()) |
| 1364 | socket_pipe.CloseWriteFileDescriptor(); |
| 1365 | if (socket_pipe.CanRead()) |
Chaoren Lin | 368c9f6 | 2015-04-27 23:20:30 +0000 | [diff] [blame] | 1366 | { |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1367 | char port_cstr[PATH_MAX] = {0}; |
Chaoren Lin | 368c9f6 | 2015-04-27 23:20:30 +0000 | [diff] [blame] | 1368 | port_cstr[0] = '\0'; |
| 1369 | size_t num_bytes = sizeof(port_cstr); |
| 1370 | // Read port from pipe with 10 second timeout. |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1371 | error = socket_pipe.ReadWithTimeout(port_cstr, num_bytes, |
Chaoren Lin | 368c9f6 | 2015-04-27 23:20:30 +0000 | [diff] [blame] | 1372 | std::chrono::seconds{10}, num_bytes); |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1373 | if (error.Success() && (port != nullptr)) |
Greg Clayton | 3121fde | 2014-02-28 20:47:08 +0000 | [diff] [blame] | 1374 | { |
Chaoren Lin | 368c9f6 | 2015-04-27 23:20:30 +0000 | [diff] [blame] | 1375 | assert(num_bytes > 0 && port_cstr[num_bytes-1] == '\0'); |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1376 | *port = StringConvert::ToUInt32(port_cstr, 0); |
Chaoren Lin | 368c9f6 | 2015-04-27 23:20:30 +0000 | [diff] [blame] | 1377 | if (log) |
| 1378 | log->Printf("GDBRemoteCommunication::%s() " |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1379 | "debugserver listens %u port", |
| 1380 | __FUNCTION__, *port); |
Greg Clayton | 3121fde | 2014-02-28 20:47:08 +0000 | [diff] [blame] | 1381 | } |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 1382 | else |
| 1383 | { |
| 1384 | if (log) |
Chaoren Lin | 368c9f6 | 2015-04-27 23:20:30 +0000 | [diff] [blame] | 1385 | log->Printf("GDBRemoteCommunication::%s() " |
| 1386 | "failed to read a port value from pipe %s: %s", |
| 1387 | __FUNCTION__, named_pipe_path.c_str(), error.AsCString()); |
| 1388 | |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 1389 | } |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1390 | socket_pipe.Close(); |
Chaoren Lin | 368c9f6 | 2015-04-27 23:20:30 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | if (named_pipe_path.size() > 0) |
| 1394 | { |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 1395 | const auto err = socket_pipe.Delete(named_pipe_path); |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 1396 | if (err.Fail()) |
| 1397 | { |
| 1398 | if (log) |
Chaoren Lin | 368c9f6 | 2015-04-27 23:20:30 +0000 | [diff] [blame] | 1399 | log->Printf ("GDBRemoteCommunication::%s failed to delete pipe %s: %s", |
| 1400 | __FUNCTION__, named_pipe_path.c_str(), err.AsCString()); |
Oleksiy Vyalov | d5f8b6a | 2015-01-13 23:19:40 +0000 | [diff] [blame] | 1401 | } |
Greg Clayton | 91a9b247 | 2013-12-04 19:19:12 +0000 | [diff] [blame] | 1402 | } |
Chaoren Lin | 368c9f6 | 2015-04-27 23:20:30 +0000 | [diff] [blame] | 1403 | |
| 1404 | // Make sure we actually connect with the debugserver... |
| 1405 | JoinListenThread(); |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 1406 | } |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1407 | } |
| 1408 | else |
| 1409 | { |
Greg Clayton | 86edbf4 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 1410 | error.SetErrorStringWithFormat ("unable to locate " DEBUGSERVER_BASENAME ); |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1411 | } |
Vince Harron | 8b33567 | 2015-05-12 01:10:56 +0000 | [diff] [blame] | 1412 | |
| 1413 | if (error.Fail()) |
| 1414 | { |
| 1415 | if (log) |
| 1416 | log->Printf ("GDBRemoteCommunication::%s() failed: %s", __FUNCTION__, error.AsCString()); |
| 1417 | } |
| 1418 | |
Greg Clayton | 8b82f08 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 1419 | return error; |
| 1420 | } |
| 1421 | |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 1422 | void |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 1423 | GDBRemoteCommunication::DumpHistory(Stream &strm) |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 1424 | { |
Greg Clayton | d451c1a | 2012-04-13 21:24:18 +0000 | [diff] [blame] | 1425 | m_history.Dump (strm); |
Greg Clayton | c1422c1 | 2012-04-09 22:46:21 +0000 | [diff] [blame] | 1426 | } |
Tamas Berghammer | 912800c | 2015-02-24 10:23:39 +0000 | [diff] [blame] | 1427 | |
| 1428 | GDBRemoteCommunication::ScopedTimeout::ScopedTimeout (GDBRemoteCommunication& gdb_comm, |
| 1429 | uint32_t timeout) : |
| 1430 | m_gdb_comm (gdb_comm) |
| 1431 | { |
| 1432 | m_saved_timeout = m_gdb_comm.SetPacketTimeout (timeout); |
| 1433 | } |
| 1434 | |
| 1435 | GDBRemoteCommunication::ScopedTimeout::~ScopedTimeout () |
| 1436 | { |
| 1437 | m_gdb_comm.SetPacketTimeout (m_saved_timeout); |
| 1438 | } |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 1439 | |
| 1440 | // This function is called via the Communications class read thread when bytes become available |
| 1441 | // for this connection. This function will consume all incoming bytes and try to parse whole |
| 1442 | // packets as they become available. Full packets are placed in a queue, so that all packet |
| 1443 | // requests can simply pop from this queue. Async notification packets will be dispatched |
| 1444 | // immediately to the ProcessGDBRemote Async thread via an event. |
| 1445 | void GDBRemoteCommunication::AppendBytesToCache (const uint8_t * bytes, size_t len, bool broadcast, lldb::ConnectionStatus status) |
| 1446 | { |
| 1447 | StringExtractorGDBRemote packet; |
| 1448 | |
| 1449 | while (true) |
| 1450 | { |
| 1451 | PacketType type = CheckForPacket(bytes, len, packet); |
| 1452 | |
| 1453 | // scrub the data so we do not pass it back to CheckForPacket |
| 1454 | // on future passes of the loop |
| 1455 | bytes = nullptr; |
| 1456 | len = 0; |
| 1457 | |
| 1458 | // we may have received no packet so lets bail out |
| 1459 | if (type == PacketType::Invalid) |
| 1460 | break; |
| 1461 | |
| 1462 | if (type == PacketType::Standard) |
| 1463 | { |
| 1464 | // scope for the mutex |
| 1465 | { |
| 1466 | // lock down the packet queue |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 1467 | std::lock_guard<std::mutex> guard(m_packet_queue_mutex); |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 1468 | // push a new packet into the queue |
| 1469 | m_packet_queue.push(packet); |
| 1470 | // Signal condition variable that we have a packet |
Saleem Abdulrasool | 2d6a9ec | 2016-07-28 17:32:20 +0000 | [diff] [blame] | 1471 | m_condition_queue_not_empty.notify_one(); |
Ewan Crawford | fab40d3 | 2015-06-16 15:50:18 +0000 | [diff] [blame] | 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | if (type == PacketType::Notify) |
| 1476 | { |
| 1477 | // put this packet into an event |
| 1478 | const char *pdata = packet.GetStringRef().c_str(); |
| 1479 | |
| 1480 | // as the communication class, we are a broadcaster and the |
| 1481 | // async thread is tuned to listen to us |
| 1482 | BroadcastEvent( |
| 1483 | eBroadcastBitGdbReadThreadGotNotify, |
| 1484 | new EventDataBytes(pdata)); |
| 1485 | } |
| 1486 | } |
| 1487 | } |