GDB more than 10-minute tutorial

Source: Internet
Author: User

GDB more than 10-minute tutorial

Author: Liigo
Original link: http://blog.csdn.net/liigo/archive/2006/01/17/582231.aspx
Date: January 16, 2006

This article is written to programs that are mainly working on Windows operating systems and need to develop some cross-platform software for ape friends, 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.

There are many commands in gdb, though. But we just need to grasp the 10 or so orders. will be able to complete the daily major program debugging work.

Command Explain Demo sample
File < document name > Loads the executable program files that are being debugged.
Because GDB is normally run under the folder where the program is being debugged, the text name does not need a path.
(gdb) file Gdb-sample
R Run, execute the program that is being debugged.
Assume that no previous breakpoint has been hit. The entire program is executed and a breakpoint is assumed. The program pauses at the first available breakpoint.
(GDB) R
C Continue, continue to run the debugged program. Until the next breakpoint or program ends.

(GDB) C
b < line number >
b < function name >
b *< Function name >
b *< Code address >

d [NUMBER]

A shorthand for b:breakpoint. Sets a breakpoint. Both can specify the breakpoint location by using the line number function name, run address, and so on.


The "*" symbol in front of the function name indicates that the breakpoint is set to "Prolog code generated by the compiler". If you do not understand the assembly, you can disregard the use of this method.

D:delete Breakpoint, deletes a breakpoint of the specified number. or remove all breakpoints.

The breakpoint number is incremented from 1.

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

(GDB) d

S, n S: Run a line of source code, assuming that there is a function call in this line code, then enter the function;
N: Run a line of source program code. The function calls in this line of code also run.

S is equivalent to "Step into (Single Step Into") in other debuggers.
N is equivalent to "step over" in other debuggers.

These two commands must be able to be used in the case of Source debugging information (the "-G" parameter is used when GCC compiles).

(GDB) s
(GDB) n
Si, ni The SI command is similar to the S command, the NI command is similar to 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. Displays the value of the specified variable (a temporary variable or a 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, suppose you want to be able to see the next assembly instruction that will be running each time the program is interrupted. Ability to use commands
"Display/i $pc"
$PC represents the current assembly instruction,/I indicates to be displayed in 16. When you need to care about assembly code. This command is quite practical.

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

(GDB) display/i $pc

(GDB) Undisplay 1

I For info, please refer to "Help me" for details. (GDB) I R
Q The shorthand for quit. Exit the GDB debugging environment. (GDB) Q
Help [command name] GDB help command. Provides an explanation of the GDB name commands.
If you specify a command name parameter, the specific description of the command is displayed. Assuming 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, the following begins to practice.

A small program for demonstrating the sample is given first. C language code. Simple can no longer be simpler:

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 demo 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 folder where the file resides. Compiled with GCC:

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

In the above command line, the run-time file name of the build is specified with the-o parameter, which is called Gdb-sample. Use the parameter-G to compile the source information into a running file. Assume that the parameter-G is not used. Will cause inconvenience to the debugging behind GDB. Of course. Assuming that we do not have the source code of the program, it is naturally impossible to use the-G parameter, Debug/trace can only be compiled code-level debugging/tracing.

The following "GDB" command launches GDB, which displays the GDB description first. Whatever it is:

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 gdb-sample is the executable file of the previous GCC compiled output):

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

The last line above indicates that a success has been loaded.

The following uses the "R" command to run the (run) debug file. Since no breakpoint has been set, it will run 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 this program (ordinal 1). The code address at the breakpoint is 0x804835c (this value may only be valid during this debugging process). Look back to the source. The code in line 19th is "n = 1". It happens to be the first running statement in the main function ("int n;" in front). Defines a statement for a variable, not a statement that can be run.

Run the debug program again using the "R" command:

(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 of GDB-SAMPLE.C, where the main function is the first one to run the statement.

The last line of information above is: the next source to be run is "n = 1;", which is the 19th line in the source file gdb-sample.c.

The following runs the next line of code (that is, line 19th "n = 1;") using the "s" Command (Step). ):

(GDB) s
n++;

The above information indicates that "n = 1;" is already running. and displays the next code to be run as "n++;" in line 20th.

Now that you have run "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. ($ 1 Roughly means that this is the first time that the "P" command is used--running "P N" again will show "$" = "-"--this information should be of no use.) )

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

(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) to run the Debugged program, the program will break at the second breakpoint (26 rows), at which time the global variable Nglobalvar value should be 88; run the "C" command again. The program breaks at the third breakpoint (12 lines, at the beginning of the Tempfunction function), at which point the two parameters A and B of the tempfunction function should each be 1 and 2:

(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 ~ ~ ~

Run the "C" command Again (Continue). Since there are no more breakpoints behind. The program will run 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 tracing. And how do we do that?

This will use the display command "display/i $pc" (which is explained 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 assembler code is "MOVL $0X1,0XFFFFFFFC (%EBP)".

And then each break of the program will show the next assembly designation (the "si" command is used to run a assembler code--different from "s" Running a line 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 breakpoints now (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. The code that the compiler inserts itself at the beginning and end of each function, respectively):

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 use the "I r" command to display the current value in the Register ——— "I R" or "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 a random value of the specified register:

(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

All rights reserved: Liigo.com

Please obtain the permission of the author Liigo prior to reprint.

[Email protected]
Www.liigo.com
http://blog.csdn.net/liigo/
qq:175199125

GDB more than 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.