pdb在python程序中应用

1.什么是pdb?

  pdb是python提供的调试程序的一种工具。

2.为什么需要pdb模块?

  当我们的程序越写越大的时候,我们用print xxx 这种方式打断点,调试,非常不方便,这个时候我们需要专业的调试工具

3 如何使用pdb模块?

#!/usr/bin/env python
#coding:utf-8


import pdb

def a():
    print "this is a"

def b():
    print "this is b"

def c():
    print "this is c"

def main():
    pdb.set_trace()
    a()
    print "a is done"
    print "---------------"
    pdb.set_trace()
    b()
    print "b is done"
    print "---------------"
    pdb.set_trace()
    c()
    print "c is done"
    print "--------------"
    

main()

 

 我们写了这样一个程序,在行首import了pdb模块

然后在需要打断点插入这么一行代码  pdb.set_trace(),然后我们在执行的时候就变成这样

root@SSS:/data/last/rebootMon/nbNet# python pdb_test.py 
> /data/last/rebootMon/nbNet/pdb_test.py(18)main()
-> a()
(Pdb) 

 这个时候我们可以敲入对应指令,达到对应效果

      n  --->  下一行:

   

 start
> /data/last/rebootMon/nbNet/pdb_test.py(19)main()
-> a()
(Pdb) n
this is a
> /data/last/rebootMon/nbNet/pdb_test.py(20)main()
-> print "a is done"
(Pdb) n
a is done
> /data/last/rebootMon/nbNet/pdb_test.py(21)main()
-> print "---------------"
(Pdb) n
---------------
> /data/last/rebootMon/nbNet/pdb_test.py(22)main()
-> pdb.set_trace()
(Pdb) n
> /data/last/rebootMon/nbNet/pdb_test.py(23)main()
-> b()
(Pdb) n
this is b
> /data/last/rebootMon/nbNet/pdb_test.py(24)main()
-> print "b is done"
(Pdb) n
b is done
> /data/last/rebootMon/nbNet/pdb_test.py(25)main()
-> print "---------------"
(Pdb) n
---------------
> /data/last/rebootMon/nbNet/pdb_test.py(26)main()
-> pdb.set_trace()
(Pdb) n
> /data/last/rebootMon/nbNet/pdb_test.py(27)main()
-> c()
(Pdb) n
this is c
> /data/last/rebootMon/nbNet/pdb_test.py(28)main()
-> print "c is done"
(Pdb) n
c is done
> /data/last/rebootMon/nbNet/pdb_test.py(29)main()
-> print "--------------"
(Pdb) n
--------------
--Return--

 

 我们可以看到当代码执行到一个断点的位置就会阻塞住,然后我们每敲一次n都会往下执行一行代码。  

  p  --->  打印:

 假如我们程序是这样

#!/usr/bin/env python
#coding:utf-8


import pdb

def a(msg):
    print "this is %s",msg

def b(msg):
    print "this is %s"% msg

def c(msg):
    print "this is %s"%msg

def main():
    msg1 = 'a'
    msg2 = 'b'
    msg3 = 'c'
    print "start"
    pdb.set_trace()
    a(msg1)
    print "a is done"
    print "---------------"
    pdb.set_trace()
    b(msg2)
    print "b is done"
    print "---------------"
    pdb.set_trace()
    c(msg3)
    print "c is done"
    print "--------------"
    

main()

 我们想打印变量的值,就可以这样

root@SSS:/data/last/rebootMon/nbNet# python pdb_test.py 
start
> /data/last/rebootMon/nbNet/pdb_test.py(22)main()
-> a(msg1)
(Pdb) p msg1
'a'
(Pdb) 

 

   l  ---> 展示:

   

root@SSS:/data/last/rebootMon/nbNet# python pdb_test.py 
start
> /data/last/rebootMon/nbNet/pdb_test.py(22)main()
-> a(msg1)
(Pdb) l
 17  	    msg1 = 'a'
 18  	    msg2 = 'b'
 19  	    msg3 = 'c'
 20  	    print "start"
 21  	    pdb.set_trace()
 22  ->	    a(msg1)
 23  	    print "a is done"
 24  	    print "---------------"
 25  	    pdb.set_trace()
 26  	    b(msg2)
 27  	    print "b is done"
(Pdb) n
this is %s a
> /data/last/rebootMon/nbNet/pdb_test.py(23)main()
-> print "a is done"
(Pdb) n
a is done
> /data/last/rebootMon/nbNet/pdb_test.py(24)main()
-> print "---------------"
(Pdb) l
 19  	    msg3 = 'c'
 20  	    print "start"
 21  	    pdb.set_trace()
 22  	    a(msg1)
 23  	    print "a is done"
 24  ->	    print "---------------"
 25  	    pdb.set_trace()
 26  	    b(msg2)
 27  	    print "b is done"
 28  	    print "---------------"
 29  	    pdb.set_trace()

 

   b ---> 动态生成断点

       我们先重写代码

#!/usr/bin/env python
#coding:utf-8


import pdb

def a(msg):
    print "this is %s",msg

def b(msg):
    print "this is %s"% msg

def c(msg):
    print "this is %s"%msg

def main():
    msg1 = 'a'
    msg2 = 'b'
    msg3 = 'c'
    print "start"
    while 1:
        pdb.set_trace()
        print "a is start"
        a(msg1)
        print "a is done"
        print "---------------"
        pdb.set_trace()
        print "b is start"
        b(msg2)
        print "b is done"
        print "---------------"
        pdb.set_trace()
        print "c is start"
        c(msg3)
        print "c is done"
        print "--------------"
    

main()

 

-> print "a is start"
(Pdb) l
 18  	    msg2 = 'b'
 19  	    msg3 = 'c'
 20  	    print "start"
 21  	    while 1:
 22  	        pdb.set_trace()
 23  ->	        print "a is start"
 24  	        a(msg1)
 25  	        print "a is done"
 26  	        print "---------------"
 27  	        pdb.set_trace()
 28  	        print "b is start"
(Pdb) b 25
Breakpoint 1 at /data/last/rebootMon/nbNet/pdb_test.py:25
(Pdb) n
a is start
> /data/last/rebootMon/nbNet/pdb_test.py(24)main()
-> a(msg1)
(Pdb) n
this is %s a
> /data/last/rebootMon/nbNet/pdb_test.py(25)main()
-> print "a is done"
(Pdb) n
a is done
> /data/last/rebootMon/nbNet/pdb_test.py(26)main()
-> print "---------------"

我们 输入 b25 增加了一个断点

然后我们输入n 就跳到 25行。

q --退出:

  退出交互

---使用动态变量

(Pdb) !x = 123
(Pdb) p x
123

 

 

 

 

posted @ 2018-05-15 14:26  nerdlerss  阅读(967)  评论(0编辑  收藏  举报