tallcoder
tallcoder

Reputation: 317

How to debug backwards in PyCharm?

We all know F9 goes forward in debug mode. But how can we go backward after going a couple steps forward with F9 or is that even possible?

Upvotes: 11

Views: 5228

Answers (3)

AXO
AXO

Reputation: 9076

I don't know when this feature has been added, but I'm using PyCharm 2023.3.5 (Community Edition) and I can move both forward and backward in the code using Jump To Cursor action located in the Run menu. Just moving the cursor to line that you want to be executed and then choose Jump To Cursor.

Note that similar to pdb, you can only jump between the lines of the bottom frame and it does not restore the state, it only changes the line that is going to be executed next. If you try to jump to another frame/function you'll get Error: jump is available only within the bottom frame.

Tip: You can assign a keyboard shortcut for this action from Setting > keymap > main menu > run > debugger actions > Jump to cursor.

enter image description here enter image description here

Upvotes: 3

Oskar Zdrojewski
Oskar Zdrojewski

Reputation: 79

The accepted answer is not correct. You can step back, the state of the program doesn't have to be rewinded. The interpretation where you would rewind the state falls apart when you are in any sort of loop, recursion or function call as it's not clear which state you want to rewind to.

Stepping back is useful when you don't want to restart the whole application to check the code in question. In that case you step back to the last fresh state or just keep in mind that some variables have changed.

As of 2024 this feature is not implemented in PyCharm, however it is present in many other editors, like VS, VSCode, Rider.

PyCharm Moving debugging cursor backward

Upvotes: 2

bad_coder
bad_coder

Reputation: 12870

how can we go backward after going a couple steps forward with F9 or is that possible?

It isn't possible, you can't "go back" during debugging. (That is the case in Python and other programming languages/debuggers in general.)

The reason is the debugger would have to restore the state of your program to the previous step. Which using your logic would involve holding a copy of the entire state (memory, stack, temporary files, etc...) at every step of the program. (Not to mention operations protocols that aren't reversible and require precise timing.) That would be exponential and become easily unfeasible. That's why it's not done.

Upvotes: 6

Related Questions