foreword
In the IDE of the VS series, we are accustomed to the debugging of the graphical window; and under the command line of linux, we use the tool gdb to complete the debugging of the program
After reading this article, you will know:
- show code
- breakpoint, jump to breakpoint
- Process by process
- execute statement by statement
- Long display variable information
- Jump to the specified line
- Disable breakpoints
- Change variables while debugging
1. How to compile the source code into a debuggable binary file
We used to use this command when compiling a C language source file mytest.c:
gcc mytest.c -o mytest
Executable file was successfully generatedmytest
If we debug this executable directly with gdb, we will find no debug information:
why is that?
When the program is compiled by default in Linux
- The executable program generated by default is the release version and is not adjustable
so
- Need to debug, gcc needs to add
-g
parameters publish the program in debug mode, so that the executable file with debugging information -> can be tracked and debugged by gdb, that is:
gcc mytest.c -o mytest_debug -g
What we can also find is that the executable file of the debug version is larger than the release version, which is because the debug information is added
2. Start debugging
First give the source file:
#include <stdio.h>
int sum(int top) {
int _sum = 0;
int i = 0;
for(;i <= top; i++) {
_sum += i;
}
return _sum;
}
int main() {
int max = 0;
printf("please enter your data# ");
scanf("%d",&max);
int _sum = sum(max);
printf("1:%d\n", _sum);
printf("2:%d\n", _sum);
printf("3:%d\n", _sum);
printf("4:%d\n", _sum);
printf("5:%d\n", _sum);
printf("6:%d\n", _sum);
return 0;
}
We first compile and generate an executable file with debugging information
gcc mytest.c -o mytest_debug -g
Then gdb debug:
gdb mytest_debug
Order | effect |
---|---|
list/l | Show source code (10 lines at a time) |
run/r | run the program |
next/n | Process by process |
step/s | Execute statement by statement, you can enter the function |
break/b line number | Set a breakpoint on a line |
info break | View breakpoint information |
finish | Execute until the current function returns |
print/p | print the value of an expression |
there is a set | change the value of a variable |
continue | jump to next breakpoint |
delete breakpoints | remove all breakpoints |
delete breakpoints n | Delete breakpoint with sequence number n |
disable breakpoints n | Untrace the breakpoint with sequence number n |
enable breakpoints n | Resume tracking of breakpoint number n |
info breakpoints | Print set breakpoints |
display variable name | Display a variable for a long time |
undisplay variable name | Cancel the long display of a variable |
until line number | jump to a line |
quit/q | quit gdb |
The following demonstrates common commands
2.1 Display code l
list/l 行号
Display the source code, display 10 lines at a time, and then press Enter will continue to display down
2.2 Running the program r
We start debugging, that is, to run the program:
run/r
We found that after r, the code was directly executed to the input statement on line 14, and the program ended directly after the input, without debugging at all. The reason is because we didn't set a breakpoint, if a breakpoint was set, r would stop at the first breakpoint, so that we could single-step.
2.3 Set and view breakpoints
b 行号 //设置断点
info b //查看断点
We set breakpoints on lines 14, 15, 19
2.4 next step-by-step debugging
We continue r to execute the program, and then n step through
n
It is found that n does not let us enter the function body, which shows that n is a process-by-process debugging
2.5 step statement-by-statement debugging
We enter the function body with s
2.6 Finish is executed until the current function returns and stops
finish
2.7 c jump to next breakpoint
c
2.8 dispaly long display variable
display 变量名
undisplay 变量序号
Note that undisolay is the serial number in front of the variable