GDB is an essential debugging tool for Linux C + +, which is much more powerful than other Ides. This article mainly introduces some basic uses of GDB, starting the debugger, setting breakpoints, displaying variable values, stepping through, and so on.
Take the following small code test.c as an example, perform environment ubuntu14.04, 32-bit:
1 #include <stdio.h> 2 #include <unistd.h> 3 4 int sum; 5 6 void swap (int *m, Int *n) 7 { 8 int tmp; 9 10 tmp = *m; 11 *m = *n; 12 *n = tmp; 13 } 14 15 int diff_fun (int x, int y) 16 { 17 int tmp_sum; 18 19 if (x > y) { 20 swap (&x, &y); 21 } 22 23 tmp_sum = y - x; 24 25 return tmp_sum; 26 } 27 28 int main ( ) 29 { 30 int a, b; 31 32 a = 10; 33 b = 5; 34 sum = diff_fun (a, b); 35 36 return sum; 37 }
1. Preparation
To debug using GDB, you must use the-G option at GCC compilation, such as:
Gcc-g-wall test.c-o Test
2. Start the debugger
#gdb./test -----> execution GDB
[Email protected]:test_work# gdb./test
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) Free Software Foundation, Inc.
License gplv3+: GNU GPL version 3 or later
This was free software:you was free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "Show copying"
and "Show warranty" for details.
This GDB is configured as "I686-linux-gnu".
Type "Show Configuration" for configuration details.
For bugs reporting instructions, please see:
Find the GDB manual and other documentation resources online at:
For help, type ' help '.
Type "Apropos word" to search for commands related to "word" ...
Reading symbols from./test...done.
(GDB)
(GDB) Start -----> Start Debugging
Temporary breakpoint 1 at 0x8048421:file GDB.C, line 32.
Starting program:/root/work/test_work/test
Temporary breakpoint 1, Main () at gdb.c:32
a = 10;
(GDB)
3. Set Breakpoints
GDB can set breakpoints in a number of ways, depending on the function, line number, current position offset, address, and so on. Break write can also be abbreviated to B, such as:
Break line number Break function name Break + offset B-offset B * address
As implemented:
(GDB) List -----> list The code for a nearby row
27
int Main ()
{
int A, b;
31
a = 10;
B = 5;
sum = Diff_fun (A, b);
35
return sum;
(GDB) b diff_fun -----> Break point for Diff_fun
Breakpoint 2 at 0x80483eb:file GDB.C, line 19.
(GDB) b -----> Break point at 23 lines
Breakpoint 3 at 0x8048407:file GDB.C, line 23.
(GDB) b +1 -----> Pause position down offset line break point
Breakpoint 4 at 0x8048428:file GDB.C, line 33.
(gdb) Disas diff_fun -----> disassembly of the specified function code, if not with the function name, the default disassembly of the current function
Dump of assembler code for function Diff_fun:
0x080483e5 <+0>:push%EBP
0x080483e6 <+1>:mov%esp,%ebp
0x080483e8 <+3>:Sub $0x18,%esp
0x080483eb <+6>:mov 0x8 (%EBP),%edx
0x080483ee <+9>:mov 0xc (%EBP),%eax
0x080483f1 <+12>:cmp%eax,%edx
0x080483f3 <+14>:jle 0x8048407 <diff_fun+34>
0x080483f5 <+16>:Lea 0xc (%EBP),%eax
0x080483f8 <+19>:mov%eax,0x4 (%ESP)
0X080483FC <+23>:Lea 0x8 (%EBP),%eax
0X080483FF <+26>:mov%eax, (%ESP)
0x08048402 <+29>:call 0X80483C3 <swap>
0x08048407 <+34>:mov 0xc (%EBP),%edx
0x0804840a <+37>:mov 0x8 (%EBP),%eax
0x0804840d <+40>:mov%edx,%ecx
0x0804840f <+42>:Sub%eax,%ecx
0x08048411 <+44>:mov%ecx,%eax
0x08048413 <+46>:mov%eax,-0x4 (%EBP)
0x08048416 <+49>:mov-0x4 (%EBP),%eax
0x08048419 <+52>:leave
0x0804841a <+53>:ret
End of assembler dump.
(GDB) b *0x080483e8 -----> Break breakpoints at a designated location
Breakpoint 5 at 0x80483e8:file GDB.C, line 16.
(GDB) I b -----> View the Breakpoints Set
Num Type Disp Enb Address What
2 Breakpoint Keep Y 0x080483eb in Diff_fun at gdb.c:19
3 Breakpoint Keep Y 0x08048407 in Diff_fun at gdb.c:23
4 Breakpoint Keep Y 0x08048428 in main at gdb.c:33
5 Breakpoint Keep Y 0x080483e8 in Diff_fun at gdb.c:16
4.display variables, registers, address values, and single step execution
Print is used to display the value of a variable, abbreviated to P
Info can be used to display the value of the Register, abbreviated to I
The x command is used to display the contents of memory
(GDB) Run ----> Continue execution until the first breakpoint is encountered
The program being debugged have been started already.
Start it from the beginning? (Y or N) y
Starting program:/root/work/test_work/test
Breakpoint 4, Main () at gdb.c:33
B = 5;
(gdb) layout split ----> View Source and assembly code in different windows, and currently paused position
650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M02/8F/43/wKioL1jZIHryKQ4gAACh3xOHeK8740.png "title=" Qq20170327222249.png "width=" "height=" 284 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:400PX;HEIGHT:284PX; "alt = "Wkiol1jzihrykq4gaach3xohek8740.png"/>
(GDB) p a -----> Print variable a
$ = 10
(GDB)
Also p can assign values to variables, such as:
(GDB) P a=9 -----> Modify the value of variable a
$9
(GDB) p a
$4 = 9
(GDB)
(GDB) Info reg -----> Show values with multiple registers
EAX 0x11
ECX 0xaace997e-1429300866
edx 0xbffff614-1073744364
EBX 0xb7fbe000-1208229888
ESP 0xbffff5d00xbffff5d0 ---> may be used frequently
EBP 0xbffff5e80xbffff5e8
ESI 0x00
EDI 0x00
EIP 0x80484280x8048428 <main+13> ---> may be used frequently
EFlags 0x282[SF IF]
CS 0x73115
SS 0x7b123
DS 0x7b123
ES 0x7b123
FS 0x00
GS 0x3351
(GDB)
The SP and program counter IPs that point to the top of the stack may be used frequently, or a register can be displayed separately, for example,
(GDB) P $EIP -----> Print a register
$ = (void (*) ()) 0x8048428 <main+13>
(GDB)
Print also supports a variety of printing formats, such as:
X appears as hexadecimal
D displayed as Decimal
o Shown as octal
A display address
C Display character types
F Display floating point decimal type
S is displayed as a string
I display for machine language (x command available)
(GDB) p/c a ----> characters to display the value of variable a
$6 = 9 ' \ t '
(GDB)
(GDB) x &a -----> Displays the value of the address where variable A is located
0xbffff5e4:9 ' \ t '
(GDB) x/i A
0X9:cannot access memory at address 0x9
(gdb) x/i &a -----> Show assembly Instructions for variable a address
0xbffff5e4:or%eax, (%EAX)
(GDB)
(GDB) C ----->continue continue execution, abbreviated to C, paused at the next breakpoint
Continuing.
Breakpoint 2, Diff_fun (x=9, y=5) at gdb.c:19
if (x > Y) {
(GDB) L
14
int Diff_fun (int x, int y)
+{
Tmp_sumint;
18
if (x > Y) {
Swap (&x, &y);
+}
22
tmp_sum = y-x;
(GDB)
(GDB) bt ------>backtrace Show all stack frames
#0 Diff_fun (x=9, y=5) at gdb.c:19
#1 0x08048441 in Main () at gdb.c:34
(GDB
(GDB) Frame 1 -----> enter stack frame 1
#1 0x08048441 in Main () at gdb.c:34
sum = Diff_fun (A, b);
(GDB) I locals
A = 9
b = 5
(GDB) frame 0 ------> enter stack frame 0
#0 Diff_fun (x=9, y=5) at gdb.c:19
if (x > Y) {
(GDB) bt
#0 Diff_fun (x=9, y=5) at gdb.c:19
#1 0x08048441 in Main () at gdb.c:34
(GDB)
(GDB) bt full-----> display more comprehensive information for each stack frame
#0 Diff_fun (x=9, y=5) at gdb.c:19
Tmp_sum = 134513408
#1 0x08048441 in Main () at gdb.c:34
A = 9
b = 5
(GDB)
Single step execution has next and step where the latter will enter the function, the former does not, plus I words nexti, Stepi, single-step assembly instructions, which is less used in large programs.
If you delete a breakpoint, use the D number
(GDB) I B
Num Type Disp Enb Address What
2 Breakpoint Keep Y 0x080483eb in Diff_fun at gdb.c:19
Breakpoint already hit 1 time
3 Breakpoint Keep Y 0x08048407 in Diff_fun at gdb.c:23
4 Breakpoint Keep Y 0x08048428 in main at gdb.c:33
Breakpoint already hit 1 time
5 Breakpoint Keep Y 0x080483eb in Diff_fun at gdb.c:16
Breakpoint already hit 1 time
(GDB) d 2 ----->delete number 2 Breakpoint
(GDB) I B
Num Type Disp Enb Address What
3 Breakpoint Keep Y 0x08048407 in Diff_fun at gdb.c:23
4 Breakpoint Keep Y 0x08048428 in main at gdb.c:33
Breakpoint already hit 1 time
5 Breakpoint Keep Y 0x080483eb in Diff_fun at gdb.c:16
Breakpoint already hit 1 time
(GDB)
These are some of the basic GDB commands, and the following are some common scenarios.
Resources:
Introduction to GDB debugging, high quality guide written by Daniel
Debug hacks
Mans page
This article is from the "12128867" blog, please be sure to keep this source http://12138867.blog.51cto.com/12128867/1910950
GDB Basic Knowledge One