Kartik Anand
Kartik Anand

Reputation: 4609

Interactively watch code in python debugger pdb

Is is possible to watch the code being executed line by line interactively in python debugger pdb ?

For example in gdb one can press ^x + ^a and it brings up a code window.

GDB Screenshot

I know I can see a bit of code using list but is there a gdb like option?

Upvotes: 4

Views: 397

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124988

Not out of the box, but you can add Cmd.preloop() and Cmd.precmd() hooks of the pdb.Pdb command subclass in a .pdbrc file in your home directory, then drive a text editor to show the text.

This is the approach used by the PdbSublimeTextSupport and PdbTextMateSupport packages.

These packages simply read the current location from the Cmd subclass; self.stack[self.curindex] contains the current frame and line number, for example.

PdbSublimeTextSupport does:

def launch(self):
    frame, lineno = self.stack[self.curindex]
    filename = self.canonic(frame.f_code.co_filename)
    if exists(filename):
        command = 'subl -b "%s:%d"' % (filename, lineno)
        os.system(command)

def preloop(self):
    launch(self)

def precmd(self, line):
    launch(self)
    return line

and the Sublime Text editor opens filename at line lineno.

You can reference the bdb documentation (the bedrock on which PDB is built), together with the bdb.py and pdb.py source code, but the above example should be enough to drive just about any method of displaying the current source code lines.

Upvotes: 2

Related Questions