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 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) <-------------------- 直接回車表示,重復上一次命令 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 /n", func(250) ); Value returned is $6 = 31375 (gdb) c <--------------------- 繼續運行。 Continuing. result[1-250] = 31375 <----------程序輸出。
Program exited with code 027. <--------程序退出,調試結束。 (gdb) q <--------------------- 退出gdb。 hchen/test>
/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命令是make: make <make-args> 可以在gdb中執行make命令來重新build自己的程序。這個命令等價于“shell make <make-args>”。 在GDB中運行程序 當以gdb <program>方式啟動gdb后,gdb會在PATH路徑和當前目錄中搜索<program>的源文件。如要確認gdb是否讀到源文件,可使用l或list命令,看看gdb是否能列出源代碼。
在gdb中,運行程序使用r或是run命令。程序的運行,你有可能需要設置下面四方面的事。
1、程序運行參數。 set args 可指定運行時參數。(如:set args 10 20 30 40 50) show args 命令可以查看設置好的運行參數。
2、運行環境。 path <dir> 可設定程序的運行路徑。 show paths 查看程序的運行路徑。 set environment varname [=value] 設置環境變量。如:set env USER=hchen show environment [varname] 查看環境變量。
3、工作目錄。 cd <dir> 相當于shell的cd命令。 pwd 顯示當前的所在目錄。
4、程序的輸入輸出。 info terminal 顯示你程序用到的終端的模式。 使用重定向控制程序輸出。如:run > outfile tty命令可以指寫輸入輸出的終端設備。如:tty /dev/ttyb
break ... if <condition> ...可以是上述的參數,condition表示條件,在條件成立時停住。比如在循環境體中,可以設置break if i=100,表示當i為100時停住程序。
查看斷點時,可使用info命令,如下所示:(注:n表示斷點號) info breakpoints [n] info break [n] 二、設置觀察點(WatchPoint) 觀察點一般來觀察某個表達式(變量也是一種表達式)的值是否有變化了,假如有變化,馬上停住程序。我們有下面的幾種方法來設置觀察點:
(gdb) b String::after [0] cancel [1] all [2] file:String.cc; line number:867 [3] file:String.cc; line number:860 [4] file:String.cc; line number:875 [5] file:String.cc; line number:853 [6] file:String.cc; line number:846 [7] file:String.cc; line number:735 > 2 4 6 Breakpoint 1 at 0xb26c: file String.cc, line 867. Breakpoint 2 at 0xb344: file String.cc, line 875. Breakpoint 3 at 0xafcc: file String.cc, line 846. Multiple breakpoints were set. Use the "delete" command to delete unwanted breakpoints. (gdb)
(gdb) bt #0 func (n=250) at tst.c:6 #1 0x08048524 in main (argc=1, argv=0xbffff674) at tst.c:30 #2 0x400409ed in __libc_start_main () from /lib/libc.so.6
frame <n> f <n> n是一個從0開始的整數,是棧中的層編號。比如:frame 0,表示棧頂,frame 1,表示棧的第二層。
up <n> 表示向棧的上面移動n層,可以不打n,表示向上移動一層。
down <n> 表示向棧的下面移動n層,可以不打n,表示向下移動一層。
上面的命令,都會打印出移動到的棧層的信息。假如你不想讓其打出信息。你可以使用這三個命令:
select-frame <n> 對應于 frame 命令。 up-silently <n> 對應于 up 命令。 down-silently <n> 對應于 down 命令。
查看當前棧層的信息,你可以用以下GDB命令:
frame 或 f 會打印出這些信息:棧的層編號,當前的函數名,函數參數值,函數所在文件及行號,函數執行到的語句。
info frame info f 這個命令會打印出更為具體的當前棧層的信息,只不過,大多數都是運行時的內內地址。比如:函數地址,調用函數的地址,被調用函數的地址,目前的函數是由什么樣的程序語言寫成的、函數參數地址及值、局部變量的地址等等。如: (gdb) info f Stack level 0, frame at 0xbffff5d4: eip = 0x804845d in func (tst.c:6); saved eip 0x8048524 called by frame at 0xbffff60c source language c. Arglist at 0xbffff5d4, args: n=250 Locals at 0xbffff5d4, Previous frame's sp is 0x0 Saved registers: ebp at 0xbffff5d4, eip at 0xbffff5d8
info args 打印出當前函數的參數名及其值。
info locals 打印出當前函數中所有局部變量及其值。
info catch 打印出當前的函數中的異常處理信息。 查看源程序 一、顯示源代碼 GDB 可以打印出所調試程序的源代碼,當然,在程序編譯時一定要加上-g的參數,把源程序信息編譯到執行文件中。不然就看不到源程序了。當程序停下來以后,GDB會報告程序停在了那個文件的第幾行上。你可以用list命令來打印程序的源代碼。還是來看一看查看源代碼的GDB命令吧。
x 按十六進制格式顯示變量。 d 按十進制格式顯示變量。 u 按十六進制格式顯示無符號整型。 o 按八進制格式顯示變量。 t 按二進制格式顯示變量。 a 按十六進制格式顯示變量。 c 按字符格式顯示變量。 f 按浮點數格式顯示變量。
(gdb) p i $21 = 101
(gdb) p/a i $22 = 0x65
(gdb) p/c i $23 = 101 'e'
(gdb) p/f i $24 = 1.41531145e-43
(gdb) p/x i $25 = 0x65
(gdb) p/t i $26 = 1100101 五、查看內存 你可以使用examine命令(簡寫是x)來查看內存地址中的值。x命令的語法如下所示:
x/<n/f/u> <addr>
n、f、u是可選的參數。
n 是一個正整數,表示顯示內存的長度,也就是說從當前地址向后顯示幾個地址的內容。 f 表示顯示的格式,參見上面。假如地址所指的是字符串,那么格式可以是s,假如地十是指令地址,那么格式可以是i。 u 表示從當前地址往后請求的字節數,假如不指定的話,GDB默認是4個bytes。u參數可以用下面的字符來代替,b表示單字節,h表示雙字節,w表示四字節,g表示八字節。當我們指定了字節長度后,GDB會從指內存定的內存地址開始,讀寫指定字節,并把其當作一個值取出來。
(gdb) set language The currently understood settings are:
local or auto Automatic setting based on source file c Use the C language c++ Use the C++ language asm Use the Asm language chill Use the Chill language fortran Use the Fortran language java Use the Java language modula-2 Use the Modula-2 language pascal Use the Pascal language scheme Use the Scheme language