linux——gdb調(diào)試技巧_第1頁
linux——gdb調(diào)試技巧_第2頁
linux——gdb調(diào)試技巧_第3頁
linux——gdb調(diào)試技巧_第4頁
linux——gdb調(diào)試技巧_第5頁
已閱讀5頁,還剩6頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、用GDB調(diào)試程序GDB概述GDB是GNU開源組織發(fā)布的一個強(qiáng)大的UNIX 下的程序調(diào)試工具?;蛟S,各位比較喜歡那種圖形界面方式的,像VC、 BCB等IDE的調(diào)試,但如果你是在UNIX平臺下做軟件,你會發(fā)現(xiàn)GDB這個調(diào)試工具有比 VC、BCB的圖形化調(diào)試器更強(qiáng)大的功能。所謂“寸有所長,尺有所短”就是這個道理。一般來說,GDB主要幫忙你完成下面四個方面的功能:1、啟動你的程序,可以按照你的自定義的要求隨心所欲的運(yùn)行程序。2、可讓被調(diào)試的程序在你所指定的調(diào)置的斷點處停住。(斷點可以是條件表達(dá)式)3、當(dāng)程序被停住時,可以檢查此時你的程序中所發(fā)生的事。4、動態(tài)的改變你程序的執(zhí)行環(huán)境。從上面看來, GDB

2、和一般的調(diào)試工具沒有什么兩樣,基本上也是完成這些功能,不過在細(xì)節(jié) 上,你會發(fā)現(xiàn) GDB這個調(diào)試工具的強(qiáng)大,大家可能比較習(xí)慣了圖形化的調(diào)試工具,但有時候, 命令行的調(diào)試工具卻有著圖形化工具所不能完成的功能。讓我們一一 一看來。一個調(diào)試示例源程序:tst.c1 #in elude <stdio.h>22 int fun c(i nt n)3 4 intsum=0,i;5 for(i=0;i< n; i+)6 7 sum+=i;8 9 return sum;10 121314 mai n()15 16 int i;17 long result = 0;181920212223242

3、5 for(i=1; i<=100; i+)result += i;printf("result1-100 = %d n", result);prin tf("result1-250 = %d n", fun c(250);編譯生成執(zhí)行文件:(Linux下)hche n/test> cc -g tst.c -o tst使用GDB調(diào)試:hchen/test> gdb tst < 啟動GDBGNU gdb 5.1.1Copyright 2002 Free Software Fou ndati on, Inc.GDB is free s

4、oftware, covered by the GNU Gen eral Public Lice nse, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copy ing" to see the con diti ons.There is absolutely no warranty for GDB. Type "show warranty" for details.This GDB was con

5、figured as "i386-suse-l in ux".(gdb) l <l命令相當(dāng)于list,從第一行開始例岀原碼1 #i nclude <stdio.h>22 int fun c(i nt n)3 4 intsum=0,i;5 for(i=0;i< n; i+)6 7 sum+=i;8 9 return sum;(gdb) < 直接回車表示,重復(fù)上一次命令10 121314 mai n()15 16 int i;17 longresult = 0;18 for(i=1;i<=100; i+)19 20result += i;(g

6、db) break 16< 設(shè)置斷點,在源程序第16行處。Breakpoint 1 at 0x8048496: file tst.c, line 16.(gdb) break func < 設(shè)置斷點,在函數(shù) func() 入口處。Breakpoint 2 at 0x8048456: file tst.c, line 5.(gdb) info break < 查看斷點信息。Num TypeDisp Enb Address What1 breakpoi ntkeep y0x08048496 in mai n at tst.c:162 breakpoi ntkeep y0x0804

7、8456 in func at tst.c:5(gdb) r< 運(yùn)行程序,run命令簡寫Starting program: /home/hchen/test/tstBreakpoi nt 1, ma in () at tst.c:17< 在斷點處停住。17long result = 0;(gdb) n<單條語句執(zhí)行,next命令簡寫。18for(i=1; i<=100; i+)(gdb) n20result += i;(gdb) n18for(i=1; i<=100; i+)(gdb) n20result += i;(gdb) c<繼續(xù)運(yùn)行程序,contin

