PDB is a fantastic debugger for Python, but it cannot be easily attached to an already running Python program. The recommended method to attach to a running Python program for debugging is GDB as described here. But, examining stack trace of a Python program and Python objects in a C++ debugger like GDB is not straightforward.
I recently discovered that the GUI debugger in PyCharm IDE can be used to attach to a running Python program and debug it. It is easy to do this:
- An already running program: Let us assume that I already have a running Python program whose source files are all inside a
/home/joe/foobardirectory. It has been running an important task for hours now and I have discovered a tiny bug that can be fixed in the running program by changing the value of a global variable.
- Enable ptrace of any process: For this type of live debugging, we need any process to be able to ptrace any other process. However, the kernel in your distribution may be setup to only allow ptrace of a child process by a parent process. Check that the value of
/proc/sys/kernel/yama/ptrace_scopeis 0. If not, set it temporarily to 0:
$ echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
- Install PyCharm: Download PyCharm and unzip the downloaded file. I use the Community Edition which is free.
- Run PyCharm: Run
bin/pycharm.shand open the directory containing the source files of the running program.
- If necessary, set the Python interpreter for this project to be the same as that of the running program. That is, we make sure they both use the same version of Python.
- In the source files, set one or more breakpoints where you would like to stop, inspect or change the running program.
- Attach: Now we are ready to attach to our running program! Choose Run → Attach to local process and choose the PID of our already running program from the list.
- Debug: Once attached, the program should stop at our breakpoints. We can now step through the program and change the value of variables to effect some live bug fixes! Once done, we can disable the breakpoints and allow the program to continue by itself.
Tried with: PyCharm 2016.2, Python 2.7.11 and Ubuntu 16.04