GDB工具如何使用断点信息

来源:互联网 发布:java软件工程师一个月挣多少钱 编辑:程序博客网 时间:2024/05/21 16:55
  在Linux平台下,使用最多的就是调试工具GDB.通过命令gcc -g test.c 。默认会生成a.out文件,这个文件由于参数-g的原因加入了调试信息。所以可以使用gdb来加载并调试,但是在使用断点的时候,你会很郁闷。真的。比如下面先来看这个过程。

/******
test.c 代码
********/

   1 #include <stdio.h>
  2
  3 void docout()
  4 {
  5
  6 }
  7 int main(void)
  8 {
  9   
10   int b;
11   printf("%p\n",&b);
12   docout();
13   return 0;
14 }


执行命令 gcc -g test.c  输出:
[root@yongxin ~]# gcc -g test.c
[root@yongxin ~]# ls
anaconda-ks.cfg  Download               nautilus-debug-log.txt  test.c
a.out            Firefox_wallpaper.png  Pictures                Videos
crossplatform    install.log            Projects
Desktop          install.log.syslog     Public
Documents        Music                  Templates

再执行gdb  a.out 设置断点信息 breakpoint
2
3       void docout()
4       {
5         
6       }
7       int main(void)
8       {
9
10        int b;
11        printf("%p\n",&b);
(gdb) break 10
Breakpoint 1 at 0x80483da: file test.c, line 10.
(gdb) break 12
Breakpoint 2 at 0x80483ed: file test.c, line 12.
(gdb) infobreak
Undefined command: "infobreak".  Try "help".
(gdb) info break
Num Type           Disp Enb Address    What
1   breakpoint     keep y   0x080483da in main at test.c:10
2   breakpoint     keep y   0x080483ed in main at test.c:12

可以运行,没问题。但是这些断点信息是没有办法保持的。下次进来的时候,必须重新设置断点。当你要频繁退出的时候,就会非常烦。特别是断点特别多的情况。气死你。
所以解决办法就是将打断点的命令(breakpoint 行号)写到一个文件里面去 ,在初始化的时候自动加载。
  比如文件break.list
          breakpoint 10
          breakpoint 12
然后通过命令   gdb a.out  -x break.list -tui     加载这个程序
然后 info break  你就可以看到所有加载的断点了。
完毕。