8、 ue命令簡寫Con ti nuing.result1-100=5050<-程序輸岀。Breakpoint 2, func (n=250) at tst.c:55 int sum=0,i;(gdb) n6 for(i=1; i<=n; i+)(gdb) p i < 打印變量i的值,print命令簡寫$1 =134513808(gdb) n8sum+=i;(gdb) n6for(i=1; i<=n; i+)(gdb) p sum$2 = 1(gdb) n8sum+=i;(gdb) p i$3 = 2(gdb) n6for(i=1; i<=n; i+)(gdb) p

9、sum$4 = 3(gdb) bt < 查看函數(shù)堆棧。#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) fin ish< 退岀函數(shù)。Run till exit from #0 fun c (n=250) at tst.c:50x080484e4 in main () at tst.c:2424prin tf("result1-250 = %d n", fun c(25

10、0);Value returned is $6 = 31375(gdb) c < 繼續(xù)運(yùn)行。Con ti nuing.result1-250 = 31375< 程序輸岀。Program exited with code 027. < 程序退岀,調(diào)試結(jié)束。(gdb) q < 退岀 gdb。hche n/test>好了,有了以上的感性認(rèn)識,還是讓我們來系統(tǒng)地認(rèn)識一下gdb吧使用GDB一般來說GDB主要調(diào)試的是 C/C+的程序。要調(diào)試C/C+的程序,首先在編譯時,我們必 須要把調(diào)試信息加到可執(zhí)行文件中。使用編譯器(cc/gcc/g+ )的-g參數(shù)可以做到這一點。如:&g

11、t; cc -g hello.c -o hello> g+ -g hello.cpp -o hello如果沒有-g,你將看不見程序的函數(shù)名、變量名,所代替的全是運(yùn)行時的內(nèi)存地址。當(dāng)你用-g把調(diào)試信息加入之后,并成功編譯目標(biāo)代碼以后,讓我們來看看如何用gdb來調(diào)試他。啟動GDB的方法有以下幾種:1、gdb <program>program 也就是你的執(zhí)行文件,一般在當(dāng)然目錄下。2、gdb <program> core用gdb同時調(diào)試一個運(yùn)行程序和core文件,core是程序非法執(zhí)行后core dump 后產(chǎn)生的文件。3、gdb <program> <

12、;PID>如果你的程序是一個服務(wù)程序,那么你可以指定這個服務(wù)程序運(yùn)行時的進(jìn)程ID。gdb會自動attach上去,并調(diào)試他。program 應(yīng)該在 PATH 環(huán)境變量中搜索得到。GDB啟動時,可以加上一些GDB的啟動開關(guān),詳細(xì)的開關(guān)可以用gdb -help 查看。我在下面只例舉一些比較常用的參數(shù):-symbols <file>-s <file>從指定文件中讀取符號表。-se file從指定文件中讀取符號表信息,并把他用在可執(zhí)行文件中。-core <file>-c <file>調(diào)試時 core dump 的core 文件。-directory

13、<directory>-d <directory>加入一個源文件的搜索路徑。默認(rèn)搜索路徑是環(huán)境變量中PATH所定義的路徑。GDB的命令概貌啟動gdb后,就你被帶入 gdb的調(diào)試環(huán)境中,就可以使用 gdb的命令開始調(diào)試程序了,gdb的命令可以使用 help命令來查看,如下所示:/home/hche n> gdbGNU gdb 5.1.1Copyright 2002 Free Software Fou ndati on, Inc.GDB is free software, covered by the GNU Gen eral Public Lice nse, and

14、 you arewelcome to change it and/or distribute copies of it under certain conditions.Type "show copy ing" to see the con diti ons.There is absolutely no warranty for GDB. Type "show warranty" for details.This GDB was con figured as "i386-suse-l in ux".(gdb) helpList of

