现在的位置: 首页 > 综合 > 正文

Python 使用pdb进行简单调试

2013年10月09日 ⁄ 综合 ⁄ 共 4133字 ⁄ 字号 评论关闭

Python 使用pdb进行简单调试

pdb 是 python 自带的一个包,为 python 程序提供了一种交互的源代码调试功能,主要特性包括设置断点、单步调试、进入函数调试、查看当前代码、查看栈片段、动态改变变量的值等。pdb 提供了一些常用的调试命令,详情见表 1。

表 1. pdb 常用命令
命令 解释
break 或 b 设置断点
设置断点
continue 或 c         继续执行程序
list 或 l         查看当前行的代码段
step 或 s 进入函数
return 或 r 执行代码直到从当前函数返回
exit 或 q         中止并退出
next 或 n         执行下一行
pp 打印变量的值
help 帮助

下面结合具体的实例讲述如何使用 pdb 进行调试。

#!/usr/bin/env python
# 1.py
# use UTF-8
# Python 3.3.0

# pdb简单调试例子
import pdb

a = "aaa"
pdb.set_trace()
b = "bbb"
c = "ccc"
final = a + b + c
print(final)

1. 开始调试

直接运行脚本,会停留在 pdb.set_trace() 处,选择 n + 回车 可以执行当前的 statement。在第一次按下了
 n + 回车 之后可以直接按 enter 表示重复执行上一条 debug 命令。

[root@rcc-pok-idg-2255 ~]#  python 1.py 
> /root/1.py(4)?() 
-> b = "bbb"
(Pdb) n
> /root/1.py(5)?() 
-> c = "ccc"
(Pdb) 直接回车
> /root/1.py(6)?() 
-> final = a + b + c 
(Pdb) list 
1     import pdb 
2     a = "aaa"
3     pdb.set_trace() 
4     b = "bbb"
5     c = "ccc"
6  -> final = a + b + c 
7     print final 
[EOF] 
(Pdb) 
[EOF] 
(Pdb)
> /root/1.py(7)?() 
-> print final 
(Pdb)

2. 退出 debug

使用 quit 或者 q 可以退出当前的 debug,但是 quit 会以一种非常粗鲁的方式退出程序,其结果是直接 crash。

[root@rcc-pok-idg-2255 ~]#  python 1.py 
> /root/1.py(4)?() 
-> b = "bbb"
(Pdb)
> /root/1.py(5)?() 
-> c = "ccc"
(Pdb)
Traceback (most recent call last): 
File "1.py", line 5, in ? 
c = "ccc"
File "1.py", line 5, in ? 
c = "ccc"
File "/usr/lib64/python2.4/bdb.py", line 48, in trace_dispatch 
return self.dispatch_line(frame) 
File "/usr/lib64/python2.4/bdb.py", line 67, in dispatch_line 
if self.quitting: raise BdbQuit 
bdb.BdbQuit

3. 打印变量的值

如果需要在调试过程中打印变量的值,可以直接使用 p 加上变量名,但是需要注意的是打印仅仅在当前的 statement 已经被执行了之后才能看到具体的值,否则会报 NameError: < exceptions.NameError … ....> 错误。

[root@rcc-pok-idg-2255 ~]#  python 1.py 
> /root/1.py(4)?() 
-> b = "bbb"
(Pdb)
> /root/1.py(5)?() 
-> c = "ccc"
(Pdb) p b 
'bbb'
(Pdb) 
'bbb'
(Pdb) n 
> /root/1.py(6)?() 
-> final = a + b + c 
(Pdb) p c 
'ccc'
(Pdb) p final 
*** NameError: <exceptions.NameError instance at 0x1551b710 > 
(Pdb)
> /root/1.py(7)?() 
-> print final 
(Pdb) p final 
'aaabbbccc'
(Pdb)

4. 停止调试, 让程序继续执行

使用 c 可以停止当前的 debug 使程序继续执行。如果在下面的程序中继续有 set_statement() 的申明,则又会重新进入到 debug 的状态,读者可以在代码 print final 之前再加上 set_trace() 验证。


[root@rcc-pok-idg-2255 ~]#  python 1.py 
> /root/1.py(4)?() 
-> b = "bbb"
(Pdb)
> /root/1.py(5)?() 
-> c = "ccc"
(Pdb)
aaabbbccc

