b 命令定义断点。您可以指定条件,但不必这样做。
condition 命令需要一个已经定义的断点(即之前需要一个 b 命令)并更改它的条件。
以下两个调试器命令可以认为是相等的:
b debugme:3,x==100
和
b debugme:3
condition 1 x==100
让我们开始实践吧:
创建以下 Python 脚本并将其命名为 debugme.py:
def myfunction():
for x in range(0,200):
print x
if __name__ == "__main__":
myfunction()
语法 1 演练(b 带条件):
C:\tmp\py>C:\Python27\python.exe
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pdb
>>> import debugme
>>> pdb.run('debugme.myfunction()')
> <string>(1)<module>()
(Pdb) b debugme:3,x==100 <---- note this line
Breakpoint 1 at c:\tmp\py\debugme.py:3
(Pdb) c
0
1
2
3
...
97
98
99
> c:\tmp\py\debugme.py(3)myfunction()
-> print x
(Pdb) p x
100
(Pdb) q
>>> exit()
语法2演练(b无条件加条件):
C:\tmp\py>C:\Python27\python.exe
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pdb
>>> import debugme
>>> pdb.run('debugme.myfunction()')
> <string>(1)<module>()
(Pdb) b debugme:3 <---- note this line
Breakpoint 1 at c:\tmp\py\debugme.py:3
(Pdb) b
Num Type Disp Enb Where
1 breakpoint keep yes at c:\tmp\py\debugme.py:3
(Pdb) condition 1 x==100 <---- and this one
(Pdb) b
Num Type Disp Enb Where
1 breakpoint keep yes at c:\tmp\py\debugme.py:3
stop only if x==100
(Pdb) c
0
1
2
3
...
97
98
99
> c:\tmp\py\debugme.py(3)myfunction()
-> print x
(Pdb) p x
100
(Pdb) q
>>> exit()