GDB 10-Minute tutorial

Source: Internet
Author: User

Original link: http://blog.csdn.net/liigo/archive/2006/01/17/582231.aspx

This article is written to programmers who work mainly in Windows operating systems and who need to develop some cross-platform software, as well as program enthusiasts.

GDB is a command-line-based, powerful program debugging tool published by the GNU Open source organization under the Unix/linux operating system.

GDB is a lot of commands, but we only need to grasp the 10 or so of the commands, you can generally complete the basic routine debugging work.

Command Explain Example
File < file name > Loads the executable program files that are being debugged.
Because GDB is typically executed in the same directory as the debugger, the text name does not need to have a path.
(gdb) file Gdb-sample
R Run the shorthand for running the program that is being debugged.
If the breakpoint has not been previously, the entire program is executed, and if there is a breakpoint, the program pauses at the first available breakpoint.
(GDB) R
C Continue, continue executing the debugger until the next breakpoint or program ends. (GDB) C
b < line number >
b < function name >
b *< Function name >
b *< Code address >

d [NUMBER]

B:breakpoint, set a breakpoint. You can specify the breakpoint location by using the line number function name "Execute address" and so on.
The "*" symbol in front of the function name indicates that the breakpoint is set at "Prolog code generated by the compiler". If you do not understand the assembly, you can disregard this usage.

D:delete Breakpoint, deletes a breakpoint of the specified number, or removes all breakpoints. The breakpoint number increments from 1 onwards.

(GDB) B 8
(GDB) B main
(GDB) B *main
(GDB) B *0x804835c

(GDB) d

S, n S: Executes a line of source code, if there is a function call in this line of code, enter the function;
N: Executes a line of source code, and the function calls in this line of code are executed in conjunction.

S is equivalent to "step into (single-step tracking entry)" in other debuggers;
N is equivalent to "step over" in other debuggers.

These two commands must be available in the case of source code debugging information (the "-G" parameter is used by GCC compilation).

(GDB) s
(GDB) n
Si, ni The SI command resembles the S command, and the NI command resembles the n command. The difference is that the two commands (Si/ni) are for assembly instructions, and S/n is for the source code. (GDB) Si
(gdb) NI
P < variable name > A shorthand for print that displays the value of the specified variable (temporary or global variable). (GDB) P I
(GDB) P Nglobalvar
Display ...

Undisplay < numbering >

Display, set the data to be displayed after the program is interrupted and its format.
For example, if you want to see the next assembly instruction that will be executed each time the program is interrupted, you can use the command
"Display/i $pc"
Where $pc represents the current assembly instruction,/I indicates to be displayed in 16. This command is useful when you need to care about assembly code.

Undispaly, cancels the previous display settings, incrementing the numbering from 1 onwards.

(GDB) display/i $pc

(GDB) Undisplay 1

I For info, please refer to "Help me" for details. (GDB) I R
Q Quit, exit the GDB debugging environment. (GDB) Q
Help [command name] GDB Help command that provides an explanation of the GDB name commands.
If the command name argument is specified, a detailed description of the command is displayed, and if no parameters are specified, the category displays all GDB commands for further browsing and querying by the user.
(GDB) Help display

 

Nonsense not much to say, below began to practice.

Give an example of a small program, C language code, simple can no longer simple:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33

//This program is only a sample code for "GdB 10 minute Tutorial" by Liigo
//email: [email protected]
//blog:http://blog.csdn.net/ Liigo
//website:www.liigo.com

#include <stdio.h>

int nglobalvar = 0;

int tempfunction (int a, int b)
{
    printf ("Tempfunction is called, a =%d, B =%d/n", A, b);     return (a + B);
}

int main ()
{
    int n;
    n = 1;
    n++;
    n--;

    Nglobalvar + = +;
    Nglobalvar-= 12;

    printf ("n =%d, Nglobalvar =%d/n", n, Nglobalvar);

    n = tempfunction (1, 2);
    printf ("n =%d", n);

    return 0;
}

Please copy this code and save it to the file gdb-sample.c, then switch to the directory where this file is located and compile it with GCC:

GCC Gdb-sample.c-o gdb-sample-g

