python pdb调试方法分享
import pdb
def pdb_test(arg):
for i in range(arg):
print(i)
return arg
pdb.run("pdb_test(3)")
b 函数名、行号:
打断点,b可以查询所有的断点。
(pdb) b pdb_test
breakpoint 1 at c:\users\plpcc\desktop\pdbtest.py:3
(pdb) b
num type disp enb where
breakpoint keep yes at c:\users\plpcc\desktop\pdbtest.py:3
c:
运行程序,直到遇到断点。
(pdb) c
> c:\users\plpcc\desktop\pdbtest.py(4)pdb_test()
-> for i in range(arg):
l:
查看断点周围的代码
(pdb) l
import pdb
b def pdb_test(arg):
-> for i in range(arg):
print(i)
return arg
pdb.run("pdb_test(3)")
a:
查看参数
(pdb) a
arg = 3
s, n:
单步运行,区别s会进入路径中的函数,n不会进入
p:
查看表达式的值
(pdb) p i
condition:
条件断点,只有条件为true断点才命中
> c:\users\plpcc\desktop\pdbtest.py(5)pdb_test()
-> print(i)
(pdb) l
import pdb
def pdb_test(arg):
for i in range(arg):
b-> print(i)
return arg
pdb.run("pdb_test(3)")
[eof]
(pdb) b
num type disp enb where
breakpoint keep yes at c:\users\plpcc\desktop\pdbtest.py:5
(pdb) condition 2 i==1 //i==1时才触发断点2
new condition set for breakpoint 2.
(pdb) b
num type disp enb where
breakpoint keep yes at c:\users\plpcc\desktop\pdbtest.py:5
stop only if i==1
(pdb) c
//i==0直接打印未断住
> c:\users\plpcc\desktop\pdbtest.py(5)pdb_test()
-> print(i) //触发断点,i==1
(pdb) p i
bt:
查看调用堆栈
(pdb) bt
c:\python33\lib\bdb.py(405)run()
-> exec(cmd, globals, locals)
<string>(1)<module>()
> c:\users\plpcc\desktop\pdbtest.py(5)pdb_test()
-> print(i)
r:
执行到函数返回
(pdb) r
--return--
> c:\users\plpcc\desktop\pdbtest.py(6)pdb_test()->3 //代码位置、函数返回值->3
-> return arg //代码位置的语句
(pdb) l
import pdb
def pdb_test(arg):
for i in range(arg):
print(i)
-> return arg
pdb.run("pdb_test(3)")
通过pdb.set_trace() 在代码中指定位置嵌入一个断点,通常可以通过调试开关来控制
import pdb
__debug__ = true
def pdb_test(arg):
if true == __debug__:
pdb.set_trace()
for i in range(arg):
print(i)
return arg
pdb_test(3)
运行后在pdb.set_trace()位置被断住,当__debug__ = false,代码正常运行
> c:\users\plpcc\desktop\pdbtest.py(8)pdb_test()
-> for i in range(arg):
(pdb) l
__debug__ = true
def pdb_test(arg):
if true == __debug__:
pdb.set_trace()
-> for i in range(arg):
print(i)
return arg
pdb_test(3)
[eof]
通过pdb.pm()进行事后调试,可以跟踪异常程序最后的堆载信息:
traceback (most recent call last):
file "c:\users\plpcc\desktop\pdbtest.py", line 13, in <module>
pdb_test(3)
file "c:\users\plpcc\desktop\pdbtest.py", line 10, in pdb_test
1/0
zerodivisionerror: division by zero
>>> import pdb
>>> pdb.pm()
> c:\users\plpcc\desktop\pdbtest.py(10)pdb_test()
-> 1/0
(pdb) l
def pdb_test(arg):
if true == __debug__:
pdb.set_trace()
for i in range(arg):
print(i)
-> 1/0
return arg
pdb_test(3)
上一篇: PHP完全二叉树定义与实现方法示例
下一篇: 彩票模式和电子商务结合的一种网站盈利模式