Improved RAII of DebugSession class

This commit is contained in:
Nav
2022-08-13 03:06:30 +01:00
parent d59c4f92ba
commit 2372b93e11
5 changed files with 26 additions and 37 deletions

View File

@@ -1,6 +1,7 @@
#include "DebugSession.hpp" #include "DebugSession.hpp"
#include "src/Logger/Logger.hpp" #include "src/Logger/Logger.hpp"
#include "src/EventManager/EventManager.hpp"
namespace Bloom::DebugServer::Gdb namespace Bloom::DebugServer::Gdb
{ {
@@ -16,9 +17,11 @@ namespace Bloom::DebugServer::Gdb
this->supportedFeatures.insert({ this->supportedFeatures.insert({
Feature::PACKET_SIZE, std::to_string(this->connection.getMaxPacketSize()) Feature::PACKET_SIZE, std::to_string(this->connection.getMaxPacketSize())
}); });
EventManager::triggerEvent(std::make_shared<Events::DebugSessionStarted>());
} }
void DebugSession::terminate() { DebugSession::~DebugSession() {
EventManager::triggerEvent(std::make_shared<Events::DebugSessionFinished>());
} }
} }

View File

@@ -39,6 +39,12 @@ namespace Bloom::DebugServer::Gdb
const TargetDescriptor& targetDescriptor const TargetDescriptor& targetDescriptor
); );
void terminate(); DebugSession(const DebugSession& other) = delete;
DebugSession(DebugSession&& other) = delete;
DebugSession& operator = (const DebugSession& other) = delete;
DebugSession& operator = (DebugSession&& other) = delete;
~DebugSession();
}; };
} }

View File

