Over a million developers have joined DZone.

Stepping Backwards while Debugging: Move To Line

· Performance Zone

Evolve your approach to Application Performance Monitoring by adopting five best practices that are outlined and explored in this e-book, brought to you in partnership with BMC.

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.

:idea: 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 :)

Learn tips and best practices for optimizing your capacity management strategy with the Market Guide for Capacity Management, brought to you in partnership with BMC.


Published at DZone with permission of Erich Styger, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}