Linux contains a GNU debugging program called GDB. GDB is a powerful debugger used to debug C and C ++ programs. It enables you to observe the internal structure and memory usage of the program while running the program. The following are some functions provided by GDB:
(1) It enables you to monitor the value of variables in your program;
(2) It enables you to set breakpoints so that the program stops running on the specified code line;
(3) It enables you to execute your code in one line;
I. Overview
Type GDB on the command line and press the Enter key to run GDB. If everything works properly, GDB will be started and you will see similar content on the screen: GDB is free software and you are welcome to distribute ,.........., copyright 1995 Free Software Foundation, Inc. (GDB ). And start to enter the gdb debugging status. After you start GDB, you can specify many options on the command line. You can also run GDB in the following ways: GDB <fname>. When you run GDB in this way, you can directly specify the program to be debugged, which will tell GDB to load the executable file named fname.
Ii. Use Process
To make GDB work normally, you must make your program contain debugging information during compilation. The debugging information contains the type of each variable in your program, the address ing in the executable file, and the source code line number. GDB uses this information to associate the source code with the machine code. Enable the debugging option with the-G option during compilation.
GDB supports many commands so that you can implement different functions. These commands are loaded from simple files to complex commands that allow you to check the stack content that you call. The following table lists some of the commands you will use when debugging with GDB:
Iii. Instances
This section uses an instance to debug the program step by step with GDB. The program to be debugged is quite simple, but it shows typical applications of GDB. This program is called greeting. It displays a simple greeting and lists it in reverse order.
# Include <stdio. h>
Void my_print (char * string)
{
Printf ("the string is % s/n", string );
}
Void my_print2 (char * string)
{
Char * string2;
Int size, I;
Size = strlen (string );
String2 = (char *) malloc (size + 1 );
For (I = 0; I <size; I ++)
String2 [size-I] = string [I];
String2 [size + 1] = '/0 ';
Printf ("the string printed backward is % s/n", string2 );
}
Main ()
{
Char my_string [] = "Hello there ";
My_print (my_string );
My_print2 (my_string );
}
Compile it with the following command:
Gcc-G greeting. c
./A. out:
The string is hello there
The string printed backward is
(1) We can see that the first line of output is correct, but the output in the second line is not what we expected. The expected output is: the string printed backward is ereht olleh. For some reason, the my_print2 function does not work properly. Let's use GDB to see where the problem is. First, enter the following command:
(GDB) file greeting. You can use the gdb Run Command to run greeting.
(2) To find out the crux of the problem, we can set a breakpoint after the for statement of the my_print2 function, the specific method is to enter the LIST Command three times at the gdb prompt (10 lines of code are listed each time) to list the source code:
(GDB) List
(GDB) List
(GDB) List
According to the listed source code, you can see that the place where the breakpoint is to be set is in line 24th. At the gdb command line prompt, type the following command to set the breakpoint: (GDB) Break 24. GDB will make the following response: breakpoint 1 at 0x139: file greeting. C, line 24.
At the same time, you can set an observation point to observe the value of the string2 [size-I] variable to see how the error is generated, by typing: (GDB) watch string2 [size-I], GDB will respond as follows: watchpoint 2: string2 [size-I]. Next, you can use the next command to execute the for loop step by step:
(GDB) Next.
Through these processes, you can see where the error is.
(3) the correct code function is as follows:
My_print2 (char * string)
{
Char * string2;
Int size, size2, I;
Size = strlen (string );
Size2 = size-1;
String2 = (char *) malloc (size + 1 );
For (I = 0; I <size; I ++)
String2 [size2-I] = string [I];
String2 [size] = '/0 ';
Printf ("the string printed backward is % s/n", string2 );
}
Http://chinalinuxpub.com/doc/pro/gdb.html ()