Save/restore breakpoints between pdb runs

Ed Blackman ed at edgewood.to
Fri Jul 11 15:16:19 EDT 2014


I've recently started using Python for my job (as opposed to writing 
smallish scripts for personal use), and so have needed to debug quite a 
bit more.

I've been using pdb mainly via 'python -m pdb script args'.  Perhaps 
it's my Java background, but even when I have permissions to change the 
source, I find editing source to insert 'import pdb; pdb.set_trace()' 
unnatural.  The consequence is that I have to recreate my breakpoints 
when I have to exit pdb.

I've written the following code, which I load from .pdbrc with 
'execfile(os.path.expanduser('~/.pydb.py'))'

Is there an alternate solution to keeping persistant breakpoints that 
works much better?  My python editing happens on a lot of different 
machines/VMs, so I prefer alternate solutions that allow me to sync over 
a couple of files, not install new binaries.

If not:
1) If is there a way in pdb to set a breakpoint on a function that isn't 
in the current file?  I can see the .funcname property of the 
breakpoint, and would prefer restoring breakpoints on functions so they 
don't break if I change line numbers.  "b func_name" works in the 
current file, but "b file:func_name" doesn't.
2) Is there a way to list the commands for each breakpoint, so that they 
can be restored as well?

Any other comments or suggestions for improvement would be welcome.

def savebps():
    import pdb

    bp_num = 0
    for bp in pdb.bdb.Breakpoint.bpbynumber:
        # pdb commands operate on breakpoint number, so keep track of
        # the number the recreated breakpoint would have
        if bp is None:
            continue
        else:
            bp_num += 1

        command = 'tbreak' if bp.temporary else 'b'
        cond = '' if bp.cond is None else ', ' + bp.cond

        print("%s %s:%d%s" % (command, bp.file, bp.line, cond))

        if not bp.enabled:
            print("disable %d" % (bp_num))

        if bp.ignore > 0:
            print("ignore %d %d" % (bp_num, bp.ignore))

        print('')

-- 
Ed Blackman



More information about the Python-list mailing list