Sub topic 11
Debugging after a crash with post-mortem mode
Post-mortem debugging opens the debugger on an exception that has already happened. The program has stopped, but the traceback still holds every frame and its local variables. This chapter assumes you can read a traceback, use try/except, and know the basic (Pdb) commands p, pp, w, u, and d.
What post-mortem mode gives you
With breakpoint(), you guess where the bug is and stop there. With post-mortem mode, you let the program crash and then inspect the frame where the exception was raised. You don't need to rerun the program with a breakpoint added.
You can't step forward from a post-mortem session, because the stack has already unwound. You can only look at the state the frames were in when the error occurred.
Three ways to enter it
Inside an except block
pdb.post_mortem() with no argument uses the exception currently being handled.
import pdb
def main():
data = {"a": 1}
return data["b"]
try:
main()
except Exception:
pdb.post_mortem()The prompt opens in main, on the line that raised the KeyError:
> /tmp/crash.py(6)main()
-> return data["b"]
(Pdb) p data
{'a': 1}
(Pdb) p data.keys()
dict_keys(['a'])
(Pdb) w
(Pdb) qHere w prints the stack, so you can use u and d to look at the callers' variables.
Running the script under pdb
python -m pdb script.py starts the program under the debugger. If the script ends with an uncaught exception, pdb prints the traceback and enters post-mortem mode on its own:
python -m pdb crash.pyAt the first (Pdb) prompt, type c to run until the crash. Typing c or s after the crash restarts the program from the beginning, with your breakpoints kept.
From an interactive session
If an exception escapes in the interactive interpreter, call pdb.pm() on the next line. It opens the debugger on the last uncaught exception.
>>> import pdb
>>> import crash
Traceback (most recent call last):
...
KeyError: 'b'
>>> pdb.pm()A practical pattern
Add post-mortem mode only when you want it. Putting the call in a handler at your program's entry point means the debugger opens on the first unexpected error and nowhere else.
import pdb
import sys
def run():
items = [1, 2, 0]
return [10 / n for n in items]
if __name__ == "__main__":
try:
run()
except Exception:
if sys.stdin.isatty():
pdb.post_mortem()
else:
raiseThe isatty() check keeps the program from hanging on a prompt when no one is at the terminal, such as in a cron job or a CI run. In a list comprehension the failing frame is the comprehension itself (before Python 3.12), so use u to reach run.
<br>
Exercise
A script crashes with an uncaught ZeroDivisionError. You want to inspect the local variables of the function that raised it without editing the script. What command do you run, and what do you type at the prompt?
Show answer
Run python -m pdb script.py, then type c at the first (Pdb) prompt. When the exception is uncaught, pdb enters post-mortem mode on the failing frame. Use p or pp locals() to inspect variables, and u to look at the callers.