Custom debugger trace hooks

R

R. Bernstein

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top