5. 显示代码

在 debug 的时候不一定能记住当前的代码块,如要要查看具体的代码块,则可以通过使用 list 或者 l
命令显示。list 会用箭头 -> 指向当前 debug 的语句。

[root@rcc-pok-idg-2255 ~]#  python 1.py 
> /root/1.py(4)?() 
-> b = "bbb"
(Pdb) list 
1     import pdb 
2     a = "aaa"
3     pdb.set_trace() 
4  -> b = "bbb"
5     c = "ccc"
6     final = a + b + c 
7     pdb.set_trace() 
8     print final 
[EOF] 
(Pdb)
> /root/1.py(8)?() 
-> print final 
(Pdb) list 
3     pdb.set_trace() 
4     b = "bbb"
5     c = "ccc"
6     final = a + b + c 
7     pdb.set_trace() 
8  -> print final 
[EOF] 
(Pdb)

6. 在使用函数的情况下进行 debug

#!/usr/bin/env python
# 2.py
# use UTF-8
# Python 3.3.0

import pdb 
def combine(s1,s2):			# define subroutine combine, which... 
	s3 = s1 + s2 + s1    	# sandwiches s2 between copies of s1, ... 
	s3 = '"' + s3 +'"'   	# encloses it in double quotes,... 
	return s3            	# and returns it. 
	
a = "aaa"
pdb.set_trace() 
b = "bbb"
c = "ccc"
final = combine(a,b) 
print(final)

如果直接使用 n 进行 debug 则到 final=combine(a,b) 这句的时候会将其当做普通的赋值语句处理,进入到 print final。如果想要对函数进行 debug 如何处理呢 ?
可以直接使用 s 进入函数块。函数里面的单步调试与上面的介绍类似。如果不想在函数里单步调试可以在断点处直接按 r 退出到调用的地方。

[root@rcc-pok-idg-2255 ~]# python 2.py 
> /root/2.py(10)?() 
-> b = "bbb"
(Pdb)
> /root/2.py(11)?() 
-> c = "ccc"
(Pdb)
> /root/2.py(12)?() 
-> final = combine(a,b) 
(Pdb)
--Call-- 
> /root/2.py(3)combine() 
-> def combine(s1,s2):      # define subroutine combine, which... 
(Pdb)
> /root/2.py(4)combine() 
-> s3 = s1 + s2 + s1    # sandwiches s2 between copies of s1, ... 
(Pdb) list 
1     import pdb 

3     def combine(s1,s2):      # define subroutine combine, which... 
4  ->     s3 = s1 + s2 + s1    # sandwiches s2 between copies of s1, ... 
5         s3 = '"' + s3 +'"'   # encloses it in double quotes,... 
6         return s3            # and returns it. 

8     a = "aaa"
9     pdb.set_trace() 
10     b = "bbb"
11     c = "ccc"
(Pdb)
> /root/2.py(5)combine() 
-> s3 = '"' + s3 +'"'   # encloses it in double quotes,... 
(Pdb)
> /root/2.py(6)combine() 
-> return s3            # and returns it. 
(Pdb)
--Return-- 
> /root/2.py(6)combine()->'"aaabbbaaa"'
-> return s3            # and returns it. 
(Pdb)
> /root/2.py(13)?() 
-> print final 
(Pdb)


7. 在调试的时候动态改变变量值

在调试的时候可以动态改变变量的值,具体如下实例。

需要注意的是下面有个错误,原因是 b 已经被赋值了, 如果想重新改变 b 的赋值, 在变量前加一个"!", 例如 !b= "jjjj"


[root@rcc-pok-idg-2255 ~]# python 2.py 
> /root/2.py(10)?() 
-> b = "bbb"
(Pdb) var = "1234"
(Pdb) b = "avfe"
*** The specified object '= "avfe"' is not a function 
or was not found along sys.path. 
(Pdb) !b="afdfd"
(Pdb)

pdb 调试有个明显的缺陷就是对于多线程,远程调试等支持得不够好,同时没有较为直观的界面显示,不太适合大型的 python 项目。而在较大的 python 项目中,这些调试需求比较常见,因此需要使用更为高级的调试工具。

PyCharm IDE不错.

转自: http://www.ibm.com/developerworks/cn/linux/l-cn-pythondebugger/

抱歉!评论已关闭.