Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python pdb (debugger) disp equivalent?

Is there a pdb equivalent to disp in gdb?

E.g. when I'm debugging C using gdb I can have variables printed on every 'step' through the code by typing:

disp var

When I'm debugging python using pdb I would like similar functionality, but disp does not seem to be there, the python pdb documentation does not seem to offer an alternative - but it seems like an odd omission?

like image 673
bph Avatar asked Dec 15 '11 13:12

bph


2 Answers

During the pdb debugging you can type normal python code, beyond the one letter commands - so just using print var should work for you.

like image 147
jsbueno Avatar answered Nov 17 '22 20:11

jsbueno


You can set up some aliases that will do this for you:

alias n next;; p var
alias s step;; p var

Printing a whole list of variable names is left as an exercise to the reader. Unfortunately doing it this way means that when you send the debugger an empty line, the "last command" it executes is p var rather than, for example, n. If you want to fix that, then you can use this somewhat hacky set of Pdb commands instead:

!global __stack; from inspect import stack as __stack
!global __Pdb; from pdb import Pdb as __Pdb
!global __pdb; __pdb = [__framerec[0].f_locals.get("pdb") or __framerec[0].f_locals.get("self") for __framerec in __stack() if (__framerec[0].f_locals.get("pdb") or __framerec[0].f_locals.get("self")).__class__ == __Pdb][-1]

alias s step;; p var;; !__pdb.lastcmd = "!__pdb.cmdqueue.append('s')"
alias n next;; p var;; !__pdb.lastcmd = "!__pdb.cmdqueue.append('n')"
like image 24
Michael Hoffman Avatar answered Nov 17 '22 19:11

Michael Hoffman