pdb调试python程序

Python自带的pdb库,发现用pdb来调试程序还是很方便的
用pdb调试有多种方式可选:
1. 命令行启动目标程序,加上-m参数,这样调用myscript.py的话断点就是程序的执行第一行之前

python -m pdb myscript.py


2 在Python交互环境中启用调试
>>> import pdb
>>> import mymodule
>>> pdb.run(‘mymodule.test()’)

3. 比较常用的,就是在程序中间插入一段程序,相对于在一般IDE里面打上断点然后启动debug,不过这种方式是hardcode的

if __name__ == "__main__":
a = 1
import pdb
pdb.set_trace()
b = 2
c = a + b
print (c)
然后正常运行脚本,到了pdb.set_trace()那就会定下来,就可以看到调试的提示符(Pdb)了

常用的调试命令

h(elp),会打印当前版本Pdb可用的命令,如果要查询某个命令,可以输入 h [command],例如:“h l” — 查看list命令 
l(ist),可以列出当前将要运行的代码块 
(Pdb) l
497 pdb.set_trace()
498 base_data = {}
499 new_data = {}
500 try:
501 execfile(base_file_name,{},base_data)
502 -> execfile(new_file_name,{},new_data)
503 except:
504 logger.writeLog(“error! load result log error!”)
505 print “load cmp logs error!”
506 raise Exception, “load cmp logs error!”
507 

b(reak), 设置断点,例如 “b 77″,就是在当前脚本的77行打上断点,还能输入函数名作为参数,断点就打到具体的函数入口,如果只敲b,会显示现有的全部断点 
(Pdb) b 504
Breakpoint 4 at /home/jchen/regression/regressionLogCMP.py:504

condition bpnumber [condition],设置条件断点,下面语句就是对第4个断点加上条件“a==3” 
(Pdb) condition 4 a==3
(Pdb) b
Num Type Disp Enb Where
4 breakpoint keep yes at /home/jchen/regression/regressionLogCMP.py:504
stop only if a==3

cl(ear),如果后面带有参数,就是清除指定的断点(我在Python2.4上从来没成功过!!!);如果不带参数就是清除所有的断点 
(Pdb) cl
Clear all breaks? y

disable/enable,禁用/激活断点 
(Pdb) disable 3
(Pdb) b
Num Type Disp Enb Where
3 breakpoint keep no at /home/jchen/regression/regressionLogCMP.py:505

n(ext),让程序运行下一行,如果当前语句有一个函数调用,用n是不会进入被调用的函数体中的 

s(tep),跟n相似,但是如果当前有一个函数调用,那么s会进入被调用的函数体中 
c(ont(inue)),让程序正常运行,直到遇到断点 
j(ump),让程序跳转到指定的行数 
(Pdb) j 497
> /home/jchen/regression/regressionLogCMP.py(497)compareLog()
-> pdb.set_trace()

a(rgs),打印当前函数的参数 
(Pdb) a
_logger =
_base = ./base/MRM-8137.log
_new = ./new/MRM-8137.log
_caseid = 5550001
_toStepNum = 10
_cmpMap = {‘_bcmpbinarylog’: ‘True’, ‘_bcmpLog’: ‘True’, ‘_bcmpresp’: ‘True’}

p,最有用的命令之一,打印某个变量 
(Pdb) p _new
u’./new/MRM-8137.log’

!,感叹号后面跟着语句,可以直接改变某个变量 
q(uit),退出调试 
发现在命令行下调试程序也是一件挺有意思的事情,记录下来分享一下

w ,Print a stack trace, with the most recent frame at the bottom.An arrow indicates the "current frame", which determines the context of most commands. 'bt' is an alias for this command.

d ,Move the current frame one level down in the stack trace
(to a newer frame).

u ,Move the current frame one level up in the stack trace
(to an older frame).

使用 u 和 d 命令,我们可以在栈帧之间切换,用以获取其相关上下文变量信息。w可以显示最近的一些栈帧信息。

在交互环境中通常使用pdb.run来调试:

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
1    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
   1      import  pdb
   2    
   3  B   def  pdb_test(arg):
   4   - >      for  i in  range (arg):
   5              print (i)
   6          return  arg
   7    
   8      pdb.run( "pdb_test(3)" )

 

  a:

     查看参数

(Pdb) a
arg =  3

 

  s, n:

     单步运行,区别s会进入路径中的函数,n不会进入 

  p:

     查看表达式的值

(Pdb) p i
0

  condition:

     条件断点,只有条件为true断点才命中

> c:\users\plpcc\desktop\pdbtest.py( 5 )pdb_test()
- > print (i)
(Pdb) l
   1      import  pdb
   2    
   3      def  pdb_test(arg):
   4          for  i in  range (arg):
   5  B - >          print (i)
   6          return  arg
   7    
   8      pdb.run( "pdb_test(3)" )
[EOF]
(Pdb) b
Num Type          Disp Enb   Where
2    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
2    breakpoint   keep yes   at c:\users\plpcc\desktop\pdbtest.py: 5
     stop only if  i = = 1
(Pdb) c
0                        / / i = = 0 直接打印未断住
> c:\users\plpcc\desktop\pdbtest.py( 5 )pdb_test()
- > print (i)             / / 触发断点,i = = 1
(Pdb) p i
1

  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
1
2
- - Return - -
> c:\users\plpcc\desktop\pdbtest.py( 6 )pdb_test() - > 3  / / 代码位置、函数返回值 - > 3
- > return  arg                                       / / 代码位置的语句
(Pdb) l
   1      import  pdb
   2    
   3      def  pdb_test(arg):
   4          for  i in  range (arg):
   5              print (i)
   6   - >      return  arg
   7    
   8      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
   3      __DEBUG__ =  True
   4    
   5      def  pdb_test(arg):
   6          if  True  = =  __DEBUG__:
   7              pdb.set_trace()
   8   - >      for  i in  range (arg):
   9              print (i)
  10          return  arg
  11    
  12      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
   5      def  pdb_test(arg):
   6          if  True  = =  __DEBUG__:
   7              pdb.set_trace()
   8          for  i in  range (arg):
   9              print (i)
  10   - >          1 / 0
  11          return  arg
  12    
  13      pdb_test( 3 )

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值