13 November 2014

Save pdb tips that are not included directly in tutorial. Just for convenience of looking up.

How to begin debugging

  • Insert import pdb; pdb.set_trace() into codeļ¼Œ then python <your-program>. Or
  • python -m pdb

Command tips

  • where / w: show where you are in call stack.
  • dir() / locals() / globals(): show all varibales.
  • continue: continue running.
  • Use time.sleep(N) to switch processes in PDB. Refer to here

How to debug multiprocess program

I use remote debugger for this. Winpdb and rpdb2. To install and launch a debug session (referred to here):

# == On host side, where you program ====

# Install wxpython from http://www.wxpython.org/download.php#msw
# If you see Mac says wxpython package damaged, temporarily turn off Gatekeeper, see http://stackoverflow.com/questions/21223717/install-wxpython-on-mac-os-mavericks

# Install winpdb
wget https://winpdb.googlecode.com/files/winpdb-1.4.8.tar.gz
tar -xzvf winpdb-1.4.8.tar.gz
cd winpdb-1.4.8
sudo python setup.py install -f

#pip install winpdb  # Don't use this because it installs an early version with problem http://stackoverflow.com/questions/3464013/winpdb-error-debugging-django-ctimeouthttp-instance-has-no-attribute-getresp

# Start winpdb

# == On dev machine, where you run python ==

# Install winpdb but no need for wxpython
... # like the above

# Embed debug breakpoint in code
print 'hello'
import rpdb2; rpdb2.start_embedded_debugger('your_password')
print 'world'

# You can use rpdb2 on localhost
> password 'your_password'
> attach
...  # list of wait debugees and pids
> attach <pid>
> thread  # to show threads
> thread <n>  # to switch thread

# == On host side ==

# On winpdb GUI, set password and attach

You can run rpdb2 right on the dev machine, from another ssh session. Note that rpdb2 command set is a bit different from pdb. This helps when you want to use rpdb2 only and avoid remove connection.

Other References

Create an Issue or comment below