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