GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具。或许,各位比较喜欢那种图形界面方式的,像VC、BCB等IDE的调试,但如果你是在UNIX平台下做软件,你会发现GDB这个调试工具有比VC、BCB的图形化调试器更强大的功能。所谓“寸有所长,尺有所短”就是这个道理。
一般来说,GDB主要帮忙你完成下面四个方面的功能:
1、启动你的程序,可以按照你的自定义的要求随心所欲的运行程序。 2、可让被调试的程序在你所指定的调置的断点处停住。(断点可以是条件表达式) 3、当程序被停住时,可以检查此时你的程序中所发生的事。 4、动态的改变你程序的执行环境。
从上面看来,GDB和一般的调试工具没有什么两样,基本上也是完成这些功能,不过在细节上,你会发现GDB这个调试工具的强大,大家可能比较习惯了图形化的调试工具,但有时候,命令行的调试工具却有着图形化工具所不能完成的功能。让我们一一看来。
先用vim写一个示例程序(tst.c):
#include <stdio.h> int func(int n){ int sum=0, i; for(i=0;i<n;i++) { sum+=i; } return sum;} void main(){ int i; long result=0; for(i=1;i<=100;i++) { result+=i; } printf("result[1-100]=%d \n", result); printf("result[1-250]=%d \n", func(250));} 编译生成可执行文件:
$ gcc -g tst.c -o tst # -g 选项的意思是加入调试信息使用gdb调试(感性理解):
(体验好差,可以来这里看:https://blog.csdn.net/HaoZiHu...
$ gdb tst <----------启动GDB GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git <-------显示GDB说明,不管它Copyright (C) 2018 Free Software Foundation, Inc.License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>This is free software: you are free to change and redistribute it.There is NO WARRANTY, to the extent permitted by law. Type "show copying"and "show warranty" for details.This GDB was configured as "x86_64-linux-gnu".Type "show configuration" for configuration details.For bug reporting instructions, please see:<http://www.gnu.org/software/gdb/bugs/>.Find the GDB manual and other documentation resources online at:<http://www.gnu.org/software/gdb/documentation/>.For help, type "help".Type "apropos word" to search for commands related to "word"...Reading symbols from tst...done.(gdb) l <-----------l命令相当于list,从第一行开始列出源码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 }(gdb) <-------------- 直接回车,重复上一次命令12 13 void main()14 {15 int i;16 int result=0;17 for(i=1;i<=100;i++)18 {19 result+=i;20 }21 printf("result[1-100]=%d /n", result);(gdb) break 15 <-------------- 设置断点,在源程序第15行设置断点Breakpoint 1 at 0x680: file tst.c, line 15.(gdb) break func <-------------- 设置断点,在函数func()入口处Breakpoint 2 at 0x651: file tst.c, line 5.(gdb) info break <--------------- 查看断点信息Num Type Disp Enb Address What1 breakpoint keep y 0x0000000000000680 in main at tst.c:152 breakpoint keep y 0x0000000000000651 in func at tst.c:5(gdb) r <--------------- 运行程序,run命令简写Starting program: /home/jack/demo/gdb/tst Breakpoint 1, main () at tst.c:1616 int result=0; <--------------- 在断点处停住(gdb) n <--------------- 单条语句执行,next命令简写17 for(i=1;i<=100;i++)(gdb) n19 result+=i;(gdb) n17 for(i=1;i<=100;i++)(gdb) n19 result+=i;(gdb) n17 for(i=1;i<=100;i++)(gdb) n19 result+=i;(gdb) c <--------------- 继续运行命令,continue命令简写Continuing.Breakpoint 2, func (n=250) at tst.c:55 int sum=0, i;(gdb) n6 for(i=0;i<n;i++)(gdb) n8 sum+=i;(gdb) p i <--------------- 打印变量的值 print命令缩写$1 = 0(gdb) p sum$2 = 0(gdb) bt <--------------- 查看函数堆栈#0 func (n=250) at tst.c:8#1 0x00005555555546c0 in main () at tst.c:22(gdb) finish <---------------- 退出函数Run till exit from #0 func (n=250) at tst.c:80x00005555555546c0 in main () at tst.c:2222 printf("result[1-250]=%d /n", func(250));Value returned is $3 = 31125(gdb) cContinuing.result[1-100]=5050 result[1-250]=31125 [Inferior 1 (process 7775) exited with code 026] <-------------- 程序退出,调试结束(gdb) q <--------------- 退出gdb在此处我们简单总结一下gdb的命令(<br/>是换行的意思,SF的markdown没有内嵌这个元素......):
...