Fixed stale program counter bug

This commit is contained in:
Nav
2024-12-16 21:38:30 +00:00
parent 36abea6ce1
commit b7aea71327
2 changed files with 23 additions and 26 deletions

View File

@@ -492,6 +492,13 @@ namespace TargetController
throw exception; throw exception;
} catch (const Exception& 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( this->registerCommandResponse(
commandId, commandId,
std::make_unique<Responses::Error>(exception.getMessage()) std::make_unique<Responses::Error>(exception.getMessage())
@@ -621,37 +628,27 @@ namespace TargetController
TargetControllerComponent::notifier.notify(); TargetControllerComponent::notifier.notify();
} }
void TargetControllerComponent::refreshExecutionState() { void TargetControllerComponent::refreshExecutionState(bool forceRefresh) {
auto newExecutionState = this->target->getExecutionState(); auto newState = *(this->targetState);
newState.executionState = this->target->getExecutionState();
if (newExecutionState != this->targetState->executionState) { if (!forceRefresh && newState.executionState == 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
return; 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); const auto previousState = *(this->targetState);
*(this->targetState) = newState; *(this->targetState) = newState;
EventManager::triggerEvent(std::make_shared<TargetStateChanged>(*(this->targetState), previousState));
if (newState != previousState) {
EventManager::triggerEvent(std::make_shared<TargetStateChanged>(*(this->targetState), previousState));
}
} }
void TargetControllerComponent::stopTarget() { void TargetControllerComponent::stopTarget() {

View File

@@ -272,7 +272,7 @@ namespace TargetController
void startAtomicSession(); void startAtomicSession();
void endActiveAtomicSession(); void endActiveAtomicSession();
void refreshExecutionState(); void refreshExecutionState(bool forceRefresh = false);
void updateTargetState(const Targets::TargetState& newState); void updateTargetState(const Targets::TargetState& newState);
void stopTarget(); void stopTarget();