linux-GDB调试入门到精通

上传人:汽*** 文档编号:487610368 上传时间:2022-08-04 格式:DOC 页数:38 大小:127KB
返回 下载 相关 举报
linux-GDB调试入门到精通_第1页
第1页 / 共38页
linux-GDB调试入门到精通_第2页
第2页 / 共38页
linux-GDB调试入门到精通_第3页
第3页 / 共38页
linux-GDB调试入门到精通_第4页
第4页 / 共38页
linux-GDB调试入门到精通_第5页
第5页 / 共38页
点击查看更多>>
资源描述

《linux-GDB调试入门到精通》由会员分享,可在线阅读,更多相关《linux-GDB调试入门到精通(38页珍藏版)》请在金锄头文库上搜索。

1、topGDB概述GDB 是GNU开源组织发布的一个强大的UNIX下的程序调试工具。或许,各位比较喜欢那种图形界面方式的,像VC、BCB等IDE的调试,但如果你是在 UNIX平台下做软件,你会发现GDB这个调试工具有比VC、BCB的图形化调试器更强大的功能。所谓“寸有所长,尺有所短”就是这个道理。一般来说,GDB主要帮忙你完成下面四个方面的功能:1、启动你的程序,可以按照你的自定义的要求随心所欲的运行程序。2、可让被调试的程序在你所指定的调置的断点处停住。(断点可以是条件表达式)3、当程序被停住时,可以检查此时你的程序中所发生的事。4、动态的改变你程序的执行环境。从上面看来,GDB和一般的调试工

2、具没有什么两样,基本上也是完成这些功能,不过在细节上,你会发现GDB这个调试工具的强大,大家可能比较习惯了图形化的调试工具,但有时候,命令行的调试工具却有着图形化工具所不能完成的功能。让我们一一看来。top一个调试示例源程序:tst.c1 #include 23 int func(int n)4 5 int sum=0,i;6 for(i=0; in; i+)7 8 sum+=i;9 10 return sum;11 121314 main()15 16 int i;17 long result = 0;18 for(i=1; i cc -g tst.c -o tst使用GDB调试:hchen

3、/test gdb tst - 启动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 warra

4、nty for GDB. Type show warranty for details.This GDB was configured as i386-suse-linux.(gdb) l - l命令相当于list,从第一行开始例出原码。1 #include 23 int func(int n)4 5 int sum=0,i;6 for(i=0; in; i+)7 8 sum+=i;9 10 return sum;(gdb) - 直接回车表示,重复上一次命令11 121314 main()15 16 int i;17 long result = 0;18 for(i=1; i=100; i+)

5、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 What1 breakpoint keep y 0x08048496 in main at tst.c:162 breakpoint k

6、eep y 0x08048456 in func at tst.c:5(gdb) r - 运行程序,run命令简写Starting program: /home/hchen/test/tstBreakpoint 1, main () at tst.c:17 - 在断点处停住。17 long result = 0;(gdb) n - 单条语句执行,next命令简写。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 - 继续运行程序,conti

7、nue命令简写。Continuing.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) 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(g

8、db) 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:50x080484e4 in main () at tst.c:2424 printf(result1-250 = %d n, func(250) );Value returned is $6 = 313

9、75(gdb) c - 继续运行。Continuing.result1-250 = 31375 -程序输出。Program exited with code 027. -程序退出,调试结束。(gdb) q 好了,有了以上的感性认识,还是让我们来系统地认识一下gdb吧。top使用GDB一般来说GDB主要调试的是C/C+的程序。要调试C/C+的程序,首先在编译时,我们必须要把调试信息加到可执行文件中。使用编译器(cc/gcc/g+)的 -g 参数可以做到这一点。如: cc -g hello.c -o hello g+ -g hello.cpp -o hello如果没有-g,你将看不见程序的函数名、

10、变量名,所代替的全是运行时的内存地址。当你用-g把调试信息加入之后,并成功编译目标代码以后,让我们来看看如何用gdb来调试他。启动GDB的方法有以下几种:1、gdb program也就是你的执行文件,一般在当然目录下。2、gdb core用gdb同时调试一个运行程序和core文件,core是程序非法执行后core dump后产生的文件。3、gdb 如果你的程序是一个服务程序,那么你可以指定这个服务程序运行时的进程ID。gdb会自动attach上去,并调试他。program应该在PATH环境变量中搜索得到。GDB启动时,可以加上一些GDB的启动开关,详细的开关可以用gdb-help查看。我在下面

11、只例举一些比较常用的参数:-symbols -s 从指定文件中读取符号表。-se file从指定文件中读取符号表信息,并把他用在可执行文件中。-core -c 调试时core dump的core文件。-directory -d 加入一个源文件的搜索路径。默认搜索路径是环境变量中PATH所定义的路径。topGDB的命令概貌 启动gdb后,就你被带入gdb的调试环境中,就可以使用gdb的命令开始调试程序了,gdb的命令可以使用help命令来查看,如下所示:/home/hchen gdbGNU gdb 5.1.1Copyright 2002 Free Software Foundation, Inc

12、.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-su

13、se-linux.(gdb) helpList of classes of commands:aliases - Aliases of other commandsbreakpoints - Making program stop at certain pointsdata - Examining datafiles - Specifying and examining filesinternals - Maintenance commandsobscure - Obscure featuresrunning - Running the programstack - Examining the stackstatus - Statu

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 幼儿/小学教育 > 小学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号