一、初识GDB(Linux下调试工具GDB教程)

一、初识GDB(Linux下调试工具GDB教程)

1.GDB概述

2.调试示例

--------------

1.GDB概述

<wbr><wbr><wbr>GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具。或许,各位比较喜欢那种图形界面方式的,像VC、BCB等IDE的调试,但如果你是在UNIX平台下做软件,你会发现GDB这个调试工具有比VC、BCB的图形化调试器更强大的功能。所谓“寸有所长,尺有所短”就是这个道理。</wbr></wbr></wbr>

一般来说,GDB主要帮忙你完成下面四个方面的功能:

<wbr><wbr><wbr>1、启动你的程序,可以按照你的自定义的要求随心所欲的运行程序。<br><wbr><wbr><wbr>2、可让被调试的程序在你所指定的调置的断点处停住。(断点可以是条件表达式)<br><wbr><wbr><wbr>3、当程序被停住时,可以检查此时你的程序中所发生的事。<br><wbr><wbr><wbr>4、动态的改变你程序的执行环境。</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

从上面看来,GDB和一般的调试工具没有什么两样,基本上也是完成这些功能,不过在细节上,你会发现GDB这个调试工具的强大,大家可能比较习惯了图形化的调试工具,但有时候,命令行的调试工具却有着图形化工具所不能完成的功能。让我们一一看来。

2.调试示例

<wbr><wbr> 源程序:tst.c</wbr></wbr>

<wbr><wbr><wbr><wbr>1 #include &lt;stdio.h&gt;<br><wbr><wbr><wbr><wbr>2<br><wbr><wbr><wbr><wbr>3 int func(int n)<br><wbr><wbr><wbr><wbr>4 {<br><wbr><wbr><wbr><wbr>5<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>int sum=0,i;<br><wbr><wbr><wbr><wbr>6<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>for(i=0; i&lt;n; i++)<br><wbr><wbr><wbr><wbr>7<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr>8<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>sum+=i;<br><wbr><wbr><wbr><wbr>9<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr>10<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return sum;<br><wbr><wbr><wbr> 11 }<br><wbr><wbr><wbr> 12<br><wbr><wbr><wbr> 13<br><wbr><wbr><wbr> 14main()<br><wbr><wbr><wbr> 15 {<br><wbr><wbr><wbr>16<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>int i;<br><wbr><wbr><wbr>17<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>long result = 0;<br><wbr><wbr><wbr>18<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>for(i=1; i&lt;=100; i++)<br><wbr><wbr><wbr>19<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr>20<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>result += i;<br><wbr><wbr><wbr>21<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr> 22<br><wbr><wbr><wbr>23<wbr><wbr><wbr><wbr><wbr><wbr><wbr>printf(“result[1-100] = %d /n”, result );<br><wbr><wbr><wbr>24<wbr><wbr><wbr><wbr><wbr><wbr><wbr>printf(“result[1-250] = %d /n”, func(250) );<br><wbr><wbr><wbr> 25 }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

编译生成执行文件:(Linux下)
<wbr><wbr><wbr>hchen/test&gt; cc -g tst.c -o tst</wbr></wbr></wbr>

使用GDB调试:

hchen/test> gdb tst<wbr>&lt;———- 启动GDB<br> GNU gdb 5.1.1<br> Copyright 2002 Free Software Foundation, Inc.<br> GDB is free software, covered by the GNU General Public License,and you are<br> welcome to change it and/or distribute copies of it under certainconditions.<br> Type “show copying” to see the conditions.<br> There is absolutely no warranty for GDB.<wbr> Type“show warranty” for details.<br> This GDB was configured as “i386-suse-linux”…<br> (gdb)l<wbr><wbr><wbr><wbr>&lt;——————– l命令相当于list,从第一行开始例出原码。<br> 1<wbr><wbr><wbr><wbr><wbr><wbr><wbr>#include &lt;stdio.h&gt;<br> 2<br> 3<wbr><wbr><wbr><wbr><wbr><wbr><wbr>int func(int n)<br> 4<wbr><wbr><wbr><wbr><wbr><wbr><wbr>{<br> 5<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>int sum=0,i;<br> 6<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>for(i=0; i&lt;n; i++)<br> 7<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>{<br> 8<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>sum+=i;<br> 9<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br> 10<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>return sum;<br> (gdb)<wbr><wbr><wbr><wbr><wbr><wbr>&lt;——————– 直接回车表示,重复上一次命令<br> 11<wbr><wbr><wbr><wbr><wbr><wbr>}<br> 12<br> 13<br> 14<wbr><wbr><wbr><wbr><wbr><wbr>main()<br> 15<wbr><wbr><wbr><wbr><wbr><wbr>{<br> 16<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>int i;<br> 17<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>long result = 0;<br> 18<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>for(i=1; i&lt;=100; i++)<br> 19<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>{<br> 20<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>result += i;<wbr><wbr><br> (gdb) break16<wbr><wbr><wbr>&lt;——————– 设置断点,在源程序第16行处。<br> Breakpoint 1 at 0×8048496: file tst.c, line 16.<br> (gdb) break func<wbr> &lt;——————–设置断点,在函数func()入口处。<br> Breakpoint 2 at 0×8048456: file tst.c, line 5.<br> (gdb) info break<wbr> &lt;——————–查看断点信息。<br> NumType<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>Disp EnbAddress<wbr><wbr><wbr>What<br> 1<wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

你可能感兴趣的:(linux)