python pdb调试方法分享
时间:2022-02-15 22:39:22
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)
标签:python,pdb,调试
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SQL Server讲堂:备份和恢复措施
2009-05-22 10:21:00
js+csss实现的一个带复选框的下拉框
2023-08-18 03:11:19
![](https://img.aspxhome.com/file/2023/9/56099_0s.gif)
技术性击倒与抬杠
2009-02-12 12:28:00
DBA经验:如何进行MySQL数据库表的故障检测
2009-02-12 17:37:00
详解Laravel服务容器的优势
2023-10-31 03:36:04
HTML语言将加入硬件操作功能
2009-12-16 19:19:00
![](https://img.aspxhome.com/file/UploadPic/200912/16/01-55s.jpg)
js调用flash代码
2010-01-23 12:35:00
形式追随内容?
2010-03-07 15:55:00
![](https://img.aspxhome.com/file/UploadPic/20103/7/ryan100224_01-77s.jpg)
sql之IN和BETWEEN条件运算
2007-09-11 13:35:00
CSS的优先级与特殊性
2008-06-24 11:36:00
页面重构应注意的repaint和reflow
2011-03-31 17:11:00
从IIS到SQL Server数据库安全
2008-12-24 15:58:00
Christopher Schmitt 谈学习CSS的益处
2008-07-13 14:15:00
Python浅析迭代器Iterator的使用
2023-11-07 12:04:25
js处理自己不能定义二维数组的方法详解
2023-09-06 21:25:12
下一站:HandlerSocket!
2011-04-11 09:02:00
原来CSS也可以把IE6弄死
2007-08-14 09:30:00
sqlplus登录\\连接命令、sqlplus命令的使用大全
2023-07-01 08:16:31
八条常见的CSS错误及修复方法
2010-04-08 16:54:00
![](https://img.aspxhome.com/file/UploadPic/20104/8/spec-18s.jpg)
流行WEB开发语言比较之ASP篇
2007-12-23 17:23:00