Article title: debug GCC program with GDB in Linux (2 ). Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Gdb application example
This section uses an example to teach you how to debug a program with gdb step by step. the program to be debugged is quite simple, but it shows typical applications of gdb.
The program to be debugged is listed below. this program is called greeting, which displays a simple greeting and is listed in reverse order.
# Include
Main ()
{
Char my_string [] = "hello there ";
My_print (my_string );
My_print2 (my_string );
}
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 );
}
Compile it with the following command:
Gcc-o test. c
The following results are displayed when the program is executed:
The string is hello there
The string printed backward is
The first line of output is correct, but the output in the second line is not what we expect. the output we imagine should be:
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 greeting
Note: Remember to open the debugging option when compiling the greeting program.
If you forget to pass the program to be debugged as a parameter when entering the command to gdb, you can use the file command at the gdb prompt to load it:
(Gdb) file greeting
This command will load the greeting executable file just as you load it in the gdb command line.
Then you can run greeting using the gdb run command. when it is run in gdb, the result will be like this:
(Gdb) run
Starting program:/root/greeting
The string is hello there
The string printed backward is
Program exited with code 041
The output is the same as the running result outside gdb. The question is, why does reverse printing not work? 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 to list the source code:
(Gdb) list
(Gdb) list
(Gdb) list
Tip: Press enter at the gdb prompt to repeat the previous command.
The output of the first list command is as follows:
1 # include
2
3 main ()
4 {
5 char my_string [] = "hello there ";
6
7 my_print (my_string );
8 my_print2 (my_string );
9}
10
If you press enter, gdb will execute the list command again and give the following output:
11 my_print (char * string)
12 {
13 printf ("The string is % s \ n", string );
14}
15
16 my_print2 (char * string)
17 {
18 char * string2;
19 int size, I;
20
Press enter again to list the remaining parts of the greeting program:
21 size = strlen (string );
22 string2 = (char *) malloc (size + 1 );
23 for (I = 0; I <size; I ++)
24 string2 [size-I] = string [I];
25 string2 [size + 1] = '\ 0 ';
26 printf ("The string printed backward is % s \ n", string2 );
27}
Based on the listed source program, 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
(Gdb)
Enter the run command to generate the following output:
Starting program:/root/greeting
The string is hello there
Breakpoint 1, my_print2 (string = 0xbfffdc4 "hello there") at greeting. c: 24
24 string2 [size-I] = string [I]
You can set an observation point to observe the value of the string2 [size-I] variable to see how an error is generated by typing:
(Gdb) watch string2 [size-I]
Gdb will respond as follows:
Watchpoint 2: string2 [size-I]
Now we can use the next command to execute the for loop step by step:
(Gdb) next
After the first loop, gdb tells us that the value of string2 [size-I] is 'H'. gdb uses the following display to tell you this information:
Watchpoint 2, string2 [size-I]
Old value = 0' \ 000'
New value = 104 'h'
My_print2 (string = 0xbfffdc4 "hello there") at greeting. c: 23
23 for (I = 0; I
This value is expected. the results of subsequent cycles are correct. when I = 10, the value of the expression string2 [size-I] is equal to 'E', the value of size-I is equal to 1, and the last character has been copied to the new string.
If you run the loop again, you will see that no value is allocated to string2 [0], and it is the first character of the new string, because the malloc function initializes them as null characters when allocating memory. therefore, the first character of string2 is a null character. this explains why there is no output when string2 is printed.
It is very easy to correct the problem after finding out the problem. you have to change the offset of the first character written to string2 in the code to size-1 instead of size. this is because the size of string2 is 12, but the starting offset is 0. the characters in the string are offset 0 to offset 10, and the offset 11 is reserved as null characters.
There are many ways to modify the code properly. one is to set a variable that is 1 smaller than the actual size of the string. this is the code for this solution:
# Include
Main ()
{
Char my_string [] = "hello there ";
My_print (my_string );
My_print2 (my_string );
}
My_print (char * string)
{
Printf ("The string is % s \ n", string );
}
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 );
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.