GDB debugging program

xiaoxiao2021-03-06  19

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 allows you to perform your code.

Type on the command line

GDB and press Enter key 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 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

After GDB, you can specify a lot of options on the command line. You can also run the following way

GDB:

GDB When you run in this way

GDB, you can specify the program you want to debug. This will tell

GDB is loaded into an executable file called FNAME. You can also use

GDB to check an Core file that is generated by the program, or connects with a running program. You can refer to

GDB guide page or type on the command line

GDB -H gets a simple list of instructions on these options.

To debug compilation for debugging in order to make

GDB works normally, you must make your program contain debugging information when compiling. Debug information contains the type of each variable in your program and the line number of the address map in the executable file and the line number of the source code.

GDB uses this information to associate 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 enable you to achieve 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 27.1 lists you are using

Some commands that will be used when GDB debugging. I want to know

Please refer to the details of GDB

GDB's guide page.

Table 27.1. Basic GDB Command.

The command describes the executable file that File is loaded. Kill terminates the program that is debugging. List lists a part of the source code generating the execution file. NexT executes a row source code but does not enter the function of the function. Step execute a row source code And enter the function inside .Run executes the current debugged program Quit Termination GDBWATCH allows you to monitor the value of a variable, regardless of whether it is changed. Break sets breakpoints in the code, which will make the program to be executed here .make allows you to re-generate the executable file without exiting GDB. SHELL allows you to execute a 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 you don't have unique, GDB will list all match commands. You You can also flip history commands with cursor keys.

GDB Application Examples This section teaches you step by step

GDB debugging program. The debugged program is quite simple, but it shows it.

Typical application of GDB.

The program that will be debugged will be listed below. This program is called

Greeting, it 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

The string is hello theethe string printed backward is the first line of the output is correct, but the second line prints are not what we expect. The output we want should be:

The String Printed Backward Is Ereht Olleh Due to some reason,

My_print2 functions are not working properly. Let us use

GDB looks at where the problem is, type the following command:

GDB GRETING

Note: Remember in compilation

The debug option opens when a Greeting program.

If you forget the program to debug as a parameter to pass when entering the command

GDB, you can

The GDB prompt uses the file command to load it:

(GDB) file greeting This command will be loaded

Greeting executable is like you

The GDB command line is loaded.

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) Runstarting Program: / Root / GreetingThe String I Hello Therethe String Printed Backward Isprogram EXITED WITH CODE 041 This output is

The result of running outside GDB. The problem is why don't you work in the reverse order? In order to find out the crux, we can

MY_PRINT2 function

After the FOR statement, set a breakpoint, the specific practice is

GDB prompt type

List commands three times, listing the source code:

(GDB) List (GDB) List (GDB) LIST

Tips:

The GDB prompt pressing the car will repeat the previous command.

First type

The output of the List command is as follows:

1 #include 23 main () 4 {5 char my_string [] = "hello there"; 67 my_print (my_string); 8 my_print2 (my_string); 9} 10 If you press Enter,

GDB will be executed again

The list command gives the following output:

11 MY_PRINT (CHAR * STRING) 12 {13 Printf ("THE STRING IS% S / N", String); 14} 1516 my_print2 (char * string) 17 {18 char * string2; 19 int size, i; 20 A carriage return will list the remaining parts of the Greeting program:

21 size = strlen (String); 22 string2 = (char *) Malloc (size 1); 23 for (i = 0; i

The GDB command line prompt is typed to set the breakpoint below:

(GDB) BREAK 24

GDB will make the following response:

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

Type it now

The Run command will produce the following output:

Starting Program: / rootingthe string is hello therebreakpoint 1, my_print2 (string = 0xbffdc4 "hello there") at Greeting.c: 2424 string2 [size-i] = String [i] You can set an observation

String2 [size - i] Value of the value of the value of the variable is what the error is generated, and the practice is to type:

(gdb) Watch string2 [size - i]

GDB will make the following response:

WatchPoint 2: string2 [size - i] can now be used

Next command to perform a step

For loop:

(GDB) NEXT After the first cycle,

GDB tells us

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 = 0xbffdc4 "hello there") at Greeting.c: 2323 for (i = 0; i

I = 10, expressions

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 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 character in the string from the offset amount 0 to the offset amount 10, the offset amount is an empty character reservation. In order to make the code normal work has many modifications. One is A variable that is more than 1 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

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

New Post(0)