1 //===-- GDBRemoteCommunicationServer.cpp ------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
11 #include "lldb/Host/Config.h"
13 #include "GDBRemoteCommunicationServer.h"
17 #include "ProcessGDBRemoteLog.h"
18 #include "lldb/Utility/StreamString.h"
19 #include "lldb/Utility/StringExtractorGDBRemote.h"
22 using namespace lldb_private;
23 using namespace lldb_private::process_gdb_remote;
25 GDBRemoteCommunicationServer::GDBRemoteCommunicationServer(
26 const char *comm_name, const char *listener_name)
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); });
34 GDBRemoteCommunicationServer::~GDBRemoteCommunicationServer() {}
36 void GDBRemoteCommunicationServer::RegisterPacketHandler(
37 StringExtractorGDBRemote::ServerPacketType packet_type,
38 PacketHandler handler) {
39 m_packet_handlers[packet_type] = std::move(handler);
42 GDBRemoteCommunication::PacketResult
43 GDBRemoteCommunicationServer::GetPacketAndSendResponse(
44 Timeout<std::micro> timeout, Status &error, bool &interrupt, bool &quit) {
45 StringExtractorGDBRemote packet;
47 PacketResult packet_result = WaitForPacketNoLock(packet, timeout, false);
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:
56 case StringExtractorGDBRemote::eServerPacketType_invalid:
57 error.SetErrorString("invalid packet");
61 case StringExtractorGDBRemote::eServerPacketType_unimplemented:
62 packet_result = SendUnimplementedResponse(packet.GetStringRef().c_str());
66 auto handler_it = m_packet_handlers.find(packet_type);
67 if (handler_it == m_packet_handlers.end())
69 SendUnimplementedResponse(packet.GetStringRef().c_str());
71 packet_result = handler_it->second(packet, error, interrupt, quit);
76 error.SetErrorString("lost connection");
79 error.SetErrorString("timeout");
83 // Check if anything occurred that would force us to want to exit.
90 GDBRemoteCommunication::PacketResult
91 GDBRemoteCommunicationServer::SendUnimplementedResponse(const char *) {
92 // TODO: Log the packet we aren't handling...
93 return SendPacketNoLock("");
96 GDBRemoteCommunication::PacketResult
97 GDBRemoteCommunicationServer::SendErrorResponse(uint8_t err) {
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));
104 GDBRemoteCommunication::PacketResult
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.PutStringAsRawHex8(error.AsCString());
110 return SendPacketNoLock(packet.GetString());
112 return SendErrorResponse(error.GetError());
115 GDBRemoteCommunication::PacketResult
116 GDBRemoteCommunicationServer::SendErrorResponse(llvm::Error error) {
117 std::unique_ptr<llvm::ErrorInfoBase> EIB;
118 std::unique_ptr<PacketUnimplementedError> PUE;
119 llvm::handleAllErrors(
121 [&](std::unique_ptr<PacketUnimplementedError> E) { PUE = std::move(E); },
122 [&](std::unique_ptr<llvm::ErrorInfoBase> E) { EIB = std::move(E); });
125 return SendErrorResponse(Status(llvm::Error(std::move(EIB))));
127 return SendUnimplementedResponse(PUE->message().c_str());
128 return SendErrorResponse(Status("Unknown Error"));
131 GDBRemoteCommunication::PacketResult
132 GDBRemoteCommunicationServer::Handle_QErrorStringEnable(
133 StringExtractorGDBRemote &packet) {
134 m_send_error_strings = true;
135 return SendOKResponse();
138 GDBRemoteCommunication::PacketResult
139 GDBRemoteCommunicationServer::SendIllFormedResponse(
140 const StringExtractorGDBRemote &failed_packet, const char *message) {
141 Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PACKETS));
143 log->Printf("GDBRemoteCommunicationServer::%s: ILLFORMED: '%s' (%s)",
144 __FUNCTION__, failed_packet.GetStringRef().c_str(),
145 message ? message : "");
146 return SendErrorResponse(0x03);
149 GDBRemoteCommunication::PacketResult
150 GDBRemoteCommunicationServer::SendOKResponse() {
151 return SendPacketNoLock("OK");
154 bool GDBRemoteCommunicationServer::HandshakeWithClient() {
155 return GetAck() == PacketResult::Success;
158 char PacketUnimplementedError::ID;