GDB is the GNU Open source organization released a powerful UNIX program debugging tools, GDB can help engineers to complete the following 4 aspects of the function: Start the program, can be customized according to the requirements of the engineer to run the program. Let the program being debugged stop at the breakpoint specified by the engineer, the breakpoint can be a conditional expression. When the program is stopped, you can check what happened in the procedure and trace it. Dynamically changing the execution environment of the program. Whether you are debugging a Linux kernel space driver or debugging a user-space application, it is essential to master GDB's usage. Also, the GDB command used to debug the kernel and debug the application is exactly the same, and the following example demonstrates the use of the GDB debugger in listing 22.2.
1 int Add (int a, int b)
2 {
3 return a + b;
4 }
5
6 Main ()
7 {
8 int sum[10] =
9 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
one } ; int i; array1[10 int] = (in)------------- 544 />18 int array2[10] = {
1, 2, 3, 4}, 0x199, 393 ;
For (i = 0; i < i++) { sum[i] = Add (array1[i), array2[i]); }
27}
Using the command gcc–g gdb_example.c–o gdb_example compile the above program, get the binary file containing debugging information example, execute the GDB gdb_example command into the debug state:
In GDB, run the program using the Run command. Before the program runs, we can set the following 4 aspects of the Working environment: The program run parameter set args can specify Run-time parameters, such as: Set args 50;show args command to see the set of running parameters. Operating Environment
Path <dir> programmable running paths; how paths can view the running path of the program; set environment VarName [=value] is used to set environment variables, such as set env User=baohua;
Show environment [VarName] is used to view environment variables. Working directory CD <dir> equivalent to Shell's CD command; pwd displays the current directory. The input and output of the program
Info terminal is used to display the mode of the terminal used by the program, and the redirection control program output can also be used in gdb, such as Run > outfile;
<expr> is an expression, is an expression in the program being debugged, <f> is the output format, for example, if you want to output the expression in 16 format, then it is/x. In an expression, there are several operators supported by GDB that can be used in any language, and "@" is an array of operators, "::" Specify a variable in a file or function, "{<TYPE>} <addr>" indicates a point to memory address <addr> is an object of type.The following shows the procedure for viewing the values of the sum[] array:
(gdb) print sum
$ = {0, 0, 0, 0, 0, 0, 0, 0}
(gdb) Next
Breakpoint 1, Main () at gdb_example.c:25
25 Sum[i] = Add (Array1[i], array2[i]);
(GDB) Next
for (i = 0; i < i++)
(gdb) print sum
$ = {133, 155, 143, 0, 0, 0, 0, 0, 0, 0}
When you need to view the value of a contiguous memory space, you can use the GDB "@" operator, the left of which is the first memory address, and the right of the @ to see the length of memory. For example, the following dynamically requested memory:int *array = (int *) malloc (len * sizeof (int));
This displays the value of this dynamic array during the GDB debugging process:
P *array@len
The output format for print includes: x displays variables in hexadecimal format. D displays variables in decimal format. U displays the unsigned integer in hexadecimal format. o Display variables in octal format. T displays variables in binary format. A the variable is displayed in hexadecimal format. C displays variables in character format. F displays variables in floating-point format. We can use the display command to set some variables that are automatically displayed, and when the program stops, or when you step through it, these variables are automatically displayed. If you want to modify a variable, such as the value of x, you can use the following command:
Print x=4
<addr> represents a memory address. N, F, and U after "x/" are optional parameters, n is a positive integer indicating the length of the display memory, that is, displaying the contents of several addresses backwards from the current address; F indicates the format to display, if the address refers to a string, then the format can be S, if the address is the instruction address, then the format can be i;u Represents the number of bytes requested from the current address and, if not specified, gdb defaults to 4 bytes. The u parameter can be replaced by some characters: B for Single-byte, H for Double-byte, W for four bytes, and G for eight bytes. When we specify the byte length, GDB starts with the specified memory address, reads and writes the specified byte, and takes it out as a value. N, F, and u these 3 parameters can be used together, for example, the command "X/3uh 0x54320" indicates that 3 units (3) of memory are displayed in Double-byte 1 units (h) and 16 (U) from the memory address 0x54320. ==
For instance, the following example:
Main ()
{
char *c = "Hello World";
printf ("%s\n", c);
}
We arechar *c = "Hello World";
After setting a breakpoint on the next line:(GDB) L
1 Main ()
2 {
3 char *c = "Hello World";
4 printf ("%s\n", c);
5 }
(GDB) b 4
breakpoint 1 at 0x100000f17:file main.c, line 4.
(GDB) r
starting program:/users/songbarry/main
Reading symbols for shared libraries +. Done
Breakpoint 1 , Main () at Main.c:4
4 printf ("%s\n", c);
You can see the string that C points to in a variety of ways:Method 1:
(GDB) p C
$ = 0x100000f2e "Hello World"
Method 2:(gdb) x/s 0x100000f2e
0x100000f2e: "Hello World"
Method 3:(GDB) p (char *) 0x100000f2e
$ = 0x100000f2e "Hello World"
Change the first character to uppercase:(GDB) p * (char *) 0x100000f2e= ' h '
$ = ' h '
Then look at c:(GDB) p C
$ = 0x100000f2e "Hello World"