1 //===-- GDBRemoteCommunicationServer.cpp ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "lldb/Host/Config.h"
14 #include "GDBRemoteCommunicationServer.h"
21 #include "ProcessGDBRemoteLog.h"
22 #include "Utility/StringExtractorGDBRemote.h"
25 using namespace lldb_private;
26 using namespace lldb_private::process_gdb_remote;
28 GDBRemoteCommunicationServer::GDBRemoteCommunicationServer(const char *comm_name,
29 const char *listener_name) :
30 GDBRemoteCommunication (comm_name, listener_name),
35 GDBRemoteCommunicationServer::~GDBRemoteCommunicationServer()
39 void GDBRemoteCommunicationServer::RegisterPacketHandler(
40 StringExtractorGDBRemote::ServerPacketType packet_type,
41 PacketHandler handler)
43 m_packet_handlers[packet_type] = std::move(handler);
46 GDBRemoteCommunication::PacketResult
47 GDBRemoteCommunicationServer::GetPacketAndSendResponse (uint32_t timeout_usec,
52 StringExtractorGDBRemote packet;
54 PacketResult packet_result = WaitForPacketWithTimeoutMicroSecondsNoLock (packet, timeout_usec, false);
55 if (packet_result == PacketResult::Success)
57 const StringExtractorGDBRemote::ServerPacketType packet_type = packet.GetServerPacketType ();
60 case StringExtractorGDBRemote::eServerPacketType_nack:
61 case StringExtractorGDBRemote::eServerPacketType_ack:
64 case StringExtractorGDBRemote::eServerPacketType_invalid:
65 error.SetErrorString("invalid packet");
69 case StringExtractorGDBRemote::eServerPacketType_unimplemented:
70 packet_result = SendUnimplementedResponse (packet.GetStringRef().c_str());
74 auto handler_it = m_packet_handlers.find(packet_type);
75 if (handler_it == m_packet_handlers.end())
76 packet_result = SendUnimplementedResponse (packet.GetStringRef().c_str());
78 packet_result = handler_it->second (packet, error, interrupt, quit);
86 error.SetErrorString("lost connection");
91 error.SetErrorString("timeout");
95 // Check if anything occurred that would force us to want to exit.
102 GDBRemoteCommunication::PacketResult
103 GDBRemoteCommunicationServer::SendUnimplementedResponse (const char *)
105 // TODO: Log the packet we aren't handling...
106 return SendPacketNoLock ("", 0);
110 GDBRemoteCommunication::PacketResult
111 GDBRemoteCommunicationServer::SendErrorResponse (uint8_t err)
114 int packet_len = ::snprintf (packet, sizeof(packet), "E%2.2x", err);
115 assert (packet_len < (int)sizeof(packet));
116 return SendPacketNoLock (packet, packet_len);
119 GDBRemoteCommunication::PacketResult
120 GDBRemoteCommunicationServer::SendIllFormedResponse (const StringExtractorGDBRemote &failed_packet, const char *message)
122 Log *log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PACKETS));
124 log->Printf ("GDBRemoteCommunicationServer::%s: ILLFORMED: '%s' (%s)", __FUNCTION__, failed_packet.GetStringRef ().c_str (), message ? message : "");
125 return SendErrorResponse (0x03);
128 GDBRemoteCommunication::PacketResult
129 GDBRemoteCommunicationServer::SendOKResponse ()
131 return SendPacketNoLock ("OK", 2);
135 GDBRemoteCommunicationServer::HandshakeWithClient()
137 return GetAck() == PacketResult::Success;