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