gdb概述
當程序編譯完成后,它可能無法正常運行;或許程序會徹底崩潰;或許只是不能正常地運行某些功能;或許它的輸出會被掛起;或許不會提示要求正常的輸入。無論在何種情況下,都需要一種工具來跟蹤這些問題,gdb(GNU debugger)是一個調試器,是用來幫助程序員尋找程序中的錯誤的軟件。
gdb主要幫忙用戶完成下面4個方面的功能:
- 啟動程序,可以按照用戶自定義的要求隨心所欲的運行程序。
- 可讓被調試的程序在用戶所指定的調試的斷點處停住 (斷點可以是條件表達式)。
- 當程序停住時,可以檢查此時程序中所發生的事。
- 動態地改變程序的執行環境。
gdb的使用
//test_gdb.c
#include <stdio.h>
int func(int n)
{
int sum=0,i;
for(i=0; i<n; i++) {
sum+=i;
}
return sum;
}
int main(void)
{
int i;
long result = 0;
for(i=1; i<=100; i++) {
result += i;
}
printf("result[1-100] = %ld \n", result );
printf("result[1-250] = %d \n", func(250) );
}
gdb主要調試的是C/C++的程序。要調試C/C++的程序,首先在編譯時,必須要把調試信息加到可執行文件中。使用編譯器的 -g 參數即可。如果沒有-g,將看不見程序的函數名和變量名,代替它們的全是運行時的內存地址。使用gcc生成執行文件:
gcc -g test_gdb.c -o test_gdb
用gdb來調試:
gdb test_gdb
GNU gdb (GDB) 7.12
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are 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 was configured as "x86_64-apple-darwin16.1.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test_gdb...Reading symbols from /Users/zhengzhizhao/Local Documents/learnCode/linux-project/test_gdb.dSYM/Contents/Resources/DWARF/test_gdb...done.
done.
// l命令相當于list命令,從第一行開始列出源碼:
(gdb) l
1 #include <stdio.h>
2 int func(int n)
3 {
4 int sum=0,i;
5 for(i=0; i<n; i++) {
6 sum+=i;
7 }
8 return sum;
9 }
10
// 設置斷點,在源程序第16行處。
(gdb) break 16
Breakpoint 1 at 0x100000f08: file test_gdb.c, line 16.
(gdb) break func
Breakpoint 2 at 0x100000ea7: file test_gdb.c, line 4.
// 查看斷點
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000100000f08 in main at test_gdb.c:16
2 breakpoint keep y 0x0000000100000ea7 in func at test_gdb.c:4
// 運行程序,run
(gdb) r
Starting program: /Users/zhengzhizhao/Local Documents/learnCode/linux-project/test_gdb
Breakpoint 1, main () at test.c:16
16 long result = 0;
// 單條語句執行,next
(gdb) n
17 for(i=1; i<=100; i++)
(gdb) n
19 result += i;
(gdb) n
17 for(i=1; i<=100; i++)
(gdb) n
19 result += i;
(gdb) n
17 for(i=1; i<=100; i++)
// 繼續運行程序,continue
(gdb) c
Continuing.
result[1-100] = 5050 <----------程序輸出。
Breakpoint 2, func (n=250) at test.c:5
5 int sum=0,i;
(gdb) n
6 for(i=0; i<n; i++)
// 打印變量i的值,print。
(gdb) p I
$1 = 1107620064
(gdb) n
8 sum+=i;
(gdb) n
6 for(i=0; i<n; i++)
(gdb) p sum
$2 = 0
// 查看函數堆棧
(gdb) bt
#0 func (n=250) at test.c:6
#1 0x080483b2 in main () at test.c:22
#2 0x42015574 in __libc_start_main () from /lib/tls/libc.so.6
(gdb) finish <--------------------- 退出函數。
Run till exit from #0 func (n=250) at test.c:6
0x080483b2 in main () at test.c:22
22 printf("result[1-250] = %d /n", func(250) );
Value returned is $3 = 31125
(gdb) c
Continuing.
result[1-250] = 31125
Program exited with code 027.
// 退出gdb,quit
(gdb) q