[python] - pdb调试

1. 代码如下:

# coding=gbk
import pdb

def pdb_test(arg):
    for i in range(arg):
        print (i)
    return arg

pdb.run("pdb_test(3)")

2. 调试过程

> <string>(1)<module>()
(Pdb) b pdb_test
Breakpoint 1 at c:\users\administrator\workspace\practice\src\practice_test\dailytest.py:4
(Pdb) b
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at c:\users\administrator\workspace\practice\src\practice_test\dailytest.py:4
(Pdb) c
> c:\users\administrator\workspace\practice\src\practice_test\dailytest.py(5)pdb_test()
-> for i in range(arg):
(Pdb) l
  1      # coding=gbk
  2      import pdb
  3      
  4 B    def pdb_test(arg):
  5  ->        for i in range(arg):
  6              print (i)
  7          return arg
  8      
  9      pdb.run("pdb_test(3)")
 10      
 11      
(Pdb) n
> c:\users\administrator\workspace\practice\src\practice_test\dailytest.py(6)pdb_test()
-> print (i)
(Pdb) p i
0
(Pdb) n
0
> c:\users\administrator\workspace\practice\src\practice_test\dailytest.py(5)pdb_test()
-> for i in range(arg):
(Pdb) n
> c:\users\administrator\workspace\practice\src\practice_test\dailytest.py(6)pdb_test()
-> print (i)
(Pdb) p i
1
(Pdb) n
1
> c:\users\administrator\workspace\practice\src\practice_test\dailytest.py(5)pdb_test()
-> for i in range(arg):
(Pdb) n
> c:\users\administrator\workspace\practice\src\practice_test\dailytest.py(6)pdb_test()
-> print (i)
(Pdb) p i
2
(Pdb) n
2
> c:\users\administrator\workspace\practice\src\practice_test\dailytest.py(5)pdb_test()
-> for i in range(arg):
(Pdb) n
> c:\users\administrator\workspace\practice\src\practice_test\dailytest.py(7)pdb_test()
-> return arg
(Pdb) p arg
3
(Pdb) a
arg = 3
(Pdb) n
--Return--
> c:\users\administrator\workspace\practice\src\practice_test\dailytest.py(7)pdb_test()->3
-> return arg
(Pdb) exit

3. 调试命令详解

b设置断点:

(Pdb) b pdb_test
Breakpoint 1 at c:\users\administrator\workspace\practice\src\practice_test\dailytest.py:4

b查看断点:

(Pdb) b
Num Type         Disp Enb   Where
1   breakpoint   keep yes   at c:\users\administrator\workspace\practice\src\practice_test\dailytest.py:4

c运行程序,直到遇到断点:

(Pdb) c
> c:\users\administrator\workspace\practice\src\practice_test\dailytest.py(5)pdb_test()
-> for i in range(arg):

l查看断点周围的代码:

(Pdb) l
  1      # coding=gbk
  2      import pdb
  3      
  4 B    def pdb_test(arg):
  5  ->        for i in range(arg):
  6              print (i)
  7          return arg
  8      
  9      pdb.run("pdb_test(3)")
 10      
 11      

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

(Pdb) n
> c:\users\administrator\workspace\practice\src\practice_test\dailytest.py(6)pdb_test()

p查看表达式的值:

-> print (i)
(Pdb) p i
0
(Pdb) n
0
> c:\users\administrator\workspace\practice\src\practice_test\dailytest.py(5)pdb_test()
-> for i in range(arg):
(Pdb) n
> c:\users\administrator\workspace\practice\src\practice_test\dailytest.py(6)pdb_test()
-> print (i)
(Pdb) p i
1
(Pdb) n
1
> c:\users\administrator\workspace\practice\src\practice_test\dailytest.py(5)pdb_test()
-> for i in range(arg):
(Pdb) n
> c:\users\administrator\workspace\practice\src\practice_test\dailytest.py(6)pdb_test()
-> print (i)
(Pdb) p i
2
(Pdb) n
2
> c:\users\administrator\workspace\practice\src\practice_test\dailytest.py(5)pdb_test()
-> for i in range(arg):
(Pdb) n
> c:\users\administrator\workspace\practice\src\practice_test\dailytest.py(7)pdb_test()
-> return arg
(Pdb) p arg
3

a查看参数:

(Pdb) a
arg = 3

终止并退出:

(Pdb) exit

 

参考:

http://www.cnblogs.com/chencheng/archive/2013/07/07/3161778.html 

http://blog.csdn.net/carolzhang8406/article/details/6923997

你可能感兴趣的:([python] - pdb调试)