How To Debug A Running Python Process
Software Contributor Documentation Table of Contents
Sometimes it is helpful to debug a running python process. If you can restart the process, use remote-pdb. If not, use gdb.
Using remote-pdb
Setup
yum install -y epel-release
yum install -y python-pip socat
pip install remote-pdb
Preparing remote-pdb
-
Locate the source for the service that is running. The source is most like under
/usr/lib/python2.7/site-packages/<service>
. Once located, open the file of interest and add the following lines to set a breakpoint:from remote_pdb import RemotePdb RemotePdb('127.0.0.1', 4444).set_trace()
-
Restart the service:
systemctl restart <servicename>
Connecting To remote-pdb
When the current line of execution hits the breakpoint the service will pause and no further execution will take place. When this happens, the pdb session can be established by doing:
socat readline tcp:127.0.0.1:4444
Once connected, pdb can be used to control the execution of the service.
Using gdb
Setup gdb
yum install -y gdb
Connecting To gdb
-
Find the pid of the service to debug.
-
gdb python <PID_HERE>
Note: When running you may notice some missing debuginfos in the output. You can copy paste the suggested command to install them and try debugging again.