Linux programming tool C compiler and C programming tool GDB steps

Source: Internet
Author: User
Linux programming tool gdb supports many command editing features like the UNIXshell program. you can press the Tab key in bash or tcsh to help gdb complete a unique command, if it is not unique, gdb will list all matching commands. you can also use the mouse key.

Linux programming tool gdb supports many command editing features similar to those of UNIX shell programs. you can press the Tab key like in bash or tcsh to allow gdb to help you complete a unique command. if it is not unique, gdb will list all matching commands. you can also use the mouse key to flip up and down History commands.

Linux programming tool gdb application example

This section uses an example to teach you how to debug a program step by step using the Linux programming tool gdb. the program to be debugged is quite simple, but it shows a typical application of the Linux programming tool gdb.

The program to be debugged is listed below. this program is called "hello", which displays a simple greeting and is listed in reverse order.

  1. # Include
  2. Static void my_print (char *);
  3. Static void my_print2 (char *);
  4. Main ()
  5. {
  6. Char my_string [] = "hello world! ";
  7. My_print (my_string );
  8. My_print2 (my_string );
  9. }
  10. Void my_print (char * string)
  11. {
  12. Printf ("The string is % s", string );
  13. }
  14. Void my_print2 (char * string)
  15. {
  16. Char * string2;
  17. Int size, I;
  18. Size = strlen (string );
  19. String2 = (char *) malloc (size + 1 );
  20. For (I = 0; I <size; I ++)
  21. String2 [size-I] = string [I];
  22. String2 [size + 1] = '';
  23. Printf ("The string printed backward is % s", string2 );
  24. }

Run the following command to compile the program: gcc-g-o hello. c:

  1. ./Hello
  2. The string is hello world!
  3. The string printed backward is

The first line of output is correct, but The output in The second line is not what we expected. The output we imagine should be: The string printed backward is! Dlrow olleh for some reason, the my_print2 function does not work properly. let's use gdb to see where the problem is. First, press the following command: gdb hello

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 hello
This command will load the hello executable file just as you load it in the gdb command line. you can use the gdb run command to run hello. when it is run in gdb, the results will look like this:

  1. (Gdb) run
  2. Starting program:/root/hello
  3. The string is hello world!
  4. The string printed backward is
  5. Program exited with code 040

The output is the same as the result of running the program outside the Linux programming tool 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:

  1. (Gdb) list
  2. (Gdb) list
  3. (Gdb) list

The output of the first list command is as follows:

  1. # Include
  2. Static void my_print (char *);
  3. Static void my_print2 (char *);
  4. Main ()
  5. {
  6. Char my_string [] = "hello world! ";
  7. My_print (my_string );
  8. My_print2 (my_string );

Press enter again to list the remaining parts of the hello program:

According to the listed source program, you can see that the place where the breakpoint is to be set is in line 26th. at the gdb command line prompt, type the following command to set the breakpoint: (gdb) break 26

Gdb will make the following response: Breakpoint 1 at 0x804857c: file hello. c, line 26. (gdb)

Enter the run command to generate the following output:

  1. Starting program:/root/hello
  2. The string is hello world!
  3. Breakpoint 1, my_print2 (string = 0xbffffab0 "hello world! ") At hello. c: 26
  4. 26 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: Hardware 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:

  1. Hardware watchpoint 2: string2 [size-I]
  2. Old value = 0 '00'
  3. New value = 104 'h'
  4. My_print2 (string = 0xbffffab0 "hello world! ") At hello. c: 25
  5. 25 for (I = 0; I <size; I ++)

This value is expected. The results of subsequent cycles are correct. when I = 11, the value of the expression string2 [size-I] is equal '! ', 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.

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.