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