实例使用gdb调试器.docx
- 文档编号:13108482
- 上传时间:2023-06-11
- 格式:DOCX
- 页数:27
- 大小:25.49KB
实例使用gdb调试器.docx
《实例使用gdb调试器.docx》由会员分享,可在线阅读,更多相关《实例使用gdb调试器.docx(27页珍藏版)》请在冰点文库上搜索。
实例使用gdb调试器
2.4实例—使用gdb调试器
1.编写实例程序gcctest.c,见2.2小节的开头部分
2.编译
[root@localhostgdbtesttxt]#gccgcctest.c-ogcctest
[root@localhostgdbtesttxt]#gcc-ggcctest.c-ogcctestg
[root@localhostgdbtesttxt]#ll
-rwxr-xr-x1rootroot518704-3018:
42gcctest
-rwxr-xr-x1rootroot689104-3018:
42gcctestg
//注意文件的大小
3.启动GDB,执行程序
启动gdb,进入gdb调试环境,可以使用gdb的命令对程序进行调试。
[root@localhostgdbtesttxt]#gdb//启动gdb
GNUgdbFedora(6.8-27.el5)
Copyright(C)2008FreeSoftwareFoundation,Inc.
LicenseGPLv3+:
GNUGPLversion3orlater //gnu.org/licenses/gpl.html> Thisisfreesoftware: youarefreetochangeandredistributeit. ThereisNOWARRANTY,totheextentpermittedbylaw.Type"showcopying" and"showwarranty"fordetails. ThisGDBwasconfiguredas"i386-redhat-linux-gnu". (gdb)rungcctest//在gdb中,运行程序使用r或是run命令,注意,gcctest没有调试信息 Startingprogram: gcctest Noexecutablefilespecified. Usethe"file"or"exec-file"command.//要使用file或exec-file命令指出要运行的程序 (gdb)filegcctest//使用file命令指出要运行的程序gcctest,注意,对gdb命令也可以使用Tab gcctestgcctest.cgcctestg (gdb)filegcctest//使用file命令指出要运行的程序gcctest Readingsymbolsfrom/root/Desktop/gdbtesttxt/gcctest...(nodebuggingsymbolsfound)...done. (gdb)r//在gdb中,运行程序使用r或是run命令 Startingprogram: /root/Desktop/gdbtesttxt/gcctestgcctest (nodebuggingsymbolsfound) (nodebuggingsymbolsfound) (nodebuggingsymbolsfound) helloinmain hello1 hello2 sum=54125560035401396161080590579269632.000000 Programexitedwithcode057. (gdb)filegcctestg//使用file命令指出要运行的程序gcctestg Readingsymbolsfrom/root/Desktop/gdbtesttxt/gcctestg...done. (gdb)r//在gdb中,运行程序使用r或是run命令 Startingprogram: /root/Desktop/gdbtesttxt/gcctestggcctest helloinmain hello1 hello2 sum=54125560035401396161080590579269632.000000 Programexitedwithcode057. (gdb)q//使用q或是quit命令退出gdb [root@localhostgdbtesttxt]# 4.GDB命令简介 (gdb)help//gdb的命令可以使用help命令来查看,gdb的命令很多,gdb将之分成许多种类。 //help命令只列出gdb的命令种类,如果要看其中的命令,可以使用help Listofclassesofcommands: aliases--Aliasesofothercommands breakpoints--Makingprogramstopatcertainpoints data--Examiningdata files--Specifyingandexaminingfiles internals--Maintenancecommands obscure--Obscurefeatures running--Runningtheprogram stack--Examiningthestack status--Statusinquiries support--Supportfacilities tracepoints--Tracingofprogramexecutionwithoutstoppingtheprogram user-defined--User-definedcommands Type"help"followedbyaclassnameforalistofcommandsinthatclass. Type"helpall"forthelistofallcommands. Type"help"followedbycommandnameforfulldocumentation. Type"aproposword"tosearchforcommandsrelatedto"word". Commandnameabbreviationsareallowedifunambiguous. (gdb)helpfiles//使用help Specifyingandexaminingfiles. Listofcommands: add-shared-symbol-files--Loadthesymbolsfromsharedobjectsinthedynamiclinker'slinkmap add-symbol-file--LoadsymbolsfromFILE add-symbol-file-from-memory--Loadthesymbolsoutofmemoryfromadynamicallyloadedobjectfile cd--SetworkingdirectorytoDIRfordebuggerandprogrambeingdebugged core-file--UseFILEascoredumpforexaminingmemoryandregisters directory--AdddirectoryDIRtobeginningofsearchpathforsourcefiles edit--Editspecifiedfileorfunction exec-file--UseFILEasprogramforgettingcontentsofpurememory file--UseFILEasprogramtobedebugged forward-search--Searchforregularexpression(seeregex(3))fromlastlinelisted generate-core-file--Saveacorefilewiththecurrentstateofthedebuggedprocess list--Listspecifiedfunctionorline load--DynamicallyloadFILEintotherunningprogram nosharedlibrary--Unloadallsharedobjectlibrarysymbols path--AdddirectoryDIR(s)tobeginningofsearchpathforobjectfiles pwd--Printworkingdirectory remote--Manipulatefilesontheremotesystem remotedelete--Deletearemotefile remoteget--Copyaremotefiletothelocalsystem remoteput--Copyalocalfiletotheremotesystem ---Type reverse-search--Searchbackwardforregularexpression(seeregex(3))fromlastlinelisted search--Searchforregularexpression(seeregex(3))fromlastlinelisted section--ChangethebaseaddressofsectionSECTIONoftheexecfiletoADDR sharedlibrary--LoadsharedobjectlibrarysymbolsforfilesmatchingREGEXP symbol-file--LoadsymboltablefromexecutablefileFILE Type"help"followedbycommandnameforfulldocumentation. Type"aproposword"tosearchforcommandsrelatedto"word". Commandnameabbreviationsareallowedifunambiguous. (gdb) 5.显示源代码 (gdb)filegcctestg//使用file命令指出要运行的程序gcctestg Loadnewsymboltablefrom"/root/Desktop/gdbtesttxt/gcctestg"? (yorn)y Readingsymbolsfrom/root/Desktop/gdbtesttxt/gcctestg...done. (gdb)list//显示当前行后面的源程序 1#include 2 3voidprint_hello1(char*p_str); 4voidprint_hello2(char*p_str); 5 6intmain(intargc,char**argv) 7{ 8doublei,sum=0; 9char*pstr="helloinmain"; 10intarr[]={1,2,3,4,5}; (gdb)list//显示当前行后面的源程序 11printf("%s\n",pstr); 12print_hello1("hello1"); 13print_hello2("hello2"); 14 15for(i=1;i<=1020000020.01*1020000020.01*10100020.01*10100020.00202;i=i*1.0000016) 16sum=sum/1.0201809902203*1.000102101203*1.00006605+i*10.01016/1.0005; 17printf("sum=%f\n",sum); 18} 19 20voidprint_hello1(char*p_str) (gdb)//敲回车键,继续执行list命令,显示当前行后面的源程序 21{ 22printf("%s\n",p_str); 23} 24 25voidprint_hello2(char*p_str) 26{ 27printf("%s\n",p_str); 28} (gdb)list8//显示程序第8行的周围的源程序 3voidprint_hello1(char*p_str); 4voidprint_hello2(char*p_str); 5 6intmain(intargc,char**argv) 7{ 8doublei,sum=0; 9char*pstr="helloinmain"; 10intarr[]={1,2,3,4,5}; 11printf("%s\n",pstr); 12print_hello1("hello1"); (gdb)list6,10//显示程序第6-10行的源程序 6intmain(intargc,char**argv) 7{ 8doublei,sum=0; 9char*pstr="helloinmain"; 10intarr[]={1,2,3,4,5}; (gdb) gdb可以显示调试程序(编译程序时一定要加上-g参数,把源程序信息编译到执行文件中)的源代码。 可以用list命令显示程序的源代码。 list命令的使用方法见表2.16。 表2.16list命令的使用方法 命令 说明 list 显示当前行后面的源代码 list 显示程序第linenum行周围的源代码,一般是显示当前行的上5行和下5行,默认共显示10行。 当然,也可以使用setlistsize命令指定显示的范围 setlistsize 设置一次显示源代码的行数 showlistsize 查看当前listsize的设置 list 显示函数名为function的函数的源代码 list<-offset> 显示当前行前面的源代码 list<+offset> 显示当前行后面的源代码 list 显示从first行到last行之间的源代码 list, 显示从当前行到last行之间的源代码 list linenum> 显示文件中的一行 list function> 显示文件中的函数 6.源代码的内存地址 (gdb)info addresscommonframeprocsourcetypes all-registerscopyingfunctionsprogramsourcesvariables argsdcachehandleregistersstackvector auxvdisplaylinescopesymbolwarranty breakpointsextensionslinkmapselectorstargetwatchpoints catchfileslocalssetterminalwin checkpointsfloatmacrosharedlibrarythreads classesforksmemsignalstracepoints (gdb)infoframe Nostack.//注意: 此时还没有运行gcctestg程序 (gdb)infoline//查看源代码在内存中的地址 Line10of"gcctest.c"startsataddress0x80483d1 andendsat0x80483f4 (gdb)infoline1 Line1of"gcctest.c"isataddress0x80483b4 (gdb)infoline2 Line2of"gcctest.c"isataddress0x80483b4 (gdb)infoline3 Line3of"gcctest.c"isataddress0x80483b4 (gdb)infoline4 Line4of"gcctest.c"isataddress0x80483b4 (gdb)infoline5 Line5of"gcctest.c"isataddress0x80483b4 (gdb)infoline6 Line6of"gcctest.c"isataddress0x80483b4 (gdb)infoline7 Line7of"gcctest.c"startsataddress0x80483b4 (gdb)infoline8 Line8of"gcctest.c"startsataddress0x80483c5 andendsat0x80483ca (gdb)infoline9 Line9of"gcctest.c"startsataddress0x80483ca andendsat0x80483d1 (gdb)infoline10 Line10of"gcctest.c"startsataddress0x80483d1 andendsat0x80483f4 (gdb)infoline11 Line11of"gcctest.c"startsataddress0x80483f4 andendsat0x80483ff (gdb)infoline12 Line12of"gcctest.c"startsataddress0x80483ff andendsat0x804840b (gdb)infoline13 Line13of"gcctest.c"startsataddress0x804840b andendsat0x8048417 (gdb)infoline14 Line14of"gcctest.c"isataddress0x8048417 (gdb)infoline15 Line15of"gcctest.c"startsataddress0x8048417 andendsat0x804841e (gdb)infoline16 Line16of"gcctest.c"startsataddress0x804841e andendsat0x8048451 (gdb)infoline17 Line17of"gcctest.c"startsataddress0x804846f andendsat0x8048482 (gdb)infoline18 Line18of"gcctest.c"startsataddress0x8048482 andendsat0x804848b (gdb)infoline19 Line19of"gcctest.c"isataddress0x804848b (gdb)infoline20 Line20of"gcctest.c"isataddress0x804848b (gdb)infoline21 Line21of"gcctest.c"startsataddress0x804848b andendsat0x8048491 (gdb)infoline22 Line22of"gcctest.c"startsataddress0x8048491 andendsat0x804849c (gdb)infoline23 Line23of"gcctest.c"startsataddress0x804849c andendsat0x804849e (gdb)infoline24 Line24
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实例 使用 gdb 调试