Stepping Backwards while Debugging: Move To Line

It happens to me many times: I’m stepping with the debugger through my code, and ups! I made one step too far!

Debugging, and made one step over too far

Debugging, and made one step over 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:

Move To Line

Move To Line

What it does: it changes the current PC (Program Counter) of the program to that line:

Performed Move to Line

Performed Move to 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:

  1. This only changes the program counter. Any variable changes/etc are not affected or reverted.
  2. 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.
  3. 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 🙂

2 thoughts on “Stepping Backwards while Debugging: Move To Line

  1. Nice, this could be done with codewarrior 6.3? I think that would be nice that keep supporting this version in your tutorials, there are many developers, that we dont make the switch to eclipse, best regards

    Like

    • Hi braulio,
      Yes, there is a similar thing at least in HI-WAVE (the S08 simulator/debugger): you can use the ‘Set Program Counter’ in the debugger to do a similar thing.
      CodeWarrior 6.3 is not an option for me, as it does not work with my 64bit Windows machine. And maybe as you, I was very reluctant to make the switch to Eclipse. Along the line “never change a running system”. But I realized that I had to change things anyway to move to a different microcontroller family. Yes, it has been a learning curve. But I cannot imagine how I ever could be at the same productivity level as before? It is somehow like Windows 3.1 (still remember this one?) compared to Windows 7: yes, they do basically the same thing, and for sure both are useful, and I can claim I could do the same thing I do with Win7 as well in Win3.1 and vice versa. But the difference the whole package and environment. Nothing wrong with everyone who still wants to use 6.3. But it is a dead end, and I decided to move on 🙂

      Like

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.