python:在pdb中是否可以在n次点击后才启用断点?

时间:2013-01-03 13:24:35

标签: python debugging pdb

在eclipse(以及其他几个IDE)中,只有在一定数量的点击后才能打开断点。在Python的pdb中,断点有一个命中计数,并且有condition命令。我如何连接它们?

2 个答案:

答案 0 :(得分:16)

条件断点可以用两种方式设置 -

FIRST:使用break

指定断点的条件
python -m pdb pdb_break.py
> .../pdb_break.py(7)<module>()
-> def calc(i, n):
(Pdb) break 9, j>0
Breakpoint 1 at .../pdb_break.py:9

(Pdb) break
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at .../pdb_break.py:9
        stop only if j>0

(Pdb) continue
i = 0
j = 0
i = 1
> .../pdb_break.py(9)calc()
-> print 'j =', j

(Pdb)

SECOND:条件也可以使用condition命令应用于现有断点。参数是断点ID和表达式。

$ python -m pdb pdb_break.py
> .../pdb_break.py(7)<module>()
-> def calc(i, n):
(Pdb) break 9
Breakpoint 1 at .../pdb_break.py:9

(Pdb) break
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at .../pdb_break.py:9

(Pdb) condition 1 j>0

(Pdb) break
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at .../pdb_break.py:9
        stop only if j>0

(Pdb)

source

更新:我写了一个更简单的代码

import pdb; pdb.set_trace()
for i in range(100):
    print i

终端上的调试 -

$ python 1.py 
> /code/python/1.py(3)<module>()
-> for i in range(100):
(Pdb) l
  1     
  2     import pdb; pdb.set_trace()
  3  -> for i in range(100):
  4         print i
[EOF]
(Pdb) break 4, i==3
Breakpoint 1 at /code/python/1.py:4
(Pdb) break
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at /code/python/1.py:4
    stop only if i==3
(Pdb) c
0
1
2
> /Users/srikar/code/python/1.py(4)<module>()
-> print i
(Pdb) p i
3

答案 1 :(得分:16)

我找到了答案。实际上很简单,有一个名为ignore的命令,假设您希望在1000次点击后在第9行的断点处中断:

b 9

输出:Breakpoint 2 at ...

ignore 1 1000

输出:Will ignore next 1000 crossings of breakpoint 1.

 c