In the above command line, using the-o parameter specifies that the compiled-generated executable file is named Gdb-sample, using the parameter-G to compile the source code information into the executable file. If you do not use the parameter-G, the subsequent debugging of GDB will cause inconvenience. Of course, if we do not have the source code of the program, it is naturally impossible to use the-G parameter, debugging/tracing can only be compiled code-level debugging/tracing.

The following "GDB" command launches GDB, which displays the GDB description first, regardless of it:

GNU gdb Red Hat Linux (5.3POST-0.20021129.18RH)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU general public License, and you are
Welcome to change it and/or distribute copies of it under certain.
Type "Show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "Show warranty" for details.
This GDB is configured as "I386-redhat-linux-gnu".
(GDB)

The last line Above "(GDB)" is the GDB internal command guide, waiting for the user to enter the GDB command.

The following uses the "file" command to load the debugged program Gdb-sample (here the gdb-sample is the executable file of the previous GCC compiler Output):

(gdb) file Gdb-sample
Reading symbols from Gdb-sample...done.

The last line above indicates that the load was successful.

The following uses the "R" command to execute (run) the debug file because no breakpoints have been set and will be executed directly to the end of the program:

(GDB) R
Starting program:/home/liigo/temp/test_jmp/test_jmp/gdb-sample
n = 1, Nglobalvar = 88
Tempfunction is called, a = 1, b = 2
n = 3
Program exited normally.

The following uses the "B" command to set a breakpoint at the beginning of the main function (breakpoint):

(GDB) B main
Breakpoint 1 at 0x804835c:file gdb-sample.c, line 19.

The last line above indicates that the breakpoint has been successfully set and gives the breakpoint information: set a breakpoint at line 19th of the source file gdb-sample.c, which is the first breakpoint in the program (ordinal 1); The code address at the breakpoint is 0x804835c (this value may only be valid during this debugging process). Looking back at the source code, the code in line 19th is "n = 1", which happens to be the first executable statement in the main function ("int n;" above). Defines a statement for a variable, not an executable statement).

Use the "R" command again to execute (run) the debugged program:

(GDB) R
Starting program:/home/liigo/temp/gdb-sample

Breakpoint 1, Main () at gdb-sample.c:19
n = 1;

The program breaks at line 19th gdb-sample.c, where the main function is the first executable statement.

The last line of information above is: The next source code to be executed is "n = 1;", which is the 19th line in the source code file GDB-SAMPLE.C.

Use the "s" Command (STEP) below to execute the next line of code (that is, line 19th "n = 1;" ):

(GDB) s
n++;

The above information indicates that "n = 1;" Is executed and the next code to be executed is "n++;" in line 20th.

Now that we have executed "n = 1;", that is, assigning a value of variable N to 1, we use the "P" command (Print) to see if the value of variable n is 1:

(GDB) P n
$ = 1

Sure enough, it was 1. ($ = Roughly means that this is the first time to use the "P" command--executing "P n" again will display "$ = 1"-This information should be of no use.) )

Here we set a breakpoint at the beginning of line 26th, tempfunction function (using the command "B" and "B tempfunction" respectively):

(GDB) B 26
Breakpoint 2 at 0x804837b:file gdb-sample.c, line 26.
(GDB) B tempfunction
Breakpoint 3 at 0x804832e:file gdb-sample.c, line 12.

Using the "C" command to continue (Continue) execution of the debugger, the program will break at the second breakpoint (26 lines), the global variable Nglobalvar value should be 88, and then execute the "C" command, the program will break the third breakpoint (12 lines, tempfunction At the beginning of the function), the values for the two parameters A and B of the tempfunction function should be 1 and 2, respectively:

(GDB) C
Continuing.

Breakpoint 2, Main () at gdb-sample.c:26
printf ("n =%d, Nglobalvar =%d/n", n, Nglobalvar);
(GDB) P Nglobalvar
$ = 88
(GDB) C
Continuing.
n = 1, Nglobalvar = 88

Breakpoint 3, Tempfunction (A=1, b=2) at Gdb-sample.c:12
printf ("Tempfunction is called, a =%d, B =%d/n", A, b);
(GDB) p a
$1
(GDB) P b
$4 = 2

The information above feedback everything is expected, haha ~ ~ ~

Execute the "C" command Again (Continue), since there are no more breakpoints, the program will execute until the end:

(GDB) C
Continuing.
Tempfunction is called, a = 1, b = 2
n = 3
Program exited normally.

 

Sometimes you need to see the compiler generated assembly code for assembly-level debugging or tracking, and how to do it?

This will use the display command "display/i $pc" (which is explained in detail earlier in this command):

(GDB) display/i $pc
(GDB)

The assembly code can then be displayed when the program is interrupted again:

(GDB) R
Starting program:/home/liigo/temp/test_jmp/test_jmp/gdb-sample

Breakpoint 1, Main () at gdb-sample.c:19
n = 1;
1:x/i $pc 0x804835c <main+16>: Movl $0X1,0XFFFFFFFC (%EBP)

See the assembly code, "n = 1;" The corresponding assembly code is "MOVL $0X1,0XFFFFFFFC (%EBP)".

and later the program each break will show the next assembly designation ("Si" command is used to execute a assembler code--which differs from "s" in executing a line of C code):

(GDB) Si
n++;
1:x/i $pc 0x8048363 <main+23>: Lea 0XFFFFFFFC (%EBP),%eax
(GDB) Si
0x08048366 n++;
1:x/i $pc 0x8048366 <main+26>: Incl (%EAX)
(GDB) Si
n--;
1:x/i $pc 0x8048368 <main+28>: Lea 0XFFFFFFFC (%EBP),%eax
(GDB) Si
0x0804836b n--;
1:x/i $pc 0x804836b <main+31>: Decl (%eax)
(GDB) Si
Nglobalvar + = 100;
1:x/i $pc 0x804836d <main+33>: Addl $0X64,0X80494FC

 

Next we try the command "b *< function name >".

To be more concise, it is necessary to remove all current breakpoints (using the "D" command--delete Breakpoint):

(GDB) d
Delete all breakpoints? (Y or N) y
(GDB)

When asked whether to delete all breakpoints, enter "Y" and press ENTER.

The following uses the command "B *main" to set breakpoints at the Prolog code of the main function (Prolog, Epilog, respectively, the code that the compiler inserts itself at the beginning and end of each function):

Breakpoint 4 at 0x804834c:file gdb-sample.c, line.
(GDB) r
The program being debugged have been started already.
Start it from the beginning? (Y or N) y
Starting program:/home/liigo/temp/test_jmp/test_jmp/gdb-sample

Breakpoint 4, Main () at Gdb-sample . c:17

1:x/i $pc 0x804834c <main>: Push%EBP
(gdb) Si
0x0804834d {
1:x/i $pc 0x804834d <m AIN+1>: mov%esp,%ebp
(gdb) SI
0x0804834f in Main () at Gdb-sample.c:17
+
1:x/i $pc 0x804834f <main+ 3>: Sub $0x8,%esp
(gdb) Si
0x08048352 {
1:x/i $pc 0x8048352 <main+6>: and $0xfffffff0,%esp
(GDB) Si
0x08048355 {
1:x/i $pc 0x8048355 <main+9>: mov $0x0,%eax
(gdb) Si
0x0804835a {
1:x/i $pc 0x804835a <main+14>: Sub%eax,%esp
(gdb) si
n = 1;
1:x/i $pc 0x804835c <main+16>: Movl $0X1,0XFFFFFFFC (%EBP)

You can now use the "I r" command to display the current value in the Register ——— "I R" is "Infomation Register":

(GDB) I R
EAX 0xbffff6a4-1073744220
ECX 0x42015554 1107383636
EdX 0x40016bc8 1073834952
EBX 0x42130a14 1108544020
ESP 0xbffff6a0 0xbffff6a0
EBP 0xbffff6a8 0xbffff6a8
ESI 0x40015360 1073828704
EDI 0x80483f0 134513648
EIP 0x8048366 0x8048366
EFlags 0x386 902
CS 0x23 35
SS 0X2B 43
DS 0x2b 43
Es 0x2b 43
FS 0x0 0
GS 0x33 51

Of course, you can also display any one of the specified register values:

(GDB) I r eax
EAX 0xbffff6a4-1073744220

 

The last command to introduce is "Q" and exit (quit) the GDB debug environment:

(GDB) Q
The program is running. Exit anyway? (Y or N) y

GDB 10-Minute tutorial

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.