Use pdb to debug python programs under Linux

Generally, we can use the following methods to enter debugging (for example, the source file we want to debug is cmd_pq1.0.py):

1. Start the target program on the command line. Add the -m parameter

python -m pdb cmd_pq1,0.py

In this way, the program will automatically stop at the first line, waiting for your debugging. As shown in the figure below, we can use the debug command to debug, which is similar to using the IDE to debug. As shown in Figure 1.1.

 Figure 1.1

2. Common debugging commands (see the official website documentation for detailed information):

(Pdb) b 8 #Set the breakpoint to the 8th line of the file (b is the first letter of break)

(Pdb)b #b command, without parameters, displays all breakpoints

(Pdb)cl 2 #Delete the second breakpoint (the first letter of clear)

(Pdb) n # single-step execution, the first letter of nest

(Pdb) j 10 #run to line 10, the first letter of jump

(Pdb)p param #View the variable value of the current param

(Pdb)I # View code running to somewhere

(Pdb) a #View all variables in the stack

(Pdb) q # exit, the first letter of quit

(Pdb) info b #Check the situation at the breakpoint, you can set multiple breakpoints

(Pdb)r # run the code

(Pdb) watch n #observe variables

(Pdb)c #The program continues to run until it encounters a breakpoint or the end of the program, the first letter of continue

Guess you like

Origin blog.csdn.net/qq_43593751/article/details/127949587