Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/llvm-project/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.cpp
39642 views
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
27
GDBRemoteCommunicationServer::GDBRemoteCommunicationServer()
28
: GDBRemoteCommunication(), m_exit_now(false) {
29
RegisterPacketHandler(
30
StringExtractorGDBRemote::eServerPacketType_QEnableErrorStrings,
31
[this](StringExtractorGDBRemote packet, Status &error, bool &interrupt,
32
bool &quit) { return this->Handle_QErrorStringEnable(packet); });
33
}
34
35
GDBRemoteCommunicationServer::~GDBRemoteCommunicationServer() = default;
36
37
void GDBRemoteCommunicationServer::RegisterPacketHandler(
38
StringExtractorGDBRemote::ServerPacketType packet_type,
39
PacketHandler handler) {
40
m_packet_handlers[packet_type] = std::move(handler);
41
}
42
43
GDBRemoteCommunication::PacketResult
44
GDBRemoteCommunicationServer::GetPacketAndSendResponse(
45
Timeout<std::micro> timeout, Status &error, bool &interrupt, bool &quit) {
46
StringExtractorGDBRemote packet;
47
48
PacketResult packet_result = ReadPacket(packet, timeout, false);
49
if (packet_result == PacketResult::Success) {
50
const StringExtractorGDBRemote::ServerPacketType packet_type =
51
packet.GetServerPacketType();
52
switch (packet_type) {
53
case StringExtractorGDBRemote::eServerPacketType_nack:
54
case StringExtractorGDBRemote::eServerPacketType_ack:
55
break;
56
57
case StringExtractorGDBRemote::eServerPacketType_invalid:
58
error.SetErrorString("invalid packet");
59
quit = true;
60
break;
61
62
case StringExtractorGDBRemote::eServerPacketType_unimplemented:
63
packet_result = SendUnimplementedResponse(packet.GetStringRef().data());
64
break;
65
66
default:
67
auto handler_it = m_packet_handlers.find(packet_type);
68
if (handler_it == m_packet_handlers.end())
69
packet_result = SendUnimplementedResponse(packet.GetStringRef().data());
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
assert(error);
118
std::unique_ptr<llvm::ErrorInfoBase> EIB;
119
std::unique_ptr<UnimplementedError> UE;
120
llvm::handleAllErrors(
121
std::move(error),
122
[&](std::unique_ptr<UnimplementedError> E) { UE = std::move(E); },
123
[&](std::unique_ptr<llvm::ErrorInfoBase> E) { EIB = std::move(E); });
124
125
if (EIB)
126
return SendErrorResponse(Status(llvm::Error(std::move(EIB))));
127
return SendUnimplementedResponse("");
128
}
129
130
GDBRemoteCommunication::PacketResult
131
GDBRemoteCommunicationServer::Handle_QErrorStringEnable(
132
StringExtractorGDBRemote &packet) {
133
m_send_error_strings = true;
134
return SendOKResponse();
135
}
136
137
GDBRemoteCommunication::PacketResult
138
GDBRemoteCommunicationServer::SendIllFormedResponse(
139
const StringExtractorGDBRemote &failed_packet, const char *message) {
140
Log *log = GetLog(GDBRLog::Packets);
141
LLDB_LOGF(log, "GDBRemoteCommunicationServer::%s: ILLFORMED: '%s' (%s)",
142
__FUNCTION__, failed_packet.GetStringRef().data(),
143
message ? message : "");
144
return SendErrorResponse(0x03);
145
}
146
147
GDBRemoteCommunication::PacketResult
148
GDBRemoteCommunicationServer::SendOKResponse() {
149
return SendPacketNoLock("OK");
150
}
151
152
GDBRemoteCommunication::PacketResult
153
GDBRemoteCommunicationServer::SendJSONResponse(const json::Value &value) {
154
std::string json_string;
155
raw_string_ostream os(json_string);
156
os << value;
157
os.flush();
158
StreamGDBRemote escaped_response;
159
escaped_response.PutEscapedBytes(json_string.c_str(), json_string.size());
160
return SendPacketNoLock(escaped_response.GetString());
161
}
162
163
GDBRemoteCommunication::PacketResult
164
GDBRemoteCommunicationServer::SendJSONResponse(Expected<json::Value> value) {
165
if (!value)
166
return SendErrorResponse(value.takeError());
167
return SendJSONResponse(*value);
168
}
169
170