Custom debugger trace hooks

R. Bernstein rocky at panix.com
Thu Dec 11 05:17:09 EST 2008


A colleague recently asked this:
 
Is there a cleaner way to dump a trace/track of running a python script. 
 With Pydb I made work-around with
 
   import pydb
   pydb.debugger(dbg_cmds=['bt', 'l', 's']*300 + ['c'])
  
   So now I have a dump of 300 steps with backtraces, so I can easily
   compare two executions of the same script to find where they diverged. I
   think it is a really nice feature.

pydb and pdb inherit from the cmd module which does allow pre- and
post-command hooks. 

Neither pdb nor pydb make it easy to add one's own hook. However

If there's something you want to run before stepping you can do that
by monkey-patching Pdb.precmd:

#################### snip
import pydb
def myprecmd(obj, debug_cmd):
    """Custom Hook method executed before issuing a debugger command."""
    global _pydb_trace
    obj.do_list('')
    obj.do_where('10')  # limit stack to at most 10 entries
    return obj.old_precmd(debug_cmd)  # is always string 's' in example below

_pydb_trace = pydb.Pdb()
pydb.Pdb.old_precmd = pydb.Pdb.precmd
pydb.Pdb.precmd = myprecmd
pydb.debugger(dbg_cmds=['s'] * 30)
# ....
####################

I believe the same is applicable to pdb, although I haven't
investigated.



More information about the Python-list mailing list