It happens to me many times: I’m stepping with the debugger through my code, and ups! I made one step too far!
What now? Restart the whole debugging session?
Actually, there is a way to go ‘backwards’ 🙂
❓ gdb has a ‘reverse debugging’ feature, described here. I’m using the Eclipse based CodeWarrior debugger, and this debug engine is not using gdb.
The CodeWarrior debugger in MCU10.3 supports an Eclipse feature: I select a code line in the Editor view and use Move to Line:
What it does: it changes the current PC (Program Counter) of the program to that line:
Now I can continue debugging from that line, e.g. stepping into that function call.
Yes, this is not true backward debugging. But it is simple and very effective. To perform true backward stepping, the debugger would need to reverse all operations, typically with a rather heavy state machine and data recording. But for the usual case where I simply need to go back a few lines, the ‘Move to Line’ is perfect.
Of course there are a few points to consider:
- This only changes the program counter. Any variable changes/etc are not affected or reverted.
- In case of highly optimized code, there might be multiple sequence points per source line. So doing this for highly optimized code might not work correctly.
- It works ok within a function. It is not recommended to use it e.g. to set the PC outside of a function. Because the context/stack frame is not set up.
💡 I use the ‘Move to Line’ frequently to ‘advance’ the program execution. E.g. to bypass some long sequences I’m not interested in, or to get out of an ‘endless’ loop.
The same ‘Move To Line’ as available while doing assembly stepping too. See this post for details.
Happy Line Moving 🙂