From b7aea71327fe237c6a0222d6f4964242be0b6f38 Mon Sep 17 00:00:00 2001 From: Nav Date: Mon, 16 Dec 2024 21:38:30 +0000 Subject: [PATCH] Fixed stale program counter bug --- .../TargetControllerComponent.cpp | 47 +++++++++---------- .../TargetControllerComponent.hpp | 2 +- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/TargetController/TargetControllerComponent.cpp b/src/TargetController/TargetControllerComponent.cpp index 7dcf7140..3dd0a6b0 100644 --- a/src/TargetController/TargetControllerComponent.cpp +++ b/src/TargetController/TargetControllerComponent.cpp @@ -492,6 +492,13 @@ namespace TargetController throw exception; } catch (const Exception& exception) { + try { + this->refreshExecutionState(true); + + } catch (const Exception& refreshException) { + Logger::error("Post exception target state refresh failed: " + refreshException.getMessage()); + } + this->registerCommandResponse( commandId, std::make_unique(exception.getMessage()) @@ -621,37 +628,27 @@ namespace TargetController TargetControllerComponent::notifier.notify(); } - void TargetControllerComponent::refreshExecutionState() { - auto newExecutionState = this->target->getExecutionState(); + void TargetControllerComponent::refreshExecutionState(bool forceRefresh) { + auto newState = *(this->targetState); + newState.executionState = this->target->getExecutionState(); - if (newExecutionState != this->targetState->executionState) { - Logger::debug("Target execution state changed"); - - auto newState = *(this->targetState); - newState.executionState = newExecutionState; - - if (newExecutionState == TargetExecutionState::STOPPED) { - Logger::debug("Target stopped"); - newState.programCounter = this->target->getProgramCounter(); - - } else { - Logger::debug("Target resumed"); - newState.programCounter = std::nullopt; - } - - this->updateTargetState(newState); - } - } - - void TargetControllerComponent::updateTargetState(const TargetState& newState) { - if (newState == *(this->targetState)) { - // Nothing has changed, nothing to do + if (!forceRefresh && newState.executionState == this->targetState->executionState) { return; } + newState.programCounter = newState.executionState == TargetExecutionState::STOPPED + ? std::optional{this->target->getProgramCounter()} + : std::nullopt; + this->updateTargetState(newState); + } + + void TargetControllerComponent::updateTargetState(const TargetState& newState) { const auto previousState = *(this->targetState); *(this->targetState) = newState; - EventManager::triggerEvent(std::make_shared(*(this->targetState), previousState)); + + if (newState != previousState) { + EventManager::triggerEvent(std::make_shared(*(this->targetState), previousState)); + } } void TargetControllerComponent::stopTarget() { diff --git a/src/TargetController/TargetControllerComponent.hpp b/src/TargetController/TargetControllerComponent.hpp index d99d8eb2..66610b9f 100644 --- a/src/TargetController/TargetControllerComponent.hpp +++ b/src/TargetController/TargetControllerComponent.hpp @@ -272,7 +272,7 @@ namespace TargetController void startAtomicSession(); void endActiveAtomicSession(); - void refreshExecutionState(); + void refreshExecutionState(bool forceRefresh = false); void updateTargetState(const Targets::TargetState& newState); void stopTarget();