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? 
            Asked
            
        
        
            Active
            
        
            Viewed 1.6k times
        
    2 Answers
22
            
            
        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)
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
 
    
    
        Srikar Appalaraju
        
- 71,928
- 54
- 216
- 264
- 
                    is there, instead of `j`, a variable that holds the hit count for this breakpoint? – zenpoy Jan 03 '13 at 13:46
- 
                    `j` is the current loop iteration. what do you mean "hit count for this breakpoint" ? – Srikar Appalaraju Jan 03 '13 at 13:58
22
            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 1 at ...
ignore 1 1000
Output: Will ignore next 1000 crossings of breakpoint 1.
 c
 
    
    
        zenpoy
        
- 19,490
- 9
- 60
- 87