2021-10-02 17:39:27 +01:00
|
|
|
#include "GdbRspDebugServer.hpp"
|
|
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
#include <sys/socket.h>
|
2022-04-05 22:37:00 +01:00
|
|
|
#include <unistd.h>
|
2021-10-06 21:12:31 +01:00
|
|
|
|
|
|
|
|
#include "src/Logger/Logger.hpp"
|
2021-04-04 21:04:12 +01:00
|
|
|
|
|
|
|
|
#include "Exceptions/ClientDisconnected.hpp"
|
|
|
|
|
#include "Exceptions/ClientNotSupported.hpp"
|
|
|
|
|
#include "Exceptions/ClientCommunicationError.hpp"
|
2021-05-30 16:53:49 +01:00
|
|
|
#include "Exceptions/DebugSessionAborted.hpp"
|
2022-03-27 18:32:13 +01:00
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
#include "src/Exceptions/Exception.hpp"
|
|
|
|
|
#include "src/Exceptions/InvalidConfig.hpp"
|
2022-03-27 18:32:13 +01:00
|
|
|
#include "src/Exceptions/DebugServerInterrupted.hpp"
|
|
|
|
|
|
2022-03-27 19:43:20 +01:00
|
|
|
// Command packets
|
|
|
|
|
#include "CommandPackets/CommandPacket.hpp"
|
|
|
|
|
#include "CommandPackets/SupportedFeaturesQuery.hpp"
|
|
|
|
|
#include "CommandPackets/InterruptExecution.hpp"
|
|
|
|
|
#include "CommandPackets/ContinueExecution.hpp"
|
|
|
|
|
#include "CommandPackets/StepExecution.hpp"
|
|
|
|
|
#include "CommandPackets/ReadRegisters.hpp"
|
|
|
|
|
#include "CommandPackets/WriteRegister.hpp"
|
|
|
|
|
#include "CommandPackets/SetBreakpoint.hpp"
|
|
|
|
|
#include "CommandPackets/RemoveBreakpoint.hpp"
|
2022-04-08 22:18:52 +01:00
|
|
|
#include "CommandPackets/Monitor.hpp"
|
2022-04-08 22:23:30 +01:00
|
|
|
#include "CommandPackets/ResetTarget.hpp"
|
2022-03-27 19:43:20 +01:00
|
|
|
|
|
|
|
|
// Response packets
|
2022-03-27 18:32:13 +01:00
|
|
|
#include "ResponsePackets/TargetStopped.hpp"
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-03-31 16:05:39 +01:00
|
|
|
namespace Bloom::DebugServer::Gdb
|
2022-02-05 15:32:08 +00:00
|
|
|
{
|
|
|
|
|
using namespace Exceptions;
|
|
|
|
|
using namespace Bloom::Exceptions;
|
|
|
|
|
|
2022-03-27 19:43:20 +01:00
|
|
|
using CommandPackets::CommandPacket;
|
|
|
|
|
|
2022-03-27 18:32:13 +01:00
|
|
|
GdbRspDebugServer::GdbRspDebugServer(
|
|
|
|
|
const DebugServerConfig& debugServerConfig,
|
2022-04-15 22:05:50 +01:00
|
|
|
EventListener& eventListener,
|
|
|
|
|
EventFdNotifier& eventNotifier
|
2022-03-27 18:32:13 +01:00
|
|
|
)
|
|
|
|
|
: debugServerConfig(GdbDebugServerConfig(debugServerConfig))
|
|
|
|
|
, eventListener(eventListener)
|
2022-04-15 22:05:50 +01:00
|
|
|
, interruptEventNotifier(eventNotifier)
|
|
|
|
|
{}
|
2022-02-05 15:32:08 +00:00
|
|
|
|
|
|
|
|
void GdbRspDebugServer::init() {
|
|
|
|
|
this->socketAddress.sin_family = AF_INET;
|
2022-04-05 18:49:54 +01:00
|
|
|
this->socketAddress.sin_port = htons(this->debugServerConfig.listeningPortNumber);
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-03-19 15:16:59 +00:00
|
|
|
if (::inet_pton(
|
|
|
|
|
AF_INET,
|
2022-04-05 18:49:54 +01:00
|
|
|
this->debugServerConfig.listeningAddress.c_str(),
|
2022-03-19 15:16:59 +00:00
|
|
|
&(this->socketAddress.sin_addr)
|
|
|
|
|
) == 0
|
|
|
|
|
) {
|
2022-02-05 15:32:08 +00:00
|
|
|
// Invalid IP address
|
|
|
|
|
throw InvalidConfig(
|
2022-04-05 18:49:54 +01:00
|
|
|
"Invalid IP address provided in config file: (\"" + this->debugServerConfig.listeningAddress
|
2022-03-19 15:16:59 +00:00
|
|
|
+ "\")"
|
2022-02-05 15:32:08 +00:00
|
|
|
);
|
|
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-03-19 15:17:07 +00:00
|
|
|
int socketFileDescriptor = 0;
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
if ((socketFileDescriptor = ::socket(AF_INET, SOCK_STREAM, 0)) == 0) {
|
|
|
|
|
throw Exception("Failed to create socket file descriptor.");
|
|
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-04-05 18:49:54 +01:00
|
|
|
const auto enableReuseAddressSocketOption = 1;
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
if (::setsockopt(
|
|
|
|
|
socketFileDescriptor,
|
|
|
|
|
SOL_SOCKET,
|
|
|
|
|
SO_REUSEADDR,
|
2022-04-05 18:49:54 +01:00
|
|
|
&(enableReuseAddressSocketOption),
|
|
|
|
|
sizeof(enableReuseAddressSocketOption)
|
2022-02-05 15:32:08 +00:00
|
|
|
) < 0
|
|
|
|
|
) {
|
|
|
|
|
Logger::error("Failed to set socket SO_REUSEADDR option.");
|
|
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
if (::bind(
|
|
|
|
|
socketFileDescriptor,
|
|
|
|
|
reinterpret_cast<const sockaddr*>(&(this->socketAddress)),
|
|
|
|
|
sizeof(this->socketAddress)
|
|
|
|
|
) < 0
|
|
|
|
|
) {
|
|
|
|
|
throw Exception("Failed to bind address. The selected port number ("
|
2022-04-05 18:49:54 +01:00
|
|
|
+ std::to_string(this->debugServerConfig.listeningPortNumber) + ") may be in use.");
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
this->serverSocketFileDescriptor = socketFileDescriptor;
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-03-28 01:04:14 +01:00
|
|
|
this->epollInstance.addEntry(
|
2022-04-05 18:49:54 +01:00
|
|
|
this->serverSocketFileDescriptor.value(),
|
2022-03-28 01:04:14 +01:00
|
|
|
static_cast<std::uint16_t>(EpollEvent::READ_READY)
|
|
|
|
|
);
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-03-28 01:04:14 +01:00
|
|
|
this->epollInstance.addEntry(
|
2022-04-15 22:05:50 +01:00
|
|
|
this->interruptEventNotifier.getFileDescriptor(),
|
2022-03-28 01:04:14 +01:00
|
|
|
static_cast<std::uint16_t>(EpollEvent::READ_READY)
|
|
|
|
|
);
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-04-05 18:49:54 +01:00
|
|
|
Logger::info("GDB RSP address: " + this->debugServerConfig.listeningAddress);
|
|
|
|
|
Logger::info("GDB RSP port: " + std::to_string(this->debugServerConfig.listeningPortNumber));
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-03-27 18:32:13 +01:00
|
|
|
this->eventListener.registerCallbackForEventType<Events::TargetControllerStateReported>(
|
2022-02-05 15:32:08 +00:00
|
|
|
std::bind(&GdbRspDebugServer::onTargetControllerStateReported, this, std::placeholders::_1)
|
|
|
|
|
);
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-03-27 18:32:13 +01:00
|
|
|
this->eventListener.registerCallbackForEventType<Events::TargetExecutionStopped>(
|
2022-02-05 15:32:08 +00:00
|
|
|
std::bind(&GdbRspDebugServer::onTargetExecutionStopped, this, std::placeholders::_1)
|
|
|
|
|
);
|
2021-10-06 21:12:31 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void GdbRspDebugServer::close() {
|
2022-03-24 19:17:41 +00:00
|
|
|
this->terminateActiveDebugSession();
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-04-05 18:49:54 +01:00
|
|
|
if (this->serverSocketFileDescriptor.has_value()) {
|
|
|
|
|
::close(this->serverSocketFileDescriptor.value());
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-03-27 18:32:13 +01:00
|
|
|
void GdbRspDebugServer::run() {
|
2022-02-05 15:32:08 +00:00
|
|
|
try {
|
2022-03-24 19:17:41 +00:00
|
|
|
if (!this->activeDebugSession.has_value()) {
|
2022-02-05 15:32:08 +00:00
|
|
|
Logger::info("Waiting for GDB RSP connection");
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-03-24 19:17:41 +00:00
|
|
|
auto connection = this->waitForConnection();
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-03-24 19:17:41 +00:00
|
|
|
if (!connection.has_value()) {
|
|
|
|
|
// Likely an interrupt
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger::info("Accepted GDP RSP connection from " + connection->getIpAddress());
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-03-25 00:17:28 +00:00
|
|
|
this->activeDebugSession.emplace(
|
2022-03-28 01:04:14 +01:00
|
|
|
DebugSession(std::move(connection.value()), this->getGdbTargetDescriptor())
|
2022-03-25 00:17:28 +00:00
|
|
|
);
|
2022-03-27 18:32:13 +01:00
|
|
|
|
2022-03-20 17:37:36 +00:00
|
|
|
EventManager::triggerEvent(std::make_shared<Events::DebugSessionStarted>());
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
/*
|
|
|
|
|
* Before proceeding with a new debug session, we must ensure that the TargetController is able to
|
|
|
|
|
* service it.
|
|
|
|
|
*/
|
|
|
|
|
if (!this->targetControllerConsole.isTargetControllerInService()) {
|
2022-03-24 19:17:41 +00:00
|
|
|
this->terminateActiveDebugSession();
|
2022-02-05 15:32:08 +00:00
|
|
|
throw DebugSessionAborted("TargetController not in service");
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-03-27 19:43:20 +01:00
|
|
|
auto commandPacket = this->waitForCommandPacket();
|
|
|
|
|
|
|
|
|
|
if (commandPacket == nullptr) {
|
|
|
|
|
// Likely an interrupt
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
commandPacket->handle(this->activeDebugSession.value(), this->targetControllerConsole);
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
} catch (const ClientDisconnected&) {
|
|
|
|
|
Logger::info("GDB RSP client disconnected");
|
2022-03-24 19:17:41 +00:00
|
|
|
this->terminateActiveDebugSession();
|
2022-02-05 15:32:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
} catch (const ClientCommunicationError& exception) {
|
|
|
|
|
Logger::error(
|
|
|
|
|
"GDB RSP client communication error - " + exception.getMessage() + " - closing connection"
|
|
|
|
|
);
|
2022-03-24 19:17:41 +00:00
|
|
|
this->terminateActiveDebugSession();
|
2022-02-05 15:32:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
} catch (const ClientNotSupported& exception) {
|
|
|
|
|
Logger::error("Invalid GDB RSP client - " + exception.getMessage() + " - closing connection");
|
2022-03-24 19:17:41 +00:00
|
|
|
this->terminateActiveDebugSession();
|
2022-02-05 15:32:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
} catch (const DebugSessionAborted& exception) {
|
|
|
|
|
Logger::warning("GDB debug session aborted - " + exception.getMessage());
|
2022-03-24 19:17:41 +00:00
|
|
|
this->terminateActiveDebugSession();
|
2022-02-05 15:32:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
} catch (const DebugServerInterrupted&) {
|
|
|
|
|
// Server was interrupted
|
|
|
|
|
Logger::debug("GDB RSP interrupted");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-03-24 19:17:41 +00:00
|
|
|
std::optional<Connection> GdbRspDebugServer::waitForConnection() {
|
2022-04-05 18:49:54 +01:00
|
|
|
if (::listen(this->serverSocketFileDescriptor.value(), 3) != 0) {
|
2022-02-05 15:32:08 +00:00
|
|
|
throw Exception("Failed to listen on server socket");
|
|
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-03-28 01:04:14 +01:00
|
|
|
const auto eventFileDescriptor = this->epollInstance.waitForEvent();
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-03-28 01:04:14 +01:00
|
|
|
if (
|
|
|
|
|
!eventFileDescriptor.has_value()
|
2022-04-15 22:05:50 +01:00
|
|
|
|| eventFileDescriptor.value() == this->interruptEventNotifier.getFileDescriptor()
|
2022-03-28 01:04:14 +01:00
|
|
|
) {
|
2022-04-15 22:05:50 +01:00
|
|
|
this->interruptEventNotifier.clear();
|
2022-03-28 01:04:14 +01:00
|
|
|
return std::nullopt;
|
2022-03-24 19:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
2022-04-05 18:49:54 +01:00
|
|
|
return std::make_optional<Connection>(
|
|
|
|
|
this->serverSocketFileDescriptor.value(),
|
2022-04-15 22:05:50 +01:00
|
|
|
this->interruptEventNotifier
|
2022-04-05 18:49:54 +01:00
|
|
|
);
|
2022-03-24 19:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
2022-03-27 19:43:20 +01:00
|
|
|
std::unique_ptr<CommandPacket> GdbRspDebugServer::waitForCommandPacket() {
|
|
|
|
|
const auto rawPackets = this->activeDebugSession->connection.readRawPackets();
|
|
|
|
|
|
|
|
|
|
if (rawPackets.empty()) {
|
|
|
|
|
// The wait was interrupted
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We only process the last packet - any others will probably be duplicates from an impatient client.
|
|
|
|
|
return this->resolveCommandPacket(rawPackets.back());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<CommandPacket> GdbRspDebugServer::resolveCommandPacket(const RawPacketType& rawPacket) {
|
|
|
|
|
if (rawPacket.size() == 5 && rawPacket[1] == 0x03) {
|
|
|
|
|
// This is an interrupt request - create a fake packet for it
|
|
|
|
|
return std::make_unique<CommandPackets::InterruptExecution>(rawPacket);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto rawPacketString = std::string(rawPacket.begin(), rawPacket.end());
|
|
|
|
|
|
|
|
|
|
if (rawPacketString.size() >= 2) {
|
|
|
|
|
/*
|
|
|
|
|
* First byte of the raw packet will be 0x24 ('$'), so find() should return 1, not 0, when
|
|
|
|
|
* looking for a command identifier string.
|
|
|
|
|
*/
|
|
|
|
|
if (rawPacketString.find("qSupported") == 1) {
|
|
|
|
|
return std::make_unique<CommandPackets::SupportedFeaturesQuery>(rawPacket);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rawPacketString[1] == 'g' || rawPacketString[1] == 'p') {
|
|
|
|
|
return std::make_unique<CommandPackets::ReadRegisters>(rawPacket);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rawPacketString[1] == 'P') {
|
|
|
|
|
return std::make_unique<CommandPackets::WriteRegister>(rawPacket);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rawPacketString[1] == 'c') {
|
|
|
|
|
return std::make_unique<CommandPackets::ContinueExecution>(rawPacket);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rawPacketString[1] == 's') {
|
|
|
|
|
return std::make_unique<CommandPackets::StepExecution>(rawPacket);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rawPacketString[1] == 'Z') {
|
|
|
|
|
return std::make_unique<CommandPackets::SetBreakpoint>(rawPacket);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rawPacketString[1] == 'z') {
|
|
|
|
|
return std::make_unique<CommandPackets::RemoveBreakpoint>(rawPacket);
|
|
|
|
|
}
|
2022-04-08 22:18:52 +01:00
|
|
|
|
|
|
|
|
if (rawPacketString.find("qRcmd") == 1) {
|
|
|
|
|
// This is a monitor packet
|
|
|
|
|
auto monitorCommand = std::make_unique<CommandPackets::Monitor>(rawPacket);
|
2022-04-08 22:23:30 +01:00
|
|
|
|
|
|
|
|
if (monitorCommand->command == "reset") {
|
|
|
|
|
return std::make_unique<CommandPackets::ResetTarget>(std::move(*(monitorCommand.get())));
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-08 22:18:52 +01:00
|
|
|
return monitorCommand;
|
|
|
|
|
}
|
2022-03-27 19:43:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::make_unique<CommandPacket>(rawPacket);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-24 19:17:41 +00:00
|
|
|
void GdbRspDebugServer::terminateActiveDebugSession() {
|
2022-04-05 18:49:54 +01:00
|
|
|
if (!this->activeDebugSession.has_value()) {
|
|
|
|
|
return;
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
2022-04-05 18:49:54 +01:00
|
|
|
|
|
|
|
|
this->activeDebugSession->terminate();
|
|
|
|
|
this->activeDebugSession = std::nullopt;
|
|
|
|
|
|
|
|
|
|
EventManager::triggerEvent(std::make_shared<Events::DebugSessionFinished>());
|
2021-10-06 21:12:31 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void GdbRspDebugServer::onTargetControllerStateReported(const Events::TargetControllerStateReported& event) {
|
2022-04-09 15:57:24 +01:00
|
|
|
if (
|
|
|
|
|
event.state == TargetController::TargetControllerState::SUSPENDED
|
|
|
|
|
&& this->activeDebugSession.has_value()
|
|
|
|
|
) {
|
2022-02-05 15:32:08 +00:00
|
|
|
Logger::warning("Terminating debug session - TargetController suspended unexpectedly");
|
2022-03-24 19:17:41 +00:00
|
|
|
this->terminateActiveDebugSession();
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void GdbRspDebugServer::onTargetExecutionStopped(const Events::TargetExecutionStopped&) {
|
2022-04-16 21:21:29 +01:00
|
|
|
try {
|
|
|
|
|
if (this->activeDebugSession.has_value() && this->activeDebugSession->waitingForBreak) {
|
|
|
|
|
this->activeDebugSession->connection.writePacket(
|
|
|
|
|
ResponsePackets::TargetStopped(Signal::TRAP)
|
|
|
|
|
);
|
|
|
|
|
this->activeDebugSession->waitingForBreak = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (const ClientDisconnected&) {
|
|
|
|
|
Logger::info("GDB RSP client disconnected");
|
|
|
|
|
this->terminateActiveDebugSession();
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
} catch (const ClientCommunicationError& exception) {
|
|
|
|
|
Logger::error(
|
|
|
|
|
"GDB RSP client communication error - " + exception.getMessage() + " - closing connection"
|
2022-03-27 18:32:13 +01:00
|
|
|
);
|
2022-04-16 21:21:29 +01:00
|
|
|
this->terminateActiveDebugSession();
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
} catch (const DebugServerInterrupted&) {
|
|
|
|
|
// Server was interrupted
|
|
|
|
|
Logger::debug("GDB RSP interrupted");
|
|
|
|
|
return;
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
}
|
|
|
|
|
}
|