python程序使用pdb调试

常用命令:

break 或 b       设置断点
continue 或 c    继续执行程序, 或是跳到下个断点
list 或 l        查看当前行的代码段
step 或 s        进入函数
return 或 r      执行代码直到从当前函数返回
exit 或 q        中止并退出
next 或 n        执行下一行
p 或!            打印变量的值,例如p a
help 或 h        帮助

程序代码:

1.py

def Test():
    "注释"
    print("test")

a = 11
Test()
b = 22
c = 33

调试

shihao@shihao-PC:~/Desktop$ python -m pdb 1.py      #进入调试
> /home/shihao/Desktop/1.py(1)<module>()
-> def Test():
(Pdb) l                         #显示代码
  1  -> def Test():             #箭头指向当前运行的位置
  2         "注释"
  3         print("test")
  4     
  5     a = 11
  6     Test()
  7     b = 22
  8     c = 33
[EOF]
(Pdb) n         #执行下一行
> /home/shihao/Desktop/1.py(5)<module>()
-> a = 11
(Pdb) l             #显示
  1     def Test():
  2         "注释"
  3         print("test")
  4     
  5  -> a = 11         #箭头指向当前运行行
  6     Test()
  7     b = 22
  8     c = 33
[EOF]
(Pdb) n                 #执行下一行
> /home/shihao/Desktop/1.py(6)<module>()
-> Test()
(Pdb) l               #显示
  1     def Test():
  2         "注释"
  3         print("test")
  4     
  5     a = 11
  6  -> Test()
  7     b = 22
  8     c = 33
[EOF]
(Pdb) s           #进入函数
--Call--
> /home/shihao/Desktop/1.py(1)Test()
-> def Test():
(Pdb) l          #显示
  1  -> def Test():
  2         "注释"
  3         print("test")
  4     
  5     a = 11
  6     Test()
  7     b = 22
  8     c = 33
[EOF]
(Pdb) n          #执行下一行
> /home/shihao/Desktop/1.py(3)Test()
-> print("test")
(Pdb) l
  1     def Test():
  2         "注释"
  3  ->     print("test")
  4     
  5     a = 11
  6     Test()
  7     b = 22
  8     c = 33
[EOF]
(Pdb) n         #执行下一行
test
--Return--
> /home/shihao/Desktop/1.py(3)Test()->None
-> print("test")
(Pdb) l         #显示
  1     def Test():
  2         "注释"
  3  ->     print("test")
  4     
  5     a = 11
  6     Test()
  7     b = 22
  8     c = 33
[EOF]
(Pdb) n         #执行下一行
> /home/shihao/Desktop/1.py(7)<module>()
-> b = 22
(Pdb) l
  2         "注释"
  3         print("test")
  4     
  5     a = 11
  6     Test()
  7  -> b = 22
  8     c = 33
[EOF]
(Pdb) b 8       #给第8行设置断点
Breakpoint 1 at /home/shihao/Desktop/1.py:8
(Pdb) c         #执行到下一断点处
> /home/shihao/Desktop/1.py(8)<module>()
-> c = 33
(Pdb) l
  3         print("test")
  4     
  5     a = 11
  6     Test()
  7     b = 22
  8 B-> c = 33         #B表示该处有断点
[EOF]
(Pdb) clear 1       #删除第一个断点
Deleted breakpoint 1 at /home/shihao/Desktop/1.py:8     #显示第一个断点在第八行
(Pdb) n     #执行1行
--Return--      #提示程序执行到最后
> /home/shihao/Desktop/传智学习/python核心编程/1.py(8)<module>()->None
-> c = 33
(Pdb) l     #显示程序
  3         print("test")
  4     
  5     a = 11
  6     Test()
  7     b = 22
  8  -> c = 33      #前面的B已经消失,说明断点已经删除
[EOF]
(Pdb) p a           #打印a的值
11
(Pdb) p b           #打印b的值
22
(Pdb) q             #输入q退出

猜你喜欢

转载自blog.csdn.net/duke10/article/details/79854152