Reprint Address: https://blog.csdn.net/theonegis/article/details/52211799
Python provides a PDB for debugging tools similar to C + + GDB, and we can use the PDB under Linux to debug Python programs under the command line.
Official Reference website:
Python2:https://docs.python.org/2/library/pdb.html
Python3:https://docs.python.org/3/library/pdb.html
Generally, we can enter debugging in the following way (for example, the source file we want to debug is hello.py):
1. Start the target program at the command line, plus the-m parameter.
PYTHON-M PDB hello.py
The program will automatically stop at the first line, waiting for you to debug, as shown below. We can debug using debug commands, similar to using IED debugging.
2. Enable debugging in a python interactive environment
We can add the following code where the source file is to be debugged:
Import PDB
pdb.set_trace ()
You can then automatically enter the PDB debug mode in an interactive environment, similar to the previous debugging method.
The following is a common debugging command (detailed information refer to the official website documentation):
Breakpoint Related commands:
To set a breakpoint: (PDB) b 8 #断点设置该文件的第8行 (b is the first letter of the break)
Show All Breakpoints: (Pdb) b #b命令, no parameters, show all breakpoints
Delete breakpoint: (PDB) CL 2 #删除第2个断点 (first letter of Clear)
Step over: (PDB) n #单步执行, next first letter
Step into: (PDB) s #step的首字母
SETP return: (Pdb) R #return的首字母
Resume: (Pdb) C #continue的首字母
Run To Line: (PDB) J #运行到地10行, Jump's first letter
(Pdb) p param #查看当前param变量值
(PDB) L #查看运行到某处代码
(PDB) A #查看全部栈内变量
(Pdb) H #帮助, first letter of Help
(Pdb) Q #退出, first letter of quit