1dda28197Spatrick //===-- GDBRemoteCommunicationServer.cpp ----------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9be691f3bSpatrick #include <cerrno>
10061da546Spatrick
11061da546Spatrick #include "lldb/Host/Config.h"
12061da546Spatrick
13061da546Spatrick #include "GDBRemoteCommunicationServer.h"
14061da546Spatrick
15061da546Spatrick #include "ProcessGDBRemoteLog.h"
16061da546Spatrick #include "lldb/Utility/StreamString.h"
17061da546Spatrick #include "lldb/Utility/StringExtractorGDBRemote.h"
18be691f3bSpatrick #include "lldb/Utility/UnimplementedError.h"
19be691f3bSpatrick #include "llvm/Support/JSON.h"
20be691f3bSpatrick #include <cstring>
21061da546Spatrick
22061da546Spatrick using namespace lldb;
23061da546Spatrick using namespace lldb_private;
24061da546Spatrick using namespace lldb_private::process_gdb_remote;
25be691f3bSpatrick using namespace llvm;
26061da546Spatrick
GDBRemoteCommunicationServer()27*f6aab3d8Srobert GDBRemoteCommunicationServer::GDBRemoteCommunicationServer()
28*f6aab3d8Srobert : GDBRemoteCommunication(), m_exit_now(false) {
29061da546Spatrick RegisterPacketHandler(
30061da546Spatrick StringExtractorGDBRemote::eServerPacketType_QEnableErrorStrings,
31061da546Spatrick [this](StringExtractorGDBRemote packet, Status &error, bool &interrupt,
32061da546Spatrick bool &quit) { return this->Handle_QErrorStringEnable(packet); });
33061da546Spatrick }
34061da546Spatrick
35be691f3bSpatrick GDBRemoteCommunicationServer::~GDBRemoteCommunicationServer() = default;
36061da546Spatrick
RegisterPacketHandler(StringExtractorGDBRemote::ServerPacketType packet_type,PacketHandler handler)37061da546Spatrick void GDBRemoteCommunicationServer::RegisterPacketHandler(
38061da546Spatrick StringExtractorGDBRemote::ServerPacketType packet_type,
39061da546Spatrick PacketHandler handler) {
40061da546Spatrick m_packet_handlers[packet_type] = std::move(handler);
41061da546Spatrick }
42061da546Spatrick
43061da546Spatrick GDBRemoteCommunication::PacketResult
GetPacketAndSendResponse(Timeout<std::micro> timeout,Status & error,bool & interrupt,bool & quit)44061da546Spatrick GDBRemoteCommunicationServer::GetPacketAndSendResponse(
45061da546Spatrick Timeout<std::micro> timeout, Status &error, bool &interrupt, bool &quit) {
46061da546Spatrick StringExtractorGDBRemote packet;
47061da546Spatrick
48*f6aab3d8Srobert PacketResult packet_result = ReadPacket(packet, timeout, false);
49061da546Spatrick if (packet_result == PacketResult::Success) {
50061da546Spatrick const StringExtractorGDBRemote::ServerPacketType packet_type =
51061da546Spatrick packet.GetServerPacketType();
52061da546Spatrick switch (packet_type) {
53061da546Spatrick case StringExtractorGDBRemote::eServerPacketType_nack:
54061da546Spatrick case StringExtractorGDBRemote::eServerPacketType_ack:
55061da546Spatrick break;
56061da546Spatrick
57061da546Spatrick case StringExtractorGDBRemote::eServerPacketType_invalid:
58061da546Spatrick error.SetErrorString("invalid packet");
59061da546Spatrick quit = true;
60061da546Spatrick break;
61061da546Spatrick
62061da546Spatrick case StringExtractorGDBRemote::eServerPacketType_unimplemented:
63061da546Spatrick packet_result = SendUnimplementedResponse(packet.GetStringRef().data());
64061da546Spatrick break;
65061da546Spatrick
66061da546Spatrick default:
67061da546Spatrick auto handler_it = m_packet_handlers.find(packet_type);
68061da546Spatrick if (handler_it == m_packet_handlers.end())
69061da546Spatrick packet_result = SendUnimplementedResponse(packet.GetStringRef().data());
70061da546Spatrick else
71061da546Spatrick packet_result = handler_it->second(packet, error, interrupt, quit);
72061da546Spatrick break;
73061da546Spatrick }
74061da546Spatrick } else {
75061da546Spatrick if (!IsConnected()) {
76061da546Spatrick error.SetErrorString("lost connection");
77061da546Spatrick quit = true;
78061da546Spatrick } else {
79061da546Spatrick error.SetErrorString("timeout");
80061da546Spatrick }
81061da546Spatrick }
82061da546Spatrick
83061da546Spatrick // Check if anything occurred that would force us to want to exit.
84061da546Spatrick if (m_exit_now)
85061da546Spatrick quit = true;
86061da546Spatrick
87061da546Spatrick return packet_result;
88061da546Spatrick }
89061da546Spatrick
90061da546Spatrick GDBRemoteCommunication::PacketResult
SendUnimplementedResponse(const char *)91061da546Spatrick GDBRemoteCommunicationServer::SendUnimplementedResponse(const char *) {
92061da546Spatrick // TODO: Log the packet we aren't handling...
93061da546Spatrick return SendPacketNoLock("");
94061da546Spatrick }
95061da546Spatrick
96061da546Spatrick GDBRemoteCommunication::PacketResult
SendErrorResponse(uint8_t err)97061da546Spatrick GDBRemoteCommunicationServer::SendErrorResponse(uint8_t err) {
98061da546Spatrick char packet[16];
99061da546Spatrick int packet_len = ::snprintf(packet, sizeof(packet), "E%2.2x", err);
100061da546Spatrick assert(packet_len < (int)sizeof(packet));
101061da546Spatrick return SendPacketNoLock(llvm::StringRef(packet, packet_len));
102061da546Spatrick }
103061da546Spatrick
104061da546Spatrick GDBRemoteCommunication::PacketResult
SendErrorResponse(const Status & error)105061da546Spatrick GDBRemoteCommunicationServer::SendErrorResponse(const Status &error) {
106061da546Spatrick if (m_send_error_strings) {
107061da546Spatrick lldb_private::StreamString packet;
108061da546Spatrick packet.Printf("E%2.2x;", static_cast<uint8_t>(error.GetError()));
109061da546Spatrick packet.PutStringAsRawHex8(error.AsCString());
110061da546Spatrick return SendPacketNoLock(packet.GetString());
111061da546Spatrick } else
112061da546Spatrick return SendErrorResponse(error.GetError());
113061da546Spatrick }
114061da546Spatrick
115061da546Spatrick GDBRemoteCommunication::PacketResult
SendErrorResponse(llvm::Error error)116061da546Spatrick GDBRemoteCommunicationServer::SendErrorResponse(llvm::Error error) {
117be691f3bSpatrick assert(error);
118061da546Spatrick std::unique_ptr<llvm::ErrorInfoBase> EIB;
119be691f3bSpatrick std::unique_ptr<UnimplementedError> UE;
120061da546Spatrick llvm::handleAllErrors(
121061da546Spatrick std::move(error),
122be691f3bSpatrick [&](std::unique_ptr<UnimplementedError> E) { UE = std::move(E); },
123061da546Spatrick [&](std::unique_ptr<llvm::ErrorInfoBase> E) { EIB = std::move(E); });
124061da546Spatrick
125061da546Spatrick if (EIB)
126061da546Spatrick return SendErrorResponse(Status(llvm::Error(std::move(EIB))));
127be691f3bSpatrick return SendUnimplementedResponse("");
128061da546Spatrick }
129061da546Spatrick
130061da546Spatrick GDBRemoteCommunication::PacketResult
Handle_QErrorStringEnable(StringExtractorGDBRemote & packet)131061da546Spatrick GDBRemoteCommunicationServer::Handle_QErrorStringEnable(
132061da546Spatrick StringExtractorGDBRemote &packet) {
133061da546Spatrick m_send_error_strings = true;
134061da546Spatrick return SendOKResponse();
135061da546Spatrick }
136061da546Spatrick
137061da546Spatrick GDBRemoteCommunication::PacketResult
SendIllFormedResponse(const StringExtractorGDBRemote & failed_packet,const char * message)138061da546Spatrick GDBRemoteCommunicationServer::SendIllFormedResponse(
139061da546Spatrick const StringExtractorGDBRemote &failed_packet, const char *message) {
140*f6aab3d8Srobert Log *log = GetLog(GDBRLog::Packets);
141061da546Spatrick LLDB_LOGF(log, "GDBRemoteCommunicationServer::%s: ILLFORMED: '%s' (%s)",
142061da546Spatrick __FUNCTION__, failed_packet.GetStringRef().data(),
143061da546Spatrick message ? message : "");
144061da546Spatrick return SendErrorResponse(0x03);
145061da546Spatrick }
146061da546Spatrick
147061da546Spatrick GDBRemoteCommunication::PacketResult
SendOKResponse()148061da546Spatrick GDBRemoteCommunicationServer::SendOKResponse() {
149061da546Spatrick return SendPacketNoLock("OK");
150061da546Spatrick }
151061da546Spatrick
152be691f3bSpatrick GDBRemoteCommunication::PacketResult
SendJSONResponse(const json::Value & value)153be691f3bSpatrick GDBRemoteCommunicationServer::SendJSONResponse(const json::Value &value) {
154be691f3bSpatrick std::string json_string;
155be691f3bSpatrick raw_string_ostream os(json_string);
156be691f3bSpatrick os << value;
157be691f3bSpatrick os.flush();
158be691f3bSpatrick StreamGDBRemote escaped_response;
159be691f3bSpatrick escaped_response.PutEscapedBytes(json_string.c_str(), json_string.size());
160be691f3bSpatrick return SendPacketNoLock(escaped_response.GetString());
161be691f3bSpatrick }
162be691f3bSpatrick
163be691f3bSpatrick GDBRemoteCommunication::PacketResult
SendJSONResponse(Expected<json::Value> value)164be691f3bSpatrick GDBRemoteCommunicationServer::SendJSONResponse(Expected<json::Value> value) {
165be691f3bSpatrick if (!value)
166be691f3bSpatrick return SendErrorResponse(value.takeError());
167be691f3bSpatrick return SendJSONResponse(*value);
168be691f3bSpatrick }
169