1. Some basic commands of gdb
l
: Display code
l n
: jump to the code of the nth line of the current code page
l filename.c :n
: jump to the nth line of the filename.c file code
b 行号
: add a breakpoint
info break
: view the breakpoint information
delete 断点编号
: delete the breakpoint
r
, run the program
n
, single-step execution
c
, Continue to execute, stop execution when a breakpoint is encountered
p
, print
s
, enter a function
finish
, exit a function
q
, and exit gdb
2. Debugging example
First write a main.c program, which is used to output the string input from the keyboard in a loop until it encounters "end" to end the loop:
Compile and run main.c:
It is found that it is not the same as our expectation. After entering "end", the loop does not end, and the process is terminated by Ctrl+c.
- Enter gdb, gdb to debug the Release version:
It can be seen from the above figure that debugging is currently not possible. The reason why some information can be printed out is because the information is printed by gdb, and the main program has not been executed. The following sentence tells us that we did not find it in the main program
. Appropriate symbols, that is, do not contain debugging information, so it cannot be debugged at this time. When entering l
code for display, the following sentence
tells us that no debug symbols were imported. It shows that there is a problem with the main program at this time, and no debugging information is included. Type q to exit gdb.
- gdb to debug the Debug version:
(1) Compile and track
Follow gcc -o main main.c -g
to compile main.c to generate the executable program main, and use gdb to track the main program:
At this time, we can
see through this sentence that gdb has read the symbol table from main.
(2) Display the written code
l
Display code through gdb command
Only 10 lines of code can be displayed at a time.
(3) Set breakpoints
Grammatical form: break 行号
, break can be abbreviated as b
Add a breakpoint to line 13:
You can continue to set a breakpoint:
Check which breakpoints are added info break
:
Remove breakpoint delete Num
:
Remove the 2nd breakpoint:
(4) Run the program for debugging
Input run
can be abbreviated as r
, press Enter:
The current program execution stops at line 13.
Enter n
, and let the program step through:
It can be seen from the above figure that the string "hello" is entered, and then the program runs to the if statement on line 17.
At this time, we enter p buff
to view the contents of some buffs:
It can be seen that the string "hello\n" is stored in the current buff.
Continue typing n
to step through:
The current program has been executed to 13 lines.
Let's continue stepping, this time enter the string "end", and the program jumps to the if statement on line 17:
At this point, let's enter again p buff
to view the contents of some buffs:
It can be seen that the string "end\n" is stored in the current buff, which is different from the string "end" we input.
Therefore, when continuing to execute, the program will still enter a loop:
At this point, we found the problem in the code through debugging.
- exit gdb
Enter q
to exit gdb. At this time, it will prompt that if you want to exit, it will kill the main program just now. If you want to exit, input y
means exit, and press Enter to exit gdb.
After exiting gdb, the main program just ended:
- Correct code by debugging results
Change strcmp() to strncmp():
At this point, we will compile main.c and execute the main program. The result is as follows: