2021-08-30 22:17:59 +01:00
|
|
|
#include "InsightWorker.hpp"
|
|
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
#include <QObject>
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
2022-04-27 21:27:59 +01:00
|
|
|
#include "src/TargetController/TargetControllerState.hpp"
|
|
|
|
|
|
2021-08-30 22:17:59 +01:00
|
|
|
#include "src/Logger/Logger.hpp"
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
namespace Bloom
|
|
|
|
|
{
|
|
|
|
|
using namespace Bloom::Exceptions;
|
2021-05-24 20:58:49 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
using Bloom::Targets::TargetState;
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void InsightWorker::queueTask(InsightWorkerTask* task) {
|
|
|
|
|
auto taskQueueLock = this->queuedTasks.acquireLock();
|
|
|
|
|
task->moveToThread(this->thread());
|
|
|
|
|
task->setParent(this);
|
2022-04-15 14:32:26 +01:00
|
|
|
this->queuedTasks.getValue().push(task);
|
2022-02-05 15:32:08 +00:00
|
|
|
emit this->taskQueued();
|
|
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void InsightWorker::startup() {
|
|
|
|
|
Logger::debug("Starting InsightWorker thread");
|
2022-03-20 17:37:36 +00:00
|
|
|
EventManager::registerListener(this->eventListener);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-04-27 21:27:59 +01:00
|
|
|
this->eventListener->registerCallbackForEventType<Events::TargetControllerStateChanged>(
|
|
|
|
|
std::bind(&InsightWorker::onTargetControllerStateChangedEvent, this, std::placeholders::_1)
|
2022-02-05 15:32:08 +00:00
|
|
|
);
|
2021-05-30 16:53:24 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
this->eventListener->registerCallbackForEventType<Events::TargetExecutionStopped>(
|
|
|
|
|
std::bind(&InsightWorker::onTargetStoppedEvent, this, std::placeholders::_1)
|
|
|
|
|
);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
this->eventListener->registerCallbackForEventType<Events::TargetExecutionResumed>(
|
|
|
|
|
std::bind(&InsightWorker::onTargetResumedEvent, this, std::placeholders::_1)
|
|
|
|
|
);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-04-08 22:14:37 +01:00
|
|
|
this->eventListener->registerCallbackForEventType<Events::TargetReset>(
|
|
|
|
|
std::bind(&InsightWorker::onTargetResetEvent, this, std::placeholders::_1)
|
|
|
|
|
);
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
this->eventListener->registerCallbackForEventType<Events::RegistersWrittenToTarget>(
|
|
|
|
|
std::bind(&InsightWorker::onTargetRegistersWrittenEvent, this, std::placeholders::_1)
|
|
|
|
|
);
|
2021-09-11 20:45:06 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
this->eventDispatchTimer = new QTimer(this);
|
|
|
|
|
QObject::connect(this->eventDispatchTimer, &QTimer::timeout, this, &InsightWorker::dispatchEvents);
|
|
|
|
|
this->eventDispatchTimer->start(5);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
QObject::connect(this, &InsightWorker::taskQueued, this, &InsightWorker::executeTasks);
|
2021-08-30 22:17:59 +01:00
|
|
|
|
2022-03-20 17:37:36 +00:00
|
|
|
EventManager::triggerEvent(
|
2022-02-05 15:32:08 +00:00
|
|
|
std::make_shared<Events::InsightThreadStateChanged>(ThreadState::READY)
|
|
|
|
|
);
|
2021-10-18 01:03:22 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
emit this->ready();
|
|
|
|
|
}
|
2021-08-30 22:17:59 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
std::optional<InsightWorkerTask*> InsightWorker::getQueuedTask() {
|
|
|
|
|
auto task = std::optional<InsightWorkerTask*>();
|
2021-08-30 22:17:59 +01:00
|
|
|
|
2022-04-15 14:32:26 +01:00
|
|
|
auto& queuedTasks = this->queuedTasks.getValue();
|
2022-02-05 15:32:08 +00:00
|
|
|
auto taskQueueLock = this->queuedTasks.acquireLock();
|
2021-08-30 22:17:59 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
if (!queuedTasks.empty()) {
|
|
|
|
|
task = queuedTasks.front();
|
|
|
|
|
queuedTasks.pop();
|
|
|
|
|
}
|
2021-08-30 22:17:59 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
return task;
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void InsightWorker::onTargetStoppedEvent(const Events::TargetExecutionStopped& event) {
|
2022-04-23 17:30:14 +01:00
|
|
|
if (this->lastTargetState == TargetState::STOPPED) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
/*
|
|
|
|
|
* When we report a target halt to Insight, Insight will immediately seek more data from the target (such as
|
|
|
|
|
* GPIO pin states). This can be problematic for cases where the target had halted due to a conditional
|
|
|
|
|
* breakpoint.
|
|
|
|
|
*
|
|
|
|
|
* For conditional breakpoints, a software breakpoint is employed to halt target execution and give the debug
|
|
|
|
|
* client an opportunity to check the condition. In cases where the condition is not met, the client will
|
|
|
|
|
* immediately request for execution to be resumed. It's important that Insight does not get in the way of this
|
|
|
|
|
* process, as it could end up slowing things down significantly.
|
|
|
|
|
*
|
|
|
|
|
* For the above reason, we don't want to report any target halts to Insight, unless we can be sure that the
|
|
|
|
|
* client isn't going to immediately resume execution upon checking the condition.
|
|
|
|
|
*
|
|
|
|
|
* We do this by providing a time window for the TargetExecutionResumed event. If the event is triggered within
|
|
|
|
|
* that time window, we won't report the target halt to Insight, thus preventing Insight from needlessly
|
|
|
|
|
* seeking data from the target and slowing things down.
|
|
|
|
|
*
|
|
|
|
|
* This isn't the best approach, TBH, as it introduces a delay to Insight's response to the target halting. The
|
|
|
|
|
* problem is, we cannot differentiate a conditional breakpoint with a software breakpoint, so this seems to be
|
|
|
|
|
* the only way. It would be nice if the debug client gave us some form of indication of whether the breakpoint
|
|
|
|
|
* is a conditional one.
|
|
|
|
|
*/
|
|
|
|
|
auto resumedEvent = this->eventListener->waitForEvent<Events::TargetExecutionResumed>(
|
|
|
|
|
std::chrono::milliseconds(650)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!resumedEvent.has_value()) {
|
2022-04-23 17:30:14 +01:00
|
|
|
this->lastTargetState = TargetState::STOPPED;
|
2022-02-05 15:32:08 +00:00
|
|
|
emit this->targetStateUpdated(TargetState::STOPPED);
|
|
|
|
|
emit this->targetProgramCounterUpdated(event.programCounter);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-11 20:45:06 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void InsightWorker::onTargetResumedEvent(const Events::TargetExecutionResumed& event) {
|
2022-04-23 17:30:14 +01:00
|
|
|
this->lastTargetState = TargetState::RUNNING;
|
2022-02-05 15:32:08 +00:00
|
|
|
emit this->targetStateUpdated(TargetState::RUNNING);
|
|
|
|
|
}
|
2021-05-30 16:53:24 +01:00
|
|
|
|
2022-04-08 22:14:37 +01:00
|
|
|
void InsightWorker::onTargetResetEvent(const Events::TargetReset& event) {
|
|
|
|
|
// TODO: Pull PC from target. This will be done as part of the TC refactor (https://github.com/navnavnav/Bloom/issues/25)
|
2022-04-23 17:30:14 +01:00
|
|
|
this->lastTargetState = TargetState::STOPPED;
|
2022-04-08 22:14:37 +01:00
|
|
|
emit this->targetStateUpdated(TargetState::STOPPED);
|
|
|
|
|
emit this->targetProgramCounterUpdated(0);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void InsightWorker::onTargetRegistersWrittenEvent(const Events::RegistersWrittenToTarget& event) {
|
|
|
|
|
emit this->targetRegistersWritten(event.registers, event.createdTimestamp);
|
|
|
|
|
}
|
2021-05-30 16:53:24 +01:00
|
|
|
|
2022-04-27 21:27:59 +01:00
|
|
|
void InsightWorker::onTargetControllerStateChangedEvent(const Events::TargetControllerStateChanged& event) {
|
2022-04-09 15:57:24 +01:00
|
|
|
using TargetController::TargetControllerState;
|
|
|
|
|
|
2022-04-27 21:27:59 +01:00
|
|
|
if (event.state == TargetControllerState::SUSPENDED) {
|
2022-02-05 15:32:08 +00:00
|
|
|
emit this->targetControllerSuspended();
|
|
|
|
|
|
2022-04-27 21:27:59 +01:00
|
|
|
} else if (event.state == TargetControllerState::ACTIVE) {
|
2022-02-05 15:32:08 +00:00
|
|
|
try {
|
|
|
|
|
emit this->targetControllerResumed(this->targetControllerConsole.getTargetDescriptor());
|
|
|
|
|
|
|
|
|
|
} catch (const Exception& exception) {
|
|
|
|
|
Logger::error("Insight resume failed - " + exception.getMessage());
|
|
|
|
|
}
|
2021-05-30 16:53:24 +01:00
|
|
|
}
|
|
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void InsightWorker::executeTasks() {
|
|
|
|
|
auto task = std::optional<InsightWorkerTask*>();
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
while ((task = this->getQueuedTask()).has_value()) {
|
|
|
|
|
task.value()->execute(this->targetControllerConsole);
|
|
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
}
|
|
|
|
|
}
|