Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make pdb recognize that the source has changed between runs?

From what I can tell, pdb does not recognize when the source code has changed between "runs". That is, if I'm debugging, notice a bug, fix that bug, and rerun the program in pdb (i.e. without exiting pdb), pdb will not recompile the code. I'll still be debugging the old version of the code, even if pdb lists the new source code.

So, does pdb not update the compiled code as the source changes? If not, is there a way to make it do so? I'd like to be able to stay in a single pdb session in order to keep my breakpoints and such.

FWIW, gdb will notice when the program it's debugging changes underneath it, though only on a restart of that program. This is the behavior I'm trying to replicate in pdb.

like image 419
user88028 Avatar asked Apr 07 '09 10:04

user88028


People also ask

How do you set a breakpoint in pdb?

Optionally, you can also tell pdb to break only when a certain condition is true. Use the command b (break) to set a breakpoint. You can specify a line number or a function name where execution is stopped. If filename: is not specified before the line number lineno , then the current source file is used.

What does import pdb pdb Set_trace () do?

It's import pdb; pdb. set_trace() . When execution reaches this point in the program, the program stops and you're dropped into the pdb debugger. Effectively, this is the same as inserting a breakpoint on the line below where we call set_trace() .

What are these pdb commands used for?

It can set conditional breakpoints and single stepping at the source line level. It also supports inspection of stack frames, source code listing, and evaluation of arbitrary Python code in any stack frame's context. Other facilities include post-mortem debugging. To aid us in our purpose, we have the 'pdb' module.


1 Answers

The following mini-module may help. If you import it in your pdb session, then you can use:

pdb> pdbs.r()

at any time to force-reload all non-system modules except main. The code skips that because it throws an ImportError('Cannot re-init internal module main') exception.

# pdbs.py - PDB support

from __future__ import print_function

def r():
    """Reload all non-system modules, to reload stuff on pbd restart. """
    import importlib
    import sys

    # This is likely to be OS-specific
    SYS_PREFIX = '/usr/lib'

    for k, v in list(sys.modules.items()):
        if (
            k == "__main__" or
            k.startswith("pdb") or
            not getattr(v, "__file__", None)
            or v.__file__.startswith(SYS_PREFIX)
        ):
            continue
        print("reloading %s [%s]" % (k, v.__file__), file=sys.stderr)
        importlib.reload(v)
like image 147
pourhaus Avatar answered Sep 30 '22 18:09

pourhaus