Debug GCC program with GDB

xiaoxiao2021-03-06  103

Debug GCC program with GDB 2004-07-13 Rick McMullin Click: 28

Debug GCC program with GDB

Linux includes a GNU debugger called GDB. GDB is a powerful debugger for debugging C and C programs. It allows you to observe the internal structure and memory usage of the program at runtime. The following is GDB provided Some features: it allows you to monitor the value of the variables in your program. It allows you to set breakpoints to stop executing on the specified code line. It makes you execute your code in a line. On the command line Type GDB and press Enter to run GDB, if everything is normal, GDB will be started and you will see similar content on the screen:

GDB Is Free Software and You Are Welcome to Distribute Copies of IT

Under 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 lot of options on the command line. You can also run GDB: GDB in the following ways: GDB

When you run GDB in this way, you can specify the program you want to debug. This will tell GDB to load an executable file named FNAME. You can also use GDB to check an Core file that is generated due to an exception termination Or connect with 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 instructions for these options.

Compilant code for debugging

In order to make GDB work properly, you must make your program contain debugging information when compiling. Debug information contains the type of each variable in your program and the address map in the executable, and the line number of the source code. GDB uses these The information is associated with the source code and the machine code. Turn on the debug option with the -g option when compiling.

GDB basic order

GDB supports a lot of commands to make you different features. These commands are loaded from simple files to the complex command that allows you to check the content called the stack content, Table 1 lists some of you will use when using GDB debugging command.

Table 1. Basic GDB command.

Command Description File Loads the executable of the executable of the executable of the desired debugTed to terminate the program list that generates the source code that generates the source code of the execution file executes a row source code but does not enter the function inside STEP to execute a row of source code and enter the function internal RUN execution The currently debugging program quit termination GDB Watch allows you to monitor the value of a variable, regardless of whether it is changed to set breakpoints in the code, which will cause the program to be hanged as this is hung by Make to make you not withdraw from GDB You can react to execute the executable SHELL to execute the unix shell command without leaving GDB.

GDB supports a lot of command editing features like UNIX shell programs. You can press the Tab key like it in Bash or TCSH to let GDB help you make up a unique command, if not, GDB will list all matching commands. You You can also flip history commands with cursor keys.

GDB application example

This section teaches you step by step by step. The debugged program is quite simple, but it shows the typical application of GDB.

The program that will be debugged is listed. This program is called Greeting, which shows a simple greeting and listed it in the back sequence.

#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

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 Test.c

The following results displayed when this program is executed:

The string is hello there

The String Printed Backward IS

The first line of output is correct, but the second line of printing is not what we expect. The output we have envisage should be:

The String Printed Backward Is Ereht Olleh

For some reason, my_print2 functions do not work properly. Let us use GDB to see where there is, type the following command:

GDB GRETING

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

If you forget to pass the program you want to debug as a parameter, you can load it with the file command under the GDB prompt:

(GDB) File Greeting

This command will load the Greeting executable is like you to load it in the GDB command line.

At this time, you can run Greeting with GDB's Run command. When it is running 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 evted with code 041

This output is the same as the result of running outside GDB. The problem is, why don't you work in the reverse order? In order to find the crux, we can set a breakpoint after the FOR statement of my_print2 function, the specific practice is in the GDB prompt. Type the list command three times, list the source code:

(GDB) List

(GDB) List

(GDB) List

Tip: Press Enter to return to the car at the GDB prompt.

The output of the first type of 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 a list command, 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

Pressing a cycle will list the remaining parts of the Greeting program:

21 size = strlen (string); 22 string2 = (char *) malloc (size 1);

23 for (i = 0; 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 source program listed, you can see where you want to break points on line 24, type the following command to set breakpoints under the GDB command line prompt:

(GDB) BREAK 24

GDB will make the following response:

Breakpoint 1 AT 0x139: File Greeting.c, Line 24

(GDB)

Now type the RUN command, will 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 see how the error is generated by setting an observation point of the value of the string2 [size - i] variable, the practice is to type:

(gdb) Watch string2 [size - i]

GDB will make the following response:

WatchPoint 2: String2 [size - i]

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

(GDB) Next

After the first cycle, GDB tells us that the value of string2 [size - i] is `h` GDB tells you this information with the following display:

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 result of the later several cycles is correct. When i = 10, the value of expression string2 [size - i] is equal to the value of `E`, size - i is equal to 1, the last one The characters have been copied to the new stroke.

If you do 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 initials them into empty (null) characters when allocating memory So the first character of String2 is empty. This explains why there is no output when printing string2.

Now find out where the problem is, correct this error is very easy. You have to change the shift of the first character in String2 in the code to Size - 1 instead of size. This is because String2 size 12, but the start offset is 0, the characters in the string from the offset 0 to the offset 10, the offset amount 11 is a null character.

In order to make the code work properly, there are many modifications. One is a variable that sets another actual size of the string. This is the code of 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

String2 [Size2 - I] = String [i];

String2 [size] = `/ 0 ';

Printf ("THE STRING Printed Backward IS% S / N", String2);

}

转载请注明原文地址:https://www.9cbs.com/read-104585.html

New Post(0)