国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

GDB程序調(diào)試從初級(jí)到高級(jí)(一)

2019-11-09 15:29:16
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

轉(zhuǎn)自:http://blog.csdn.net/haoel/article/details/2879

用GDB調(diào)試程序 GDB概述 ———— GDB是GNU開源組織發(fā)布的一個(gè)強(qiáng)大的UNIX下的程序調(diào)試工具。或許,各位比較喜歡那種圖形界面方式的,像VC、BCB等IDE的調(diào)試,但如果你是在UNIX平臺(tái)下做軟件,你會(huì)發(fā)現(xiàn)GDB這個(gè)調(diào)試工具有比VC、BCB的圖形化調(diào)試器更強(qiáng)大的功能。所謂“寸有所長(zhǎng),尺有所短”就是這個(gè)道理。 一般來(lái)說(shuō),GDB主要幫忙你完成下面四個(gè)方面的功能: 1、啟動(dòng)你的程序,可以按照你的自定義的要求隨心所欲的運(yùn)行程序。 2、可讓被調(diào)試的程序在你所指定的調(diào)置的斷點(diǎn)處停住。(斷點(diǎn)可以是條件表達(dá)式) 3、當(dāng)程序被停住時(shí),可以檢查此時(shí)你的程序中所發(fā)生的事。 4、動(dòng)態(tài)的改變你程序的執(zhí)行環(huán)境。 從上面看來(lái),GDB和一般的調(diào)試工具沒(méi)有什么兩樣,基本上也是完成這些功能,不過(guò)在細(xì)節(jié)上,你會(huì)發(fā)現(xiàn)GDB這個(gè)調(diào)試工具的強(qiáng)大,大家可能比較習(xí)慣了圖形化的調(diào)試工具,但有時(shí)候,命令行的調(diào)試工具卻有著圖形化工具所不能完成的功能。讓我們一一看來(lái)。 一個(gè)調(diào)試示例 源程序:test.c

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; 11 } 14 main() 15 { 16 int i; 17 long result = 0; 18 for(i=1; i<=100; i++) 19 { 20 result += i; 21 } 22 23 編譯生成執(zhí)行文件:(linux下) hchen/test> cc -g tst.c -o tst 使用GDB調(diào)試:

hchen/test> gdb tst <---------- 啟動(dòng)GDBGNU gdb 5.1.1Copyright 2002 Free Software Foundation, Inc.GDB is free software, covered by the GNU General Public License, and you arewelcome 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命令相當(dāng)于list,從第一行開始例出原碼。1 #include <stdio.h>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) <-------------------- 直接回車表示,重復(fù)上一次命令11 }14 main()15 {16 int i;17 long result = 0;18 for(i=1; i<=100; i++)19 {20 result += i; (gdb) break 16 <-------------------- 設(shè)置斷點(diǎn),在源程序第16行處。Breakpoint 1 at 0x8048496: file tst.c, line 16.(gdb) break func <-------------------- 設(shè)置斷點(diǎn),在函數(shù)func()入口處。Breakpoint 2 at 0x8048456: file tst.c, line 5.(gdb) info break <-------------------- 查看斷點(diǎn)信息。Num Type Disp Enb Address What1 breakpoint keep y 0x08048496 in main at tst.c:162 breakpoint keep y 0x08048456 in func at tst.c:5(gdb) r <--------------------- 運(yùn)行程序,run命令簡(jiǎn)寫Starting program: /home/hchen/test/tstBreakpoint 1, main () at tst.c:17 <---------- 在斷點(diǎn)處停住。17 long result = 0;(gdb) n <--------------------- 單條語(yǔ)句執(zhí)行,next命令簡(jiǎn)寫。18 for(i=1; i<=100; i++)(gdb) n20 result += i;(gdb) n18 for(i=1; i<=100; i++)(gdb) n20 result += i;(gdb) c <--------------------- 繼續(xù)運(yùn)行程序,continue命令簡(jiǎn)寫。Continuing.result[1-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命令簡(jiǎn)寫。$1 = 134513808(gdb) n8 sum+=i;(gdb) n6 for(i=1; i<=n; i++)(gdb) p sum$2 = 1(gdb) n8 sum+=i;(gdb) p i$3 = 2(gdb) n6 for(i=1; i<=n; i++)(gdb) p 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) finish <--------------------- 退出函數(shù)。Run till exit from #0 func (n=250) at tst.c:50x080484e4 in main () at tst.c:2424 printf("result[1-250] = %d /n", func(250) );Value returned is $6 = 31375(gdb) c <--------------------- 繼續(xù)運(yùn)行。Continuing.result[1-250] = 31375 <----------程序輸出。Program exited with code 027. <--------程序退出,調(diào)試結(jié)束。(gdb) q <--------------------- 退出gdb。hchen/test>

好了,有了以上的感性認(rèn)識(shí),還是讓我們來(lái)系統(tǒng)地認(rèn)識(shí)一下gdb吧。 使用GDB ————

一般來(lái)說(shuō)GDB主要調(diào)試的是C/C++的程序。要調(diào)試C/C++的程序,首先在編譯時(shí),我們必須要把調(diào)試信息加到可執(zhí)行文件中。使用編譯器(cc/gcc/g++)的 -g 參數(shù)可以做到這一點(diǎn)。如:

> cc -g hello.c -o hello> g++ -g hello.cpp -o hello

如果沒(méi)有-g,你將看不見(jiàn)程序的函數(shù)名、變量名,所代替的全是運(yùn)行時(shí)的內(nèi)存地址。當(dāng)你用-g把調(diào)試信息加入之后,并成功編譯目標(biāo)代碼以后,讓我們來(lái)看看如何用gdb來(lái)調(diào)試他。

啟動(dòng)GDB的方法有以下幾種:

1、gdb <program> program也就是你的執(zhí)行文件,一般在當(dāng)然目錄下。2、gdb <program> core 用gdb同時(shí)調(diào)試一個(gè)運(yùn)行程序和core文件,core是程序非法執(zhí)行后core dump后產(chǎn)生的文件。3、gdb <program> <PID> 如果你的程序是一個(gè)服務(wù)程序,那么你可以指定這個(gè)服務(wù)程序運(yùn)行時(shí)的進(jìn)程ID。gdb會(huì)自動(dòng)attach上去,并調(diào)試他。program應(yīng)該在PATH環(huán)境變量中搜索得到。

GDB啟動(dòng)時(shí),可以加上一些GDB的啟動(dòng)開關(guān),詳細(xì)的開關(guān)可以用gdb -help查看。我在下面只例舉一些比較常用的參數(shù):

-symbols <file> -s <file> 從指定文件中讀取符號(hào)表。-se file 從指定文件中讀取符號(hào)表信息,并把他用在可執(zhí)行文件中。-core <file>-c <file> 調(diào)試時(shí)core dump的core文件。-directory <directory>-d <directory>加入一個(gè)源文件的搜索路徑。默認(rèn)搜索路徑是環(huán)境變量中PATH所定義的路徑。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 金昌市| 亳州市| 内丘县| 上蔡县| 宜章县| 古交市| 吉安市| 长白| 天门市| 望谟县| 泰宁县| 双柏县| 瑞金市| 富阳市| 石楼县| 普兰县| 桃江县| 和田县| 宁波市| 土默特右旗| 潼南县| 保定市| 迁安市| 偏关县| 锡林郭勒盟| 房山区| 屏山县| 佛山市| 讷河市| 丹寨县| 普宁市| 菏泽市| 淮阳县| 鄢陵县| 桐梓县| 金平| 邯郸市| 商洛市| 黔西县| 岑溪市| 甘孜县|