Linux 包含了一個(gè)叫 gdb 的 GNU 調(diào)試程序. gdb 是一個(gè)用來(lái)調(diào)試 C 和 C++ 程序的強(qiáng)力調(diào)試器. 它使你能在程序運(yùn)行時(shí)觀察程序的內(nèi)部結(jié)構(gòu)和內(nèi)存的使用情況. 以下是 gdb 所提供的一些功能:
1、啟動(dòng)你的程序,可以按照你的自定義的要求隨心所欲的運(yùn)行程序。
2、可讓被調(diào)試的程序在你所指定的調(diào)置的斷點(diǎn)處停住。(斷點(diǎn)可以是條件表達(dá)式)
3、當(dāng)程序被停住時(shí),可以檢查此時(shí)你的程序中所發(fā)生的事。
4、動(dòng)態(tài)的改變你程序的執(zhí)行環(huán)境。
當(dāng)你啟動(dòng)
gdb 后, 你能在命令行上指定很多的選項(xiàng). 可以以下面的方式來(lái)運(yùn)行
gdb gdb <fname>
當(dāng)你用這種方式運(yùn)行
gdb , 你能直接指定想要調(diào)試的程序. 這將告訴
gdb 裝入名為 fname 的可執(zhí)行文件. 你也可以用
gdb 去檢查一個(gè)因程序異常終止而產(chǎn)生的 core 文件, 或者與一個(gè)正在運(yùn)行的程序相連. 你可以參考
gdb 指南頁(yè)或在命令行上鍵入
gdb -h 得到一個(gè)有關(guān)這些選項(xiàng)的說(shuō)明的簡(jiǎn)單列表.
為調(diào)試編譯代碼
為了使
gdb 正常工作, 你必須使你的程序在編譯時(shí)包含調(diào)試信息. 調(diào)試信息包含你程序里的每個(gè)變量的類型和在可執(zhí)行文件里的地址映射以及源代碼的行號(hào).
gdb 利用這些信息使源代碼和機(jī)器碼相關(guān)聯(lián).
在編譯時(shí)用 -g 選項(xiàng)打開調(diào)試選項(xiàng).
命 令 |
描 述 |
file |
裝入想要調(diào)試的可執(zhí)行文件. |
kill |
終止正在調(diào)試的程序. |
list |
列出產(chǎn)生執(zhí)行文件的源代碼的一部分. |
next |
執(zhí)行一行源代碼但不進(jìn)入函數(shù)內(nèi)部. |
step |
執(zhí)行一行源代碼而且進(jìn)入函數(shù)內(nèi)部. |
run |
執(zhí)行當(dāng)前被調(diào)試的程序 |
quit |
終止 gdb |
watch |
使你能監(jiān)視一個(gè)變量的值而不管它何時(shí)被改變. |
break |
在代碼里設(shè)置斷點(diǎn), 這將使程序執(zhí)行到這里時(shí)被掛起. |
make |
使你能不退出 gdb 就可以重新產(chǎn)生可執(zhí)行文件. |
shell |
使你能不離開 gdb 就執(zhí)行 UNIX shell 命令. |
(a)設(shè)置斷點(diǎn)
break 20;---在第20行設(shè)置斷點(diǎn)
break func;---在函數(shù)func的入口處設(shè)置斷點(diǎn)
(b)取消斷點(diǎn)
delete break 20;---取消第20行的斷點(diǎn)
delete break func;---取消函數(shù)func入口處的斷點(diǎn)
(c)運(yùn)行代碼
run;
r;
(d)顯示變量或函數(shù)值
display;
p;
(e)單步執(zhí)行
next;
n;
(f)跳步執(zhí)行
step;
s;
(g)循環(huán)執(zhí)行
continue;
c;
(h)列出運(yùn)行棧內(nèi)容
bt;
一個(gè)調(diào)試示例
——————
源程序:tst.c
1 #include <stdio.h>
2
3 int func(int n)
4 {
5 int sum=0,i;
6 for(i=0; i<n; i++)
7 {
8 sum+=i;
9 }
10 return sum;
11 }
12
13
14 main()
15 {
16 int i;
17 long result = 0;
18 for(i=1; i<=100; i++)
19 {
20 result += i;
21 }
22
23 printf("result[1-100] = %d \n", result );
24 printf("result[1-250] = %d \n", func(250) );
25 }
編譯生成執(zhí)行文件:(Linux下)
cc -g tst.c -o tst
使用GDB調(diào)試:
gdb tst <---------- 啟動(dòng)GDB
GNU gdb 5.1.1
Copyright 2002 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 conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-SuSE-linux"...
(gdb) l <-------------------- l命令相當(dāng)于list,從第一行開始例出原碼。
1 #include <stdio.h>
2
3 int func(int n)
4 {
5 int sum=0,i;
6 for(i=0; i<n; i++)
7 {
8 sum+=i;
9 }
10 return sum;
(gdb) <-------------------- 直接回車表示,重復(fù)上一次命令
11 }
12
13
14 main()
15 {
16 int i;
17 long result = 0;
18 for(i=1; i<=100; i++)
19 {
20 result += i;
(gdb) break 16 <-------------------- 設(shè)置斷點(diǎn),在源程序第16行處。
Breakpoint 1 at 0x8048496: file tst.c, line 16.
(gdb) break func <-------------------- 設(shè)置斷點(diǎn),在函數(shù)func()入口處。
Breakpoint 2 at 0x8048456: file tst.c, line 5.
(gdb) info break <-------------------- 查看斷點(diǎn)信息。
Num Type Disp Enb Address What
1 breakpoint keep y 0x08048496 in main at tst.c:16
2 breakpoint keep y 0x08048456 in func at tst.c:5
(gdb) r <--------------------- 運(yùn)行程序,run命令簡(jiǎn)寫
Starting program: /home/hchen/test/tst
Breakpoint 1, main () at tst.c:17 <---------- 在斷點(diǎn)處停住。
17 long result = 0;
(gdb) n <--------------------- 單條語(yǔ)句執(zhí)行,next命令簡(jiǎn)寫。
18 for(i=1; i<=100; i++)
(gdb) n
20 result += i;
(gdb) n
18 for(i=1; i<=100; i++)
(gdb) n
20 result += i;
(gdb) c <--------------------- 繼續(xù)運(yùn)行程序,continue命令簡(jiǎn)寫。
Continuing.
result[1-100] = 5050 <----------程序輸出。
Breakpoint 2, func (n=250) at tst.c:5
5 int sum=0,i;
(gdb) n
6 for(i=1; i<=n; i++)
(gdb) p i <--------------------- 打印變量i的值,print命令簡(jiǎn)寫。
$1 = 134513808
(gdb) n
8 sum+=i;
(gdb) n
6 for(i=1; i<=n; i++)
(gdb) p sum
$2 = 1
(gdb) n
8 sum+=i;
(gdb) p i
$3 = 2
(gdb) n
6 for(i=1; i<=n; i++)
(gdb) p sum
$4 = 3
(gdb) bt <--------------------- 查看函數(shù)堆棧。
#0 func (n=250) at tst.c:5
#1 0x080484e4 in main () at tst.c:24
#2 0x400409ed in __libc_start_main () from /lib/libc.so.6
(gdb) finish <--------------------- 退出函數(shù)。
Run till exit from #0 func (n=250) at tst.c:5
0x080484e4 in main () at tst.c:24
24 printf("result[1-250] = %d \n", func(250) );
Value returned is $6 = 31375
(gdb) c <--------------------- 繼續(xù)運(yùn)行。
Continuing.
result[1-250] = 31375 <----------程序輸出。
Program exited with code 027. <--------程序退出,調(diào)試結(jié)束。
(gdb) q <--------------------- 退出gdb。
posted on 2008-04-18 15:51
一凡 閱讀(497)
評(píng)論(0) 編輯 收藏 所屬分類:
Linux C