GDB is a powerful program debugging tool released by GNU. It provides three functions:
1. Start the program debugging.
2. Let the program being debugged stop at the specified position
3. When the program is stopped, you can view the running status of the program.
Example:
1. compile and generate an executable file:
Gcc-g hello. c-o hello
2. Start GDB
Gdb hello
3. Set breakpoints in the main function
Break main
4. Run the program
Run
Common GDB commands
Gdb hello |
Start GDB |
File hello |
Load file name |
List (l)
|
View programs |
Breakb) function name |
Add a breakpoint at the function entry |
Break (B) row number |
Add a breakpoint to a specified row |
Break (B) File Name: row number |
Add a breakpoint to the specified row of the specified file |
Break (B) row number if condition |
When the condition is true, the breakpoint at the specified row number takes effect. |
Info break |
View All set breakpoints |
Delete breakpoint number |
Delete breakpoint |
Run (r) |
Start running the program |
Next (n)
|
A single-step running program does not enter the sub-function) |
Steps)
|
One-step program running |
Continue (c) |
Continue running the program |
Print (p) variable name
|
View specified variable values |
Finish
|
Run the program until the current function ends. |
Watch variable name
|
Monitor specified variables |
Quit (q)
|
Exit gdb |
#include<stdio.h>#define MAX_RECORD_NUMBER 10int record[MAX_RECORD_NUMBER] ={12,76,48,62,94,17,37,52,69,32};swap(int* x,int* y){ int temp; temp = *x; *x = *y; *y = temp;}int main(){ int i,j; for(i=0;i<MAX_RECORD_NUMBER-1;i++) { for(j=MAX_RECORD_NUMBER-1;j>1;j++) { if(record[j])<record[j-1] { swap(&record[j],&record[j-1] } } } for(i=0;i<MAX_RECORD_NUMBER-1;i++) { printf("%d",record[i]); } printf("\n"); return 0;}