Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 1 | //===-- GDBRemoteCommunicationServer.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 |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Sylvestre Ledru | d28b993 | 2013-09-28 15:23:41 +0000 | [diff] [blame] | 9 | #include <errno.h> |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 10 | |
Zachary Turner | 0ec7baa | 2014-07-01 00:18:46 +0000 | [diff] [blame] | 11 | #include "lldb/Host/Config.h" |
| 12 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 13 | #include "GDBRemoteCommunicationServer.h" |
| 14 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 15 | #include <cstring> |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 16 | |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 17 | #include "ProcessGDBRemoteLog.h" |
Ravitheja Addepally | dab1d5f | 2017-07-12 11:15:34 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/StreamString.h" |
Pavel Labath | 9af71b3 | 2018-03-20 16:14:00 +0000 | [diff] [blame] | 19 | #include "lldb/Utility/StringExtractorGDBRemote.h" |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace lldb; |
| 22 | using namespace lldb_private; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 23 | using namespace lldb_private::process_gdb_remote; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 24 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | GDBRemoteCommunicationServer::GDBRemoteCommunicationServer( |
| 26 | const char *comm_name, const char *listener_name) |
Ravitheja Addepally | dab1d5f | 2017-07-12 11:15:34 +0000 | [diff] [blame] | 27 | : GDBRemoteCommunication(comm_name, listener_name), m_exit_now(false) { |
| 28 | RegisterPacketHandler( |
| 29 | StringExtractorGDBRemote::eServerPacketType_QEnableErrorStrings, |
| 30 | [this](StringExtractorGDBRemote packet, Status &error, bool &interrupt, |
| 31 | bool &quit) { return this->Handle_QErrorStringEnable(packet); }); |
| 32 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 33 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 34 | GDBRemoteCommunicationServer::~GDBRemoteCommunicationServer() {} |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 35 | |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 36 | void GDBRemoteCommunicationServer::RegisterPacketHandler( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | StringExtractorGDBRemote::ServerPacketType packet_type, |
| 38 | PacketHandler handler) { |
| 39 | m_packet_handlers[packet_type] = std::move(handler); |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 42 | GDBRemoteCommunication::PacketResult |
Pavel Labath | 1eff73c | 2016-11-24 10:54:49 +0000 | [diff] [blame] | 43 | GDBRemoteCommunicationServer::GetPacketAndSendResponse( |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 44 | Timeout<std::micro> timeout, Status &error, bool &interrupt, bool &quit) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 45 | StringExtractorGDBRemote packet; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 46 | |
Pavel Labath | 1eff73c | 2016-11-24 10:54:49 +0000 | [diff] [blame] | 47 | PacketResult packet_result = WaitForPacketNoLock(packet, timeout, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 48 | if (packet_result == PacketResult::Success) { |
| 49 | const StringExtractorGDBRemote::ServerPacketType packet_type = |
| 50 | packet.GetServerPacketType(); |
| 51 | switch (packet_type) { |
| 52 | case StringExtractorGDBRemote::eServerPacketType_nack: |
| 53 | case StringExtractorGDBRemote::eServerPacketType_ack: |
| 54 | break; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 55 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | case StringExtractorGDBRemote::eServerPacketType_invalid: |
| 57 | error.SetErrorString("invalid packet"); |
| 58 | quit = true; |
| 59 | break; |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 60 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | case StringExtractorGDBRemote::eServerPacketType_unimplemented: |
| 62 | packet_result = SendUnimplementedResponse(packet.GetStringRef().c_str()); |
| 63 | break; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 64 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 65 | default: |
| 66 | auto handler_it = m_packet_handlers.find(packet_type); |
| 67 | if (handler_it == m_packet_handlers.end()) |
| 68 | packet_result = |
| 69 | SendUnimplementedResponse(packet.GetStringRef().c_str()); |
| 70 | else |
| 71 | packet_result = handler_it->second(packet, error, interrupt, quit); |
| 72 | break; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 73 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | } else { |
| 75 | if (!IsConnected()) { |
| 76 | error.SetErrorString("lost connection"); |
| 77 | quit = true; |
| 78 | } else { |
| 79 | error.SetErrorString("timeout"); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 80 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 81 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 82 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 83 | // Check if anything occurred that would force us to want to exit. |
| 84 | if (m_exit_now) |
| 85 | quit = true; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 86 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | return packet_result; |
Greg Clayton | 576d883 | 2011-03-22 04:00:09 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 90 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | GDBRemoteCommunicationServer::SendUnimplementedResponse(const char *) { |
| 92 | // TODO: Log the packet we aren't handling... |
| 93 | return SendPacketNoLock(""); |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 96 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 97 | GDBRemoteCommunicationServer::SendErrorResponse(uint8_t err) { |
| 98 | char packet[16]; |
| 99 | int packet_len = ::snprintf(packet, sizeof(packet), "E%2.2x", err); |
| 100 | assert(packet_len < (int)sizeof(packet)); |
| 101 | return SendPacketNoLock(llvm::StringRef(packet, packet_len)); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 102 | } |
Greg Clayton | 32e0a75 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 103 | |
Greg Clayton | 3dedae1 | 2013-12-06 21:45:27 +0000 | [diff] [blame] | 104 | GDBRemoteCommunication::PacketResult |
Ravitheja Addepally | dab1d5f | 2017-07-12 11:15:34 +0000 | [diff] [blame] | 105 | GDBRemoteCommunicationServer::SendErrorResponse(const Status &error) { |
| 106 | if (m_send_error_strings) { |
| 107 | lldb_private::StreamString packet; |
| 108 | packet.Printf("E%2.2x;", static_cast<uint8_t>(error.GetError())); |
| 109 | packet.PutCStringAsRawHex8(error.AsCString()); |
| 110 | return SendPacketNoLock(packet.GetString()); |
| 111 | } else |
| 112 | return SendErrorResponse(error.GetError()); |
| 113 | } |
| 114 | |
| 115 | GDBRemoteCommunication::PacketResult |
| 116 | GDBRemoteCommunicationServer::Handle_QErrorStringEnable( |
| 117 | StringExtractorGDBRemote &packet) { |
| 118 | m_send_error_strings = true; |
| 119 | return SendOKResponse(); |
| 120 | } |
| 121 | |
| 122 | GDBRemoteCommunication::PacketResult |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 123 | GDBRemoteCommunicationServer::SendIllFormedResponse( |
| 124 | const StringExtractorGDBRemote &failed_packet, const char *message) { |
| 125 | Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PACKETS)); |
| 126 | if (log) |
| 127 | log->Printf("GDBRemoteCommunicationServer::%s: ILLFORMED: '%s' (%s)", |
| 128 | __FUNCTION__, failed_packet.GetStringRef().c_str(), |
| 129 | message ? message : ""); |
| 130 | return SendErrorResponse(0x03); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 133 | GDBRemoteCommunication::PacketResult |
| 134 | GDBRemoteCommunicationServer::SendOKResponse() { |
| 135 | return SendPacketNoLock("OK"); |
| 136 | } |
| 137 | |
| 138 | bool GDBRemoteCommunicationServer::HandshakeWithClient() { |
| 139 | return GetAck() == PacketResult::Success; |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 140 | } |