From c9a8220500e22f340b293261c1742d08a1379c10 Mon Sep 17 00:00:00 2001 From: Nav Date: Thu, 15 Sep 2022 00:33:01 +0100 Subject: [PATCH] Suspend TargetController upon GDB detach, if running under CLion --- src/DebugServer/CMakeLists.txt | 1 + .../Gdb/CommandPackets/CommandPacket.cpp | 6 --- src/DebugServer/Gdb/CommandPackets/Detach.cpp | 38 +++++++++++++++++++ src/DebugServer/Gdb/CommandPackets/Detach.hpp | 17 +++++++++ src/DebugServer/Gdb/GdbRspDebugServer.cpp | 12 ++++++ 5 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 src/DebugServer/Gdb/CommandPackets/Detach.cpp create mode 100644 src/DebugServer/Gdb/CommandPackets/Detach.hpp diff --git a/src/DebugServer/CMakeLists.txt b/src/DebugServer/CMakeLists.txt index e546d76f..5d7e1bfd 100755 --- a/src/DebugServer/CMakeLists.txt +++ b/src/DebugServer/CMakeLists.txt @@ -23,6 +23,7 @@ target_sources( ${CMAKE_CURRENT_SOURCE_DIR}/Gdb/CommandPackets/BloomVersion.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Gdb/CommandPackets/BloomVersionMachine.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Gdb/CommandPackets/GenerateSvd.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Gdb/CommandPackets/Detach.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Gdb/ResponsePackets/SupportedFeaturesResponse.cpp # AVR GDB RSP Server diff --git a/src/DebugServer/Gdb/CommandPackets/CommandPacket.cpp b/src/DebugServer/Gdb/CommandPackets/CommandPacket.cpp index 0c1f20c1..fe2047c2 100644 --- a/src/DebugServer/Gdb/CommandPackets/CommandPacket.cpp +++ b/src/DebugServer/Gdb/CommandPackets/CommandPacket.cpp @@ -38,12 +38,6 @@ namespace Bloom::DebugServer::Gdb::CommandPackets return; } - if (packetString[0] == 'D') { - // Detach packet - there's not really anything we need to do here, so just respond with an OK - debugSession.connection.writePacket(OkResponsePacket()); - return; - } - if (packetString.find("vMustReplyEmpty") == 0) { Logger::debug("Handling vMustReplyEmpty"); debugSession.connection.writePacket(EmptyResponsePacket()); diff --git a/src/DebugServer/Gdb/CommandPackets/Detach.cpp b/src/DebugServer/Gdb/CommandPackets/Detach.cpp new file mode 100644 index 00000000..00c6462d --- /dev/null +++ b/src/DebugServer/Gdb/CommandPackets/Detach.cpp @@ -0,0 +1,38 @@ +#include "Detach.hpp" + +#include "src/DebugServer/Gdb/ResponsePackets/OkResponsePacket.hpp" +#include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp" + +#include "src/Helpers/Process.hpp" + +#include "src/Logger/Logger.hpp" + +namespace Bloom::DebugServer::Gdb::CommandPackets +{ + using TargetController::TargetControllerConsole; + + using ResponsePackets::OkResponsePacket; + using ResponsePackets::ErrorResponsePacket; + + using Exceptions::Exception; + + Detach::Detach(const RawPacketType& rawPacket) + : CommandPacket(rawPacket) + {} + + void Detach::handle(DebugSession& debugSession, TargetControllerConsole& targetControllerConsole) { + Logger::debug("Handling Detach packet"); + + try { + if (Process::isProcessManagedByClion(Process::getParentProcessId())) { + targetControllerConsole.suspendTargetController(); + } + + debugSession.connection.writePacket(OkResponsePacket()); + + } catch (const Exception& exception) { + Logger::error("Failed to suspend TargetController - " + exception.getMessage()); + debugSession.connection.writePacket(ErrorResponsePacket()); + } + } +} diff --git a/src/DebugServer/Gdb/CommandPackets/Detach.hpp b/src/DebugServer/Gdb/CommandPackets/Detach.hpp new file mode 100644 index 00000000..6f547214 --- /dev/null +++ b/src/DebugServer/Gdb/CommandPackets/Detach.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "CommandPacket.hpp" + +namespace Bloom::DebugServer::Gdb::CommandPackets +{ + class Detach: public CommandPacket + { + public: + explicit Detach(const RawPacketType& rawPacket); + + void handle( + DebugSession& debugSession, + TargetController::TargetControllerConsole& targetControllerConsole + ) override; + }; +} diff --git a/src/DebugServer/Gdb/GdbRspDebugServer.cpp b/src/DebugServer/Gdb/GdbRspDebugServer.cpp index b7419000..f0fe3289 100644 --- a/src/DebugServer/Gdb/GdbRspDebugServer.cpp +++ b/src/DebugServer/Gdb/GdbRspDebugServer.cpp @@ -31,9 +31,11 @@ #include "CommandPackets/BloomVersion.hpp" #include "CommandPackets/BloomVersionMachine.hpp" #include "CommandPackets/GenerateSvd.hpp" +#include "CommandPackets/Detach.hpp" // Response packets #include "ResponsePackets/TargetStopped.hpp" +#include "src/Helpers/Process.hpp" namespace Bloom::DebugServer::Gdb { @@ -122,6 +124,12 @@ namespace Bloom::DebugServer::Gdb this->eventListener.registerCallbackForEventType( std::bind(&GdbRspDebugServer::onTargetExecutionStopped, this, std::placeholders::_1) ); + + if (Process::isProcessManagedByClion(Process::getParentProcessId())) { + Logger::warning( + "Bloom's process is being managed by CLion - Bloom will automatically shutdown upon detaching from GDB." + ); + } } void GdbRspDebugServer::close() { @@ -252,6 +260,10 @@ namespace Bloom::DebugServer::Gdb return std::make_unique(rawPacket); } + if (rawPacket[1] == 'D') { + return std::make_unique(rawPacket); + } + const auto rawPacketString = std::string(rawPacket.begin(), rawPacket.end()); if (rawPacketString.size() >= 2) {