了解gdb用法
更新記錄
item | note |
---|---|
20170105 | 第一版 |
目錄
gdb指令
指令皆可以使用help去查訊
ex.help break
cmd | note |
---|---|
l (list) | 顯示內容 |
b (breakpoint) | 設定斷點 |
r (run執行) | 會執行到斷點停止 |
n (next) | 執行下一步,若遇到function會當成一個 |
step | 單步執行,遇到function會進入單步執行 |
q (quit) | 離開 |
display a | 設定變數a,每次顯示內容 |
info display | 查看display設定內容 |
info break | 查看break設定內容 |
{disable/enable} breakpoints 1 | {關閉/開啟} 第1個斷點 |
{disable/enable} display 1 | {關閉/開啟} 第1個變數 |
gdb
example
gcc要帶-g參數(-g: Debugging Options)1
2
3
4
5
6
7
8
9
10
11
12
13[root@localhost tt]# cat test2.c
#include <stdio.h>
int main()
{
int a,b,c;
a = 5;
b = 10;
b +=a;
c = b+a;
return 0;
}
[root@localhost tt]# gcc -Wall -g test2.c
test
使用gdb帶起執行檔
1
gdb a.out
list顯示程式內容 (或則用 l)
list 5 or list 101
2
3
4
5
6
7
8
9
10
11(gdb) l
1 #include <stdio.h>
2
3 int main()
4 {
5 int a,b,c;
6 a = 5;
7 b = 10;
8 b +=a;
9 c = b+a;
10 return 0;設定中斷點(breakpoint)
1
2(gdb) b 2
Breakpoint 1 at 0x4004da: file test2.c, line 2.執行(run)
1
2
3
4
5
6(gdb) r
Starting program: /root/tt/a.out
Missing separate debuginfos, use: dnf debuginfo-install glibc-2.23.1-8.fc24.x86_64
Breakpoint 1, main () at test2.c:6
6 a = 5;查看數值
未設定值之前a為0(line 6),設定值之後為5(linie 7)
n (next),執行下一行
p (print),打印變數內容1
2
3
4
5
6
7
8Breakpoint 1, main () at test2.c:6
6 a = 5;
(gdb) p a
$1 = 0
(gdb) n
7 b = 10;
(gdb) p a
$2 = 5執行到結束
c (continue)執行到結束1
2
3
4
5
6
7
8
9
10
11
12(gdb) c
Continuing.
Breakpoint 2, main () at test2.c:10
10 return 0;
(gdb) print a
$16 = 5
(gdb) print b
$17 = 15
(gdb) print c
$18 = 20
(gdb)
display
display
display a : 設定變數a,每次顯示內容
info display: 查看全部的display內容1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /root/tt/a.out
Breakpoint 1, main () at test2.c:6
6 a = 5;
2: b = 0
(gdb) n
7 b = 10;
2: b = 0
(gdb) n
8 b +=a;
2: b = 10
(gdb) n
9 c = b+a;
2: b = 15
(gdb) info display
Auto-display expressions now in effect:
Num Enb Expression
2: y b
(gdb)disalbe display
disable display 2
關閉顯示第2個變數(即變數c)1
2
3
4
5
6
7
8
9
10
11
12(gdb) info display
Auto-display expressions now in effect:
Num Enb Expression
1: y b
2: y c
(gdb) disable display 2
(gdb) info display
Auto-display expressions now in effect:
Num Enb Expression
1: y b
2: n c
(gdb)
關閉斷點
- disable breakpoints 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000004004da in main at test2.c:6
breakpoint already hit 1 time
(gdb) disable breakpoints 1
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep n 0x00000000004004da in main at test2.c:6
breakpoint already hit 1 time
(gdb) r
Starting program: /root/tt/a.out
[Inferior 1 (process 12772) exited normally]
(gdb) r
Starting program: /root/tt/a.out
[Inferior 1 (process 12773) exited normally]
(gdb) c
The program is not being run.
(gdb) r
Starting program: /root/tt/a.out
[Inferior 1 (process 12778) exited normally]
(gdb)
gdb正在執行的程式
- 先取得程式的pid
- 開啟gdb
- attach pid
進入process pid的gdb - detach
離開process pid的gdb