#Python : debugging your python processes with GDB
PyCharm is an awesome IDE, and its debugger is a massively useful tool to help in code development.
However, there are instances where the bug express itself only at runtime in conditions that are hard to reproduce on the developer machine or where traces are not available. Example of these types of bugs that are difficult to debug from within Python are:
- segfaults (not uncaught Python exceptions)
- hung processes (in cases where you can’t get a Python traceback or debug with pdb)
- out of control daemon processes
- python processes running in a Docker container in a production environment
In these cases, you can try gdb.
Let’s take the case of your python process running in a Docker container. You can get a shell into the container and install a couple of packages (e.g., for Ubuntu Linux):
#apt-get install gdb python2.7-dbg
Now you are ready to debug your process either interactively
(gdb) run [program name].py [arguments]
#gdb -ex r --args python [program name].py
If the process is already running (which will be the case if in production and the bug did not cause the process to terminate):
#gdb python [pid of process]
Happy debugging! 😎