45

From PDB

(Pdb) help l
l(ist) [first [,last]]
  List source code for the current file.
  Without arguments, list 11 lines around the current line
  or continue the previous listing.
  With one argument, list 11 lines starting at that line.
  With two arguments, list the given range;
  if the second argument is less than the first, it is a count.

The "continue the previous listing" feature is really nice, but how do you turn it off?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Jorge Vargas
  • 6,712
  • 7
  • 32
  • 29
  • 6
    For those arriving here wondering how to get back to the original starting set, one work-around is `w` to get line number, than for example `l 42` to see 11 lines around line 42. – Brady Trainor Aug 29 '15 at 03:29
  • 1
    and another is: up then down – j-a May 29 '17 at 22:44

5 Answers5

31

Late but hopefully still helpful. In pdb, make the following alias (which you can add to your .pdbrc file so it's always available):

alias ll u;;d;;l

Then whenever you type ll, pdb will list from the current position. It works by going up the stack and then down the stack, which resets 'l' to show from the current position. (This won't work if you are at the top of the stack trace.)

Ghopper21
  • 10,287
  • 10
  • 63
  • 92
  • How to add it in the `pdb` config? Now I have to run it every time it breaks on `pdb.set_trace()`. – srgbnd Nov 01 '16 at 12:00
  • 1
    Put the alias line to a file `.pdbrc` in CWD or $HOME; On Windows you may also need to set HOME=%HOMEPATH% manually in the envs because pdb is not using `os.path.expanduser('~')` correctly. – kxr Apr 19 '17 at 11:21
  • Best answer :) Thanks. – Apteryx May 12 '17 at 23:45
  • 1
    I've realised that `ll` is actually an alias that brings you to the end of the file, so I ended up naming it `rl` which stands for "reset list". Hope this helps! – Caumons Nov 15 '17 at 12:15
  • 1
    this is useless nowadays, just do `l.` – Jules G.M. Dec 07 '21 at 01:42
25

Try this.

(pdb) l .

Maybe you can always type the dot.

ps. You may consider to use pudb. This is a nice UI to pdb what gdbtui is to gdb.

plhn
  • 5,017
  • 4
  • 47
  • 47
  • 2
    This should have more upvotes I think...nice answer. I've been annoyed by this for a long time now, and would always type random line numbers till I could figure out where I was. Thanks! – Matt Messersmith Dec 14 '18 at 16:40
6

If you use epdb instead of pdb, you can use "l" to go forward like in pdb, but then "l." goes back to the current line number, and "l-" goes backwards through the file. You can also use until # to continue until a given line. Epdb offers a bunch of other niceties too. Need to debug remotely? Try serve() instead of set_trace() and then telnet in (port 8080 is the default port).

import epdb
epdb.serve()
matan h
  • 900
  • 1
  • 10
  • 19
Joseph Tate
  • 94
  • 1
  • 1
5

I don't think there is a way to turn it off. It's annoyed me enough that once I went looking in the pdb source to see if there was an undocumented syntax, but I didn't find any.

There really needs to be a syntax that means, "List the lines near the current execution pointer."

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • 1
    That is both great and horrible news. It's great because now I know this is not a stupid question, horrible because I really like something like this. Since we are going into the feature request category then I'm thinking the best will be to have a reverse operation. Lets call it "rlist" that will be a reverse pager on list. So doing (Pdb) l (Pdb) r will get you to the same spot. – Jorge Vargas Aug 24 '09 at 13:49
4

You could monkey patch it for the behavior you want. For example, here is a full script which adds a "reset_list" or "rl" command to pdb:

import pdb

def Pdb_reset_list(self, arg):
    self.lineno = None
    print >>self.stdout, "Reset list position."
pdb.Pdb.do_reset = Pdb_reset_list
pdb.Pdb.do_rl = Pdb_reset_list

a = 1
b = 2

pdb.set_trace()

print a, b

One could conceivably monkey patch the standard list command to not retain the lineno history.

edit: And here is such a patch:

import pdb
Pdb = pdb.Pdb

Pdb._do_list = Pdb.do_list
def pdb_list_wrapper(self, arg):
    if arg.strip().lower() in ('r', 'reset', 'c', 'current'):
        self.lineno = None
        arg = ''
    self._do_list(arg)
Pdb.do_list = Pdb.do_l = pdb_list_wrapper

a = 1
b = 2

pdb.set_trace()

print a, b
Mike Boers
  • 6,665
  • 3
  • 31
  • 40