wiki頁:http://code.google.com/p/ldd6410/wiki/GDBDebug
不論是調試Linux內核空間的驅動仍是調試用戶空間的應用程序,掌握gdb的用法都是必須。並且,調試內核和調試應用程序時使用的gdb命令是徹底相同的,下面以代碼清單22.2的應用程序爲例演示gdb調試器的用法。
1 int add(int a, int b)
2 {
3 return a + b;
4 }
5
6 main()
7 {
8 int sum[10] =
9 {
10 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
11 } ;
12 int i;
13
14 int array1[10] =
15 {
16 48, 56, 77, 33, 33, 11, 226, 544, 78, 90
17 };
18 int array2[10] =
19 {
20 85, 99, 66, 0x199, 393, 11, 1, 2, 3, 4
21 };
22
23 for (i = 0; i < 10; i++)
24 {
25 sum[i] = add(array1[i], array2[i]);
26 }
27 }
[root@localhost driver_study]# gdb gdb_example
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 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-redhat-linux-gnu"...
(gdb)
(gdb) list 15
10
11 int array1[10] =
12 {
13 48, 56, 77, 33, 33, 11, 226, 544, 78, 90
14 };
15 int array2[10] =
16 {
17 85, 99, 66, 0x199, 393, 11, 1, 2, 3, 4
18 };
19
list <function> ,顯示函數名爲function的函數的源程序,如:
(gdb) list main
2 {
3 return a + b;
4 }
5
6 main()
7 {
8 int sum[10];
9 int i;
10
11 int array1[10] =
list,顯示當前行後面的源程序。
list - ,顯示當前行前面的源程序。
(gdb) break add
Breakpoint 1 at 0x80482f7: file gdb_example.c, line 3.
(gdb) run
Starting program: /driver_study/gdb_example
Breakpoint 1, add (a=48, b=85) at gdb_example.c:3
warning: Source file is more recent than executable.
3 return a + b;
(gdb) next
4 }
(gdb) next
main () at gdb_example.c:23
23 for (i = 0; i < 10; i++)
(gdb) next
25 sum[i] = add(array1[i], array2[i]);
(gdb) print sum
$1 = {133, 0, 0, 0, 0, 0, 0, 0, 0, 0}
set args 可指定運行時參數,如:set args 10 20 30 40 50;show args 命令能夠查看設置好的運行參數。
path <dir> 可設定程序的運行路徑;how paths可查看程序的運行路徑;set environment varname [=value]用於設置環境變量,如set env USER=baohua;show environment [varname]則用於查看環境變量。
cd <dir> 至關於shell的cd命令;pwd 顯示當前所在的目錄。
info terminal 用於顯示程序用到的終端的模式;gdb中也可使用重定向控制程序輸出,如run > outfile;tty命令能夠指定輸入輸出的終端設備,如:tty /dev/ttyS1。
在進入指定函數時停住,C++中可使用class::function或function(type, type)格式來指定函數名。
在指定行號停住。
在當前行號的前面或後面的offset行停住,offiset爲天然數。
在源文件filename的linenum行處停住。
在源文件filename的function函數的入口處停住。
在程序運行的內存地址處停住。
break命令沒有參數時,表示在下一條指令處停住。
「...」能夠是上述的break <linenum>、break +offset / break –offset中的參數,condition表示條件,在條件成立時停住。好比在循環體中,能夠設置break if i=100,表示當i爲100時停住程序。 查看斷點時,可以使用info命令,如info breakpoints [n]、info break [n](n表示斷點號)。
(gdb) break 25
Breakpoint 1 at 0x8048362: file gdb_example.c, line 25.
(gdb) run
Starting program: /driver_study/gdb_example
Breakpoint 1, main () at gdb_example.c:25
25 sum[i] = add(array1[i], array2[i]);
(gdb) step
add (a=48, b=85) at gdb_example.c:3
3 return a + b;
單步跟蹤,若是有函數調用,它不會進入該函數。一樣地,next後面不加count表示一條條地執行,加表示執行後面的count條指令,而後再停住。
set step-mode on用於打開step-mode模式,這樣,在進行單步跟蹤時,程序不會由於沒有debug信息而不停住,這個參數的設置可便於查看機器碼。set step-mod off用於關閉step-mode模式。
運行程序,直到當前函數完成返回,並打印函數返回時的堆棧地址和返回值及參數值等信息。
一直在循環體內執行單步,退不出來是一件使人煩惱的事情,until命令能夠運行程序直到退出循環體。
stepi和nexti用於單步跟蹤一條機器指令,一條程序代碼有可能由數條機器指令完成,stepi和nexti能夠單步執行機器指令。 另外,運行「display/i $pc」命令後,單步跟蹤會在打出程序代碼的同時打出機器指令,即彙編代碼。
continue [ignore-count]
c [ignore-count]
fg [ignore-count]
(gdb) continue
Continuing.
Hardware watchpoint 3: i
Old value = 2
New value = 3
0x0804838d in main () at gdb_example.c:23
23 for (i = 0; i < 10; i++)
(gdb) continue
Continuing.
Breakpoint 1, main () at gdb_example.c:25
25 sum[i] = add(array1[i], array2[i]);
(gdb) continue
Continuing.
Hardware watchpoint 3: i
Old value = 3
New value = 4
0x0804838d in main () at gdb_example.c:23
23 for (i = 0; i < 10; i++)
print <expr>
print /<f> <expr>
<expr>是表達式,是被調試的程序中的表達式,<f>是輸出的格式,好比,若是要把表達式按16進制的格式輸出,那麼就是/x。在表達式中,有幾種GDB所支持的操做符,它們能夠用在任何一種語言中,「@」是一個和數組有關的操做符,「::」指定一個在文件或是函數中的變量,「{<type>} <addr>」表示一個指向內存地址<addr>的類型爲type的一個對象。
(gdb) print sum
$2 = {133, 155, 0, 0, 0, 0, 0, 0, 0, 0}
(gdb) next
Breakpoint 1, main () at gdb_example.c:25
25 sum[i] = add(array1[i], array2[i]);
(gdb) next
23 for (i = 0; i < 10; i++)
(gdb) print sum
$3 = {133, 155, 143, 0, 0, 0, 0, 0, 0, 0}
int *array = (int *) malloc (len * sizeof (int));
p *array@len
咱們可用display命令設置一些自動顯示的變量,當程序停住時,或是單步跟蹤時,這些變量會自動顯示。 若是要修改變量,如x的值,可以使用以下命令:
print x=4
(gdb) watch i
Hardware watchpoint 3: i
(gdb) next
23 for (i = 0; i < 10; i++)
(gdb) next
Hardware watchpoint 3: i
Old value = 0
New value = 1
0x0804838d in main () at gdb_example.c:23
23 for (i = 0; i < 10; i++)
(gdb) next
Breakpoint 1, main () at gdb_example.c:25
25 sum[i] = add(array1[i], array2[i]);
(gdb) next
23 for (i = 0; i < 10; i++)
(gdb) next
Hardware watchpoint 3: i
Old value = 1
New value = 2
0x0804838d in main () at gdb_example.c:23
23 for (i = 0; i < 10; i++)
x/<n/f/u> <addr>
return
return <expression>
info line tst.c:func
(gdb) disassemble func
Dump of assembler code for function func:
0x8048450 <func>: push %ebp
0x8048451 <func+1>: mov %esp,%ebp
0x8048453 <func+3>: sub $0x18,%esp
0x8048456 <func+6>: movl $0x0,0xfffffffc(%ebp)
...
End of assembler dump.