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

monwx1rj  于 2023-02-04  发布在  Python
关注(0)|答案(2)|浏览(194)

在eclipse(以及其他几个IDE)中有一个选项,可以在达到一定的命中次数后打开断点。在Python的pdb中,有断点的命中次数和condition命令。如何连接它们?

k4emjkb1

k4emjkb11#

我找到了答案,其实很简单,有一个命令叫做ignore,假设你想在第9行的断点处,点击1000次后断点:

b 9

输出:Breakpoint 1 at ...

ignore 1 1000

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

c
iq0todco

iq0todco2#

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

**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

相关问题