Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between "next" and "until" in pdb

Tags:

python

pdb

I am using Python 2.6.6, and using pdb to debug my Python program, but I am not clear about what is the difference between "next" and "until" in pdb, it seems both of them will continue execution until the next line in the current function.

like image 642
Eric Avatar asked Jan 03 '15 02:01

Eric


People also ask

How do you go to the next line in pdb?

The difference between n (next) and s (step) is where pdb stops. Use n (next) to continue execution until the next line and stay within the current function, i.e. not stop in a foreign function if one is called.

How do you get out of a loop in pdb?

Once you get you pdb prompt . Just hit n (next) 10 times to exit the loop.

How do you continue after pdb?

After some scientifically-crafted experimentation, turns out ctrl+Z haults the pdb shell.

How do you remove a breakpoint in pdb?

To remove all commands from a breakpoint, type commands and follow it immediately with end ; that is, give no commands. Specifying any command resuming execution (currently continue , step , next , return , jump , skip , and quit ) terminates the command list as if that command was immediately followed by end .


1 Answers

The pdb help doc describes it this way:

(Pdb) help next
n(ext)
Continue execution until the next line in the current function
is reached or it returns.

(Pdb) help until
unt(il)
Continue execution until the line with a number greater than the current
one is reached or until the current frame returns

More helpfully, Doug Hellman gives an example in his Python Module Tutorial of the Week which illustrates the difference:

The until command is like next, except it explicitly continues until execution reaches a line in the same function with a line number higher than the current value. That means, for example, that until can be used to step past the end of a loop.

pdb_next.py

import pdb

def calc(i, n):
    j = i * n
    return j

def f(n):
    for i in range(n):
        j = calc(i, n)
        print i, j
    return

if __name__ == '__main__':
    pdb.set_trace()
    f(5)

$ python pdb_next.py
> .../pdb_next.py(21)<module>()
-> f(5)
(Pdb) step
--Call--
> .../pdb_next.py(13)f()
-> def f(n):

(Pdb) step
> .../pdb_next.py(14)f()
-> for i in range(n):

(Pdb) step
> .../pdb_next.py(15)f()
-> j = calc(i, n)

(Pdb) next
> .../pdb_next.py(16)f()
-> print i, j

(Pdb) until
0 0
1 5
2 10
3 15
4 20
> .../pdb_next.py(17)f()
-> return

(Pdb)

Before until was run, the current line was 16, the last line of the loop. After until ran, execution was on line 17, and the loop had been exhausted.

The purpose of until is shared with the eponymous gdb command:

until

Continue running until a source line past the current line, in the current stack frame, is reached. This command is used to avoid single stepping through a loop more than once. It is like the next command, except that when until encounters a jump, it automatically continues execution until the program counter is greater than the address of the jump. This means that when you reach the end of a loop after single stepping though it, until makes your program continue execution until it exits the loop. In contrast, a next command at the end of a loop simply steps back to the beginning of the loop, which forces you to step through the next iteration.

like image 113
unutbu Avatar answered Sep 24 '22 04:09

unutbu