Reputation: 20091
This question has been asked before for other IDE's, but what about for PHPStorm? Is there a way to take a step back while debugging?
Specifically after an exception is thrown, I'd like to easily take a step back. Or at least be able to see the call stack so I can quickly see where I need to set the breakpoint, because currently I just have to run again from the beginning and try to stop just before exception.
Upvotes: 13
Views: 4179
Reputation: 9466
EDIT: 1st thing I should have mentioned is that you should be able to see where the exception was thrown in the stack trace displayed on screen or in a log file (see php error logging). You can then place a breakpoint a line or two above where the exception was thrown and run again.
Original answer:
You cannot ever "step back", but you can go up the stack. The entire stack is "current", part of the current state. But there is no way to step back in a program because programs can be fundmanetally irreversible by theorem.
As Gerard de Visser mentioned, it would certainly be possible to record every step of execution, in theory. But I think there's a much simpler solution.
What you can do is use the PhpStorm shortcut to "go to last cursor position" (VS Code also has such a shortcut, as I'm sure do all other IDEs). On Ubuntu, it's shift
+alt
+left-arrow
, but on Windows I think it's ctrl
+alt
+left-arrow
. I use this all the time when an exception is thrown. Then I put a breakpoint in and re-run the code.
Upvotes: 1
Reputation: 8050
There is something called time travel debugging
that makes it possible to step backwards through the code. It records the execution of a php script so you can replay it and will be able to step in reverse mode.
One example that works with PhpStorm and Xdebug is this one: https://github.com/sidkshatriya/dontbug
Upvotes: 2
Reputation: 20091
Yes you can take a step back, sort of. The Frames window in the Debugger tab shows the execution path that was taken to get to this point. By default the debugger simply has the current frame selected.
You can easily click any of the frames, or use your arrow key to move up and down to view the state of variables and even the location in the code at that point.
Upvotes: 4