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