Introduction to the
Python debugger (pdb)




                @raulcumplido
Usages of pdb

Invoked as a script
Post Mortem Mode
Run Mode
Trace Mode
Invoked as a script

                           Only
                           Python
~$ python -m pdb test.py
                           3.2+

~$ python -m pdb –c continue test.py


~$ python -i test.py
Post Mortem Mode



    pdb.pm()
Post Mortem Mode
>>> import pdb
>>> import mymodule
>>> mymodule.test()
Traceback (most recent call   last):
 File "<stdin>", line 1, in   ?
 File "./mymodule.py", line   4, in test
   test2()
 File "./mymodule.py", line   3, in
test2
   print spam
NameError: spam
>>> pdb.pm()
> ./mymodule.py(3)test2()
> -> print spam
(Pdb)
Run Mode

pdb.run(statement[, globals[, locals])


pdb.runeval(expression[globals[, locals])



pdb.runcall(function[, argument, ...])
Run Mode

>>> import pdb
>>> import mymodule
>>> pdb.run('mymodule.test()')
> <string>(0)?()
(Pdb) continue
NameError: 'spam'
> <string>(1)?()
(Pdb)
Trace Mode



pdb.set_trace()
Debugger tips

• Entering a blank line
  repeats(except list)
• Commands not recognized are
  executed
  – When exception nothing
    changes
• Support for aliases
Debugger commands
h(elp) [command]
Debugger commands
w(here)
Debugger commands
d(own)
u(p)
Debugger commands
s(tep)
Debugger commands
n(ext)
Debugger commands
unt(il)
Debugger commands
c(ont(inue))
Debugger commands
j(ump) lineno
Debugger commands
l(ist) [first[, last]]
Debugger commands
a(rgs)
Debugger commands
p expression
pp expression
Debugger commands
[!]statement
Debugger commands
q(uit)
Debug over the network or
 to an existing process
http://dev.man-online.org/man1/rpdb2/
http://code.activestate.com/recipes/576515/
http://stackoverflow.com/questions/4163964/p
ython-is-it-possible-to-attach-a-console-
into-a-running-process
Attach to python using gdb 
$> gdb
(gdb) attach (pid)
Investigar si se puede hacer algo más con
esto
 Working on github on RPDB
We’re hiring!



                     Raúl Cumplido | @raulcumplido
                raul@catchoom.com | +34 627566095
                     catchoom.com |    @catchoom

Introduction to the Python Debugger (pdb)