15ffd83dbSDimitry Andric //===-- GDBRemoteCommunicationServer.cpp ----------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
9fe6060f1SDimitry Andric #include <cerrno>
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #include "lldb/Host/Config.h"
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "GDBRemoteCommunicationServer.h"
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "ProcessGDBRemoteLog.h"
160b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h"
170b57cec5SDimitry Andric #include "lldb/Utility/StringExtractorGDBRemote.h"
18e8d8bef9SDimitry Andric #include "lldb/Utility/UnimplementedError.h"
19fe6060f1SDimitry Andric #include "llvm/Support/JSON.h"
20e8d8bef9SDimitry Andric #include <cstring>
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using namespace lldb;
230b57cec5SDimitry Andric using namespace lldb_private;
240b57cec5SDimitry Andric using namespace lldb_private::process_gdb_remote;
25fe6060f1SDimitry Andric using namespace llvm;
260b57cec5SDimitry Andric 
GDBRemoteCommunicationServer()27*bdd1243dSDimitry Andric GDBRemoteCommunicationServer::GDBRemoteCommunicationServer()
28*bdd1243dSDimitry Andric     : GDBRemoteCommunication(), m_exit_now(false) {
290b57cec5SDimitry Andric   RegisterPacketHandler(
300b57cec5SDimitry Andric       StringExtractorGDBRemote::eServerPacketType_QEnableErrorStrings,
310b57cec5SDimitry Andric       [this](StringExtractorGDBRemote packet, Status &error, bool &interrupt,
320b57cec5SDimitry Andric              bool &quit) { return this->Handle_QErrorStringEnable(packet); });
330b57cec5SDimitry Andric }
340b57cec5SDimitry Andric 
35fe6060f1SDimitry Andric GDBRemoteCommunicationServer::~GDBRemoteCommunicationServer() = default;
360b57cec5SDimitry Andric 
RegisterPacketHandler(StringExtractorGDBRemote::ServerPacketType packet_type,PacketHandler handler)370b57cec5SDimitry Andric void GDBRemoteCommunicationServer::RegisterPacketHandler(
380b57cec5SDimitry Andric     StringExtractorGDBRemote::ServerPacketType packet_type,
390b57cec5SDimitry Andric     PacketHandler handler) {
400b57cec5SDimitry Andric   m_packet_handlers[packet_type] = std::move(handler);
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric GDBRemoteCommunication::PacketResult
GetPacketAndSendResponse(Timeout<std::micro> timeout,Status & error,bool & interrupt,bool & quit)440b57cec5SDimitry Andric GDBRemoteCommunicationServer::GetPacketAndSendResponse(
450b57cec5SDimitry Andric     Timeout<std::micro> timeout, Status &error, bool &interrupt, bool &quit) {
460b57cec5SDimitry Andric   StringExtractorGDBRemote packet;
470b57cec5SDimitry Andric 
484824e7fdSDimitry Andric   PacketResult packet_result = ReadPacket(packet, timeout, false);
490b57cec5SDimitry Andric   if (packet_result == PacketResult::Success) {
500b57cec5SDimitry Andric     const StringExtractorGDBRemote::ServerPacketType packet_type =
510b57cec5SDimitry Andric         packet.GetServerPacketType();
520b57cec5SDimitry Andric     switch (packet_type) {
530b57cec5SDimitry Andric     case StringExtractorGDBRemote::eServerPacketType_nack:
540b57cec5SDimitry Andric     case StringExtractorGDBRemote::eServerPacketType_ack:
550b57cec5SDimitry Andric       break;
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric     case StringExtractorGDBRemote::eServerPacketType_invalid:
580b57cec5SDimitry Andric       error.SetErrorString("invalid packet");
590b57cec5SDimitry Andric       quit = true;
600b57cec5SDimitry Andric       break;
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric     case StringExtractorGDBRemote::eServerPacketType_unimplemented:
639dba64beSDimitry Andric       packet_result = SendUnimplementedResponse(packet.GetStringRef().data());
640b57cec5SDimitry Andric       break;
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric     default:
670b57cec5SDimitry Andric       auto handler_it = m_packet_handlers.find(packet_type);
680b57cec5SDimitry Andric       if (handler_it == m_packet_handlers.end())
699dba64beSDimitry Andric         packet_result = SendUnimplementedResponse(packet.GetStringRef().data());
700b57cec5SDimitry Andric       else
710b57cec5SDimitry Andric         packet_result = handler_it->second(packet, error, interrupt, quit);
720b57cec5SDimitry Andric       break;
730b57cec5SDimitry Andric     }
740b57cec5SDimitry Andric   } else {
750b57cec5SDimitry Andric     if (!IsConnected()) {
760b57cec5SDimitry Andric       error.SetErrorString("lost connection");
770b57cec5SDimitry Andric       quit = true;
780b57cec5SDimitry Andric     } else {
790b57cec5SDimitry Andric       error.SetErrorString("timeout");
800b57cec5SDimitry Andric     }
810b57cec5SDimitry Andric   }
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric   // Check if anything occurred that would force us to want to exit.
840b57cec5SDimitry Andric   if (m_exit_now)
850b57cec5SDimitry Andric     quit = true;
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric   return packet_result;
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric GDBRemoteCommunication::PacketResult
SendUnimplementedResponse(const char *)910b57cec5SDimitry Andric GDBRemoteCommunicationServer::SendUnimplementedResponse(const char *) {
920b57cec5SDimitry Andric   // TODO: Log the packet we aren't handling...
930b57cec5SDimitry Andric   return SendPacketNoLock("");
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric GDBRemoteCommunication::PacketResult
SendErrorResponse(uint8_t err)970b57cec5SDimitry Andric GDBRemoteCommunicationServer::SendErrorResponse(uint8_t err) {
980b57cec5SDimitry Andric   char packet[16];
990b57cec5SDimitry Andric   int packet_len = ::snprintf(packet, sizeof(packet), "E%2.2x", err);
1000b57cec5SDimitry Andric   assert(packet_len < (int)sizeof(packet));
1010b57cec5SDimitry Andric   return SendPacketNoLock(llvm::StringRef(packet, packet_len));
1020b57cec5SDimitry Andric }
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric GDBRemoteCommunication::PacketResult
SendErrorResponse(const Status & error)1050b57cec5SDimitry Andric GDBRemoteCommunicationServer::SendErrorResponse(const Status &error) {
1060b57cec5SDimitry Andric   if (m_send_error_strings) {
1070b57cec5SDimitry Andric     lldb_private::StreamString packet;
1080b57cec5SDimitry Andric     packet.Printf("E%2.2x;", static_cast<uint8_t>(error.GetError()));
1090b57cec5SDimitry Andric     packet.PutStringAsRawHex8(error.AsCString());
1100b57cec5SDimitry Andric     return SendPacketNoLock(packet.GetString());
1110b57cec5SDimitry Andric   } else
1120b57cec5SDimitry Andric     return SendErrorResponse(error.GetError());
1130b57cec5SDimitry Andric }
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric GDBRemoteCommunication::PacketResult
SendErrorResponse(llvm::Error error)1160b57cec5SDimitry Andric GDBRemoteCommunicationServer::SendErrorResponse(llvm::Error error) {
117e8d8bef9SDimitry Andric   assert(error);
1180b57cec5SDimitry Andric   std::unique_ptr<llvm::ErrorInfoBase> EIB;
119e8d8bef9SDimitry Andric   std::unique_ptr<UnimplementedError> UE;
1200b57cec5SDimitry Andric   llvm::handleAllErrors(
1210b57cec5SDimitry Andric       std::move(error),
122e8d8bef9SDimitry Andric       [&](std::unique_ptr<UnimplementedError> E) { UE = std::move(E); },
1230b57cec5SDimitry Andric       [&](std::unique_ptr<llvm::ErrorInfoBase> E) { EIB = std::move(E); });
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric   if (EIB)
1260b57cec5SDimitry Andric     return SendErrorResponse(Status(llvm::Error(std::move(EIB))));
127e8d8bef9SDimitry Andric   return SendUnimplementedResponse("");
1280b57cec5SDimitry Andric }
1290b57cec5SDimitry Andric 
1300b57cec5SDimitry Andric GDBRemoteCommunication::PacketResult
Handle_QErrorStringEnable(StringExtractorGDBRemote & packet)1310b57cec5SDimitry Andric GDBRemoteCommunicationServer::Handle_QErrorStringEnable(
1320b57cec5SDimitry Andric     StringExtractorGDBRemote &packet) {
1330b57cec5SDimitry Andric   m_send_error_strings = true;
1340b57cec5SDimitry Andric   return SendOKResponse();
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric GDBRemoteCommunication::PacketResult
SendIllFormedResponse(const StringExtractorGDBRemote & failed_packet,const char * message)1380b57cec5SDimitry Andric GDBRemoteCommunicationServer::SendIllFormedResponse(
1390b57cec5SDimitry Andric     const StringExtractorGDBRemote &failed_packet, const char *message) {
1401fd87a68SDimitry Andric   Log *log = GetLog(GDBRLog::Packets);
1419dba64beSDimitry Andric   LLDB_LOGF(log, "GDBRemoteCommunicationServer::%s: ILLFORMED: '%s' (%s)",
1429dba64beSDimitry Andric             __FUNCTION__, failed_packet.GetStringRef().data(),
1430b57cec5SDimitry Andric             message ? message : "");
1440b57cec5SDimitry Andric   return SendErrorResponse(0x03);
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric GDBRemoteCommunication::PacketResult
SendOKResponse()1480b57cec5SDimitry Andric GDBRemoteCommunicationServer::SendOKResponse() {
1490b57cec5SDimitry Andric   return SendPacketNoLock("OK");
1500b57cec5SDimitry Andric }
1510b57cec5SDimitry Andric 
152fe6060f1SDimitry Andric GDBRemoteCommunication::PacketResult
SendJSONResponse(const json::Value & value)153fe6060f1SDimitry Andric GDBRemoteCommunicationServer::SendJSONResponse(const json::Value &value) {
154fe6060f1SDimitry Andric   std::string json_string;
155fe6060f1SDimitry Andric   raw_string_ostream os(json_string);
156fe6060f1SDimitry Andric   os << value;
157fe6060f1SDimitry Andric   os.flush();
158fe6060f1SDimitry Andric   StreamGDBRemote escaped_response;
159fe6060f1SDimitry Andric   escaped_response.PutEscapedBytes(json_string.c_str(), json_string.size());
160fe6060f1SDimitry Andric   return SendPacketNoLock(escaped_response.GetString());
161fe6060f1SDimitry Andric }
162fe6060f1SDimitry Andric 
163fe6060f1SDimitry Andric GDBRemoteCommunication::PacketResult
SendJSONResponse(Expected<json::Value> value)164fe6060f1SDimitry Andric GDBRemoteCommunicationServer::SendJSONResponse(Expected<json::Value> value) {
165fe6060f1SDimitry Andric   if (!value)
166fe6060f1SDimitry Andric     return SendErrorResponse(value.takeError());
167fe6060f1SDimitry Andric   return SendJSONResponse(*value);
168fe6060f1SDimitry Andric }
169