GDB是linux上的调试利器,是我们每个基于linux系统编程coder必须掌握的一门技术。在此记录一个使用gdb条件断点的小例子:
gdb条件调试一般应用在循环、链表的遍历、或者其他变量的值可能被多次改变的场合。先上一个例子:
调试用例
gdb_condition.c源码:
1/***************************************************
2 *Author: Robin
3 *Mail: liuyalong.email@gmail.com
4 *Description:
5***************************************************/
6
7#include <stdio.h>
8
9void show (const char* const cheers){
10 const int count = 10;
11 int i = 0;
12 for (i; i < count; ++i){
13 printf ("%d\t%s\n", i, cheers);
14 }
15 }
16
17int main(int argc, char** argv, char** envp){
18 show ("中国达人秀,加油!!!!!");
19 return 0;
20 }
编译方法:
robin@ubuntu:/media/2ndDisk/workspace/c_wspace$gcc -g gdb_condition.c -o gdb_condition
调试方法:
1.启动gdb:
robin@ubuntu:~/workspace/c_wspace$ gdb./gdb_condition
GNU gdb (Ubuntu/Linaro7.4-2012.04-0ubuntu2.1) 7.4-2012.04
Copyright (C) 2012 Free SoftwareFoundation, Inc.
License GPLv3+: GNU GPL version 3 or later<http://gnu.org/licenses/gpl.html>
This is free software: you are free tochange and redistribute it.
There is NO WARRANTY, to the extentpermitted by law. Type "showcopying"
and "show warranty" for details.
This GDB was configured as"i686-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from/media/2ndDisk/workspace/c_wspace/gdb_condition...done.
(gdb)
2.设置条件断点:
(gdb) b 13 if i == 8
Breakpoint 1 at 0x80483fa: filegdb_condition.c, line 13.
(gdb)
3.在gdb中运行程序:
(gdb) r
Starting program:/media/2ndDisk/workspace/c_wspace/gdb_condition
0 中国达人秀,加油!!!!!
1 中国达人秀,加油!!!!!
2 中国达人秀,加油!!!!!
3 中国达人秀,加油!!!!!
4 中国达人秀,加油!!!!!
5 中国达人秀,加油!!!!!
6 中国达人秀,加油!!!!!
7 中国达人秀,加油!!!!!
Breakpoint 1, show (cheers=0x8048518 "中国达人秀,加油!!!!!")
at gdb_condition.c:13
13 printf ("%d\t%s\n", i,cheers);
(gdb)
我们可以看到,程序输出了前面7个,也就是说i=8时,程序自动设置了断点,并且停在了给i赋值为8的操作上,即,i=7;++i;此时我们可以查看断点位置处,程序的状态,可以打印i的值,cheers的值:
(gdb) p i
$1 = 8
(gdb) p cheers
$2 = 0x8048518 "中国达人秀,加油!!!!!"
(gdb)
条件调试容易出的问题:
1. 设置断点时,把if == 8错设为 if=8,这样会导致一直在该断点:
(gdb) b 13 if i = 8
Breakpoint 1 at 0x80483fa: filegdb_condition.c, line 13.
(gdb) r
Starting program:/media/2ndDisk/workspace/c_wspace/gdb_condition
Breakpoint 1, show (cheers=0x8048518 "中国达人秀,加油!!!!!")
at gdb_condition.c:13
13 printf ("%d\t%s\n", i,cheers);
(gdb) c
Continuing.
8 中国达人秀,加油!!!!!
Breakpoint 1, show (cheers=0x8048518 "中国达人秀,加油!!!!!")
at gdb_condition.c:13
13 printf ("%d\t%s\n", i,cheers);
(gdb) c
Continuing.
8 中国达人秀,加油!!!!!
Breakpoint 1, show (cheers=0x8048518 "中国达人秀,加油!!!!!")
at gdb_condition.c:13
13 printf ("%d\t%s\n", i,cheers);
(gdb) c
Continuing.
8 中国达人秀,加油!!!!!
Breakpoint 1, show (cheers=0x8048518 "中国达人秀,加油!!!!!")
at gdb_condition.c:13
13 printf ("%d\t%s\n", i,cheers);
(gdb)
2. 在条件变量i的作用域外设置断点,这样会导致断点设置无效(下面的结果是因为,我设置断点到第10行,但是i是在第11行才有的,所以断点无效):
(gdb) b 10 if i == 8
Breakpoint 3 at 0x80483ea: filegdb_condition.c, line 10.
(gdb) r
Starting program:/media/2ndDisk/workspace/c_wspace/gdb_condition
0 中国达人秀,加油!!!!!
1 中国达人秀,加油!!!!!
2 中国达人秀,加油!!!!!
3 中国达人秀,加油!!!!!
4 中国达人秀,加油!!!!!
5 中国达人秀,加油!!!!!
6 中国达人秀,加油!!!!!
7 中国达人秀,加油!!!!!
8 中国达人秀,加油!!!!!
9 中国达人秀,加油!!!!!
[Inferior 1 (process 3562) exited normally]
(gdb)