Set Next Statement menu item in Delphi’s Code Editor
In my “Debugger Enhancements since Delphi 7” post, I received a comment that it was not clear what I meant with the item “Set Next Statement menu item in the editor“.
To clarify, this feature is a new menu item on the code editor’s local popup menu. Under normal circumstances, this menu item appears on the “Debug” submenu of the editor’s local menu. If you’ve enabled the Rearrange editor local menu on run option on the Tools | Options | Debugger options page, then, while you are debugging, this menu item will appear directly on the editor’s local menu (and not on a Debug submenu) along with the other debugger items.
This menu item lets you change the next source line that will be executed, effectively letting you alter the behavior of your program at runtime. Before this menu item was added, you had to drop into the CPU view to change the next statement to be executed. You could do this in the CPU view using the New EIP menu item on the disassembly pane’s local menu or by directly changing the value of the EIP register in the Registers pane. Now, with this new menu item, you can accomplish the same thing in the code editor. To use this menu item, simply right click on the source statement that you want to be executed next and select the Set Next Statement menu item. Then when you Run or Step using the debugger, it will continue execution from that point.
The editor menu item is roughly equivalent to the menu item in the CPU view — the major difference is that when you use it in the editor, the debugger must first figure out which instruction corresponds to the source code statement you’ve selected. This is simply a matter of getting the address of the first instruction and setting the EIP register to that address. If you need to set the EIP to an address that does not correspond to the first instruction of the source code statement, then you will still need to use the CPU view to accomplish this.
Being able to change the next instruction executed is a powerful feature of a debugger; however, you can easily shoot yourself in the foot by doing this. It is important to note that using this menu item does not cause your program to run until the statement is reached. If you want to do that, you should use the Run to Cursor or Run to Current commands in the editor or CPU view, respectively (both of which can be executed using the F4 keystroke in the default keymapping). Instead, all that happens is the debugger sets the next statment or instruction to be executed. It does not change any other program data, so it is very easy to cause your program to crash using these commands (for example, if you use these commands to skip code that sets up data required later in program execution). Because of the inherent danger of this command, the IDE will prompt you for confirmation if you use the “Set Next Statement“ command in the editor on a source code line that is not part of the current method being debugged.
There are a couple of situations where I use the Set Next Statement and New EIP commands. A common one is where I’ve inadvertently stepped over a method that I meant to step into. Rather than re-run the program, in many cases, it is safe to set the Next Statement back to the method call that I wanted to step into. After doing this, I can then use Step Into to step into the function. Note, that this basically means that the method will get executed twice — once when I stepped over it, and once again when I reset the EIP and then step into it. If the code is not intended to be executed twice, then, of course, problems may arise, but in many situations, it may be perfectly safe to do this.
Another situation where I use this is when I want to step through a method multiple times. Sometimes it takes quite a few steps to get your application to its failure point. In these situations, it might be easier to reset the EIP to be able to further examine code that has already executed, rather than start the program over again and repeat the steps required to reproduce the failure. Again, as long as the code is safe to execute more than once, you should be able to use this functionality over and over to repeatedly step through a method without having to repeat the steps required to get to that point.
These are just two cases where Set Next Statement and New EIP are useful. There are plenty of others as well, and once you’ve used them to help debug your program, they can become indispensable tools in efficiently tracking down problems in your code.
Share This | Email this page to a friend
Posted by Chris Hesik on April 30th, 2007 under CodeGear Debugger |

RSS Feed

April 30th, 2007 at 12:02 pm
Great post, Chris, as usual. I’ve never been quite as bold as you are in manually setting the EIP, but I will often do it on boolean if/then flag checks so that I can ‘make’ the code go, for instance, into the ‘else’ block instead of the ‘if’ block to see what would happen if the test value were different.
On another note - everyone please vote for my QC debugger feature request: http://qc.borland.com/wc/qcmain.aspx?d=43304
Visual Studio and JBuilder people shouldn’t be the only ones who get this
April 30th, 2007 at 12:14 pm
Handy, but I suspect it would be more useful with $O-, as the optimizer and register optimization causes variables to go out of scope constantly, mix in storage reuse and I guess that means you could easily cross the wires and mix up variables.
ie: storage xxx previously held variable a, but now is varaible b, jumping back to when the compiler assumed storage xxx held a would cause some pretty bizarre problems.
This can happen pretty quickly with the compiler I’ve noticed…
Result := fn(FirstinputParameter);
Would be about all it would take to do it in most cases… Definitely an advanced users only, tread lightly sort of feature.
May 4th, 2007 at 3:08 am
Hi,
I just saw this on VMWare Workstation 6.0 Beta info:
http://www.vmware.com/products/beta/ws/
One of the new features to expect in 6.0:
====
Integrated Virtual Debugger — Workstation integrates with Visual Studio and Eclipse so you can deploy, run, and debug programs in a VM directly from your preferred IDE.
====
Release notes here for more info:
http://www.vmware.com/products/beta/ws/releasenotes_ws60_beta.html
Why only Visual Studio & Eclipse, but no Delphi?
Since VMWare 6.0 product is still in beta, maybe CodeGear should contact them to see if there’s a way to add support for Delphi too before v6.0 ships?
Steve C