]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / source / Plugins / Process / gdb-remote / GDBRemoteCommunicationServer.cpp
1 //===-- GDBRemoteCommunicationServer.cpp ------------------------*- C++ -*-===//
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 <cstring>
16
17 #include "ProcessGDBRemoteLog.h"
18 #include "lldb/Utility/StreamString.h"
19 #include "lldb/Utility/StringExtractorGDBRemote.h"
20
21 using namespace lldb;
22 using namespace lldb_private;
23 using namespace lldb_private::process_gdb_remote;
24
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
34 GDBRemoteCommunicationServer::~GDBRemoteCommunicationServer() {}
35
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
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().c_str());
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 =
69             SendUnimplementedResponse(packet.GetStringRef().c_str());
70       else
71         packet_result = handler_it->second(packet, error, interrupt, quit);
72       break;
73     }
74   } else {
75     if (!IsConnected()) {
76       error.SetErrorString("lost connection");
77       quit = true;
78     } else {
79       error.SetErrorString("timeout");
80     }
81   }
82
83   // Check if anything occurred that would force us to want to exit.
84   if (m_exit_now)
85     quit = true;
86
87   return packet_result;
88 }
89
90 GDBRemoteCommunication::PacketResult
91 GDBRemoteCommunicationServer::SendUnimplementedResponse(const char *) {
92   // TODO: Log the packet we aren't handling...
93   return SendPacketNoLock("");
94 }
95
96 GDBRemoteCommunication::PacketResult
97 GDBRemoteCommunicationServer::SendErrorResponse(uint8_t err) {
98   char packet[16];
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));
102 }
103
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());
111   } else
112     return SendErrorResponse(error.GetError());
113 }
114
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(
120       std::move(error),
121       [&](std::unique_ptr<PacketUnimplementedError> E) { PUE = 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   if (PUE)
127     return SendUnimplementedResponse(PUE->message().c_str());
128   return SendErrorResponse(Status("Unknown Error"));
129 }
130
131 GDBRemoteCommunication::PacketResult
132 GDBRemoteCommunicationServer::Handle_QErrorStringEnable(
133     StringExtractorGDBRemote &packet) {
134   m_send_error_strings = true;
135   return SendOKResponse();
136 }
137
138 GDBRemoteCommunication::PacketResult
139 GDBRemoteCommunicationServer::SendIllFormedResponse(
140     const StringExtractorGDBRemote &failed_packet, const char *message) {
141   Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(GDBR_LOG_PACKETS));
142   if (log)
143     log->Printf("GDBRemoteCommunicationServer::%s: ILLFORMED: '%s' (%s)",
144                 __FUNCTION__, failed_packet.GetStringRef().c_str(),
145                 message ? message : "");
146   return SendErrorResponse(0x03);
147 }
148
149 GDBRemoteCommunication::PacketResult
150 GDBRemoteCommunicationServer::SendOKResponse() {
151   return SendPacketNoLock("OK");
152 }
153
154 bool GDBRemoteCommunicationServer::HandshakeWithClient() {
155   return GetAck() == PacketResult::Success;
156 }
157
158 char PacketUnimplementedError::ID;