15、classes of comma nds:aliases - Aliases of other comma ndsbreakpo ints - Mak ing program stop at certa in pointsdata - Exami ning datafiles - Specifying and examining filesintern als - Maintenance comma ndsobscure - Obscure featuresrunning - Running the programstack - Exami ning the stack status - St

16、atus in quiries support - Support facilities tracepo ints - Tracing of program executi on without stopp ing the program user-defi ned - User-defi ned comma ndsType "help" followed by a class n ame for a list of comma nds in that class.Type "help" followed by comma nd n ame for fu

17、ll docume ntati on.Comma nd n ame abbreviati ons are allowed if un ambiguous.(gdb)gdb的命令很多, gdb把之分成許多個種類。help命令只是例岀 gdb看種類中的命令,可以使用help <class> 命令,如: help breakpoi nts ,命令。也可以直接 help <comma nd> 來查看命令的幫助。的命令種類,如果要 查看設(shè)置斷點的所有g(shù)db中,輸入命令時,可以不用打全命令,只用打命令的前幾個字符就可以了,當(dāng) 前幾個字符應(yīng)該要標(biāo)志著一個唯一的命令,在Linux下,你

18、可以敲擊兩次的全稱,如果有重復(fù)的,那么 gdb會把其例岀來。,命令的TAB鍵來補(bǔ)齊命令示例一:在進(jìn)入函數(shù)func時,設(shè)置一個斷點??梢郧萌?break func(gdb) b funcBreakpoint 1 at 0x8048458: file hello.c, line 10.示例二:敲入 b按兩次TAB鍵,你會看到所有 b打頭的命令:(gdb) bbacktrace break bt或是直接就是 b func(gdb)示例三:只記得函數(shù)的前綴,可以這樣:(gdb) b make_ < 按 TAB 鍵 >(再按下一次 TAB鍵,你會看到:)make_a_secti on _fr

19、om_file make_abs_sectio n make_blockvector make_clea nup make_comma nd (gdb) b make_make_e nvir onmake_fu nctio n_typemake_po in ter_typemake_refere nce_typemake_symbol_completio n_listGDB把所有 make開頭的函數(shù)全部例岀來給你查看。示例四:調(diào)試 C+的程序時,有可以函數(shù)名一樣。如:(gdb) b 'bubble( M-?bubble(double,double) bubble(i nt, int)(

20、gdb) b 'bubble(你可以查看到C+中的所有的重載函數(shù)及參數(shù)。(注:M-?和 “按兩次TAB鍵”是個意思)要退岀gdb時,只用發(fā) quit或命令簡稱 q就行了。GDB中運(yùn)行 UNIX的shell程序在gdb環(huán)境中,你可以執(zhí)行UNIX的shell的命令,使用 gdb的shell命令來完成:shell vcomma nd stri ng>調(diào)用 UNIX 的shell來執(zhí)行 vcommand string>,環(huán)境變量 SHELL 中定義的 UNIX 的shell將會被用來執(zhí)行vcommand string>,如果SHELL沒有定義,那就使用UNIX的標(biāo)準(zhǔn)shell

21、 : /bin/sh 。(在 Windows 中使用 C或 cmd.exe )還有一個 gdb 命令是 make :make <make-args>可以在gdb中執(zhí)行make命令來重新 build自己的程序。這個命令等價于"shell makevmake- args> ”。在GDB中運(yùn)行程序當(dāng)以gdb <program> 方式啟動 gdb后,gdb會在PATH路徑和當(dāng)前目錄中搜索<program> 的源文件。如要確認(rèn)gdb是否讀到源文件,可使用 l或list命令,看看 gdb是否能列岀源代碼。在gdb中,運(yùn)行程序使用r或是run命令。程序的運(yùn)行,你有可能需要設(shè)置下面四方面的事。1、程序運(yùn)行參數(shù)。set args 可指定運(yùn)行時參數(shù)。(如: set args 10 20 30 40 50)show args命令可以查看設(shè)置好的運(yùn)行參數(shù)。2、運(yùn)行環(huán)境。path <dir>可設(shè)定程序的運(yùn)行路徑。show paths 查看

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論