@@ -3,6 +3,7 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
#include "src/EventManager/EventManager.hpp"
#include "src/Logger/Logger.hpp" #include "src/Logger/Logger.hpp"
#include "Exceptions/ClientDisconnected.hpp" #include "Exceptions/ClientDisconnected.hpp"
@@ -124,7 +125,7 @@ namespace Bloom::DebugServer::Gdb
} }
void GdbRspDebugServer::close() { void GdbRspDebugServer::close() {
this->terminateActiveDebugSession(); this->activeDebugSession.reset();
if (this->serverSocketFileDescriptor.has_value()) { if (this->serverSocketFileDescriptor.has_value()) {
::close(this->serverSocketFileDescriptor.value()); ::close(this->serverSocketFileDescriptor.value());
@@ -146,15 +147,11 @@ namespace Bloom::DebugServer::Gdb
Logger::info("Accepted GDP RSP connection from " + connection->getIpAddress()); Logger::info("Accepted GDP RSP connection from " + connection->getIpAddress());
this->activeDebugSession.emplace( this->activeDebugSession.emplace(
DebugSession( std::move(connection.value()),
std::move(connection.value()), this->getSupportedFeatures(),
this->getSupportedFeatures(), this->getGdbTargetDescriptor()
this->getGdbTargetDescriptor()
)
); );
EventManager::triggerEvent(std::make_shared<Events::DebugSessionStarted>());
/* /*
* Before proceeding with a new debug session, we must ensure that the TargetController is able to * Before proceeding with a new debug session, we must ensure that the TargetController is able to
* service it. * service it.
@@ -177,7 +174,7 @@ namespace Bloom::DebugServer::Gdb
!targetControllerStateChangedEvent.has_value() !targetControllerStateChangedEvent.has_value()
|| targetControllerStateChangedEvent->get()->state != TargetControllerState::ACTIVE || targetControllerStateChangedEvent->get()->state != TargetControllerState::ACTIVE
) { ) {
this->terminateActiveDebugSession(); this->activeDebugSession.reset();
throw DebugSessionAborted("TargetController not in service"); throw DebugSessionAborted("TargetController not in service");
} }
} }
@@ -197,24 +194,24 @@ namespace Bloom::DebugServer::Gdb
} catch (const ClientDisconnected&) { } catch (const ClientDisconnected&) {
Logger::info("GDB RSP client disconnected"); Logger::info("GDB RSP client disconnected");
this->terminateActiveDebugSession(); this->activeDebugSession.reset();
return; return;
} catch (const ClientCommunicationError& exception) { } catch (const ClientCommunicationError& exception) {
Logger::error( Logger::error(
"GDB RSP client communication error - " + exception.getMessage() + " - closing connection" "GDB RSP client communication error - " + exception.getMessage() + " - closing connection"
); );
this->terminateActiveDebugSession(); this->activeDebugSession.reset();
return; return;
} catch (const ClientNotSupported& exception) { } catch (const ClientNotSupported& exception) {
Logger::error("Invalid GDB RSP client - " + exception.getMessage() + " - closing connection"); Logger::error("Invalid GDB RSP client - " + exception.getMessage() + " - closing connection");
this->terminateActiveDebugSession(); this->activeDebugSession.reset();
return; return;
} catch (const DebugSessionAborted& exception) { } catch (const DebugSessionAborted& exception) {
Logger::warning("GDB debug session aborted - " + exception.getMessage()); Logger::warning("GDB debug session aborted - " + exception.getMessage());
this->terminateActiveDebugSession(); this->activeDebugSession.reset();
return; return;
} catch (const DebugServerInterrupted&) { } catch (const DebugServerInterrupted&) {
@@ -335,21 +332,10 @@ namespace Bloom::DebugServer::Gdb
}; };
} }
void GdbRspDebugServer::terminateActiveDebugSession() {
if (!this->activeDebugSession.has_value()) {
return;
}
this->activeDebugSession->terminate();
this->activeDebugSession = std::nullopt;
EventManager::triggerEvent(std::make_shared<Events::DebugSessionFinished>());
}
void GdbRspDebugServer::onTargetControllerStateChanged(const Events::TargetControllerStateChanged& event) { void GdbRspDebugServer::onTargetControllerStateChanged(const Events::TargetControllerStateChanged& event) {
if (event.state == TargetControllerState::SUSPENDED && this->activeDebugSession.has_value()) { if (event.state == TargetControllerState::SUSPENDED && this->activeDebugSession.has_value()) {
Logger::warning("TargetController suspended unexpectedly - terminating debug session"); Logger::warning("TargetController suspended unexpectedly - terminating debug session");
this->terminateActiveDebugSession(); this->activeDebugSession.reset();
} }
} }
@@ -364,14 +350,14 @@ namespace Bloom::DebugServer::Gdb
} catch (const ClientDisconnected&) { } catch (const ClientDisconnected&) {
Logger::info("GDB RSP client disconnected"); Logger::info("GDB RSP client disconnected");
this->terminateActiveDebugSession(); this->activeDebugSession.reset();
return; return;
} catch (const ClientCommunicationError& exception) { } catch (const ClientCommunicationError& exception) {
Logger::error( Logger::error(
"GDB RSP client communication error - " + exception.getMessage() + " - closing connection" "GDB RSP client communication error - " + exception.getMessage() + " - closing connection"
); );
this->terminateActiveDebugSession(); this->activeDebugSession.reset();
return; return;
} catch (const DebugServerInterrupted&) { } catch (const DebugServerInterrupted&) {

View File

@@ -176,12 +176,6 @@ namespace Bloom::DebugServer::Gdb
*/ */
virtual std::set<std::pair<Feature, std::optional<std::string>>> getSupportedFeatures(); virtual std::set<std::pair<Feature, std::optional<std::string>>> getSupportedFeatures();
/**
* Terminates any active debug session (if any) by closing the connection to the GDB client.
*/
void terminateActiveDebugSession();
/** /**
* Should return the GDB target descriptor for the connected target. * Should return the GDB target descriptor for the connected target.
* *

View File

@@ -102,7 +102,7 @@ the [GDB debug server implementation](../DebugServer/Gdb/README.md) will termina
void GdbRspDebugServer::onTargetControllerStateChanged(const Events::TargetControllerStateChanged& event) { void GdbRspDebugServer::onTargetControllerStateChanged(const Events::TargetControllerStateChanged& event) {
if (event.state == TargetControllerState::SUSPENDED && this->activeDebugSession.has_value()) { if (event.state == TargetControllerState::SUSPENDED && this->activeDebugSession.has_value()) {
Logger::warning("TargetController suspended unexpectedly - terminating debug session"); Logger::warning("TargetController suspended unexpectedly - terminating debug session");
this->terminateActiveDebugSession(); this->activeDebugSession.reset();
} }
} }
``` ```