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