硬件断点使用watch监测,可以监测栈变量和堆变量值的变化,当被监测变量值发生变化时,程序被停住。
1. 栈变量
测试代码(文件名为1.c,比较low,哈哈):
-
#include <string.h>
-
-
void test(void)
-
{
-
int iA, iB, iC;
-
-
iA = 1;
-
iB = 2;
-
iC = 3;
-
-
iB += iA + iC;
-
printf("iB = %d\n", iB);
-
iB = 0;
-
printf("iB = %d\n", iB);
-
iB *= iC;
-
printf("iB = %d\n", iB);
-
-
return;
-
}
-
-
void main(void)
-
{
-
test();
-
-
return;
-
}
测试过程(监测变量iB值在代码中发生变化的位置):
-
[root@ceph181 test]# vim 1.c
-
[root@ceph181 test]# gcc -g 1.c
-
[root@ceph181 test]# ls
-
1.c a.out debug.c log.c mmap.c sscanf.c sync_fetch.c time.c unlikely.c
-
[root@ceph181 test]# gdb a.out
-
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)
-
Copyright (C) 2010 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-redhat-linux-gnu".
-
For bug reporting instructions, please see:
-
<http://www.gnu.org/software/gdb/bugs/>...
-
Reading symbols from /home/work/test/a.out...done.
-
(gdb) b test
-
Breakpoint 1 at 0x4004cc: file 1.c, line 8.
-
(gdb) r
-
Starting program: /home/work/test/a.out
-
-
Breakpoint 1, test () at 1.c:8
-
8 iA = 1;
-
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64
-
(gdb) watch iB
-
Hardware watchpoint 2: iB
-
(gdb) c
-
Continuing.
-
Hardware watchpoint 2: iB
-
-
Old value = 4195296
-
New value = 2
-
test () at 1.c:10
-
10 iC = 3;
-
(gdb) c
-
Continuing.
-
Hardware watchpoint 2: iB
-
-
Old value = 2
-
New value = 6
-
test () at 1.c:13
-
13 printf("iB = %d\n", iB);
-
(gdb) c
-
Continuing.
-
iB = 6
-
Hardware watchpoint 2: iB
-
-
Old value = 6
-
New value = 0
-
test () at 1.c:15
-
15 printf("iB = %d\n", iB);
-
(gdb) c
-
Continuing.
-
iB = 0
-
iB = 0
-
-
Watchpoint 2 deleted because the program has left the block in
-
which its expression is valid.
-
main () at 1.c:26
-
26 return;
-
(gdb) c
-
Continuing.
-
-
Program exited with code 07.
-
(gdb)
注意: 栈变量的生命周期有限,因此,使用watch监测其硬件断点时,要注意生命周期
2. 堆变量
int *piA = malloc(sizeof(int));
监测堆变量
piA值的变化时,
1) 在gdb中打印出变量
piA的地址: print &
piA,记为piB;
2) watch *piB
3) continue
如下例所示,要监测指针connection->session的值在什么时候被修改:
-
Breakpoint 1, xio_connection_destroy (connection=0x7ffff0009240) at ../common/xio_connection.c:2364
-
2364 int retval = 0;
-
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64 libibverbs-1.1.8mlnx1-OFED.3.1.1.0.0.x86_64 libmlx4-1.0.6mlnx1-OFED.3.1.1.0.0.x86_64 libmlx5-1.0.2mlnx1-OFED.3.1.1.0.3.x86_64 libnl-1.1.4-2.el6.x86_64 librdmacm-1.0.21mlnx-OFED.3.0.1.5.2.x86_64 numactl-2.0.7-8.el6.x86_64
-
(gdb) p connection->session
-
$1 = (struct xio_session *) 0x6120b0
-
(gdb) p &connection->session
-
$2 = (struct xio_session **) 0x7ffff0009298
-
(gdb) watch *($2)
-
Hardware watchpoint 2: *($2)
-
(gdb) disable 1
-
(gdb) c
-
Continuing.
-
Hardware watchpoint 2: *($2)
-
-
Old value = (struct xio_session *) 0x6120b0
-
New value = (struct xio_session *) 0x0
-
0x0000003917805e94 in rdma_get_cm_event () from /usr/lib64/librdmacm.so.1
-
(gdb)
阅读(10750) | 评论(0) | 转发(0) |