Chinaunix首页 | 论坛 | 博客
  • 博客访问: 810954
  • 博文数量: 274
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 862
  • 用 户 组: 普通用户
  • 注册时间: 2015-10-24 15:31
个人简介

不合格的程序猿

文章分类

全部博文(274)

文章存档

2019年(3)

2018年(1)

2017年(4)

2016年(160)

2015年(106)

我的朋友

分类: LINUX

2016-06-22 21:08:27

硬件断点使用watch监测,可以监测栈变量和堆变量值的变化,当被监测变量值发生变化时,程序被停住。

1.    栈变量
测试代码(文件名为1.c,比较low,哈哈):

点击(此处)折叠或打开

  1. #include <string.h>

  2. void test(void)
  3. {
  4.         int iA, iB, iC;

  5.         iA = 1;
  6.         iB = 2;
  7.         iC = 3;

  8.         iB += iA + iC;
  9.         printf("iB = %d\n", iB);
  10.         iB = 0;
  11.         printf("iB = %d\n", iB);
  12.         iB *= iC;
  13.         printf("iB = %d\n", iB);

  14.         return;
  15. }

  16. void main(void)
  17. {
  18.         test();

  19.         return;
  20. }
测试过程(监测变量iB值在代码中发生变化的位置):

点击(此处)折叠或打开

  1. [root@ceph181 test]# vim 1.c
  2. [root@ceph181 test]# gcc -g 1.c
  3. [root@ceph181 test]# ls
  4. 1.c a.out debug.c log.c mmap.c sscanf.c sync_fetch.c time.c unlikely.c
  5. [root@ceph181 test]# gdb a.out
  6. GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1)
  7. Copyright (C) 2010 Free Software Foundation, Inc.
  8. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  9. This is free software: you are free to change and redistribute it.
  10. There is NO WARRANTY, to the extent permitted by law. Type "show copying"
  11. and "show warranty" for details.
  12. This GDB was configured as "x86_64-redhat-linux-gnu".
  13. For bug reporting instructions, please see:
  14. <http://www.gnu.org/software/gdb/bugs/>...
  15. Reading symbols from /home/work/test/a.out...done.
  16. (gdb) b test
  17. Breakpoint 1 at 0x4004cc: file 1.c, line 8.
  18. (gdb) r
  19. Starting program: /home/work/test/a.out

  20. Breakpoint 1, test () at 1.c:8
  21. 8        iA = 1;
  22. Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.132.el6.x86_64
  23. (gdb) watch iB
  24. Hardware watchpoint 2: iB
  25. (gdb) c
  26. Continuing.
  27. Hardware watchpoint 2: iB

  28. Old value = 4195296
  29. New value = 2
  30. test () at 1.c:10
  31. 10        iC = 3;
  32. (gdb) c
  33. Continuing.
  34. Hardware watchpoint 2: iB

  35. Old value = 2
  36. New value = 6
  37. test () at 1.c:13
  38. 13        printf("iB = %d\n", iB);
  39. (gdb) c
  40. Continuing.
  41. iB = 6
  42. Hardware watchpoint 2: iB

  43. Old value = 6
  44. New value = 0
  45. test () at 1.c:15
  46. 15        printf("iB = %d\n", iB);
  47. (gdb) c
  48. Continuing.
  49. iB = 0
  50. iB = 0

  51. Watchpoint 2 deleted because the program has left the block in
  52. which its expression is valid.
  53. main () at 1.c:26
  54. 26        return;
  55. (gdb) c
  56. Continuing.

  57. Program exited with code 07.
  58. (gdb)
注意: 栈变量的生命周期有限,因此,使用watch监测其硬件断点时,要注意生命周期

2.    堆变量
int *piA = malloc(sizeof(int));
监测堆变量piA值的变化时,
    1)    在gdb中打印出变量piA的地址: print  &piA,记为piB;
    2)    watch *piB
    3)    continue
如下例所示,要监测指针connection->session的值在什么时候被修改:

点击(此处)折叠或打开

  1. Breakpoint 1, xio_connection_destroy (connection=0x7ffff0009240) at ../common/xio_connection.c:2364
  2. 2364        int            retval = 0;
  3. 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
  4. (gdb) p connection->session
  5. $1 = (struct xio_session *) 0x6120b0
  6. (gdb) p &connection->session
  7. $2 = (struct xio_session **) 0x7ffff0009298
  8. (gdb) watch *($2)
  9. Hardware watchpoint 2: *($2)
  10. (gdb) disable 1
  11. (gdb) c
  12. Continuing.
  13. Hardware watchpoint 2: *($2)

  14. Old value = (struct xio_session *) 0x6120b0
  15. New value = (struct xio_session *) 0x0
  16. 0x0000003917805e94 in rdma_get_cm_event () from /usr/lib64/librdmacm.so.1
  17. (gdb)

阅读(10768) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~