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