Debugging the GCC program with GDB "Go"

Source: Internet
Author: User

Debugging a program with GDB

Original: Rick McMullin

Debugging the GCC program with GDB

Transferred from: http://blog.csdn.net/bonnshore/article/details/7955422

Linux contains a GNU debug program called GDB. GDB is a powerful debugger for debugging C and C + + programs. It allows you to observe the program's internal structure and memory usage while the program is running. Here are some of the features that GDB offers:

    • It allows you to monitor the values of variables in your program.
    • It enables you to set breakpoints so that the program stops executing on the specified line of code.
    • It allows you to execute your code in a row.

Type gdb on the command line and press ENTER to run gdb , and if everything works, gdb will be launched and you will see something similar on the screen:

GDB is free software and you were welcome to distribute copies of itunder certain conditions; Type "show copying" to see the conditions. There is absolutely no warranty for GDB; Type "show warranty" for details. GDB 4.14 (I486-slakware-linux), Copyright 1995 free Software Foundation, Inc. (GDB)

When you start gdb , you can specify a number of options on the command line. You can also run gdb in the following way:

GDB <fname>

When you run gdb this way, you can directly specify the program you want to debug. This tells GdB to mount an executable file named FName. You can also use gdb to check for a core file that results from an abnormally terminated program, or to connect to a running program. You can refer to the gdb Guide page or type gdb-h on the command line to get a simple list of descriptions of these options.

Compiling code for debugging (compiling codes for debugging)

For gdb to work correctly, you must have your program include debugging information at compile time.  The debug information contains the type of each variable in your program and the address mapping in the executable file and the line number of the source code. GDB uses this information to correlate the source code with the machine code.

Open the debug option at compile time with the-G option.

GDB basic Commands

GDB supports a number of commands that enable you to implement different functions. These commands are loaded from a simple file into a complex command that allows you to examine the contents of the called stack, and table 27.1 lists some of the commands you will use when debugging with gdb . To learn more about GdB 's use, refer to the gdb Guide page.

table 27.1. Basic GDB commands.

Command Description
File Load the executable file that you want to debug.
Kill Terminates the program being debugged.
List Lists part of the source code that generated the execution file.
Next Executes a line of source code but does not enter the inside of the function.
Step Executes a line of source code and goes inside the function.
Run Executing a program that is currently being debugged
Quit Terminate gdb
Watch Enables you to monitor the value of a variable regardless of when it is changed.
Break Set breakpoints in the code, which will cause the program to be suspended when it is executed here.
Make Allows you to regenerate the executable without having to exit gdb .
Shell Enables you to execute UNIX shell commands without leaving gdb .


GDB supports many of the same command editing features as UNIX shell programs.  You can press the Tab key like in bash or tcsh to have gdb help you with a unique command, if not the only one. gdb All matching commands are listed. You can also flip the history command up and down with the cursor keys.

Examples of GDB applications

An example of this section teaches you to use gdb to debug a program step-by-step. The program being debugged is quite simple, but it shows the typical application of gdb .

The program that will be debugged is listed below. This program is called greeting , it shows a simple greeting, and then it is listed in reverse order.

#include <stdio.h>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] = ' + '; printf ("The string printed backward is%s\n", string2);}

Compile it with the following command:

Gcc-o Test test.c

This program displays the following results when it executes:

The string is hello therethe string printed backward is

The first line of the output is correct, but the second line prints out something that is not what we expected. The output we envision should be:

The string printed backward is Ereht Olleh

For some reason, the my_print2 function is not working properly. Let's use gdb to see where the problem is, and first type the following command:

GdB Greeting
Note: Remember to open the debug option when compiling the greeting program.

If you forgot to pass the program you want to debug as a parameter to GDB when you entered the command, you can load it with the file command at the gdb prompt:

(gdb) file greeting

This command will load the greeting executable as if you were loading it in the gdb command line.

You can then run greeting with the gdb Run command. When it is run in gdb , the result will be something like this:

(GDB) runstarting program:/root/greetingthe string was hello therethe string printed backward Isprogram exited with code 0 41

This output is the same as the result of running outside gdb . The question is, why is the reverse print not working? To find the crux of the problem, we can set a breakpoint after the for statement of the my_print2 function by typing the list command three times at the gdb prompt, listing the source code:

(GDB) List (GDB) list
Tip: Press ENTER at the gdb prompt to repeat the previous command.

The first time you type the output of the List command is as follows:

1 #include <stdio.h>23 main () 4 {5 char my_string[] = "Hello there"; My_print ( my_string); 8 My_print2 (my_string); 9}10

If you press ENTER, GDB executes the List command again, giving the following output:

My_print (char *string) ("The string is%s\n", "string"), and}1516 My_print2 (char *string) ~ {*string2;19 int size, i;20

Press ENTER again to list the remainder of the greeting program:

Size = strlen (string), string2 = (char *) malloc (size + 1), for (i = 0; i < size; i++) 24 String2[size-i] = string[i];25 string2[size+1] = ' + + ', "the string printed backward is %s\n ", string2); 27}

Depending on the source program listed, you can see where to set the breakpoint on line 24th, at the gdb command line prompt, type the following command to set the breakpoint:

(GDB) Break 24

GDB will respond as follows:

Breakpoint 1 at 0x139:file greeting.c, line (GDB)


Typing the run command now produces the following output:

Starting program:/root/greetingthe string was hello Therebreakpoint 1, my_print2 (string = 0xbfffdc4 "Hello there") at GRE eting.c:2424 String2[size-i]=string[i]

You can see how the error is generated by setting an observation point that observes the value of the string2[size-i] variable by typing:

(GDB) Watch String2[size-i]

GDB will respond as follows:

Watchpoint 2:string2[size-i]

Now you can use the next command to step through the for loop:

(GDB) Next

After the first iteration, GdB tells us that the value of String2[size-i] is ' h '. GDB uses the following display to tell you the message:

Watchpoint 2, String2[size-i]old value = 0 ' \000 ' New value = 104 ' h ' my_print2 (string = 0xbfffdc4 "Hello there") at greet ing.c:2323 for (i=0; i<size; i++)

This value is exactly what is expected. The results of the subsequent cycles were correct. When i=10 , the value of the expression string2[size-i] equals ' E ', the value of size-i equals 1, and the last character is copied into the new string.

If you run the loop again, you will see that there is no value assigned to string2[0] , and it is the first character of the new string, because the malloc function initializes them to null (NULL) characters when allocating memory. So the first character of string2 is the null character. This explains why there is no output when printing string2 .

Now that you have found out where the problem is, it is easy to fix the error. You have to change the Fu of the first word written to string2 in the code to size-1 instead of size. This is because the size of the string2 is 12, but the starting offset is 0, the characters in the string range from offset 0 to offset 10, and the offset 11 is reserved for null characters.

There are many ways to modify the code to work correctly. One is to set up a different variable than the actual size of the string 1. This is the code for this workaround:

#include <stdio.h>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] = ' + '; printf ("The string printed backward is%s\n", string2);}

Debugging the GCC program with GDB "Go"

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.