Fixed bug in EDBG driver that was resulting in program memory corruption when flashing the target with software breakpoints installed

This commit is contained in:
Nav
2024-11-17 18:18:44 +00:00
parent 5908b74cc1
commit a3ed513b84
2 changed files with 38 additions and 10 deletions

View File

@@ -430,13 +430,7 @@ namespace DebugToolDrivers::Microchip::Protocols::Edbg::Avr
}
void EdbgAvr8Interface::clearAllBreakpoints() {
const auto responseFrame = this->edbgInterface->sendAvrCommandFrameAndWaitForResponseFrame(
ClearAllSoftwareBreakpoints{}
);
if (responseFrame.id == Avr8ResponseId::FAILED) {
throw Avr8CommandFailure{"AVR8 Clear all software breakpoints command failed", responseFrame};
}
this->clearAllSoftwareBreakpoints();
// Clear all hardware breakpoints
while (!this->hardwareBreakpointNumbersByAddress.empty()) {
@@ -889,6 +883,24 @@ namespace DebugToolDrivers::Microchip::Protocols::Edbg::Avr
return;
}
/*
* When clearing individual software breakpoints via EDBG tools, the breakpoints are not actually removed
* until this next program flow command.
*
* This wouldn't be a problem, if the tool handled the stale breakpoint removals properly, when programming
* the target (overwriting the program memory at which the software breakpoints reside). But the tool
* doesn't handle this properly - it seems to completely ignore the fact that the program memory has been
* updated, and so it will corrupt the new program by overwriting the program memory where the old
* software breakpoints used to reside. The memory is overwritten with the old instruction - the one that
* was captured at the time the software breakpoint was inserted. So we end up with a corrupted program.
*
* To avoid this issue, we send the 'clear all software breakpoints' command to the tool, just before
* entering programming mode. That command will clear all breakpoints immediately, preventing program
* memory corruption at the next flow control command.
*/
Logger::debug("Clearing all software breakpoints in preparation for programming mode");
this->clearAllSoftwareBreakpoints();
const auto responseFrame = this->edbgInterface->sendAvrCommandFrameAndWaitForResponseFrame(
EnterProgrammingMode{}
);
@@ -1222,6 +1234,16 @@ namespace DebugToolDrivers::Microchip::Protocols::Edbg::Avr
this->targetAttached = false;
}
void EdbgAvr8Interface::clearAllSoftwareBreakpoints() {
const auto responseFrame = this->edbgInterface->sendAvrCommandFrameAndWaitForResponseFrame(
ClearAllSoftwareBreakpoints{}
);
if (responseFrame.id == Avr8ResponseId::FAILED) {
throw Avr8CommandFailure{"AVR8 Clear all software breakpoints command failed", responseFrame};
}
}
std::unique_ptr<AvrEvent> EdbgAvr8Interface::getAvrEvent() {
auto event = this->edbgInterface->requestAvrEvent();