GDB

um install gdb    安裝gdb調試工具

yum install gcc    安裝gcc編譯工具

man gdb 查看gdb的幫助文檔。也能夠網上搜索使用方法。

功能:

 通常來講,GDB主要幫助你完成下面四個方面的功能:
  一、啓動你的程序,能夠按照你的自定義的要求爲所欲爲的運行程序。
  二、可以讓被調試的程序在你所指定的調置的斷點處停住。(斷點能夠是條件表達式)
  三、當程序被停住時,能夠檢查此時你的程序中所發生的事。
  四、動態的改變你程序的執行環境。
linux

<strong>GDB概述
————
GDB是GNU開源組織發佈的一個強大的UNIX下的程序調試工具。或許,各位比較喜歡
那種圖形界面方式的,像VC、BCB等IDE的調試,但若是你是在UNIX平臺下作軟件,你
會發現GDB這個調試工具備比VC、BCB的圖形化調試器更強大的功能。所謂「寸有所
長,尺有所短」就是這個道理。
通常來講,GDB主要幫忙你完成下面四個方面的功能:
一、啓動你的程序,能夠按照你的自定義的要求爲所欲爲的運行程序。
二、可以讓被調試的程序在你所指定的調置的斷點處停住。(斷點能夠是條件表達式)
三、當程序被停住時,能夠檢查此時你的程序中所發生的事。
四、動態的改變你程序的執行環境。
從上面看來,GDB和通常的調試工具沒有什麼兩樣,基本上也是完成這些功能,不過
在細節上,你會發現GDB這個調試工具的強大,你們可能比較習慣了圖形化的調試工
具,但有時候,命令行的調試工具卻有着圖形化工具所不能完成的功能。讓咱們一
一看來。
一個調試示例</strong>
——————
源程序:tst.c
1 #include <stdio.h>
2
int  func( int  n)
4 {
int  sum=0,i;
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 }
編譯生成執行文件:(Linux下)
hchen/test> gcc -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 <stdio.h>
2
int  func( int  n)
4 {
int  sum=0,i;
for (i=0; i<=n; 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
int  sum=0,i;
(gdb) n
for (i=1; i<=n; i++)
(gdb) p i <--------------------- 打印變量i的值,print命令簡寫。
= 134513808
(gdb) n
8 sum+=i;
(gdb) n
for (i=1; i<=n; i++)
(gdb) p sum
= 1
(gdb) n
8 sum+=i;
(gdb) p i
= 2
(gdb) n
for (i=1; i<=n; i++)
(gdb) p sum
= 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 \n" , func(250) );
Value returned is = 31375
(gdb) c <--------------------- 繼續運行。
Continuing.
result[1-250] = 31375 <----------程序輸出。
Program exited with code 027. <--------程序退出,調試結束。
(gdb) q <--------------------- 退出gdb。
hchen/test>
相關文章
相關標籤/搜索