Hacker News new | past | comments | ask | show | jobs | submit login
Debugging running Python scripts with PDB via GDB (disconnect3d.pl)
28 points by woodruffw 33 days ago | hide | past | favorite | 6 comments



I like to add this function to my Python scripts so that I can drop into pdb by sending a signal:

    import signal

    def sigusr1_handler(signum, stack):
        breakpoint()

    signal.signal(signal.SIGUSR1, sigusr1_handler)
And then I can drop into pdb by sending a signal:

    $ kill -s sigusr1 $PID
I also want to highlight something non-obvious about debugging mixed native and Python code. Because Python is interpreted, it is possible to run both gdb and pdb on a process at the same time, and set breakpoints using both, without issue. This can be really nice when you're working on Python bindings.


i was one of the lucky 10,000 that discovered pdb a year ago. jsut wanted to point out that you can alias your normal `python` command to run `python -m pdb -c c` and it will always run in pdb mode for most development https://twitter.com/jeremyphoward/status/1756541454288396402


That's really cool! I've just tested it, and it seems to still be in pdb even if the program finished successfully. Is there any way to make it so that it would enter the debugger only on an exception?


You can also inspect python frames, objects/variables etc directly from GDB: https://docs.python.org/3/howto/gdb_helpers.html


I've had good luck with this with simple variables and native data structures list lists, dictionaries, tuples. But when I try it with something bulkier like a Pandas dataframe, I'm not able to make any sense of the stack and find what I'm looking for, which is usually the data.


Probably worth mention madbg⁽¹⁾, a fully-featured remote and preemptive debugger for Python with both a CLI and an API, that allows attaching to running programs preemptively (but does not require gdb).

⁽¹⁾https://github.com/kmaork/madbg




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: