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 <cerrno>
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 "llvm/Support/JSON.h"
20 #include <cstring>
21
22 using namespace lldb;
23 using namespace lldb_private;
24 using namespace lldb_private::process_gdb_remote;
25 using namespace llvm;
26
GDBRemoteCommunicationServer(const char * comm_name,const char * listener_name)27 GDBRemoteCommunicationServer::GDBRemoteCommunicationServer(
28 const char *comm_name, const char *listener_name)
29 : GDBRemoteCommunication(comm_name, listener_name), m_exit_now(false) {
30 RegisterPacketHandler(
31 StringExtractorGDBRemote::eServerPacketType_QEnableErrorStrings,
32 [this](StringExtractorGDBRemote packet, Status &error, bool &interrupt,
33 bool &quit) { return this->Handle_QErrorStringEnable(packet); });
34 }
35
36 GDBRemoteCommunicationServer::~GDBRemoteCommunicationServer() = default;
37
RegisterPacketHandler(StringExtractorGDBRemote::ServerPacketType packet_type,PacketHandler handler)38 void GDBRemoteCommunicationServer::RegisterPacketHandler(
39 StringExtractorGDBRemote::ServerPacketType packet_type,
40 PacketHandler handler) {
41 m_packet_handlers[packet_type] = std::move(handler);
42 }
43
44 GDBRemoteCommunication::PacketResult
GetPacketAndSendResponse(Timeout<std::micro> timeout,Status & error,bool & interrupt,bool & quit)45 GDBRemoteCommunicationServer::GetPacketAndSendResponse(
46 Timeout<std::micro> timeout, Status &error, bool &interrupt, bool &quit) {
47 StringExtractorGDBRemote packet;
48
49 PacketResult packet_result = ReadPacket(packet, timeout, false);
50 if (packet_result == PacketResult::Success) {
51 const StringExtractorGDBRemote::ServerPacketType packet_type =
52 packet.GetServerPacketType();
53 switch (packet_type) {
54 case StringExtractorGDBRemote::eServerPacketType_nack:
55 case StringExtractorGDBRemote::eServerPacketType_ack:
56 break;
57
58 case StringExtractorGDBRemote::eServerPacketType_invalid:
59 error.SetErrorString("invalid packet");
60 quit = true;
61 break;
62
63 case StringExtractorGDBRemote::eServerPacketType_unimplemented:
64 packet_result = SendUnimplementedResponse(packet.GetStringRef().data());
65 break;
66
67 default:
68 auto handler_it = m_packet_handlers.find(packet_type);
69 if (handler_it == m_packet_handlers.end())
70 packet_result = SendUnimplementedResponse(packet.GetStringRef().data());
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
SendUnimplementedResponse(const char *)92 GDBRemoteCommunicationServer::SendUnimplementedResponse(const char *) {
93 // TODO: Log the packet we aren't handling...
94 return SendPacketNoLock("");
95 }
96
97 GDBRemoteCommunication::PacketResult
SendErrorResponse(uint8_t err)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
SendErrorResponse(const Status & error)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.PutStringAsRawHex8(error.AsCString());
111 return SendPacketNoLock(packet.GetString());
112 } else
113 return SendErrorResponse(error.GetError());
114 }
115
116 GDBRemoteCommunication::PacketResult
SendErrorResponse(llvm::Error error)117 GDBRemoteCommunicationServer::SendErrorResponse(llvm::Error error) {
118 assert(error);
119 std::unique_ptr<llvm::ErrorInfoBase> EIB;
120 std::unique_ptr<UnimplementedError> UE;
121 llvm::handleAllErrors(
122 std::move(error),
123 [&](std::unique_ptr<UnimplementedError> E) { UE = std::move(E); },
124 [&](std::unique_ptr<llvm::ErrorInfoBase> E) { EIB = std::move(E); });
125
126 if (EIB)
127 return SendErrorResponse(Status(llvm::Error(std::move(EIB))));
128 return SendUnimplementedResponse("");
129 }
130
131 GDBRemoteCommunication::PacketResult
Handle_QErrorStringEnable(StringExtractorGDBRemote & packet)132 GDBRemoteCommunicationServer::Handle_QErrorStringEnable(
133 StringExtractorGDBRemote &packet) {
134 m_send_error_strings = true;
135 return SendOKResponse();
136 }
137
138 GDBRemoteCommunication::PacketResult
SendIllFormedResponse(const StringExtractorGDBRemote & failed_packet,const char * message)139 GDBRemoteCommunicationServer::SendIllFormedResponse(
140 const StringExtractorGDBRemote &failed_packet, const char *message) {
141 Log *log = GetLog(GDBRLog::Packets);
142 LLDB_LOGF(log, "GDBRemoteCommunicationServer::%s: ILLFORMED: '%s' (%s)",
143 __FUNCTION__, failed_packet.GetStringRef().data(),
144 message ? message : "");
145 return SendErrorResponse(0x03);
146 }
147
148 GDBRemoteCommunication::PacketResult
SendOKResponse()149 GDBRemoteCommunicationServer::SendOKResponse() {
150 return SendPacketNoLock("OK");
151 }
152
153 GDBRemoteCommunication::PacketResult
SendJSONResponse(const json::Value & value)154 GDBRemoteCommunicationServer::SendJSONResponse(const json::Value &value) {
155 std::string json_string;
156 raw_string_ostream os(json_string);
157 os << value;
158 os.flush();
159 StreamGDBRemote escaped_response;
160 escaped_response.PutEscapedBytes(json_string.c_str(), json_string.size());
161 return SendPacketNoLock(escaped_response.GetString());
162 }
163
164 GDBRemoteCommunication::PacketResult
SendJSONResponse(Expected<json::Value> value)165 GDBRemoteCommunicationServer::SendJSONResponse(Expected<json::Value> value) {
166 if (!value)
167 return SendErrorResponse(value.takeError());
168 return SendJSONResponse(*value);
169 }
170