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 10 #include <errno.h> 11 12 #include "lldb/Host/Config.h" 13 14 #include "GDBRemoteCommunicationServer.h" 15 16 #include <cstring> 17 18 #include "ProcessGDBRemoteLog.h" 19 #include "lldb/Utility/StreamString.h" 20 #include "lldb/Utility/StringExtractorGDBRemote.h" 21 22 using namespace lldb; 23 using namespace lldb_private; 24 using namespace lldb_private::process_gdb_remote; 25 26 GDBRemoteCommunicationServer::GDBRemoteCommunicationServer( 27 const char *comm_name, const char *listener_name) 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 } 34 35 GDBRemoteCommunicationServer::~GDBRemoteCommunicationServer() {} 36 37 void GDBRemoteCommunicationServer::RegisterPacketHandler( 38 StringExtractorGDBRemote::ServerPacketType packet_type, 39 PacketHandler handler) { 40 m_packet_handlers[packet_type] = std::move(handler); 41 } 42 43 GDBRemoteCommunication::PacketResult 44 GDBRemoteCommunicationServer::GetPacketAndSendResponse( 45 Timeout<std::micro> timeout, Status &error, bool &interrupt, bool &quit) { 46 StringExtractorGDBRemote packet; 47 48 PacketResult packet_result = WaitForPacketNoLock(packet, timeout, false); 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; 56 57 case StringExtractorGDBRemote::eServerPacketType_invalid: 58 error.SetErrorString("invalid packet"); 59 quit = true; 60 break; 61 62 case StringExtractorGDBRemote::eServerPacketType_unimplemented: 63 packet_result = SendUnimplementedResponse(packet.GetStringRef().c_str()); 64 break; 65 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; 74 } 75 } else { 76 if (!IsConnected()) { 77 error.SetErrorString("lost connection"); 78 quit = true; 79 } else { 80 error.SetErrorString("timeout"); 81 } 82 } 83 84 // Check if anything occurred that would force us to want to exit. 85 if (m_exit_now) 86 quit = true; 87 88 return packet_result; 89 } 90 91 GDBRemoteCommunication::PacketResult 92 GDBRemoteCommunicationServer::SendUnimplementedResponse(const char *) { 93 // TODO: Log the packet we aren't handling... 94 return SendPacketNoLock(""); 95 } 96 97 GDBRemoteCommunication::PacketResult 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)); 103 } 104 105 GDBRemoteCommunication::PacketResult 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 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); 132 } 133 134 GDBRemoteCommunication::PacketResult 135 GDBRemoteCommunicationServer::SendOKResponse() { 136 return SendPacketNoLock("OK"); 137 } 138 139 bool GDBRemoteCommunicationServer::HandshakeWithClient() { 140 return GetAck() == PacketResult::Success; 141 } 142