The GNU Debugger is called gdb. This program is an interactive tool that works in character mode. In the X Window System, there is a gdb front-end graphics tool called xxgdb. Gdb is a powerful debugging program that can complete the following debugging tasks:
* Set breakpoints;
* The value of the Monitored Program variable;
* One-Step Program Execution;
* Modify the value of a variable.
Before you can use gdb to debug a program, you must use the-g option to compile the source file. You can define the CFLAGS variable in makefile:
CFLAGS =-g
The following command is often used to run the gdb debugging program:
Gdb progname
Enter help at the gdb prompt to list the command categories, including:
* Aliases: Command alias
* Breakpoints: breakpoint definition;
* Data: View data;
* Files: Specify and view files;
* Internals: maintenance command;
* Running: program execution;
* Stack: view the call stack;
* Statu: view the status;
* Tracepoints: trace program execution.
Enter help and the classification name of the command to obtain a detailed list of such commands.
Common gdb commands
Command explanation
Break NUM sets a breakpoint on the specified row.
Bt displays all call stack frames. This command can be used to display the call sequence of a function.
Clear deletes a breakpoint set on a specific source file or line. The usage is clear FILENAME: NUM.
Continue continues to execute the program being debugged. This command is used when the program stops running because of processing signals or breakpoints.
Display EXPR: The expression value is displayed after each program is stopped. An expression is composed of variables defined by the program.
File FILE to load the specified executable file for debugging.
Help NAME: displays the help information of a specified command.
Info break displays the current breakpoint list, including the number of times the breakpoint is reached.
Info files: displays detailed information about the file to be debugged.
Info func shows all function names.
Info local shows the local variable information in the function.
Info prog displays the execution status of the program to be debugged.
Info var displays all global and static variable names.
Kill the program being debugged.
List shows source code segments.
Make runs the make tool without exiting gdb.
Next executes a source code line forward without entering other functions.
Print EXPR: the value of the expression EXPR.
************************
-----------------
Listing a wrong C source code bugging. c
-----------------
1 # include <unistd. h>
2
3 static char buff [256];
4 static char * string;
5 int main ()
6 {
7 printf ("Please input a string :");
8 gets (string );
9 printf ("Your string is: % s", string );
10}
-----------------
The above program is very simple, the purpose is to accept the user input, and then print out the user input. This program uses an uninitialized string address string. Therefore, after compilation and running, the Segment Fault error will occur:
$ Gcc-o bugging-g bugging. c
$./Bugging
Please input a string: asfd
Segmentation fault (core dumped)
To find problems in the program, we use gdb and follow the steps below:
1. Run the gdb bugging command to load the bugging executable file;
2. run the loaded bugging command;
3. Run the where command to view the errors in the program;
4. Use the list command to view the code near the call to the gets function;
5. the only cause of the gets function error is the variable string. Use the print command to view the string value;
6. In gdb, we can directly modify the value of the variable, as long as a valid pointer value is taken for the string, so we can set the breakpoint break 8 at line 8th;
7. Run the program again and stop it at line 8th. Then, we can use the set variable command to modify the value of string;
8. Continue running. The correct program running result is displayed.