#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 python
...
(gdb) run [program name].py [arguments]

or automatically:

#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! 😎

 

Advertisement

About whitehatty

Computer Engineer and Scientist interested in Computer Security, Complex Networks, Math, Biology and Medicine. "Think Different" life style. Quake 3 Arena player. NERD by DNA.

Posted on January 18, 2019, in Hacking, Linux, Uncategorized and tagged , , , . Bookmark the permalink. 1 Comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: