Debug PDB
The PDB is a command-line-based debugging tool, very similar to the GNU GDB (Debug/C + +).
Command |
Shorthand Command |
function |
Break |
B |
Set breakpoints |
Continue |
C |
Continue to execute the program |
List |
L |
View the code snippet for the current row |
Step |
S |
Enter function |
Return |
R |
Executes the code until it returns from the current function |
Quit |
Q |
Abort and exit |
Next |
N |
Executes the next line |
Print |
P |
Print the value of a variable |
Help |
H |
Help |
Args |
A |
View Incoming parameters |
|
Enter |
Repeat the previous command |
Break |
B |
Show All Breakpoints |
Break Lineno |
b Lineno |
Set a breakpoint on a specified line |
Break File:lineno |
b File:lineno |
Set a breakpoint on the line of the specified file |
Clear num |
|
Delete specified breakpoint |
Bt |
|
View function Call stack frame |
Debug at execution time
The program starts and stops waiting for single-step debugging on the first line.
-m pdb some.py
Interactive debugging
Go to Python or Ipython interpreter
import pdbpdb.run('testfun(args)'#此时会打开pdb调试,注意:先使用s跳转到这个testfun函数中,然后就可以使用l看到代码了
Bury the dots in the program.
Stop debugging when the program executes to the Pdb.set_trace () position
代码上下文...import pdb pdb.set_trace() ...
Example
5 demos that are debugged with a PDB
Demo 1
ImportPDB a= "AAA"Pdb.set_trace () b= "BBB"C= "CCC"Final=A+B+CPrint(final)#调试方法# "1 Display Code"# L----> can display the code in the current debugging process, in fact L means list #如下, where the,-> point indicates the location to be executed # 2 A = "AAA" # 3 Pdb.set_trace () # 4 B = "BBB" # 5 C = "CCC" # 6 Pdb.set_trace () # 7, final = a + B + C # 8 Print (final)# 2 Execute next line of code# N----> be able to execute a line of code down and then stop waiting to continue debugging N means next# 3 Viewing the value of a variable# p----> be able to see the value of the variable, p indicates the meaning of the Prit printout #例如: # p name indicates the value of view variable name
Demo 2
import="aaa"="bbb"="ccc"=++print(final)# 《4 将程序继续运行》# c----->让程序继续向下执行,与n的区别是n只会执行下面的一行代码,而c会像python xxxx.py一样 继续执行不会停止;c表示continue的意思# 《5 set_trace()》# 如果程序中有多个set_trace(),那么能够让程序在使用c的时候停留在下一个set_trace()位置处
Demo 3
#coding =utf-8ImportPdbdefCombine (S1,S2): S3=S1+S2+S1 S3= '"' +S3+'"' returns3a= "AAA"Pdb.set_trace () b= "BBB"C= "CCC"Final=Combine (A, B)Print(final)# "6 Set Breakpoints"# b----> Set breakpoints, that is, when using C, C can be encountered Set_trace () when the time of the stop, you can also encounter a breakpoint marked with the place stop; b means break. #例如: #b 11 Set a breakpoint on line 11th, note that this 11 can be used to get # (PDB) L # 4 s3 = s1 + s2 + S1 # 5 S3 = ' "' + s3 + '" ' # 6 return S3 # 7 A = "AAA" # 8 Pdb.set_trace () # 9, b = "BBB" # Ten C = "CCC" # One final = Combine (A, b) # Print (final) # [EOF] # (PDB) b One # breakpoint 1 at/users/wangmingdong/desktop/test3.py:11 # (PDB) C # >/users/wangmingdong/desktop/test3.py (one) <module> () #, Final = Combine (A, b) # (PDB) L # 6 return S3 # 7 A = "AAA" # 8 Pdb.set_trace () # 9 B = "BBB" # Ten C = "CCC" # one B-> final = Combine (A, b) # Print (final)# 7 Enter function to continue debugging# s----> enter function to continue debugging, if using n means that a function call as a statement to execute the past, and use S, will enter this function and stop #例如 # (PDB) L # 6 return S3 # 7 A = "AAA" # 8 Pdb.set_trace () # 9 B = "BBB" # Ten C = "CCC" # one B-> final = Combine (A, b) # Print (final) # [EOF] # (PDB) s #--call-- # >/users/wangmingdong/desktop/test3.py (3) combine () #-def Combine (S1,S2): # (PDB) L # 1 Import pdb # 2 # 3, def combine (S1,S2): # 4 s3 = s1 + s2 + S1 # 5 S3 = ' "' + s3 + '" ' # 6 return S3 # 7 A = "AAA" # 8 Pdb.set_trace () # 9 B = "BBB" # Ten C = "CCC" # one B final = combine (b) # (PDB)# 8 Viewing variables passed into a function# A----> Call a function, you can see all the arguments passed to the function; a denotes the meaning of arg #例如: # (PDB) L # 1 #coding =utf-8 # 2 Import pdb # 3 # 4, def combine (S1,S2): # 5 S3 = s1 + s2 + S1 # 6 s3 = ' "' + s3 + '" ' # 7 return S3 # 8 # 9 A = "AAA" # pdb.set_trace () # One B = "BBB" # (PDB) a # S1 = AAA # s2 = bbb# 9 execution to the last step of the function# R-----> If you do not want to step through the function of debugging, just think of the last statement of the function that position, such as the return statement, then you can use R;r to express the meaning of return
Demo 4
in [1]:defPdb_test (ARG): ...: forIinch Range(ARG): ...:Print(i) ...:returnArg ...: in [2]:#在python交互模式中, if you want to debug this function, you canin [3]:#采用, Pdb.run the way, as follows:in [4]:ImportPdbin [5]: Pdb.run ("Pdb_test (Ten)")> <String>(1)<Module>() (PDB) s--Pager--> <Ipython-input-1-Ef4d08b8cc81>(1) Pdb_test () - defPdb_test (ARG):(Pdb) L1 - defPdb_test (ARG):2 forIinch Range(ARG):3 Print(i)4 returnArg[eof] (PDB) n> <Ipython-input-1-Ef4d08b8cc81>(2) Pdb_test () - forIinch Range(ARG):(Pdb) L1 defPdb_test (ARG):2 - forIinch Range(ARG):3 Print(i)4 returnArg[eof] (PDB) n> <Ipython-input-1-Ef4d08b8cc81>(3) Pdb_test () - Print(i) (PDB)0> <Ipython-input-1-Ef4d08b8cc81>(2) Pdb_test () - forIinch Range(ARG):(Pdb)> <Ipython-input-1-Ef4d08b8cc81>(3) Pdb_test () - Print(i) (PDB)1> <Ipython-input-1-Ef4d08b8cc81>(2) Pdb_test () - forIinch Range(ARG):(Pdb)
Demo 5 Use the PDB to modify the value of a variable while running
in [7]: Pdb.run ("Pdb_test (1)")> <String>(1)<Module>() (PDB) s--Pager--> <Ipython-input-1-Ef4d08b8cc81>(1) Pdb_test () - defPdb_test (ARG):(Pdb) Aarg= 1(PDB) L1 - defPdb_test (ARG):2 forIinch Range(ARG):3 Print(i)4 returnArg[eof] (PDB)!Arg= - #!!! Here's how to modify variables(PDB) n> <Ipython-input-1-Ef4d08b8cc81>(2) Pdb_test () - forIinch Range(ARG):(Pdb) L1 defPdb_test (ARG):2 - forIinch Range(ARG):3 Print(i)4 returnArg[eof] (PDB) p ARG -(PDB)
The obvious drawback of PDB debugging is that it is not good enough to support multi-threading, remote debugging, and there is no more intuitive interface to display, and it is not suitable for large python projects. In larger Python projects, these debugging requirements are common, so you need to use more advanced debugging tools.
Python PDB debugging