python代码调试工具pdb
此工具适合在linux环境下使用。
pdb工具的使用方式一:
在python中使用pdb模块可以进行调试
import pdb
pdb.set_trace()
pdb工具的使用方式二:
可以使用python -m pdb mysqcript.py这样的方式,同gdb的调试方式差不多。
1 [root@localhost pp]# python -m pdb exp.py 2 > /home/sl/prog/pp/exp.py(4)<module>() 3 -> length = 5 4 (Pdb) n 5 > /home/sl/prog/pp/exp.py(5)<module>() 6 -> breadth = 2 7 (Pdb) l 8 1 #!/usr/bin/python 9 2 #Filename:exp.py 10 3 11 4 length = 5 12 5 -> breadth = 2 13 6 area = length*breadth 14 7 print 'Area is',area 15 8 print 'Perimeter is', 2 * (length + breadth) 16 9 17 10 18 11 19 (Pdb) n 20 > /home/sl/prog/pp/exp.py(6)<module>() 21 -> area = length*breadth 22 (Pdb) p area 23 *** NameError: NameError("name 'area' is not defined",) 24 (Pdb) n 25 > /home/sl/prog/pp/exp.py(7)<module>() 26 -> print 'Area is',area 27 (Pdb) p area 28 10 29 (Pdb)
pdb工具帮助信息的使用:
1 [root@localhost pp]# python -m pdb exp.py 2 > /home/sl/prog/pp/exp.py(4)<module>() 3 -> length = 5 4 (Pdb) help 5 6 Documented commands (type help <topic>): 7 ======================================== 8 EOF bt cont enable jump pp run unt 9 a c continue exit l q s until 10 alias cl d h list quit step up 11 args clear debug help n r tbreak w 12 b commands disable ignore next restart u whatis 13 break condition down j p return unalias where 14 15 Miscellaneous help topics: 16 ========================== 17 exec pdb 18 19 Undocumented commands: 20 ====================== 21 retval rv 22 23 (Pdb)
1 (Pdb) help b 2 b(reak) ([file:]lineno | function) [, condition] 3 With a line number argument, set a break there in the current 4 file. With a function name, set a break at first executable line 5 of that function. Without argument, list all breaks. If a second 6 argument is present, it is a string specifying an expression 7 which must evaluate to true before the breakpoint is honored. 8 9 The line number may be prefixed with a filename and a colon, 10 to specify a breakpoint in another file (probably one that 11 hasn't been loaded yet). The file is searched for on sys.path; 12 the .py suffix may be omitted. 13 (Pdb)
1 (Pdb) help c 2 c(ont(inue)) 3 Continue execution, only stop when a breakpoint is encountered. 4 (Pdb) help pp 5 pp expression 6 Pretty-print the value of the expression. 7 (Pdb)
查看帮助信息:
1 (Pdb) help help 2 h(elp) 3 Without argument, print the list of available commands. 4 With a command name as argument, print help about that command 5 "help pdb" pipes the full documentation file to the $PAGER // 可以将pdb的所有命令的帮助信息打印出来,进行查看。 6 "help exec" gives help on the ! command 7 (Pdb)
pdb工具使用说明:
可以使用python -m pdb mysqcript.py这样的方式,
1 (Pdb) 会自动停在第一行,等待调试,这时你可以看看 帮助 2 (Pdb) h 3 说明下这几个关键 命令 4 5 >断点设置 6 (Pdb)b 10 #断点设置在本py的第10行 7 或(Pdb)b ots.py:20 #断点设置到 ots.py第20行 8 删除断点(Pdb)b #查看断点编号 9 (Pdb)cl 2 #删除第2个断点 10 11 >运行 12 (Pdb)n #单步运行 13 (Pdb)s #细点运行 也就是会下到,方法 14 (Pdb)c #跳到下个断点 15 >查看 16 (Pdb)p param #查看当前 变量值 17 (Pdb)l #查看运行到某处代码 18 (Pdb)a #查看全部栈内变量 19 (Pdb)w 列出目前call stack 中的所在层。 20 (Pdb)d 在call stack中往下移一层 21 (Pdb)u 在call stack中往上移一层。如果在上移一层之后按下 n ,则会在上移之后的一层执行下一个叙述,之前的 function call 就自动返回。 22 (Pdb)cl 清除指定的断点。如果没有带参数,则清除所有断点。 23 (Pdb)disable 取消所有断点的功能,但仍然保留这些断点。 24 (Pdb)enable 恢复断点的功能。 25 (Pdb)ignore 设定断点的忽略次数。如果没指定 count,其初始 为 0。当 count 为 0 时,断点会正常动作。若有指定 count,则每次执行到该中断, count 就少 1,直到 count 数为 0。 26 (Pdb)condition bpnumber [condition] 27 (Pdb)j(ump) lineNo. 跳到某行执行。只有在 call stack 的最底部才能作用。 28 (Pdb)l 列出目前所在档案中的位置。连续地 l 命令会一直列到档案结尾,可以使用指定行数或范围来打印。 29 (Pdb)pp 和 p 命令类似,但是使用 pprint module(没用过 pprint,详情请参考 Python Library Reference)。 30 (Pdb)alias 以一个"别名"代替"一群除错命令",有点类似 c/c++ 的 macro(详情请参考 Python Library Reference)。