有关gdb调试watch

原文件编译的时候要加-g,例gcc -g -o test test.c, 这样在能在调试的时候用list,watch等命令。

全局变量或当前堆栈区可见的变量才能watch,watch之后用Continuing,就可显示观察变量。



gdb watch 跟踪变量的变化


01 #include <stdio.h>
02 #include <stdlib.h>
03 #include <string.h>
04 
05 int main()
06 {
07     char * name = NULL;
08     int len = 10;
09 
10     name = (char *)malloc(len);
11     strncpy(name, "zengxiaolong", len);
12 
13     char ** wild_pointer;
14     wild_pointer = &name;          // 野指针,指向了变量name
15     *wild_pointer = (char *)0x100; // 野指针,破坏了变量name
16 
17     name[0] = 'a';
18     return 0;
19 }

# gcc test.c -o test -g
# gdb -q test
(gdb) l
1    #include <stdio.h>
2    #include <stdlib.h>
3    #include <string.h>
4    
5    int main()
6    {
7        char * name = NULL;
8        int len = 10;
9    
10        name = (char *)malloc(len);
(gdb) b 7
Breakpoint 1 at 0x80483b5: file test.c, line 7.
(gdb) r
Starting program: /tftpboot/test 

Breakpoint 1, main () at test.c:7
7        char * name = NULL;
(gdb) watch name // 跟踪变量name变化情况
Hardware watchpoint 2: name // 说明该系统结构支持硬件断点
(gdb) c
Continuing.
Hardware watchpoint 2: name

Old value = 0xb7f79dc0 "U\211�WVS��y" // 这里变量name发生了变化
New value = 0x804a008 ""

main () at test.c:11
11        strncpy(name, "zengxiaolong", len);
(gdb) c
Continuing.
Hardware watchpoint 2: name

Old value = 0x804a008 "zengxiaolo"
New value = 0x100 <Address 0x100 out of bounds> // 指针越界了

main () at test.c:17
17        name[0] = 'a';
(gdb) c
Continuing.

Program received signal SIGSEGV, Segmentation fault.
0x080483fd in main () at test.c:17
17        name[0] = 'a';
(gdb) 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值