Multiple issues related to resuming and breakpoints
While debugging a local issue and analyzing the code of riscv-openocd, I came across multiple issues that relate to resuming/stepping and how breakpoints/triggers are handled.
Since I don't have the capacity to address them right now, I am writing them down here as a note for self (or for someone else):
Item 1:
Single-step should not be reported to GDB, if it is done internally by OpenOCD as part of temporary disabling of triggers before resuming.
Currently, the single-step is reported to gdb always (see riscv_openocd_step()): https://github.com/riscv/riscv-openocd/blob/4d274298b24f013f2211981b48500dd9bcffa98f/src/target/riscv/riscv.c#L2748
This caused a real issue in one of my local multi-hart debug scenarios when in smp+hwthread mode was used: gdb reported incorrect state for one of the harts to the user and no recovery was possible, short of restarting the debug session.
Item 2:
Reaction on argument handle_breakpoints in riscv_openocd_step is incorrect - the breakpoints are removed before the step but not re-inserted after the step. The issue is user-visible and can be reproduced by OpenOCD's resume command.
https://github.com/riscv/riscv-openocd/blob/4d274298b24f013f2211981b48500dd9bcffa98f/src/target/riscv/riscv.c#L2695
Edit May-15-2023: This is not an issue (mis-read of the code, as pointed out below https://github.com/riscv/riscv-openocd/issues/836#issuecomment-1527166148).
Item 3:
When resuming from a trigger, the disabling of triggers (disable_triggers()) is done twice:
the first call is from resume_prep()the second call is from riscv_openocd_step()
This very likely does not disrupt the functionality itself. But at the very least, the second attempt is a pointless operation that will have some performance impact.
Edit Apr-28-2023: Handled in https://github.com/riscv/riscv-openocd/pull/840
Item 4: GDB already contains the logic to temporarily disable breakpoints on step or resume. It would be good to review it and get clarity on the following:
- in which cases the debugger temporarily disables the breakpoints (watchpoints?) on its own - therefore no action in OpenOCD is needed
- in which cases this needs to be handled internally in OpenOCD
@JanMatCodasip , regarding Item2. Breakpoints are restored, I believe. See here: https://github.com/riscv/riscv-openocd/blob/4d274298b24f013f2211981b48500dd9bcffa98f/src/target/riscv/riscv.c#L2743
@aap-sc - Thank you for correcting me, I believe you're right. I will double-check it and update the issue.