python: in pdb is it possible to enable a breakpoint only after n hit counts?

浪子不回头ぞ 提交于 2019-11-29 11:06:29

问题


In eclipse (and several other IDE's as well) there is an option to turn on the breakpoint only after a certain number of hits. In Python's pdb there is a hit count for breakpoints and there is the condition command. How do I connect them?


回答1:


Conditional Breakpoints can be set in 2 ways -

FIRST: specify the condition when the breakpoint is set using 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 can also be applied to an existing breakpoint using the condition command. The arguments are the breakpoint ID and the expression.

$ 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

UPDATE: I wrote a simpler code

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

debugging on terminal -

$ 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



回答2:


I found the answer. It's pretty easy actually, there's a command called ignore let's say you want to break at breakpoint in line 9 after 1000 hits:

b 9

Output: Breakpoint 2 at ...

ignore 1 1000

Output: Will ignore next 1000 crossings of breakpoint 1.

 c


来源:https://stackoverflow.com/questions/14139817/python-in-pdb-is-it-possible-to-enable-a-breakpoint-only-after-n-hit-counts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!