1 //===-- GDBRemoteCommunicationServer.cpp ----------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include <errno.h>
10
11 #include "lldb/Host/Config.h"
12
13 #include "GDBRemoteCommunicationServer.h"
14
15 #include "ProcessGDBRemoteLog.h"
16 #include "lldb/Utility/StreamString.h"
17 #include "lldb/Utility/StringExtractorGDBRemote.h"
18 #include "lldb/Utility/UnimplementedError.h"
19 #include <cstring>
20
21 using namespace lldb;
22 using namespace lldb_private;
23 using namespace lldb_private::process_gdb_remote;
24
GDBRemoteCommunicationServer(const char * comm_name,const char * listener_name)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); });
32 }
33
~GDBRemoteCommunicationServer()34 GDBRemoteCommunicationServer::~GDBRemoteCommunicationServer() {}
35
RegisterPacketHandler(StringExtractorGDBRemote::ServerPacketType packet_type,PacketHandler handler)36 void GDBRemoteCommunicationServer::RegisterPacketHandler(
37 StringExtractorGDBRemote::ServerPacketType packet_type,
38 PacketHandler handler) {
39 m_packet_handlers[packet_type] = std::move(handler);
40 }
41
42 GDBRemoteCommunication::PacketResult
GetPacketAndSendResponse(Timeout<std::micro> timeout,Status & error,bool & interrupt,bool & quit)43 GDBRemoteCommunicationServer::GetPacketAndSendResponse(
44 Timeout<std::micro> timeout, Status &error, bool &interrupt, bool &quit) {
45 StringExtractorGDBRemote packet;
46
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:
54 break;
55
56 case StringExtractorGDBRemote::eServerPacketType_invalid:
57 error.SetErrorString("invalid packet");
58 quit = true;
59 break;
60
61 case StringExtractorGDBRemote::eServerPacketType_unimplemented:
62 packet_result = SendUnimplementedResponse(packet.GetStringRef().data());
63 break;
64
65 default:
66 auto handler_it = m_packet_handlers.find(packet_type);
67 if (handler_it == m_packet_handlers.end())
68 packet_result = SendUnimplementedResponse(packet.GetStringRef().data());
69 else
70 packet_result = handler_it->second(packet, error, interrupt, quit);
71 break;
72 }
73 } else {
74 if (!IsConnected()) {
75 error.SetErrorString("lost connection");
76 quit = true;
77 } else {
78 error.SetErrorString("timeout");
79 }
80 }
81
82 // Check if anything occurred that would force us to want to exit.
83 if (m_exit_now)
84 quit = true;
85
86 return packet_result;
87 }
88
89 GDBRemoteCommunication::PacketResult
SendUnimplementedResponse(const char *)90 GDBRemoteCommunicationServer::SendUnimplementedResponse(const char *) {
91 // TODO: Log the packet we aren't handling...
92 return SendPacketNoLock("");
93 }
94
95 GDBRemoteCommunication::PacketResult
SendErrorResponse(uint8_t err)96 GDBRemoteCommunicationServer::SendErrorResponse(uint8_t err) {
97 char packet[16];
98 int packet_len = ::snprintf(packet, sizeof(packet), "E%2.2x", err);
99 assert(packet_len < (int)sizeof(packet));
100 return SendPacketNoLock(llvm::StringRef(packet, packet_len));
101 }
102
103 GDBRemoteCommunication::PacketResult
SendErrorResponse(const Status & error)104 GDBRemoteCommunicationServer::SendErrorResponse(const Status &error) {
105 if (m_send_error_strings) {
106 lldb_private::StreamString packet;
107 packet.Printf("E%2.2x;", static_cast<uint8_t>(error.GetError()));
108 packet.PutStringAsRawHex8(error.AsCString());
109 return SendPacketNoLock(packet.GetString());
110 } else
111 return SendErrorResponse(error.GetError());
112 }
113
114 GDBRemoteCommunication::PacketResult
SendErrorResponse(llvm::Error error)115 GDBRemoteCommunicationServer::SendErrorResponse(llvm::Error error) {
116 assert(error);
117 std::unique_ptr<llvm::ErrorInfoBase> EIB;
118 std::unique_ptr<UnimplementedError> UE;
119 llvm::handleAllErrors(
120 std::move(error),
121 [&](std::unique_ptr<UnimplementedError> E) { UE = std::move(E); },
122 [&](std::unique_ptr<llvm::ErrorInfoBase> E) { EIB = std::move(E); });
123
124 if (EIB)
125 return SendErrorResponse(Status(llvm::Error(std::move(EIB))));
126 return SendUnimplementedResponse("");
127 }
128
129 GDBRemoteCommunication::PacketResult
Handle_QErrorStringEnable(StringExtractorGDBRemote & packet)130 GDBRemoteCommunicationServer::Handle_QErrorStringEnable(
131 StringExtractorGDBRemote &packet) {
132 m_send_error_strings = true;
133 return SendOKResponse();
134 }
135
136 GDBRemoteCommunication::PacketResult
SendIllFormedResponse(const StringExtractorGDBRemote & failed_packet,const char * message)137 GDBRemoteCommunicationServer::SendIllFormedResponse(
138 const StringExtractorGDBRemote &failed_packet, const char *message) {
139 Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PACKETS));
140 LLDB_LOGF(log, "GDBRemoteCommunicationServer::%s: ILLFORMED: '%s' (%s)",
141 __FUNCTION__, failed_packet.GetStringRef().data(),
142 message ? message : "");
143 return SendErrorResponse(0x03);
144 }
145
146 GDBRemoteCommunication::PacketResult
SendOKResponse()147 GDBRemoteCommunicationServer::SendOKResponse() {
148 return SendPacketNoLock("OK");
149 }
150
HandshakeWithClient()151 bool GDBRemoteCommunicationServer::HandshakeWithClient() {
152 return GetAck() == PacketResult::Success;
153 }
154