GDB調試

GDB調試

一個調試示例

源程序:tst.c

代碼:
1 #include
2
3 int func(int n)
4 {
5 int sum=0,i;
6 for(i=0; 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  
", result );
24 printf("result[1-250] = %d  
", func(250) );
25 }  


編譯生成執行文件:(Linux下)
hchen/test> cc -g tst.c -o tst

使用GDB調試:

代碼:
hchen/test> gdb tst <---------- 啓動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命令相當於list,從第一行開始例出原碼。
1 #include
2
3 int func(int n)
4 {
5 int sum=0,i;
6 for(i=0; i
7 {
8 sum+=i;
9 }
10 return sum;
(gdb) <-------------------- 直接回車表示,重複上一次命令
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 <-------------------- 設置斷點,在源程序第16行處。
Breakpoint 1 at 0x8048496: file tst.c, line 16.
(gdb) break func <-------------------- 設置斷點,在函數func()入口處。
Breakpoint 2 at 0x8048456: file tst.c, line 5.
(gdb) info break <-------------------- 查看斷點信息。
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 <--------------------- 運行程序,run命令簡寫
Starting program: /home/hchen/test/tst

Breakpoint 1, main () at tst.c:17 <---------- 在斷點處停住。
17 long result = 0;
(gdb) n <--------------------- 單條語句執行,next命令簡寫。
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 <--------------------- 繼續運行程序,continue命令簡寫。
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命令簡寫。
$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 <--------------------- 查看函數堆棧。
#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 <--------------------- 退出函數。
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  
", func(250) );
Value returned is $6 = 31375
(gdb) c <--------------------- 繼續運行。
Continuing.
result[1-250] = 31375 <----------程序輸出。

Program exited with code 027. <--------程序退出,調試結束。
(gdb) q <--------------------- 退出gdb。
hchen/test>  


好了,有了以上的感性認識,還是讓我們來系統地認識一下gdb吧。

使用GDB

  一般來說GDB主要調試的是C/C++的程序。要調試C/C++的程序,首先在編譯時,我們必須要把調試信息加到可執行文件中。使用編譯器(cc/gcc/g++)的 -g 參數可以做到這一點。如:
> cc -g hello.c -o hello
> g++ -g hello.cpp -o hello
如果沒有-g,你將看不見程序的函數名、變量名,所代替的全是運行時的內存地址。當你用-g把調試信息加入之後,併成功編譯目標代碼以後,讓我們來看看如何用gdb來調試他。

啓動GDB的方法有以下幾種:
1、gdb
program也就是你的執行文件,一般在當然目錄下。
2、gdb core
用gdb同時調試一個運行程序和core文件,core是程序非法執行後core dump後產生的文件。
3、gdb
如果你的程序是一個服務程序,那麼你可以指定這個服務程序運行時的進程ID。gdb會自動attach上去,並調試他。program應該在PATH環境變量中搜索得到。
  GDB啓動時,可以加上一些GDB的啓動開關,詳細的開關可以用gdb -help查看。我在下面只例舉一些比較常用的參數:
-symbols
-s
從指定文件中讀取符號表。

-se file
從指定文件中讀取符號表信息,並把他用在可執行文件中。

-core
-c
調試時core dump的core文件。

-directory
-d
加入一個源文件的搜索路徑。默認搜索路徑是環境變量中PATH所定義的路徑。
GDB的命令概貌
  啓動gdb後,就你被帶入gdb的調試環境中,就可以使用gdb的命令開始調試程序了,gdb的命令可以使用help命令來查看,如下所示:
/home/hchen> 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) help
List of classes of commands:

aliases -- Aliases of other commands
breakpoints -- Making program stop at certain points
data -- Examining data
files -- Specifying and examining files
internals -- Maintenance commands
obscure -- Obscure features
running -- Running the program
stack -- Examining the stack
status -- Status inquiries
support -- Support facilities
tracepoints -- Tracing of program execution without stopping the program
user-defined -- User-defined commands

Type "help" followed by a class name for a list of commands in that class.
Type "help" followed by command name for full documentation.
Command name abbreviations are allowed if unambiguous.
(gdb)
  gdb的命令很多,gdb把之分成許多個種類。help命令只是例出gdb的命令種類,如果要看種類中的命令,可以使用help 命令,如:help breakpoints,查看設置斷點的所有命令。也可以直接help 來查看命令的幫助。
  gdb中,輸入命令時,可以不用打全命令,只用打命令的前幾個字符就可以了,當然,命令的前幾個字符應該要標誌着一個唯一的命令,在Linux下,你可以敲擊兩次TAB鍵來補齊命令的全稱,如果有重複的,那麼gdb會把其例出來。

示例一:在進入函數func時,設置一個斷點。可以敲入break func,或是直接就是b func
(gdb) b func
Breakpoint 1 at 0x8048458: file hello.c, line 10.
示例二:敲入b按兩次TAB鍵,你會看到所有b打頭的命令:
(gdb) b
backtrace break bt
(gdb)
示例三:只記得函數的前綴,可以這樣:
(gdb) b make_ <按TAB鍵>
(再按下一次TAB鍵,你會看到:)
make_a_section_from_file make_environ
make_abs_section make_function_type
make_blockvector make_pointer_type
make_cleanup make_reference_type
make_command make_symbol_completion_list
(gdb) b make_
GDB把所有make開頭的函數全部例出來給你查看。
示例四:調試C++的程序時,有可以函數名一樣。如:
(gdb) b 'bubble( M-?
bubble(double,double) bubble(int,int)
(gdb) b 'bubble(
你可以查看到C++中的所有的重載函數及參數。(注:M-?和“按兩次TAB鍵”是一個意思)

要退出gdb時,只用發quit或命令簡稱q就行了。

總結

.c文件
tst.c

> cc -g tst.c -o tst
> gdb tst <---------- 啓動GDB
(gdb) l <-------------------- l命令相當於list,從第一行開始例出原碼。
(gdb) <-------------------- 直接回車表示,重複上一次命令
(gdb) break 16 <-------------------- 設置斷點,在源程序第16行處。
(gdb) break func <-------------------- 設置斷點,在函數func()入口處。
(gdb) info break <-------------------- 查看斷點信息。
(gdb) r <--------------------- 運行程序,run命令簡寫
(gdb) n <--------------------- 單條語句執行,next命令簡寫。
(gdb) c <--------------------- 繼續運行程序,continue命令簡寫。
(gdb) p i <--------------------- 打印變量i的值,print命令簡寫。
(gdb) bt <--------------------- 查看函數堆棧。
(gdb) finish <--------------------- 退出函數。
(gdb) q <--------------------- 退出gdb。

發佈了79 篇原創文章 · 獲贊 17 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章